Browse Source

重构部分代码

master
zhaohe 2 years ago
parent
commit
93097b5c57
  1. 93
      README.md
  2. 35
      src/db/db_service.cpp
  3. 3
      src/db/db_service.hpp
  4. 292
      src/main_control_service.cpp
  5. 6
      src/main_control_service.hpp

93
README.md

@ -3,47 +3,46 @@
```
1. 输入rootiflytop9973可以进入超级用户
2. ws测试地址: ws://192.168.1.148:19000
```
```
1. 功能点
FrontEndService
MainCtrlService
UnixCan
IflytopCmdServer
SqlService
UsrDB
SensorDataDB
DisinfectionRecordDB
DeviceConfigDB
2. 完善功能管理
1. 磁盘空间管理
2. 日志过期自动删除
3. DB过期记录自动删除
4. 量产时记得将websocketIP修改成127.0.0.1
5. 远程控制功能
必须要完成的功能:
6. 软件升级功能,frp
7. 程序重启
//补充协议
1. 时间修改
2. getState时,传感器信息的上报,所有受控设备的开关状态
3. 修改密码时,需要传递旧密码。
4. 设置的默认配置信息name错误
5. 排液,加液,液体体积
6. 开关量状态上报,压力传感器状态上报
----------------------------------
修改点:
1. 用户等级分为3级别(后台不限制用户等级,前台限制用户操作)OK
2. iflytop9973属于admin的超级密码
3. 修改用户密码 OK
4. 登陆时给出提示,用户不存在,或者密码错误
添加预设参数
添加预设参数配置
添加审计数据库接口
添加审计
添加U盘数据导出
添加审计数据导出
通过getState可以获得到log,小数
```
# 测试指令
```
修改密码
{
"command":"chpasswd",
"messageId":"123",
"userId":"admin",
"newpasswd":"1234"
}
读取用户数据
{
"command":"getAllUser",
"messageId":"123",
"userId":"admin",
"newpasswd":"1234"
}
```
```
@ -52,15 +51,10 @@ sudo apt-get install sqlite3:arm64 libsqlite3-dev:arm64
sqlite可视化
sudo apt-get install sqlitebrowser
```
```
https://www.sqlite.org/docs.html
```
```
```
```
测试指令
@ -77,30 +71,9 @@ https://www.sqlite.org/docs.html
```
液位测量压力传感器:
设备地址:01
量程:-1~4kPa
4 kPa=407.888 毫米水柱
传感器测量精度:0.407888 毫米水柱
1. 需求
加液 液位变换
排液 1.控制蠕动泵
2.液位变换
消毒 1. 上报时间,计算剩余时间.
2. 上报所有传感器信息.
3. 根据当前浓度估算D数值.
开始消毒
停止消毒
超时后设备自动杀死自己
```

35
src/db/db_service.cpp

@ -244,6 +244,19 @@ void DBService::updateUserPermissionLevel(int id, int permission_level) {
usertable.update(user[0]);
}
void DBService::changePasswd(string uid, string passwd) {
auto usertable = make_storage(USER_DB_STRUCT);
usertable.sync_schema();
auto user = usertable.get_all<User>(where(c(&User::uid) == uid));
if (user.size() == 0) {
logger->error("change passwd fail, user not found");
return;
}
logger->info("change passwd: {} {} -> {}", uid, user[0].passwd, passwd);
user[0].passwd = passwd;
usertable.update(user[0]);
}
json DBService::getAllUserJson() {
json j_users;
auto usertable = make_storage(USER_DB_STRUCT);
@ -301,6 +314,28 @@ json DBService::getAllSettingJson() {
return j_settings;
}
bool DBService::isUserExist(string uid) {
auto usertable = make_storage(USER_DB_STRUCT);
usertable.sync_schema();
auto user = usertable.get_all<User>(where(c(&User::uid) == uid));
if (user.size() == 0) {
return false;
}
return true;
}
bool DBService::ispasswdCorrect(string uid, string passwd) {
auto usertable = make_storage(USER_DB_STRUCT);
usertable.sync_schema();
auto user = usertable.get_all<User>(where(c(&User::uid) == uid));
if (user.size() == 0) {
return false;
}
if (user[0].passwd == passwd) {
return true;
}
return false;
}
bool DBService::setSettingVal(int id, int val) {
auto settingtable = make_storage(SETTING_DB, SETTING_DB_STRUCT);
settingtable.sync_schema();

3
src/db/db_service.hpp

@ -132,6 +132,8 @@ class DBService : public enable_shared_from_this<DBService> {
json getAllUserJson();
shared_ptr<db::User> getUser(string uid);
vector<string> getUserNames();
bool isUserExist(string uid);
bool ispasswdCorrect(string uid, string passwd);
/**
* @brief
@ -143,6 +145,7 @@ class DBService : public enable_shared_from_this<DBService> {
void addUser(string uid, string passwd, int permission_level);
void delUser(int id);
void updateUserPermissionLevel(int id, int permission_level);
void changePasswd(string uid, string passwd);
public:
/*******************************************************************************

292
src/main_control_service.cpp

@ -175,7 +175,7 @@ json MainControlService::createSensorDataJson() {
return report;
}
// {"command":"startReplenishingFluids","messageId":"startReplenishingFluids","stopAt":123}
void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
void MainControlService::processFrontEndMessage_userOperate(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
string cmdstr = cmd["command"];
/*******************************************************************************
* LOGIN_CMD *
@ -184,25 +184,21 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
string uid = cmd["userid"];
string pwd = cmd["passwd"];
if (pwd == "rootiflytop9973") {
m_deviceStateService->setLoginState("root", 0, 1);
logger->info("user {} login success", "root");
if (uid == "admin" && pwd == "iflytop9973") {
m_deviceStateService->setLoginState("admin", 0, 1);
logger->info("user {} login success", "admin");
} else {
auto usr = m_dbService->getUser(uid);
if (usr == nullptr) {
logger->warn("login fail, user {} not exist", uid);
if (!m_dbService->isUserExist(uid)) {
receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kuser_not_exist);
receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kuser_not_exist, "");
return;
}
if (usr->passwd != pwd) {
logger->warn("login fail, user {} passwd error", uid);
if (!m_dbService->ispasswdCorrect(uid, pwd)) {
receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kpasswd_error);
receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kpasswd_error, "");
return;
}
m_deviceStateService->setLoginState(uid, usr->permission_level, usr->visible);
m_deviceStateService->setLoginState(uid, m_dbService->getUser(uid)->permission_level, m_dbService->getUser(uid)->visible);
logger->info("user {} login success", uid);
}
@ -222,23 +218,21 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
* chpasswd *
*******************************************************************************/
if (cmdstr == "chpasswd") {
string uid = cmd["userid"];
string pwd = cmd["passwd"];
logger->info("changet passwd {} {}", uid, pwd);
string uid = cmd["userId"];
string newpasswd = cmd["newpasswd"];
string passwd = cmd["passwd"];
logger->info("changet passwd {} {}", uid, passwd);
if (!m_dbService->isUserExist(uid)) {
receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kuser_not_exist);
receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kuser_not_exist, "");
return;
}
/*******************************************************************************
* shutdown *
*******************************************************************************/
if (cmdstr == "shutdown") {
int delayms = jsonGet<int>(cmd["delayms"]);
logger->info("shutdown {} ms", delayms);
m_autoshutdownThread.reset(new Thread("autoShutdown", [delayms, this]() {
ThisThread thisThread;
thisThread.sleepForMs(delayms);
dosystem("shutdown -h now");
}));
if (!m_dbService->ispasswdCorrect(uid, passwd)) {
receipt["ackcode"] = err::error_code_get_get_ecode(err::kce, err::kpasswd_error);
receipt["ackcodeInfo"] = err::error_code_get_desc(err::kce, err::kpasswd_error, "");
return;
}
m_dbService->changePasswd(uid, newpasswd);
return;
}
@ -260,6 +254,27 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
m_dbService->updateUserPermissionLevel(id, permission_level);
return;
}
if (cmdstr == "getAllUser") {
auto users = m_dbService->getAllUserJson();
receipt["dbval"] = users;
return;
}
}
void MainControlService::processFrontEndMessage_systemOperate(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
string cmdstr = cmd["command"];
/*******************************************************************************
* shutdown *
*******************************************************************************/
if (cmdstr == "shutdown") {
int delayms = jsonGet<int>(cmd["delayms"]);
logger->info("shutdown {} ms", delayms);
m_autoshutdownThread.reset(new Thread("autoShutdown", [delayms, this]() {
ThisThread thisThread;
thisThread.sleepForMs(delayms);
dosystem("shutdown -h now");
}));
return;
}
if (cmdstr == "updateDate") {
#if 0
@ -302,72 +317,43 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
return;
}
}
void MainControlService::processFrontEndMessage_Disinfection(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
/*******************************************************************************
* *
*******************************************************************************/
string cmdstr = cmd["command"];
if (cmdstr == "startDisinfection2") {
int loglevel = jsonGet<int>(cmd["loglevel"]);
int injection_pump_speed = jsonGet<int>(cmd["injection_pump_speed"]); //
int stoped_gs = jsonGet<int>(cmd["stoped_gs"]); //
int continued_gs = jsonGet<int>(cmd["continued_gs"]); //
int stoped_satur = jsonGet<int>(cmd["stoped_satur"]); //
int continued_satur = jsonGet<int>(cmd["continued_satur"]); //
int stoped_humi = jsonGet<int>(cmd["stoped_humi"]); //
int continued_humi = jsonGet<int>(cmd["continued_humi"]); //
m_disinfectionCtrlService->startDisinfection(loglevel, //
injection_pump_speed, //
stoped_gs, //
continued_gs, //
stoped_satur, //
continued_satur, //
stoped_humi, //
continued_humi //
);
m_disinfectionCtrlService->startDisinfection(jsonGet<int>(cmd["loglevel"]),
jsonGet<int>(cmd["injection_pump_speed"]), //
jsonGet<int>(cmd["stoped_gs"]), //
jsonGet<int>(cmd["continued_gs"]),
jsonGet<int>(cmd["stoped_satur"]), //
jsonGet<int>(cmd["continued_satur"]), //
jsonGet<int>(cmd["stoped_humi"]),
jsonGet<int>(cmd["continued_humi"])); //
return;
}
if (cmdstr == "startDisinfection") {
int loglevel = jsonGet<int>(cmd["loglevel"]);
int injection_pump_speed = m_dbService->getSettingVal("injection_pump_speed");
int stoped_gs = m_dbService->getSettingVal("stoped_gs");
int continued_gs = m_dbService->getSettingVal("continued_gs");
int stoped_satur = m_dbService->getSettingVal("stoped_satur");
int continued_satur = m_dbService->getSettingVal("continued_satur");
int stoped_humi = m_dbService->getSettingVal("stoped_humi");
int continued_humi = m_dbService->getSettingVal("continued_humi");
m_disinfectionCtrlService->startDisinfection(loglevel, //
injection_pump_speed, //
stoped_gs, //
continued_gs, //
stoped_satur, //
continued_satur, //
stoped_humi, //
continued_humi //
);
m_disinfectionCtrlService->startDisinfection(jsonGet<int>(cmd["loglevel"]), //
m_dbService->getSettingVal("injection_pump_speed"), //
m_dbService->getSettingVal("stoped_gs"), //
m_dbService->getSettingVal("continued_gs"), //
m_dbService->getSettingVal("stoped_satur"), //
m_dbService->getSettingVal("continued_satur"), //
m_dbService->getSettingVal("stoped_humi"), //
m_dbService->getSettingVal("continued_humi"));
return;
}
if (cmdstr == "changeDisinfectionParameter") {
int injection_pump_speed = jsonGet<int>(cmd["injection_pump_speed"]); //
int stoped_gs = jsonGet<int>(cmd["stoped_gs"]); //
int continued_gs = jsonGet<int>(cmd["continued_gs"]); //
int stoped_satur = jsonGet<int>(cmd["stoped_satur"]); //
int continued_satur = jsonGet<int>(cmd["continued_satur"]); //
int stoped_humi = jsonGet<int>(cmd["stoped_humi"]); //
int continued_humi = jsonGet<int>(cmd["continued_humi"]); //
m_disinfectionCtrlService->changeDisinfectionParameter( //
injection_pump_speed, //
stoped_gs, //
continued_gs, //
stoped_satur, //
continued_satur, //
stoped_humi, //
continued_humi //
);
m_disinfectionCtrlService->changeDisinfectionParameter(jsonGet<int>(cmd["injection_pump_speed"]), //
jsonGet<int>(cmd["stoped_gs"]), //
jsonGet<int>(cmd["continued_gs"]), //
jsonGet<int>(cmd["stoped_satur"]), //
jsonGet<int>(cmd["continued_satur"]), //
jsonGet<int>(cmd["stoped_humi"]), //
jsonGet<int>(cmd["continued_humi"]));
return;
}
@ -376,25 +362,6 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
return;
}
#if 0
//开始加液
{
"command":"startReplenishingFluids",
}
//停止加液
{
"command":"stopReplenishingFluids",
}
//开始排液
{
"command":"startDraining",
}
//停止排液
{
"command":"stopDraining",
}
#endif
if (cmdstr == "startReplenishingFluids") {
int16_t stopAt = jsonGet<int>(cmd["stopAt"]);
logger->info("startReplenishingFluids {}", stopAt);
@ -419,55 +386,9 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
m_disinfectionCtrlService->stopDraining();
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"]["disinfectionWorkState"] = m_disinfectionCtrlService->getDisinfectionWorkState();
receipt["state"]["replenishingFluidsWorkState"] = m_disinfectionCtrlService->getReplenishingFluidsWorkState();
receipt["state"]["drainingWorkState"] = m_disinfectionCtrlService->getDrainingWorkState();
receipt["state"]["estimatedRemainingTimeS"] = m_disinfectionCtrlService->getEstimatedRemainingTimeS();
receipt["state"]["disinfection_id"] = m_disinfectionCtrlService->getDisinfectionID();
// receipt["state"]["preHeat"] = m_disinfectionCtrlService->processPreheatState
// getPreHeatRaminTimeS
// isPreheatState
receipt["state"]["preHeat"] = m_disinfectionCtrlService->isPreheatState();
receipt["state"]["preHeatRaminTimeS"] = m_disinfectionCtrlService->getPreHeatRaminTimeS(); // 预热剩余时间
receipt["state"]["sensor_data"] = createSensorDataJson();
receipt["disinfectionConfig"]["loglevel"] = m_disinfectionCtrlService->m_context.m_targetLoglevel;
receipt["disinfectionConfig"]["injection_pump_speed"] = m_disinfectionCtrlService->m_context.injection_pump_speed;
receipt["disinfectionConfig"]["stoped_gs"] = m_disinfectionCtrlService->m_context.stoped_gs;
receipt["disinfectionConfig"]["continued_gs"] = m_disinfectionCtrlService->m_context.continued_gs;
receipt["disinfectionConfig"]["stoped_satur"] = m_disinfectionCtrlService->m_context.stoped_satur;
receipt["disinfectionConfig"]["continued_satur"] = m_disinfectionCtrlService->m_context.continued_satur;
receipt["disinfectionConfig"]["stoped_humi"] = m_disinfectionCtrlService->m_context.stoped_humi;
receipt["disinfectionConfig"]["continued_humi"] = m_disinfectionCtrlService->m_context.continued_humi;
return;
}
// // 排液泵
// void drainingPump_open();
// void drainingPump_close();
// // 充液泵
// void replenishingFluidsPump_open();
// void replenishingFluidsPump_close();
// // 喷液泵
// void sprayLiquidPump_open(int gpm);
// void sprayLiquidPump_close();
// int sprayLiquidPump_getState();
// int sprayLiquidPump_getRPM();
// int sprayLiquidPump_getGPM();
#if 1
}
void MainControlService::processFrontEndMessage_test(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
string cmdstr = cmd["command"];
if (cmdstr == "test_sprayLiquidPumpCtrl") {
int ctrl = jsonGet<int>(cmd["ctrl"]);
int speed = jsonGet<int>(cmd["speed"]);
@ -493,8 +414,6 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
}
return;
}
#endif
/*******************************************************************************
* *
*******************************************************************************/
@ -513,30 +432,16 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
}
return;
}
}
/*******************************************************************************
* *
*******************************************************************************/
if (cmdstr == "getAllUser") {
auto users = m_dbService->getAllUserJson();
receipt["dbval"] = users;
return;
}
void MainControlService::processFrontEndMessage_setting(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
string cmdstr = cmd["command"];
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"]);
@ -549,21 +454,56 @@ void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, j
}
}
void MainControlService::processFrontEndMessage(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt) {
string cmdstr = cmd["command"];
processFrontEndMessage_userOperate(webSocket, cmd, receipt);
processFrontEndMessage_systemOperate(webSocket, cmd, receipt);
processFrontEndMessage_Disinfection(webSocket, cmd, receipt);
processFrontEndMessage_test(webSocket, cmd, receipt);
processFrontEndMessage_setting(webSocket, cmd, receipt);
/*******************************************************************************
* 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"]["disinfectionWorkState"] = m_disinfectionCtrlService->getDisinfectionWorkState();
receipt["state"]["replenishingFluidsWorkState"] = m_disinfectionCtrlService->getReplenishingFluidsWorkState();
receipt["state"]["drainingWorkState"] = m_disinfectionCtrlService->getDrainingWorkState();
receipt["state"]["estimatedRemainingTimeS"] = m_disinfectionCtrlService->getEstimatedRemainingTimeS();
receipt["state"]["disinfection_id"] = m_disinfectionCtrlService->getDisinfectionID();
// receipt["state"]["preHeat"] = m_disinfectionCtrlService->processPreheatState
// getPreHeatRaminTimeS
// isPreheatState
receipt["state"]["preHeat"] = m_disinfectionCtrlService->isPreheatState();
receipt["state"]["preHeatRaminTimeS"] = m_disinfectionCtrlService->getPreHeatRaminTimeS(); // 预热剩余时间
receipt["state"]["sensor_data"] = createSensorDataJson();
receipt["disinfectionConfig"]["loglevel"] = m_disinfectionCtrlService->m_context.m_targetLoglevel;
receipt["disinfectionConfig"]["injection_pump_speed"] = m_disinfectionCtrlService->m_context.injection_pump_speed;
receipt["disinfectionConfig"]["stoped_gs"] = m_disinfectionCtrlService->m_context.stoped_gs;
receipt["disinfectionConfig"]["continued_gs"] = m_disinfectionCtrlService->m_context.continued_gs;
receipt["disinfectionConfig"]["stoped_satur"] = m_disinfectionCtrlService->m_context.stoped_satur;
receipt["disinfectionConfig"]["continued_satur"] = m_disinfectionCtrlService->m_context.continued_satur;
receipt["disinfectionConfig"]["stoped_humi"] = m_disinfectionCtrlService->m_context.stoped_humi;
receipt["disinfectionConfig"]["continued_humi"] = m_disinfectionCtrlService->m_context.continued_humi;
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");
}

6
src/main_control_service.hpp

@ -87,7 +87,13 @@ class MainControlService : public enable_shared_from_this<MainControlService> {
HttpResponsePtr stopscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>);
void createReactionConfigCardInfoReportAndSend();
void processFrontEndMessage(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt);
void processFrontEndMessage_userOperate(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt);
void processFrontEndMessage_systemOperate(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt);
void processFrontEndMessage_Disinfection(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt);
void processFrontEndMessage_test(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt);
void processFrontEndMessage_setting(weak_ptr<WebSocket> webSocket, json& cmd, json& receipt);
json createSensorDataJson();

Loading…
Cancel
Save