提交 6f1ad9c4 authored 作者: blu's avatar blu

revise getModulesOperFromConfDiff and related applications

上级 7d1a8b8f
...@@ -310,7 +310,7 @@ json *findModuleConfig(string peerId, json &data) ...@@ -310,7 +310,7 @@ json *findModuleConfig(string peerId, json &data)
return ret; return ret;
} }
/// return json key: gid; value: 0 - stop, 1 - start, 3 - restart /// return json key: gid; value: 0 - disabled, 1 - removed, 2 - start, 3 - restart
json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, string sn) { json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, string sn) {
/// key: gid; value: 0 - stop, 1 - start, 3 - restart /// key: gid; value: 0 - stop, 1 - start, 3 - restart
json ret; json ret;
...@@ -331,7 +331,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -331,7 +331,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
set<string> oprations{"add", "replace", "remove"}; set<string> oprations{"add", "replace", "remove"};
set<string> pullerTag{"addr", "user", "password", "proto", "port" /*, "sn"*/}; set<string> pullerTag{"addr", "user", "password", "proto", "port" /*, "sn"*/};
// one ipc // whole 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,"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"} // {"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"}
...@@ -344,29 +344,95 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -344,29 +344,95 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
matched = true; matched = true;
string mgrSn = results[1].str(); string mgrSn = results[1].str();
int ipcIdx = stoi(results[2].str()); int ipcIdx = stoi(results[2].str());
json mgr; json oldIpc, newIpc;
if(d["op"] == "remove"){ json oldMgr, newMgr;
mgr[mgrSn] = oldConfig[mgrSn];
if(oldConfig[mgrSn]["ipcs"].size() >= ipcIdx+1){
oldIpc = oldConfig[mgrSn]["ipcs"][ipcIdx];
oldMgr[mgrSn] = json();
oldMgr[mgrSn]["ipcs"] = json();
oldMgr[mgrSn]["ipcs"].push_back(oldIpc);
}
if(newConfig[mgrSn]["ipcs"].size() >= ipcIdx+1){
newIpc = newConfig[mgrSn]["ipcs"][ipcIdx];
newMgr[mgrSn] = json();
newMgr[mgrSn]["ipcs"] = json();
newMgr[mgrSn]["ipcs"].push_back(newIpc);
}
if(newIpc.size() != 0 && newIpc.count("modules") != 0 && newIpc.count("addr") != 0) {
for(auto &[mn, v]: newIpc["modules"].items()) {
string modName = mn;
json &mods = v;
int modIdx = 0;
for(auto &modObj:mods) {
string modSn = sn.empty()? modObj["sn"].get<string>(): sn;
if(modObj["sn"].get<string>() == modSn){
if(modName == "evml") {
if(modObj.count("type") == 0) {
string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no type field", ipcIdx, modName, modIdx);
spdlog::error(msg);
hasError = true;
break;
}else{ }else{
mgr[mgrSn] = newConfig[mgrSn]; modName = modName + modObj["type"].get<string>();
}
} }
string info = string(__FILE__) + ":" + to_string(__LINE__); if(modObj.count("iid") == 0) {
json jret = cfgutils::getModuleGidsFromCfg(sn, mgr, info, ipcIdx); string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no iid field", ipcIdx, modName, modIdx);
spdlog::info("jret: {}", jret.dump()); spdlog::error(msg);
if(jret["code"] != 0) {
ret["msg"] = jret["msg"];
hasError = true; hasError = true;
break; break;
}
string modGid = modSn + ":" + modName + ":" + to_string(modObj["iid"].get<int>());
if(modObj.count("enabled") == 0 ||modObj["enabled"] == 0 ) {
ret["data"][modGid] = 0; // disabled
}else{ }else{
if(jret["msg"] != "ok") { ret["data"][modGid] = 2; // start
ret["msg"] = jret["msg"];
} }
for(auto &k: jret["data"]) { }
if(d["op"] == "remove"){ modIdx++;
ret["data"][string(k)] = 0; }
}
}
if(oldIpc.size() != 0 && oldIpc.count("modules") != 0 && oldIpc.count("addr") != 0) {
for(auto &[mn,v]: oldIpc["modules"].items()) {
string modName = mn;
json &mods = v;
int modIdx = 0;
for(auto &modObj:mods) {
string modSn = sn.empty()? modObj["sn"].get<string>(): sn;
if(modObj["sn"].get<string>() == modSn){
if(modName == "evml") {
if(modObj.count("type") == 0) {
string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no type field", ipcIdx, modName, modIdx);
spdlog::error(msg);
hasError = true;
break;
}else{ }else{
ret["data"][string(k)] = 2; modName = modName + modObj["type"].get<string>();
}
}
if(modObj.count("iid") == 0) {
string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no iid field", ipcIdx, modName, modIdx);
spdlog::error(msg);
hasError = true;
break;
}
string modGid = modSn + ":" + modName + ":" + to_string(modObj["iid"].get<int>());
if(ret["data"].count(modGid) != 0 && ret["data"][modGid] == 2) {
ret["data"][modGid] = 3; // restart
}else{
ret["data"][modGid] = 1; // perm stop
}
}
modIdx++;
} }
} }
} }
...@@ -374,7 +440,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -374,7 +440,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
} }
} }
// match module config // match module prop config
if(!matched && !hasError) { if(!matched && !hasError) {
// /PSBV7GKN/ipcs/0/modules/evslicer/0/videoServerAddr // /PSBV7GKN/ipcs/0/modules/evslicer/0/videoServerAddr
// /NMXH73Y2/ipcs/0/modules/evpusher/0/urlDest // /NMXH73Y2/ipcs/0/modules/evpusher/0/urlDest
...@@ -390,7 +456,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -390,7 +456,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
int modIdx = stoi(results[4].str()); int modIdx = stoi(results[4].str());
string modName = results[3].str(); string modName = results[3].str();
string propName = results[5].str(); string propName = results[5].str();
if(d["op"] == "replace"||d["op"] == "add" || d["op"] == "remove") { if(true){ //d["op"] == "replace"||d["op"] == "add" || d["op"] == "remove") {
auto &oldMod = oldConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx]; auto &oldMod = oldConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx];
auto &newMod = newConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx]; auto &newMod = newConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx];
if(oldMod.count("iid") == 0 || newMod.count("iid") == 0) { if(oldMod.count("iid") == 0 || newMod.count("iid") == 0) {
...@@ -425,7 +491,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -425,7 +491,7 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
string newGid = newSn + ":" + modName + ":" + to_string(newMod["iid"].get<int>()); string newGid = newSn + ":" + modName + ":" + to_string(newMod["iid"].get<int>());
if(oldGid != newGid) { if(oldGid != newGid) {
ret["data"][oldGid] = 0; ret["data"][oldGid] = 1; // perm stop
} }
if(!sn.empty() && sn != newSn) { if(!sn.empty() && sn != newSn) {
...@@ -434,17 +500,16 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -434,17 +500,16 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
if(propName == "enabled") { if(propName == "enabled") {
if(newMod.count("enabled") == 0||newMod["enabled"].get<int>() == 0) { if(newMod.count("enabled") == 0||newMod["enabled"].get<int>() == 0) {
ret["data"][newGid] = 0; ret["data"][newGid] = 0; // disable
}else{ }else{
ret["data"][newGid] = 1; ret["data"][newGid] = 2; // start
} }
}else{ // other prop modification }else{ // other prop modification
// it was disabled. just ignore // it was disabled. just ignore
if(ret["data"].count(newGid) != 0 && ret["data"][newGid].get<int>() == 0) { if(ret["data"].count(newGid) != 0 && (ret["data"][newGid].get<int>() == 0 || ret["data"][newGid].get<int>() == 1)) {
// nop // nop. stopped or disabled
}else{ }else{
// restart ret["data"][newGid] = 3; // restart
ret["data"][newGid] = 2;
} }
} }
} }
...@@ -468,46 +533,72 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st ...@@ -468,46 +533,72 @@ json getModulesOperFromConfDiff(json& oldConfig, json &newConfig, json &diff, st
int ipcIdx = stoi(results[2].str()); int ipcIdx = stoi(results[2].str());
int modIdx = stoi(results[4].str()); int modIdx = stoi(results[4].str());
string modName = results[3].str(); string modName = results[3].str();
json modObj; json oldModObj, newModObj;
if(d["op"] == "remove") {
modObj = oldConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx]; if(oldConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName].size() >= modIdx +1) {
oldModObj = oldConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx];
}
if(newConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName].size() >= modIdx +1){
newModObj = newConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx];
}
if(newModObj.size() != 0) {
string modSn = sn.empty()? newModObj["sn"].get<string>(): sn;
if(newModObj["sn"].get<string>() == modSn){
if(modName == "evml") {
if(newModObj.count("type") == 0) {
string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no type field", ipcIdx, modName, modIdx);
spdlog::error(msg);
hasError = true;
break;
}else{ }else{
modObj = newConfig[mgrSn]["ipcs"][ipcIdx]["modules"][modName][modIdx]; modName = modName + newModObj["type"].get<string>();
}
} }
if(modObj.count("sn") == 0) {
if(d["op"] != "remove"){ if(newModObj.count("iid") == 0) {
string msg = fmt::format("invalid modue config having no sn /{}/ipcs/{}/modules/{}/{}", mgrSn, ipcIdx, modName, modIdx); string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no iid field", ipcIdx, modName, modIdx);
spdlog::error(msg); spdlog::error(msg);
hasError = true; hasError = true;
ret["msg"] = msg;
break; break;
} }
string modGid = modSn + ":" + modName + ":" + to_string(newModObj["iid"].get<int>());
if(newModObj.count("enabled") == 0 ||newModObj["enabled"] == 0 ) {
ret["data"][modGid] = 0; // disabled
}else{ }else{
string modSn = sn.empty()? modObj["sn"].get<string>(): sn; ret["data"][modGid] = 2; // start
if(modObj["sn"].get<string>() == modSn){ }
}
}
if(oldModObj.size() != 0) {
string modSn = sn.empty()? oldModObj["sn"].get<string>(): sn;
if(oldModObj["sn"].get<string>() == modSn){
if(modName == "evml") { if(modName == "evml") {
if(modObj.count("type") == 0) { if(oldModObj.count("type") == 0) {
string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no type field", ipcIdx, modName, modIdx); string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no type field", ipcIdx, modName, modIdx);
spdlog::error(msg); spdlog::error(msg);
hasError = true; hasError = false; // TODO
break; continue;
}else{ }else{
modName = modName + modObj["type"].get<string>(); modName = modName + oldModObj["type"].get<string>();
} }
} }
if(modObj.count("iid") == 0) { if(oldModObj.count("iid") == 0) {
string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no iid field", ipcIdx, modName, modIdx); string msg = fmt::format("invalid evml module config ipcs[{}]['modules'][{}][{}] having no iid field", ipcIdx, modName, modIdx);
spdlog::error(msg); spdlog::error(msg);
hasError = true; hasError = false;
break; continue;
} }
string modGid = modSn + ":" + modName + ":" + to_string(modObj["iid"].get<int>()); string modGid = modSn + ":" + modName + ":" + to_string(oldModObj["iid"].get<int>());
if(d["op"] == "remove") { if(ret["data"].count(modGid) != 0 && ret["data"][modGid] == 2 ) {
ret["data"][modGid] = 0; ret["data"][modGid] = 3; // restart
}else{ }else{
ret["data"][modGid] = 1; ret["data"][modGid] = 1; // perm stop
} }
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论