You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
11 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "main_control_service.hpp"
  2. #include "configs/project_setting.hpp"
  3. #include "iflytop/core/components/stringutils.hpp"
  4. #include "iflytop/core/core.hpp"
  5. #include "version.hpp"
  6. using namespace iflytop;
  7. using namespace core;
  8. using namespace std;
  9. using namespace nlohmann;
  10. #define BIND
  11. static void getJsonValFromJson(json j, int& val) {
  12. if (j.is_string()) {
  13. string valstr = j;
  14. val = atoi(valstr.c_str());
  15. } else if (j.is_number()) {
  16. val = j;
  17. } else {
  18. throw std::runtime_error("getJsonValFromJson(int) error");
  19. }
  20. }
  21. template <typename T>
  22. static T jsonGet(json j) {
  23. T val;
  24. getJsonValFromJson(j, val);
  25. return val;
  26. }
  27. void MainControlService::initialize() {
  28. /**
  29. * @brief
  30. */
  31. BUILD_AND_REG_SERRVICE(DeviceStateService);
  32. GET_SERVICE(DeviceStateService)->initialize();
  33. /**
  34. * @brief Get the to service object
  35. */
  36. GET_TO_SERVICE(m_zconfig);
  37. GET_TO_SERVICE(m_deviceStateService);
  38. m_disinfectionCtrlService.reset(new DisinfectionCtrlService());
  39. m_disinfectionCtrlService->initialize();
  40. m_restfulServer.reset(new RestfulServer());
  41. m_restfulServer->regAPI("/hello_world", RESTFUL_SERVER_BIND(MainControlService::hello_world));
  42. m_restfulServer->regAPI("/api1/script_processer/doscript", RESTFUL_SERVER_BIND(MainControlService::doscript));
  43. m_restfulServer->regAPI("/api1/script_processer/stopscript", RESTFUL_SERVER_BIND(MainControlService::stopscript));
  44. m_restfulServer->start(20000, 20001, "0.0.0.0");
  45. m_iflytopwsService.reset(new IflytopFrontEndService());
  46. m_iflytopwsService->initialize("0.0.0.0");
  47. m_iflytopwsService->onMessage.connect([this](weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) { processFrontEndMessage(webSocket, cmd, receipt); });
  48. m_iflytopwsService->startListen();
  49. m_dbService.reset(new DBService());
  50. m_dbService->initialize();
  51. m_reportThread.reset(new Thread("reportThread", [this]() {
  52. //
  53. // {
  54. // "command": "RealtimeSensorDataReport",
  55. // "sensor_data": {
  56. // "heating_strip": 1,
  57. // "air_compressor": 0,
  58. // "sprinkler_pump": 1,
  59. // "disinfectant_volume": 99,
  60. // "h2o2_1": 10,
  61. // "temp_1": 11,
  62. // "humid_1": 12,
  63. // "saturation_1": 13,
  64. // "h2o2_2": 14,
  65. // "temp_2": 15,
  66. // "humid_2": 16,
  67. // "saturation_2": 17,
  68. // "h2o2_3": 18,
  69. // "temp_3": 19,
  70. // "humid_3": 20,
  71. // "saturation_3": 21
  72. // }
  73. // }
  74. ThisThread thisThread;
  75. while (!thisThread.getExitFlag()) {
  76. json report;
  77. report["command"] = "RealtimeSensorDataReport";
  78. report["sensor_data"]["heating_strip"] = 1;
  79. report["sensor_data"]["air_compressor"] = 0;
  80. report["sensor_data"]["sprinkler_pump"] = 1;
  81. report["sensor_data"]["disinfectant_volume"] = 99;
  82. report["sensor_data"]["h2o2_1"] = 10;
  83. report["sensor_data"]["temp_1"] = 11;
  84. report["sensor_data"]["humid_1"] = 12;
  85. report["sensor_data"]["saturation_1"] = 13;
  86. report["sensor_data"]["h2o2_2"] = 14;
  87. report["sensor_data"]["temp_2"] = 15;
  88. report["sensor_data"]["humid_2"] = 16;
  89. report["sensor_data"]["saturation_2"] = 17;
  90. report["sensor_data"]["h2o2_3"] = 18;
  91. report["sensor_data"]["temp_3"] = 19;
  92. report["sensor_data"]["humid_3"] = 20;
  93. report["sensor_data"]["saturation_3"] = 21;
  94. m_iflytopwsService->sendReport(report);
  95. thisThread.sleepForMs(1000);
  96. }
  97. }));
  98. };
  99. void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  100. #if 0
  101. //login
  102. {
  103. "command":"login",
  104. "userid":"zhaohe",
  105. "passwd":"xxxxxx"
  106. }
  107. //unlogin
  108. {
  109. "command":"unlogin"
  110. }
  111. //chpasswd
  112. {
  113. "command":"chpasswd",
  114. "userId":"1",
  115. "passwd":"xxxxxxxxx"
  116. }
  117. //shutdown
  118. {
  119. "command":"shutdown",
  120. "delayms": 10,
  121. }
  122. //
  123. //startDisinfection
  124. {
  125. "command":"startDisinfection",
  126. }
  127. //stopDisinfection
  128. {
  129. "command":"stopDisinfection",
  130. }
  131. ---------------------------------------------------
  132. //getState
  133. {
  134. "command":"getState",
  135. }
  136. //receipt
  137. {
  138. "ackcode":0
  139. "state":{
  140. //存放所有当前设备的状态信息
  141. "loginuser":"",
  142. "permissionLevel":1,
  143. "workState":0, //0,idle,1,消毒中,2,测试中
  144. "estimatedRemainingTimeS":10000,//预计消毒结束剩余时间,数值是不断修正的,可能会变大。
  145. "disinfection_id":"2023-0825-111059"
  146. }
  147. }
  148. #endif
  149. string cmdstr = cmd["command"];
  150. /*******************************************************************************
  151. * LOGIN_CMD *
  152. *******************************************************************************/
  153. if (cmdstr == "login") {
  154. string uid = cmd["userid"];
  155. string pwd = cmd["passwd"];
  156. auto usr = m_dbService->getUser(uid);
  157. if (usr == nullptr) {
  158. logger->warn("login fail, user {} not exist", uid);
  159. receipt["ackcode"] = err::error_code_get_get_ecode(err::kcommon_error_code, err::kuser_not_exist);
  160. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kcommon_error_code, err::kuser_not_exist, "");
  161. return;
  162. }
  163. if (usr->passwd != pwd) {
  164. logger->warn("login fail, user {} passwd error", uid);
  165. receipt["ackcode"] = err::error_code_get_get_ecode(err::kcommon_error_code, err::kpasswd_error);
  166. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kcommon_error_code, err::kpasswd_error, "");
  167. return;
  168. }
  169. m_deviceStateService->setLoginState(uid, usr->permission_level, usr->visible);
  170. logger->info("user {} login success", uid);
  171. return;
  172. }
  173. /*******************************************************************************
  174. * unlogin *
  175. *******************************************************************************/
  176. if (cmdstr == "unlogin") {
  177. m_deviceStateService->unlogin();
  178. logger->info("user unlogin success");
  179. return;
  180. }
  181. /*******************************************************************************
  182. * chpasswd *
  183. *******************************************************************************/
  184. if (cmdstr == "chpasswd") {
  185. string uid = cmd["userid"];
  186. string pwd = cmd["passwd"];
  187. logger->info("changet passwd {} {}", uid, pwd);
  188. return;
  189. }
  190. /*******************************************************************************
  191. * shutdown *
  192. *******************************************************************************/
  193. if (cmdstr == "shutdown") {
  194. int delayms = jsonGet<int>(cmd["delayms"]);
  195. logger->info("shutdown {} ms", delayms);
  196. // system("shutdown -r -t");
  197. return;
  198. }
  199. /*******************************************************************************
  200. * *
  201. *******************************************************************************/
  202. if (cmdstr == "startDisinfection") {
  203. int loglevel = jsonGet<int>(cmd["loglevel"]);
  204. int roomVolume = jsonGet<int>(cmd["roomVolume"]); //
  205. m_disinfectionCtrlService->startDisinfection(loglevel, roomVolume);
  206. return;
  207. }
  208. if (cmdstr == "stopDisinfection") {
  209. m_disinfectionCtrlService->stopDisinfection();
  210. return;
  211. }
  212. /*******************************************************************************
  213. * getState *
  214. *******************************************************************************/
  215. if (cmdstr == "getState") {
  216. receipt["state"]["isLogin"] = m_deviceStateService->isLogin();
  217. receipt["state"]["loginuser"] = m_deviceStateService->getLoginUid();
  218. receipt["state"]["permissionLevel"] = m_deviceStateService->getLoginPermissionLevel();
  219. receipt["state"]["workState"] = m_disinfectionCtrlService->isDisinfectionRunning();
  220. receipt["state"]["estimatedRemainingTimeS"] = m_disinfectionCtrlService->getEstimatedRemainingTimeS();
  221. receipt["state"]["disinfection_id"] = m_disinfectionCtrlService->getDisinfectionID();
  222. return;
  223. }
  224. /*******************************************************************************
  225. * *
  226. *******************************************************************************/
  227. if (cmdstr == "getAllUser") {
  228. auto users = m_dbService->getAllUserJson();
  229. receipt["dbval"] = users;
  230. return;
  231. }
  232. if (cmdstr == "getAllSetting") {
  233. auto dbval = m_dbService->getAllSettingJson();
  234. receipt["dbval"] = dbval;
  235. return;
  236. }
  237. if (cmdstr == "getAllRecords") {
  238. string disinfection_id = cmd["disinfection_id"];
  239. auto dbval = m_dbService->getAllRecords(disinfection_id);
  240. receipt["dbval"] = dbval;
  241. return;
  242. }
  243. if (cmdstr == "setSettingVal") {
  244. string settingName = cmd["settingName"];
  245. int settingVal = jsonGet<int>(cmd["settingVal"]);
  246. bool suc = m_dbService->setSettingVal(settingName, settingVal);
  247. if (!suc) {
  248. receipt["ackcode"] = err::error_code_get_get_ecode(err::kcommon_error_code, err::kdb_operate_error);
  249. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kcommon_error_code, err::kdb_operate_error, "setSettingVal fail");
  250. }
  251. return;
  252. }
  253. }
  254. HttpResponsePtr MainControlService::hello_world( //
  255. HttpRequestPtr request, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState>) {
  256. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world");
  257. }
  258. HttpResponsePtr MainControlService::doscript(HttpRequestPtr httpreq, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState> conn) {
  259. // logger->info("do\n{}", httpreq->body);
  260. // if (m_a8000_script_processer->isWorking()) {
  261. // return std::make_shared<HttpResponse>(200, "FAIL", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script fail, script processer is running");
  262. // }
  263. // m_a8000_script_processer->executeScript(httpreq->body);
  264. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script success");
  265. }
  266. HttpResponsePtr MainControlService::stopscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>) {
  267. // m_a8000_script_processer->stopScript();
  268. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "stop script success");
  269. }