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

upload video files

上级 081b0087
......@@ -4,13 +4,21 @@
#include <vector>
#include <string>
#include <set>
#include "inc/json.hpp"
#include "json.hpp"
#include <spdlog/spdlog.h>
#include <fmt/format.h>
using namespace std;
using namespace nlohmann;
json getModuleActionFromDiff(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
json ret;
ret["code"] = 0;
ret["msg"] = "ok";
ret["data"] = json();
bool hasError = false;
try{
for(auto &d: diff) {
if(d.count("path") != 0) {
string path_ = d["path"];
......@@ -21,7 +29,7 @@ json getModuleActionFromDiff(json& oldConfig, json &newConfig, json &diff, strin
set<string> oprations{"add", "replace", "remove"};
set<string> pullerTag{"addr", "user", "password", "proto", "port" /*, "sn"*/};
string ipcRegStr = string("/") + this->devSn + "/ipcs/(\\d+)/(\\w+)";
string ipcRegStr = string("/") + sn + "/ipcs/(\\d+)/(\\w+)";
std::smatch results;
std::regex ipcRegex(ipcRegStr);
if (std::regex_match(path_, results, ipcRegex)) {
......@@ -30,31 +38,115 @@ json getModuleActionFromDiff(json& oldConfig, json &newConfig, json &diff, strin
int ipcIdx = stoi(results[1].str());
string tag = results[2].str();
if(pullerTag.find(tag) != pullerTag.end()) {
if(d["op"] == "add") {
// TODO: op = remove
if(d["op"] == "add" || d["op"] == "replace") {
// start
auto ipc = newConfig[sn]["ipcs"][ipcIdx];
if(ipc.count("modules") == 0 || ipc["modules"].size() == 0 || ipc["moudles"].count("evpuller") ==0 || ipc["modules"] )
if(ipc.count("modules") == 0 || ipc["modules"].size() == 0 || ipc["moudles"].count("evpuller") ==0 || ipc["modules"]["evpuller"].size() == 0 ) {
string msg = fmt::format("invalid config for ipc[{}]['modules']['evpuller']: {}", ipcIdx, newConfig.dump());
spdlog::error(msg);
ret["msg"] = msg;
hasError = true;
break;
}else{
auto &evpullers = ipc["module"]["evpuller"];
int idx = 0;
for(auto &puller:evpullers) {
// strutil
if(puller.count("iid") == 0 || puller.count("addr") == 0) {
string msg = fmt::format("invliad config as of having no iid/addr/enabled field in ipc[{}]['modules']['evpuller'][{}]: {}", ipcIdx, idx, newConfig.dump());
spdlog::error(msg);
ret["msg"] = msg;
hasError = true;
}else{
string gid = sn + ":evpuller:" + to_string(puller["iid"].get<int>());
if(puller.count("enabled") == 0 || puller["enabled"].get<int>() == 0) {
ret["data"][gid] = 0; // stop
}else{
ret["data"][gid] = 2;
}
}
idx++;
}
}
}
}
}
}
// match module config
if(!matched) {
if(!matched && !hasError) {
// /NMXH73Y2/ipcs/0/modules/evpusher/0/urlDest
string moduleRegStr = "/NMXH73Y2/ipcs/(\\d+)/modules/(\\w+)/(\\d+)/(\\w+)";
string moduleRegStr = string("/") + sn + "/ipcs/(\\d+)/modules/(\\w+)/(\\d+)/(\\w+)";
std::regex moduleRegex(moduleRegStr);
std::smatch results2;
if (std::regex_match(_path, results2, moduleRegex)) {
if (std::regex_match(path_, results2, moduleRegex)) {
if (results2.size() == 5) {
for(auto &v: results2) {
cout<< v.str() << endl;
int ipcIdx = stoi(results2[1].str());
int modIdx = stoi(results[3].str());
string modName = results[2].str();
string propName = results[4].str();
if(d["op"] == "replace"||d["op"] == "add") {
auto &oldMod = oldConfig[sn]["ipcs"][ipcIdx]["modules"][modIdx];
auto &newMod = newConfig[sn]["ipcs"][ipcIdx]["modules"][modIdx];
if(oldMod.count("iid") == 0 || newMod.count("iid") == 0) {
string msg = fmt::format("invalid module config ipcs[{}]['modules'][{}][{}] having no iid field", ipcIdx, modName, modIdx);
spdlog::error(msg);
ret["msg"] = msg;
hasError = true;
break;
}else{
if(modName == "evml") {
if(newMod.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{
modName = modName + newMod["type"].get<string>();
}
}
string oldGid = sn + ":" + modName + ":" + oldMod["iid"].get<string>();
string newGid = sn + ":" + modName + ":" + newMod["iid"].get<string>();
if(oldGid != newGid) {
ret["data"][oldGid] = 0;
}
if(propName == "enabled") {
if(newMod["enabled"].get<int>() == 0) {
ret["data"][newGid] = 0;
}else{
ret["data"][newGid] = 2;
}
}else{
// disabled
if(ret["data"].count(newGid) != 0 && ret["data"][newGid].get<int>() == 0) {
// nop
}else{
// restart
ret["data"][newGid] = 2;
}
}
}
}
}
}
}
if(hasError){
ret["code"] = 1;
}
}
}
}catch(exception &e) {
spdlog::error("getModulesOperFromConfDiff exception: {}", e.what());
ret["code"] = -1;
ret["msg"] = e.what();
}
return ret;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论