提交 6891f5af authored 作者: blu's avatar blu

init

上级 5cee9924
...@@ -3,14 +3,18 @@ ...@@ -3,14 +3,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <mutex> #include <mutex>
#include <map> #include <map>
#include <vector>
#include <spdlog/spdlog.h>
#include <json.hpp>
using namespace std; using namespace std;
using json = nlohmann::json;
namespace DB { namespace DB {
map<string, sqlite3 *> maphdb; map<string, sqlite3 *> maphdb;
map<string, mutex> mapMut; map<string, mutex> mapMut;
//typedef int (*callback)(void*,int,char**,char**);
int exec(char* fileName, const char* stmt, callback cb, void *pUserData){ int exec(void *pUserData, const char* fileName, const char* stmt, callback cb){
int ret = 0; int ret = 0;
if(fileName == NULL||strlen(fileName) == 0) { if(fileName == NULL||strlen(fileName) == 0) {
fileName = const_cast<char*>("default.db"); fileName = const_cast<char*>("default.db");
...@@ -30,7 +34,7 @@ namespace DB { ...@@ -30,7 +34,7 @@ namespace DB {
ret = sqlite3_open(fileName,&ppdb); ret = sqlite3_open(fileName,&ppdb);
if(ret != SQLITE_OK) if(ret != SQLITE_OK)
{ {
printf("sqlite3_open:%s\n",sqlite3_errmsg(ppdb)); spdlog::error("sqlite3_open: {}",sqlite3_errmsg(ppdb));
exit(1); exit(1);
} }
} }
...@@ -44,11 +48,138 @@ namespace DB { ...@@ -44,11 +48,138 @@ namespace DB {
ret = sqlite3_exec(ppdb, stmt, cb, pUserData, NULL); ret = sqlite3_exec(ppdb, stmt, cb, pUserData, NULL);
if(ret != SQLITE_OK) if(ret != SQLITE_OK)
{ {
printf("error: sqlite3_exec:%s\n",sqlite3_errmsg(ppdb)); spdlog::error("sqlite3_exec: {}",sqlite3_errmsg(ppdb));
} }
} }
//sqlite3_close(ppdb); //sqlite3_close(ppdb);
return ret; return ret;
} }
int _getSlices(void *pUser, int cc, char **cv, char **cn) {
auto v = static_cast< vector<int>* >(pUser);
if(cc != v->size()) {
return SQLITE_ERROR;
}else{
for(int i = 0; i < v->size(); i ++) {
v->at(i) = atoi(cv[0]);
}
}
}
/*
{
"code":0,
"time":0,
"data":{
"ipc":"172.31.0.51",
"username":"admin",
"password":"FWBWTU",
"services":{
"evmgr":{
"sn":"ILS-1",
"addr":"0.0.0.0",
"port-pub":5556,
"port-rep":5557,
"iid":1
},
"evpuller":{
"sn":"ILS-2",
"addr":"0.0.0.0",
"port-pub":5556,
"port-rep":5557,
"iid":2
},
"evpusher":[
{
"sn":"ILS-2",
"addr":"localhost",
"iid":2,
"enabled":1,
"urlDest":"rtsp://40.73.41.176:554/test1"
}
],
"evslicer":[
{
"sn":"ILS-3",
"addr":"192.168.0.25",
"iid":3,
"path": "/var/lib/slices/"
}
],
"evml":[
{
"feature":"motion",
"sn":"ILS-4",
"addr":"192.168.0.26",
"iid":4
}
]
}
}
}
*/
// tables: device, evmgr, evpuller, evpusher, evslice, evml, ipc, log
// schemas:
// info: id, cls text, value text, version, update datetime
// ex: 1, sn, ILS112233, NULL, NULL
// 2, evmgr, NULL, 1.2, 2019-09-02
// 3, evpuller, NULL, 1.2, 2019-09-02
// ipc: id, user, passwd, addr, status
// ex: 1, admin, FWBWTU, 172.31.0.51, 0
// evmgr:
// evpuller: iid, cid, addr, pub, rep, status;
// evpusher: iid, cid, addr, urldest, enabled, status;
// evslicer: iid, cid, addr, urldest, days, miniutes, status;
// evml: iid, cid, type, addr, enabled, status
//
//
int setLocalConfig(json config, const char* fileName) {
int ret;
string stmt;
// init tables;
stmt = "create table if not exists info(cls text, value text, version text, update datetime, primary key (cls, value));";
ret = exec(NULL, fileName, stmt.c_str(), NULL);
if(ret != SQLITE_OK) {
return ret;
}
}
int getLocalConfig(json config) {
}
int getSlices(void *pUser, int iid, const char *fileName) {
int ret = 0;
auto v = static_cast< vector<int>* >(pUser);
string stmt = "select ts from slices where iid="+to_string(iid)+" order by id;";
ret = exec(pUser, fileName, stmt.c_str(), _getSlices);
if(ret != SQLITE_OK) {
// create
stmt = "create table if not exists slices(id integer, iid integer, ts integer);";
ret = exec(NULL, fileName, stmt.c_str(), NULL);
if(ret != SQLITE_OK) {
spdlog::error("failed create table slices for evslicer {}", iid);
return ret;
}else{
for(int i = 1; i <= v->size(); i ++) {
stmt = "insert into slices(id, iid, ts) values(" + to_string(i) + to_string(iid) + ", 0";
ret = exec(NULL, fileName, stmt.c_str(), NULL);
if(ret != SQLITE_OK) return ret;
v->push_back(0);
}
stmt = "update slices set ts=1 where id = 1 and iid="+to_string(iid) +";";
ret = exec(NULL, stmt.c_str(), NULL, NULL);
if(ret != SQLITE_OK) return ret;
v->at(0) = 2;
}
}else{
}
return SQLITE_OK;
}
} }
\ No newline at end of file
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <vector>
#ifdef OS_LINUX #ifdef OS_LINUX
#include <filesystem> #include <filesystem>
...@@ -27,15 +28,16 @@ private: ...@@ -27,15 +28,16 @@ private:
#define MINUTES_PER_SLICE_DEFAULT 10 #define MINUTES_PER_SLICE_DEFAULT 10
// 2 days, 10 minutes per record // 2 days, 10 minutes per record
#define NUM_SLICES_DEFAULT (24 * NUM_DAYS_DEFAULT * 60 / MINUTES_PER_SLICE_DEFAULT) #define NUM_SLICES_DEFAULT (24 * NUM_DAYS_DEFAULT * 60 / MINUTES_PER_SLICE_DEFAULT)
void *pSubCtx = NULL, *pReqCtx = NULL; // for packets relay void *pSubCtx = NULL, *pReqCtx = NULL; // for packets relay
void *pSub = NULL, *pReq = NULL; void *pSub = NULL, *pReq = NULL;
string urlOut, urlPub, urlRep, sn; string urlOut, urlPub, urlRep, sn;
int iid, days, minutes, numSlices; int iid, days, minutes, numSlices, lastSliceId;
bool enablePush = false; bool enablePush = false;
int *streamList = NULL; int *streamList = NULL;
AVFormatContext *pAVFormatRemux = NULL; AVFormatContext *pAVFormatRemux = NULL;
AVFormatContext *pAVFormatInput = NULL; AVFormatContext *pAVFormatInput = NULL;
// load from db
vector<int> *sliceIdxToName = NULL;
int init() int init()
{ {
...@@ -81,6 +83,11 @@ private: ...@@ -81,6 +83,11 @@ private:
} }
numSlices = 24 * days * 60 /minutes; numSlices = 24 * days * 60 /minutes;
// alloc memory
sliceIdxToName = new vector<int>(numSlices);
// load db
DB::exec(NULL, "select id, ts, last from slices;", DB::get_slices, sliceIdxToName);
break; break;
} }
} }
...@@ -91,7 +98,7 @@ private: ...@@ -91,7 +98,7 @@ private:
} }
if(bcnt || urlOut.empty()) { if(bcnt || urlOut.empty()) {
// TODO: waiting for command // TODO: waiting for command
spdlog::warn("evpusher {} {} waiting for command", sn, iid); spdlog::warn("evpusher {} {} waiting for command & retrying", sn, iid);
this_thread::sleep_for(chrono::milliseconds(1000*20)); this_thread::sleep_for(chrono::milliseconds(1000*20));
continue; continue;
} }
......
...@@ -6,8 +6,8 @@ extern "C" { ...@@ -6,8 +6,8 @@ extern "C" {
namespace DB { namespace DB {
typedef int (*callback)(void*,int,char**,char**); typedef int (*callback)(void*,int,char**,char**);
int exec(char* fileName, const char* stmt, callback cb, void *pUserData); int exec(void *pUserData, char* fileName, const char* stmt, callback cb);
int getSlices(void *pUser, int iid, const char *fileName);
} }
#endif #endif
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论