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.

612 lines
27 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
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
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
2 years ago
2 years ago
2 years ago
  1. #include "main_control_service.hpp"
  2. #include "configs/project_setting.hpp"
  3. #include "iflytop/components/zcanreceiver/zcanreceiverhost.hpp"
  4. #include "iflytop/core/components/stringutils.hpp"
  5. #include "iflytop/core/core.hpp"
  6. #include "version.hpp"
  7. using namespace iflytop;
  8. using namespace core;
  9. using namespace std;
  10. using namespace nlohmann;
  11. #define BIND
  12. static void getJsonValFromJson(json j, int& val) {
  13. if (j.is_string()) {
  14. string valstr = j;
  15. val = atoi(valstr.c_str());
  16. } else if (j.is_number()) {
  17. val = j;
  18. } else {
  19. throw std::runtime_error("getJsonValFromJson(int) error");
  20. }
  21. }
  22. template <typename T>
  23. static T jsonGet(json j) {
  24. T val;
  25. getJsonValFromJson(j, val);
  26. return val;
  27. }
  28. void MainControlService::dosystem(string order) {
  29. logger->info("do:{}", order);
  30. system(order.c_str());
  31. }
  32. void MainControlService::initialize() {
  33. GET_TO_SERVICE(m_zconfig);
  34. // system("systemctl restart zchromium.service");
  35. // m_dbService.reset(new DBService());
  36. // m_dbService->initialize();
  37. BUILD_AND_REG_SERRVICE(DBService);
  38. GET_SERVICE(DBService)->initialize();
  39. GET_TO_SERVICE(m_dbService);
  40. /**
  41. * @brief
  42. */
  43. BUILD_AND_REG_SERRVICE(DeviceStateService);
  44. GET_SERVICE(DeviceStateService)->initialize();
  45. GET_TO_SERVICE(m_deviceStateService);
  46. BUILD_AND_REG_SERRVICE(ZCanHost);
  47. GET_SERVICE(ZCanHost)->initialize(m_zconfig->get_iflytopSubDeviceCanIFName(), m_zconfig->get_iflytopSubDeviceCanBitrate(), false);
  48. GET_TO_SERVICE(m_zcanhost);
  49. BUILD_AND_REG_SERRVICE(DisinfectionLogsManager);
  50. GET_SERVICE(DisinfectionLogsManager)->initialize();
  51. GET_TO_SERVICE(m_disinfectionLogsManager);
  52. BUILD_AND_REG_SERRVICE(DeviceIoControlService);
  53. GET_SERVICE(DeviceIoControlService)->initialize();
  54. GET_TO_SERVICE(m_deviceIoControlService);
  55. m_deviceIoControlService->startScan();
  56. /**
  57. * @brief Get the to service object
  58. */
  59. m_disinfectionCtrlService.reset(new DisinfectionCtrlService());
  60. m_disinfectionCtrlService->initialize();
  61. m_restfulServer.reset(new RestfulServer());
  62. m_restfulServer->regAPI("/hello_world", RESTFUL_SERVER_BIND(MainControlService::hello_world));
  63. m_restfulServer->regAPI("/api1/script_processer/doscript", RESTFUL_SERVER_BIND(MainControlService::doscript));
  64. m_restfulServer->regAPI("/api1/script_processer/stopscript", RESTFUL_SERVER_BIND(MainControlService::stopscript));
  65. m_restfulServer->start(20000, 20001, "0.0.0.0");
  66. m_iflytopwsService.reset(new IflytopFrontEndService());
  67. m_iflytopwsService->initialize("0.0.0.0");
  68. m_iflytopwsService->onMessage.connect([this](weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  69. string cmdstr = cmd["command"];
  70. if (cmdstr != "getState") {
  71. logger->info("rx:{}", cmd.dump());
  72. }
  73. processFrontEndMessage(webSocket, cmd, receipt);
  74. if (cmdstr != "getState") {
  75. logger->info("tx:{}", receipt.dump());
  76. }
  77. });
  78. m_iflytopwsService->onUdpCmdMessage.connect([this](struct sockaddr_in* from, char* data, size_t len) {
  79. //
  80. for (size_t i = 0; i < len; i++) {
  81. if (data[i] == '\r' || data[i] == '\n') {
  82. data[i] = '\0';
  83. }
  84. }
  85. bool execsuc = false;
  86. for (size_t i = 0; i < len; i++) {
  87. if (data[i] != '\0') {
  88. int inext = strlen(&data[i]) + i;
  89. {
  90. string retmsg;
  91. execsuc = m_zcanhost->execcmd(string(&data[i]), retmsg);
  92. string retmsgstr = fmt::format("{},{}", execsuc ? "suc" : "fail", retmsg);
  93. m_iflytopwsService->sendToUDP(from, retmsgstr.c_str(), retmsgstr.size());
  94. }
  95. i = inext;
  96. if (!execsuc) {
  97. break;
  98. }
  99. }
  100. }
  101. });
  102. m_iflytopwsService->startListen();
  103. m_reportThread.reset(new Thread("reportThread", [this]() {
  104. ThisThread thisThread;
  105. while (!thisThread.getExitFlag()) {
  106. json report;
  107. report["command"] = "RealtimeSensorDataReport";
  108. report["sensor_data"] = createSensorDataJson();
  109. m_iflytopwsService->sendReport(report);
  110. thisThread.sleepForMs(1000);
  111. }
  112. }));
  113. };
  114. json MainControlService::createSensorDataJson() {
  115. json report;
  116. report["airCompressor"]["io1"] = m_deviceIoControlService->airCompressor_getio1();
  117. report["airCompressor"]["io2"] = m_deviceIoControlService->airCompressor_getio2();
  118. report["airCompressor"]["currentVal"] = m_deviceIoControlService->airCompressor_getcurrentValue();
  119. report["airBlower"]["io1"] = m_deviceIoControlService->airBlower_getio1();
  120. report["airBlower"]["io2"] = m_deviceIoControlService->airBlower_getio2();
  121. report["airBlower"]["currentVal"] = m_deviceIoControlService->airBlower_getcurrentValue();
  122. report["heatingStrip"]["io1"] = m_deviceIoControlService->heatingStrip_getio1();
  123. report["heatingStrip"]["io2"] = m_deviceIoControlService->heatingStrip_getio2();
  124. report["heatingStrip"]["currentVal"] = m_deviceIoControlService->heatingStrip_getcurrentValue();
  125. report["sprinklerPumpRPM"] = m_deviceIoControlService->sprayLiquidPump_getRPM();
  126. report["chargingPumpRPM"] = m_deviceIoControlService->getChargingPump_PumpRPM();
  127. report["sprinklerPumpGPM"] = m_deviceIoControlService->sprayLiquidPump_getGPM();
  128. // report["chargingPumpGPM"] = m_deviceIoControlService->getChargingPump_PumpGPM();
  129. report["waterImmersionSensor1"] = m_deviceIoControlService->getWaterImmersionSensor1();
  130. report["waterImmersionSensor2"] = m_deviceIoControlService->getWaterImmersionSensor2();
  131. // Water immersion sensor
  132. report["disinfectant_volume"] = m_deviceIoControlService->getDisinfectantVolume_g();
  133. // report["h2o2_1"] = m_deviceIoControlService->getH2O2SenSorData1().h2o2;
  134. report["h2o2_1"] = m_deviceIoControlService->getH2O2SenSorData1().h2o2;
  135. report["temp_1"] = m_deviceIoControlService->getH2O2SenSorData1().temp;
  136. report["humid_1"] = m_deviceIoControlService->getH2O2SenSorData1().humid;
  137. report["saturation_1"] = m_deviceIoControlService->getH2O2SenSorData1().saturation + 1; // 为了让曲线不重叠
  138. // logger->info("m_deviceIoControlService->getH2O2SenSorData1().h2o2 {}", m_deviceIoControlService->getH2O2SenSorData1().h2o2);
  139. report["h2o2_2"] = -1;
  140. report["temp_2"] = -1;
  141. report["humid_2"] = -1;
  142. report["saturation_2"] = -1;
  143. report["h2o2_3"] = -1;
  144. report["temp_3"] = -1;
  145. report["humid_3"] = -1;
  146. report["saturation_3"] = -1;
  147. return report;
  148. }
  149. // {"command":"startReplenishingFluids","messageId":"startReplenishingFluids","stopAt":123}
  150. void MainControlService::processFrontEndMessage_userOperate(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  151. string cmdstr = cmd["command"];
  152. /*******************************************************************************
  153. * LOGIN_CMD *
  154. *******************************************************************************/
  155. if (cmdstr == "login") {
  156. string uid = cmd["userid"];
  157. string pwd = cmd["passwd"];
  158. if (uid == "admin" && pwd == "iflytop9973") {
  159. m_deviceStateService->setLoginState("admin", 0, 1);
  160. logger->info("user {} login success", "admin");
  161. } else {
  162. if (!m_dbService->isUserExist(uid)) {
  163. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kuser_not_exist);
  164. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kuser_not_exist, "");
  165. return;
  166. }
  167. if (!m_dbService->ispasswdCorrect(uid, pwd)) {
  168. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kpasswd_error);
  169. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kpasswd_error, "");
  170. return;
  171. }
  172. m_deviceStateService->setLoginState(uid, m_dbService->getUser(uid)->permission_level, m_dbService->getUser(uid)->visible);
  173. logger->info("user {} login success", uid);
  174. }
  175. return;
  176. }
  177. /*******************************************************************************
  178. * unlogin *
  179. *******************************************************************************/
  180. if (cmdstr == "unlogin") {
  181. m_deviceStateService->unlogin();
  182. logger->info("user unlogin success");
  183. return;
  184. }
  185. /*******************************************************************************
  186. * chpasswd *
  187. *******************************************************************************/
  188. if (cmdstr == "chpasswd") {
  189. string uid = cmd["userId"];
  190. string newpasswd = cmd["newpasswd"];
  191. string passwd = cmd["passwd"];
  192. logger->info("changet passwd {} {}", uid, passwd);
  193. if (!m_dbService->isUserExist(uid)) {
  194. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kuser_not_exist);
  195. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kuser_not_exist, "");
  196. return;
  197. }
  198. if (!m_dbService->ispasswdCorrect(uid, passwd)) {
  199. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kpasswd_error);
  200. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kpasswd_error, "");
  201. return;
  202. }
  203. m_dbService->changePasswd(uid, newpasswd);
  204. return;
  205. }
  206. if (cmdstr == "addUser") {
  207. string uid = cmd["uid"];
  208. string passwd = cmd["passwd"];
  209. int permission_level = jsonGet<int>(cmd["permission_level"]);
  210. m_dbService->addUser(uid, passwd, permission_level);
  211. return;
  212. }
  213. if (cmdstr == "delUser") {
  214. int id = jsonGet<int>(cmd["id"]);
  215. m_dbService->delUser(id);
  216. return;
  217. }
  218. if (cmdstr == "updateUserPermissionLevel") {
  219. int id = jsonGet<int>(cmd["id"]);
  220. int permission_level = jsonGet<int>(cmd["permission_level"]);
  221. m_dbService->updateUserPermissionLevel(id, permission_level);
  222. return;
  223. }
  224. if (cmdstr == "getAllUser") {
  225. auto users = m_dbService->getAllUserJson();
  226. receipt["dbval"] = users;
  227. return;
  228. }
  229. }
  230. void MainControlService::processFrontEndMessage_systemOperate(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  231. string cmdstr = cmd["command"];
  232. /*******************************************************************************
  233. * shutdown *
  234. *******************************************************************************/
  235. if (cmdstr == "shutdown") {
  236. int delayms = jsonGet<int>(cmd["delayms"]);
  237. logger->info("shutdown {} ms", delayms);
  238. m_autoshutdownThread.reset(new Thread("autoShutdown", [delayms, this]() {
  239. ThisThread thisThread;
  240. thisThread.sleepForMs(delayms);
  241. dosystem("shutdown -h now");
  242. }));
  243. return;
  244. }
  245. if (cmdstr == "updateDate") {
  246. #if 0
  247. {
  248. "command":"updateDate",
  249. "messageId":"1111222333444555",
  250. "year":2023,
  251. "month":8,
  252. "day":25,
  253. }
  254. #endif
  255. int32_t year = jsonGet<int>(cmd["year"]);
  256. int32_t month = jsonGet<int>(cmd["month"]);
  257. int32_t day = jsonGet<int>(cmd["day"]);
  258. logger->info("updateDate {} {} {}", year, month, day);
  259. // date -s "2023-01-02 02:32:32"
  260. dosystem(fmt::format("date -s \"{}{:02}{:02} `date +%T`\"", year, month, day).c_str());
  261. dosystem(fmt::format("hwclock -w").c_str());
  262. return;
  263. }
  264. if (cmdstr == "updateTime") {
  265. #if 0
  266. {
  267. "command":"updateTime",
  268. "messageId":"1111222333444555",
  269. "hour":11,
  270. "min":12,
  271. "second":13,
  272. }
  273. #endif
  274. int32_t hour = jsonGet<int>(cmd["hour"]);
  275. int32_t min = jsonGet<int>(cmd["min"]);
  276. int32_t second = jsonGet<int>(cmd["second"]);
  277. logger->info("updateDate {}:{}:{}", hour, min, second);
  278. // date -s "2023-01-02 02:32:32"
  279. dosystem(fmt::format("date -s \"`date +%Y-%m-%d` {:02}:{:02}:{:02}\"", hour, min, second).c_str());
  280. dosystem(fmt::format("hwclock -w").c_str());
  281. return;
  282. }
  283. }
  284. void MainControlService::processFrontEndMessage_Disinfection(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  285. /*******************************************************************************
  286. * *
  287. *******************************************************************************/
  288. string cmdstr = cmd["command"];
  289. if (cmdstr == "startDisinfection") {
  290. m_disinfectionCtrlService->startDisinfection(jsonGet<int>(cmd["loglevel"]), //
  291. m_dbService->getSettingVal("injection_pump_speed"), //
  292. m_dbService->getSettingVal("stoped_gs"), //
  293. m_dbService->getSettingVal("continued_gs"), //
  294. m_dbService->getSettingVal("stoped_satur"), //
  295. m_dbService->getSettingVal("continued_satur"), //
  296. m_dbService->getSettingVal("stoped_humi"), //
  297. m_dbService->getSettingVal("continued_humi"));
  298. return;
  299. }
  300. if (cmdstr == "changeDisinfectionParameter") {
  301. m_disinfectionCtrlService->changeDisinfectionParameter(jsonGet<int>(cmd["injection_pump_speed"]), //
  302. jsonGet<int>(cmd["stoped_gs"]), //
  303. jsonGet<int>(cmd["continued_gs"]), //
  304. jsonGet<int>(cmd["stoped_satur"]), //
  305. jsonGet<int>(cmd["continued_satur"]), //
  306. jsonGet<int>(cmd["stoped_humi"]), //
  307. jsonGet<int>(cmd["continued_humi"]));
  308. return;
  309. }
  310. if (cmdstr == "stopDisinfection") {
  311. m_disinfectionCtrlService->stopDisinfection();
  312. return;
  313. }
  314. if (cmdstr == "startReplenishingFluids") {
  315. int16_t stopAt = jsonGet<int>(cmd["stopAt"]);
  316. logger->info("startReplenishingFluids {}", stopAt);
  317. m_disinfectionCtrlService->startReplenishingFluids(stopAt);
  318. return;
  319. }
  320. if (cmdstr == "stopReplenishingFluids") {
  321. logger->info("stopReplenishingFluids");
  322. m_disinfectionCtrlService->stopReplenishingFluids();
  323. return;
  324. }
  325. if (cmdstr == "startDraining") {
  326. logger->info("startDraining");
  327. m_disinfectionCtrlService->startDraining();
  328. return;
  329. }
  330. if (cmdstr == "stopDraining") {
  331. logger->info("stopDraining");
  332. m_disinfectionCtrlService->stopDraining();
  333. return;
  334. }
  335. }
  336. void MainControlService::processFrontEndMessage_test(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  337. string cmdstr = cmd["command"];
  338. if (cmdstr == "test_sprayLiquidPumpCtrl") {
  339. int ctrl = jsonGet<int>(cmd["ctrl"]);
  340. int speed = jsonGet<int>(cmd["speed"]);
  341. if (ctrl == 1) {
  342. m_deviceIoControlService->sprayLiquidPump_open_for_test(speed);
  343. } else if (ctrl == -1) {
  344. m_deviceIoControlService->sprayLiquidPump_open_for_test(-speed);
  345. } else {
  346. m_deviceIoControlService->sprayLiquidPump_close();
  347. }
  348. return;
  349. }
  350. if (cmdstr == "test_replenishingFluidsPumpCtrl") {
  351. int ctrl = jsonGet<int>(cmd["ctrl"]);
  352. int speed = jsonGet<int>(cmd["speed"]);
  353. if (ctrl == 1) {
  354. m_deviceIoControlService->replenishingFluidsPump_open_for_test(speed);
  355. } else if (ctrl == -1) {
  356. m_deviceIoControlService->replenishingFluidsPump_open_for_test(-speed);
  357. } else {
  358. m_deviceIoControlService->replenishingFluidsPump_close_for_test();
  359. }
  360. return;
  361. }
  362. /*******************************************************************************
  363. * *
  364. *******************************************************************************/
  365. // {
  366. // "command":"exceCanCmd",
  367. // "cancmd": "",
  368. // }
  369. if (cmdstr == "exceCanCmd") {
  370. string cancmd = cmd["cancmd"];
  371. logger->info("exceCanCmd {}", cancmd);
  372. string receipt_str;
  373. bool suc = m_zcanhost->execcmd(cancmd, receipt_str);
  374. receipt["receipt_str"] = receipt_str;
  375. if (!suc) {
  376. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kfail);
  377. }
  378. return;
  379. }
  380. }
  381. void MainControlService::processFrontEndMessage_setting(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  382. string cmdstr = cmd["command"];
  383. if (cmdstr == "getAllSetting") {
  384. auto dbval = m_dbService->getAllSettingJson();
  385. receipt["dbval"] = dbval;
  386. return;
  387. }
  388. if (cmdstr == "setSettingVal") {
  389. string settingName = cmd["settingName"];
  390. int settingVal = jsonGet<int>(cmd["settingVal"]);
  391. bool suc = m_dbService->setSettingVal(settingName, settingVal);
  392. if (!suc) {
  393. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kdb_operate_error);
  394. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kdb_operate_error, "setSettingVal fail");
  395. }
  396. return;
  397. }
  398. }
  399. void MainControlService::processFrontEndMessage_processFormulaCmd(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  400. /**
  401. * @brief
  402. */
  403. string cmdstr = cmd["command"];
  404. if (cmdstr == "startFormula") {
  405. int id = jsonGet<int>(cmd["id"]);
  406. auto formul = m_dbService->getFormula(id);
  407. if (!formul) {
  408. logger->error("formula id {} not exist", id);
  409. receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kdb_operate_error);
  410. receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kdb_operate_error, "");
  411. return;
  412. }
  413. m_disinfectionCtrlService->startDisinfection(atoi(formul->loglevel.c_str()), //
  414. atoi(formul->injection_pump_speed.c_str()), //
  415. atoi(formul->stoped_gs.c_str()), //
  416. atoi(formul->continued_gs.c_str()), //
  417. atoi(formul->stoped_satur.c_str()), //
  418. atoi(formul->continued_satur.c_str()), //
  419. atoi(formul->stoped_humi.c_str()), //
  420. atoi(formul->continued_humi.c_str())); //
  421. return;
  422. }
  423. if (cmdstr == "getAllFormula") {
  424. receipt["formula"] = m_dbService->getAllFormulaJson();
  425. return;
  426. }
  427. if (cmdstr == "addFormula") {
  428. m_dbService->addFormula(cmd["formula_id"], //
  429. jsonGet<int>(cmd["stoped_gs"]), //
  430. jsonGet<int>(cmd["continued_gs"]), //
  431. jsonGet<int>(cmd["stoped_satur"]), //
  432. jsonGet<int>(cmd["continued_satur"]), //
  433. jsonGet<int>(cmd["stoped_humi"]), //
  434. jsonGet<int>(cmd["continued_humi"]), //
  435. jsonGet<int>(cmd["injection_pump_speed"]));
  436. return;
  437. }
  438. if (cmdstr == "delFormula") {
  439. m_dbService->delFormula(jsonGet<int>(cmd["id"]));
  440. return;
  441. }
  442. if (cmdstr == "updateFormula") {
  443. string val_str;
  444. if (cmd["val"].is_string()) {
  445. val_str = cmd["val"];
  446. } else {
  447. val_str = std::to_string(cmd["val"].get<int>());
  448. }
  449. m_dbService->updateFormula(jsonGet<int>(cmd["id"]), cmd["column"], val_str);
  450. return;
  451. }
  452. }
  453. void MainControlService::processFrontEndMessage_processBehaviorRecordCmd(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  454. string cmdstr = cmd["command"];
  455. if (cmdstr == "getUserBehaviorRecordDescJson") {
  456. receipt["records"] = m_dbService->getUserBehaviorRecordDescJson(jsonGet<int>(cmd["page"]), jsonGet<int>(cmd["page_size"]));
  457. return;
  458. }
  459. if (cmdstr == "cleanUserBehaviorRecord") {
  460. m_dbService->cleanUserBehaviorRecord();
  461. return;
  462. }
  463. // 导入用户行为测试数据
  464. if (cmdstr == "importTestData") {
  465. for (size_t i = 0; i < 100; i++) {
  466. logger->info("addUserBehaviorRecord {}", i);
  467. m_dbService->addUserBehaviorRecord("admin", kbehavior_login, "(admin)");
  468. m_dbService->addUserBehaviorRecord("admin", kbehavior_logout, "(admin)");
  469. m_dbService->addUserBehaviorRecord("admin", kbehavior_add_user, "(zhaohe)");
  470. m_dbService->addUserBehaviorRecord("admin", kbehavior_del_user, "(zhaohe)");
  471. m_dbService->addUserBehaviorRecord("admin", kbehavior_update_user_permission_level, "(zhaohe,3)");
  472. m_dbService->addUserBehaviorRecord("admin", kbehavior_update_user_passwd, "()");
  473. m_dbService->addUserBehaviorRecord("admin", kbehavior_set_setting_val, "(....)");
  474. m_dbService->addUserBehaviorRecord("admin", kbehavior_add_formula, "(....)");
  475. m_dbService->addUserBehaviorRecord("admin", kbehavior_del_formula, "(....)");
  476. m_dbService->addUserBehaviorRecord("admin", kbehavior_update_formula, "(....)");
  477. m_dbService->addUserBehaviorRecord("admin", kbehavior_do_disinfection, "(....)");
  478. m_dbService->addUserBehaviorRecord("admin", kbehavior_stop_disinfection, "(....)");
  479. m_dbService->addUserBehaviorRecord("admin", kbehavior_do_formula, "(....)");
  480. m_dbService->addUserBehaviorRecord("admin", kbehavior_update_setting_val_on_disinfection, "(....)");
  481. m_dbService->addUserBehaviorRecord("admin", kbehavior_export_data, "(....)");
  482. }
  483. return;
  484. }
  485. }
  486. void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
  487. string cmdstr = cmd["command"];
  488. processFrontEndMessage_userOperate(webSocket, cmd, receipt);
  489. processFrontEndMessage_systemOperate(webSocket, cmd, receipt);
  490. processFrontEndMessage_Disinfection(webSocket, cmd, receipt);
  491. processFrontEndMessage_test(webSocket, cmd, receipt);
  492. processFrontEndMessage_setting(webSocket, cmd, receipt);
  493. processFrontEndMessage_processFormulaCmd(webSocket, cmd, receipt);
  494. processFrontEndMessage_processBehaviorRecordCmd(webSocket, cmd, receipt);
  495. /*******************************************************************************
  496. * getState *
  497. *******************************************************************************/
  498. if (cmdstr == "getState") {
  499. receipt["state"]["isLogin"] = m_deviceStateService->isLogin();
  500. receipt["state"]["loginuser"] = m_deviceStateService->getLoginUid();
  501. receipt["state"]["permissionLevel"] = m_deviceStateService->getLoginPermissionLevel();
  502. // receipt["state"]["workState"] = m_disinfectionCtrlService->isDisinfectionRunning();
  503. /*******************************************************************************
  504. * disinfectionState *
  505. *******************************************************************************/
  506. receipt["state"]["replenishingFluidsWorkState"] = m_disinfectionCtrlService->getReplenishingFluidsWorkState();
  507. receipt["state"]["drainingWorkState"] = m_disinfectionCtrlService->getDrainingWorkState();
  508. /**
  509. * @brief
  510. */
  511. receipt["state"]["preHeat"] = m_disinfectionCtrlService->isPreheatState();
  512. receipt["state"]["preHeatRaminTimeS"] = m_disinfectionCtrlService->getPreHeatRaminTimeS(); // 预热剩余时间
  513. receipt["state"]["estimatedRemainingTimeS"] = m_disinfectionCtrlService->getEstimatedRemainingTimeS();
  514. receipt["state"]["disinfection_id"] = m_disinfectionCtrlService->getDisinfectionID();
  515. // 消毒状态
  516. receipt["state"]["disinfectionWorkState"] = m_disinfectionCtrlService->getDisinfectionWorkState();
  517. receipt["state"]["disinfectionState"]["id"] = m_disinfectionCtrlService->getDisinfectionID();
  518. receipt["state"]["disinfectionState"]["estimatedRemainingTimeS"] = m_disinfectionCtrlService->getEstimatedRemainingTimeS();
  519. receipt["state"]["disinfectionState"]["workState"] = m_disinfectionCtrlService->getDisinfectionWorkState();
  520. receipt["state"]["disinfectionState"]["preHeat"] = m_disinfectionCtrlService->isPreheatState();
  521. receipt["state"]["disinfectionState"]["preHeatRaminTimeS"] = m_disinfectionCtrlService->getPreHeatRaminTimeS(); // 预热剩余时间
  522. receipt["state"]["disinfectionState"]["nowlog"] = m_disinfectionCtrlService->m_context.m_nowLoglevel;
  523. receipt["state"]["disinfectionState"]["targetlog"] = m_disinfectionCtrlService->m_context.m_targetLoglevel;
  524. receipt["state"]["disinfectionState"]["dvalue"] = m_disinfectionCtrlService->m_context.dvalue;
  525. /*******************************************************************************
  526. * sensor *
  527. *******************************************************************************/
  528. receipt["state"]["sensor_data"] = createSensorDataJson();
  529. /*******************************************************************************
  530. * disinfectionConfig *
  531. *******************************************************************************/
  532. receipt["disinfectionConfig"]["loglevel"] = m_disinfectionCtrlService->m_context.m_targetLoglevel;
  533. receipt["disinfectionConfig"]["injection_pump_speed"] = m_disinfectionCtrlService->m_context.injection_pump_speed;
  534. receipt["disinfectionConfig"]["stoped_gs"] = m_disinfectionCtrlService->m_context.stoped_gs;
  535. receipt["disinfectionConfig"]["continued_gs"] = m_disinfectionCtrlService->m_context.continued_gs;
  536. receipt["disinfectionConfig"]["stoped_satur"] = m_disinfectionCtrlService->m_context.stoped_satur;
  537. receipt["disinfectionConfig"]["continued_satur"] = m_disinfectionCtrlService->m_context.continued_satur;
  538. receipt["disinfectionConfig"]["stoped_humi"] = m_disinfectionCtrlService->m_context.stoped_humi;
  539. receipt["disinfectionConfig"]["continued_humi"] = m_disinfectionCtrlService->m_context.continued_humi;
  540. return;
  541. }
  542. }
  543. HttpResponsePtr MainControlService::hello_world( //
  544. HttpRequestPtr request, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState>) {
  545. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world");
  546. }
  547. HttpResponsePtr MainControlService::doscript(HttpRequestPtr httpreq, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState> conn) {
  548. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script success");
  549. }
  550. HttpResponsePtr MainControlService::stopscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>) {
  551. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "stop script success");
  552. }