提交 f7aa0a70 authored 作者: blu's avatar blu

init

上级 c12209ff
...@@ -7,8 +7,10 @@ CFLAGS = $(DEBUG) -Wall ...@@ -7,8 +7,10 @@ CFLAGS = $(DEBUG) -Wall
LIBOPENCV = `pkg-config opencv --cflags --libs` LIBOPENCV = `pkg-config opencv --cflags --libs`
LIBFFMPEG = `pkg-config libavformat libavutil libavcodec libswscale --cflags --libs` LIBFFMPEG = `pkg-config libavformat libavutil libavcodec libswscale --cflags --libs`
LIBS=-Ivendor/lib -lpthread -lleveldb LD_FLAGS=-Lvendor/lib -lpthread -lrocksdb
#-static
HEADERS=-Iinc -Ivendor/include HEADERS=-Iinc -Ivendor/include
LIBS=
#SQLITE_SRC=vendor/sqlite/sqlite3. #SQLITE_SRC=vendor/sqlite/sqlite3.
#SQLITE=sqlite3.o #SQLITE=sqlite3.o
SQLITE_SRC= SQLITE_SRC=
...@@ -21,22 +23,22 @@ sqlite3.o: vendor/sqlite/sqlite3.c ...@@ -21,22 +23,22 @@ sqlite3.o: vendor/sqlite/sqlite3.c
gcc -D SQLITE_THREADSAFE=1 -c vendor/sqlite/sqlite3.c gcc -D SQLITE_THREADSAFE=1 -c vendor/sqlite/sqlite3.c
evmgr: evmgr.cpp database.cpp inc/common.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE_SRC) evmgr: evmgr.cpp database.cpp inc/common.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE_SRC)
$(CPP) $(CPPFLAGS) -o evmgr evmgr.cpp $(SQLITE) database.cpp $(HEADERS) $(LIBFFMPEG) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS) $(CPP) $(CPPFLAGS) $(LD_FLAGS) -o evmgr evmgr.cpp $(SQLITE) database.cpp $(HEADERS) $(LIBFFMPEG) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evpuller: evpuller.cpp database.cpp inc/common.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE_SRC) evpuller: evpuller.cpp database.cpp inc/common.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE_SRC)
$(CPP) $(CPPFLAGS) -o evpuller evpuller.cpp $(SQLITE) database.cpp $(HEADERS) $(LIBFFMPEG) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS) $(CPP) $(CPPFLAGS) $(LD_FLAGS) -o evpuller evpuller.cpp $(SQLITE) database.cpp $(HEADERS) $(LIBFFMPEG) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evpusher: evpusher.cpp inc/common.hpp inc/tinythread.hpp inc/database.h inc/zmqhelper.hpp database.cpp $(SQLITE_SRC) evpusher: evpusher.cpp inc/common.hpp inc/tinythread.hpp inc/database.h inc/zmqhelper.hpp database.cpp $(SQLITE_SRC)
$(CPP) $(CPPFLAGS) -o evpusher evpusher.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS) $(CPP) $(CPPFLAGS) $(LD_FLAGS) -o evpusher evpusher.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evslicer: evslicer.cpp inc/common.hpp inc/tinythread.hpp inc/database.h inc/zmqhelper.hpp database.cpp $(SQLITE_SRC) evslicer: evslicer.cpp inc/common.hpp inc/tinythread.hpp inc/database.h inc/zmqhelper.hpp database.cpp $(SQLITE_SRC)
$(CPP) $(CPPFLAGS) -o evslicer evslicer.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS) $(CPP) $(CPPFLAGS) $(LD_FLAGS) -o evslicer evslicer.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evmlmotion: evmlmotion.cpp inc/common.hpp inc/avcvhelpers.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp database.cpp $(SQLITE_SRC) evmlmotion: evmlmotion.cpp inc/common.hpp inc/avcvhelpers.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp database.cpp $(SQLITE_SRC)
$(CPP) $(CPPFLAGS) -o evmlmotion evmlmotion.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) $(LIBOPENCV) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS) $(CPP) $(CPPFLAGS) $(LD_FLAGS) -o evmlmotion evmlmotion.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) $(LIBOPENCV) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
rtspr: rtsp-relay.cpp rtspr: rtsp-relay.cpp
$(CPP) $(CPPFLAGS) -o rtspr rtsp-relay.cpp $(LIBFFMPEG) $(LIBS) $(CPP) $(CPPFLAGS) $(LD_FLAGS) -o rtspr rtsp-relay.cpp $(LIBFFMPEG) $(LD_FLAGS)
cvsample: cvsample.cpp cvsample: cvsample.cpp
$(CPP) $(CPPFLAGS) -o cvsample cvsample.cpp $(LIBOPENCV) $(CPP) $(CPPFLAGS) -o cvsample cvsample.cpp $(LIBOPENCV)
......
...@@ -5,7 +5,34 @@ ...@@ -5,7 +5,34 @@
#include <mutex> #include <mutex>
#include <map> #include <map>
using namespace leveldb; using namespace rocksdb;
string _config_default_tmpl = "{\"time\":0,\"code\":0,\"data\":{\"<SN_MGR>\":{\"sn\":\"<SN_MGR>\",\"addr\":\"127.0.0.1\",\"addr-cloud\":\"<cloud_addr>\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"FWBWTU\",\"status\":0,\"modules\":{\"evpuller\":[{\"sn\":\"<SN_PULLER>\",\"addr\":\"127.0.0.1\",\"iid\":1,\"port-pub\":5556,\"status\":0}],\"evpusher\":[{\"sn\":\"<SN_PUSHER>\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":0}],\"evslicer\":[{\"sn\":\"<SN_SLICER>\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":0}],\"evml\":[{\"type\":\"motion\",\"sn\":\"<SN_ML>\",\"iid\":1,\"enabled\":1,\"status\":0}]}}]}}}";
const string _sn_tmpl[] = {"<SN_MGR>", "<SN_PULLER>", "<SN_PUSHER>", "<SN_SLICER>", "<SN_ML>"};
const string _addr_tmpl[] = {"<ADDR_CAMERA>"};
// TODO:
string getStrRand(int length)
{
static bool bRand = false;
if(!bRand) {
srand(time(NULL));
bRand = true;
}
static string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result;
result.resize(length);
srand(time(NULL));
for (int i = 0; i < length; i++)
result[i] = charset[rand() % charset.length()];
return result;
}
namespace LVDB { namespace LVDB {
map<string, DB*> mappDB; map<string, DB*> mappDB;
DB *_getDB(string fileName) { DB *_getDB(string fileName) {
...@@ -45,9 +72,9 @@ namespace LVDB { ...@@ -45,9 +72,9 @@ namespace LVDB {
int getValue(string &value, string key, string fileName, cb_verify_str cb) { int getValue(string &value, string key, string fileName, cb_verify_str cb) {
int ret = 0; int ret = 0;
DB* pdb = _getDB(fileName); DB* pdb = _getDB(fileName);
Status s = pdb->Get(leveldb::ReadOptions(), key, &value); Status s = pdb->Get(ReadOptions(), key, &value);
if(!s.ok()) { if(!s.ok()) {
spdlog::error("failed to get {} from {}: {}",key, fileName, s.ToString()); spdlog::debug("failed to get {} from {}: {}",key, fileName, s.ToString());
return -1; return -1;
} }
if(cb != NULL) { if(cb != NULL) {
...@@ -57,7 +84,6 @@ namespace LVDB { ...@@ -57,7 +84,6 @@ namespace LVDB {
return ret; return ret;
} }
int setValue(const string &value, string key, string fileName, cb_verify_str cb) { int setValue(const string &value, string key, string fileName, cb_verify_str cb) {
int ret = 0; int ret = 0;
if(cb != NULL) { if(cb != NULL) {
...@@ -71,17 +97,17 @@ namespace LVDB { ...@@ -71,17 +97,17 @@ namespace LVDB {
string oldVal; string oldVal;
Status s = pdb->Get(ReadOptions(), key, &oldVal); Status s = pdb->Get(ReadOptions(), key, &oldVal);
if(!s.ok()) { if(!s.ok()) {
spdlog::warn("get old {} error {}:{}", key, fileName, s.ToString()); spdlog::debug("get old {} error {}:{}", key, fileName, s.ToString());
} }
s = pdb->Put(leveldb::WriteOptions(), key, value); s = pdb->Put(WriteOptions(), key, value);
if(!s.ok()) { if(!s.ok()) {
spdlog::error("failed to put {} -> {}: {}", key, value, s.ToString()); spdlog::error("failed to put {} -> {}: {}", key, value, s.ToString());
return -2; return -2;
} }
if(!oldVal.empty()) { if(!oldVal.empty()) {
s = pdb->Put(leveldb::WriteOptions(), key+LVDB_KEY_SUFFIX_BACK, oldVal); s = pdb->Put(WriteOptions(), key + LVDB_KEY_SUFFIX_BACK, oldVal);
if(!s.ok()) { if(!s.ok()) {
spdlog::error("failed to put backup {} -> {}: {}", key, oldVal, s.ToString()); spdlog::error("failed to put backup {} -> {}: {}", key, oldVal, s.ToString());
return -2; return -2;
...@@ -92,15 +118,6 @@ namespace LVDB { ...@@ -92,15 +118,6 @@ namespace LVDB {
} }
int getValue(json &value, string key, string fileName, cb_verify_json cb) { int getValue(json &value, string key, string fileName, cb_verify_json cb) {
// DB* pdb = _getDB(fileName);
// string oldVal;
// int ret = 0;
// json j;
// Status s = pdb->Get(leveldb::ReadOptions(), key, &oldVal);
// if(!s.ok()) {
// spdlog::error("failed to get {} from {}: {}",key, fileName, s.ToString());
// return -1;
// }
string s; string s;
int ret = getValue(s, key, fileName, NULL); int ret = getValue(s, key, fileName, NULL);
if(ret < 0) { if(ret < 0) {
...@@ -160,7 +177,41 @@ namespace LVDB { ...@@ -160,7 +177,41 @@ namespace LVDB {
} }
int getSn(json &info, string fileName){ int getSn(json &info, string fileName){
return getValue(info, LVDB_KEY_SN, fileName, _validateSn); int ret = 0;
ret = getValue(info, LVDB_KEY_SN, fileName, _validateSn);
if(ret < 0) {
// create default sn.
string sn = getStrRand(8);
info["sn"] = sn;
spdlog::warn("no local sn set. create a new one: {}", sn);
auto tsNow = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
info["lastboot"] = tsNow;
info["updatetime"] = tsNow;
ret = setSn(info, fileName);
if(ret < 0) {
spdlog::error("failed to save new generated sn");
exit(1);
}else{
// replace sn
int idx = 0;
for(auto &j:_sn_tmpl) {
idx = 0;
while(true) {
idx = _config_default_tmpl.find(j, idx);
if(idx == string::npos) break;
_config_default_tmpl.replace(idx, j.size(), sn);
idx+=sn.size();
}
}
// replace camera addr, user, password, cloud-addr
spdlog::debug("new config: {}", _config_default_tmpl);
return setValue(_config_default_tmpl, LVDB_KEY_CONFIG, fileName, NULL);
}
}
return ret;
}; };
int setSn(json &info, string fileName){ int setSn(json &info, string fileName){
......
...@@ -19,6 +19,7 @@ update: 2019/08/23 ...@@ -19,6 +19,7 @@ update: 2019/08/23
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <queue> #include <queue>
#include <ctime>
#ifdef OS_LINUX #ifdef OS_LINUX
#include <filesystem> #include <filesystem>
...@@ -45,6 +46,7 @@ private: ...@@ -45,6 +46,7 @@ private:
mutex cacheLock; mutex cacheLock;
queue<string> eventQue; queue<string> eventQue;
mutex eventQLock; mutex eventQLock;
time_t tsLastBoot, tsUpdateTime;
// //
void init() void init()
...@@ -52,12 +54,29 @@ private: ...@@ -52,12 +54,29 @@ private:
int ret; int ret;
bool inited = false; bool inited = false;
// TODO: load config from local db // TODO: load config from local db
devSn = "ILSEVMGR1"; json info;
ret = LVDB::getSn(info);
if(ret < 0) {
spdlog::error("failed to get sn");
exit(1);
}
tsLastBoot = info["lastboot"];
tsUpdateTime=info["updatetime"];
spdlog::info("evmgr info: sn = {}, lastboot = {}, updatetime = {}", info["sn"].get<string>(), ctime(&tsLastBoot), ctime(&tsUpdateTime));
devSn = info["sn"];
ret = LVDB::getLocalConfig(config);
if(ret < 0) {
spdlog::error("failed to get local configuration");
exit(1);
}
int opt_notify = ZMQ_NOTIFY_DISCONNECT|ZMQ_NOTIFY_CONNECT; int opt_notify = ZMQ_NOTIFY_DISCONNECT|ZMQ_NOTIFY_CONNECT;
string proto, addr; string proto, addr;
while(!inited) { while(!inited) {
try { try {
config = json::parse(cloudutils::config);
spdlog::info("config dumps: \n{}", config.dump()); spdlog::info("config dumps: \n{}", config.dump());
// TODO: verify sn // TODO: verify sn
if(!config.count("data")||!config["data"].count(devSn)||!config["data"][devSn].count("ipcs")) { if(!config.count("data")||!config["data"].count(devSn)||!config["data"][devSn].count("ipcs")) {
...@@ -65,6 +84,7 @@ private: ...@@ -65,6 +84,7 @@ private:
goto togo_sleep_continue; goto togo_sleep_continue;
} }
jmgr = config["data"][devSn]; jmgr = config["data"][devSn];
proto = jmgr["proto"];
if(proto != "zmq") { if(proto != "zmq") {
spdlog::warn("evmgr {} unsupported protocol: {}, try fallback to zmq instead now...", devSn, proto); spdlog::warn("evmgr {} unsupported protocol: {}, try fallback to zmq instead now...", devSn, proto);
......
...@@ -26,6 +26,7 @@ update: 2019/08/23 ...@@ -26,6 +26,7 @@ update: 2019/08/23
namespace fs = std::filesystem; namespace fs = std::filesystem;
#endif #endif
#include <cstdlib> #include <cstdlib>
#include <ctime>
#include "zmqhelper.hpp" #include "zmqhelper.hpp"
#include "tinythread.hpp" #include "tinythread.hpp"
#include "common.hpp" #include "common.hpp"
...@@ -77,6 +78,7 @@ private: ...@@ -77,6 +78,7 @@ private:
chrono::system_clock::time_point evtStartTm, evtStartTmLast; chrono::system_clock::time_point evtStartTm, evtStartTmLast;
queue<string> *evtQueue; queue<string> *evtQueue;
int streamIdx = -1; int streamIdx = -1;
time_t tsLastBoot, tsUpdateTime;
json config; json config;
thread thPing; thread thPing;
thread thEvent; thread thEvent;
...@@ -86,15 +88,31 @@ private: ...@@ -86,15 +88,31 @@ private:
{ {
int ret = 0; int ret = 0;
bool inited = false; bool inited = false;
// TODO: read db to get devSn // TODO: load config from local db
devSn = "ILSEVMLMOTION1"; json info;
iid = 1; ret = LVDB::getSn(info);
if(ret < 0) {
spdlog::error("failed to get sn");
exit(1);
}
tsLastBoot = info["lastboot"];
tsUpdateTime=info["updatetime"];
spdlog::info("evmgr info: sn = {}, lastboot = {}, updatetime = {}", info["sn"].get<string>(), ctime(&tsLastBoot), ctime(&tsUpdateTime));
devSn = info["sn"];
ret = LVDB::getLocalConfig(config);
if(ret < 0) {
spdlog::error("failed to get local configuration");
exit(1);
}
selfId = devSn + ":evmlmotion:" + to_string(iid); selfId = devSn + ":evmlmotion:" + to_string(iid);
while(!inited) { while(!inited) {
// TODO: req config // TODO: req config
bool found = false; bool found = false;
try { try {
config = json::parse(cloudutils::config);
spdlog::info("config: {:s}", config.dump()); spdlog::info("config: {:s}", config.dump());
json evmlmotion; json evmlmotion;
json evmgr; json evmgr;
...@@ -387,6 +405,7 @@ private: ...@@ -387,6 +405,7 @@ private:
if(detect) { if(detect) {
detectMotion(pCodecContext->pix_fmt,pFrame); detectMotion(pCodecContext->pix_fmt,pFrame);
} }
break;
} }
} }
return 0; return 0;
......
...@@ -18,6 +18,7 @@ update: 2019/08/23 ...@@ -18,6 +18,7 @@ update: 2019/08/23
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <ctime>
#ifdef OS_LINUX #ifdef OS_LINUX
#include <filesystem> #include <filesystem>
...@@ -153,22 +154,38 @@ private: ...@@ -153,22 +154,38 @@ private:
AVFormatContext *pAVFormatInput = NULL; AVFormatContext *pAVFormatInput = NULL;
string urlIn, urlPub, urlDealer, mgrSn, devSn, selfId; string urlIn, urlPub, urlDealer, mgrSn, devSn, selfId;
int *streamList = NULL, numStreams = 0, iid; int *streamList = NULL, numStreams = 0, iid;
time_t tsLastBoot, tsUpdateTime;
json config; json config;
int init() int init()
{ {
bool inited = false; bool inited = false;
// TODO: load devSn iid from database // TODO: load config from local db
devSn = "ILSEVPULLER1"; json info;
iid = 1; int ret = LVDB::getSn(info);
if(ret < 0) {
spdlog::error("failed to get sn");
exit(1);
}
tsLastBoot = info["lastboot"];
tsUpdateTime=info["updatetime"];
spdlog::info("evmgr info: sn = {}, lastboot = {}, updatetime = {}", info["sn"].get<string>(), ctime(&tsLastBoot), ctime(&tsUpdateTime));
devSn = info["sn"];
ret = LVDB::getLocalConfig(config);
if(ret < 0) {
spdlog::error("failed to get local configuration");
exit(1);
}
selfId = devSn+":evpuller:" + to_string(iid); selfId = devSn+":evpuller:" + to_string(iid);
int ret = 0;
while(!inited) { while(!inited) {
// TODO: req config // TODO: req config
bool found = false; bool found = false;
string user, passwd, addr; string user, passwd, addr;
try { try {
config = json::parse(cloudutils::config);
spdlog::info("config dump: {:s}", config.dump()); spdlog::info("config dump: {:s}", config.dump());
json data = config["data"]; json data = config["data"];
// first try to check mgr with same sn // first try to check mgr with same sn
......
...@@ -18,6 +18,7 @@ update: 2019/08/23 ...@@ -18,6 +18,7 @@ update: 2019/08/23
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <ctime>
#ifdef OS_LINUX #ifdef OS_LINUX
#include <filesystem> #include <filesystem>
...@@ -44,21 +45,38 @@ private: ...@@ -44,21 +45,38 @@ private:
int *streamList = NULL; int *streamList = NULL;
AVFormatContext *pAVFormatRemux = NULL; AVFormatContext *pAVFormatRemux = NULL;
AVFormatContext *pAVFormatInput = NULL; AVFormatContext *pAVFormatInput = NULL;
time_t tsLastBoot, tsUpdateTime;
json config; json config;
thread thPing; thread thPing;
int init() int init()
{ {
bool inited = false; bool inited = false;
// TODO: read db to get devSn // TODO: load config from local db
devSn = "ILSEVPUSHER1"; json info;
iid = 1; int ret = LVDB::getSn(info);
if(ret < 0) {
spdlog::error("failed to get sn");
exit(1);
}
tsLastBoot = info["lastboot"];
tsUpdateTime=info["updatetime"];
spdlog::info("evmgr info: sn = {}, lastboot = {}, updatetime = {}", info["sn"].get<string>(), ctime(&tsLastBoot), ctime(&tsUpdateTime));
devSn = info["sn"];
ret = LVDB::getLocalConfig(config);
if(ret < 0) {
spdlog::error("failed to get local configuration");
exit(1);
}
selfId = devSn + ":evpusher:" + to_string(iid); selfId = devSn + ":evpusher:" + to_string(iid);
while(!inited) { while(!inited) {
// TODO: req config // TODO: req config
bool found = false; bool found = false;
try { try {
config = json::parse(cloudutils::config);
spdlog::info("config: {:s}", config.dump()); spdlog::info("config: {:s}", config.dump());
json evpusher; json evpusher;
json evmgr; json evmgr;
...@@ -349,7 +367,7 @@ protected: ...@@ -349,7 +367,7 @@ protected:
bStopSig = true; bStopSig = true;
break; break;
} }
int ret =zmq_msg_init(&msg); ret =zmq_msg_init(&msg);
if(ret != 0) { if(ret != 0) {
spdlog::error("failed to init zmq msg"); spdlog::error("failed to init zmq msg");
continue; continue;
......
...@@ -19,6 +19,7 @@ update: 2019/08/20 ...@@ -19,6 +19,7 @@ update: 2019/08/20
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <vector> #include <vector>
#include <ctime>
#ifdef OS_LINUX #ifdef OS_LINUX
#include <filesystem> #include <filesystem>
...@@ -52,6 +53,7 @@ private: ...@@ -52,6 +53,7 @@ private:
// load from db // load from db
vector<int> *sliceIdxToName = NULL; vector<int> *sliceIdxToName = NULL;
int *streamList = NULL; int *streamList = NULL;
time_t tsLastBoot, tsUpdateTime;
json config; json config;
thread thPing; thread thPing;
...@@ -59,9 +61,25 @@ private: ...@@ -59,9 +61,25 @@ private:
{ {
int ret = 0; int ret = 0;
bool inited = false; bool inited = false;
// TODO: read db to get devSn // TODO: load config from local db
devSn = "ILSEVSLICER1"; json info;
iid = 1; ret = LVDB::getSn(info);
if(ret < 0) {
spdlog::error("failed to get sn");
exit(1);
}
tsLastBoot = info["lastboot"];
tsUpdateTime=info["updatetime"];
spdlog::info("evmgr info: sn = {}, lastboot = {}, updatetime = {}", info["sn"].get<string>(), ctime(&tsLastBoot), ctime(&tsUpdateTime));
devSn = info["sn"];
ret = LVDB::getLocalConfig(config);
if(ret < 0) {
spdlog::error("failed to get local configuration");
exit(1);
}
selfId = devSn + ":evslicer:" + to_string(iid); selfId = devSn + ":evslicer:" + to_string(iid);
// TODO: read local slices list and last index // TODO: read local slices list and last index
...@@ -69,7 +87,6 @@ private: ...@@ -69,7 +87,6 @@ private:
// TODO: req config // TODO: req config
bool found = false; bool found = false;
try { try {
config = json::parse(cloudutils::config);
spdlog::info("config: {:s}", config.dump()); spdlog::info("config: {:s}", config.dump());
json evslicer; json evslicer;
json evmgr; json evmgr;
......
...@@ -364,18 +364,20 @@ namespace cloudutils ...@@ -364,18 +364,20 @@ namespace cloudutils
} }
} }
*/ */
const char *config = "{\"time\":0,\"code\":0,\"data\":{\"ILSEVMGR1\":{\"sn\":\"ILSEVMGR1\",\"addr\":\"127.0.0.1\",\"addr-cloud\":\"127.0.0.1\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.203\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"iLabService\",\"status\":1,\"modules\":{\"evpuller\":[{\"sn\":\"ILSEVPULLER1\",\"addr\":\"127.0.0.1\",\"iid\":1,\"port-pub\":5556,\"status\":1}],\"evpusher\":[{\"sn\":\"ILSEVPUSHER1\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":1}],\"evslicer\":[{\"sn\":\"ILSEVSLICER1\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":1}],\"evml\":[{\"type\":\"motion\",\"sn\":\"ILSEVMLMOTION1\",\"iid\":1,\"enabled\":1,\"status\":1}]}}]}}}"; // const char *config = "{\"time\":0,\"code\":0,\"data\":{\"ILSEVMGR1\":{\"sn\":\"ILSEVMGR1\",\"addr\":\"127.0.0.1\",\"addr-cloud\":\"127.0.0.1\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"iLabService\",\"status\":1,\"modules\":{\"evpuller\":[{\"sn\":\"ILSEVPULLER1\",\"addr\":\"127.0.0.1\",\"iid\":1,\"port-pub\":5556,\"status\":1}],\"evpusher\":[{\"sn\":\"ILSEVPUSHER1\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":1}],\"evslicer\":[{\"sn\":\"ILSEVSLICER1\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":1}],\"evml\":[{\"type\":\"motion\",\"sn\":\"ILSEVMLMOTION1\",\"iid\":1,\"enabled\":1,\"status\":1}]}}]}}}";
json registry(const char *sn, const char *scn, int iid) json registry(const char *sn, const json &config)
{ {
// find local info in db // find local info in db
// request cloud info // request cloud info
// moc // moc
json ret = json::parse(config); return json();
return ret;
} }
} // namespace cloudutils } // namespace cloudutils
......
#ifndef __DATABSE_LEVEL_DB_H__ #ifndef __DATABSE_LEVEL_DB_H__
#define __DATABSE_LEVEL_DB_H__ #define __DATABSE_LEVEL_DB_H__
#include "leveldb/db.h" // #include "leveldb/db.h"
#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"
#include "json.hpp" #include "json.hpp"
using namespace nlohmann; using namespace nlohmann;
...@@ -10,7 +13,7 @@ namespace LVDB { ...@@ -10,7 +13,7 @@ namespace LVDB {
#define LVDB_PATH "/opt/lvldb/" #define LVDB_PATH "/opt/lvldb/"
// sn, config // sn, config
#define LVDB_FILE_GENERAL LVDB_PATH"general.db" #define LVDB_FILE_GENERAL LVDB_PATH "general.db"
// slices, log // slices, log
#define LVDB_FILE_LOG LVDB_PATH"log.db" #define LVDB_FILE_LOG LVDB_PATH"log.db"
...@@ -23,12 +26,12 @@ namespace LVDB { ...@@ -23,12 +26,12 @@ namespace LVDB {
int delValue(string key, string fileName); int delValue(string key, string fileName);
// sn, updatetime, boottime // sn, updatetime, boottime
int setSn(json &info, string fileName); int setSn(json &info, string fileName=LVDB_FILE_GENERAL);
int getSn(json &info, string fileName); int getSn(json &info, string fileName=LVDB_FILE_GENERAL);
// cloudutils::config // cloudutils::config
int getLocalConfig(json &config, string fileName); int getLocalConfig(json &config, string fileName=LVDB_FILE_GENERAL);
int setLocalConfig(json &config, string fileName); int setLocalConfig(json &config, string fileName=LVDB_FILE_GENERAL);
// slices // slices
int getSlices(json &slices, string fileName); int getSlices(json &slices, string fileName);
......
...@@ -2,20 +2,16 @@ ...@@ -2,20 +2,16 @@
#include "inc/database.h" #include "inc/database.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
const char *_config = "{\"time\":0,\"code\":0,\"data\":{\"ILSEVMGR1\":{\"sn\":\"ILSEVMGR1\",\"addr\":\"172.31.0.76\",\"addr-cloud\":\"172.31.0.76\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"FWBWTU\",\"status\":1,\"modules\":{\"evpuller\":[{\"sn\":\"ILSEVPULLER1\",\"addr\":\"172.31.0.76\",\"iid\":1,\"port-pub\":5556,\"status\":1}],\"evpusher\":[{\"sn\":\"ILSEVPUSHER1\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":1}],\"evslicer\":[{\"sn\":\"ILSEVSLICER1\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":1}],\"evml\":[{\"type\":\"motion\",\"sn\":\"ILSEVMLMOTION1\",\"iid\":1,\"enabled\":1,\"status\":1}]}}]}}}";
int main(){ int main(){
json j; json j;
int ret = 0; int ret = 0;
// sn // sn
ret = LVDB::delValue(LVDB_KEY_SN, LVDB_FILE_GENERAL); ret = LVDB::delValue(LVDB_KEY_SN, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret); spdlog::info("ret: {}", ret);
ret = LVDB::setSn(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret);
j["sn"] = "snaaaa"; ret = LVDB::getSn(j, LVDB_FILE_GENERAL);
j["lastboot"] = "2019-08-25 10:10:10"; spdlog::info("ret2: {}, {}", ret, j.dump());
j["updatetime"] = "2019-08-25 10:10:11";
ret = LVDB::setSn(j, LVDB_FILE_GENERAL); ret = LVDB::setSn(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret); spdlog::info("ret: {}", ret);
...@@ -24,23 +20,17 @@ int main(){ ...@@ -24,23 +20,17 @@ int main(){
ret = LVDB::getSn(j, LVDB_FILE_GENERAL); ret = LVDB::getSn(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump()); spdlog::info("ret: {}, {}", ret, j.dump());
//
json config = json::parse(_config);
j.clear(); j.clear();
ret = LVDB::delValue(LVDB_KEY_CONFIG, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret); spdlog::info("ret: {}", ret);
ret = LVDB::getLocalConfig(j, LVDB_FILE_GENERAL); ret = LVDB::getLocalConfig(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump()); spdlog::info("ret1: {}, {}", ret, j.dump());
ret= LVDB::setLocalConfig(j, LVDB_FILE_GENERAL); ret= LVDB::setLocalConfig(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump()); spdlog::info("ret2: {}, {}", ret, j.dump());
ret= LVDB::setLocalConfig(config, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, config.dump());
ret = LVDB::getLocalConfig(j, LVDB_FILE_GENERAL); ret = LVDB::getLocalConfig(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump()); spdlog::info("ret3: {}, {}", ret, j.dump());
return 0; return 0;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论