提交 e4928744 authored 作者: bruce.lu's avatar bruce.lu

init

上级 6c00a563
...@@ -31,68 +31,75 @@ class HttpSrv{ ...@@ -31,68 +31,75 @@ class HttpSrv{
ret["code"] = 0; ret["code"] = 0;
ret["msg"] = "ok"; ret["msg"] = "ok";
ret["time"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count(); ret["time"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
try{ spdlog::info(body);
json newConfig = json::parse(body); if(body.empty()){
if(newConfig.count("data") == 0 || newConfig["data"].size() == 0) { ret["code"] = 1;
ret["code"] = 1; ret["msg"] = "no body payload";
ret["msg"] = "evcloudsvc invalid config body received: " + body; }else{
spdlog::error(ret["msg"].get<string>()); try{
}else{ json newConfig = json::parse(body);
json &data = newConfig["data"]; if(newConfig.count("data") == 0 || newConfig["data"].size() == 0) {
for(auto &[k, v]: data.items()) { ret["code"] = 1;
// this is one evmgr ret["msg"] = "evcloudsvc invalid config body received: " + body;
if(v.count(k) == 0||v[k].size()==0) { spdlog::error(ret["msg"].get<string>());
ret["code"] = 2; }else{
ret["msg"] = "evcloudsvc invalid value for key " + k; json &data = newConfig["data"];
spdlog::error(ret["msg"].get<string>()); for(auto &[k, v]: data.items()) {
continue; // this is one evmgr
}else{ if(v.count(k) == 0||v[k].size()==0) {
// find all modules ret["code"] = 2;
if(v.count("ipcs") == 0||v["ipcs"].size() == 0) { ret["msg"] = "evcloudsvc invalid value for key " + k;
spdlog::error("invalid ipcs in config body"); spdlog::error(ret["msg"].get<string>());
continue; continue;
}else{ }else{
json &ipcs = v["ipcs"]; // find all modules
for(auto &ipc : ipcs) { if(v.count("ipcs") == 0||v["ipcs"].size() == 0) {
if(ipc.count("modules") == 0||ipc["modules"].size() == 0) { spdlog::error("invalid ipcs in config body");
spdlog::error("invalid modules in ipcs config body"); continue;
continue; }else{
}else{ json &ipcs = v["ipcs"];
json &modules = ipc["modules"]; for(auto &ipc : ipcs) {
for(auto &[mn, ml]: modules.items()) { if(ipc.count("modules") == 0||ipc["modules"].size() == 0) {
if(ml.count("sn") != 0 && ml["sn"].size() != 0){ spdlog::error("invalid modules in ipcs config body");
string modKey; continue;
//ml }else{
if(mn == "evml" && ml.count("type") != 0 && ml["type"].size() != 0) { json &modules = ipc["modules"];
modKey = ml["sn"].get<string>() +":evml:" + ml["type"].get<string>(); for(auto &[mn, ml]: modules.items()) {
}else{ if(ml.count("sn") != 0 && ml["sn"].size() != 0){
modKey = ml["sn"].get<string>() + ":" + mn; string modKey;
//ml
if(mn == "evml" && ml.count("type") != 0 && ml["type"].size() != 0) {
modKey = ml["sn"].get<string>() +":evml:" + ml["type"].get<string>();
}else{
modKey = ml["sn"].get<string>() + ":" + mn;
}
this->configMap[modKey] = v;
} }
this->configMap[modKey] = v; } // for modules
} }
} // for modules } // for ipc
} }
} // for ipc }
} // update evmgr config
} auto lastupdated = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
// update evmgr config json evmgrData;
auto lastupdated = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count(); v["lastupdated"] = lastupdated;
json evmgrData; evmgrData[k] = v;
v["lastupdated"] = lastupdated; //save
evmgrData[k] = v; LVDB::setLocalConfig(evmgrData, k);
//save } // for evmgr
LVDB::setLocalConfig(evmgrData, k);
} // for evmgr // save configmap
LVDB::setValue(this->configMap, "configmap");
// save configmap ret["data"] = newConfig["data"];
LVDB::setValue(this->configMap, "configmap"); }
}catch(exception &e) {
ret.clear();
ret["code"] = -1;
ret["msg"] = e.what();
} }
}catch(exception &e) {
ret.clear();
ret["code"] = -1;
ret["msg"] = e.what();
} }
return ret; return ret;
} }
...@@ -110,11 +117,10 @@ class HttpSrv{ ...@@ -110,11 +117,10 @@ class HttpSrv{
svr.Post("/register", [this](const Request& req, Response& res){ svr.Post("/register", [this](const Request& req, Response& res){
json ret; json ret;
try{ try{
json config = json::parse(req.body);
string sn = req.get_param_value("sn"); string sn = req.get_param_value("sn");
string module = req.get_param_value("module"); string module = req.get_param_value("module");
if(sn.empty()||module.empty()){ if(sn.empty()||module.empty()){
throw "no para sn/module"; throw StrException("no para sn/module");
} }
string modname = module.substr(0,4); string modname = module.substr(0,4);
if(modname == "evml") { if(modname == "evml") {
...@@ -130,7 +136,7 @@ class HttpSrv{ ...@@ -130,7 +136,7 @@ class HttpSrv{
spdlog::info("evcloudsvc no such edge module registred: {}, create new entry", key); spdlog::info("evcloudsvc no such edge module registred: {}, create new entry", key);
ret = this->config(req.body); ret = this->config(req.body);
if(ret["code"] == 0) { if(ret["code"] == 0) {
ret["data"] = config["data"]; //ret["data"] =ret["data"];
} }
}else{ }else{
// TODO: calc md5 // TODO: calc md5
......
...@@ -108,4 +108,12 @@ vector<string> split(const std::string& s, char delimiter) ...@@ -108,4 +108,12 @@ vector<string> split(const std::string& s, char delimiter)
} // namespace cloudutils } // namespace cloudutils
struct StrException : public std::exception
{
std::string s;
StrException(std::string ss) : s(ss) {}
~StrException() throw () {} // Updated
const char* what() const throw() { return s.c_str(); }
};
#endif #endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论