提交 4a60847c authored 作者: blu's avatar blu

init

上级 1735612e
...@@ -54,7 +54,7 @@ private: ...@@ -54,7 +54,7 @@ private:
config = json::parse(cloudutils::config); 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")) {
spdlog::error("evmgr {} invalid config. reload now...", devSn); spdlog::error("evmgr {} invalid config. reload now...", devSn);
this_thread::sleep_for(chrono::seconds(3)); this_thread::sleep_for(chrono::seconds(3));
continue; continue;
...@@ -63,7 +63,7 @@ private: ...@@ -63,7 +63,7 @@ private:
string proto = jmgr["proto"]; string proto = jmgr["proto"];
string addr; string addr;
if(proto != "zmq"){ if(proto != "zmq") {
spdlog::error("evmgr {} unsupported protocol: {}, try fallback to zmq instead now...", devSn, proto); spdlog::error("evmgr {} unsupported protocol: {}, try fallback to zmq instead now...", devSn, proto);
} }
addr = "tcp://" + jmgr["addr"].get<string>() + ":" + to_string(jmgr["port-router"]); addr = "tcp://" + jmgr["addr"].get<string>() + ":" + to_string(jmgr["port-router"]);
...@@ -92,29 +92,51 @@ private: ...@@ -92,29 +92,51 @@ private:
spdlog::info("evmgr {} successfuly inited", devSn); spdlog::info("evmgr {} successfuly inited", devSn);
} }
int mqErrorMsg(string cls, string devSn, string extraInfo, int ret) { int mqErrorMsg(string cls, string devSn, string extraInfo, int ret)
{
if(ret < 0) { if(ret < 0) {
spdlog::error("{} {} {}:{} ", cls, devSn, extraInfo, zmq_strerror(zmq_errno())); spdlog::error("{} {} {}:{} ", cls, devSn, extraInfo, zmq_strerror(zmq_errno()));
} }
return ret; return ret;
} }
int handleMsg(vector<vector<uint8_t> > &body) { int handleMsg(vector<vector<uint8_t> > &body)
{
int ret = 0; int ret = 0;
zmq_msg_t msg; zmq_msg_t msg;
// ID_SENDER, ID_TARGET, meta ,MSG // ID_SENDER, ID_TARGET, meta ,MSG
string selfId, peerId, meta; string selfId, peerId, meta;
if(body.size() == 2 && body[1].size() == 0) { if(body.size() == 2 && body[1].size() == 0) {
selfId = body2str(body[0]); selfId = body2str(body[0]);
spdlog::warn("evmgr {} peer disconnected: {}", devSn, selfId); bool eventConn = false;
if(peerStatus.count(selfId) == 0) { if(peerStatus.count(selfId) == 0||peerStatus[selfId] == 0) {
peerStatus[selfId] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count(); peerStatus[selfId] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
}else{ spdlog::info("evmgr {} peer connected: {}", devSn, selfId);
eventConn = true;
}
else {
peerStatus[selfId] = 0; peerStatus[selfId] = 0;
spdlog::warn("evmgr {} peer disconnected: {}", devSn, selfId);
}
// event
json jEvt;
jEvt["type"] = EV_MSG_TYPE_CONN_STAT;
jEvt["gid"] = selfId;
if(eventConn) {
jEvt["event"] = EV_MSG_EVENT_CONN_CONN;
}
else {
jEvt["event"] = EV_MSG_EVENT_CONN_DISCONN;
}
eventQue.push(jEvt.dump());
if(eventQue.size() > MAX_EVENT_QUEUE_SIZE) {
eventQue.pop();
} }
return 0; return 0;
}else if(body.size() != 4) { }
else if(body.size() != 4) {
spdlog::warn("evmgr {} dropped an invalid message, size: {}", devSn, body.size()); spdlog::warn("evmgr {} dropped an invalid message, size: {}", devSn, body.size());
return 0; return 0;
} }
...@@ -136,7 +158,8 @@ private: ...@@ -136,7 +158,8 @@ private:
if(ret < 0) { if(ret < 0) {
spdlog::error("evmgr {} failed to send multiple: {}", devSn, zmq_strerror(zmq_errno())); spdlog::error("evmgr {} failed to send multiple: {}", devSn, zmq_strerror(zmq_errno()));
} }
}else{ }
else {
// cache // cache
spdlog::warn("evmgr {} cached msg from {} to {}", devSn, selfId, peerId); spdlog::warn("evmgr {} cached msg from {} to {}", devSn, selfId, peerId);
lock_guard<mutex> lock(cacheLock); lock_guard<mutex> lock(cacheLock);
...@@ -147,7 +170,7 @@ private: ...@@ -147,7 +170,7 @@ private:
} }
// check if event // check if event
try{ try {
string metaType = json::parse(meta)["type"]; string metaType = json::parse(meta)["type"];
if(metaType == EV_MSG_META_EVENT) { if(metaType == EV_MSG_META_EVENT) {
eventQue.push(body2str(body[3])); eventQue.push(body2str(body[3]));
...@@ -155,10 +178,12 @@ private: ...@@ -155,10 +178,12 @@ private:
eventQue.pop(); eventQue.pop();
} }
} }
}catch(exception &e) { }
catch(exception &e) {
spdlog::error("evmgr {} exception parse event msg from {} to {}: ", devSn, selfId, peerId, e.what()); spdlog::error("evmgr {} exception parse event msg from {} to {}: ", devSn, selfId, peerId, e.what());
} }
}else{ }
else {
// message to mgr // message to mgr
spdlog::info("evmgr {} subsystem report msg received: {}; {}; {}", devSn, zmqhelper::body2str(body[0]), zmqhelper::body2str(body[1]), zmqhelper::body2str(body[2])); spdlog::info("evmgr {} subsystem report msg received: {}; {}; {}", devSn, zmqhelper::body2str(body[0]), zmqhelper::body2str(body[1]), zmqhelper::body2str(body[2]));
if(meta == "pong"||meta == "ping") { if(meta == "pong"||meta == "ping") {
...@@ -166,7 +191,7 @@ private: ...@@ -166,7 +191,7 @@ private:
spdlog::info("evmgr {}, ping msg from {}", devSn, selfId); spdlog::info("evmgr {}, ping msg from {}", devSn, selfId);
if(meta=="ping") { if(meta=="ping") {
if(cachedMsg.find(selfId) != cachedMsg.end()) { if(cachedMsg.find(selfId) != cachedMsg.end()) {
while(!cachedMsg[selfId].empty()){ while(!cachedMsg[selfId].empty()) {
lock_guard<mutex> lock(cacheLock); lock_guard<mutex> lock(cacheLock);
auto v = cachedMsg[selfId].front(); auto v = cachedMsg[selfId].front();
cachedMsg[selfId].pop(); cachedMsg[selfId].pop();
...@@ -177,7 +202,8 @@ private: ...@@ -177,7 +202,8 @@ private:
} }
} }
} }
}else{ }
else {
// TODO: // TODO:
spdlog::warn("evmgr {} received unknown meta {} from {}", devSn, meta, selfId); spdlog::warn("evmgr {} received unknown meta {} from {}", devSn, meta, selfId);
} }
...@@ -216,7 +242,8 @@ private: ...@@ -216,7 +242,8 @@ private:
} }
protected: protected:
void run(){ void run()
{
bool bStopSig = false; bool bStopSig = false;
int ret = 0; int ret = 0;
zmq_msg_t msg; zmq_msg_t msg;
...@@ -278,7 +305,7 @@ public: ...@@ -278,7 +305,7 @@ public:
zmq_close(pRouter); zmq_close(pRouter);
pRouter = NULL; pRouter = NULL;
} }
if(pRouterCtx != NULL){ if(pRouterCtx != NULL) {
zmq_ctx_destroy(pRouterCtx); zmq_ctx_destroy(pRouterCtx);
pRouterCtx = NULL; pRouterCtx = NULL;
} }
......
...@@ -453,9 +453,9 @@ private: ...@@ -453,9 +453,9 @@ private:
evtState = IN; evtState = IN;
json p; json p;
spdlog::info("state: PRE->IN"); spdlog::info("state: PRE->IN");
p["type"] = "motion"; p["type"] = EV_MSG_TYPE_AI_MOTION;
p["gid"] = selfId; p["gid"] = selfId;
p["event"] = "end"; p["event"] = EV_MSG_EVENT_MOTION_START;
p["ts"] = chrono::duration_cast<chrono::seconds>(evtStartTmLast.time_since_epoch()).count(); p["ts"] = chrono::duration_cast<chrono::seconds>(evtStartTmLast.time_since_epoch()).count();
//p["frame"] = origin.clone(); //p["frame"] = origin.clone();
evtQueue->push(p.dump()); evtQueue->push(p.dump());
...@@ -489,9 +489,9 @@ private: ...@@ -489,9 +489,9 @@ private:
spdlog::info("state: POST->NONE"); spdlog::info("state: POST->NONE");
evtState = NONE; evtState = NONE;
json p; json p;
p["type"] = "motion"; p["type"] = EV_MSG_TYPE_AI_MOTION;
p["gid"] = selfId; p["gid"] = selfId;
p["event"] = "end"; p["event"] = EV_MSG_EVENT_MOTION_END;
p["ts"] = chrono::duration_cast<chrono::seconds>(evtStartTmLast.time_since_epoch()).count() + (int)(detPara.post/2); p["ts"] = chrono::duration_cast<chrono::seconds>(evtStartTmLast.time_since_epoch()).count() + (int)(detPara.post/2);
evtQueue->push(p.dump()); evtQueue->push(p.dump());
if(evtQueue->size() > MAX_EVENT_QUEUE_SIZE*2) { if(evtQueue->size() > MAX_EVENT_QUEUE_SIZE*2) {
......
...@@ -20,8 +20,18 @@ namespace zmqhelper { ...@@ -20,8 +20,18 @@ namespace zmqhelper {
#define EV_MSG_META_UPDATE "update" #define EV_MSG_META_UPDATE "update"
#define EV_MSG_META_EVENT "event" #define EV_MSG_META_EVENT "event"
#define EV_MSG_META_AVFORMATCTX "afctx" #define EV_MSG_META_AVFORMATCTX "afctx"
#define EV_MSG_TYPE_AI_MOTION "ai_motion"
#define EV_MSG_TYPE_CONN_STAT "connstat"
#define EV_MSG_TYPE_SYS_STAT "sysstat"
#define EV_MSG_EVENT_MOTION_START "start"
#define EV_MSG_EVENT_MOTION_END "end"
#define EV_MSG_EVENT_CONN_CONN "connect"
#define EV_MSG_EVENT_CONN_DISCONN "disconnect"
#define EV_NUM_CACHE_PERPEER 100 #define EV_NUM_CACHE_PERPEER 100
#define MAX_EVENT_QUEUE_SIZE 20 #define MAX_EVENT_QUEUE_SIZE 50
// //
string body2str(vector<uint8_t> body) string body2str(vector<uint8_t> body)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论