提交 94ecc77e authored 作者: blu's avatar blu

cloud cmd api for platform client

上级 18570676
...@@ -21,7 +21,7 @@ update: 2019/09/10 ...@@ -21,7 +21,7 @@ update: 2019/09/10
using namespace leveldb; using namespace leveldb;
using namespace httplib; using namespace httplib;
string _config_default_tmpl = "{\"time\":0,\"code\":0,\"data\":{\"<SN_MGR>\":{\"sn\":\"<SN_MGR>\",\"addr\":\"127.0.0.1\",\"mqtt-cloud\":\"<cloud_addr>\",\"api-cloud\":\"http://127.0.0.1:8089\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"FWBWTU\",\"status\":0,\"port\":554,\"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,\"pre\":3,\"area\":300,\"post\":30,\"thresh\":80,\"enabled\":1,\"status\":0}]}}]}}}"; string _config_default_tmpl = "{\"time\":0,\"code\":0,\"data\":{\"<SN_MGR>\":{\"sn\":\"<SN_MGR>\",\"addr\":\"127.0.0.1\",\"mqttCloud\":\"<cloud_addr>\",\"apiCloud\":\"http://127.0.0.1:8089\",\"proto\":\"zmq\",\"portCloud\":5556,\"portRouter\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"FWBWTU\",\"status\":0,\"port\":554,\"modules\":{\"evpuller\":[{\"sn\":\"<SN_PULLER>\",\"addr\":\"127.0.0.1\",\"iid\":1,\"portPub\":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,\"pre\":3,\"area\":300,\"post\":30,\"thresh\":80,\"enabled\":1,\"status\":0}]}}]}}}";
const string _sn_tmpl[] = {"<SN_MGR>", "<SN_PULLER>", "<SN_PUSHER>", "<SN_SLICER>", "<SN_ML>"}; const string _sn_tmpl[] = {"<SN_MGR>", "<SN_PULLER>", "<SN_PUSHER>", "<SN_SLICER>", "<SN_ML>"};
const string _addr_tmpl[] = {"<ADDR_CAMERA>"}; const string _addr_tmpl[] = {"<ADDR_CAMERA>"};
...@@ -381,7 +381,7 @@ togo_end: ...@@ -381,7 +381,7 @@ togo_end:
// create default sn. // create default sn.
string sn = getStrRand(8); string sn = getStrRand(8);
info["sn"] = sn; info["sn"] = sn;
info["api-cloud"] = "http://127.0.0.1:8089"; info["apiCloud"] = "http://127.0.0.1:8089";
spdlog::warn("no local sn set. create a new one: {}", 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(); auto tsNow = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
info["lastboot"] = tsNow; info["lastboot"] = tsNow;
......
{ {
"data":{ "data":{
"W2E0VFN1":{ "V2I0C7KC":{
"addr":"127.0.0.1", "addr":"127.0.0.1",
"api-cloud":"http://127.0.0.1:8089", "apiCloud":"http://127.0.0.1:8089",
"ipcs":[ "ipcs":[
{ {
"addr":"172.31.0.129", "addr":"172.31.0.129",
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"iid":1, "iid":1,
"post":30, "post":30,
"pre":3, "pre":3,
"sn":"W2E0VFN1", "sn":"V2I0C7KC",
"thresh":30, "thresh":30,
"entropy": 0.3, "entropy": 0.3,
"type":"motion" "type":"motion"
...@@ -25,8 +25,8 @@ ...@@ -25,8 +25,8 @@
"addr":"127.0.0.1", "addr":"127.0.0.1",
"iid":1, "iid":1,
"enabled": 1, "enabled": 1,
"port-pub":5556, "portPub":5556,
"sn":"W2E0VFN1" "sn":"V2I0C7KC"
} }
], ],
"evpusher":[ "evpusher":[
...@@ -34,9 +34,9 @@ ...@@ -34,9 +34,9 @@
"enabled":0, "enabled":0,
"iid":1, "iid":1,
"password":"", "password":"",
"sn":"W2E0VFN1", "sn":"V2I0C7KC",
"token":"", "token":"",
"urlDest":"rtsp://40.73.41.176/W2E0VFN1", "urlDest":"rtsp://40.73.41.176/V2I0C7KC",
"user":"" "user":""
} }
], ],
...@@ -45,8 +45,8 @@ ...@@ -45,8 +45,8 @@
"enabled":1, "enabled":1,
"iid":1, "iid":1,
"path":"slices", "path":"slices",
"sn":"W2E0VFN1", "sn":"V2I0C7KC",
"video-server-addr": "http://40.73.41.176:10009/upload/evtvideos/" "videoServerAddr": "http://40.73.41.176:10009/upload/evtvideos/"
} }
] ]
}, },
...@@ -57,11 +57,11 @@ ...@@ -57,11 +57,11 @@
"sn": "iLabService" "sn": "iLabService"
} }
], ],
"mqtt-cloud":"<cloud_addr>", "mqttCloud":"<cloud_addr>",
"port-cloud":5556, "portCloud":5556,
"port-router":5550, "portRouter":5550,
"proto":"zmq", "proto":"zmq",
"sn":"W2E0VFN1" "sn":"V2I0C7KC"
} }
}, },
"lastupdated":1567669674 "lastupdated":1567669674
......
...@@ -68,7 +68,7 @@ private: ...@@ -68,7 +68,7 @@ private:
goto error_exit; goto error_exit;
} }
addr = "tcp://*:" + to_string(config["port-router"]); addr = "tcp://*:" + to_string(config["portRouter"]);
// setup zmq // setup zmq
// TODO: connect to cloud // TODO: connect to cloud
......
...@@ -217,8 +217,8 @@ private: ...@@ -217,8 +217,8 @@ private:
json evslicer = ipc["modules"]["evslicer"][0]; json evslicer = ipc["modules"]["evslicer"][0];
slicerGid = evslicer["sn"].get<string>()+":evslicer:" + to_string(evslicer["iid"]); slicerGid = evslicer["sn"].get<string>()+":evslicer:" + to_string(evslicer["iid"]);
urlPub = string("tcp://") + evpuller["addr"].get<string>() + ":" + to_string(evpuller["port-pub"]); urlPub = string("tcp://") + evpuller["addr"].get<string>() + ":" + to_string(evpuller["portPub"]);
urlRouter = string("tcp://") + evmgr["addr"].get<string>() + ":" + to_string(evmgr["port-router"]); urlRouter = string("tcp://") + evmgr["addr"].get<string>() + ":" + to_string(evmgr["portRouter"]);
spdlog::info("evmlmotion {} will connect to {} for sub, {} for router", selfId, urlPub, urlRouter); spdlog::info("evmlmotion {} will connect to {} for sub, {} for router", selfId, urlPub, urlRouter);
// TODO: multiple protocols support // TODO: multiple protocols support
......
...@@ -220,8 +220,8 @@ private: ...@@ -220,8 +220,8 @@ private:
exit(1); exit(1);
} }
urlPub = string("tcp://*:") + to_string(evpuller["port-pub"]); urlPub = string("tcp://*:") + to_string(evpuller["portPub"]);
urlDealer = "tcp://" + evmgr["addr"].get<string>() + string(":") + to_string(evmgr["port-router"]); urlDealer = "tcp://" + evmgr["addr"].get<string>() + string(":") + to_string(evmgr["portRouter"]);
spdlog::info("evpuller {} bind on {} for pub, connect to {} for dealer", selfId, urlPub, urlDealer); spdlog::info("evpuller {} bind on {} for pub, connect to {} for dealer", selfId, urlPub, urlDealer);
pPubCtx = zmq_ctx_new(); pPubCtx = zmq_ctx_new();
......
...@@ -91,8 +91,8 @@ private: ...@@ -91,8 +91,8 @@ private:
pullerGid = evpuller["sn"].get<string>() + ":evpuller:" + to_string(evpuller["iid"]); pullerGid = evpuller["sn"].get<string>() + ":evpuller:" + to_string(evpuller["iid"]);
mgrSn = evmgr["sn"]; mgrSn = evmgr["sn"];
urlPub = string("tcp://") + evpuller["addr"].get<string>() + ":" + to_string(evpuller["port-pub"]); urlPub = string("tcp://") + evpuller["addr"].get<string>() + ":" + to_string(evpuller["portPub"]);
urlDealer = string("tcp://") + evmgr["addr"].get<string>() + ":" + to_string(evmgr["port-router"]); urlDealer = string("tcp://") + evmgr["addr"].get<string>() + ":" + to_string(evmgr["portRouter"]);
spdlog::info("evpusher {} connect to {} for sub, {} for router", selfId, urlPub, urlDealer); spdlog::info("evpusher {} connect to {} for sub, {} for router", selfId, urlPub, urlDealer);
// TODO: multiple protocols support // TODO: multiple protocols support
urlOut = evpusher["urlDest"].get<string>(); urlOut = evpusher["urlDest"].get<string>();
......
...@@ -259,8 +259,8 @@ private: ...@@ -259,8 +259,8 @@ private:
ipcSn = ipc["sn"]; ipcSn = ipc["sn"];
} }
if(evslicer.count("video-server-addr") != 0 && !evslicer["video-server-addr"].get<string>().empty()) { if(evslicer.count("videoServerAddr") != 0 && !evslicer["videoServerAddr"].get<string>().empty()) {
videoFileServerApi = evslicer["video-server-addr"].get<string>(); videoFileServerApi = evslicer["videoServerAddr"].get<string>();
if(videoFileServerApi.at(videoFileServerApi.size()-1) != '/') { if(videoFileServerApi.at(videoFileServerApi.size()-1) != '/') {
videoFileServerApi += string("/"); videoFileServerApi += string("/");
} }
...@@ -301,8 +301,8 @@ private: ...@@ -301,8 +301,8 @@ private:
spdlog::info("evslicer mkdir -p {}", selfId, urlOut); spdlog::info("evslicer mkdir -p {}", selfId, urlOut);
ret = system((string("mkdir -p ") + urlOut).c_str()); ret = system((string("mkdir -p ") + urlOut).c_str());
urlPub = string("tcp://") + evpuller["addr"].get<string>() + ":" + to_string(evpuller["port-pub"]); urlPub = string("tcp://") + evpuller["addr"].get<string>() + ":" + to_string(evpuller["portPub"]);
urlRouter = string("tcp://") + evmgr["addr"].get<string>() + ":" + to_string(evmgr["port-router"]); urlRouter = string("tcp://") + evmgr["addr"].get<string>() + ":" + to_string(evmgr["portRouter"]);
spdlog::info("evslicer {} will connect to {} for sub, {} for router", selfId, urlPub, urlRouter); spdlog::info("evslicer {} will connect to {} for sub, {} for router", selfId, urlPub, urlRouter);
// setup sub // setup sub
...@@ -613,11 +613,12 @@ protected: ...@@ -613,11 +613,12 @@ protected:
string videoFileTs2Name(long ts, bool bLog = false) string videoFileTs2Name(long ts, bool bLog = false)
{ {
std::time_t now = ts; std::tm t;
std::tm * ptm = std::localtime(&now); memcpy(&t, localtime(&ts), sizeof(t));
char buffer[20]; char buffer[20];
// Format: Mo, 15.06.2009 20:20:00 // Format: Mo, 15.06.2009 20:20:00
std::strftime(buffer, 20, "%Y%m%d_%H%M%S", ptm); std::strftime(buffer, 20, "%Y%m%d_%H%M%S", &t);
if(bLog) if(bLog)
spdlog::info("ts: {}, fname: {}/{}.mp4", ts, this->urlOut, buffer); spdlog::info("ts: {}, fname: {}/{}.mp4", ts, this->urlOut, buffer);
return string(buffer); return string(buffer);
...@@ -822,8 +823,6 @@ protected: ...@@ -822,8 +823,6 @@ protected:
// find video files // find video files
vector<string> findSlicesByRange(long tss, long tse, int offsetS, int offsetE) vector<string> findSlicesByRange(long tss, long tse, int offsetS, int offsetE)
{ {
debugFilesRing(this->vTsActive);
vector<string> ret; vector<string> ret;
int found = 0; int found = 0;
int _itss = 0; int _itss = 0;
...@@ -997,9 +996,30 @@ public: ...@@ -997,9 +996,30 @@ public:
} }
// TODO: scheduled task // TODO: scheduled task
spdlog::info("evslicer {} wait for {}s to matching event videos", this->selfId, this->seconds + 5); // check tss, tse
int sIdx = 0, eIdx = 0;
this_thread::sleep_for(chrono::seconds(this->seconds + 5)); if(this->bSegFull) {
sIdx = this->incSegHead(this->segHead);
eIdx = this->decSegHead(this->segHead);
}else{
sIdx = 1;
eIdx = this->decSegHead(this->segHead);
}
if(sIdx > eIdx || tss > this->vTsActive[eIdx] || tse < this->vTsActive[sIdx]) {
spdlog::error("event range ({}, {}) not in local range ({}, {}).", tss, tse, this->vTsActive[sIdx], this->vTsActive[eIdx]);
this->debugFilesRing(this->vTsActive);
continue;
}
if(sIdx == eIdx)
{
spdlog::info("evslicer {} wait for {}s to matching event videos", this->selfId, this->seconds + 5);
this_thread::sleep_for(chrono::seconds(this->seconds + 5));
}else{
spdlog::info("event range ({}, {}) is in local range ({}, {}).", tss, tse, this->vTsActive[sIdx], this->vTsActive[eIdx]);
}
auto v = findSlicesByRange(tss, tse, offsetS, offsetE); auto v = findSlicesByRange(tss, tse, offsetS, offsetE);
if(v.size() == 0) { if(v.size() == 0) {
spdlog::error("evslicer {} ignore upload videos in range ({}, {})", this->selfId, this->videoFileTs2Name(tss), this->videoFileTs2Name(tse)); spdlog::error("evslicer {} ignore upload videos in range ({}, {})", this->selfId, this->videoFileTs2Name(tss), this->videoFileTs2Name(tse));
......
...@@ -9,10 +9,10 @@ json registry(json &conf, string sn, string module) ...@@ -9,10 +9,10 @@ json registry(json &conf, string sn, string module)
json ret; json ret;
string api; string api;
try { try {
api = conf.at("data").at(sn).at("api-cloud").get<string>() + "/register"; api = conf.at("data").at(sn).at("apiCloud").get<string>() + "/register";
Uri uri=Uri::Parse(api); Uri uri=Uri::Parse(api);
if(uri.Host.empty()||uri.Port.empty()||uri.Protocol.find("http") == string::npos||uri.Path.empty()) { if(uri.Host.empty()||uri.Port.empty()||uri.Protocol.find("http") == string::npos||uri.Path.empty()) {
string msg = "registry error. invalid api-cloud in config: " + api; string msg = "registry error. invalid apiCloud in config: " + api;
ret["code"] = 1; ret["code"] = 1;
ret["msg"] = msg; ret["msg"] = msg;
spdlog::error(msg); spdlog::error(msg);
...@@ -45,11 +45,11 @@ json reqConfig(json &info) ...@@ -45,11 +45,11 @@ json reqConfig(json &info)
json ret; json ret;
string api; string api;
try { try {
api = info.at("api-cloud").get<string>(); api = info.at("apiCloud").get<string>();
Uri uri=Uri::Parse(api); Uri uri=Uri::Parse(api);
string sn = info.at("sn").get<string>(); string sn = info.at("sn").get<string>();
if(uri.Host.empty()||uri.Port.empty()||uri.Protocol.find("http") == string::npos) { if(uri.Host.empty()||uri.Port.empty()||uri.Protocol.find("http") == string::npos) {
string msg = string(__FILE__) +":" + to_string(__LINE__) + ": request cloud configuration error. invalid api-cloud in info: " + api; string msg = string(__FILE__) +":" + to_string(__LINE__) + ": request cloud configuration error. invalid apiCloud in info: " + api;
ret["code"] = EVCLOUD_REQ_E_PARAM; ret["code"] = EVCLOUD_REQ_E_PARAM;
ret["msg"] = msg; ret["msg"] = msg;
spdlog::error(msg); spdlog::error(msg);
...@@ -334,7 +334,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -334,7 +334,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
// one ipc // one ipc
if(!matched && !hasError) { if(!matched && !hasError) {
// /PSBV7GKN/ipcs/0" // /PSBV7GKN/ipcs/0"
// {"addr":"172.31.0.129","modules":{"evml":[{"area":200,"enabled":1,"entropy":0.3,"iid":1,"post":30,"pre":3,"sn":"PSBV7GKN","thresh":30,"type":"motion"}],"evpuller":[{"addr":"127.0.0.1","enabled":1,"iid":1,"port-pub":5556,"sn":"PSBV7GKN"}],"evpusher":[{"enabled":0,"iid":1,"password":"","sn":"PSBV7GKN","token":"","urlDest":"rtsp://40.73.41.176/PSBV7GKN","user":""}],"evslicer":[{"enabled":1,"iid":1,"path":"slices","sn":"PSBV7GKN","video-server-addr":"http://40.73.41.176:10009/upload/evtvideos/"}]},"password":"iLabService","port":554,"proto":"rtsp","sn":"iLabService","user":"admin"} // {"addr":"172.31.0.129","modules":{"evml":[{"area":200,"enabled":1,"entropy":0.3,"iid":1,"post":30,"pre":3,"sn":"PSBV7GKN","thresh":30,"type":"motion"}],"evpuller":[{"addr":"127.0.0.1","enabled":1,"iid":1,"portPub":5556,"sn":"PSBV7GKN"}],"evpusher":[{"enabled":0,"iid":1,"password":"","sn":"PSBV7GKN","token":"","urlDest":"rtsp://40.73.41.176/PSBV7GKN","user":""}],"evslicer":[{"enabled":1,"iid":1,"path":"slices","sn":"PSBV7GKN","videoServerAddr":"http://40.73.41.176:10009/upload/evtvideos/"}]},"password":"iLabService","port":554,"proto":"rtsp","sn":"iLabService","user":"admin"}
string clusterRegStr = "/(\\w+)/ipcs/(\\d+)(?:/[^/]+)?"; string clusterRegStr = "/(\\w+)/ipcs/(\\d+)(?:/[^/]+)?";
std::regex clusterRegex(clusterRegStr); std::regex clusterRegex(clusterRegStr);
std::smatch results; std::smatch results;
...@@ -375,7 +375,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -375,7 +375,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
// match module config // match module config
if(!matched && !hasError) { if(!matched && !hasError) {
// /PSBV7GKN/ipcs/0/modules/evslicer/0/video-server-addr // /PSBV7GKN/ipcs/0/modules/evslicer/0/videoServerAddr
// /NMXH73Y2/ipcs/0/modules/evpusher/0/urlDest // /NMXH73Y2/ipcs/0/modules/evpusher/0/urlDest
string moduleRegStr = "/(\\w+)/ipcs/(\\d+)/modules/(\\w+)/(\\d+)/([^/]+)"; string moduleRegStr = "/(\\w+)/ipcs/(\\d+)/modules/(\\w+)/(\\d+)/([^/]+)";
std::regex moduleRegex(moduleRegStr); std::regex moduleRegex(moduleRegStr);
...@@ -518,7 +518,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -518,7 +518,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
// whole cluster // whole cluster
if(!matched && !hasError) { if(!matched && !hasError) {
// /PSBV7GKN // /PSBV7GKN
// "value":{"addr":"127.0.0.1","api-cloud":"http://127.0.0.1:8089","ipcs":[{"addr":"172.31.0.129","modules":{"evml":[{"area":200,"enabled":1,"entropy":0.3,"iid":1,"post":30,"pre":3,"sn":"PSBV7GKN","thresh":30,"type":"motion"}],"evpuller":[{"addr":"127.0.0.1","enabled":1,"iid":1,"port-pub":5556,"sn":"PSBV7GKN"}],"evpusher":[{"enabled":0,"iid":1,"password":"","sn":"PSBV7GKN","token":"","urlDest":"rtsp://40.73.41.176/PSBV7GKN","user":""}],"evslicer":[{"enabled":1,"iid":1,"path":"slices","sn":"PSBV7GKN","video-server-addr":"http://40.73.41.176:10009/upload/evtvideos/"}]},"password":"iLabService","port":554,"proto":"rtsp","sn":"iLabService","user":"admin"}],"mqtt-cloud":"<cloud_addr>","port-cloud":5556,"port-router":5550,"proto":"zmq","sn":"PSBV7GKN"} // "value":{"addr":"127.0.0.1","apiCloud":"http://127.0.0.1:8089","ipcs":[{"addr":"172.31.0.129","modules":{"evml":[{"area":200,"enabled":1,"entropy":0.3,"iid":1,"post":30,"pre":3,"sn":"PSBV7GKN","thresh":30,"type":"motion"}],"evpuller":[{"addr":"127.0.0.1","enabled":1,"iid":1,"portPub":5556,"sn":"PSBV7GKN"}],"evpusher":[{"enabled":0,"iid":1,"password":"","sn":"PSBV7GKN","token":"","urlDest":"rtsp://40.73.41.176/PSBV7GKN","user":""}],"evslicer":[{"enabled":1,"iid":1,"path":"slices","sn":"PSBV7GKN","videoServerAddr":"http://40.73.41.176:10009/upload/evtvideos/"}]},"password":"iLabService","port":554,"proto":"rtsp","sn":"iLabService","user":"admin"}],"mqttCloud":"<cloud_addr>","portCloud":5556,"portRouter":5550,"proto":"zmq","sn":"PSBV7GKN"}
string clusterRegStr = "/(\\w+)"; string clusterRegStr = "/(\\w+)";
std::regex clusterRegex(clusterRegStr); std::regex clusterRegex(clusterRegStr);
std::smatch results; std::smatch results;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论