已完成上位机通信代码结构优化

This commit is contained in:
zjk
2025-05-14 21:01:57 +08:00
parent 18f00d70d4
commit b7a624334e
24 changed files with 647 additions and 309 deletions
@@ -107,7 +107,7 @@ public:
auto checkField = [&root](const std::string& field) -> bool {
if(!root.isMember(field) || !root[field].isUInt()) {
// std::cerr << "Missing or invalid field: " << field << std::endl;
LOG_F(ERROR, "Missing or invalid field: %s", field.c_str());
// LOG_F(ERROR, "Missing or invalid field: %s", field.c_str());
return false;
}
return true;
@@ -188,6 +188,8 @@ public:
if(!checkField("sternThrow_uint8")) ok=false; state.sternThrow = root["sternThrow_uint8"].asUInt();
if(!checkField("mainINS_uint8")) ok=false; state.mainINS = root["mainINS_uint8"].asUInt();
//不检查字段缺失
ok = true;
return ok;
};
std::string buildSecdistStateToJson(DeviceSecdistState &state){
@@ -433,6 +435,8 @@ public:
std::string buildMsg(DriverTable &driver){
DeviceSecdistState s;
converFromDriverTable(s,driver);
// std::cout << (int)s.depth1 << std::endl;
// std::cout << buildSecdistStateToJson(s) << std::endl;
return buildSecdistStateToJson(s);
};
@@ -0,0 +1,43 @@
#ifndef UEXTERNCOMM_SETDEVICESWITCH_CMD_H
#define UEXTERNCOMM_SETDEVICESWITCH_CMD_H
#include <string>
class uExternComm_setDeviceSwitchMsg
{
private:
/* data */
public:
std::string key = "uExternComm_setDeviceSwitch_cmd";
uExternComm_setDeviceSwitchMsg(/* args */){};
~uExternComm_setDeviceSwitchMsg(){};
DeviceSwitchCmd getSetDevSwitch(const std::string& key, const std::string& msg)
{
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
DeviceSwitchCmd cmd;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
LOG_F(WARNING, "JSON解析失败: %s", errs.c_str());
cmd.id = -1;
}
// 检查并读取设备开关状态
if (root.isMember("id_uint8") && root["id_uint8"].isUInt()) {
cmd.id = root["id_uint8"].asUInt();
} else {
cmd.id = -1;
LOG_F(WARNING, "缺少或类型不匹配: id_uint8");
}
if (root.isMember("cmd_uint8") && root["cmd_uint8"].isUInt()) {
cmd.cmd = root["cmd_uint8"].asUInt();
} else {
LOG_F(WARNING, "缺少或类型不匹配: state_bool");
}
return cmd;
};
};
#endif
@@ -1,7 +0,0 @@
#ifndef UEXTERNCOMM_SETDEVICESWITCH_CMD_H
#define UEXTERNCOMM_SETDEVICESWITCH_CMD_H
#include <string>
#endif
@@ -0,0 +1,46 @@
#ifndef UEXTERCOMM_SETOPMODE_CMD_H
#define UEXTERCOMM_SETOPMODE_CMD_H
#include <string>
#include "../loguru.hpp"
//外部通信消息
#define DB_TO_PM_SETOPMODE "uExternComm_setOpMode_cmd"
class uExternComm_setOpModeMsg
{
private:
/* data */
public:
std::string key = "uExternComm_setOpMode_cmd";
uExternComm_setOpModeMsg(/* args */){};
~uExternComm_setOpModeMsg(){};
int getSetOpMode(const std::string& msgkey, const std::string& msg){
if (msgkey!= key)
{
LOG_F(WARNING, "消息类型错误 %s", msgkey.c_str());
return 0;
}
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
LOG_F(WARNING, "JSON解析失败: %s", errs.c_str());
return -1;
}
// 检查并读取操作模式
if (root.isMember("state_uint8") && root["state_uint8"].isUInt()) {
return root["state_uint8"].asUInt();
} else {
LOG_F(WARNING, "缺少或类型不匹配: opMode");
return -1;
}
};
};
#endif
@@ -1,2 +0,0 @@
//外部通信消息
#define DB_TO_PM_SETOPMODE "uExternComm_setOpMode_cmd"
@@ -0,0 +1,39 @@
#ifndef UEXTERNCONM_SETPMSYS_H
#define UEXTERNCONM_SETPMSYS_H
#include <string>
#include "../loguru.hpp"
#define DB_TO_PM_SETPMSYS "uExternComm_setPowerSystem_cmd"
class uExternComm_setPowerSystemMsg
{
private:
public:
std::string key = "uExternComm_setPowerSystem_cmd";
uExternComm_setPowerSystemMsg(/* args */){};
~uExternComm_setPowerSystemMsg(){};
int getSetPMSys(const std::string& key, const std::string& msg)
{
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
std::cerr << "JSON解析失败: " << errs << std::endl;
return -1;
}
// 检查并读取电源系统设置
if (root.isMember("powerCmd_uint8") && root["powerCmd_uint8"].isUInt()) {
return root["powerCmd_uint8"].asUInt();
} else {
LOG_F(WARNING, "缺少或类型不匹配: powerCmd_uint8");
return -1;
}
}
};
#endif
@@ -1,2 +0,0 @@
//外部通信消息
#define DB_TO_PM_SETPMSYS "uExternComm_setPowerSystem_cmd"
+35
View File
@@ -0,0 +1,35 @@
#ifndef UPLAN_PLANSTOP_CMD_H
#define UPLAN_PLANSTOP_CMD_H
#include <string>
#include "../loguru.hpp"
#define DB_TO_PM_TASKSTOP "uPlan_planStop_cmd"
class uPlan_planStopMsg
{
private:
/* data */
public:
std::string key = "uPlan_planStop_cmd";
uPlan_planStopMsg(/* args */){};
~uPlan_planStopMsg(){};
bool getTaskStop(const std::string& key, const std::string& msg) {
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
LOG_F(WARNING, "JSON解析失败: %s", errs.c_str());
return false;
}
// 检查op_int32字段
if (root.isMember("op_int32") && root["op_int32"].isInt()) {
return root["op_int32"].asInt() == 1;
}
return false;
};
};
#endif
@@ -1,2 +0,0 @@
//决策消息
#define DB_TO_PM_TASKSTOP "uPlan_planStop_cmd"
+55
View File
@@ -0,0 +1,55 @@
#ifndef UPPERCOMMMANAGER_H
#define UPPERCOMMMANAGER_H
#include <string>
#include "../pmSysvariable.h"
#include "../loguru.hpp"
//决策消息
#define DB_TO_PM_MISSION "uPlan_taskStart_cmd"
class uPlan_taskStartMsg
{
private:
/* data */
public:
std::string key = "uPlan_taskStart_cmd";
uPlan_taskStartMsg(/* args */){};
~uPlan_taskStartMsg(){};
MissionState getMission(const std::string& key, const std::string& msg) {
MissionState mission;
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
std::cerr << "JSON解析失败: " << errs << std::endl;
LOG_F(WARNING, "JSON解析失败: %s", errs.c_str());
return mission;
}
// 提取taskType_str
if (root.isMember("taskType_str") && root["taskType_str"].isString()) {
mission.type = TaskType::fromString(root["taskType_str"].asString());
LOG_F(INFO, "taskType: %d", mission.type, root["taskType_str"].asString());
}
// 提取balanceEnabled_bool
if (root.isMember("balanceEnabled_bool") && root["balanceEnabled_bool"].isBool()) {
mission.enableBuoyancyAdjustment = root["balanceEnabled_bool"].asBool();
LOG_F(INFO, "balanceEnabled_bool: %d", mission.enableBuoyancyAdjustment);
}
// 提取bowRudderUnfold_bool
if (root.isMember("bowRudderUnfold_bool") && root["bowRudderUnfold_bool"].isBool()) {
mission.enableBowRudder = root["bowRudderUnfold_bool"].asBool();
LOG_F(INFO, "bowRudderUnfold_bool: %d", mission.enableBowRudder);
}
return mission;
};
};
#endif
@@ -1,2 +0,0 @@
//决策消息
#define DB_TO_PM_MISSION "uPlan_taskStart_cmd"
+11
View File
@@ -14,6 +14,7 @@ public:
};
~uPower_equStateMsg() {};
std::string key = "uPower_equState_fb";
std::string key_cmd = "uPower_equState_cmd";
Driver driver;
@@ -21,6 +22,16 @@ public:
{
return driver.saveDriverTableToJson(&s);
}
const DriverTable getEquipmentCmd(const std::string& key, const std::string& msg)
{
DriverTable cmd;
if (driver.loadDriverTableFromJson(msg, cmd) < 0)
{
LOG_F(WARNING, "get equipment cmd error:%s", msg.c_str());
}
return cmd;
}
};
#endif
@@ -1,2 +0,0 @@
//控制消息
#define DB_TO_PM_EQUIPMENTCMD "uPower_equState_cmd"
+75
View File
@@ -0,0 +1,75 @@
#ifndef U_POWER_SYSSTATE_FB_H
#define U_POWER_SYSSTATE_FB_H
#include "../driver.h"
#include "string"
#include "json/json.h"
#include <bitset>
#include "../loguru.hpp"
class uPower_sysStateMsg
{
private:
/* data */
public:
uPower_sysStateMsg() {
};
~uPower_sysStateMsg() {};
std::string key = "uPower_sysState_cmd";
const MSControlCmd getMasterControlCmd(const std::string& key, const std::string& msg)
{
MSControlCmd cmd;
if (parseControlCmdFromJson(cmd, msg)) {
return cmd;
}
else
{
LOG_F(WARNING, "get master control cmd error:%s", msg.c_str());
}
};
bool parseControlCmdFromJson(MSControlCmd &cmd, const std::string &strJson)
{
Json::CharReaderBuilder builder;
Json::Value root;
std::string errs;
// 解析JSON字符串
std::istringstream s(strJson);
if (!Json::parseFromStream(builder, s, &root, &errs))
{
std::cerr << "JSON解析失败: " << errs << std::endl;
return false; // 解析失败
}
if (root.isMember("powerCMD") && root["powerCMD"].isUInt())
{
cmd.powerCMD = root["powerCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: powerCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("modCMD") && root["modCMD"].isUInt())
{
cmd.modCMD = root["modCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: modCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("workCMD") && root["workCMD"].isUInt())
{
cmd.workCMD = root["workCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: workCMD" << std::endl;
return false; // 缺少或类型不匹配
}
return true; // 成功
};
};
#endif
@@ -1,2 +0,0 @@
//控制消息
#define DB_TO_PM_MSCMD "uPower_sysState_cmd"