|
|
#include "main_control_service.hpp"
#include "configs/project_setting.hpp"
#include "iflytop/core/components/stringutils.hpp"
#include "iflytop/core/core.hpp"
#include "version.hpp"
using namespace iflytop; using namespace core; using namespace std; using namespace nlohmann;
#define BIND
static void getJsonValFromJson(json j, int& val) { if (j.is_string()) { string valstr = j; val = atoi(valstr.c_str()); } else if (j.is_number()) { val = j; } else { throw std::runtime_error("getJsonValFromJson(int) error"); } }
template <typename T> static T jsonGet(json j) { T val; getJsonValFromJson(j, val); return val; }
void MainControlService::initialize() { /**
* @brief 构造系统中的单例服务 */ BUILD_AND_REG_SERRVICE(DeviceStateService); GET_SERVICE(DeviceStateService)->initialize();
/**
* @brief Get the to service object */ GET_TO_SERVICE(m_zconfig); GET_TO_SERVICE(m_deviceStateService);
m_disinfectionCtrlService.reset(new DisinfectionCtrlService()); m_disinfectionCtrlService->initialize();
m_restfulServer.reset(new RestfulServer()); m_restfulServer->regAPI("/hello_world", RESTFUL_SERVER_BIND(MainControlService::hello_world)); m_restfulServer->regAPI("/api1/script_processer/doscript", RESTFUL_SERVER_BIND(MainControlService::doscript)); m_restfulServer->regAPI("/api1/script_processer/stopscript", RESTFUL_SERVER_BIND(MainControlService::stopscript)); m_restfulServer->start(20000, 20001, "0.0.0.0");
m_iflytopwsService.reset(new IflytopFrontEndService()); m_iflytopwsService->initialize("0.0.0.0"); m_iflytopwsService->onMessage.connect([this](weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) { processFrontEndMessage(webSocket, cmd, receipt); }); m_iflytopwsService->startListen();
m_dbService.reset(new DBService()); m_dbService->initialize();
m_reportThread.reset(new Thread("reportThread", [this]() { //
// {
// "command": "RealtimeSensorDataReport",
// "sensor_data": {
// "heating_strip": 1,
// "air_compressor": 0,
// "sprinkler_pump": 1,
// "disinfectant_volume": 99,
// "h2o2_1": 10,
// "temp_1": 11,
// "humid_1": 12,
// "saturation_1": 13,
// "h2o2_2": 14,
// "temp_2": 15,
// "humid_2": 16,
// "saturation_2": 17,
// "h2o2_3": 18,
// "temp_3": 19,
// "humid_3": 20,
// "saturation_3": 21
// }
// }
ThisThread thisThread; while (!thisThread.getExitFlag()) { json report; report["command"] = "RealtimeSensorDataReport"; report["sensor_data"]["heating_strip"] = 1; report["sensor_data"]["air_compressor"] = 0; report["sensor_data"]["sprinkler_pump"] = 1; report["sensor_data"]["disinfectant_volume"] = 99; report["sensor_data"]["h2o2_1"] = 10; report["sensor_data"]["temp_1"] = 11; report["sensor_data"]["humid_1"] = 12; report["sensor_data"]["saturation_1"] = 13; report["sensor_data"]["h2o2_2"] = 14; report["sensor_data"]["temp_2"] = 15; report["sensor_data"]["humid_2"] = 16; report["sensor_data"]["saturation_2"] = 17; report["sensor_data"]["h2o2_3"] = 18; report["sensor_data"]["temp_3"] = 19; report["sensor_data"]["humid_3"] = 20; report["sensor_data"]["saturation_3"] = 21; m_iflytopwsService->sendReport(report); thisThread.sleepForMs(1000); } })); };
void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) { #if 0
//login
{ "command":"login", "userid":"zhaohe", "passwd":"xxxxxx" } //unlogin
{ "command":"unlogin" } //chpasswd
{ "command":"chpasswd", "userId":"1", "passwd":"xxxxxxxxx" } //shutdown
{ "command":"shutdown", "delayms": 10, } //
//startDisinfection
{ "command":"startDisinfection", }
//stopDisinfection
{ "command":"stopDisinfection", } --------------------------------------------------- //getState
{ "command":"getState", } //receipt
{ "ackcode":0 "state":{ //存放所有当前设备的状态信息
"loginuser":"", "permissionLevel":1, "workState":0, //0,idle,1,消毒中,2,测试中
"estimatedRemainingTimeS":10000,//预计消毒结束剩余时间,数值是不断修正的,可能会变大。
"disinfection_id":"2023-0825-111059" } } #endif
string cmdstr = cmd["command"]; /*******************************************************************************
* LOGIN_CMD * *******************************************************************************/ if (cmdstr == "login") { string uid = cmd["userid"]; string pwd = cmd["passwd"]; auto usr = m_dbService->getUser(uid); if (usr == nullptr) { logger->warn("login fail, user {} not exist", uid); receipt["ackcode"] = err::error_code_get_get_ecode(err::kcommon_error_code, err::kuser_not_exist); receipt["ackcodeInfo"] = err::error_code_get_desc(err::kcommon_error_code, err::kuser_not_exist, ""); return; }
if (usr->passwd != pwd) { logger->warn("login fail, user {} passwd error", uid); receipt["ackcode"] = err::error_code_get_get_ecode(err::kcommon_error_code, err::kpasswd_error); receipt["ackcodeInfo"] = err::error_code_get_desc(err::kcommon_error_code, err::kpasswd_error, ""); return; }
m_deviceStateService->setLoginState(uid, usr->permission_level, usr->visible); logger->info("user {} login success", uid); return; }
/*******************************************************************************
* unlogin * *******************************************************************************/ if (cmdstr == "unlogin") { m_deviceStateService->unlogin(); logger->info("user unlogin success"); return; }
/*******************************************************************************
* chpasswd * *******************************************************************************/ if (cmdstr == "chpasswd") { string uid = cmd["userid"]; string pwd = cmd["passwd"]; logger->info("changet passwd {} {}", uid, pwd); return; }
/*******************************************************************************
* shutdown * *******************************************************************************/ if (cmdstr == "shutdown") { int delayms = jsonGet<int>(cmd["delayms"]); logger->info("shutdown {} ms", delayms); // system("shutdown -r -t");
return; }
/*******************************************************************************
* 消毒相关指令 * *******************************************************************************/
if (cmdstr == "startDisinfection") { int loglevel = jsonGet<int>(cmd["loglevel"]); int roomVolume = jsonGet<int>(cmd["roomVolume"]); //
m_disinfectionCtrlService->startDisinfection(loglevel, roomVolume); return; }
if (cmdstr == "stopDisinfection") { m_disinfectionCtrlService->stopDisinfection(); return; }
/*******************************************************************************
* getState * *******************************************************************************/ if (cmdstr == "getState") { receipt["state"]["isLogin"] = m_deviceStateService->isLogin(); receipt["state"]["loginuser"] = m_deviceStateService->getLoginUid(); receipt["state"]["permissionLevel"] = m_deviceStateService->getLoginPermissionLevel(); receipt["state"]["workState"] = m_disinfectionCtrlService->isDisinfectionRunning(); receipt["state"]["estimatedRemainingTimeS"] = m_disinfectionCtrlService->getEstimatedRemainingTimeS(); receipt["state"]["disinfection_id"] = m_disinfectionCtrlService->getDisinfectionID(); return; }
/*******************************************************************************
* 数据库查询 * *******************************************************************************/
if (cmdstr == "getAllUser") { auto users = m_dbService->getAllUserJson(); receipt["dbval"] = users; return; }
if (cmdstr == "getAllSetting") { auto dbval = m_dbService->getAllSettingJson(); receipt["dbval"] = dbval; return; }
if (cmdstr == "getAllRecords") { string disinfection_id = cmd["disinfection_id"]; auto dbval = m_dbService->getAllRecords(disinfection_id); receipt["dbval"] = dbval; return; }
if (cmdstr == "setSettingVal") { string settingName = cmd["settingName"]; int settingVal = jsonGet<int>(cmd["settingVal"]); bool suc = m_dbService->setSettingVal(settingName, settingVal); if (!suc) { receipt["ackcode"] = err::error_code_get_get_ecode(err::kcommon_error_code, err::kdb_operate_error); receipt["ackcodeInfo"] = err::error_code_get_desc(err::kcommon_error_code, err::kdb_operate_error, "setSettingVal fail"); } return; } }
HttpResponsePtr MainControlService::hello_world( //
HttpRequestPtr request, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState>) { return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world"); }
HttpResponsePtr MainControlService::doscript(HttpRequestPtr httpreq, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState> conn) { // logger->info("do\n{}", httpreq->body);
// if (m_a8000_script_processer->isWorking()) {
// return std::make_shared<HttpResponse>(200, "FAIL", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script fail, script processer is running");
// }
// m_a8000_script_processer->executeScript(httpreq->body);
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script success"); }
HttpResponsePtr MainControlService::stopscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>) { // m_a8000_script_processer->stopScript();
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "stop script success"); }
|