21 changed files with 58 additions and 3055 deletions
-
3.gitmodules
-
6.vscode/c_cpp_properties.json
-
4CMakeLists.txt
-
9README.md
-
29src/configs/gconfig.hpp
-
663src/db/db_service.cpp
-
181src/db/db_service.hpp
-
54src/db/user_behavior_des.cpp
-
41src/db/user_behavior_des.hpp
-
737src/main_control_service.cpp
-
35src/main_control_service.hpp
-
133src/service/data_export_service.cpp
-
60src/service/data_export_service.hpp
-
0src/service/device_state_service.cpp
-
63src/service/device_state_service.hpp
-
679src/service/disinfection_ctl_service.cpp
-
186src/service/disinfection_ctl_service.hpp
-
99src/service/disinfection_logs_manager.cpp
-
48src/service/disinfection_logs_manager.hpp
-
44src/service/iflytop_can_host_device.cpp
-
39src/service/iflytop_can_host_device.hpp
@ -1,29 +0,0 @@ |
|||
|
|||
#pragma once
|
|||
#include <exception>
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "iflytop/core/components/config_template/config_template.hpp"
|
|||
|
|||
#define ConfigELEMENT_LIST(marco) \
|
|||
marco(string /* */, deviceId, "") /*设备ID*/ \ |
|||
marco(string /* */, iflytopSubDeviceCanIFName, "can0") /*子设备Can设备名称*/ \ |
|||
marco(int32_t /* */, iflytopSubDeviceCanBitrate, 500000) /*子设备Can设备波特率*/ \ |
|||
marco(string /* */, pipettingRobotCanIFName, "can1") /*移液臂Can设备名称*/ \ |
|||
marco(int32_t /* */, pipettingRobotCanBitrate, 500000) /*移液臂Can设备波特率*/ \ |
|||
marco(float /* */, dvalueCoefficient, 1) /*数值越小,相对消毒时间越长*/ \ |
|||
marco(float /* */, disinfectantWeightCorrectionFactor, 0.9) /*数值越小,相对消毒时间越长*/ |
|||
|
|||
configTemplateDEFILE_CONFIG_SERVICE2( //
|
|||
GConfig, //
|
|||
ConfigELEMENT_LIST, //
|
|||
"./configs/config.json", {}); |
@ -1,663 +0,0 @@ |
|||
#include <sqlite3.h>
|
|||
|
|||
//
|
|||
#include <stdio.h>
|
|||
#include <time.h>
|
|||
|
|||
#include "db_service.hpp"
|
|||
//
|
|||
#include "iflytop/components/sqlite_orm/sqlite_orm.hpp"
|
|||
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
using namespace iflytop::db; |
|||
using namespace sqlite_orm; |
|||
using namespace nlohmann; |
|||
|
|||
// 数据库工具使用手册
|
|||
// auto notJohn = storage.get_all<User>(where(c(&User::firstName) != "John"));
|
|||
// 主键必须是int,或者不要主键
|
|||
|
|||
#define USER_DB_STRUCT \
|
|||
USER_DB, /**/ \ |
|||
make_table("users", /**/ \ |
|||
make_column("id", &User::id, primary_key().autoincrement()), /**/ \ |
|||
make_column("uid", &User::uid), /**/ \ |
|||
make_column("passwd", &User::passwd), /**/ \ |
|||
make_column("permission_level", &User::permission_level), /**/ \ |
|||
make_column("visible", &User::visible)) |
|||
|
|||
#define SETTING_DB_STRUCT \
|
|||
make_table("settings", /**/ \ |
|||
make_column("id", &Setting::id, primary_key()), /**/ \ |
|||
make_column("name", &Setting::name), /**/ \ |
|||
make_column("name_ch", &Setting::name_ch), /**/ \ |
|||
make_column("val_lower_limit", &Setting::val_lower_limit), /**/ \ |
|||
make_column("val_upper_limit", &Setting::val_upper_limit), /**/ \ |
|||
make_column("permission_level", &Setting::permission_level), /**/ \ |
|||
make_column("val", &Setting::val)) |
|||
|
|||
#define FORMULA_DB_STRUCT \
|
|||
make_table("formulas", /**/ \ |
|||
make_column("id", &Formula::id, primary_key()), /**/ \ |
|||
make_column("loglevel", &Formula::loglevel), /**/ \ |
|||
make_column("formula_id", &Formula::formula_id), /**/ \ |
|||
make_column("stoped_gs", &Formula::stoped_gs), /**/ \ |
|||
make_column("continued_gs", &Formula::continued_gs), /**/ \ |
|||
make_column("stoped_satur", &Formula::stoped_satur), /**/ \ |
|||
make_column("continued_satur", &Formula::continued_satur), /**/ \ |
|||
make_column("stoped_humi", &Formula::stoped_humi), /**/ \ |
|||
make_column("continued_humi", &Formula::continued_humi), /**/ \ |
|||
make_column("injection_pump_speed", &Formula::injection_pump_speed)) |
|||
|
|||
#define USER_BEHAVIOR_RECORD_STRUCT \
|
|||
make_table("user_behavior_records", /**/ \ |
|||
make_column("id", &UserBehaviorRecord::id, primary_key().autoincrement()), /**/ \ |
|||
make_column("uid", &UserBehaviorRecord::uid), /**/ \ |
|||
make_column("date", &UserBehaviorRecord::date), /**/ \ |
|||
make_column("behavior", &UserBehaviorRecord::behavior), /**/ \ |
|||
make_column("behaviorinfo", &UserBehaviorRecord::behaviorinfo)) |
|||
|
|||
DBService::DBService(/* args */) {} |
|||
|
|||
void DBService::initialize() { |
|||
/**
|
|||
* @brief 初始化用户表 |
|||
*/ |
|||
|
|||
//
|
|||
init_usr_db(); |
|||
init_setting_db(); |
|||
|
|||
json settings = getAllSettingJson(); |
|||
logger->info("settings: {}", settings.dump()); |
|||
} |
|||
|
|||
void DBService::init_usr_db() { |
|||
bool suc = false; |
|||
do { |
|||
try { |
|||
logger->info("init user db"); |
|||
auto storage = make_storage(USER_DB_STRUCT); |
|||
storage.sync_schema(); |
|||
auto admin = storage.get_all<User>(where(c(&User::uid) == "admin")); |
|||
if (admin.size() == 0) { |
|||
storage.insert<User>({-1, "admin", "9973", 1, true}); // 管理员
|
|||
} |
|||
auto user = storage.get_all<User>(where(c(&User::uid) == "user")); |
|||
if (user.size() == 0) { |
|||
storage.insert<User>({-1, "user", "0000", 3, true}); // 普通用户
|
|||
} |
|||
suc = true; |
|||
} catch (const std::exception& e) { |
|||
logger->error("init user db failed: {}", e.what()); |
|||
system("rm -rf user.db"); |
|||
sleep(1); |
|||
} |
|||
} while (!suc); |
|||
} |
|||
void DBService::init_setting_db() { |
|||
bool suc = false; |
|||
#if 0
|
|||
id setting_name setting_name_ch val_upper_limit val_lower_limit permission_level val |
|||
0 stoped_gs 消毒停止过氧化氢溶度 0 2000 1 1000 |
|||
1 continued_gs 消毒继续过氧化氢溶度 0 2000 1 800 |
|||
2 stoped_satur 消毒停止过氧化氢相对饱和度 0 100 1 80 |
|||
3 continued_satur 消毒继续过氧化氢相对饱和度 0 100 1 60 |
|||
4 max_humidity 允许消毒最大湿度 0 100 1 90 |
|||
5 drainage_pump_speed 排液蠕动泵转速 0 2000 2 500 |
|||
6 injection_pump_speed 喷射蠕动泵转速 0 2000 2 500 |
|||
#endif
|
|||
|
|||
do { |
|||
try { |
|||
logger->info("init setting db"); |
|||
auto storage = make_storage(SETTING_DB, SETTING_DB_STRUCT); |
|||
storage.sync_schema(); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 1)).size() == 0) //
|
|||
storage.insert(Setting{1, "stoped_gs", "消毒停止过氧化氢溶度", 0, 2000, 0, 1800}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 2)).size() == 0) |
|||
storage.insert(Setting{2, "continued_gs", "消毒继续过氧化氢溶度", 0, 2000, 0, 1500}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 3)).size() == 0) |
|||
storage.insert(Setting{3, "stoped_satur", "消毒停止过氧化氢相对饱和度", 0, 100, 0, 85}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 4)).size() == 0) |
|||
storage.insert(Setting{4, "continued_satur", "消毒继续过氧化氢相对饱和度", 0, 100, 0, 70}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 5)).size() == 0) //
|
|||
storage.insert(Setting{5, "max_humidity", "允许消毒最大湿度", 0, 100, 0, 90}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 6)).size() == 0) |
|||
storage.insert(Setting{6, "drainage_pump_speed", "排液蠕动泵转速", 0, 90, 0, 90}); // g/min
|
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 7)).size() == 0) |
|||
storage.insert(Setting{7, "injection_pump_speed", "喷射蠕动泵转速", 0, 50, 0, 50}); // g/min
|
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 8)).size() == 0) //
|
|||
storage.insert(Setting{8, "pre_heat_time_s", "预热时间", 0, 600, 0, 120}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 9)).size() == 0) //
|
|||
storage.insert(Setting{9, "stoped_humi", "消毒停止相对湿度", 0, 100, 0, 85}); |
|||
|
|||
if (storage.get_all<Setting>(where(c(&Setting::id) == 10)).size() == 0) //
|
|||
storage.insert(Setting{10, "continued_humi", "消毒继续相对湿度", 0, 100, 0, 70}); |
|||
|
|||
suc = true; |
|||
} catch (const std::exception& e) { |
|||
logger->error("init setting db failed: {}", e.what()); |
|||
system("rm -rf setting.db"); |
|||
sleep(1); |
|||
} |
|||
} while (!suc); |
|||
} |
|||
|
|||
void DBService::init_formula_db() { |
|||
bool suc = false; |
|||
|
|||
do { |
|||
try { |
|||
logger->info("init formula db"); |
|||
auto storage = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
storage.sync_schema(); |
|||
|
|||
suc = true; |
|||
} catch (const std::exception& e) { |
|||
logger->error("init setting db failed: {}", e.what()); |
|||
system("rm -rf setting.db"); |
|||
sleep(1); |
|||
} |
|||
} while (!suc); |
|||
} |
|||
|
|||
list<shared_ptr<User>> DBService::getAllUser() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
list<shared_ptr<User>> users; |
|||
auto usertable = make_storage(USER_DB_STRUCT); |
|||
usertable.sync_schema(); |
|||
auto all = usertable.get_all<User>(); |
|||
for (auto& u : all) { |
|||
users.push_back(make_shared<User>(u)); |
|||
} |
|||
return users; |
|||
} |
|||
void DBService::addUser(string uid, string passwd, int permission_level) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto usertable = make_storage(USER_DB_STRUCT); |
|||
usertable.sync_schema(); |
|||
logger->info("add user: {} {} {}", uid, passwd, permission_level); |
|||
usertable.insert(User{-1, uid, passwd, permission_level, true}); |
|||
} |
|||
shared_ptr<db::User> DBService::delUser(int id) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto usertable = make_storage(USER_DB_STRUCT); |
|||
usertable.sync_schema(); |
|||
/**
|
|||
* @brief find admin user |
|||
*/ |
|||
auto admin = usertable.get_all<User>(where(c(&User::uid) == "admin")); |
|||
ZCHECK(admin.size() == 1, "admin user not found"); |
|||
if (admin[0].id == id) { |
|||
logger->error("can not delete admin user"); |
|||
return nullptr; |
|||
} |
|||
auto remove_user = usertable.get_all<User>(where(c(&User::id) == id)); |
|||
if (remove_user.size() == 0) { |
|||
logger->error("remove user fail, user not found"); |
|||
return nullptr; |
|||
} |
|||
logger->info("delete user: {}:{}", id, remove_user[0].uid); |
|||
usertable.remove_all<User>(where(c(&User::id) == id)); |
|||
return make_shared<User>(remove_user[0]); |
|||
} |
|||
|
|||
shared_ptr<db::User> DBService::updateUserPermissionLevel(int id, int permission_level) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto usertable = make_storage(USER_DB_STRUCT); |
|||
usertable.sync_schema(); |
|||
|
|||
auto user = usertable.get_all<User>(where(c(&User::id) == id)); |
|||
if (user.size() == 0) { |
|||
logger->error("update user permission level fail, user not found"); |
|||
return nullptr; |
|||
} |
|||
logger->info("update user permission level: {} {} -> {}", id, user[0].permission_level, permission_level); |
|||
user[0].permission_level = permission_level; |
|||
usertable.update(user[0]); |
|||
return make_shared<User>(user[0]); |
|||
} |
|||
|
|||
shared_ptr<db::User> DBService::changePasswd(string uid, string passwd) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
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 nullptr; |
|||
} |
|||
logger->info("change passwd: {} {} -> {}", uid, user[0].passwd, passwd); |
|||
user[0].passwd = passwd; |
|||
usertable.update(user[0]); |
|||
return make_shared<User>(user[0]); |
|||
} |
|||
shared_ptr<db::User> DBService::updateUserUid(int id, string uid, string& olduid) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto usertable = make_storage(USER_DB_STRUCT); |
|||
usertable.sync_schema(); |
|||
auto user = usertable.get_all<User>(where(c(&User::id) == id)); |
|||
if (user.size() == 0) { |
|||
logger->error("change user uid fail, user not found"); |
|||
return nullptr; |
|||
} |
|||
olduid = user[0].uid; |
|||
logger->info("change user uid: {} {} -> {}", id, user[0].uid, uid); |
|||
user[0].uid = uid; |
|||
usertable.update(user[0]); |
|||
return make_shared<User>(user[0]); |
|||
} |
|||
|
|||
json DBService::getAllUserJson() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
json j_users; |
|||
auto usertable = make_storage(USER_DB_STRUCT); |
|||
usertable.sync_schema(); |
|||
auto all = usertable.get_all<User>(); |
|||
for (auto& u : all) { |
|||
json j_user; |
|||
j_user["id"] = u.id; |
|||
j_user["uid"] = u.uid; |
|||
j_user["passwd"] = u.passwd; |
|||
j_user["permission_level"] = u.permission_level; |
|||
j_user["visible"] = u.visible; |
|||
j_users.push_back(j_user); |
|||
} |
|||
return j_users; |
|||
} |
|||
|
|||
shared_ptr<User> DBService::getUser(string uid) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
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 nullptr; |
|||
} |
|||
return make_shared<User>(user[0]); |
|||
} |
|||
|
|||
list<shared_ptr<db::Setting>> DBService::getAllSetting() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
list<shared_ptr<db::Setting>> settings; |
|||
auto settingtable = make_storage(SETTING_DB, SETTING_DB_STRUCT); |
|||
settingtable.sync_schema(); |
|||
auto all = settingtable.get_all<Setting>(); |
|||
for (auto& s : all) { |
|||
settings.push_back(make_shared<Setting>(s)); |
|||
} |
|||
return settings; |
|||
} |
|||
json DBService::getAllSettingJson() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
json j_settings; |
|||
auto settingtable = make_storage(SETTING_DB, SETTING_DB_STRUCT); |
|||
settingtable.sync_schema(); |
|||
auto all = settingtable.get_all<Setting>(); |
|||
for (auto& s : all) { |
|||
json j_setting; |
|||
j_setting["id"] = s.id; |
|||
j_setting["name"] = s.name; |
|||
j_setting["name_ch"] = s.name_ch; |
|||
j_setting["val"] = s.val; |
|||
j_setting["val_lower_limit"] = s.val_lower_limit; |
|||
j_setting["val_upper_limit"] = s.val_upper_limit; |
|||
j_setting["permission_level"] = s.permission_level; |
|||
j_setting["val"] = s.val; |
|||
j_settings.push_back(j_setting); |
|||
} |
|||
return j_settings; |
|||
} |
|||
|
|||
bool DBService::isUserExist(string uid) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
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) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
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) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto settingtable = make_storage(SETTING_DB, SETTING_DB_STRUCT); |
|||
settingtable.sync_schema(); |
|||
auto setting = settingtable.get_all<Setting>(where(c(&Setting::id) == id)); |
|||
if (setting.size() == 0) { |
|||
return false; |
|||
} |
|||
if (setting[0].val < setting[0].val_lower_limit || setting[0].val > setting[0].val_upper_limit) { |
|||
return false; |
|||
} |
|||
setting[0].val = val; |
|||
settingtable.update(setting[0]); |
|||
return true; |
|||
} |
|||
bool DBService::setSettingVal(string setting_name, int val) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
logger->info("set setting val: {} {}", setting_name, val); |
|||
auto settingtable = make_storage(SETTING_DB, SETTING_DB_STRUCT); |
|||
settingtable.sync_schema(); |
|||
auto setting = settingtable.get_all<Setting>(where(c(&Setting::name) == setting_name)); |
|||
if (setting.size() == 0) { |
|||
logger->error("set setting val failed: {} not found", setting_name); |
|||
return false; |
|||
} |
|||
if (setting[0].val < setting[0].val_lower_limit || setting[0].val > setting[0].val_upper_limit) { |
|||
logger->error("set setting val failed: {} out of range", setting_name); |
|||
return false; |
|||
} |
|||
setting[0].val = val; |
|||
Setting s = setting[0]; |
|||
settingtable.update(s); |
|||
settingtable.sync_schema(); |
|||
return true; |
|||
} |
|||
|
|||
int DBService::getSettingVal(string name) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto settingtable = make_storage(SETTING_DB, SETTING_DB_STRUCT); |
|||
settingtable.sync_schema(); |
|||
auto setting = settingtable.get_all<Setting>(where(c(&Setting::name) == name)); |
|||
if (setting.size() == 0) { |
|||
return -1; |
|||
} |
|||
return setting[0].val; |
|||
} |
|||
|
|||
list<shared_ptr<db::Formula>> DBService::getAllFormula() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
list<shared_ptr<db::Formula>> formulas; |
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
auto all = formulatable.get_all<Formula>(); |
|||
for (auto& f : all) { |
|||
formulas.push_back(make_shared<Formula>(f)); |
|||
} |
|||
return formulas; |
|||
} |
|||
shared_ptr<db::Formula> DBService::getFormula(int id) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
auto formula = formulatable.get_all<Formula>(where(c(&Formula::id) == id)); |
|||
if (formula.size() == 0) { |
|||
return nullptr; |
|||
} |
|||
return make_shared<Formula>(formula[0]); |
|||
} |
|||
|
|||
json DBService::getAllFormulaJson() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
json j_formulas; |
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
auto all = formulatable.get_all<Formula>(); |
|||
for (auto& f : all) { |
|||
json j_formula; |
|||
j_formula["id"] = f.id; |
|||
j_formula["loglevel"] = f.loglevel; |
|||
j_formula["formula_id"] = f.formula_id; |
|||
j_formula["stoped_gs"] = f.stoped_gs; |
|||
j_formula["continued_gs"] = f.continued_gs; |
|||
j_formula["stoped_satur"] = f.stoped_satur; |
|||
j_formula["continued_satur"] = f.continued_satur; |
|||
j_formula["stoped_humi"] = f.stoped_humi; |
|||
j_formula["continued_humi"] = f.continued_humi; |
|||
j_formula["injection_pump_speed"] = f.injection_pump_speed; |
|||
j_formulas.push_back(j_formula); |
|||
} |
|||
|
|||
json jret; |
|||
jret["formulas"] = j_formulas; |
|||
jret["settings"] = getAllSettingJson(); |
|||
|
|||
return jret; |
|||
} |
|||
void DBService::addFormula(string formula_id, string loglevel, string stoped_gs, string continued_gs, string stoped_satur, string continued_satur, |
|||
string stoped_humi, string continued_humi, string injection_pump_speed) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
Formula f; |
|||
f.loglevel = loglevel; |
|||
f.formula_id = formula_id; |
|||
f.stoped_gs = stoped_gs; |
|||
f.continued_gs = continued_gs; |
|||
f.stoped_satur = stoped_satur; |
|||
f.continued_satur = continued_satur; |
|||
f.stoped_humi = stoped_humi; |
|||
f.continued_humi = continued_humi; |
|||
f.injection_pump_speed = injection_pump_speed; |
|||
formulatable.insert(f); |
|||
|
|||
formulatable.sync_schema(); |
|||
} |
|||
|
|||
void DBService::addFormula(string formula_id, int loglevel, int stoped_gs, int continued_gs, int stoped_satur, int continued_satur, int stoped_humi, |
|||
int continued_humi, int injection_pump_speed) { |
|||
addFormula(formula_id, to_string(loglevel), to_string(stoped_gs), to_string(continued_gs), to_string(stoped_satur), to_string(continued_satur), |
|||
to_string(stoped_humi), to_string(continued_humi), to_string(injection_pump_speed)); |
|||
} |
|||
shared_ptr<db::Formula> DBService::delFormula(int id) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
// remove_all
|
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
auto formula = formulatable.get_all<Formula>(where(c(&Formula::id) == id)); |
|||
formulatable.remove_all<Formula>(where(c(&Formula::id) == id)); |
|||
formulatable.sync_schema(); |
|||
|
|||
if (formula.size() == 0) { |
|||
return nullptr; |
|||
} |
|||
|
|||
return make_shared<Formula>(formula[0]); |
|||
} |
|||
|
|||
shared_ptr<db::Formula> DBService::updateFormula(shared_ptr<db::Formula> var_formula) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (var_formula == nullptr) { |
|||
return nullptr; |
|||
} |
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
auto formula = formulatable.get_all<Formula>(where(c(&Formula::id) == var_formula->id)); |
|||
if (formula.size() == 0) { |
|||
return nullptr; |
|||
} |
|||
formulatable.update(*var_formula); |
|||
formulatable.sync_schema(); |
|||
return var_formula; |
|||
} |
|||
|
|||
shared_ptr<db::Formula> DBService::updateFormula(int id, string column, string val) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto formulatable = make_storage(FORMULA_DB, FORMULA_DB_STRUCT); |
|||
formulatable.sync_schema(); |
|||
auto formula = formulatable.get_all<Formula>(where(c(&Formula::id) == id)); |
|||
if (formula.size() == 0) { |
|||
return nullptr; |
|||
} |
|||
if (column == "formula_id") { |
|||
formula[0].formula_id = val; |
|||
} else if (column == "loglevel") { |
|||
formula[0].loglevel = val; |
|||
} else if (column == "stoped_gs") { |
|||
formula[0].stoped_gs = val; |
|||
} else if (column == "continued_gs") { |
|||
formula[0].continued_gs = val; |
|||
} else if (column == "stoped_satur") { |
|||
formula[0].stoped_satur = val; |
|||
} else if (column == "continued_satur") { |
|||
formula[0].continued_satur = val; |
|||
} else if (column == "stoped_humi") { |
|||
formula[0].stoped_humi = val; |
|||
} else if (column == "continued_humi") { |
|||
formula[0].continued_humi = val; |
|||
} else if (column == "injection_pump_speed") { |
|||
formula[0].injection_pump_speed = val; |
|||
} |
|||
formulatable.update(formula[0]); |
|||
formulatable.sync_schema(); |
|||
return make_shared<Formula>(formula[0]); |
|||
} |
|||
|
|||
int DBService::getUserBehaviorRecordCount() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto user_behavior_record_table = make_storage(USER_BEHAVIOR_RECORD_DB, USER_BEHAVIOR_RECORD_STRUCT); |
|||
user_behavior_record_table.sync_schema(); |
|||
return user_behavior_record_table.count<UserBehaviorRecord>(where(c(&UserBehaviorRecord::id) > 0)); |
|||
} |
|||
int DBService::getUserBehaviorRecordTheFirstId() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto user_behavior_record_table = make_storage(USER_BEHAVIOR_RECORD_DB, USER_BEHAVIOR_RECORD_STRUCT); |
|||
user_behavior_record_table.sync_schema(); |
|||
auto all = user_behavior_record_table.get_all<UserBehaviorRecord>(order_by(&UserBehaviorRecord::id).asc()); |
|||
if (all.size() == 0) { |
|||
return -1; |
|||
} |
|||
return all[0].id; |
|||
} |
|||
|
|||
list<shared_ptr<db::UserBehaviorRecord>> DBService::getAllUserBehaviorRecord() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto user_behavior_record_table = make_storage(USER_BEHAVIOR_RECORD_DB, USER_BEHAVIOR_RECORD_STRUCT); |
|||
user_behavior_record_table.sync_schema(); |
|||
|
|||
auto all = user_behavior_record_table.get_all<UserBehaviorRecord>(order_by(&UserBehaviorRecord::id).desc()); |
|||
|
|||
list<shared_ptr<db::UserBehaviorRecord>> user_behavior_records; |
|||
for (auto& u : all) { |
|||
user_behavior_records.push_back(make_shared<UserBehaviorRecord>(u)); |
|||
} |
|||
return user_behavior_records; |
|||
} |
|||
|
|||
json DBService::getUserBehaviorRecordDescJson(int page, int page_size) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
json j_user_behavior_records; |
|||
|
|||
auto user_behavior_record_table = make_storage(USER_BEHAVIOR_RECORD_DB, USER_BEHAVIOR_RECORD_STRUCT); |
|||
user_behavior_record_table.sync_schema(); |
|||
|
|||
auto all = user_behavior_record_table.get_all<UserBehaviorRecord>(order_by(&UserBehaviorRecord::id).desc()); |
|||
|
|||
int i = 0; |
|||
|
|||
int from = page * page_size; |
|||
int to = (page + 1) * page_size; |
|||
|
|||
for (auto& u : all) { |
|||
if (i >= from && i < to) { |
|||
json j_user_behavior_record; |
|||
j_user_behavior_record["id"] = u.id; |
|||
j_user_behavior_record["uid"] = u.uid; |
|||
j_user_behavior_record["behavior"] = u.behavior; |
|||
j_user_behavior_record["behaviorZH"] = user_behavior_to_str((user_behavior_t)u.behavior); |
|||
j_user_behavior_record["behaviorinfo"] = u.behaviorinfo; |
|||
j_user_behavior_record["date"] = u.date; |
|||
j_user_behavior_records["iterms"].push_back(j_user_behavior_record); |
|||
} else if (i >= to) { |
|||
break; |
|||
} |
|||
i++; |
|||
} |
|||
|
|||
j_user_behavior_records["total"] = all.size(); |
|||
j_user_behavior_records["page"] = page; |
|||
j_user_behavior_records["totalpage"] = all.size() / page_size + (all.size() % page_size == 0 ? 0 : 1); |
|||
|
|||
return j_user_behavior_records; |
|||
} |
|||
|
|||
static string getTime() { |
|||
struct tm tm = {0}; |
|||
|
|||
time_t t = ::time(nullptr); |
|||
struct tm* tmp = localtime_r(&t, &tm); |
|||
return fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}", tm.tm_year + 1900, //
|
|||
tm.tm_mon + 1, //
|
|||
tm.tm_mday, //
|
|||
tm.tm_hour, //
|
|||
tm.tm_min, tm.tm_sec); |
|||
} |
|||
|
|||
void DBService::addUserBehaviorRecord(string uid, int behavior, string behaviorinfo) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
auto user_behavior_record_table = make_storage(USER_BEHAVIOR_RECORD_DB, USER_BEHAVIOR_RECORD_STRUCT); |
|||
user_behavior_record_table.sync_schema(); |
|||
UserBehaviorRecord u; |
|||
u.uid = uid; |
|||
u.behavior = behavior; |
|||
u.behaviorinfo = behaviorinfo; |
|||
u.date = getTime(); |
|||
user_behavior_record_table.insert(u); |
|||
user_behavior_record_table.sync_schema(); |
|||
|
|||
auto all = user_behavior_record_table.get_all<UserBehaviorRecord>(order_by(&UserBehaviorRecord::id).asc()); |
|||
/**
|
|||
* @brief 如果记录总数量大于5000条,删除第一条 |
|||
*/ |
|||
if (all.size() > USER_BEHAVIOR_RECORD_DB_MAX_RECORDS) { |
|||
user_behavior_record_table.remove_all<UserBehaviorRecord>(where(c(&UserBehaviorRecord::id) == all[0].id)); |
|||
user_behavior_record_table.sync_schema(); |
|||
} |
|||
return; |
|||
} |
|||
void DBService::cleanUserBehaviorRecord() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
system(fmt::format("rm -rf {}", USER_BEHAVIOR_RECORD_DB).c_str()); |
|||
} |
@ -1,181 +0,0 @@ |
|||
#pragma once
|
|||
|
|||
#include <sqlite3.h>
|
|||
#include <stdio.h>
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "iflytop/core/spdlogfactory/logger.hpp"
|
|||
#include "iflytop/core/thread/thread.hpp"
|
|||
#include "user_behavior_des.hpp"
|
|||
|
|||
#define USER_DB "user.db"
|
|||
#define SETTING_DB "setting.db"
|
|||
#define DISINFECTION_RECORD_DB "disinfection_record.db"
|
|||
#define FORMULA_DB "formula.db"
|
|||
#define USER_BEHAVIOR_RECORD_DB "user_behavior_record.db"
|
|||
|
|||
#define USER_BEHAVIOR_RECORD_DB_MAX_RECORDS 30000
|
|||
/**
|
|||
* @brief |
|||
* |
|||
* USER_DB |
|||
* table: user |
|||
* id uid passwd permission_level |
|||
* |
|||
* |
|||
* SETTING_DB |
|||
* table: setting |
|||
* id setting_name setting_name_ch val_upper_limit val_lower_limit permission_level val |
|||
* |
|||
* DISINFECTION_RECORD_DB |
|||
* table: disinfection_record |
|||
* id uuid uid date loglevel duration |
|||
* |
|||
* |
|||
* table: sensor_record |
|||
* id disinfection_id date heating_strip air_compressor sprinkler_pump disinfectant_volume h2o2_1 temp_1 humid_1 saturation_1 h2o2_2 temp_2 humid_2 |
|||
* saturation_2 h2o2_3 temp_3 humid_3 saturation_3 |
|||
* |
|||
*/ |
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace std; |
|||
using namespace core; |
|||
using namespace nlohmann; |
|||
namespace db { |
|||
|
|||
struct User { |
|||
public: |
|||
int id; |
|||
string uid; |
|||
string passwd; |
|||
int permission_level; |
|||
int visible; |
|||
}; |
|||
|
|||
struct Setting { |
|||
public: |
|||
int id; |
|||
string name; |
|||
string name_ch; |
|||
int val_lower_limit; |
|||
int val_upper_limit; |
|||
int permission_level; |
|||
int val; |
|||
}; |
|||
|
|||
struct Formula { |
|||
public: |
|||
int id; |
|||
string loglevel; |
|||
string formula_id; |
|||
string stoped_gs; |
|||
string continued_gs; |
|||
string stoped_satur; |
|||
string continued_satur; |
|||
string stoped_humi; |
|||
string continued_humi; |
|||
string injection_pump_speed; |
|||
}; |
|||
|
|||
struct UserBehaviorRecord { |
|||
int id; |
|||
string uid; |
|||
string date; |
|||
int behavior; |
|||
string behaviorinfo; |
|||
}; |
|||
|
|||
} // namespace db
|
|||
using namespace db; |
|||
class DBService : public enable_shared_from_this<DBService> { |
|||
ENABLE_LOGGER(DBService); |
|||
recursive_mutex lock_; |
|||
|
|||
public: |
|||
DBService(); |
|||
void initialize(); |
|||
|
|||
public: |
|||
/*******************************************************************************
|
|||
* USER_DB * |
|||
*******************************************************************************/ |
|||
list<shared_ptr<db::User>> getAllUser(); |
|||
json getAllUserJson(); |
|||
shared_ptr<db::User> getUser(string uid); |
|||
vector<string> getUserNames(); |
|||
bool isUserExist(string uid); |
|||
bool ispasswdCorrect(string uid, string passwd); |
|||
|
|||
/**
|
|||
* @brief 添加用户 |
|||
* |
|||
* @param uid 用户名 |
|||
* @param passwd 密码 |
|||
* @param permission_level 许可等级,0超级管理员 3普通用户 |
|||
*/ |
|||
void addUser(string uid, string passwd, int permission_level); |
|||
shared_ptr<db::User> delUser(int id); |
|||
shared_ptr<db::User> updateUserPermissionLevel(int id, int permission_level); |
|||
shared_ptr<db::User> changePasswd(string uid, string passwd); |
|||
shared_ptr<db::User> updateUserUid(int id, string uid, string& olduid); |
|||
|
|||
public: |
|||
/*******************************************************************************
|
|||
* SETTING_DB * |
|||
*******************************************************************************/ |
|||
list<shared_ptr<db::Setting>> getAllSetting(); |
|||
json getAllSettingJson(); |
|||
bool setSettingVal(int id, int val); |
|||
bool setSettingVal(string name, int val); |
|||
int getSettingVal(string name); |
|||
|
|||
public: |
|||
/*******************************************************************************
|
|||
* Formula * |
|||
*******************************************************************************/ |
|||
|
|||
list<shared_ptr<db::Formula>> getAllFormula(); |
|||
json getAllFormulaJson(); |
|||
void addFormula(string formula_id, string loglevel, string stoped_gs, string continued_gs, string stoped_satur, string continued_satur, string stoped_humi, |
|||
string continued_humi, string injection_pump_speed); |
|||
void addFormula(string formula_id, int loglevel, int stoped_gs, int continued_gs, int stoped_satur, int continued_satur, int stoped_humi, int continued_humi, |
|||
int injection_pump_speed); |
|||
shared_ptr<db::Formula> delFormula(int id); |
|||
shared_ptr<db::Formula> updateFormula(int id, string column, string val); |
|||
shared_ptr<db::Formula> updateFormula(shared_ptr<db::Formula> formula); |
|||
|
|||
shared_ptr<db::Formula> getFormula(int id); |
|||
|
|||
/*******************************************************************************
|
|||
* UserBehaviorRecord * |
|||
*******************************************************************************/ |
|||
|
|||
list<shared_ptr<db::UserBehaviorRecord>> getAllUserBehaviorRecord(); |
|||
|
|||
int getUserBehaviorRecordCount(); |
|||
int getUserBehaviorRecordTheFirstId(); |
|||
|
|||
json getUserBehaviorRecordDescJson(int page, int page_size); |
|||
void addUserBehaviorRecord(string uid, int behavior, string behaviorinfo); |
|||
void cleanUserBehaviorRecord(); |
|||
|
|||
private: |
|||
void init_usr_db(); |
|||
void init_setting_db(); |
|||
void init_formula_db(); |
|||
}; |
|||
|
|||
}; // namespace iflytop
|
@ -1,54 +0,0 @@ |
|||
#include "user_behavior_des.hpp"
|
|||
|
|||
using namespace iflytop; |
|||
using namespace iflytop::db; |
|||
|
|||
namespace iflytop { |
|||
namespace db { |
|||
|
|||
string user_behavior_to_str(user_behavior_t behavior) { |
|||
switch (behavior) { |
|||
case kbehavior_login: |
|||
return "登陆"; |
|||
case kbehavior_logout: |
|||
return "登出"; |
|||
case kbehavior_add_user: |
|||
return "添加用户"; |
|||
case kbehavior_del_user: |
|||
return "删除用户"; |
|||
case kbehavior_update_user_permission_level: |
|||
return "更新用户权限"; |
|||
case kbehavior_update_user_passwd: |
|||
return "更新用户密码"; |
|||
case kbehavior_update_user_uid: |
|||
return "更新用户ID"; |
|||
case kbehavior_set_setting_val: |
|||
return "设置参数"; |
|||
case kbehavior_add_formula: |
|||
return "添加配方"; |
|||
case kbehavior_del_formula: |
|||
return "删除配方"; |
|||
case kbehavior_update_formula: |
|||
return "更新配方"; |
|||
case kbehavior_update_formula_name: |
|||
return "更新配方名称"; |
|||
case kbehavior_do_disinfection: |
|||
return "开始消毒"; |
|||
case kbehavior_stop_disinfection: |
|||
return "停止消毒"; |
|||
case kbehavior_do_formula: |
|||
return "执行配方"; |
|||
case kbehavior_update_setting_val_on_disinfection: |
|||
return "消毒中更新参数"; |
|||
case kbehavior_export_disinfection_data: |
|||
return "导出消毒数据"; |
|||
case kbehavior_export_user_action_data: |
|||
return "导出用户操作数据"; |
|||
default: |
|||
break; |
|||
} |
|||
return "未知行为"; |
|||
} |
|||
|
|||
} // namespace db
|
|||
} // namespace iflytop
|
@ -1,41 +0,0 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
|
|||
namespace db { |
|||
typedef enum { |
|||
kbehavior_login = 0, |
|||
kbehavior_logout = 1, |
|||
kbehavior_add_user = 2, |
|||
kbehavior_del_user = 3, |
|||
kbehavior_update_user_uid = 5, |
|||
kbehavior_update_user_permission_level = 6, |
|||
kbehavior_update_user_passwd = 7, |
|||
kbehavior_set_setting_val = 8, |
|||
kbehavior_add_formula = 9, |
|||
kbehavior_del_formula = 10, |
|||
kbehavior_update_formula = 11, |
|||
kbehavior_do_disinfection = 12, |
|||
kbehavior_stop_disinfection = 13, |
|||
kbehavior_do_formula = 14, |
|||
kbehavior_update_setting_val_on_disinfection = 15, |
|||
kbehavior_export_disinfection_data = 16, |
|||
kbehavior_export_user_action_data = 17, |
|||
kbehavior_update_formula_name = 18, |
|||
} user_behavior_t; |
|||
|
|||
string user_behavior_to_str(user_behavior_t behavior); |
|||
|
|||
} // namespace db
|
|||
|
|||
} // namespace iflytop
|
@ -1,133 +0,0 @@ |
|||
#include "data_export_service.hpp"
|
|||
using namespace iflytop; |
|||
|
|||
void DataExportService::loop() { |
|||
ThisThread thisThread; |
|||
while (!thisThread.getExitFlag()) { |
|||
} |
|||
} |
|||
|
|||
void DataExportService::initialize() { GET_TO_SERVICE(m_dbService); } |
|||
|
|||
bool DataExportService::isDetectedUDisk(string& diskpath) { |
|||
/**
|
|||
* @brief 检查/dev/sda是否存在 |
|||
*/ |
|||
// 检查 /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh 是否存在
|
|||
for (int i = 0; i < 8; i++) { |
|||
string dev = fmt::format("/dev/sd{}", (char)('a' + i)); |
|||
logger->info("check dev: {}", dev); |
|||
if (access(dev.c_str(), F_OK) == 0) { |
|||
string subdev = fmt::format("/dev/sd{}1", (char)('a' + i)); |
|||
if (access(subdev.c_str(), F_OK) == 0) { |
|||
diskpath = subdev; |
|||
} else { |
|||
diskpath = dev; |
|||
} |
|||
m_ismounted = true; |
|||
return true; |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
bool DataExportService::dosystem(string cmd) { |
|||
logger->info("do cmd: {}", cmd); |
|||
int ret = system(cmd.c_str()); |
|||
if (ret == 0) { |
|||
return true; |
|||
} else { |
|||
logger->error("do cmd: {} failed,{}", cmd, ret); |
|||
return false; |
|||
} |
|||
} |
|||
static string getTime() { |
|||
struct tm tm = {0}; |
|||
|
|||
time_t t = ::time(nullptr); |
|||
struct tm* tmp = localtime_r(&t, &tm); |
|||
return fmt::format("{:0>4}{:0>2}{:0>2}{:0>2}{:0>2}{:0>2}", tm.tm_year + 1900, //
|
|||
tm.tm_mon + 1, //
|
|||
tm.tm_mday, //
|
|||
tm.tm_hour, //
|
|||
tm.tm_min, tm.tm_sec); |
|||
} |
|||
|
|||
#define IF_ERROR_RETURN(x) \
|
|||
if (!x) { \ |
|||
return err::ksys_copy_file_error; \ |
|||
} |
|||
err::error_t DataExportService::exportDisinfectionData() { |
|||
lock_guard<mutex> lock(lock_); |
|||
|
|||
string diskpath; |
|||
if (!isDetectedUDisk(diskpath)) { |
|||
logger->error("no disk detected"); |
|||
return err::kharde_unfound; |
|||
} |
|||
|
|||
logger->info("diskpath: {}", diskpath); |
|||
|
|||
// 创建目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("rm -rf /mnt/exportdata"))); |
|||
IF_ERROR_RETURN(dosystem(fmt::format("mkdir -p /mnt/exportdata"))); |
|||
// 挂载目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("mount {} /mnt/exportdata", diskpath))); |
|||
// 拷贝文件
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("cp -rf /app/disinfection_logs /mnt/exportdata/disinfection_logs{}", getTime()))); |
|||
// 卸载目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("umount /mnt/exportdata"))); |
|||
// 删除目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("rm -rf /mnt/exportdata"))); |
|||
|
|||
return err::ksucc; |
|||
} |
|||
err::error_t DataExportService::exportAuditData() { |
|||
lock_guard<mutex> lock(lock_); |
|||
|
|||
string diskpath; |
|||
if (!isDetectedUDisk(diskpath)) { |
|||
logger->error("no disk detected"); |
|||
return err::kharde_unfound; |
|||
} |
|||
|
|||
logger->info("diskpath: {}", diskpath); |
|||
|
|||
/**
|
|||
* @brief 创建导出文件csv |
|||
*/ |
|||
|
|||
auto records = m_dbService->getAllUserBehaviorRecord(); |
|||
string filename = fmt::format("/tmp/audit{}.csv", getTime()); |
|||
|
|||
ofstream ofs(filename); |
|||
if (!ofs.is_open()) { |
|||
logger->error("open file {} failed", filename); |
|||
return err::ksys_open_file_error; |
|||
} |
|||
|
|||
ofs << "date,uid,operation" << endl; |
|||
for (auto& record : records) { |
|||
ofs << fmt::format("{},{},\"{}{}\"", record->date, record->uid, db::user_behavior_to_str((user_behavior_t)record->behavior), record->behaviorinfo) << endl; |
|||
} |
|||
ofs.close(); |
|||
// 文件编码转换
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("iconv -f UTF-8 -tgb18030 {} -o {}", filename, filename + ".1"))); |
|||
dosystem(fmt::format("mv {} {}", filename + ".1", filename)); |
|||
|
|||
/**
|
|||
* @brief 拷贝文件到U盘 |
|||
*/ |
|||
// 创建目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("rm -rf /mnt/exportdata"))); |
|||
IF_ERROR_RETURN(dosystem(fmt::format("mkdir -p /mnt/exportdata"))); |
|||
// 挂载目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("mount {} /mnt/exportdata", diskpath))); |
|||
// 拷贝文件
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("cp -rf {} /mnt/exportdata", filename))); |
|||
// 卸载目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("umount /mnt/exportdata"))); |
|||
// 删除目录
|
|||
IF_ERROR_RETURN(dosystem(fmt::format("rm -rf /mnt/exportdata"))); |
|||
return err::ksucc; |
|||
} |
@ -1,60 +0,0 @@ |
|||
//
|
|||
// Created by zwsd
|
|||
//
|
|||
|
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
#include <mutex>
|
|||
|
|||
#include "iflytop/components/zcanreceiver/zcanhost.hpp"
|
|||
#include "iflytop/core/core.hpp"
|
|||
#include "zservice_container/zservice_container.hpp"
|
|||
// lock_guard<mutex> lock(lock_);
|
|||
|
|||
#include <mutex>
|
|||
// std::lock_guard<std::recursive_mutex> lock(lock_);
|
|||
#include "iflytop/core/error/error_code.hpp"
|
|||
//
|
|||
#include "db/db_service.hpp"
|
|||
|
|||
namespace iflytop { |
|||
|
|||
class DataExportService { |
|||
ENABLE_LOGGER(DataExportService); |
|||
|
|||
private: |
|||
bool m_ismounted = false; |
|||
|
|||
unique_ptr<Thread> m_thread; |
|||
shared_ptr<DBService> m_dbService; |
|||
|
|||
mutex lock_; |
|||
|
|||
public: |
|||
DataExportService(/* args */){}; |
|||
~DataExportService(){}; |
|||
|
|||
void initialize(); |
|||
|
|||
bool isDetectedUDisk(string& diskpath); |
|||
void mountDisk(); |
|||
void unmountDisk(); |
|||
|
|||
err::error_t exportDisinfectionData(); |
|||
err::error_t exportAuditData(); |
|||
|
|||
private: |
|||
void loop(); |
|||
bool dosystem(string cmd); |
|||
}; |
|||
|
|||
}; // namespace iflytop
|
@ -1,63 +0,0 @@ |
|||
//
|
|||
// Created by zwsd
|
|||
//
|
|||
|
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "iflytop/core/core.hpp"
|
|||
|
|||
/**
|
|||
* @brief |
|||
* |
|||
* service: DeviceStateService |
|||
* |
|||
* 监听事件: |
|||
* 依赖状态: |
|||
* 依赖服务: |
|||
* 作用: |
|||
* |
|||
*/ |
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace core; |
|||
class DeviceStateService : public enable_shared_from_this<DeviceStateService> { |
|||
ENABLE_LOGGER(DeviceStateService); |
|||
|
|||
bool loginFlag = false; |
|||
string login_uid; |
|||
int login_permission_level; |
|||
int login_visible; |
|||
|
|||
public: |
|||
DeviceStateService(){}; |
|||
|
|||
void setLoginState(string uid, int permission_level, int visible) { |
|||
loginFlag = true; |
|||
login_uid = uid; |
|||
login_permission_level = permission_level; |
|||
login_visible = visible; |
|||
} |
|||
|
|||
void unlogin() { |
|||
loginFlag = false; |
|||
login_uid = ""; |
|||
login_permission_level = 0; |
|||
} |
|||
bool isLogin() { return loginFlag; } |
|||
int getLoginPermissionLevel() { return login_permission_level; } |
|||
int getLoginVisible() { return login_visible; } |
|||
string getLoginUid() { return login_uid; } |
|||
|
|||
void initialize(){}; |
|||
}; |
|||
} // namespace iflytop
|
@ -1,679 +0,0 @@ |
|||
#include "disinfection_ctl_service.hpp"
|
|||
using namespace iflytop; |
|||
using namespace std; |
|||
/**
|
|||
* @brief |
|||
* 流程 |
|||
* 预热: |
|||
* |
|||
* 风机开 |
|||
* 1s |
|||
* 空压机 |
|||
* 1s |
|||
* 蠕动泵 |
|||
* 1s |
|||
* |
|||
* |
|||
* 关蠕动泵 |
|||
* 1s |
|||
* 关空压机 |
|||
* 1s |
|||
* 关风机 |
|||
* |
|||
*/ |
|||
|
|||
// #define PRE_HEAT_TIME (2)
|
|||
// #define PRE_HEAT_TIME (5 * 60)
|
|||
|
|||
#define DVALUE_COMPUTEPERIOD_TIME_S (10.0)
|
|||
#define MAX_VOLUME (5000)
|
|||
|
|||
namespace iflytop { |
|||
extern bool g_in_test; |
|||
|
|||
} |
|||
|
|||
DisinfectionCtrlService::DisinfectionCtrlService() {} |
|||
void DisinfectionCtrlService::initialize() { |
|||
GET_TO_SERVICE(m_zcanHost); |
|||
GET_TO_SERVICE(m_deviceIoControlService); |
|||
GET_TO_SERVICE(m_dbService); |
|||
GET_TO_SERVICE(m_disinfectionLogsManager); |
|||
|
|||
m_deviceIoControlService->drainingPump_close(); |
|||
m_deviceIoControlService->replenishingFluidsPump_close(); |
|||
m_deviceIoControlService->sprayLiquidPump_close(); |
|||
|
|||
m_deviceIoControlService->heartingPlate_setPower(false); |
|||
m_deviceIoControlService->airBlower_setState(false); |
|||
m_deviceIoControlService->airCompressor_setState(false); |
|||
m_dvalueComputer.initialize(); |
|||
} |
|||
|
|||
static string getTime() { |
|||
struct tm tm = {0}; |
|||
|
|||
time_t t = time(nullptr); |
|||
struct tm* tmp = localtime_r(&t, &tm); |
|||
return fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}", tm.tm_year + 1900, //
|
|||
tm.tm_mon + 1, //
|
|||
tm.tm_mday, //
|
|||
tm.tm_hour, //
|
|||
tm.tm_min, tm.tm_sec); |
|||
} |
|||
|
|||
string DisinfectionCtrlService::createDisinfectionID() { |
|||
struct tm tm = {0}; |
|||
|
|||
time_t t = time(nullptr); |
|||
if (t == -1) { |
|||
logger->error("time(nullptr) failed"); |
|||
exit(-1); |
|||
} |
|||
struct tm* tmp = localtime_r(&t, &tm); |
|||
if (!tmp) { |
|||
logger->error("localtime_r failed"); |
|||
exit(-1); |
|||
} |
|||
// tm = *utctime::tm_increment_hour(&tm, 8);
|
|||
// logger->info("trace sendmsg_startCapture {}:{}", __FILE__, __LINE__);
|
|||
return fmt::format("{:0>4}-{:0>2}{:0>2}-{:0>2}{:0>2}{:0>2}", tm.tm_year + 1900, //
|
|||
tm.tm_mon + 1, //
|
|||
tm.tm_mday, //
|
|||
tm.tm_hour, //
|
|||
tm.tm_min, tm.tm_sec); |
|||
} |
|||
static bool zfeq(float a, float b, float eps = 0.01) { |
|||
if (fabs(a - b) < eps) { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
float DisinfectionCtrlService::getDisinfectionDValue(float ppm) { //
|
|||
return m_dvalueComputer.computeDValue(ppm); |
|||
} |
|||
float DisinfectionCtrlService::computeNowLogLevel(DisinfectionContext& context) { |
|||
float dvalue = context.dvalue; |
|||
if (dvalue > 0) { |
|||
/**
|
|||
* @brief 计算 m_nowLoglevel |
|||
*/ |
|||
return context.m_nowLoglevel + DVALUE_COMPUTEPERIOD_TIME_S / (dvalue * 60); |
|||
} |
|||
return context.m_nowLoglevel; |
|||
} |
|||
|
|||
void DisinfectionCtrlService::computeRemainTime(DisinfectionContext& context) { |
|||
/**
|
|||
* @brief 计算Dvalue |
|||
*/ |
|||
float dvalue = context.dvalue; |
|||
|
|||
if (dvalue > 0) { |
|||
/**
|
|||
* @brief 计算 m_nowLoglevel |
|||
*/ |
|||
if (context.m_targetLoglevel >= context.m_nowLoglevel) { |
|||
context.m_remaintime = (context.m_targetLoglevel - context.m_nowLoglevel) * (dvalue * 60); |
|||
} else { |
|||
context.m_remaintime = 0; |
|||
} |
|||
} else { |
|||
//
|
|||
} |
|||
|
|||
logger->info("computeRemainTime minh2o2 {} dvalue {}", context.h2o2data.min_h2o2, dvalue); |
|||
} |
|||
|
|||
void DisinfectionCtrlService::initContext(DisinfectionContext& context, //
|
|||
int loglevel, //
|
|||
float injection_pump_speed, //
|
|||
float stoped_gs, //
|
|||
float continued_gs, //
|
|||
float stoped_satur, //
|
|||
float continued_satur, //
|
|||
float stoped_humi, //
|
|||
float continued_humi //
|
|||
) { |
|||
context.m_disinfectionID = createDisinfectionID(); |
|||
context.pre_heat_time_s = m_dbService->getSettingVal("pre_heat_time_s"); |
|||
context.stoped_gs = stoped_gs; |
|||
context.continued_gs = continued_gs; |
|||
context.stoped_satur = stoped_satur; |
|||
context.continued_satur = continued_satur; |
|||
context.stoped_humi = stoped_humi; |
|||
context.continued_humi = continued_humi; |
|||
context.injection_pump_speed = injection_pump_speed; |
|||
context.injection_pump_speed_changed = true; |
|||
|
|||
if (g_in_test) { |
|||
logger->warn("in test mode, pre_heat_time_s = 5"); |
|||
context.pre_heat_time_s = 5; |
|||
} |
|||
|
|||
logger->info("startDisinfection {} {}", m_context.m_targetLoglevel, m_context.m_disinfectionID); |
|||
logger->info(" stoped_gs {}", context.stoped_gs); |
|||
logger->info(" continued_gs {}", context.continued_gs); |
|||
logger->info(" stoped_satur {}", context.stoped_satur); |
|||
logger->info(" continued_satur {}", context.continued_satur); |
|||
logger->info(" stoped_humi {}", context.stoped_humi); |
|||
logger->info(" continued_humi {}", context.continued_humi); |
|||
logger->info(" pre_heat_time_s {}", context.pre_heat_time_s); |
|||
logger->info(""); |
|||
|
|||
// m_remaintime = loglevel * 20 * 60; // 计算总的加热时间
|
|||
context.m_remaintime = context.pre_heat_time_s + loglevel * 90 * 60; // 计算总的加热时间
|
|||
m_disinfectionWorkState = 1; |
|||
context.m_targetLoglevel = loglevel; |
|||
context.m_nowLoglevel = 0; |
|||
// m_context.m_preheatFlag = true;
|
|||
m_context.dvalue = 0; |
|||
// m_context.stopedflag = false;
|
|||
|
|||
m_context.m_state = kpreheat; |
|||
|
|||
m_context.m_starttp = zsteady_clock().now(); |
|||
|
|||
m_zcanHost->warning_light_ctrl_c1002(1, 0, 0, 1, 0); |
|||
m_deviceIoControlService->heartingPlate_setPower(true); |
|||
context.csvlogger = m_disinfectionLogsManager->createNewLogger(context.m_disinfectionID); |
|||
|
|||
// zsteady_clock().elapsedTimeS(m_context.m_starttp), //
|
|||
// sensors[0].h2o2, sensors[0].temp, sensors[0].humid, sensors[0].saturation, //
|
|||
// sensors[1].h2o2, sensors[1].temp, sensors[1].humid, sensors[1].saturation, //
|
|||
// sensors[2].h2o2, sensors[2].temp, sensors[2].humid, sensors[2].saturation, //
|
|||
// m_context.dvalue, m_context.m_nowLoglevel, (int)m_context.m_targetLoglevel, //
|
|||
// ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getRPM(), //
|
|||
// ds->getPressureSensorData(1), ds->getPressureSensorData(2), ds->getPressureSensorData(3), ds->getPressureSensorData(4), //
|
|||
// m_deviceIoControlService->getDisinfectantVolume_g(), //
|
|||
// m_context.m_remaintime
|
|||
|
|||
context.csvlogger->write( |
|||
"Date," //
|
|||
"Hydrogen peroxide volume(ppm),Temperature(C),Relative humidity(%RH),H2O H2O2 RS(%RS)," //
|
|||
// "h2o22,temp2,humi2,saturation2," //
|
|||
// "h2o23,temp3,humi3,saturation3," //
|
|||
"Dvalue,Loglevel,Targetloglevel," //
|
|||
"Heating,Blower,Compressor,Pump(g/min)," //
|
|||
"Disinfectant Volume(g)," //
|
|||
"Remaining time (s)\n" //
|
|||
); |
|||
} |
|||
static string formattimeS(int sec) { |
|||
if (sec >= 0) { |
|||
return fmt::format("{:0>2}:{:0>2}:{:0>2}", sec / 3600, sec % 3600 / 60, sec % 60); |
|||
} else { |
|||
return fmt::format("--:--:--"); |
|||
} |
|||
} |
|||
void DisinfectionCtrlService::dumpDisinfectionLogsToCSV(DisinfectionContext& context) { |
|||
auto* sensors = &m_context.h2o2data.h2o2sensor_data[0]; |
|||
auto ds = m_deviceIoControlService; |
|||
float dvalue = 0; |
|||
if (m_context.dvalue <= 0) { |
|||
dvalue = 0; |
|||
} else { |
|||
dvalue = m_context.dvalue; |
|||
} |
|||
|
|||
int remaintime = getEstimatedRemainingTimeS(); |
|||
|
|||
context.csvlogger->write( |
|||
fmt::format("{}," //
|
|||
"{},{},{},{}," //
|
|||
// "{},{},{},{}," //
|
|||
// "{},{},{},{}," //
|
|||
"{},{},{}," //
|
|||
"{},{},{},{}," //
|
|||
// "{},{},{},{}," //
|
|||
"{}," //
|
|||
"{}\n" //
|
|||
, |
|||
getTime(), //
|
|||
sensors[0].h2o2, sensors[0].temp, sensors[0].humid, sensors[0].saturation, //
|
|||
// sensors[1].h2o2, sensors[1].temp, sensors[1].humid, sensors[1].saturation, //
|
|||
// sensors[2].h2o2, sensors[2].temp, sensors[2].humid, sensors[2].saturation, //
|
|||
(int32_t)dvalue, (int32_t)m_context.m_nowLoglevel, (int32_t)m_context.m_targetLoglevel, //
|
|||
ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), //
|
|||
m_deviceIoControlService->getDisinfectantVolume_g(), //
|
|||
formattimeS(remaintime))); |
|||
} |
|||
|
|||
void DisinfectionCtrlService::finishDisinfection(DisinfectionContext& context) { |
|||
context.m_remaintime = 0; |
|||
logger->info("stop disinfection {}", context.m_disinfectionID); |
|||
|
|||
// sprayLiquidPump_close();
|
|||
m_deviceIoControlService->sprayLiquidPump_close(); |
|||
usleep(1000 * 1000); |
|||
// airCompressor(false);
|
|||
m_deviceIoControlService->airCompressor_setState(false); |
|||
usleep(1000 * 1000); |
|||
// blower_setPower(false);
|
|||
m_deviceIoControlService->airBlower_setState(false); |
|||
usleep(1000 * 1000); |
|||
// heartingPlate_setPower(false);
|
|||
m_deviceIoControlService->heartingPlate_setPower(false); |
|||
m_disinfectionWorkState = 3; |
|||
m_zcanHost->warning_light_ctrl_c1002(1, 0, 0, 0, 0); |
|||
context.csvlogger = nullptr; |
|||
} |
|||
|
|||
void DisinfectionCtrlService::processPreheatState(DisinfectionContext& context) { |
|||
int hasstarttime = zsteady_clock().elapsedTimeS(context.m_starttp); |
|||
|
|||
// logger->info("preheat {}", context.m_disinfectionID);
|
|||
if ((context.m_state == kpreheat && hasstarttime > m_context.pre_heat_time_s)) { |
|||
logger->info("preheat finished {}", context.m_disinfectionID); |
|||
// blower_setPower(true);
|
|||
m_deviceIoControlService->airBlower_setState(true); |
|||
usleep(1000 * 1000); |
|||
// airCompressor(true);
|
|||
m_deviceIoControlService->airCompressor_setState(true); |
|||
usleep(1000 * 1000); |
|||
// sprayLiquidPump_open();
|
|||
m_deviceIoControlService->sprayLiquidPump_open(context.injection_pump_speed); |
|||
context.m_state = kdisinfection; |
|||
// context.m_preheatFlag = false;
|
|||
// context.sprayLiquidFlag = true;
|
|||
} else { |
|||
logger->info("{}: preheat {}", context.m_disinfectionID, m_context.pre_heat_time_s - hasstarttime); |
|||
} |
|||
} |
|||
|
|||
void DisinfectionCtrlService::dumpDisinfectionLogs(DisinfectionContext& context) { |
|||
// float h2o2_g = m_deviceIoControlService->getDisinfectantVolume_g();
|
|||
auto* sensors = &m_context.h2o2data.h2o2sensor_data[0]; |
|||
auto ds = m_deviceIoControlService; |
|||
logger->info( |
|||
"T:{}," //
|
|||
"s1:({},{},{},{})," //
|
|||
"s2:({},{},{},{})," //
|
|||
"s3:({},{},{},{})," //
|
|||
"D:{},log:({}:{})," //
|
|||
"io:({},{},{},{})," //
|
|||
"p:({},{},{},{})," //
|
|||
"h2o2g:{}," //
|
|||
"rt:{}" //
|
|||
, |
|||
zsteady_clock().elapsedTimeS(m_context.m_starttp), //
|
|||
sensors[0].h2o2, sensors[0].temp, sensors[0].humid, sensors[0].saturation, //
|
|||
sensors[1].h2o2, sensors[1].temp, sensors[1].humid, sensors[1].saturation, //
|
|||
sensors[2].h2o2, sensors[2].temp, sensors[2].humid, sensors[2].saturation, //
|
|||
m_context.dvalue, m_context.m_nowLoglevel, (int)m_context.m_targetLoglevel, //
|
|||
ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getRPM(), //
|
|||
ds->getPressureSensorData(1), ds->getPressureSensorData(2), ds->getPressureSensorData(3), ds->getPressureSensorData(4), //
|
|||
m_deviceIoControlService->getDisinfectantVolume_g(), //
|
|||
getEstimatedRemainingTimeS()); |
|||
} |
|||
|
|||
/**
|
|||
* @brief |
|||
* 消毒中状态处理 |
|||
*/ |
|||
void DisinfectionCtrlService::processDisinfectionState(DisinfectionContext& context) { |
|||
ZCHECK(context.m_state == kdisinfection || context.m_state == kdisinfection_take_a_break, "state error"); |
|||
|
|||
/**
|
|||
* @brief 周期性计算剩余时间 |
|||
*/ |
|||
|
|||
/**
|
|||
* @brief 根据湿度启停喷雾 |
|||
*/ |
|||
if (m_context.m_state == kdisinfection) { |
|||
/**
|
|||
* @brief 检查当前 |
|||
*/ |
|||
float nowSatur = m_context.h2o2data.max_saturation; |
|||
float nowh2o2 = m_context.h2o2data.max_h2o2; |
|||
float humid = m_context.h2o2data.max_humid; |
|||
|
|||
if (m_context.injection_pump_speed_changed) { |
|||
m_deviceIoControlService->sprayLiquidPump_open(context.injection_pump_speed); |
|||
m_context.injection_pump_speed_changed = false; |
|||
} |
|||
|
|||
// humid > m_context.stoped_satur
|
|||
if (nowSatur > m_context.stoped_satur || nowh2o2 > m_context.stoped_gs || humid > m_context.stoped_humi) { |
|||
logger->info("stop sprayLiquid"); |
|||
|
|||
m_deviceIoControlService->sprayLiquidPump_close(); |
|||
usleep(1000 * 1000); |
|||
m_deviceIoControlService->airCompressor_setState(false); |
|||
|
|||
// m_context.sprayLiquidFlag = false;
|
|||
m_context.m_state = kdisinfection_take_a_break; |
|||
} |
|||
} else { |
|||
float nowSatur = m_context.h2o2data.max_saturation; |
|||
float nowh2o2 = m_context.h2o2data.max_h2o2; |
|||
float humid = m_context.h2o2data.max_humid; |
|||
|
|||
// && humid < m_context.continued_satur
|
|||
if (nowSatur < m_context.continued_satur && nowh2o2 < m_context.continued_gs && humid < context.continued_humi) { |
|||
logger->info("start sprayLiquid"); |
|||
|
|||
m_deviceIoControlService->sprayLiquidPump_open(context.injection_pump_speed); |
|||
usleep(1000 * 1000); |
|||
m_deviceIoControlService->airCompressor_setState(true); |
|||
m_context.m_state = kdisinfection; |
|||
} |
|||
} |
|||
} |
|||
|
|||
void DisinfectionCtrlService::disinfectionLoop(bool& breakflag) { |
|||
// logger->info("disinfection running {} {}s preheatFlag:{}", m_context.m_disinfectionID, m_context.m_remaintime, m_context.m_preheatFlag);
|
|||
m_context.m_remaintime--; |
|||
bool forcelog = false; |
|||
if (m_context.m_remaintime < 0) { |
|||
m_context.m_remaintime = 0; |
|||
} |
|||
|
|||
/**
|
|||
* @brief 更新传感器信息 |
|||
*/ |
|||
m_deviceIoControlService->getAllSensorData(m_context.h2o2data); |
|||
/**
|
|||
* @brief 计算D值 |
|||
*/ |
|||
m_context.dvalue = getDisinfectionDValue(m_context.h2o2data.min_h2o2); |
|||
|
|||
if (zsteady_clock().elapsedTimeS(m_context.m_lastComputeDvalueTp) > DVALUE_COMPUTEPERIOD_TIME_S) { |
|||
m_context.m_lastComputeDvalueTp = zsteady_clock().now(); |
|||
//
|
|||
if (m_context.m_state == kdisinfection || m_context.m_state == kdisinfection_take_a_break) { |
|||
m_context.m_nowLoglevel = computeNowLogLevel(m_context); |
|||
computeRemainTime(m_context); |
|||
} |
|||
} |
|||
|
|||
if (m_context.m_state == kpreheat) { |
|||
/**
|
|||
* @brief 预热中 |
|||
*/ |
|||
processPreheatState(m_context); |
|||
} else if (m_context.m_state == kdisinfection || m_context.m_state == kdisinfection_take_a_break) { |
|||
/**
|
|||
* @brief 消毒中 |
|||
*/ |
|||
processDisinfectionState(m_context); |
|||
//
|
|||
if (m_context.m_remaintime <= 0 && m_context.m_nowLoglevel > (m_context.m_targetLoglevel + 0.01)) { |
|||
m_context.m_remaintime = 0; |
|||
m_context.m_nowLoglevel = m_context.m_targetLoglevel + 0.01; |
|||
logger->info("disinfection finished {},but waitting for h2o2 to safe", m_context.m_disinfectionID); |
|||
m_deviceIoControlService->sprayLiquidPump_close(); |
|||
usleep(1000 * 1000); |
|||
m_deviceIoControlService->airCompressor_setState(false); |
|||
usleep(1000 * 1000); |
|||
m_deviceIoControlService->heartingPlate_setPower(false); |
|||
m_context.m_state = kwait_for_h2o2_down; |
|||
forcelog = true; |
|||
} |
|||
|
|||
} else if (m_context.m_state == kwait_for_h2o2_down) { |
|||
/**
|
|||
* @brief 等待h2o2浓度下降 |
|||
*/ |
|||
logger->info("waitting for h2o2 concentration to safe value {}=>{}", m_context.h2o2data.min_h2o2, 1); |
|||
if (m_context.h2o2data.min_h2o2 < 1) { |
|||
logger->info("h2o2 concentration to safe value"); |
|||
breakflag = true; |
|||
forcelog = true; |
|||
m_context.m_state = kfinished; |
|||
} |
|||
} else { |
|||
ZCHECK(false, "state error"); |
|||
} |
|||
|
|||
if (forcelog || zsteady_clock().elapsedTimeS(m_context.m_lastlogTp) > DVALUE_COMPUTEPERIOD_TIME_S) { |
|||
m_context.m_lastlogTp = zsteady_clock().now(); |
|||
dumpDisinfectionLogs(m_context); |
|||
dumpDisinfectionLogsToCSV(m_context); |
|||
} |
|||
} |
|||
|
|||
void DisinfectionCtrlService::changeDisinfectionParameter(int injection_pump_speed, //
|
|||
int stoped_gs, //
|
|||
int continued_gs, //
|
|||
int stoped_satur, //
|
|||
int continued_satur, //
|
|||
int stoped_humi, //
|
|||
int continued_humi) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
// logger->info("changeInjectionPumpSpeed {}=>{}", m_context.injection_pump_speed, speed);
|
|||
// m_context.injection_pump_speed = speed;
|
|||
m_context.injection_pump_speed = injection_pump_speed; |
|||
m_context.stoped_gs = stoped_gs; |
|||
m_context.continued_gs = continued_gs; |
|||
m_context.stoped_satur = stoped_satur; |
|||
m_context.continued_satur = continued_satur; |
|||
m_context.stoped_humi = stoped_humi; |
|||
m_context.continued_humi = continued_humi; |
|||
m_context.injection_pump_speed_changed = true; |
|||
|
|||
logger->info("changeDisinfectionParameter {} {} {} {} {} {} {}", injection_pump_speed, stoped_gs, continued_gs, stoped_satur, continued_satur, stoped_humi, |
|||
continued_humi); |
|||
} |
|||
|
|||
void DisinfectionCtrlService::startDisinfection(int loglevel, //
|
|||
int injection_pump_speed, //
|
|||
int stoped_gs, //
|
|||
int continued_gs, //
|
|||
int stoped_satur, //
|
|||
int continued_satur, //
|
|||
int stoped_humi, //
|
|||
int continued_humi //
|
|||
) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
|
|||
/**
|
|||
* @TODO |
|||
* 检查当前环境湿度,湿度过大,不允许消毒 |
|||
*/ |
|||
|
|||
if (m_disinfectionThread) { |
|||
stopDisinfection(); |
|||
} |
|||
initContext(m_context, loglevel, injection_pump_speed, stoped_gs, continued_gs, stoped_satur, continued_satur, stoped_humi, continued_humi); |
|||
|
|||
m_disinfectionThread.reset(new Thread("m_disinfectionThread", [this]() { |
|||
ThisThread thisThread; |
|||
while (!thisThread.getExitFlag()) { |
|||
thisThread.sleepForMs(1000); |
|||
bool breakflag = false; |
|||
disinfectionLoop(breakflag); |
|||
if (breakflag) { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
//
|
|||
finishDisinfection(m_context); |
|||
})); |
|||
//
|
|||
} |
|||
|
|||
int DisinfectionCtrlService::getDisinfectionWorkState() { return m_context.m_state; } |
|||
|
|||
void DisinfectionCtrlService::stopDisinfection() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (m_disinfectionThread) { |
|||
m_disinfectionThread->join(); |
|||
m_disinfectionThread = nullptr; |
|||
} |
|||
m_context.m_state = kfinished; |
|||
m_disinfectionWorkState = 0; |
|||
} |
|||
|
|||
int32_t DisinfectionCtrlService::getEstimatedRemainingTimeS() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (m_context.m_state == kpreheat) { |
|||
return getPreHeatRaminTimeS(); |
|||
} else if (m_context.m_state == kdisinfection || m_context.m_state == kdisinfection_take_a_break) { |
|||
if (m_context.dvalue > 0) { |
|||
return m_context.m_remaintime; |
|||
} else { |
|||
return -1; |
|||
} |
|||
} else { |
|||
return 0; |
|||
} |
|||
} |
|||
int32_t DisinfectionCtrlService::getPreHeatRaminTimeS() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
int32_t remaintime = 0; |
|||
if (m_context.m_state == kpreheat) { |
|||
remaintime = m_context.pre_heat_time_s - zsteady_clock().elapsedTimeS(m_context.m_starttp); |
|||
if (remaintime < 0) { |
|||
remaintime = 0; |
|||
} |
|||
return remaintime; |
|||
} else { |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
string DisinfectionCtrlService::getDisinfectionID() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
return m_context.m_disinfectionID; |
|||
} |
|||
bool DisinfectionCtrlService::isPreheatState() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
return m_context.m_state == kpreheat; |
|||
} |
|||
|
|||
int DisinfectionCtrlService::getReplenishingFluidsWorkState() { return m_replenishingFluidsWorkState; } |
|||
int DisinfectionCtrlService::getDrainingWorkState() { return m_drainingWorkState; } |
|||
|
|||
/*******************************************************************************
|
|||
* // 加液 *
|
|||
*******************************************************************************/ |
|||
/**
|
|||
* @brief 开始加液 |
|||
* |
|||
* @param stopatg |
|||
*/ |
|||
void DisinfectionCtrlService::startReplenishingFluids(int stopatg) { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (m_disinfectionThread) { |
|||
m_disinfectionThread->join(); |
|||
m_disinfectionThread = nullptr; |
|||
} |
|||
int32_t nowvolume = m_deviceIoControlService->getDisinfectantVolume_g(); |
|||
if (nowvolume > stopatg) { |
|||
logger->warn("start Replenishing fail, nowvolume {} > stopatg {}", nowvolume, stopatg); |
|||
return; |
|||
} |
|||
|
|||
m_disinfectionThread.reset(new Thread("disinfectionThread", [this, stopatg]() { |
|||
ThisThread thisThread; |
|||
m_deviceIoControlService->replenishingFluidsPump_open(); |
|||
logger->info("startReplenishingFluids {}g", stopatg); |
|||
while (!thisThread.getExitFlag()) { |
|||
int32_t nowvolume = m_deviceIoControlService->getDisinfectantVolume_g(); |
|||
logger->info("replenishingFluids {}g", nowvolume); |
|||
if (nowvolume > stopatg) { |
|||
break; |
|||
} |
|||
|
|||
if (nowvolume > MAX_VOLUME) { |
|||
logger->warn("replenishingFluids reach full level {}g", nowvolume); |
|||
break; |
|||
} |
|||
|
|||
thisThread.sleepForMs(1000); |
|||
} |
|||
logger->info("stopReplenishingFluids"); |
|||
// replenishingFluidsPump_close();
|
|||
m_deviceIoControlService->replenishingFluidsPump_close(); |
|||
m_replenishingFluidsWorkState = 0; |
|||
})); |
|||
|
|||
//
|
|||
m_replenishingFluidsWorkState = 1; |
|||
logger->info("startReplenishingFluids "); |
|||
} |
|||
/**
|
|||
* @brief 停止加液 |
|||
* |
|||
*/ |
|||
void DisinfectionCtrlService::stopReplenishingFluids() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (m_disinfectionThread) { |
|||
m_disinfectionThread->join(); |
|||
m_disinfectionThread = nullptr; |
|||
} |
|||
|
|||
logger->info("stopReplenishingFluids"); |
|||
// replenishingFluidsPump_close();
|
|||
m_deviceIoControlService->replenishingFluidsPump_close(); |
|||
m_replenishingFluidsWorkState = 0; |
|||
} |
|||
/*******************************************************************************
|
|||
* 排液 * |
|||
*******************************************************************************/ |
|||
/**
|
|||
* @brief 开始排液 |
|||
* |
|||
*/ |
|||
void DisinfectionCtrlService::startDraining() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (m_disinfectionThread) { |
|||
m_disinfectionThread->join(); |
|||
m_disinfectionThread = nullptr; |
|||
} |
|||
|
|||
m_disinfectionThread.reset(new Thread("disinfectionThread", [this]() { |
|||
ThisThread thisThread; |
|||
|
|||
m_deviceIoControlService->drainingPump_open(); |
|||
logger->info("startDraining "); |
|||
|
|||
auto startdrainingtime = zsteady_clock().now(); |
|||
|
|||
zsteady_tp volumeReachZeroTime; |
|||
bool volumeReachZeroFlag = false; |
|||
|
|||
while (!thisThread.getExitFlag()) { |
|||
int32_t nowvolume = m_deviceIoControlService->getDisinfectantVolume_g(); |
|||
logger->info("draining remain {} g", nowvolume); |
|||
if (!volumeReachZeroFlag && nowvolume == 0) { |
|||
volumeReachZeroTime = zsteady_clock().now(); |
|||
volumeReachZeroFlag = true; |
|||
} |
|||
|
|||
if (volumeReachZeroFlag) { |
|||
logger->info("stopDraining after {} s", 30 - zsteady_clock().elapsedTimeS(volumeReachZeroTime)); |
|||
if (zsteady_clock().elapsedTimeS(volumeReachZeroTime) > 30) { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
thisThread.sleepForMs(1000); |
|||
} |
|||
logger->info("stopDraining"); |
|||
// replenishingFluidsPump_close();
|
|||
m_deviceIoControlService->drainingPump_close(); |
|||
m_drainingWorkState = 0; |
|||
})); |
|||
|
|||
logger->info("startDraining"); |
|||
// drainingPump_open();
|
|||
m_drainingWorkState = 1; |
|||
} |
|||
/**
|
|||
* @brief 停止排液体 |
|||
*/ |
|||
void DisinfectionCtrlService::stopDraining() { |
|||
lock_guard<recursive_mutex> lock(lock_); |
|||
if (m_disinfectionThread) { |
|||
m_disinfectionThread->join(); |
|||
m_disinfectionThread = nullptr; |
|||
} |
|||
logger->info("stopDraining"); |
|||
m_drainingWorkState = 0; |
|||
// drainingPump_close();
|
|||
m_deviceIoControlService->drainingPump_close(); |
|||
} |
@ -1,186 +0,0 @@ |
|||
//
|
|||
// Created by zwsd
|
|||
//
|
|||
|
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "db/db_service.hpp"
|
|||
#include "disinfection_logs_manager.hpp"
|
|||
#include "iflytop/components/zcanreceiver/zcanhost.hpp"
|
|||
#include "iflytop/core/core.hpp"
|
|||
#include "service/device_io_control_service.hpp"
|
|||
#include "utils/dvalue_computer.hpp"
|
|||
#include "zservice_container/zservice_container.hpp"
|
|||
/**
|
|||
* @brief |
|||
* |
|||
* service: DisinfectionCtrlService |
|||
* |
|||
* 监听事件: |
|||
* 依赖状态: |
|||
* 依赖服务: |
|||
* 作用: |
|||
* |
|||
*/ |
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace core; |
|||
class DisinfectionCtrlService : public enable_shared_from_this<DisinfectionCtrlService> { |
|||
ENABLE_LOGGER(DisinfectionCtrlService); |
|||
|
|||
public: |
|||
typedef enum { |
|||
kidle = 0, |
|||
kpreheat = 1, |
|||
kdisinfection = 2, |
|||
kdisinfection_take_a_break = 3, |
|||
kwait_for_h2o2_down = 4, |
|||
kfinished = 5, |
|||
} state_t; |
|||
|
|||
private: |
|||
unique_ptr<Thread> m_disinfectionThread; |
|||
shared_ptr<DeviceIoControlService> m_deviceIoControlService; |
|||
shared_ptr<DBService> m_dbService; |
|||
shared_ptr<DisinfectionLogsManager> m_disinfectionLogsManager; |
|||
|
|||
DValueComputer m_dvalueComputer; |
|||
|
|||
recursive_mutex lock_; |
|||
|
|||
int m_disinfectionWorkState = 0; |
|||
int m_replenishingFluidsWorkState = 0; |
|||
int m_drainingWorkState = 0; |
|||
|
|||
shared_ptr<ZCanHost> m_zcanHost; |
|||
|
|||
class DisinfectionContext { |
|||
public: |
|||
string m_disinfectionID; |
|||
|
|||
zsteady_tp m_starttp; |
|||
zsteady_tp m_lastComputeDvalueTp; |
|||
zsteady_tp m_lastlogTp; |
|||
|
|||
DeviceIoControlService::all_h2o2sensor_data_t h2o2data; |
|||
|
|||
// bool m_preheatFlag = false;
|
|||
// bool stopedflag = false; // 消毒停止标志,但继续等待h2o2浓度下降
|
|||
int m_remaintime = 0; |
|||
float m_targetLoglevel = 0; |
|||
float m_nowLoglevel = 0; |
|||
float dvalue = 0; |
|||
|
|||
state_t m_state = kidle; |
|||
|
|||
// bool sprayLiquidFlag = false;
|
|||
|
|||
int injection_pump_speed; |
|||
bool injection_pump_speed_changed = false; |
|||
|
|||
int pre_heat_time_s = 0; |
|||
int stoped_gs = 0; |
|||
int continued_gs = 0; |
|||
int stoped_satur = 0; |
|||
int continued_satur = 0; |
|||
int stoped_humi = 0; |
|||
int continued_humi = 0; |
|||
|
|||
shared_ptr<DisinfectionLogger> csvlogger; |
|||
}; |
|||
|
|||
public: |
|||
DisinfectionContext m_context; |
|||
|
|||
public: |
|||
DisinfectionCtrlService(); |
|||
|
|||
public: |
|||
void initialize(); |
|||
/**
|
|||
* @brief 开始消毒 |
|||
* |
|||
* @param loglevel 消毒等级 |
|||
*/ |
|||
|
|||
void startDisinfection(int loglevel, //
|
|||
int injection_pump_speed, //
|
|||
int stoped_gs, //
|
|||
int continued_gs, //
|
|||
int stoped_satur, //
|
|||
int continued_satur, //
|
|||
int stoped_humi, //
|
|||
int continued_humi //
|
|||
); |
|||
void changeDisinfectionParameter(int injection_pump_speed, //
|
|||
int stoped_gs, //
|
|||
int continued_gs, //
|
|||
int stoped_satur, //
|
|||
int continued_satur, //
|
|||
int stoped_humi, //
|
|||
int continued_humi //
|
|||
); |
|||
|
|||
void stopDisinfection(); |
|||
|
|||
int getDisinfectionWorkState(); |
|||
int getReplenishingFluidsWorkState(); |
|||
int getDrainingWorkState(); |
|||
|
|||
/*******************************************************************************
|
|||
* 加液 * |
|||
*******************************************************************************/ |
|||
void startReplenishingFluids(int stopatg); |
|||
void stopReplenishingFluids(); |
|||
/*******************************************************************************
|
|||
* 排液 * |
|||
*******************************************************************************/ |
|||
void startDraining(); |
|||
void stopDraining(); |
|||
/*******************************************************************************
|
|||
* State * |
|||
*******************************************************************************/ |
|||
bool isDisinfectionRunning(); |
|||
int32_t getEstimatedRemainingTimeS(); |
|||
string getDisinfectionID(); |
|||
bool isPreheatState(); |
|||
int32_t getPreHeatRaminTimeS(); |
|||
|
|||
private: |
|||
string createDisinfectionID(); |
|||
|
|||
private: |
|||
float getDisinfectionDValue(float ppm); |
|||
|
|||
void initContext(DisinfectionContext& context, //
|
|||
int loglevel, //
|
|||
float injection_pump_speed, //
|
|||
float stoped_gs, //
|
|||
float continued_gs, //
|
|||
float stoped_satur, //
|
|||
float continued_satur, //
|
|||
float stoped_humi, //
|
|||
float continued_humi //
|
|||
); |
|||
void computeRemainTime(DisinfectionContext& context); |
|||
float computeNowLogLevel(DisinfectionContext& context); |
|||
void processPreheatState(DisinfectionContext& context); |
|||
void processDisinfectionState(DisinfectionContext& context); |
|||
void dumpDisinfectionLogs(DisinfectionContext& context); |
|||
void dumpDisinfectionLogsToCSV(DisinfectionContext& context); |
|||
void finishDisinfection(DisinfectionContext& context); |
|||
|
|||
void disinfectionLoop(bool& breakflag); |
|||
}; |
|||
} // namespace iflytop
|
@ -1,99 +0,0 @@ |
|||
#include "disinfection_logs_manager.hpp"
|
|||
|
|||
#include <dirent.h>
|
|||
#include <errno.h>
|
|||
|
|||
#include "iflytop/core/components/fileutils.hpp"
|
|||
using namespace iflytop; |
|||
using namespace core; |
|||
using namespace std; |
|||
|
|||
#define LOG_STORGE_PATH "./disinfection_logs/"
|
|||
|
|||
DisinfectionLogger::DisinfectionLogger() {} |
|||
DisinfectionLogger::~DisinfectionLogger() { |
|||
if (m_logfile.is_open()) { |
|||
m_logfile.close(); |
|||
} |
|||
} |
|||
|
|||
void DisinfectionLogger::initialize(string log_file_name) { //
|
|||
logger->info("create loggers:{}", log_file_name); |
|||
m_logfile.open(log_file_name, ios::out | ios::binary | ios::trunc); |
|||
if (!m_logfile.is_open()) { |
|||
logger->error("create loggers:{} failed", log_file_name); |
|||
} |
|||
} |
|||
void DisinfectionLogger::write(string log) { |
|||
m_logfile.write(log.c_str(), log.size()); |
|||
if (m_logfile.fail()) { |
|||
logger->error("write log failed:{}", strerror(errno)); |
|||
} |
|||
m_logfile.flush(); |
|||
} |
|||
|
|||
DisinfectionLogsManager::DisinfectionLogsManager(/* args */) {} |
|||
DisinfectionLogsManager::~DisinfectionLogsManager() {} |
|||
|
|||
shared_ptr<DisinfectionLogger> DisinfectionLogsManager::createNewLogger(string log_file_name) { |
|||
system(fmt::format("mkdir -p {}", LOG_STORGE_PATH).c_str()); |
|||
shared_ptr<DisinfectionLogger> logger = make_shared<DisinfectionLogger>(); |
|||
logger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name)); |
|||
return logger; |
|||
} |
|||
|
|||
static void split(const string& s, vector<string>& sv, const char delim = ' ') { |
|||
sv.clear(); |
|||
istringstream iss(s); |
|||
string temp; |
|||
|
|||
while (getline(iss, temp, delim)) { |
|||
sv.push_back(temp); |
|||
} |
|||
return; |
|||
} |
|||
void DisinfectionLogsManager::list_dir_csvfile(string path, vector<string>& sv) { |
|||
sv.clear(); |
|||
DIR* dir; |
|||
struct dirent* ptr; |
|||
if ((dir = opendir(path.c_str())) == NULL) { |
|||
logger->error("Open dir {} error...", path); |
|||
return; |
|||
} |
|||
while ((ptr = readdir(dir)) != NULL) { |
|||
if (ptr->d_name[0] == '.') continue; |
|||
if (ptr->d_type == 8) { |
|||
string filename = ptr->d_name; |
|||
if (filename.find(".csv") != string::npos) { |
|||
sv.push_back(filename); |
|||
} |
|||
} |
|||
} |
|||
closedir(dir); |
|||
return; |
|||
} |
|||
|
|||
nlohmann::json DisinfectionLogsManager::getlogger(string log_file_name) { |
|||
string content = FileUtils().readFileAsString(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name)); |
|||
nlohmann::json csvcontent; |
|||
vector<string> lines; |
|||
split(content, lines, '\n'); |
|||
|
|||
for (auto& line : lines) { |
|||
csvcontent["content"].push_back(line); |
|||
} |
|||
return csvcontent; |
|||
} |
|||
nlohmann::json DisinfectionLogsManager::getLoggerList() { |
|||
//
|
|||
// 1. get all files in LOG_STORGE_PATH
|
|||
vector<string> files; |
|||
list_dir_csvfile(LOG_STORGE_PATH, files); |
|||
nlohmann::json loggerlist; |
|||
for (auto& file : files) { |
|||
// 获取文件名去掉.csv
|
|||
file = file.substr(0, file.find(".csv")); |
|||
loggerlist.push_back(file); |
|||
} |
|||
return loggerlist; |
|||
} |
@ -1,48 +0,0 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "iflytop/core/basic/nlohmann/json.hpp"
|
|||
#include "iflytop/core/core.hpp"
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
|
|||
class DisinfectionLogger { |
|||
ENABLE_LOGGER(DisinfectionLogger); |
|||
ofstream m_logfile; |
|||
|
|||
public: |
|||
DisinfectionLogger(); |
|||
~DisinfectionLogger(); |
|||
|
|||
void initialize(string log_file_name); |
|||
void write(string log); |
|||
}; |
|||
|
|||
class DisinfectionLogsManager { |
|||
ENABLE_LOGGER(DisinfectionLogsManager); |
|||
|
|||
public: |
|||
DisinfectionLogsManager(/* args */); |
|||
~DisinfectionLogsManager(); |
|||
|
|||
void initialize(){}; |
|||
|
|||
shared_ptr<DisinfectionLogger> createNewLogger(string log_file_name); |
|||
|
|||
nlohmann::json getlogger(string log_file_name); |
|||
nlohmann::json getLoggerList(); |
|||
|
|||
private: |
|||
void list_dir_csvfile(string path, vector<string>& sv); |
|||
}; |
|||
|
|||
} // namespace iflytop
|
@ -1,44 +0,0 @@ |
|||
#include "iflytop_can_host_device.hpp"
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
|
|||
void IflytopCanHostDevice::initialize(string can_if_name, int baudrate, bool enablLoopback) { |
|||
// m_iflytopCanProtocolStack.reset(new IflytopCanProtocolStack());
|
|||
|
|||
m_can_if_name = can_if_name; |
|||
m_baudrate = baudrate; |
|||
m_enablLoopback = enablLoopback; |
|||
resetSocketCan(); |
|||
} |
|||
void IflytopCanHostDevice::startListen() {} |
|||
|
|||
void IflytopCanHostDevice::processRx(shared_ptr<SocketCanFrame> frame) { |
|||
if (!frame) { |
|||
return; |
|||
} |
|||
if (frame->getCanIdentifier() == socketcan::kextFrame && //
|
|||
frame->getFanFrameType() == socketcan::kdataframe) { |
|||
/**
|
|||
* @brief 接收can消息 |
|||
*/ |
|||
|
|||
} else { |
|||
logger->warn("Rx unknown can frame"); |
|||
} |
|||
} |
|||
void IflytopCanHostDevice::resetSocketCan() { |
|||
auto socketCanConfig = make_shared<SocketCanConfig>(); |
|||
socketCanConfig->enablLoopback = m_enablLoopback; // 根据 SocketCan::dumpCanDriverInfo() 的输出,确定该标志位是false还是true
|
|||
socketCanConfig->m_canName = m_can_if_name; |
|||
socketCanConfig->m_canBaudrate = m_baudrate; |
|||
socketCanConfig->m_canfilters = {}; |
|||
|
|||
logger->info("initialize() m_canName:{} {}", socketCanConfig->m_canName, socketCanConfig->m_canBaudrate); |
|||
|
|||
m_socketCan.reset(new SocketCan()); |
|||
m_socketCan->initialize(socketCanConfig); |
|||
m_socketCan->startListen(); |
|||
m_socketCan->onSocketCanFrame.connect([this](shared_ptr<SocketCanFrame> canframe) { //
|
|||
processRx(canframe); |
|||
}); |
|||
} |
@ -1,39 +0,0 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "iflytop/core/basic/nlohmann/json.hpp"
|
|||
#include "iflytop/core/driver/socketcan/socket_can.hpp"
|
|||
#include "iflytop/core/spdlogfactory/logger.hpp"
|
|||
#include "zservice_container/zservice_container.hpp"
|
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
class IflytopCanHostDevice { |
|||
ENABLE_LOGGER(IflytopCanHostDevice); |
|||
shared_ptr<SocketCan> m_socketCan; |
|||
|
|||
string m_can_if_name; |
|||
int m_baudrate; |
|||
bool m_enablLoopback; |
|||
|
|||
public: |
|||
IflytopCanHostDevice(){}; |
|||
|
|||
void initialize(string can_if_name, int baudrate, bool enablLoopback); |
|||
void startListen(); |
|||
|
|||
void processRx(shared_ptr<SocketCanFrame> frame); |
|||
|
|||
private: |
|||
void resetSocketCan(); |
|||
}; |
|||
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue