新增复合管控器(pCCU) + 惯导数据解析 + 设备控制页增强 + 0824协议文档归档
- 新增 pCCU 复合管控器程序(src/pCCU):FC/PM 双链路(UDP)、协议编解码器 (FcProtocol/PmProtocol/Frame/Message/MessageRegistry)、快照与落库(DbStore)、 内置 WebServer;挂入 src/CMakeLists.txt 与板卡 mission(h100.moos) - 惯导/深度数据:新增 uDevice_insData 消息解析,接入 UpperCommManager, pPowerMangerHost 注册监视,UpmsgTest 覆盖 - pPowerMangerHost 设备控制页增强(web/index.html) - 部署/运维脚本改为 4 个 systemd 服务(moosdb/pPowerManger/pPowerMangerHost/pCCU): deploy/clean-data/fetch-data 同步适配 - 协议文档:归档 0824 控制主机与复合管控器通信协议 xlsx 及关联 docx,移除旧 md - 测试:新增 pccu 单元/集成测试与 disSysTest Python 配电仿真 - 清理:移除未使用的 CCU 状态调试打印 coutMsg;.gitignore 补充编译产物/临时文件忽略
This commit is contained in:
@@ -20,6 +20,7 @@ ADD_SUBDIRECTORY(pPowerManger)
|
||||
# ADD_SUBDIRECTORY(appPowerMangerWebUI)
|
||||
add_subdirectory(pPMtest)
|
||||
add_subdirectory(pPowerMangerHost)
|
||||
add_subdirectory(pCCU)
|
||||
##############################################################################
|
||||
# END of CMakeLists.txt
|
||||
##############################################################################
|
||||
|
||||
@@ -0,0 +1,409 @@
|
||||
#include "CCU.h"
|
||||
#include "MBUtils.h"
|
||||
#include "../pPowerManger/logc/loguru.hpp"
|
||||
#include "protocol/Frame.h"
|
||||
#include <chrono>
|
||||
|
||||
using namespace ccu;
|
||||
using namespace std;
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Constructor / Destructor
|
||||
|
||||
CCU::CCU() {}
|
||||
|
||||
CCU::~CCU() {
|
||||
if (m_web) { m_web->stop(); delete m_web; m_web = nullptr; }
|
||||
if (m_fcLink) { m_fcLink->stop(); delete m_fcLink; m_fcLink = nullptr; }
|
||||
if (m_pmLink) { m_pmLink->stop(); delete m_pmLink; m_pmLink = nullptr; }
|
||||
if (m_db) { delete m_db; m_db = nullptr; }
|
||||
if (m_snap) { delete m_snap; m_snap = nullptr; }
|
||||
if (m_sysData) { delete m_sysData; m_sysData = nullptr; }
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// OnStartUp:读取配置并初始化各组件
|
||||
|
||||
bool CCU::OnStartUp() {
|
||||
AppCastingMOOSApp::OnStartUp();
|
||||
|
||||
STRING_LIST sParams;
|
||||
m_MissionReader.EnableVerbatimQuoting(false);
|
||||
if (!m_MissionReader.GetConfiguration(GetAppName(), sParams))
|
||||
reportConfigWarning("No config block found for " + GetAppName());
|
||||
|
||||
STRING_LIST::iterator p;
|
||||
for (p = sParams.begin(); p != sParams.end(); p++) {
|
||||
string orig = *p;
|
||||
string line = *p;
|
||||
string param = stripBlankEnds(tolower(biteStringX(line, '=')));
|
||||
string value = stripBlankEnds(line);
|
||||
|
||||
bool handled = true;
|
||||
if (param == "fc_local_port") m_fcLocalPort = atol(value.c_str());
|
||||
else if (param == "fc_remote_ip") m_fcHost = value;
|
||||
else if (param == "fc_remote_port") m_fcPort = atol(value.c_str());
|
||||
else if (param == "pm_local_port") m_pmLocalPort = atol(value.c_str());
|
||||
else if (param == "pm_remote_ip") m_pmHost = value;
|
||||
else if (param == "pm_remote_port") m_pmPort = atol(value.c_str());
|
||||
else if (param == "dbpath") m_dbPath = value;
|
||||
else if (param == "logpath") m_logPath = value;
|
||||
else if (param == "web_port") m_webPort = atoi(value.c_str());
|
||||
else if (param == "web_enable") {
|
||||
m_webEnable = (tolower(value) == "true" || value == "1");
|
||||
}
|
||||
else handled = false;
|
||||
|
||||
if (!handled)
|
||||
reportUnhandledConfigWarning(orig);
|
||||
}
|
||||
|
||||
registerVariables();
|
||||
|
||||
// 日志文件
|
||||
if (m_logPath.empty()) m_logPath = "pCCU.log";
|
||||
loguru::add_file(m_logPath.c_str(), loguru::Append, loguru::Verbosity_MAX);
|
||||
LOG_F(INFO, "pCCU log path: %s", m_logPath.c_str());
|
||||
|
||||
// 数据库
|
||||
m_db = new DbStore(m_dbPath);
|
||||
if (!m_db->open()) {
|
||||
LOG_F(ERROR, "open database failed: %s", m_db->lastError().c_str());
|
||||
delete m_db;
|
||||
m_db = nullptr;
|
||||
} else {
|
||||
LOG_F(INFO, "database path: %s", m_dbPath.c_str());
|
||||
}
|
||||
|
||||
// 系统数据快照
|
||||
m_sysData = new SystemData();
|
||||
|
||||
// FC 链路
|
||||
m_fcLink = new FcLinkManager(m_fcLocalPort, m_fcHost, m_fcPort);
|
||||
m_fcLink->setLogSink(m_db);
|
||||
m_fcLink->setOnMessage([this](Message* msg, const std::vector<uint8_t>& frame) {
|
||||
handleFcMessage(msg, frame);
|
||||
});
|
||||
m_fcLink->setOnRawFrame([](int dir, const std::vector<uint8_t>&, bool) {
|
||||
// 原始帧已通过 LogSink 落库;如需 Web 实时原始报文可在此扩展
|
||||
});
|
||||
if (!m_fcLink->start())
|
||||
LOG_F(ERROR, "FC link start failed");
|
||||
|
||||
// PM 链路
|
||||
m_pmLink = new PmLinkManager(m_pmLocalPort, m_pmHost, m_pmPort);
|
||||
m_pmLink->setLogSink(m_db);
|
||||
m_pmLink->setOnMessage([this](Message* msg, const std::vector<uint8_t>& frame) {
|
||||
handlePmMessage(msg, frame);
|
||||
});
|
||||
m_pmLink->setOnRawFrame([](int dir, const std::vector<uint8_t>&, bool) {
|
||||
});
|
||||
if (!m_pmLink->start())
|
||||
LOG_F(ERROR, "PM link start failed");
|
||||
|
||||
// 快照构建器(链路就绪后创建)
|
||||
m_snap = new SnapshotBuilder(m_sysData, m_fcLink, m_pmLink, m_db);
|
||||
|
||||
// 网页
|
||||
if (m_webEnable) {
|
||||
m_web = new WebServer();
|
||||
m_web->setOnOpen([this]() { return buildSnapshot(); });
|
||||
m_web->setApiHandler([this](const std::string& uri, const std::string& query) {
|
||||
return handleApi(uri, query);
|
||||
});
|
||||
if (!m_web->start(m_webPort)) {
|
||||
LOG_F(ERROR, "web server start failed on port %d", m_webPort);
|
||||
delete m_web;
|
||||
m_web = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_F(INFO, "pCCU started: fc_link local=%ld -> %s:%ld, pm_link local=%ld -> %s:%ld",
|
||||
m_fcLocalPort, m_fcHost.c_str(), m_fcPort,
|
||||
m_pmLocalPort, m_pmHost.c_str(), m_pmPort);
|
||||
return true;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// OnConnectToServer
|
||||
|
||||
bool CCU::OnConnectToServer() {
|
||||
registerVariables();
|
||||
return true;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// registerVariables
|
||||
|
||||
void CCU::registerVariables() {
|
||||
AppCastingMOOSApp::RegisterVariables();
|
||||
// pCCU 数据交换主要走 UDP;MOOSDB 仅登记运行状态变量
|
||||
Register("CCU_FC_LINK_STATE", 0);
|
||||
Register("CCU_PM_LINK_STATE", 0);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// OnNewMail
|
||||
|
||||
bool CCU::OnNewMail(MOOSMSG_LIST &NewMail) {
|
||||
AppCastingMOOSApp::OnNewMail(NewMail);
|
||||
MOOSMSG_LIST::iterator p;
|
||||
for (p = NewMail.begin(); p != NewMail.end(); p++) {
|
||||
CMOOSMsg &msg = *p;
|
||||
// 预留:未来如需通过 MOOSDB 下发指令可在此处理
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Iterate:周期任务(AppTick 次/秒)
|
||||
|
||||
bool CCU::Iterate() {
|
||||
AppCastingMOOSApp::Iterate();
|
||||
|
||||
double now = MOOSTime();
|
||||
|
||||
// 周期(1Hz)整合 FC 状态并发送 PM 状态报文给控制主机
|
||||
if (now - m_lastStatusTx >= 1.0) {
|
||||
sendPmStatus();
|
||||
m_lastStatusTx = now;
|
||||
}
|
||||
|
||||
// 周期(1Hz)推送网页快照
|
||||
if (m_web) {
|
||||
static double lastWebPush = 0;
|
||||
if (now - lastWebPush >= 1.0) {
|
||||
lastWebPush = now;
|
||||
m_web->broadcast(buildSnapshot());
|
||||
}
|
||||
}
|
||||
|
||||
AppCastingMOOSApp::PostReport();
|
||||
return true;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// handleFcMessage:处理 FC 链路收到的消息
|
||||
|
||||
void CCU::handleFcMessage(Message* msg, const std::vector<uint8_t>& frame) {
|
||||
if (!msg) return;
|
||||
switch (msg->id()) {
|
||||
case 0x0002: { // FC 状态反馈
|
||||
FcStatusValue v;
|
||||
if (msg->decode(frame, static_cast<void*>(&v))) {
|
||||
m_sysData->updateFcStatus(v);
|
||||
LOG_F(INFO, "[FC] status: mode=%d status=%d fault_level=%d",
|
||||
v.fc_mode, v.fc_status, v.fc_fault_level);
|
||||
} else {
|
||||
LOG_F(ERROR, "[FC] status decode failed");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LOG_F(WARNING, "[FC] unhandled msg id 0x%04X", msg->id());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// handlePmMessage:处理 PM 链路收到的消息
|
||||
|
||||
void CCU::handlePmMessage(Message* msg, const std::vector<uint8_t>& frame) {
|
||||
if (!msg) return;
|
||||
switch (msg->id()) {
|
||||
case 0x0001: { // PM 操控指令 -> 转发给 FC
|
||||
PmControlValue c;
|
||||
if (msg->decode(frame, static_cast<void*>(&c))) {
|
||||
m_sysData->updatePmControl(c);
|
||||
LOG_F(INFO, "[PM] control: mode=%d cmd=%d power=%d",
|
||||
c.mode, c.cmd, c.outputPower);
|
||||
|
||||
// 映射并转发到 FC
|
||||
FcControlValue f;
|
||||
f.mode = c.mode;
|
||||
f.cmd = c.cmd;
|
||||
f.outputPower = c.outputPower;
|
||||
f.pitch = c.pitch;
|
||||
f.roll = c.roll;
|
||||
f.emergencyAllow = c.emergencyAllow;
|
||||
f.depth = c.depth;
|
||||
f.supplyCmd = c.supplyCmd;
|
||||
f.reservedCmd5 = c.reservedCmd5;
|
||||
f.reservedCmd6 = c.reservedCmd6;
|
||||
f.heartbeat = c.heartbeat;
|
||||
m_sysData->updateFcControl(f);
|
||||
m_fcLink->sendMessage(0x0001, &f);
|
||||
LOG_F(INFO, "[FC] forward control cmd=%d power=%d", f.cmd, f.outputPower);
|
||||
} else {
|
||||
LOG_F(ERROR, "[PM] control decode failed");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x0002: { // PM 参数设定 -> 回参数反馈
|
||||
PmParamSetValue p;
|
||||
if (msg->decode(frame, static_cast<void*>(&p))) {
|
||||
m_sysData->updatePmParamSet(p);
|
||||
LOG_F(INFO, "[PM] param set received");
|
||||
// 当前阶段直接回成功
|
||||
PmParamSetFbValue fb;
|
||||
fb.flag = 0x10; // 设定成功
|
||||
fb.failCode = 0;
|
||||
m_sysData->updatePmParamFb(fb);
|
||||
m_pmLink->sendMessage(0x0003, &fb);
|
||||
LOG_F(INFO, "[PM] param set feedback sent");
|
||||
} else {
|
||||
LOG_F(ERROR, "[PM] param set decode failed");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LOG_F(WARNING, "[PM] unhandled msg id 0x%04X", msg->id());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// sendPmStatus:整合最新 FC 状态,编码 PM 状态报文发送
|
||||
|
||||
void CCU::sendPmStatus() {
|
||||
if (!m_pmLink || !m_sysData) return;
|
||||
|
||||
const FcStatusValue& fc = m_sysData->fcStatus();
|
||||
|
||||
PmStatusValue s;
|
||||
s.fc_mode = fc.fc_mode;
|
||||
s.fc_status = fc.fc_status;
|
||||
s.fault_level_1 = fc.fault_level_1;
|
||||
s.fault_level_2 = fc.fault_level_2;
|
||||
s.fault_level_3 = fc.fault_level_3;
|
||||
s.fault_level_4 = fc.fault_level_4;
|
||||
s.total_generation_time = fc.total_generation_time;
|
||||
s.fc_fault_level = fc.fc_fault_level;
|
||||
s.output_power_limit = 0; // FC 状态未含此字段,置 0
|
||||
s.generation_power = fc.generation_power;
|
||||
s.hydrogen_capacity = fc.hydrogen_capacity;
|
||||
s.liquid_oxygen_capacity = fc.liquid_oxygen_capacity;
|
||||
|
||||
s.fc1_min_cell_voltage = fc.fc1_min_cell_voltage;
|
||||
s.fc1_min_cell_pos = fc.fc1_min_cell_pos;
|
||||
s.fc1_avg_cell_voltage = fc.fc1_avg_cell_voltage;
|
||||
s.fc2_min_cell_voltage = fc.fc2_min_cell_voltage;
|
||||
s.fc2_min_cell_pos = fc.fc2_min_cell_pos;
|
||||
s.fc2_avg_cell_voltage = fc.fc2_avg_cell_voltage;
|
||||
|
||||
s.palladium_temp = fc.palladium_temp;
|
||||
s.buffer_tank_pressure = fc.buffer_tank_pressure;
|
||||
s.flue_total_emission = fc.flue_total_emission;
|
||||
s.flue_pressure = fc.flue_pressure;
|
||||
s.reactor_pressure = fc.reactor_pressure;
|
||||
s.electric_valve_open = fc.electric_valve_open;
|
||||
s.main_pipe_pressure = 0;
|
||||
s.aux_pipe_pressure = 0;
|
||||
|
||||
s.dcdc1_in_voltage = fc.dcdc1_in_voltage;
|
||||
s.dcdc1_in_current = fc.dcdc1_in_current;
|
||||
s.dcdc2_in_voltage = fc.dcdc2_in_voltage;
|
||||
s.dcdc2_in_current = fc.dcdc2_in_current;
|
||||
s.dcdc_out_voltage = fc.dcdc_out_voltage;
|
||||
s.dcdc_out_current = fc.dcdc_out_current;
|
||||
s.dcdc_ctrl_voltage = fc.dcdc_ctrl_voltage;
|
||||
s.dcdc_aux_voltage = fc.dcdc_aux_voltage;
|
||||
|
||||
s.methanol_total_use = fc.methanol_total_use;
|
||||
s.methanol_feed = fc.methanol_feed;
|
||||
s.oxygen_side_water_level = fc.oxygen_side_water_level;
|
||||
s.hydrogen_side_water_level = fc.hydrogen_side_water_level;
|
||||
s.ballast_water_level = fc.ballast_water_level;
|
||||
|
||||
s.exhaust_inlet_pressure = fc.exhaust_inlet_pressure;
|
||||
s.exhaust_outlet_pressure = fc.exhaust_outlet_pressure;
|
||||
s.exhaust_run_freq = fc.exhaust_run_freq;
|
||||
s.exhaust_inlet_temp = fc.exhaust_inlet_temp;
|
||||
s.exhaust_outlet_temp = fc.exhaust_outlet_temp;
|
||||
s.exhaust_water_in_pressure = fc.exhaust_water_in_pressure;
|
||||
s.exhaust_water_out_pressure = fc.exhaust_water_out_pressure;
|
||||
|
||||
s.tank_lo2_pressure = fc.tank_lo2_pressure;
|
||||
s.tank_co2_pressure = fc.tank_co2_pressure;
|
||||
s.tank_lo2_level = fc.tank_lo2_level;
|
||||
s.alloy_h2_flow = fc.alloy_h2_flow;
|
||||
s.fc_h2_flow = fc.fc_h2_flow;
|
||||
s.fc_o2_flow = fc.fc_o2_flow;
|
||||
|
||||
s.emergency_float_depth = fc.emergency_float_depth;
|
||||
s.emergency_float_time = fc.emergency_float_time;
|
||||
|
||||
s.cabin_pressure1 = fc.cabin_pressure1;
|
||||
s.cabin_pressure2 = fc.cabin_pressure2;
|
||||
s.cabin_temp1 = fc.cabin_temp1;
|
||||
s.cabin_temp2 = fc.cabin_temp2;
|
||||
s.cabin_humidity1 = fc.cabin_humidity1;
|
||||
s.cabin_humidity2 = fc.cabin_humidity2;
|
||||
s.h2_concentration1 = fc.h2_concentration1;
|
||||
s.h2_concentration2 = fc.h2_concentration2;
|
||||
s.h2_concentration3 = fc.h2_concentration3;
|
||||
s.o2_concentration1 = fc.o2_concentration1;
|
||||
s.o2_concentration2 = fc.o2_concentration2;
|
||||
s.ch3oh_concentration1 = fc.ch3oh_concentration1;
|
||||
s.ch3oh_concentration2 = fc.ch3oh_concentration2;
|
||||
|
||||
s.flame_detector1 = fc.flame_detector1;
|
||||
s.flame_detector2 = fc.flame_detector2;
|
||||
|
||||
// 通信心跳自增
|
||||
s.heartbeat = m_pmHeartbeat++;
|
||||
s.emergency_cmd = fc.emergency_cmd;
|
||||
|
||||
m_sysData->updatePmStatus(s);
|
||||
m_pmLink->sendMessage(0x0004, &s);
|
||||
LOG_F(INFO, "[PM] status sent: mode=%d status=%d heartbeat=%d",
|
||||
s.fc_mode, s.fc_status, s.heartbeat);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// buildSnapshot
|
||||
|
||||
std::string CCU::buildSnapshot() {
|
||||
if (m_snap) return m_snap->build();
|
||||
return "{}";
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// handleApi:/api/logs
|
||||
|
||||
std::string CCU::handleApi(const std::string& uri, const std::string& query) {
|
||||
if (uri == "/api/logs") {
|
||||
if (m_snap) return m_snap->buildLogs(50);
|
||||
return "[]";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// buildReport
|
||||
|
||||
bool CCU::buildReport() {
|
||||
m_msgs << "============================================" << "\n";
|
||||
m_msgs << "pCCU 复合管控器" << "\n";
|
||||
m_msgs << "============================================" << "\n";
|
||||
if (m_fcLink) {
|
||||
m_msgs << "FC 链路 local:" << m_fcLink->localPort()
|
||||
<< " rx:" << m_fcLink->rxCount()
|
||||
<< " tx:" << m_fcLink->txCount()
|
||||
<< " err:" << m_fcLink->errorCount() << "\n";
|
||||
}
|
||||
if (m_pmLink) {
|
||||
m_msgs << "PM 链路 local:" << m_pmLink->localPort()
|
||||
<< " rx:" << m_pmLink->rxCount()
|
||||
<< " tx:" << m_pmLink->txCount()
|
||||
<< " err:" << m_pmLink->errorCount() << "\n";
|
||||
}
|
||||
if (m_sysData) {
|
||||
m_msgs << "FC 状态接收次数:" << m_sysData->fcStatusCount() << "\n";
|
||||
m_msgs << "PM 指令接收次数:" << m_sysData->pmControlCount() << "\n";
|
||||
}
|
||||
if (m_db) {
|
||||
m_msgs << "数据库记录数:" << m_db->count() << "\n";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef PCCU_CCU_H
|
||||
#define PCCU_CCU_H
|
||||
|
||||
#define UNIX
|
||||
#include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h"
|
||||
#include "comm/FcLinkManager.h"
|
||||
#include "comm/PmLinkManager.h"
|
||||
#include "core/SystemData.h"
|
||||
#include "core/SnapshotBuilder.h"
|
||||
#include "store/DbStore.h"
|
||||
#include "web/WebServer.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// CCU:pCCU 程序主类(MOOS 应用外壳)。
|
||||
//
|
||||
// 数据流:
|
||||
// 收 FC 状态(0x0002) -> SystemData 快照 -> 周期整合为 PM 状态(0x0004)发给 pPowerManger
|
||||
// 收 PM 操控(0x0001) -> 转发为 FC 控制(0x0001)发给燃料电池
|
||||
// 收 PM 参数设定(0x0002) -> 回 PM 参数反馈(0x0003)
|
||||
// 收发全部帧落库 SQLite;网页周期推送 JSON 快照。
|
||||
//============================================================================
|
||||
|
||||
class CCU : public AppCastingMOOSApp {
|
||||
public:
|
||||
CCU();
|
||||
~CCU();
|
||||
|
||||
protected:
|
||||
bool OnNewMail(MOOSMSG_LIST &NewMail);
|
||||
bool Iterate();
|
||||
bool OnConnectToServer();
|
||||
bool OnStartUp();
|
||||
bool buildReport();
|
||||
void registerVariables();
|
||||
|
||||
private:
|
||||
// 链路收帧处理
|
||||
void handleFcMessage(Message* msg, const std::vector<uint8_t>& frame);
|
||||
void handlePmMessage(Message* msg, const std::vector<uint8_t>& frame);
|
||||
|
||||
// 整合并发送 PM 状态报文
|
||||
void sendPmStatus();
|
||||
|
||||
// 构建网页快照
|
||||
std::string buildSnapshot();
|
||||
std::string handleApi(const std::string& uri, const std::string& query);
|
||||
|
||||
// 配置
|
||||
long m_fcLocalPort = 6000;
|
||||
std::string m_fcHost = "192.168.1.162";
|
||||
long m_fcPort = 7000;
|
||||
|
||||
long m_pmLocalPort = 7000;
|
||||
std::string m_pmHost = "192.168.0.140";
|
||||
long m_pmPort = 5001;
|
||||
|
||||
std::string m_dbPath = "pccu_data.db";
|
||||
std::string m_logPath = "pCCU.log";
|
||||
int m_webPort = 8080; // 与 pPowerManger(8090)/pPowerMangerHost(18080) 错开
|
||||
bool m_webEnable = true;
|
||||
|
||||
// 心跳
|
||||
uint8_t m_pmHeartbeat = 0;
|
||||
double m_lastStatusTx = 0;
|
||||
|
||||
// 组件
|
||||
FcLinkManager* m_fcLink = nullptr;
|
||||
PmLinkManager* m_pmLink = nullptr;
|
||||
SystemData* m_sysData = nullptr;
|
||||
DbStore* m_db = nullptr;
|
||||
SnapshotBuilder* m_snap = nullptr;
|
||||
WebServer* m_web = nullptr;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_CCU_H
|
||||
@@ -0,0 +1,102 @@
|
||||
/****************************************************************/
|
||||
/* NAME: CCU_Info */
|
||||
/* FILE: CCU_Info.cpp */
|
||||
/****************************************************************/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "CCU_Info.h"
|
||||
#include "ColorParse.h"
|
||||
#include "ReleaseInfo.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void showSynopsis() {
|
||||
blk("SYNOPSIS: ");
|
||||
blk("------------------------------------ ");
|
||||
blk(" The pCCU application is the composite controller (CCU) ");
|
||||
blk(" gateway between the fuel cell (FC) system and pPowerManger. ");
|
||||
blk(" It receives FC status, integrates & forwards to pPowerManger, ");
|
||||
blk(" forwards pPowerManger commands to FC, stores data in SQLite ");
|
||||
blk(" and provides a web monitoring page. ");
|
||||
blk(" ");
|
||||
}
|
||||
|
||||
void showHelpAndExit() {
|
||||
blk(" ");
|
||||
blu("=============================================================== ");
|
||||
blu("Usage: pCCU file.moos [OPTIONS] ");
|
||||
blu("=============================================================== ");
|
||||
blk(" ");
|
||||
showSynopsis();
|
||||
blk(" ");
|
||||
blk("Options: ");
|
||||
mag(" --alias","=<ProcessName> ");
|
||||
blk(" Launch pCCU with the given process name ");
|
||||
blk(" rather than pCCU. ");
|
||||
mag(" --example, -e ");
|
||||
blk(" Display example MOOS configuration block. ");
|
||||
mag(" --help, -h ");
|
||||
blk(" Display this help message. ");
|
||||
mag(" --interface, -i ");
|
||||
blk(" Display MOOS publications and subscriptions. ");
|
||||
mag(" --version,-v ");
|
||||
blk(" Display the release version of pCCU. ");
|
||||
blk(" ");
|
||||
blk("Note: If argv[2] does not otherwise match a known option, ");
|
||||
blk(" then it will be interpreted as a run alias. This is ");
|
||||
blk(" to support pAntler launching conventions. ");
|
||||
blk(" ");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void showExampleConfigAndExit() {
|
||||
blk(" ");
|
||||
blu("=============================================================== ");
|
||||
blu("pCCU Example MOOS Configuration ");
|
||||
blu("=============================================================== ");
|
||||
blk(" ");
|
||||
blk("ProcessConfig = pCCU ");
|
||||
blk("{ ");
|
||||
blk(" AppTick = 4 ");
|
||||
blk(" CommsTick = 4 ");
|
||||
blk(" ");
|
||||
blk(" fc_local_port = 6000 // FC 状态接收端口 ");
|
||||
blk(" fc_remote_ip = 192.168.1.162 // 燃料电池控制器 IP ");
|
||||
blk(" fc_remote_port = 7000 // FC 控制端口 ");
|
||||
blk(" pm_local_port = 7000 // PM 指令接收端口 ");
|
||||
blk(" pm_remote_ip = 192.168.0.140 // 控制主机 IP ");
|
||||
blk(" pm_remote_port = 5001 // 控制主机接收端口 ");
|
||||
blk(" dbpath = pccu_data.db // 数据库路径 ");
|
||||
blk(" logpath = pCCU.log // 日志路径 ");
|
||||
blk(" web_port = 8080 // 网页端口(避开8090/18080) ");
|
||||
blk(" web_enable = true // 是否启用网页 ");
|
||||
blk("} ");
|
||||
blk(" ");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void showInterfaceAndExit() {
|
||||
blk(" ");
|
||||
blu("=============================================================== ");
|
||||
blu("pCCU INTERFACE ");
|
||||
blu("=============================================================== ");
|
||||
blk(" ");
|
||||
showSynopsis();
|
||||
blk(" ");
|
||||
blk("SUBSCRIPTIONS: ");
|
||||
blk("------------------------------------ ");
|
||||
blk(" (数据交换主要通过 UDP 链路完成,MOOSDB 交互有限) ");
|
||||
blk(" ");
|
||||
blk("PUBLICATIONS: ");
|
||||
blk("------------------------------------ ");
|
||||
blk(" CCU_FC_LINK_STATE = 运行状态字符串 ");
|
||||
blk(" CCU_PM_LINK_STATE = 运行状态字符串 ");
|
||||
blk(" ");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void showReleaseInfoAndExit() {
|
||||
showReleaseInfo("pCCU", "gpl");
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/****************************************************************/
|
||||
/* NAME: CCU_Info */
|
||||
/* FILE: CCU_Info.h */
|
||||
/****************************************************************/
|
||||
|
||||
#ifndef PCCU_INFO_HEADER
|
||||
#define PCCU_INFO_HEADER
|
||||
|
||||
void showSynopsis();
|
||||
void showHelpAndExit();
|
||||
void showExampleConfigAndExit();
|
||||
void showInterfaceAndExit();
|
||||
void showReleaseInfoAndExit();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
1.接收来自燃料电池的消息,消息协议格式见docs/超滑FC和复合管控器通讯协议 - 0821.docx(备注:日期会随着时间进行更新)
|
||||
2.将消息进行整合处理后,发送到pPowerManger,消息协议见docs/控制主机与复合管控器通信协议- 0824.xlsx
|
||||
3.接收来自pPowermanger的指令,解析和编码后转发给燃料电池
|
||||
4.具备sqlit数据库存储功能,可以把接收和发送的数据都存储到sqlit数据库中
|
||||
5.各端口配置、IP配置、日志及数据库配置可以通过.moos文件进行配置
|
||||
6.具备网页显示功能,可以显示接收、发送的数据、各数据状态等,数据展示要按照类型进行归类,要直观、简介
|
||||
7.编译部署与仓库内其他程序一样,可以跟随整个仓库一块部署
|
||||
@@ -0,0 +1,76 @@
|
||||
#--------------------------------------------------------
|
||||
# The CMakeLists.txt for: pCCU
|
||||
# 复合管控器网关:FC <-> pPowerManger
|
||||
#--------------------------------------------------------
|
||||
|
||||
if (${WIN32})
|
||||
SET(SYSTEM_LIBS wsock32)
|
||||
else (${WIN32})
|
||||
SET(SYSTEM_LIBS m pthread)
|
||||
endif (${WIN32})
|
||||
|
||||
# 复用仓库内 pPowerManger 的公共源码(相对路径引用,避免重复维护)
|
||||
SET(PM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../pPowerManger)
|
||||
|
||||
SET(SHARED_SRC
|
||||
${PM_DIR}/logc/loguru.cpp
|
||||
${PM_DIR}/sqlit3/sqlite3.c
|
||||
${PM_DIR}/httpserver/mongoose.c
|
||||
)
|
||||
|
||||
SET(CCU_PROTOCOL_SRC
|
||||
protocol/Frame.cpp
|
||||
protocol/Message.cpp
|
||||
protocol/MessageRegistry.cpp
|
||||
protocol/FcProtocol.cpp
|
||||
protocol/PmProtocol.cpp
|
||||
)
|
||||
|
||||
SET(CCU_COMM_SRC
|
||||
comm/UdpEndpoint.cpp
|
||||
comm/LinkManager.cpp
|
||||
)
|
||||
|
||||
SET(CCU_CORE_SRC
|
||||
core/SnapshotBuilder.cpp
|
||||
)
|
||||
|
||||
SET(CCU_STORE_SRC
|
||||
store/DbStore.cpp
|
||||
)
|
||||
|
||||
SET(CCU_WEB_SRC
|
||||
web/WebServer.cpp
|
||||
)
|
||||
|
||||
SET(SRC
|
||||
${SHARED_SRC}
|
||||
${CCU_PROTOCOL_SRC}
|
||||
${CCU_COMM_SRC}
|
||||
${CCU_CORE_SRC}
|
||||
${CCU_STORE_SRC}
|
||||
${CCU_WEB_SRC}
|
||||
CCU.cpp
|
||||
CCU_Info.cpp
|
||||
main.cpp
|
||||
)
|
||||
|
||||
ADD_EXECUTABLE(pCCU ${SRC})
|
||||
|
||||
TARGET_INCLUDE_DIRECTORIES(pCCU PRIVATE
|
||||
${PM_DIR}
|
||||
${PM_DIR}/sqlit3
|
||||
${PM_DIR}/httpserver
|
||||
${PM_DIR}/logc
|
||||
)
|
||||
|
||||
TARGET_LINK_LIBRARIES(pCCU
|
||||
${MOOS_LIBRARIES}
|
||||
apputil
|
||||
mbutil
|
||||
m
|
||||
pthread
|
||||
jsoncpp
|
||||
dl
|
||||
${SYSTEM_LIBS}
|
||||
)
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef PCCU_FC_LINK_MANAGER_H
|
||||
#define PCCU_FC_LINK_MANAGER_H
|
||||
|
||||
#include "LinkManager.h"
|
||||
#include "../protocol/FcProtocol.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// FcLinkManager:面向燃料电池控制器的链路。
|
||||
//
|
||||
// - 本地监听:燃料电池状态反馈端口(默认 6000)
|
||||
// - 发送目标:燃料电池控制器(默认 192.168.1.162:7000)
|
||||
// 端口/地址均可由 .moos 配置覆盖。
|
||||
//============================================================================
|
||||
|
||||
class FcLinkManager : public LinkManager {
|
||||
public:
|
||||
FcLinkManager(long localPort,
|
||||
const std::string& fcHost, long fcPort)
|
||||
: LinkManager("fc", localPort), m_fcHost(fcHost), m_fcPort(fcPort) {
|
||||
registerFcMessages(registry());
|
||||
}
|
||||
|
||||
void setFcAddress(const std::string& host, long port) {
|
||||
m_fcHost = host; m_fcPort = port;
|
||||
}
|
||||
const std::string& fcHost() const { return m_fcHost; }
|
||||
long fcPort() const { return m_fcPort; }
|
||||
|
||||
protected:
|
||||
std::string defaultRemoteHost() const override { return m_fcHost; }
|
||||
long defaultRemotePort() const override { return m_fcPort; }
|
||||
|
||||
private:
|
||||
std::string m_fcHost;
|
||||
long m_fcPort;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_FC_LINK_MANAGER_H
|
||||
@@ -0,0 +1,108 @@
|
||||
#include "LinkManager.h"
|
||||
#include "../protocol/Frame.h"
|
||||
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
|
||||
#include "loguru.hpp"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
LinkManager::LinkManager(const std::string& linkName, long localPort)
|
||||
: m_linkName(linkName), m_localPort(localPort) {}
|
||||
|
||||
LinkManager::~LinkManager() {
|
||||
stop();
|
||||
}
|
||||
|
||||
bool LinkManager::start() {
|
||||
if (m_running) return true;
|
||||
if (!m_endpoint.init(m_localPort)) {
|
||||
LOG_F(ERROR, "[%s] bind failed on port %ld", m_linkName.c_str(), m_localPort);
|
||||
return false;
|
||||
}
|
||||
m_running = true;
|
||||
m_rxThread = std::thread([this]() { rxThreadFunc(); });
|
||||
LOG_F(INFO, "[%s] listening on local port %ld", m_linkName.c_str(), m_localPort);
|
||||
return true;
|
||||
}
|
||||
|
||||
void LinkManager::stop() {
|
||||
if (!m_running) return;
|
||||
m_running = false;
|
||||
if (m_rxThread.joinable()) m_rxThread.join();
|
||||
}
|
||||
|
||||
bool LinkManager::sendMessage(uint16_t id, const void* value) {
|
||||
return sendMessageTo(id, value, defaultRemoteHost(), defaultRemotePort());
|
||||
}
|
||||
|
||||
bool LinkManager::sendMessageTo(uint16_t id, const void* value,
|
||||
const std::string& host, long port) {
|
||||
if (host.empty() || port <= 0) return false;
|
||||
Message* msg = m_registry.find(id);
|
||||
if (!msg) {
|
||||
LOG_F(ERROR, "[%s] unknown message id 0x%04X", m_linkName.c_str(), id);
|
||||
return false;
|
||||
}
|
||||
std::vector<uint8_t> frame = msg->encode(value);
|
||||
if (!m_endpoint.sendTo(host, port, frame)) {
|
||||
LOG_F(ERROR, "[%s] send msg 0x%04X to %s:%ld failed",
|
||||
m_linkName.c_str(), id, host.c_str(), port);
|
||||
return false;
|
||||
}
|
||||
++m_txCount;
|
||||
if (m_logSink) m_logSink->onRawFrame(1, id, m_linkName, frame, true);
|
||||
if (m_onRawFrame) m_onRawFrame(1, frame, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkManager::handleIncoming(const std::vector<uint8_t>& frame) {
|
||||
if (frame.size() < FRAME_HEADER_LEN) return false;
|
||||
|
||||
uint16_t id = 0;
|
||||
if (!Frame::parseId(frame, id)) {
|
||||
LOG_F(WARNING, "[%s] bad frame header", m_linkName.c_str());
|
||||
++m_errorCount;
|
||||
return false;
|
||||
}
|
||||
|
||||
Message* msg = m_registry.find(id);
|
||||
if (!msg) {
|
||||
LOG_F(WARNING, "[%s] unknown msg id 0x%04X, len=%zu",
|
||||
m_linkName.c_str(), id, frame.size());
|
||||
++m_errorCount;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool checksumOk = msg->checksum().verify(frame);
|
||||
++m_rxCount;
|
||||
m_lastRxTime = MOOSTime(false);
|
||||
|
||||
// 原始帧落库 + 回调
|
||||
if (m_logSink) m_logSink->onRawFrame(0, id, m_linkName, frame, checksumOk);
|
||||
if (m_onRawFrame) m_onRawFrame(0, frame, checksumOk);
|
||||
|
||||
if (!checksumOk) {
|
||||
LOG_F(WARNING, "[%s] checksum error msg 0x%04X", m_linkName.c_str(), id);
|
||||
++m_errorCount;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解码交给上层(CCU 层知道具体消息类型)
|
||||
if (m_onMessage) m_onMessage(msg, frame);
|
||||
return true;
|
||||
}
|
||||
|
||||
void LinkManager::rxThreadFunc() {
|
||||
uint8_t buf[2048];
|
||||
while (m_running) {
|
||||
int n = m_endpoint.receive(buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
std::vector<uint8_t> frame(buf, buf + n);
|
||||
handleIncoming(frame);
|
||||
} else {
|
||||
// 无数据,短暂休眠避免忙等
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef PCCU_LINK_MANAGER_H
|
||||
#define PCCU_LINK_MANAGER_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include "UdpEndpoint.h"
|
||||
#include "../protocol/MessageRegistry.h"
|
||||
#include "../protocol/Message.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
// 落库接口:由 DbStore 实现,供链路层记录原始帧(实现收发数据入库)
|
||||
class ILinkLogSink {
|
||||
public:
|
||||
virtual ~ILinkLogSink() = default;
|
||||
// direction: 0=收 1=发;data 为原始完整帧;checksumOk 为校验结果
|
||||
virtual void onRawFrame(int direction, uint16_t msgId, const std::string& link,
|
||||
const std::vector<uint8_t>& data, bool checksumOk) = 0;
|
||||
};
|
||||
|
||||
//============================================================================
|
||||
// LinkManager:单条 UDP 链路的管理者。
|
||||
//
|
||||
// 职责:
|
||||
// - 绑定本地端口并维护独立接收线程
|
||||
// - 持有该链路的 MessageRegistry(收发共用,id 按链路隔离)
|
||||
// - 收帧:解析 id -> 查注册表 -> 校验 -> 回调 onMessage(携带消息对象与原始帧)
|
||||
// - 发帧:编码 -> 发送 -> 落库
|
||||
//
|
||||
// 派生类(FcLinkManager / PmLinkManager)仅需提供链路名与默认收发地址。
|
||||
//============================================================================
|
||||
|
||||
class LinkManager {
|
||||
public:
|
||||
LinkManager(const std::string& linkName, long localPort);
|
||||
virtual ~LinkManager();
|
||||
|
||||
// 启动:绑定端口 + 启动接收线程。返回是否成功。
|
||||
bool start();
|
||||
void stop();
|
||||
|
||||
bool isRunning() const { return m_running; }
|
||||
|
||||
// 注册表(收发共用)
|
||||
MessageRegistry& registry() { return m_registry; }
|
||||
const MessageRegistry& registry() const { return m_registry; }
|
||||
|
||||
// 收帧回调:msg 为注册表中命中的消息对象(可调用 decode 解码),frame 为原始完整帧
|
||||
void setOnMessage(std::function<void(Message* msg, const std::vector<uint8_t>& frame)> cb) {
|
||||
m_onMessage = std::move(cb);
|
||||
}
|
||||
|
||||
// 原始帧回调(用于 Web/调试展示原始报文)
|
||||
void setOnRawFrame(std::function<void(int direction, const std::vector<uint8_t>& frame, bool checksumOk)> cb) {
|
||||
m_onRawFrame = std::move(cb);
|
||||
}
|
||||
|
||||
// 落库接口(可空)
|
||||
void setLogSink(ILinkLogSink* sink) { m_logSink = sink; }
|
||||
|
||||
// 编码并发送一条已注册消息;返回是否成功
|
||||
bool sendMessage(uint16_t id, const void* value);
|
||||
|
||||
// 编码并发送一条已注册消息,同时指定目标地址(覆盖默认)
|
||||
bool sendMessageTo(uint16_t id, const void* value,
|
||||
const std::string& host, long port);
|
||||
|
||||
// 处理一帧收到的原始数据(供接收线程调用,也可测试时手动喂入)
|
||||
bool handleIncoming(const std::vector<uint8_t>& frame);
|
||||
|
||||
// 链路状态:最近一次收到消息的时间(MOOSTime),0 表示从未收到
|
||||
double lastRxTime() const { return m_lastRxTime; }
|
||||
long localPort() const { return m_localPort; }
|
||||
const std::string& linkName() const { return m_linkName; }
|
||||
|
||||
// 收发统计
|
||||
unsigned long rxCount() const { return m_rxCount; }
|
||||
unsigned long txCount() const { return m_txCount; }
|
||||
unsigned long errorCount() const { return m_errorCount; }
|
||||
|
||||
protected:
|
||||
// 派生类提供发送默认目标
|
||||
virtual std::string defaultRemoteHost() const { return ""; }
|
||||
virtual long defaultRemotePort() const { return 0; }
|
||||
|
||||
private:
|
||||
void rxThreadFunc();
|
||||
|
||||
std::string m_linkName;
|
||||
long m_localPort;
|
||||
UdpEndpoint m_endpoint;
|
||||
MessageRegistry m_registry;
|
||||
|
||||
std::atomic<bool> m_running{false};
|
||||
std::thread m_rxThread;
|
||||
|
||||
std::function<void(Message*, const std::vector<uint8_t>&)> m_onMessage;
|
||||
std::function<void(int, const std::vector<uint8_t>&, bool)> m_onRawFrame;
|
||||
ILinkLogSink* m_logSink = nullptr;
|
||||
|
||||
double m_lastRxTime = 0;
|
||||
unsigned long m_rxCount = 0;
|
||||
unsigned long m_txCount = 0;
|
||||
unsigned long m_errorCount = 0;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_LINK_MANAGER_H
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef PCCU_PM_LINK_MANAGER_H
|
||||
#define PCCU_PM_LINK_MANAGER_H
|
||||
|
||||
#include "LinkManager.h"
|
||||
#include "../protocol/PmProtocol.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// PmLinkManager:面向控制主机 pPowerManger 的链路。
|
||||
//
|
||||
// - 本地监听:控制主机指令端口(默认 7000)
|
||||
// - 发送目标:控制主机(默认 192.168.0.140:5001)
|
||||
// 端口/地址均可由 .moos 配置覆盖。
|
||||
//============================================================================
|
||||
|
||||
class PmLinkManager : public LinkManager {
|
||||
public:
|
||||
PmLinkManager(long localPort,
|
||||
const std::string& pmHost, long pmPort)
|
||||
: LinkManager("pm", localPort), m_pmHost(pmHost), m_pmPort(pmPort) {
|
||||
registerPmMessages(registry());
|
||||
}
|
||||
|
||||
void setPmAddress(const std::string& host, long port) {
|
||||
m_pmHost = host; m_pmPort = port;
|
||||
}
|
||||
const std::string& pmHost() const { return m_pmHost; }
|
||||
long pmPort() const { return m_pmPort; }
|
||||
|
||||
protected:
|
||||
std::string defaultRemoteHost() const override { return m_pmHost; }
|
||||
long defaultRemotePort() const override { return m_pmPort; }
|
||||
|
||||
private:
|
||||
std::string m_pmHost;
|
||||
long m_pmPort;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_PM_LINK_MANAGER_H
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "UdpEndpoint.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
UdpEndpoint::UdpEndpoint() : m_socket(nullptr), m_localPort(0) {}
|
||||
|
||||
UdpEndpoint::~UdpEndpoint() {
|
||||
if (m_socket) {
|
||||
delete m_socket;
|
||||
m_socket = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool UdpEndpoint::init(long localPort) {
|
||||
m_localPort = localPort;
|
||||
m_socket = new XPCUdpSocket(localPort);
|
||||
try {
|
||||
m_socket->vBindSocket();
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[UdpEndpoint] bind failed on port " << localPort
|
||||
<< ": " << e.what() << std::endl;
|
||||
delete m_socket;
|
||||
m_socket = nullptr;
|
||||
return false;
|
||||
} catch (...) {
|
||||
std::cerr << "[UdpEndpoint] bind failed on port " << localPort
|
||||
<< ": unknown error" << std::endl;
|
||||
delete m_socket;
|
||||
m_socket = nullptr;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UdpEndpoint::sendTo(const std::string& host, long port, const std::vector<uint8_t>& data) {
|
||||
if (!m_socket || data.empty()) return false;
|
||||
try {
|
||||
m_socket->iSendMessageTo(const_cast<uint8_t*>(data.data()),
|
||||
static_cast<int>(data.size()),
|
||||
port, host);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int UdpEndpoint::receive(uint8_t* buf, size_t len) {
|
||||
if (!m_socket || !buf || len == 0) return 0;
|
||||
try {
|
||||
return m_socket->iRecieveMessage(buf, static_cast<int>(len));
|
||||
} catch (...) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef PCCU_UDP_ENDPOINT_H
|
||||
#define PCCU_UDP_ENDPOINT_H
|
||||
|
||||
#define UNIX
|
||||
#include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// UdpEndpoint:单个 UDP 套接字的封装。
|
||||
//
|
||||
// 职责:
|
||||
// - 绑定本地端口
|
||||
// - 向指定远端 (host:port) 发送数据报
|
||||
// - 阻塞接收数据报
|
||||
// 不关心协议内容,仅负责字节的收发。
|
||||
//============================================================================
|
||||
|
||||
class UdpEndpoint {
|
||||
public:
|
||||
UdpEndpoint();
|
||||
~UdpEndpoint();
|
||||
|
||||
// 绑定本地端口;返回是否成功(端口占用/权限失败返回 false)
|
||||
bool init(long localPort);
|
||||
|
||||
// 向指定远端发送完整数据报
|
||||
bool sendTo(const std::string& host, long port, const std::vector<uint8_t>& data);
|
||||
|
||||
// 阻塞接收一帧;返回接收字节数,0 或负值表示无数据/错误
|
||||
int receive(uint8_t* buf, size_t len);
|
||||
|
||||
bool isOpen() const { return m_socket != nullptr; }
|
||||
long localPort() const { return m_localPort; }
|
||||
|
||||
private:
|
||||
XPCUdpSocket* m_socket = nullptr;
|
||||
long m_localPort = 0;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_UDP_ENDPOINT_H
|
||||
@@ -0,0 +1,380 @@
|
||||
#include "SnapshotBuilder.h"
|
||||
#include "../store/DbStore.h"
|
||||
#include "json/json.h"
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
using JsonVal = Json::Value;
|
||||
|
||||
namespace {
|
||||
|
||||
void put(JsonVal& j, const char* k, uint8_t v) { j[k] = JsonVal(v); }
|
||||
void put(JsonVal& j, const char* k, uint16_t v){ j[k] = JsonVal(v); }
|
||||
void put(JsonVal& j, const char* k, uint32_t v){ j[k] = JsonVal(static_cast<Json::UInt>(v)); }
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 消息描述:根据链路 + 消息 ID + 方向(0=收 1=发),给出中文名与"来源→去向"。
|
||||
// 方向是相对 pCCU 而言:收=对方→CCU,发=CCU→对方。
|
||||
//--------------------------------------------------------------------------
|
||||
std::string describeMessageName(const std::string& link, uint16_t id) {
|
||||
if (link == "fc") {
|
||||
switch (id) {
|
||||
case 0x0001: return "FC控制指令";
|
||||
case 0x0002: return "FC状态反馈";
|
||||
default: break;
|
||||
}
|
||||
} else if (link == "pm") {
|
||||
switch (id) {
|
||||
case 0x0001: return "PM操控指令";
|
||||
case 0x0002: return "PM参数设定";
|
||||
case 0x0003: return "PM参数反馈";
|
||||
case 0x0004: return "PM状态报文";
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "未知(0x%04X)", id);
|
||||
return buf;
|
||||
}
|
||||
|
||||
std::string describeDirection(const std::string& link, int direction) {
|
||||
const std::string peer = (link == "fc") ? "FC" : "PM";
|
||||
return (direction == 1) ? ("CCU→" + peer) : (peer + "→CCU");
|
||||
}
|
||||
|
||||
// 把数据库一行记录转成 JSON(含消息名与方向描述)
|
||||
JsonVal commLogToJson(const CommLogRow& r) {
|
||||
JsonVal e(Json::objectValue);
|
||||
e["time"] = JsonVal(r.time);
|
||||
e["link"] = r.link;
|
||||
e["direction"] = r.direction;
|
||||
e["msgId"] = r.msgId;
|
||||
e["checksumOk"] = r.checksumOk;
|
||||
e["hex"] = r.hex;
|
||||
e["name"] = describeMessageName(r.link, r.msgId);
|
||||
e["dirText"] = describeDirection(r.link, r.direction);
|
||||
return e;
|
||||
}
|
||||
|
||||
// 燃料电池状态 -> JSON
|
||||
JsonVal fcStatusJson(const FcStatusValue& v) {
|
||||
JsonVal j(Json::objectValue);
|
||||
put(j, "fc_mode", v.fc_mode);
|
||||
put(j, "fc_status", v.fc_status);
|
||||
put(j, "fault_level_1", v.fault_level_1);
|
||||
put(j, "fault_level_2", v.fault_level_2);
|
||||
put(j, "fault_level_3", v.fault_level_3);
|
||||
put(j, "fault_level_4", v.fault_level_4);
|
||||
put(j, "total_generation_time", v.total_generation_time);
|
||||
put(j, "fc_fault_level", v.fc_fault_level);
|
||||
put(j, "generation_power", v.generation_power);
|
||||
put(j, "hydrogen_capacity", v.hydrogen_capacity);
|
||||
put(j, "liquid_oxygen_capacity", v.liquid_oxygen_capacity);
|
||||
|
||||
put(j, "fc1_min_cell_voltage", v.fc1_min_cell_voltage);
|
||||
put(j, "fc1_min_cell_pos", v.fc1_min_cell_pos);
|
||||
put(j, "fc1_avg_cell_voltage", v.fc1_avg_cell_voltage);
|
||||
put(j, "fc2_min_cell_voltage", v.fc2_min_cell_voltage);
|
||||
put(j, "fc2_min_cell_pos", v.fc2_min_cell_pos);
|
||||
put(j, "fc2_avg_cell_voltage", v.fc2_avg_cell_voltage);
|
||||
|
||||
put(j, "palladium_temp", v.palladium_temp);
|
||||
put(j, "buffer_tank_pressure", v.buffer_tank_pressure);
|
||||
put(j, "flue_total_emission", v.flue_total_emission);
|
||||
put(j, "flue_pressure", v.flue_pressure);
|
||||
put(j, "reactor_pressure", v.reactor_pressure);
|
||||
put(j, "electric_valve_open", v.electric_valve_open);
|
||||
|
||||
put(j, "dcdc1_in_voltage", v.dcdc1_in_voltage);
|
||||
put(j, "dcdc1_in_current", v.dcdc1_in_current);
|
||||
put(j, "dcdc2_in_voltage", v.dcdc2_in_voltage);
|
||||
put(j, "dcdc2_in_current", v.dcdc2_in_current);
|
||||
put(j, "dcdc_out_voltage", v.dcdc_out_voltage);
|
||||
put(j, "dcdc_out_current", v.dcdc_out_current);
|
||||
put(j, "dcdc_ctrl_voltage", v.dcdc_ctrl_voltage);
|
||||
put(j, "dcdc_aux_voltage", v.dcdc_aux_voltage);
|
||||
|
||||
put(j, "methanol_total_use", v.methanol_total_use);
|
||||
put(j, "methanol_feed", v.methanol_feed);
|
||||
put(j, "oxygen_side_water_level", v.oxygen_side_water_level);
|
||||
put(j, "hydrogen_side_water_level", v.hydrogen_side_water_level);
|
||||
put(j, "ballast_water_level", v.ballast_water_level);
|
||||
|
||||
put(j, "exhaust_inlet_pressure", v.exhaust_inlet_pressure);
|
||||
put(j, "exhaust_outlet_pressure", v.exhaust_outlet_pressure);
|
||||
put(j, "cabin_pressure1", v.cabin_pressure1);
|
||||
put(j, "cabin_pressure2", v.cabin_pressure2);
|
||||
put(j, "cabin_temp1", v.cabin_temp1);
|
||||
put(j, "cabin_temp2", v.cabin_temp2);
|
||||
put(j, "cabin_humidity1", v.cabin_humidity1);
|
||||
put(j, "cabin_humidity2", v.cabin_humidity2);
|
||||
put(j, "h2_concentration1", v.h2_concentration1);
|
||||
put(j, "h2_concentration2", v.h2_concentration2);
|
||||
put(j, "h2_concentration3", v.h2_concentration3);
|
||||
put(j, "o2_concentration1", v.o2_concentration1);
|
||||
put(j, "o2_concentration2", v.o2_concentration2);
|
||||
put(j, "ch3oh_concentration1", v.ch3oh_concentration1);
|
||||
put(j, "ch3oh_concentration2", v.ch3oh_concentration2);
|
||||
|
||||
put(j, "flame_detector1", v.flame_detector1);
|
||||
put(j, "flame_detector2", v.flame_detector2);
|
||||
put(j, "emergency_float_depth", v.emergency_float_depth);
|
||||
put(j, "emergency_float_time", v.emergency_float_time);
|
||||
put(j, "exhaust_run_freq", v.exhaust_run_freq);
|
||||
put(j, "exhaust_inlet_temp", v.exhaust_inlet_temp);
|
||||
put(j, "exhaust_outlet_temp", v.exhaust_outlet_temp);
|
||||
put(j, "exhaust_water_in_pressure", v.exhaust_water_in_pressure);
|
||||
put(j, "exhaust_water_out_pressure", v.exhaust_water_out_pressure);
|
||||
put(j, "tank_lo2_pressure", v.tank_lo2_pressure);
|
||||
put(j, "tank_co2_pressure", v.tank_co2_pressure);
|
||||
put(j, "tank_lo2_level", v.tank_lo2_level);
|
||||
put(j, "alloy_h2_flow", v.alloy_h2_flow);
|
||||
put(j, "fc_h2_flow", v.fc_h2_flow);
|
||||
put(j, "fc_o2_flow", v.fc_o2_flow);
|
||||
put(j, "heartbeat", v.heartbeat);
|
||||
put(j, "emergency_cmd", v.emergency_cmd);
|
||||
return j;
|
||||
}
|
||||
|
||||
// PM 操控指令 -> JSON
|
||||
JsonVal pmControlJson(const PmControlValue& v) {
|
||||
JsonVal j(Json::objectValue);
|
||||
put(j, "year", v.year);
|
||||
put(j, "month", v.month);
|
||||
put(j, "day", v.day);
|
||||
put(j, "hour", v.hour);
|
||||
put(j, "minute", v.minute);
|
||||
put(j, "second", v.second);
|
||||
put(j, "millisecond10", v.millisecond10);
|
||||
put(j, "mode", v.mode);
|
||||
put(j, "cmd", v.cmd);
|
||||
put(j, "outputPower", v.outputPower);
|
||||
j["pitch"] = JsonVal(static_cast<int>(v.pitch)); // 有符号 int16,×0.1°
|
||||
j["roll"] = JsonVal(static_cast<int>(v.roll));
|
||||
put(j, "emergencyAllow", v.emergencyAllow);
|
||||
put(j, "depth", v.depth);
|
||||
put(j, "supplyCmd", v.supplyCmd);
|
||||
put(j, "reservedCmd5", v.reservedCmd5);
|
||||
put(j, "reservedCmd6", v.reservedCmd6);
|
||||
put(j, "insBatCmd", v.insBatCmd);
|
||||
put(j, "dynBatCmd", v.dynBatCmd);
|
||||
put(j, "dynBatPower", v.dynBatPower);
|
||||
put(j, "heartbeat", v.heartbeat);
|
||||
put(j, "hostState", v.hostState);
|
||||
return j;
|
||||
}
|
||||
|
||||
// FC 控制指令(转发给 FC)-> JSON
|
||||
JsonVal fcControlJson(const FcControlValue& v) {
|
||||
JsonVal j(Json::objectValue);
|
||||
put(j, "mode", v.mode);
|
||||
put(j, "cmd", v.cmd);
|
||||
put(j, "outputPower", v.outputPower);
|
||||
j["pitch"] = JsonVal(static_cast<int>(v.pitch)); // 有符号 int16,×0.1°
|
||||
j["roll"] = JsonVal(static_cast<int>(v.roll));
|
||||
put(j, "emergencyAllow", v.emergencyAllow);
|
||||
put(j, "depth", v.depth);
|
||||
put(j, "supplyCmd", v.supplyCmd);
|
||||
put(j, "reservedCmd5", v.reservedCmd5);
|
||||
put(j, "reservedCmd6", v.reservedCmd6);
|
||||
put(j, "heartbeat", v.heartbeat);
|
||||
return j;
|
||||
}
|
||||
|
||||
// PM 参数设定(收到)-> JSON
|
||||
JsonVal pmParamJson(const PmParamSetValue& v) {
|
||||
JsonVal j(Json::objectValue);
|
||||
put(j, "insSocHold1", v.insSocHold1);
|
||||
put(j, "insSocHold2", v.insSocHold2);
|
||||
put(j, "insSocHold3", v.insSocHold3);
|
||||
put(j, "insOutputPower", v.insOutputPower);
|
||||
put(j, "dynSocHold1", v.dynSocHold1);
|
||||
put(j, "dynSocHold2", v.dynSocHold2);
|
||||
put(j, "dynSocHold3", v.dynSocHold3);
|
||||
put(j, "dynOutputPower", v.dynOutputPower);
|
||||
return j;
|
||||
}
|
||||
|
||||
// PM 参数反馈(发送)-> JSON
|
||||
JsonVal pmFbJson(const PmParamSetFbValue& v) {
|
||||
JsonVal j(Json::objectValue);
|
||||
put(j, "flag", v.flag);
|
||||
put(j, "failCode", v.failCode);
|
||||
return j;
|
||||
}
|
||||
|
||||
// PM 状态报文(整合后发送给控制主机)-> JSON(关键字段)
|
||||
JsonVal pmStatusJson(const PmStatusValue& v) {
|
||||
JsonVal j(Json::objectValue);
|
||||
put(j, "fc_mode", v.fc_mode);
|
||||
put(j, "fc_status", v.fc_status);
|
||||
put(j, "fault_level_1", v.fault_level_1);
|
||||
put(j, "fault_level_2", v.fault_level_2);
|
||||
put(j, "fault_level_3", v.fault_level_3);
|
||||
put(j, "fault_level_4", v.fault_level_4);
|
||||
put(j, "total_generation_time", v.total_generation_time);
|
||||
put(j, "fc_fault_level", v.fc_fault_level);
|
||||
put(j, "output_power_limit", v.output_power_limit);
|
||||
put(j, "generation_power", v.generation_power);
|
||||
put(j, "hydrogen_capacity", v.hydrogen_capacity);
|
||||
put(j, "liquid_oxygen_capacity", v.liquid_oxygen_capacity);
|
||||
put(j, "palladium_temp", v.palladium_temp);
|
||||
put(j, "buffer_tank_pressure", v.buffer_tank_pressure);
|
||||
put(j, "main_pipe_pressure", v.main_pipe_pressure);
|
||||
put(j, "aux_pipe_pressure", v.aux_pipe_pressure);
|
||||
put(j, "dcdc1_in_voltage", v.dcdc1_in_voltage);
|
||||
put(j, "dcdc1_in_current", v.dcdc1_in_current);
|
||||
put(j, "dcdc2_in_voltage", v.dcdc2_in_voltage);
|
||||
put(j, "dcdc2_in_current", v.dcdc2_in_current);
|
||||
put(j, "dcdc_out_voltage", v.dcdc_out_voltage);
|
||||
put(j, "dcdc_out_current", v.dcdc_out_current);
|
||||
put(j, "methanol_total_use", v.methanol_total_use);
|
||||
put(j, "methanol_feed", v.methanol_feed);
|
||||
put(j, "exhaust_inlet_pressure", v.exhaust_inlet_pressure);
|
||||
put(j, "exhaust_outlet_pressure", v.exhaust_outlet_pressure);
|
||||
put(j, "exhaust_run_freq", v.exhaust_run_freq);
|
||||
put(j, "tank_lo2_pressure", v.tank_lo2_pressure);
|
||||
put(j, "tank_co2_pressure", v.tank_co2_pressure);
|
||||
put(j, "tank_lo2_level", v.tank_lo2_level);
|
||||
put(j, "alloy_h2_flow", v.alloy_h2_flow);
|
||||
put(j, "fc_h2_flow", v.fc_h2_flow);
|
||||
put(j, "fc_o2_flow", v.fc_o2_flow);
|
||||
put(j, "emergency_float_depth", v.emergency_float_depth);
|
||||
put(j, "emergency_float_time", v.emergency_float_time);
|
||||
put(j, "cabin_pressure1", v.cabin_pressure1);
|
||||
put(j, "cabin_pressure2", v.cabin_pressure2);
|
||||
put(j, "cabin_temp1", v.cabin_temp1);
|
||||
put(j, "cabin_temp2", v.cabin_temp2);
|
||||
put(j, "cabin_humidity1", v.cabin_humidity1);
|
||||
put(j, "cabin_humidity2", v.cabin_humidity2);
|
||||
put(j, "h2_concentration1", v.h2_concentration1);
|
||||
put(j, "h2_concentration2", v.h2_concentration2);
|
||||
put(j, "h2_concentration3", v.h2_concentration3);
|
||||
put(j, "o2_concentration1", v.o2_concentration1);
|
||||
put(j, "o2_concentration2", v.o2_concentration2);
|
||||
put(j, "ch3oh_concentration1", v.ch3oh_concentration1);
|
||||
put(j, "ch3oh_concentration2", v.ch3oh_concentration2);
|
||||
put(j, "flame_detector1", v.flame_detector1);
|
||||
put(j, "flame_detector2", v.flame_detector2);
|
||||
put(j, "emergency_battery1_voltage", v.emergency_battery1_voltage);
|
||||
put(j, "emergency_battery1_current", v.emergency_battery1_current);
|
||||
put(j, "emergency_battery1_max_temp", v.emergency_battery1_max_temp);
|
||||
put(j, "emergency_battery1_fault_word", v.emergency_battery1_fault_word);
|
||||
put(j, "emergency_battery2_voltage", v.emergency_battery2_voltage);
|
||||
put(j, "emergency_battery2_current", v.emergency_battery2_current);
|
||||
put(j, "emergency_battery2_max_temp", v.emergency_battery2_max_temp);
|
||||
put(j, "emergency_battery2_fault_word", v.emergency_battery2_fault_word);
|
||||
put(j, "ins_cabin_ox_concentration", v.ins_cabin_ox_concentration);
|
||||
put(j, "ins_cabin_temperature", v.ins_cabin_temperature);
|
||||
put(j, "ins_cabin_humidity", v.ins_cabin_humidity);
|
||||
put(j, "ins_cabin_pressure", v.ins_cabin_pressure);
|
||||
put(j, "dyn_cabin_ox_concentration", v.dyn_cabin_ox_concentration);
|
||||
put(j, "dyn_cabin_temperature", v.dyn_cabin_temperature);
|
||||
put(j, "dyn_cabin_humidity", v.dyn_cabin_humidity);
|
||||
put(j, "dyn_cabin_pressure", v.dyn_cabin_pressure);
|
||||
put(j, "ins_relay_status1", v.ins_relay_status1);
|
||||
put(j, "ins_relay_status2", v.ins_relay_status2);
|
||||
put(j, "dyn_relay_status1", v.dyn_relay_status1);
|
||||
put(j, "dyn_relay_status2", v.dyn_relay_status2);
|
||||
put(j, "ins_max_discharge_power", v.ins_max_discharge_power);
|
||||
put(j, "dyn_max_discharge_power", v.dyn_max_discharge_power);
|
||||
put(j, "ins_soc", v.ins_soc);
|
||||
put(j, "dyn_soc", v.dyn_soc);
|
||||
put(j, "ins_total_energy", v.ins_total_energy);
|
||||
put(j, "dyn_total_energy", v.dyn_total_energy);
|
||||
put(j, "ins_power_input", v.ins_power_input);
|
||||
put(j, "dyn_power_input", v.dyn_power_input);
|
||||
put(j, "ins_charge_status", v.ins_charge_status);
|
||||
put(j, "dyn_charge_status", v.dyn_charge_status);
|
||||
put(j, "ins_voltage_link", v.ins_voltage_link);
|
||||
put(j, "ins_voltage_pack", v.ins_voltage_pack);
|
||||
put(j, "ins_current", v.ins_current);
|
||||
put(j, "ins_resistance_pos", v.ins_resistance_pos);
|
||||
put(j, "ins_resistance_neg", v.ins_resistance_neg);
|
||||
put(j, "dyn_voltage_link", v.dyn_voltage_link);
|
||||
put(j, "dyn_voltage_pack", v.dyn_voltage_pack);
|
||||
put(j, "dyn_current", v.dyn_current);
|
||||
put(j, "dyn_resistance_pos", v.dyn_resistance_pos);
|
||||
put(j, "dyn_resistance_neg", v.dyn_resistance_neg);
|
||||
put(j, "ins_emergency_status", v.ins_emergency_status);
|
||||
put(j, "dyn_emergency_status", v.dyn_emergency_status);
|
||||
put(j, "device_online_flag1", v.device_online_flag1);
|
||||
put(j, "device_online_flag2", v.device_online_flag2);
|
||||
put(j, "heartbeat", v.heartbeat);
|
||||
put(j, "emergency_cmd", v.emergency_cmd);
|
||||
return j;
|
||||
}
|
||||
|
||||
JsonVal linkJson(const LinkManager* lm) {
|
||||
JsonVal j(Json::objectValue);
|
||||
if (!lm) return j;
|
||||
j["rx"] = JsonVal(static_cast<Json::UInt>(lm->rxCount()));
|
||||
j["tx"] = JsonVal(static_cast<Json::UInt>(lm->txCount()));
|
||||
j["err"] = JsonVal(static_cast<Json::UInt>(lm->errorCount()));
|
||||
j["lastRx"] = JsonVal(lm->lastRxTime());
|
||||
return j;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SnapshotBuilder::SnapshotBuilder(SystemData* sys, LinkManager* fc, LinkManager* pm, DbStore* db)
|
||||
: m_sys(sys), m_fc(fc), m_pm(pm), m_db(db) {}
|
||||
|
||||
std::string SnapshotBuilder::build() const {
|
||||
JsonVal root(Json::objectValue);
|
||||
|
||||
if (m_sys) {
|
||||
root["fc"] = fcStatusJson(m_sys->fcStatus());
|
||||
root["pmCmd"] = pmControlJson(m_sys->pmControl());
|
||||
root["fcCmd"] = fcControlJson(m_sys->fcControl());
|
||||
root["pmParam"] = pmParamJson(m_sys->pmParamSet());
|
||||
root["pmFb"] = pmFbJson(m_sys->pmParamFb());
|
||||
root["pmStatus"] = pmStatusJson(m_sys->pmStatus());
|
||||
root["fcStatusCount"] = JsonVal(static_cast<Json::UInt>(m_sys->fcStatusCount()));
|
||||
root["pmControlCount"] = JsonVal(static_cast<Json::UInt>(m_sys->pmControlCount()));
|
||||
root["fcControlCount"] = JsonVal(static_cast<Json::UInt>(m_sys->fcControlCount()));
|
||||
root["pmStatusCount"] = JsonVal(static_cast<Json::UInt>(m_sys->pmStatusCount()));
|
||||
} else {
|
||||
root["fc"] = JsonVal(Json::objectValue);
|
||||
root["pmCmd"] = JsonVal(Json::objectValue);
|
||||
root["fcCmd"] = JsonVal(Json::objectValue);
|
||||
root["pmParam"] = JsonVal(Json::objectValue);
|
||||
root["pmFb"] = JsonVal(Json::objectValue);
|
||||
root["pmStatus"] = JsonVal(Json::objectValue);
|
||||
}
|
||||
|
||||
JsonVal links(Json::objectValue);
|
||||
links["fc"] = linkJson(m_fc);
|
||||
links["pm"] = linkJson(m_pm);
|
||||
root["links"] = links;
|
||||
|
||||
// 最近原始帧(默认 20 条)
|
||||
if (m_db) {
|
||||
JsonVal logs(Json::arrayValue);
|
||||
auto rows = m_db->queryRecent("", -1, 0, 20);
|
||||
for (const auto& r : rows) {
|
||||
logs.append(commLogToJson(r));
|
||||
}
|
||||
root["logs"] = logs;
|
||||
}
|
||||
|
||||
Json::StreamWriterBuilder wb;
|
||||
wb.settings_["indentation"] = "";
|
||||
return Json::writeString(wb, root);
|
||||
}
|
||||
|
||||
std::string SnapshotBuilder::buildLogs(int limit) const {
|
||||
if (!m_db) return "[]";
|
||||
JsonVal logs(Json::arrayValue);
|
||||
auto rows = m_db->queryRecent("", -1, 0, limit);
|
||||
for (const auto& r : rows) {
|
||||
logs.append(commLogToJson(r));
|
||||
}
|
||||
Json::StreamWriterBuilder wb;
|
||||
wb.settings_["indentation"] = "";
|
||||
return Json::writeString(wb, logs);
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef PCCU_SNAPSHOT_BUILDER_H
|
||||
#define PCCU_SNAPSHOT_BUILDER_H
|
||||
|
||||
#include <string>
|
||||
#include "SystemData.h"
|
||||
#include "../comm/LinkManager.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
class DbStore;
|
||||
|
||||
//============================================================================
|
||||
// SnapshotBuilder:构建网页推送的 JSON 快照。
|
||||
//
|
||||
// 组合 SystemData(最新状态)、两条链路统计、最近原始帧日志,
|
||||
// 序列化为 JSON 字符串(jsoncpp)。串行化逻辑集中在此处,便于维护。
|
||||
//============================================================================
|
||||
|
||||
class SnapshotBuilder {
|
||||
public:
|
||||
SnapshotBuilder(SystemData* sys, LinkManager* fc, LinkManager* pm, DbStore* db);
|
||||
|
||||
// 生成完整快照 JSON
|
||||
std::string build() const;
|
||||
|
||||
// 生成最近原始帧日志 JSON(供 /api/logs)
|
||||
std::string buildLogs(int limit) const;
|
||||
|
||||
private:
|
||||
SystemData* m_sys;
|
||||
LinkManager* m_fc;
|
||||
LinkManager* m_pm;
|
||||
DbStore* m_db;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_SNAPSHOT_BUILDER_H
|
||||
@@ -0,0 +1,118 @@
|
||||
#ifndef PCCU_SYSTEM_DATA_H
|
||||
#define PCCU_SYSTEM_DATA_H
|
||||
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include "../protocol/FcProtocol.h"
|
||||
#include "../protocol/PmProtocol.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// SystemData:跨线程共享的最新状态快照。
|
||||
//
|
||||
// - 接收线程(FC 链路)写 FcStatus 快照
|
||||
// - CCU 主循环(Iterate)读快照并整合编码为 PM 状态报文发送
|
||||
// - 网页线程读快照展示
|
||||
// 通过互斥锁保护读写。
|
||||
//============================================================================
|
||||
|
||||
class SystemData {
|
||||
public:
|
||||
// 更新/获取燃料电池最新状态
|
||||
void updateFcStatus(const FcStatusValue& v) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_fcStatus = v;
|
||||
m_fcStatusCount++;
|
||||
}
|
||||
FcStatusValue fcStatus() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_fcStatus;
|
||||
}
|
||||
unsigned long fcStatusCount() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_fcStatusCount;
|
||||
}
|
||||
|
||||
// 更新/获取最新 PM 操控指令(用于转发到 FC)
|
||||
void updatePmControl(const PmControlValue& v) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_pmControl = v;
|
||||
m_pmControlCount++;
|
||||
}
|
||||
PmControlValue pmControl() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_pmControl;
|
||||
}
|
||||
unsigned long pmControlCount() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_pmControlCount;
|
||||
}
|
||||
|
||||
// 更新/获取最新 PM 参数设定(预留,用于未来转发/应用)
|
||||
void updatePmParamSet(const PmParamSetValue& v) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_pmParamSet = v;
|
||||
}
|
||||
PmParamSetValue pmParamSet() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_pmParamSet;
|
||||
}
|
||||
|
||||
// 更新/获取最新转发给 FC 的控制指令
|
||||
void updateFcControl(const FcControlValue& v) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_fcControl = v;
|
||||
m_fcControlCount++;
|
||||
}
|
||||
FcControlValue fcControl() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_fcControl;
|
||||
}
|
||||
unsigned long fcControlCount() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_fcControlCount;
|
||||
}
|
||||
|
||||
// 更新/获取最新发送给 PM 的状态报文(整合后的)
|
||||
void updatePmStatus(const PmStatusValue& v) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_pmStatus = v;
|
||||
m_pmStatusCount++;
|
||||
}
|
||||
PmStatusValue pmStatus() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_pmStatus;
|
||||
}
|
||||
unsigned long pmStatusCount() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_pmStatusCount;
|
||||
}
|
||||
|
||||
// 更新/获取最新发送给 PM 的参数反馈
|
||||
void updatePmParamFb(const PmParamSetFbValue& v) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_pmParamFb = v;
|
||||
}
|
||||
PmParamSetFbValue pmParamFb() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_pmParamFb;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex m_mutex;
|
||||
FcStatusValue m_fcStatus;
|
||||
PmControlValue m_pmControl;
|
||||
PmParamSetValue m_pmParamSet;
|
||||
FcControlValue m_fcControl;
|
||||
PmStatusValue m_pmStatus;
|
||||
PmParamSetFbValue m_pmParamFb;
|
||||
unsigned long m_fcStatusCount = 0;
|
||||
unsigned long m_pmControlCount = 0;
|
||||
unsigned long m_fcControlCount = 0;
|
||||
unsigned long m_pmStatusCount = 0;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_SYSTEM_DATA_H
|
||||
@@ -0,0 +1,54 @@
|
||||
/************************************************************/
|
||||
/* NAME: pCCU */
|
||||
/* FILE: main.cpp */
|
||||
/************************************************************/
|
||||
|
||||
#include <string>
|
||||
#include "MBUtils.h"
|
||||
#include "ColorParse.h"
|
||||
#include "CCU.h"
|
||||
#include "CCU_Info.h"
|
||||
#include "../pPowerManger/logc/loguru.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
loguru::init(argc, argv);
|
||||
|
||||
string mission_file;
|
||||
string run_command = argv[0];
|
||||
// 默认以程序文件名(去掉路径)作为进程名,保证与 .moos 中 ProcessConfig 匹配
|
||||
{
|
||||
size_t slash = run_command.find_last_of('/');
|
||||
if (slash != string::npos) run_command = run_command.substr(slash + 1);
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
string argi = argv[i];
|
||||
if ((argi == "-v") || (argi == "--version") || (argi == "-version"))
|
||||
showReleaseInfoAndExit();
|
||||
else if ((argi == "-e") || (argi == "--example") || (argi == "-example"))
|
||||
showExampleConfigAndExit();
|
||||
else if ((argi == "-h") || (argi == "--help") || (argi == "-help"))
|
||||
showHelpAndExit();
|
||||
else if ((argi == "-i") || (argi == "--interface"))
|
||||
showInterfaceAndExit();
|
||||
else if (strEnds(argi, ".moos") || strEnds(argi, ".moos++"))
|
||||
mission_file = argv[i];
|
||||
else if (strBegins(argi, "--alias="))
|
||||
run_command = argi.substr(8);
|
||||
else if (i == 2)
|
||||
run_command = argi;
|
||||
}
|
||||
|
||||
if (mission_file == "")
|
||||
showHelpAndExit();
|
||||
|
||||
LOG_F(INFO, "pCCU launching as %s", run_command.c_str());
|
||||
|
||||
ccu::CCU CCU;
|
||||
CCU.Run(run_command.c_str(), mission_file.c_str());
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//============================================================================
|
||||
// pCCU 配置示例
|
||||
//
|
||||
// 链路拓扑:
|
||||
// 燃料电池控制器(FC) 192.168.1.162:7000
|
||||
// 控制主机(pPowerManger) 192.168.0.140:5001
|
||||
// pCCU 本机监听:FC 状态端口 6000、PM 指令端口 7000
|
||||
//============================================================================
|
||||
|
||||
ProcessConfig = pCCU
|
||||
{
|
||||
AppTick = 4
|
||||
CommsTick = 4
|
||||
|
||||
//======== FC 链路(与燃料电池通信) ========
|
||||
// 本机接收 FC 状态反馈的端口
|
||||
fc_local_port = 6000
|
||||
// 燃料电池控制器地址(发送 FC 控制指令目标)
|
||||
fc_remote_ip = 192.168.1.162
|
||||
fc_remote_port = 7000
|
||||
|
||||
//======== PM 链路(与控制主机通信) ========
|
||||
// 本机接收 pPowerManger 指令的端口
|
||||
pm_local_port = 7000
|
||||
// 控制主机地址(发送 PM 状态报文目标)
|
||||
pm_remote_ip = 192.168.0.140
|
||||
pm_remote_port = 5001
|
||||
|
||||
//======== 存储与日志 ========
|
||||
// 数据库路径(SQLite)
|
||||
dbpath = pccu_data.db
|
||||
// 日志文件路径
|
||||
logpath = pCCU.log
|
||||
|
||||
//======== 网页监控 ========
|
||||
// 端口避开 pPowerManger(8090) / pPowerMangerHost(18080)
|
||||
web_port = 8080
|
||||
web_enable = true
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#ifndef PCCU_CHECKSUM_POLICY_H
|
||||
#define PCCU_CHECKSUM_POLICY_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// ChecksumPolicy:校验和策略(可插拔)。
|
||||
//
|
||||
// 协议联调阶段校验和的实现可能调整,因此对每条消息独立配置策略:
|
||||
// - None : 无校验和字段(如 FC 状态帧文档未列校验行)
|
||||
// - Sum32 : uint32 字节和,从域起始符到校验和之前所有数据的字节和
|
||||
// - Sum16 : uint16 字节和(预留,备用)
|
||||
// 新增策略只需在此类扩展,不影响其它代码。
|
||||
//============================================================================
|
||||
|
||||
enum class ChecksumType {
|
||||
None, // 无校验
|
||||
Sum32, // uint32 字节和
|
||||
Sum16, // uint16 字节和
|
||||
};
|
||||
|
||||
class ChecksumPolicy {
|
||||
public:
|
||||
explicit ChecksumPolicy(ChecksumType type) : m_type(type) {}
|
||||
|
||||
ChecksumType type() const { return m_type; }
|
||||
|
||||
// 校验和字段占用的字节数;None 返回 0
|
||||
size_t size() const {
|
||||
switch (m_type) {
|
||||
case ChecksumType::Sum32: return 4;
|
||||
case ChecksumType::Sum16: return 2;
|
||||
case ChecksumType::None: return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 计算校验和:对 data[0 .. data.size()] 全部字节求和(调用方保证不含校验和自身)
|
||||
uint32_t compute(const std::vector<uint8_t>& data) const {
|
||||
uint32_t sum = 0;
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
sum += data[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
// 校验给定帧(含校验和字段):返回帧尾校验和字段是否与计算值一致
|
||||
// frame 必须包含从域起始符开始的完整帧。校验和位于末尾 size() 字节。
|
||||
bool verify(const std::vector<uint8_t>& frame) const {
|
||||
size_t cs = size();
|
||||
if (cs == 0) return true; // 无校验,视为通过
|
||||
if (frame.size() < cs) return false;
|
||||
|
||||
std::vector<uint8_t> body(frame.begin(), frame.end() - cs);
|
||||
uint32_t expect = compute(body);
|
||||
|
||||
uint32_t actual = 0;
|
||||
size_t offset = frame.size() - cs;
|
||||
if (m_type == ChecksumType::Sum32) {
|
||||
actual = static_cast<uint32_t>(frame[offset]) |
|
||||
(static_cast<uint32_t>(frame[offset + 1]) << 8) |
|
||||
(static_cast<uint32_t>(frame[offset + 2]) << 16) |
|
||||
(static_cast<uint32_t>(frame[offset + 3]) << 24);
|
||||
return expect == actual;
|
||||
}
|
||||
// Sum16
|
||||
actual = static_cast<uint32_t>(frame[offset]) |
|
||||
(static_cast<uint32_t>(frame[offset + 1]) << 8);
|
||||
return (expect & 0xFFFF) == actual;
|
||||
}
|
||||
|
||||
private:
|
||||
ChecksumType m_type;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_CHECKSUM_POLICY_H
|
||||
@@ -0,0 +1,318 @@
|
||||
#include "FcProtocol.h"
|
||||
#include "FieldCodec.h"
|
||||
#include "Frame.h"
|
||||
#include "MessageRegistry.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
void registerFcMessages(MessageRegistry& reg) {
|
||||
reg.registerMessage(std::unique_ptr<Message>(new FcControlMessage()));
|
||||
reg.registerMessage(std::unique_ptr<Message>(new FcStatusMessage()));
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
// 0x0001 控制指令域编解码
|
||||
// 数据域 14 字节,偏移 0~13(对应文档字节 7~20)
|
||||
//============================================================================
|
||||
|
||||
std::vector<uint8_t> FcControlMessage::encode(const void* obj) const {
|
||||
const FcControlValue* v = static_cast<const FcControlValue*>(obj);
|
||||
std::vector<uint8_t> payload(payloadLength(), 0);
|
||||
|
||||
FieldCodec::putU8(payload, 0, v->mode);
|
||||
FieldCodec::putU8(payload, 1, v->cmd);
|
||||
FieldCodec::putU8(payload, 2, v->outputPower);
|
||||
FieldCodec::putU16(payload, 3, static_cast<uint16_t>(v->pitch)); // 数据1=低位 / 数据2=高位
|
||||
FieldCodec::putU16(payload, 5, static_cast<uint16_t>(v->roll));
|
||||
FieldCodec::putU8(payload, 7, v->emergencyAllow);
|
||||
FieldCodec::putU16(payload, 8, v->depth);
|
||||
FieldCodec::putU8(payload, 10, v->supplyCmd);
|
||||
FieldCodec::putU8(payload, 11, v->reservedCmd5);
|
||||
FieldCodec::putU8(payload, 12, v->reservedCmd6);
|
||||
FieldCodec::putU8(payload, 13, v->heartbeat);
|
||||
|
||||
return Frame::build(id(), payload, checksum());
|
||||
}
|
||||
|
||||
bool FcControlMessage::decode(const std::vector<uint8_t>& frame, void* obj) const {
|
||||
if (!validateFrame(frame, id(), checksum(), totalLength())) return false;
|
||||
FcControlValue* v = static_cast<FcControlValue*>(obj);
|
||||
|
||||
size_t o = FRAME_HEADER_LEN; // 数据域起点
|
||||
v->mode = FieldCodec::getU8(frame, o + 0);
|
||||
v->cmd = FieldCodec::getU8(frame, o + 1);
|
||||
v->outputPower = FieldCodec::getU8(frame, o + 2);
|
||||
v->pitch = static_cast<int16_t>(FieldCodec::getU16(frame, o + 3));
|
||||
v->roll = static_cast<int16_t>(FieldCodec::getU16(frame, o + 5));
|
||||
v->emergencyAllow = FieldCodec::getU8(frame, o + 7);
|
||||
v->depth = FieldCodec::getU16(frame, o + 8);
|
||||
v->supplyCmd = FieldCodec::getU8(frame, o + 10);
|
||||
v->reservedCmd5 = FieldCodec::getU8(frame, o + 11);
|
||||
v->reservedCmd6 = FieldCodec::getU8(frame, o + 12);
|
||||
v->heartbeat = FieldCodec::getU8(frame, o + 13);
|
||||
return true;
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
// 0x0002 状态反馈域编解码
|
||||
// 数据域 144 字节
|
||||
//============================================================================
|
||||
|
||||
namespace {
|
||||
// 数据域内各字段偏移(相对数据域起点,offset = 文档字节号 - 7)
|
||||
// 注意:文档中"系统故障等级"为字节19(1字节),字节20为隐式保留位,
|
||||
// "系统发电功率"从字节21(offset 14)开始,其后的字段均按此定位。
|
||||
enum : size_t {
|
||||
OFF_MODE = 0, // 运行模式 (字节7)
|
||||
OFF_STATUS = 1, // 运行状态 (字节8)
|
||||
OFF_FL1 = 2, // 一级故障码 u16 (字节9-10)
|
||||
OFF_FL2 = 4, // 二级故障码 (字节11-12)
|
||||
OFF_FL3 = 6, // 三级故障码 (字节13-14)
|
||||
OFF_FL4 = 8, // 四级故障码 (字节15-16)
|
||||
OFF_GEN_TIME = 10, // 累积发电时间 u16 (字节17-18)
|
||||
OFF_FAULT_LEVEL = 12, // 系统故障等级 (字节19)
|
||||
// 字节20 隐式保留(offset 13)
|
||||
OFF_GEN_POWER = 14, // 系统发电功率 u16 (字节21-22)
|
||||
OFF_H2_CAP = 16, // 储氢剩余容量 (字节23)
|
||||
OFF_LO2_CAP = 17, // 液氧剩余容量 (字节24)
|
||||
OFF_FC1_MIN_V = 18, // I#FC最低单片电压 (字节25)
|
||||
OFF_FC1_MIN_POS = 19, // I#FC最低单片电压位置 (字节26)
|
||||
OFF_FC1_AVG_V = 20, // I#FC平均单片电压 (字节27)
|
||||
OFF_FC2_MIN_V = 21, // 2#FC最低单片电压 (字节28)
|
||||
OFF_FC2_MIN_POS = 22, // 2#FC最低单片电压位置 (字节29)
|
||||
OFF_FC2_AVG_V = 23, // 2#FC平均单片电压 (字节30)
|
||||
OFF_PALLADIUM_TEMP = 24, // 钯膜最高温度 u16 (字节31-32)
|
||||
OFF_BUFFER_PRES = 26, // 缓冲罐压力 (字节33-34)
|
||||
OFF_FLUE_TOTAL = 28, // 烟气累计排放量 (字节35-36)
|
||||
OFF_FLUE_PRES = 30, // 烟气压力 (字节37-38)
|
||||
OFF_REACTOR_PRES = 32, // 反应器压力 (字节39-40)
|
||||
OFF_EVALVE_OPEN = 34, // 电动阀开度 (字节41-42)
|
||||
OFF_DCDC1_IN_V = 36, // DC/DC通道1输入电压 (字节43-44)
|
||||
OFF_DCDC1_IN_I = 38, // DC/DC通道1输入电流 (字节45-46)
|
||||
OFF_DCDC2_IN_V = 40, // DC/DC通道2输入电压 (字节47-48)
|
||||
OFF_DCDC2_IN_I = 42, // DC/DC通道2输入电流 (字节49-50)
|
||||
OFF_DCDC_OUT_V = 44, // DC/DC输出电压 (字节51-52)
|
||||
OFF_DCDC_OUT_I = 46, // DC/DC输出电流 (字节53-54)
|
||||
OFF_DCDC_CTRL_V = 48, // DC/DC控制电源电压 (字节55)
|
||||
OFF_DCDC_AUX_V = 49, // DC/DC辅电输出电压 (字节56)
|
||||
OFF_METHANOL_TOTAL = 50, // 甲醇累计使用量 u16 (字节57-58)
|
||||
OFF_METHANOL_FEED = 52, // 甲醇溶液进料量 (字节59-60)
|
||||
OFF_O2_WATER = 54, // 氧侧生成水箱液位 (字节61-62)
|
||||
OFF_H2_WATER = 56, // 氢侧生成水箱液位 (字节63-64)
|
||||
OFF_BALLAST_WATER = 58, // 配重水箱液位 (字节65-66)
|
||||
OFF_EXH_IN_PRES = 60, // 尾气装置进气压力 (字节67-68)
|
||||
OFF_EXH_OUT_PRES = 62, // 尾气装置排气压力 (字节69-70)
|
||||
OFF_CABIN_P1 = 64, // 舱室压力1 (字节71-72)
|
||||
OFF_CABIN_P2 = 66, // 舱室压力2 (字节73-74)
|
||||
OFF_CABIN_T1 = 68, // 舱室温度1 (字节75-76)
|
||||
OFF_CABIN_T2 = 70, // 舱室温度2 (字节77-78)
|
||||
OFF_CABIN_H1 = 72, // 舱室湿度1 (字节79-80)
|
||||
OFF_CABIN_H2 = 74, // 舱室湿度2 (字节81-82)
|
||||
OFF_H2_C1 = 76, // 舱室H2浓度1 (字节83-84)
|
||||
OFF_H2_C2 = 78, // 舱室H2浓度2 (字节85-86)
|
||||
OFF_H2_C3 = 80, // 舱室H2浓度3 (字节87-88)
|
||||
OFF_O2_C1 = 82, // 舱室O2浓度1 (字节89-90)
|
||||
OFF_O2_C2 = 84, // 舱室O2浓度2 (字节91-92)
|
||||
OFF_CH3OH_C1 = 86, // 舱室甲醇浓度1 (字节93-94)
|
||||
OFF_CH3OH_C2 = 88, // 舱室甲醇浓度2 (字节95-96)
|
||||
OFF_FLAME1 = 90, // 火焰探测器1 (字节97)
|
||||
OFF_FLAME2 = 91, // 火焰探测器2 (字节98)
|
||||
OFF_EMER_DEPTH = 92, // 应急上浮深度 u16 (字节99-100)
|
||||
OFF_EMER_TIME = 94, // 应急上浮时间 (字节101-102)
|
||||
OFF_EXH_FREQ = 96, // 尾气运行频率 (字节103-104)
|
||||
OFF_EXH_IN_TEMP = 98, // 尾气进气温度 (字节105-106)
|
||||
OFF_EXH_OUT_TEMP = 100, // 尾气排气温度 (字节107-108)
|
||||
OFF_EXH_WIN_PRES = 102, // 尾气进水压力 (字节109-110)
|
||||
OFF_EXH_WOUT_PRES = 104, // 尾气排水压力 (字节111-112)
|
||||
OFF_TANK_LO2_PRES = 106, // 液氧罐压力 (字节113-114)
|
||||
OFF_TANK_CO2_PRES = 108, // 二氧化碳压力 (字节115-116)
|
||||
OFF_TANK_LO2_LEVEL = 110, // 液氧罐液位 (字节117-118)
|
||||
OFF_ALLOY_H2_FLOW = 112, // 合金供氢流量 (字节119-120)
|
||||
OFF_FC_H2_FLOW = 114, // FC供氢流量 (字节121-122)
|
||||
OFF_FC_O2_FLOW = 116, // FC供氧流量 (字节123-124)
|
||||
OFF_RESERVED = 118, // 预留1~12 u16*12 -> 118..141 (字节125-148)
|
||||
OFF_HEARTBEAT = 142, // 通信心跳 (字节149)
|
||||
OFF_EMERGENCY_CMD = 143, // 应急指令 (字节150)
|
||||
};
|
||||
|
||||
inline void getU16Arr(const std::vector<uint8_t>& frame, size_t off, uint16_t* dst, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i) dst[i] = FieldCodec::getU16(frame, off + i * 2);
|
||||
}
|
||||
inline void putU16Arr(std::vector<uint8_t>& p, size_t off, const uint16_t* src, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i) FieldCodec::putU16(p, off + i * 2, src[i]);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<uint8_t> FcStatusMessage::encode(const void* obj) const {
|
||||
const FcStatusValue* v = static_cast<const FcStatusValue*>(obj);
|
||||
std::vector<uint8_t> p(payloadLength(), 0);
|
||||
|
||||
FieldCodec::putU8(p, OFF_MODE, v->fc_mode);
|
||||
FieldCodec::putU8(p, OFF_STATUS, v->fc_status);
|
||||
FieldCodec::putU16(p, OFF_FL1, v->fault_level_1);
|
||||
FieldCodec::putU16(p, OFF_FL2, v->fault_level_2);
|
||||
FieldCodec::putU16(p, OFF_FL3, v->fault_level_3);
|
||||
FieldCodec::putU16(p, OFF_FL4, v->fault_level_4);
|
||||
FieldCodec::putU16(p, OFF_GEN_TIME, v->total_generation_time);
|
||||
FieldCodec::putU8(p, OFF_FAULT_LEVEL, v->fc_fault_level);
|
||||
FieldCodec::putU16(p, OFF_GEN_POWER, v->generation_power);
|
||||
FieldCodec::putU8(p, OFF_H2_CAP, v->hydrogen_capacity);
|
||||
FieldCodec::putU8(p, OFF_LO2_CAP, v->liquid_oxygen_capacity);
|
||||
|
||||
FieldCodec::putU8(p, OFF_FC1_MIN_V, v->fc1_min_cell_voltage);
|
||||
FieldCodec::putU8(p, OFF_FC1_MIN_POS, v->fc1_min_cell_pos);
|
||||
FieldCodec::putU8(p, OFF_FC1_AVG_V, v->fc1_avg_cell_voltage);
|
||||
FieldCodec::putU8(p, OFF_FC2_MIN_V, v->fc2_min_cell_voltage);
|
||||
FieldCodec::putU8(p, OFF_FC2_MIN_POS, v->fc2_min_cell_pos);
|
||||
FieldCodec::putU8(p, OFF_FC2_AVG_V, v->fc2_avg_cell_voltage);
|
||||
|
||||
FieldCodec::putU16(p, OFF_PALLADIUM_TEMP, v->palladium_temp);
|
||||
FieldCodec::putU16(p, OFF_BUFFER_PRES, v->buffer_tank_pressure);
|
||||
FieldCodec::putU16(p, OFF_FLUE_TOTAL, v->flue_total_emission);
|
||||
FieldCodec::putU16(p, OFF_FLUE_PRES, v->flue_pressure);
|
||||
FieldCodec::putU16(p, OFF_REACTOR_PRES, v->reactor_pressure);
|
||||
FieldCodec::putU16(p, OFF_EVALVE_OPEN, v->electric_valve_open);
|
||||
|
||||
FieldCodec::putU16(p, OFF_DCDC1_IN_V, v->dcdc1_in_voltage);
|
||||
FieldCodec::putU16(p, OFF_DCDC1_IN_I, v->dcdc1_in_current);
|
||||
FieldCodec::putU16(p, OFF_DCDC2_IN_V, v->dcdc2_in_voltage);
|
||||
FieldCodec::putU16(p, OFF_DCDC2_IN_I, v->dcdc2_in_current);
|
||||
FieldCodec::putU16(p, OFF_DCDC_OUT_V, v->dcdc_out_voltage);
|
||||
FieldCodec::putU16(p, OFF_DCDC_OUT_I, v->dcdc_out_current);
|
||||
FieldCodec::putU8(p, OFF_DCDC_CTRL_V, v->dcdc_ctrl_voltage);
|
||||
FieldCodec::putU8(p, OFF_DCDC_AUX_V, v->dcdc_aux_voltage);
|
||||
|
||||
FieldCodec::putU16(p, OFF_METHANOL_TOTAL, v->methanol_total_use);
|
||||
FieldCodec::putU16(p, OFF_METHANOL_FEED, v->methanol_feed);
|
||||
FieldCodec::putU16(p, OFF_O2_WATER, v->oxygen_side_water_level);
|
||||
FieldCodec::putU16(p, OFF_H2_WATER, v->hydrogen_side_water_level);
|
||||
FieldCodec::putU16(p, OFF_BALLAST_WATER, v->ballast_water_level);
|
||||
|
||||
FieldCodec::putU16(p, OFF_EXH_IN_PRES, v->exhaust_inlet_pressure);
|
||||
FieldCodec::putU16(p, OFF_EXH_OUT_PRES, v->exhaust_outlet_pressure);
|
||||
FieldCodec::putU16(p, OFF_CABIN_P1, v->cabin_pressure1);
|
||||
FieldCodec::putU16(p, OFF_CABIN_P2, v->cabin_pressure2);
|
||||
FieldCodec::putU16(p, OFF_CABIN_T1, v->cabin_temp1);
|
||||
FieldCodec::putU16(p, OFF_CABIN_T2, v->cabin_temp2);
|
||||
FieldCodec::putU16(p, OFF_CABIN_H1, v->cabin_humidity1);
|
||||
FieldCodec::putU16(p, OFF_CABIN_H2, v->cabin_humidity2);
|
||||
FieldCodec::putU16(p, OFF_H2_C1, v->h2_concentration1);
|
||||
FieldCodec::putU16(p, OFF_H2_C2, v->h2_concentration2);
|
||||
FieldCodec::putU16(p, OFF_H2_C3, v->h2_concentration3);
|
||||
FieldCodec::putU16(p, OFF_O2_C1, v->o2_concentration1);
|
||||
FieldCodec::putU16(p, OFF_O2_C2, v->o2_concentration2);
|
||||
FieldCodec::putU16(p, OFF_CH3OH_C1, v->ch3oh_concentration1);
|
||||
FieldCodec::putU16(p, OFF_CH3OH_C2, v->ch3oh_concentration2);
|
||||
|
||||
FieldCodec::putU8(p, OFF_FLAME1, v->flame_detector1);
|
||||
FieldCodec::putU8(p, OFF_FLAME2, v->flame_detector2);
|
||||
|
||||
FieldCodec::putU16(p, OFF_EMER_DEPTH, v->emergency_float_depth);
|
||||
FieldCodec::putU16(p, OFF_EMER_TIME, v->emergency_float_time);
|
||||
FieldCodec::putU16(p, OFF_EXH_FREQ, v->exhaust_run_freq);
|
||||
FieldCodec::putU16(p, OFF_EXH_IN_TEMP, v->exhaust_inlet_temp);
|
||||
FieldCodec::putU16(p, OFF_EXH_OUT_TEMP, v->exhaust_outlet_temp);
|
||||
FieldCodec::putU16(p, OFF_EXH_WIN_PRES, v->exhaust_water_in_pressure);
|
||||
FieldCodec::putU16(p, OFF_EXH_WOUT_PRES, v->exhaust_water_out_pressure);
|
||||
FieldCodec::putU16(p, OFF_TANK_LO2_PRES, v->tank_lo2_pressure);
|
||||
FieldCodec::putU16(p, OFF_TANK_CO2_PRES, v->tank_co2_pressure);
|
||||
FieldCodec::putU16(p, OFF_TANK_LO2_LEVEL, v->tank_lo2_level);
|
||||
FieldCodec::putU16(p, OFF_ALLOY_H2_FLOW, v->alloy_h2_flow);
|
||||
FieldCodec::putU16(p, OFF_FC_H2_FLOW, v->fc_h2_flow);
|
||||
FieldCodec::putU16(p, OFF_FC_O2_FLOW, v->fc_o2_flow);
|
||||
|
||||
putU16Arr(p, OFF_RESERVED, v->reserved, 12);
|
||||
|
||||
FieldCodec::putU8(p, OFF_HEARTBEAT, v->heartbeat);
|
||||
FieldCodec::putU8(p, OFF_EMERGENCY_CMD, v->emergency_cmd);
|
||||
|
||||
return Frame::build(id(), p, checksum());
|
||||
}
|
||||
|
||||
bool FcStatusMessage::decode(const std::vector<uint8_t>& frame, void* obj) const {
|
||||
if (!validateFrame(frame, id(), checksum(), totalLength())) return false;
|
||||
FcStatusValue* v = static_cast<FcStatusValue*>(obj);
|
||||
size_t o = FRAME_HEADER_LEN;
|
||||
|
||||
v->fc_mode = FieldCodec::getU8(frame, o + OFF_MODE);
|
||||
v->fc_status = FieldCodec::getU8(frame, o + OFF_STATUS);
|
||||
v->fault_level_1 = FieldCodec::getU16(frame, o + OFF_FL1);
|
||||
v->fault_level_2 = FieldCodec::getU16(frame, o + OFF_FL2);
|
||||
v->fault_level_3 = FieldCodec::getU16(frame, o + OFF_FL3);
|
||||
v->fault_level_4 = FieldCodec::getU16(frame, o + OFF_FL4);
|
||||
v->total_generation_time = FieldCodec::getU16(frame, o + OFF_GEN_TIME);
|
||||
v->fc_fault_level = FieldCodec::getU8(frame, o + OFF_FAULT_LEVEL);
|
||||
v->generation_power = FieldCodec::getU16(frame, o + OFF_GEN_POWER);
|
||||
v->hydrogen_capacity = FieldCodec::getU8(frame, o + OFF_H2_CAP);
|
||||
v->liquid_oxygen_capacity = FieldCodec::getU8(frame, o + OFF_LO2_CAP);
|
||||
|
||||
v->fc1_min_cell_voltage = FieldCodec::getU8(frame, o + OFF_FC1_MIN_V);
|
||||
v->fc1_min_cell_pos = FieldCodec::getU8(frame, o + OFF_FC1_MIN_POS);
|
||||
v->fc1_avg_cell_voltage = FieldCodec::getU8(frame, o + OFF_FC1_AVG_V);
|
||||
v->fc2_min_cell_voltage = FieldCodec::getU8(frame, o + OFF_FC2_MIN_V);
|
||||
v->fc2_min_cell_pos = FieldCodec::getU8(frame, o + OFF_FC2_MIN_POS);
|
||||
v->fc2_avg_cell_voltage = FieldCodec::getU8(frame, o + OFF_FC2_AVG_V);
|
||||
|
||||
v->palladium_temp = FieldCodec::getU16(frame, o + OFF_PALLADIUM_TEMP);
|
||||
v->buffer_tank_pressure = FieldCodec::getU16(frame, o + OFF_BUFFER_PRES);
|
||||
v->flue_total_emission = FieldCodec::getU16(frame, o + OFF_FLUE_TOTAL);
|
||||
v->flue_pressure = FieldCodec::getU16(frame, o + OFF_FLUE_PRES);
|
||||
v->reactor_pressure = FieldCodec::getU16(frame, o + OFF_REACTOR_PRES);
|
||||
v->electric_valve_open = FieldCodec::getU16(frame, o + OFF_EVALVE_OPEN);
|
||||
|
||||
v->dcdc1_in_voltage = FieldCodec::getU16(frame, o + OFF_DCDC1_IN_V);
|
||||
v->dcdc1_in_current = FieldCodec::getU16(frame, o + OFF_DCDC1_IN_I);
|
||||
v->dcdc2_in_voltage = FieldCodec::getU16(frame, o + OFF_DCDC2_IN_V);
|
||||
v->dcdc2_in_current = FieldCodec::getU16(frame, o + OFF_DCDC2_IN_I);
|
||||
v->dcdc_out_voltage = FieldCodec::getU16(frame, o + OFF_DCDC_OUT_V);
|
||||
v->dcdc_out_current = FieldCodec::getU16(frame, o + OFF_DCDC_OUT_I);
|
||||
v->dcdc_ctrl_voltage = FieldCodec::getU8(frame, o + OFF_DCDC_CTRL_V);
|
||||
v->dcdc_aux_voltage = FieldCodec::getU8(frame, o + OFF_DCDC_AUX_V);
|
||||
|
||||
v->methanol_total_use = FieldCodec::getU16(frame, o + OFF_METHANOL_TOTAL);
|
||||
v->methanol_feed = FieldCodec::getU16(frame, o + OFF_METHANOL_FEED);
|
||||
v->oxygen_side_water_level= FieldCodec::getU16(frame, o + OFF_O2_WATER);
|
||||
v->hydrogen_side_water_level = FieldCodec::getU16(frame, o + OFF_H2_WATER);
|
||||
v->ballast_water_level = FieldCodec::getU16(frame, o + OFF_BALLAST_WATER);
|
||||
|
||||
v->exhaust_inlet_pressure = FieldCodec::getU16(frame, o + OFF_EXH_IN_PRES);
|
||||
v->exhaust_outlet_pressure= FieldCodec::getU16(frame, o + OFF_EXH_OUT_PRES);
|
||||
v->cabin_pressure1 = FieldCodec::getU16(frame, o + OFF_CABIN_P1);
|
||||
v->cabin_pressure2 = FieldCodec::getU16(frame, o + OFF_CABIN_P2);
|
||||
v->cabin_temp1 = FieldCodec::getU16(frame, o + OFF_CABIN_T1);
|
||||
v->cabin_temp2 = FieldCodec::getU16(frame, o + OFF_CABIN_T2);
|
||||
v->cabin_humidity1 = FieldCodec::getU16(frame, o + OFF_CABIN_H1);
|
||||
v->cabin_humidity2 = FieldCodec::getU16(frame, o + OFF_CABIN_H2);
|
||||
v->h2_concentration1 = FieldCodec::getU16(frame, o + OFF_H2_C1);
|
||||
v->h2_concentration2 = FieldCodec::getU16(frame, o + OFF_H2_C2);
|
||||
v->h2_concentration3 = FieldCodec::getU16(frame, o + OFF_H2_C3);
|
||||
v->o2_concentration1 = FieldCodec::getU16(frame, o + OFF_O2_C1);
|
||||
v->o2_concentration2 = FieldCodec::getU16(frame, o + OFF_O2_C2);
|
||||
v->ch3oh_concentration1 = FieldCodec::getU16(frame, o + OFF_CH3OH_C1);
|
||||
v->ch3oh_concentration2 = FieldCodec::getU16(frame, o + OFF_CH3OH_C2);
|
||||
|
||||
v->flame_detector1 = FieldCodec::getU8(frame, o + OFF_FLAME1);
|
||||
v->flame_detector2 = FieldCodec::getU8(frame, o + OFF_FLAME2);
|
||||
|
||||
v->emergency_float_depth = FieldCodec::getU16(frame, o + OFF_EMER_DEPTH);
|
||||
v->emergency_float_time = FieldCodec::getU16(frame, o + OFF_EMER_TIME);
|
||||
v->exhaust_run_freq = FieldCodec::getU16(frame, o + OFF_EXH_FREQ);
|
||||
v->exhaust_inlet_temp = FieldCodec::getU16(frame, o + OFF_EXH_IN_TEMP);
|
||||
v->exhaust_outlet_temp = FieldCodec::getU16(frame, o + OFF_EXH_OUT_TEMP);
|
||||
v->exhaust_water_in_pressure = FieldCodec::getU16(frame, o + OFF_EXH_WIN_PRES);
|
||||
v->exhaust_water_out_pressure= FieldCodec::getU16(frame, o + OFF_EXH_WOUT_PRES);
|
||||
v->tank_lo2_pressure = FieldCodec::getU16(frame, o + OFF_TANK_LO2_PRES);
|
||||
v->tank_co2_pressure = FieldCodec::getU16(frame, o + OFF_TANK_CO2_PRES);
|
||||
v->tank_lo2_level = FieldCodec::getU16(frame, o + OFF_TANK_LO2_LEVEL);
|
||||
v->alloy_h2_flow = FieldCodec::getU16(frame, o + OFF_ALLOY_H2_FLOW);
|
||||
v->fc_h2_flow = FieldCodec::getU16(frame, o + OFF_FC_H2_FLOW);
|
||||
v->fc_o2_flow = FieldCodec::getU16(frame, o + OFF_FC_O2_FLOW);
|
||||
|
||||
getU16Arr(frame, o + OFF_RESERVED, v->reserved, 12);
|
||||
|
||||
v->heartbeat = FieldCodec::getU8(frame, o + OFF_HEARTBEAT);
|
||||
v->emergency_cmd = FieldCodec::getU8(frame, o + OFF_EMERGENCY_CMD);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,174 @@
|
||||
#ifndef PCCU_FC_PROTOCOL_H
|
||||
#define PCCU_FC_PROTOCOL_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "Message.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// FC 协议(燃料电池系统 <-> 复合管控器)
|
||||
// 依据:docs/超滑FC和复合管控器通讯协议 - 0821.docx
|
||||
//
|
||||
// 帧格式统一:0x40 0x40 + 域标识符(2B) + 域字节数(2B) + 数据域 + [校验和]
|
||||
// 注意:FC 协议文档未列出校验和行,故默认采用 None(可插拔,联调可切换)。
|
||||
//
|
||||
// 两条消息:
|
||||
// 0x0001 复合管控器->燃料电池控制器(控制指令域) payload 14B,总长 20B
|
||||
// 0x0002 燃料电池控制器->复合管控器(状态反馈域) payload 144B,总长 150B
|
||||
//============================================================================
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 0x0001 控制指令域(复合管控器 -> 燃料电池)
|
||||
// payload 14 字节,对应文档字节 7~20
|
||||
//--------------------------------------------------------------------------
|
||||
struct FcControlValue {
|
||||
uint8_t mode = 0; // 模式设定 00~03
|
||||
uint8_t cmd = 0; // 操控指令 00~0B
|
||||
uint8_t outputPower = 0; // 输出功率指令 0~250,分辨率0.1kW
|
||||
int16_t pitch = 0; // 纵倾姿态(数据1=低8位 / 数据2=高8位,int16),分辨率0.1°
|
||||
int16_t roll = 0; // 横倾姿态(数据1=低8位 / 数据2=高8位,int16),分辨率0.1°
|
||||
uint8_t emergencyAllow = 0; // 应急允许:Bit0 允许降载 / Bit1 允许排气
|
||||
uint16_t depth = 0; // 潜深深度,单位 m
|
||||
uint8_t supplyCmd = 0; // 补给/排放指令 00~0B
|
||||
uint8_t reservedCmd5 = 0; // 预留指令5
|
||||
uint8_t reservedCmd6 = 0; // 预留指令6
|
||||
uint8_t heartbeat = 0; // 通信心跳,每次+1
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 0x0002 状态反馈域(燃料电池 -> 复合管控器)
|
||||
// payload 144 字节,对应文档字节 7~150
|
||||
//--------------------------------------------------------------------------
|
||||
struct FcStatusValue {
|
||||
uint8_t fc_mode = 0; // 燃料电池系统运行模式
|
||||
uint8_t fc_status = 0; // 燃料电池系统运行状态
|
||||
uint16_t fault_level_1 = 0; // 一级故障码
|
||||
uint16_t fault_level_2 = 0; // 二级故障码
|
||||
uint16_t fault_level_3 = 0; // 三级故障码
|
||||
uint16_t fault_level_4 = 0; // 四级故障码
|
||||
uint16_t total_generation_time = 0;// 累积发电时间,0.1h
|
||||
uint8_t fc_fault_level = 0; // 系统故障等级 00~04
|
||||
uint16_t generation_power = 0; // 系统发电功率,0.01kW
|
||||
uint8_t hydrogen_capacity = 0; // 储氢剩余容量 %
|
||||
uint8_t liquid_oxygen_capacity = 0; // 液氧剩余容量 %
|
||||
|
||||
uint8_t fc1_min_cell_voltage = 0; // I#FC最低单片电压,10mV
|
||||
uint8_t fc1_min_cell_pos = 0; // I#FC最低单片电压位置
|
||||
uint8_t fc1_avg_cell_voltage = 0; // I#FC平均单片电压,10mV
|
||||
uint8_t fc2_min_cell_voltage = 0; // 2#FC最低单片电压
|
||||
uint8_t fc2_min_cell_pos = 0; // 2#FC最低单片电压位置
|
||||
uint8_t fc2_avg_cell_voltage = 0; // 2#FC平均单片电压
|
||||
|
||||
uint16_t palladium_temp = 0; // 甲醇制氢装置钯膜最高温度,0.1℃
|
||||
uint16_t buffer_tank_pressure = 0; // 缓冲罐压力,0.1kPa
|
||||
uint16_t flue_total_emission = 0; // 烟气累计排放量,0.1kg
|
||||
uint16_t flue_pressure = 0; // 烟气压力,0.001MPa
|
||||
uint16_t reactor_pressure = 0; // 反应器压力,0.001MPa
|
||||
uint16_t electric_valve_open = 0; // 电动阀开度,0.1%
|
||||
|
||||
uint16_t dcdc1_in_voltage = 0; // DC/DC通道1输入电压,0.1V
|
||||
uint16_t dcdc1_in_current = 0; // DC/DC通道1输入电流,0.1A
|
||||
uint16_t dcdc2_in_voltage = 0; // DC/DC通道2输入电压,0.1V
|
||||
uint16_t dcdc2_in_current = 0; // DC/DC通道2输入电流,0.1A
|
||||
uint16_t dcdc_out_voltage = 0; // DC/DC输出电压,0.1V
|
||||
uint16_t dcdc_out_current = 0; // DC/DC输出电流,0.1A
|
||||
uint8_t dcdc_ctrl_voltage = 0; // DC/DC控制电源电压,0.25V
|
||||
uint8_t dcdc_aux_voltage = 0; // DC/DC辅电输出电压,0.125V
|
||||
|
||||
uint16_t methanol_total_use = 0; // 甲醇累计使用量,0.1kg
|
||||
uint16_t methanol_feed = 0; // 甲醇溶液进料量,1mL/min
|
||||
uint16_t oxygen_side_water_level = 0; // 氧侧生成水箱液位,0.01mm
|
||||
uint16_t hydrogen_side_water_level = 0;// 氢侧生成水箱液位,0.01mm
|
||||
uint16_t ballast_water_level = 0; // 配重水箱液位,0.01mm
|
||||
|
||||
uint16_t exhaust_inlet_pressure = 0; // 尾气装置进气压力,0.01MPa
|
||||
uint16_t exhaust_outlet_pressure = 0; // 尾气装置排气压力,0.01MPa
|
||||
uint16_t cabin_pressure1 = 0; // 舱室压力1,0.01kPa
|
||||
uint16_t cabin_pressure2 = 0; // 舱室压力2,0.01kPa
|
||||
uint16_t cabin_temp1 = 0; // 舱室温度1,0.01℃
|
||||
uint16_t cabin_temp2 = 0; // 舱室温度2,0.01℃
|
||||
uint16_t cabin_humidity1 = 0; // 舱室湿度1,0.01%RH
|
||||
uint16_t cabin_humidity2 = 0; // 舱室湿度2,0.01%RH
|
||||
uint16_t h2_concentration1 = 0; // 舱室H2浓度1,0.01%LEL
|
||||
uint16_t h2_concentration2 = 0; // 舱室H2浓度2,0.01%LEL
|
||||
uint16_t h2_concentration3 = 0; // 舱室H2浓度3,0.01%LEL
|
||||
uint16_t o2_concentration1 = 0; // 舱室O2浓度1,0.01%Vol
|
||||
uint16_t o2_concentration2 = 0; // 舱室O2浓度2,0.01%Vol
|
||||
uint16_t ch3oh_concentration1 = 0; // 舱室甲醇浓度1,0.01%LEL
|
||||
uint16_t ch3oh_concentration2 = 0; // 舱室甲醇浓度2,0.01%LEL
|
||||
|
||||
uint8_t flame_detector1 = 0; // 火焰探测器1状态
|
||||
uint8_t flame_detector2 = 0; // 火焰探测器2状态
|
||||
|
||||
uint16_t emergency_float_depth = 0; // 应急上浮深度,1m
|
||||
uint16_t emergency_float_time = 0; // 应急上浮时间,1min
|
||||
uint16_t exhaust_run_freq = 0; // 尾气装置运行频率,0.01Hz
|
||||
uint16_t exhaust_inlet_temp = 0; // 尾气装置进气温度,0.01℃
|
||||
uint16_t exhaust_outlet_temp = 0; // 尾气装置排气温度,0.01℃
|
||||
uint16_t exhaust_water_in_pressure = 0;// 尾气装置进水压力,0.01kPa
|
||||
uint16_t exhaust_water_out_pressure = 0;// 尾气装置排水压力,0.01kPa
|
||||
uint16_t tank_lo2_pressure = 0; // 一体化罐装置液氧罐压力,0.01MPa
|
||||
uint16_t tank_co2_pressure = 0; // 一体化罐装置二氧化碳压力,0.01MPa
|
||||
uint16_t tank_lo2_level = 0; // 一体化罐装置液氧罐液位,0.01mm
|
||||
uint16_t alloy_h2_flow = 0; // 合金供氢流量,0.01L/min
|
||||
uint16_t fc_h2_flow = 0; // FC供氢流量,0.01L/min
|
||||
uint16_t fc_o2_flow = 0; // FC供氧流量,0.01L/min
|
||||
|
||||
uint16_t reserved[12] = {0}; // 预留1~12
|
||||
|
||||
uint8_t heartbeat = 0; // 通信心跳,每次+1
|
||||
uint8_t emergency_cmd = 0; // 应急指令:Bit0 降载/Bit1 上浮/...
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 消息类
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
class FcControlMessage : public Message {
|
||||
public:
|
||||
uint16_t id() const override { return 0x0001; }
|
||||
const char* name() const override { return "fc_control"; }
|
||||
const ChecksumPolicy& checksum() const override { return m_checksum; }
|
||||
size_t payloadLength() const override { return 14; }
|
||||
|
||||
std::vector<uint8_t> encode(const void* obj) const override;
|
||||
bool decode(const std::vector<uint8_t>& frame, void* obj) const override;
|
||||
|
||||
// 便捷重载
|
||||
std::vector<uint8_t> encode(const FcControlValue& v) const { return encode(&v); }
|
||||
bool decode(const std::vector<uint8_t>& frame, FcControlValue& v) const {
|
||||
return decode(frame, static_cast<void*>(&v));
|
||||
}
|
||||
|
||||
private:
|
||||
ChecksumPolicy m_checksum{ChecksumType::None};
|
||||
};
|
||||
|
||||
class FcStatusMessage : public Message {
|
||||
public:
|
||||
uint16_t id() const override { return 0x0002; }
|
||||
const char* name() const override { return "fc_status"; }
|
||||
const ChecksumPolicy& checksum() const override { return m_checksum; }
|
||||
size_t payloadLength() const override { return 144; }
|
||||
|
||||
std::vector<uint8_t> encode(const void* obj) const override;
|
||||
bool decode(const std::vector<uint8_t>& frame, void* obj) const override;
|
||||
|
||||
std::vector<uint8_t> encode(const FcStatusValue& v) const { return encode(&v); }
|
||||
bool decode(const std::vector<uint8_t>& frame, FcStatusValue& v) const {
|
||||
return decode(frame, static_cast<void*>(&v));
|
||||
}
|
||||
|
||||
private:
|
||||
ChecksumPolicy m_checksum{ChecksumType::None};
|
||||
};
|
||||
|
||||
// 注册 FC 消息到给定注册表(见 MessageRegistry.h)
|
||||
void registerFcMessages(class MessageRegistry& reg);
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_FC_PROTOCOL_H
|
||||
@@ -0,0 +1,58 @@
|
||||
#ifndef PCCU_FIELD_CODEC_H
|
||||
#define PCCU_FIELD_CODEC_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// FieldCodec:小端序(Little-Endian)编解码原语。
|
||||
//
|
||||
// 两条协议(FC 协议 / PM 协议)均采用:
|
||||
// - 多字节数据小端序
|
||||
// - 域起始符 0x40 0x40
|
||||
// - 域字节数 = 整个协议包的数据长度
|
||||
// 本类提供对裸缓冲区的字段读写,不关心具体业务含义。
|
||||
//============================================================================
|
||||
|
||||
class FieldCodec {
|
||||
public:
|
||||
// 在 dst 偏移 offset 处写入单字节
|
||||
static void putU8(std::vector<uint8_t>& dst, size_t offset, uint8_t v) {
|
||||
dst[offset] = v;
|
||||
}
|
||||
// 小端写入 2 字节
|
||||
static void putU16(std::vector<uint8_t>& dst, size_t offset, uint16_t v) {
|
||||
dst[offset] = static_cast<uint8_t>(v & 0xFF);
|
||||
dst[offset + 1] = static_cast<uint8_t>((v >> 8) & 0xFF);
|
||||
}
|
||||
// 小端写入 4 字节
|
||||
static void putU32(std::vector<uint8_t>& dst, size_t offset, uint32_t v) {
|
||||
dst[offset] = static_cast<uint8_t>(v & 0xFF);
|
||||
dst[offset + 1] = static_cast<uint8_t>((v >> 8) & 0xFF);
|
||||
dst[offset + 2] = static_cast<uint8_t>((v >> 16) & 0xFF);
|
||||
dst[offset + 3] = static_cast<uint8_t>((v >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
static uint8_t getU8 (const std::vector<uint8_t>& src, size_t offset) {
|
||||
return src[offset];
|
||||
}
|
||||
static uint16_t getU16(const std::vector<uint8_t>& src, size_t offset) {
|
||||
return static_cast<uint16_t>(src[offset]) |
|
||||
(static_cast<uint16_t>(src[offset + 1]) << 8);
|
||||
}
|
||||
static uint32_t getU32(const std::vector<uint8_t>& src, size_t offset) {
|
||||
return static_cast<uint32_t>(src[offset]) |
|
||||
(static_cast<uint32_t>(src[offset + 1]) << 8) |
|
||||
(static_cast<uint32_t>(src[offset + 2]) << 16) |
|
||||
(static_cast<uint32_t>(src[offset + 3]) << 24);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_FIELD_CODEC_H
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "Frame.h"
|
||||
#include "FieldCodec.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
bool Frame::parseId(const std::vector<uint8_t>& buf, uint16_t& outId) {
|
||||
if (buf.size() < FRAME_HEADER_LEN) return false;
|
||||
if (buf[0] != FRAME_START1 || buf[1] != FRAME_START2) return false;
|
||||
outId = FieldCodec::getU16(buf, 2);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Frame::parseHeader(const std::vector<uint8_t>& buf, FrameHeader& h) {
|
||||
if (buf.size() < FRAME_HEADER_LEN) return false;
|
||||
if (buf[0] != FRAME_START1 || buf[1] != FRAME_START2) return false;
|
||||
h.start1 = buf[0];
|
||||
h.start2 = buf[1];
|
||||
h.id = FieldCodec::getU16(buf, 2);
|
||||
h.length = FieldCodec::getU16(buf, 4);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Frame::build(
|
||||
uint16_t id,
|
||||
const std::vector<uint8_t>& payload,
|
||||
const ChecksumPolicy& checksum) {
|
||||
|
||||
size_t csSize = checksum.size();
|
||||
size_t total = FRAME_HEADER_LEN + payload.size() + csSize;
|
||||
|
||||
std::vector<uint8_t> frame(total, 0);
|
||||
frame[0] = FRAME_START1;
|
||||
frame[1] = FRAME_START2;
|
||||
FieldCodec::putU16(frame, 2, id);
|
||||
FieldCodec::putU16(frame, 4, static_cast<uint16_t>(total)); // 域字节数=整个包长度
|
||||
|
||||
std::copy(payload.begin(), payload.end(), frame.begin() + FRAME_HEADER_LEN);
|
||||
|
||||
if (csSize > 0) {
|
||||
// 校验和:对起始符到校验和之前所有字节求和
|
||||
std::vector<uint8_t> body(frame.begin(), frame.end() - csSize);
|
||||
uint32_t sum = checksum.compute(body);
|
||||
size_t off = frame.size() - csSize;
|
||||
if (csSize == 4) FieldCodec::putU32(frame, off, sum);
|
||||
else FieldCodec::putU16(frame, off, static_cast<uint16_t>(sum));
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef PCCU_FRAME_H
|
||||
#define PCCU_FRAME_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include "ChecksumPolicy.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// Frame:两套协议共用的通用帧模型。
|
||||
//
|
||||
// 两条协议帧结构完全一致:
|
||||
// 域起始符1 (1B) + 域起始符2 (1B) + 域标识符 (2B) + 域字节数 (2B) + 数据域 + 校验和
|
||||
//
|
||||
// - header 域字节数 = 整个协议包的数据长度(含起始符与校验和)
|
||||
// - 小端序
|
||||
// - 校验策略可插拔(见 ChecksumPolicy)
|
||||
//============================================================================
|
||||
|
||||
static constexpr uint8_t FRAME_START1 = 0x40;
|
||||
static constexpr uint8_t FRAME_START2 = 0x40;
|
||||
static constexpr size_t FRAME_HEADER_LEN = 6; // 2B start + 2B id + 2B length
|
||||
|
||||
struct FrameHeader {
|
||||
uint8_t start1;
|
||||
uint8_t start2;
|
||||
uint16_t id;
|
||||
uint16_t length; // 整个协议包的数据长度
|
||||
};
|
||||
|
||||
class Frame {
|
||||
public:
|
||||
Frame() = default;
|
||||
|
||||
// 从原始缓冲解析出消息标识符(不校验完整性)
|
||||
// 返回 true 且 id 有效(起始符正确)时填充 outId
|
||||
static bool parseId(const std::vector<uint8_t>& buf, uint16_t& outId);
|
||||
|
||||
// 解析帧头;返回是否成功且起始符正确
|
||||
static bool parseHeader(const std::vector<uint8_t>& buf, FrameHeader& h);
|
||||
|
||||
// 构造一个完整帧:data 为数据域(不含起始符与校验和),校验和自动追加
|
||||
// 调用方需保证 data.size() >= 帧头之前已经包含起始符+id+length 部分
|
||||
static std::vector<uint8_t> build(
|
||||
uint16_t id,
|
||||
const std::vector<uint8_t>& payload,
|
||||
const ChecksumPolicy& checksum);
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_FRAME_H
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "Message.h"
|
||||
#include "Frame.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
bool Message::validateFrame(const std::vector<uint8_t>& frame,
|
||||
uint16_t expectId,
|
||||
const ChecksumPolicy& policy,
|
||||
size_t expectTotalLen) {
|
||||
if (frame.size() < FRAME_HEADER_LEN) return false;
|
||||
|
||||
FrameHeader h;
|
||||
if (!Frame::parseHeader(frame, h)) return false;
|
||||
if (h.id != expectId) return false;
|
||||
if (h.length != expectTotalLen) return false;
|
||||
|
||||
// 校验和(长度不足由 verify 内部判断)
|
||||
if (!policy.verify(frame)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef PCCU_MESSAGE_H
|
||||
#define PCCU_MESSAGE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "ChecksumPolicy.h"
|
||||
#include "Frame.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// Message:协议消息抽象接口。
|
||||
//
|
||||
// 每条协议消息实现该接口。协议变更时只需:
|
||||
// 1. 修改实现类的字段偏移/长度定义
|
||||
// 2. 修改 encode()/decode() 中字段取值逻辑
|
||||
// 无需改动上层(Comm / Web / DB / 主流程)。
|
||||
//
|
||||
// 提供统一的:
|
||||
// - id()/name() :消息标识与显示名
|
||||
// - checksum() :该校验策略
|
||||
// - payloadLength() :数据域长度(不含 6 字节帧头与校验和)
|
||||
// - encode()/decode() :业务值 <-> 完整帧
|
||||
//============================================================================
|
||||
|
||||
class Message {
|
||||
public:
|
||||
virtual ~Message() = default;
|
||||
|
||||
virtual uint16_t id() const = 0;
|
||||
virtual const char* name() const = 0;
|
||||
virtual const ChecksumPolicy& checksum() const = 0;
|
||||
|
||||
// 数据域长度(不含 6 字节帧头与校验和)
|
||||
virtual size_t payloadLength() const = 0;
|
||||
// 整个协议包长度 = 6 + payloadLength + checksum.size()
|
||||
size_t totalLength() const {
|
||||
return FRAME_HEADER_LEN + payloadLength() + checksum().size();
|
||||
}
|
||||
|
||||
// 业务值编码为完整帧(含帧头+数据域+校验和)
|
||||
// obj 为具体业务对象指针(如 FcStatusValue*)
|
||||
virtual std::vector<uint8_t> encode(const void* obj) const = 0;
|
||||
|
||||
// 从完整帧解析业务值;成功返回 true 并填充 obj
|
||||
virtual bool decode(const std::vector<uint8_t>& frame, void* obj) const = 0;
|
||||
|
||||
// 供实现类解码前做通用校验:起始符 / id / 总长度 / 校验和
|
||||
static bool validateFrame(const std::vector<uint8_t>& frame,
|
||||
uint16_t expectId,
|
||||
const ChecksumPolicy& policy,
|
||||
size_t expectTotalLen);
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_MESSAGE_H
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "MessageRegistry.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
bool MessageRegistry::registerMessage(std::unique_ptr<Message> msg) {
|
||||
if (!msg) return false;
|
||||
uint16_t id = msg->id();
|
||||
if (m_msgs.count(id) != 0) return false; // id 冲突
|
||||
m_msgs[id] = std::move(msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
Message* MessageRegistry::find(uint16_t id) const {
|
||||
auto it = m_msgs.find(id);
|
||||
return (it != m_msgs.end()) ? it->second.get() : nullptr;
|
||||
}
|
||||
|
||||
size_t MessageRegistry::size() const {
|
||||
return m_msgs.size();
|
||||
}
|
||||
|
||||
std::vector<Message*> MessageRegistry::all() const {
|
||||
std::vector<Message*> out;
|
||||
out.reserve(m_msgs.size());
|
||||
for (const auto& kv : m_msgs) out.push_back(kv.second.get());
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef PCCU_MESSAGE_REGISTRY_H
|
||||
#define PCCU_MESSAGE_REGISTRY_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "Message.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// MessageRegistry:消息注册表与收发分发中心。
|
||||
//
|
||||
// 每条链路(FC 链路 / PM 链路)持有各自的注册表实例。
|
||||
// 由于 FC 协议与 PM 协议的域标识符存在重叠(0x0001/0x0002),
|
||||
// 分属不同注册表可完全隔离,互不干扰。
|
||||
//
|
||||
// 协议新增/调整消息时只需 register() 对应 Message 实现,上层无需改动。
|
||||
//============================================================================
|
||||
|
||||
class MessageRegistry {
|
||||
public:
|
||||
// 注册一条消息;返回是否成功(id 冲突时返回 false)
|
||||
bool registerMessage(std::unique_ptr<Message> msg);
|
||||
|
||||
// 按 id 查找消息;未注册返回 nullptr
|
||||
Message* find(uint16_t id) const;
|
||||
|
||||
// 已注册消息数量
|
||||
size_t size() const;
|
||||
|
||||
// 所有已注册消息(按 id 升序)
|
||||
std::vector<Message*> all() const;
|
||||
|
||||
private:
|
||||
std::map<uint16_t, std::unique_ptr<Message>> m_msgs;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_MESSAGE_REGISTRY_H
|
||||
@@ -0,0 +1,662 @@
|
||||
#include "PmProtocol.h"
|
||||
#include "FieldCodec.h"
|
||||
#include "Frame.h"
|
||||
#include "MessageRegistry.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
void registerPmMessages(MessageRegistry& reg) {
|
||||
reg.registerMessage(std::unique_ptr<Message>(new PmControlMessage()));
|
||||
reg.registerMessage(std::unique_ptr<Message>(new PmParamSetMessage()));
|
||||
reg.registerMessage(std::unique_ptr<Message>(new PmParamSetFbMessage()));
|
||||
reg.registerMessage(std::unique_ptr<Message>(new PmStatusMessage()));
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
// 0x0001 操控指令域编解码
|
||||
// 数据域 35 字节,偏移 0~34(对应文档字节 7~41)
|
||||
//============================================================================
|
||||
|
||||
namespace {
|
||||
enum : size_t {
|
||||
PO_YEAR = 0, // u16
|
||||
PO_MONTH = 2,
|
||||
PO_DAY = 3,
|
||||
PO_HOUR = 4,
|
||||
PO_MINUTE = 5,
|
||||
PO_SECOND = 6,
|
||||
PO_MS10 = 7,
|
||||
PO_MODE = 8,
|
||||
PO_CMD = 9,
|
||||
PO_OUTPOWER = 10,
|
||||
PO_PITCH = 11, // 纵倾姿态 数据1=低8位/数据2=高8位 (字节18-19)
|
||||
PO_ROLL = 13, // 横倾姿态 数据1=低8位/数据2=高8位 (字节20-21)
|
||||
PO_EMER_ALLOW = 15,
|
||||
PO_DEPTH = 16, // u16
|
||||
PO_SUPPLY_CMD = 18,
|
||||
PO_RESV_CMD5 = 19,
|
||||
PO_RESV_CMD6 = 20,
|
||||
PO_INS_BAT_CMD = 21,
|
||||
PO_DYN_BAT_CMD = 22,
|
||||
PO_DYN_BAT_PWR = 23, // u16
|
||||
PO_HEARTBEAT = 25,
|
||||
PO_HOST_STATE = 26,
|
||||
PO_RESV1 = 27, // u32
|
||||
PO_RESV2 = 31, // u32
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> PmControlMessage::encode(const void* obj) const {
|
||||
const PmControlValue* v = static_cast<const PmControlValue*>(obj);
|
||||
std::vector<uint8_t> p(payloadLength(), 0);
|
||||
|
||||
FieldCodec::putU16(p, PO_YEAR, v->year);
|
||||
FieldCodec::putU8(p, PO_MONTH, v->month);
|
||||
FieldCodec::putU8(p, PO_DAY, v->day);
|
||||
FieldCodec::putU8(p, PO_HOUR, v->hour);
|
||||
FieldCodec::putU8(p, PO_MINUTE, v->minute);
|
||||
FieldCodec::putU8(p, PO_SECOND, v->second);
|
||||
FieldCodec::putU8(p, PO_MS10, v->millisecond10);
|
||||
FieldCodec::putU8(p, PO_MODE, v->mode);
|
||||
FieldCodec::putU8(p, PO_CMD, v->cmd);
|
||||
FieldCodec::putU8(p, PO_OUTPOWER, v->outputPower);
|
||||
FieldCodec::putU16(p, PO_PITCH, static_cast<uint16_t>(v->pitch)); // 数据1=低位/数据2=高位
|
||||
FieldCodec::putU16(p, PO_ROLL, static_cast<uint16_t>(v->roll));
|
||||
FieldCodec::putU8(p, PO_EMER_ALLOW, v->emergencyAllow);
|
||||
FieldCodec::putU16(p, PO_DEPTH, v->depth);
|
||||
FieldCodec::putU8(p, PO_SUPPLY_CMD, v->supplyCmd);
|
||||
FieldCodec::putU8(p, PO_RESV_CMD5, v->reservedCmd5);
|
||||
FieldCodec::putU8(p, PO_RESV_CMD6, v->reservedCmd6);
|
||||
FieldCodec::putU8(p, PO_INS_BAT_CMD, v->insBatCmd);
|
||||
FieldCodec::putU8(p, PO_DYN_BAT_CMD, v->dynBatCmd);
|
||||
FieldCodec::putU16(p, PO_DYN_BAT_PWR, v->dynBatPower);
|
||||
FieldCodec::putU8(p, PO_HEARTBEAT, v->heartbeat);
|
||||
FieldCodec::putU8(p, PO_HOST_STATE, v->hostState);
|
||||
FieldCodec::putU32(p, PO_RESV1, v->reserved1);
|
||||
FieldCodec::putU32(p, PO_RESV2, v->reserved2);
|
||||
|
||||
return Frame::build(id(), p, checksum());
|
||||
}
|
||||
|
||||
bool PmControlMessage::decode(const std::vector<uint8_t>& frame, void* obj) const {
|
||||
if (!validateFrame(frame, id(), checksum(), totalLength())) return false;
|
||||
PmControlValue* v = static_cast<PmControlValue*>(obj);
|
||||
size_t o = FRAME_HEADER_LEN;
|
||||
|
||||
v->year = FieldCodec::getU16(frame, o + PO_YEAR);
|
||||
v->month = FieldCodec::getU8(frame, o + PO_MONTH);
|
||||
v->day = FieldCodec::getU8(frame, o + PO_DAY);
|
||||
v->hour = FieldCodec::getU8(frame, o + PO_HOUR);
|
||||
v->minute = FieldCodec::getU8(frame, o + PO_MINUTE);
|
||||
v->second = FieldCodec::getU8(frame, o + PO_SECOND);
|
||||
v->millisecond10 = FieldCodec::getU8(frame, o + PO_MS10);
|
||||
v->mode = FieldCodec::getU8(frame, o + PO_MODE);
|
||||
v->cmd = FieldCodec::getU8(frame, o + PO_CMD);
|
||||
v->outputPower = FieldCodec::getU8(frame, o + PO_OUTPOWER);
|
||||
v->pitch = static_cast<int16_t>(FieldCodec::getU16(frame, o + PO_PITCH));
|
||||
v->roll = static_cast<int16_t>(FieldCodec::getU16(frame, o + PO_ROLL));
|
||||
v->emergencyAllow= FieldCodec::getU8(frame, o + PO_EMER_ALLOW);
|
||||
v->depth = FieldCodec::getU16(frame, o + PO_DEPTH);
|
||||
v->supplyCmd = FieldCodec::getU8(frame, o + PO_SUPPLY_CMD);
|
||||
v->reservedCmd5 = FieldCodec::getU8(frame, o + PO_RESV_CMD5);
|
||||
v->reservedCmd6 = FieldCodec::getU8(frame, o + PO_RESV_CMD6);
|
||||
v->insBatCmd = FieldCodec::getU8(frame, o + PO_INS_BAT_CMD);
|
||||
v->dynBatCmd = FieldCodec::getU8(frame, o + PO_DYN_BAT_CMD);
|
||||
v->dynBatPower = FieldCodec::getU16(frame, o + PO_DYN_BAT_PWR);
|
||||
v->heartbeat = FieldCodec::getU8(frame, o + PO_HEARTBEAT);
|
||||
v->hostState = FieldCodec::getU8(frame, o + PO_HOST_STATE);
|
||||
v->reserved1 = FieldCodec::getU32(frame, o + PO_RESV1);
|
||||
v->reserved2 = FieldCodec::getU32(frame, o + PO_RESV2);
|
||||
return true;
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
// 0x0002 参数设定指令域
|
||||
// 数据域 18 字节,偏移 0~17
|
||||
//============================================================================
|
||||
|
||||
namespace {
|
||||
enum : size_t {
|
||||
PP_INS_H1 = 0,
|
||||
PP_INS_H2 = 1,
|
||||
PP_INS_H3 = 2,
|
||||
PP_INS_PWR = 3,
|
||||
PP_INS_RESV = 4,
|
||||
PP_DYN_H1 = 5,
|
||||
PP_DYN_H2 = 6,
|
||||
PP_DYN_H3 = 7,
|
||||
PP_DYN_PWR = 8,
|
||||
PP_DYN_RESV = 9,
|
||||
PP_RESV1 = 10, // u32
|
||||
PP_RESV2 = 14, // u32
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> PmParamSetMessage::encode(const void* obj) const {
|
||||
const PmParamSetValue* v = static_cast<const PmParamSetValue*>(obj);
|
||||
std::vector<uint8_t> p(payloadLength(), 0);
|
||||
|
||||
FieldCodec::putU8(p, PP_INS_H1, v->insSocHold1);
|
||||
FieldCodec::putU8(p, PP_INS_H2, v->insSocHold2);
|
||||
FieldCodec::putU8(p, PP_INS_H3, v->insSocHold3);
|
||||
FieldCodec::putU8(p, PP_INS_PWR, v->insOutputPower);
|
||||
FieldCodec::putU8(p, PP_INS_RESV, v->insReserved);
|
||||
FieldCodec::putU8(p, PP_DYN_H1, v->dynSocHold1);
|
||||
FieldCodec::putU8(p, PP_DYN_H2, v->dynSocHold2);
|
||||
FieldCodec::putU8(p, PP_DYN_H3, v->dynSocHold3);
|
||||
FieldCodec::putU8(p, PP_DYN_PWR, v->dynOutputPower);
|
||||
FieldCodec::putU8(p, PP_DYN_RESV, v->dynReserved);
|
||||
FieldCodec::putU32(p, PP_RESV1, v->reserved1);
|
||||
FieldCodec::putU32(p, PP_RESV2, v->reserved2);
|
||||
|
||||
return Frame::build(id(), p, checksum());
|
||||
}
|
||||
|
||||
bool PmParamSetMessage::decode(const std::vector<uint8_t>& frame, void* obj) const {
|
||||
if (!validateFrame(frame, id(), checksum(), totalLength())) return false;
|
||||
PmParamSetValue* v = static_cast<PmParamSetValue*>(obj);
|
||||
size_t o = FRAME_HEADER_LEN;
|
||||
|
||||
v->insSocHold1 = FieldCodec::getU8(frame, o + PP_INS_H1);
|
||||
v->insSocHold2 = FieldCodec::getU8(frame, o + PP_INS_H2);
|
||||
v->insSocHold3 = FieldCodec::getU8(frame, o + PP_INS_H3);
|
||||
v->insOutputPower = FieldCodec::getU8(frame, o + PP_INS_PWR);
|
||||
v->insReserved = FieldCodec::getU8(frame, o + PP_INS_RESV);
|
||||
v->dynSocHold1 = FieldCodec::getU8(frame, o + PP_DYN_H1);
|
||||
v->dynSocHold2 = FieldCodec::getU8(frame, o + PP_DYN_H2);
|
||||
v->dynSocHold3 = FieldCodec::getU8(frame, o + PP_DYN_H3);
|
||||
v->dynOutputPower = FieldCodec::getU8(frame, o + PP_DYN_PWR);
|
||||
v->dynReserved = FieldCodec::getU8(frame, o + PP_DYN_RESV);
|
||||
v->reserved1 = FieldCodec::getU32(frame, o + PP_RESV1);
|
||||
v->reserved2 = FieldCodec::getU32(frame, o + PP_RESV2);
|
||||
return true;
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
// 0x0003 参数设定反馈域
|
||||
// 数据域 2 字节
|
||||
//============================================================================
|
||||
|
||||
std::vector<uint8_t> PmParamSetFbMessage::encode(const void* obj) const {
|
||||
const PmParamSetFbValue* v = static_cast<const PmParamSetFbValue*>(obj);
|
||||
std::vector<uint8_t> p(payloadLength(), 0);
|
||||
FieldCodec::putU8(p, 0, v->flag);
|
||||
FieldCodec::putU8(p, 1, v->failCode);
|
||||
return Frame::build(id(), p, checksum());
|
||||
}
|
||||
|
||||
bool PmParamSetFbMessage::decode(const std::vector<uint8_t>& frame, void* obj) const {
|
||||
if (!validateFrame(frame, id(), checksum(), totalLength())) return false;
|
||||
PmParamSetFbValue* v = static_cast<PmParamSetFbValue*>(obj);
|
||||
size_t o = FRAME_HEADER_LEN;
|
||||
v->flag = FieldCodec::getU8(frame, o + 0);
|
||||
v->failCode = FieldCodec::getU8(frame, o + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
// 0x0004 状态报文域
|
||||
// 数据域 238 字节,偏移 0~237(对应文档字节 7~244)
|
||||
//============================================================================
|
||||
|
||||
namespace {
|
||||
enum : size_t {
|
||||
PS_MODE = 0,
|
||||
PS_STATUS = 1,
|
||||
PS_FL1 = 2, // u16
|
||||
PS_FL2 = 4,
|
||||
PS_FL3 = 6,
|
||||
PS_FL4 = 8,
|
||||
PS_GEN_TIME = 10, // u16
|
||||
PS_FAULT_LEVEL = 12,
|
||||
PS_OUT_POWER_LIMIT = 13, // u16
|
||||
PS_GEN_POWER = 15, // u16
|
||||
PS_H2_CAP = 17,
|
||||
PS_LO2_CAP = 18,
|
||||
PS_FC1_MIN_V = 19,
|
||||
PS_FC1_MIN_POS = 20,
|
||||
PS_FC1_AVG_V = 21,
|
||||
PS_FC2_MIN_V = 22,
|
||||
PS_FC2_MIN_POS = 23,
|
||||
PS_FC2_AVG_V = 24,
|
||||
PS_PALLADIUM = 25, // u16
|
||||
PS_BUFFER_PRES = 27,
|
||||
PS_FLUE_TOTAL = 29,
|
||||
PS_FLUE_PRES = 31,
|
||||
PS_REACTOR_PRES = 33,
|
||||
PS_EVALVE_OPEN = 35,
|
||||
PS_MAIN_PIPE_PRES = 37,
|
||||
PS_AUX_PIPE_PRES = 39,
|
||||
PS_DCDC1_IN_V = 41,
|
||||
PS_DCDC1_IN_I = 43,
|
||||
PS_DCDC2_IN_V = 45,
|
||||
PS_DCDC2_IN_I = 47,
|
||||
PS_DCDC_OUT_V = 49,
|
||||
PS_DCDC_OUT_I = 51,
|
||||
PS_DCDC_CTRL_V = 53,
|
||||
PS_DCDC_AUX_V = 54,
|
||||
PS_METHANOL_TOTAL = 55, // u16
|
||||
PS_METHANOL_FEED = 57,
|
||||
PS_O2_WATER = 59,
|
||||
PS_H2_WATER = 61,
|
||||
PS_BALLAST_WATER = 63,
|
||||
PS_EXH_IN_PRES = 65,
|
||||
PS_EXH_OUT_PRES = 67,
|
||||
PS_EXH_FREQ = 69,
|
||||
PS_EXH_IN_TEMP = 71,
|
||||
PS_EXH_OUT_TEMP = 73,
|
||||
PS_EXH_WIN_PRES = 75,
|
||||
PS_EXH_WOUT_PRES = 77,
|
||||
PS_TANK_LO2_PRES = 79,
|
||||
PS_TANK_CO2_PRES = 81,
|
||||
PS_TANK_LO2_LEVEL = 83,
|
||||
PS_ALLOY_H2_FLOW = 85,
|
||||
PS_FC_H2_FLOW = 87,
|
||||
PS_FC_O2_FLOW = 89,
|
||||
PS_EMER_DEPTH = 91, // u16
|
||||
PS_EMER_TIME = 93,
|
||||
PS_CABIN_P1 = 95,
|
||||
PS_CABIN_P2 = 97,
|
||||
PS_CABIN_T1 = 99,
|
||||
PS_CABIN_T2 = 101,
|
||||
PS_CABIN_H1 = 103,
|
||||
PS_CABIN_H2 = 105,
|
||||
PS_H2_C1 = 107,
|
||||
PS_H2_C2 = 109,
|
||||
PS_H2_C3 = 111,
|
||||
PS_O2_C1 = 113,
|
||||
PS_O2_C2 = 115,
|
||||
PS_CH3OH_C1 = 117,
|
||||
PS_CH3OH_C2 = 119,
|
||||
PS_FLAME1 = 121,
|
||||
PS_FLAME2 = 122,
|
||||
PS_RESV1 = 123, // u16
|
||||
PS_RESV2 = 125, // u16
|
||||
PS_EB1_VOLTAGE = 127, // u16
|
||||
PS_EB1_CURRENT = 129,
|
||||
PS_EB1_MAX_TEMP = 131,
|
||||
PS_EB1_FAULT = 133,
|
||||
PS_EB2_VOLTAGE = 135,
|
||||
PS_EB2_CURRENT = 137,
|
||||
PS_EB2_MAX_TEMP = 139,
|
||||
PS_EB2_FAULT = 141,
|
||||
PS_INS_CABIN_OX = 143, // u16
|
||||
PS_INS_CABIN_TEMP = 145,
|
||||
PS_INS_CABIN_HUM = 147,
|
||||
PS_INS_CABIN_PRES = 149,
|
||||
PS_DYN_CABIN_OX = 151,
|
||||
PS_DYN_CABIN_TEMP = 153,
|
||||
PS_DYN_CABIN_HUM = 155,
|
||||
PS_DYN_CABIN_PRES = 157,
|
||||
PS_RESV3 = 159, // u8
|
||||
PS_DYN_ALARM = 160, // u8 * 6
|
||||
PS_INS_ALARM = 166, // u8 * 6
|
||||
PS_INS_RELAY1 = 172,
|
||||
PS_INS_RELAY2 = 173,
|
||||
PS_DYN_RELAY1 = 174,
|
||||
PS_DYN_RELAY2 = 175,
|
||||
PS_INS_MAX_PWR = 176, // u16
|
||||
PS_DYN_MAX_PWR = 178, // u16
|
||||
PS_INS_SOC = 180,
|
||||
PS_DYN_SOC = 181,
|
||||
PS_INS_TOTAL_ENERGY= 182, // u16
|
||||
PS_DYN_TOTAL_ENERGY= 184, // u16
|
||||
PS_INS_PWR_IN = 186, // u16
|
||||
PS_DYN_PWR_IN = 188, // u16
|
||||
PS_INS_CHARGE_ST = 190,
|
||||
PS_DYN_CHARGE_ST = 191,
|
||||
PS_INS_V_LINK = 192, // u16
|
||||
PS_INS_V_PACK = 194, // u16
|
||||
PS_INS_CURRENT = 196, // u16
|
||||
PS_INS_RES_POS = 198, // u16
|
||||
PS_INS_RES_NEG = 200, // u16
|
||||
PS_DYN_V_LINK = 202, // u16
|
||||
PS_DYN_V_PACK = 204, // u16
|
||||
PS_DYN_CURRENT = 206, // u16
|
||||
PS_DYN_RES_POS = 208, // u16
|
||||
PS_DYN_RES_NEG = 210, // u16
|
||||
PS_INS_EMERGENCY = 212,
|
||||
PS_DYN_EMERGENCY = 213,
|
||||
PS_INS_SOC_H1 = 214,
|
||||
PS_INS_SOC_H2 = 215,
|
||||
PS_INS_SOC_H3 = 216,
|
||||
PS_INS_PWR_L1 = 217,
|
||||
PS_INS_PWR_L2 = 218,
|
||||
PS_DYN_SOC_H1 = 219,
|
||||
PS_DYN_SOC_H2 = 220,
|
||||
PS_DYN_SOC_H3 = 221,
|
||||
PS_DYN_PWR_L1 = 222,
|
||||
PS_DYN_PWR_L2 = 223,
|
||||
PS_ONLINE_FLAG1 = 224, // u32
|
||||
PS_ONLINE_FLAG2 = 228, // u32
|
||||
PS_RESV4 = 232, // u32
|
||||
PS_HEARTBEAT = 236,
|
||||
PS_EMERGENCY_CMD = 237,
|
||||
};
|
||||
inline void getU8Arr(const std::vector<uint8_t>& f, size_t off, uint8_t* dst, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i) dst[i] = f[off + i];
|
||||
}
|
||||
inline void putU8Arr(std::vector<uint8_t>& p, size_t off, const uint8_t* src, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i) p[off + i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> PmStatusMessage::encode(const void* obj) const {
|
||||
const PmStatusValue* v = static_cast<const PmStatusValue*>(obj);
|
||||
std::vector<uint8_t> p(payloadLength(), 0);
|
||||
|
||||
FieldCodec::putU8(p, PS_MODE, v->fc_mode);
|
||||
FieldCodec::putU8(p, PS_STATUS, v->fc_status);
|
||||
FieldCodec::putU16(p, PS_FL1, v->fault_level_1);
|
||||
FieldCodec::putU16(p, PS_FL2, v->fault_level_2);
|
||||
FieldCodec::putU16(p, PS_FL3, v->fault_level_3);
|
||||
FieldCodec::putU16(p, PS_FL4, v->fault_level_4);
|
||||
FieldCodec::putU16(p, PS_GEN_TIME, v->total_generation_time);
|
||||
FieldCodec::putU8(p, PS_FAULT_LEVEL, v->fc_fault_level);
|
||||
FieldCodec::putU16(p, PS_OUT_POWER_LIMIT, v->output_power_limit);
|
||||
FieldCodec::putU16(p, PS_GEN_POWER, v->generation_power);
|
||||
FieldCodec::putU8(p, PS_H2_CAP, v->hydrogen_capacity);
|
||||
FieldCodec::putU8(p, PS_LO2_CAP, v->liquid_oxygen_capacity);
|
||||
|
||||
FieldCodec::putU8(p, PS_FC1_MIN_V, v->fc1_min_cell_voltage);
|
||||
FieldCodec::putU8(p, PS_FC1_MIN_POS, v->fc1_min_cell_pos);
|
||||
FieldCodec::putU8(p, PS_FC1_AVG_V, v->fc1_avg_cell_voltage);
|
||||
FieldCodec::putU8(p, PS_FC2_MIN_V, v->fc2_min_cell_voltage);
|
||||
FieldCodec::putU8(p, PS_FC2_MIN_POS, v->fc2_min_cell_pos);
|
||||
FieldCodec::putU8(p, PS_FC2_AVG_V, v->fc2_avg_cell_voltage);
|
||||
|
||||
FieldCodec::putU16(p, PS_PALLADIUM, v->palladium_temp);
|
||||
FieldCodec::putU16(p, PS_BUFFER_PRES, v->buffer_tank_pressure);
|
||||
FieldCodec::putU16(p, PS_FLUE_TOTAL, v->flue_total_emission);
|
||||
FieldCodec::putU16(p, PS_FLUE_PRES, v->flue_pressure);
|
||||
FieldCodec::putU16(p, PS_REACTOR_PRES, v->reactor_pressure);
|
||||
FieldCodec::putU16(p, PS_EVALVE_OPEN, v->electric_valve_open);
|
||||
FieldCodec::putU16(p, PS_MAIN_PIPE_PRES, v->main_pipe_pressure);
|
||||
FieldCodec::putU16(p, PS_AUX_PIPE_PRES, v->aux_pipe_pressure);
|
||||
|
||||
FieldCodec::putU16(p, PS_DCDC1_IN_V, v->dcdc1_in_voltage);
|
||||
FieldCodec::putU16(p, PS_DCDC1_IN_I, v->dcdc1_in_current);
|
||||
FieldCodec::putU16(p, PS_DCDC2_IN_V, v->dcdc2_in_voltage);
|
||||
FieldCodec::putU16(p, PS_DCDC2_IN_I, v->dcdc2_in_current);
|
||||
FieldCodec::putU16(p, PS_DCDC_OUT_V, v->dcdc_out_voltage);
|
||||
FieldCodec::putU16(p, PS_DCDC_OUT_I, v->dcdc_out_current);
|
||||
FieldCodec::putU8(p, PS_DCDC_CTRL_V, v->dcdc_ctrl_voltage);
|
||||
FieldCodec::putU8(p, PS_DCDC_AUX_V, v->dcdc_aux_voltage);
|
||||
|
||||
FieldCodec::putU16(p, PS_METHANOL_TOTAL, v->methanol_total_use);
|
||||
FieldCodec::putU16(p, PS_METHANOL_FEED, v->methanol_feed);
|
||||
FieldCodec::putU16(p, PS_O2_WATER, v->oxygen_side_water_level);
|
||||
FieldCodec::putU16(p, PS_H2_WATER, v->hydrogen_side_water_level);
|
||||
FieldCodec::putU16(p, PS_BALLAST_WATER, v->ballast_water_level);
|
||||
|
||||
FieldCodec::putU16(p, PS_EXH_IN_PRES, v->exhaust_inlet_pressure);
|
||||
FieldCodec::putU16(p, PS_EXH_OUT_PRES, v->exhaust_outlet_pressure);
|
||||
FieldCodec::putU16(p, PS_EXH_FREQ, v->exhaust_run_freq);
|
||||
FieldCodec::putU16(p, PS_EXH_IN_TEMP, v->exhaust_inlet_temp);
|
||||
FieldCodec::putU16(p, PS_EXH_OUT_TEMP, v->exhaust_outlet_temp);
|
||||
FieldCodec::putU16(p, PS_EXH_WIN_PRES, v->exhaust_water_in_pressure);
|
||||
FieldCodec::putU16(p, PS_EXH_WOUT_PRES, v->exhaust_water_out_pressure);
|
||||
|
||||
FieldCodec::putU16(p, PS_TANK_LO2_PRES, v->tank_lo2_pressure);
|
||||
FieldCodec::putU16(p, PS_TANK_CO2_PRES, v->tank_co2_pressure);
|
||||
FieldCodec::putU16(p, PS_TANK_LO2_LEVEL, v->tank_lo2_level);
|
||||
FieldCodec::putU16(p, PS_ALLOY_H2_FLOW, v->alloy_h2_flow);
|
||||
FieldCodec::putU16(p, PS_FC_H2_FLOW, v->fc_h2_flow);
|
||||
FieldCodec::putU16(p, PS_FC_O2_FLOW, v->fc_o2_flow);
|
||||
|
||||
FieldCodec::putU16(p, PS_EMER_DEPTH, v->emergency_float_depth);
|
||||
FieldCodec::putU16(p, PS_EMER_TIME, v->emergency_float_time);
|
||||
|
||||
FieldCodec::putU16(p, PS_CABIN_P1, v->cabin_pressure1);
|
||||
FieldCodec::putU16(p, PS_CABIN_P2, v->cabin_pressure2);
|
||||
FieldCodec::putU16(p, PS_CABIN_T1, v->cabin_temp1);
|
||||
FieldCodec::putU16(p, PS_CABIN_T2, v->cabin_temp2);
|
||||
FieldCodec::putU16(p, PS_CABIN_H1, v->cabin_humidity1);
|
||||
FieldCodec::putU16(p, PS_CABIN_H2, v->cabin_humidity2);
|
||||
FieldCodec::putU16(p, PS_H2_C1, v->h2_concentration1);
|
||||
FieldCodec::putU16(p, PS_H2_C2, v->h2_concentration2);
|
||||
FieldCodec::putU16(p, PS_H2_C3, v->h2_concentration3);
|
||||
FieldCodec::putU16(p, PS_O2_C1, v->o2_concentration1);
|
||||
FieldCodec::putU16(p, PS_O2_C2, v->o2_concentration2);
|
||||
FieldCodec::putU16(p, PS_CH3OH_C1, v->ch3oh_concentration1);
|
||||
FieldCodec::putU16(p, PS_CH3OH_C2, v->ch3oh_concentration2);
|
||||
|
||||
FieldCodec::putU8(p, PS_FLAME1, v->flame_detector1);
|
||||
FieldCodec::putU8(p, PS_FLAME2, v->flame_detector2);
|
||||
FieldCodec::putU16(p, PS_RESV1, v->pmReserved1);
|
||||
FieldCodec::putU16(p, PS_RESV2, v->pmReserved2);
|
||||
|
||||
FieldCodec::putU16(p, PS_EB1_VOLTAGE, v->emergency_battery1_voltage);
|
||||
FieldCodec::putU16(p, PS_EB1_CURRENT, v->emergency_battery1_current);
|
||||
FieldCodec::putU16(p, PS_EB1_MAX_TEMP, v->emergency_battery1_max_temp);
|
||||
FieldCodec::putU16(p, PS_EB1_FAULT, v->emergency_battery1_fault_word);
|
||||
FieldCodec::putU16(p, PS_EB2_VOLTAGE, v->emergency_battery2_voltage);
|
||||
FieldCodec::putU16(p, PS_EB2_CURRENT, v->emergency_battery2_current);
|
||||
FieldCodec::putU16(p, PS_EB2_MAX_TEMP, v->emergency_battery2_max_temp);
|
||||
FieldCodec::putU16(p, PS_EB2_FAULT, v->emergency_battery2_fault_word);
|
||||
|
||||
FieldCodec::putU16(p, PS_INS_CABIN_OX, v->ins_cabin_ox_concentration);
|
||||
FieldCodec::putU16(p, PS_INS_CABIN_TEMP, v->ins_cabin_temperature);
|
||||
FieldCodec::putU16(p, PS_INS_CABIN_HUM, v->ins_cabin_humidity);
|
||||
FieldCodec::putU16(p, PS_INS_CABIN_PRES, v->ins_cabin_pressure);
|
||||
FieldCodec::putU16(p, PS_DYN_CABIN_OX, v->dyn_cabin_ox_concentration);
|
||||
FieldCodec::putU16(p, PS_DYN_CABIN_TEMP, v->dyn_cabin_temperature);
|
||||
FieldCodec::putU16(p, PS_DYN_CABIN_HUM, v->dyn_cabin_humidity);
|
||||
FieldCodec::putU16(p, PS_DYN_CABIN_PRES, v->dyn_cabin_pressure);
|
||||
FieldCodec::putU8(p, PS_RESV3, v->pmReserved3);
|
||||
|
||||
putU8Arr(p, PS_DYN_ALARM, v->dyn_alarm_flag, 6);
|
||||
putU8Arr(p, PS_INS_ALARM, v->ins_alarm_flag, 6);
|
||||
|
||||
FieldCodec::putU8(p, PS_INS_RELAY1, v->ins_relay_status1);
|
||||
FieldCodec::putU8(p, PS_INS_RELAY2, v->ins_relay_status2);
|
||||
FieldCodec::putU8(p, PS_DYN_RELAY1, v->dyn_relay_status1);
|
||||
FieldCodec::putU8(p, PS_DYN_RELAY2, v->dyn_relay_status2);
|
||||
|
||||
FieldCodec::putU16(p, PS_INS_MAX_PWR, v->ins_max_discharge_power);
|
||||
FieldCodec::putU16(p, PS_DYN_MAX_PWR, v->dyn_max_discharge_power);
|
||||
FieldCodec::putU8(p, PS_INS_SOC, v->ins_soc);
|
||||
FieldCodec::putU8(p, PS_DYN_SOC, v->dyn_soc);
|
||||
FieldCodec::putU16(p, PS_INS_TOTAL_ENERGY, v->ins_total_energy);
|
||||
FieldCodec::putU16(p, PS_DYN_TOTAL_ENERGY, v->dyn_total_energy);
|
||||
FieldCodec::putU16(p, PS_INS_PWR_IN, v->ins_power_input);
|
||||
FieldCodec::putU16(p, PS_DYN_PWR_IN, v->dyn_power_input);
|
||||
|
||||
FieldCodec::putU8(p, PS_INS_CHARGE_ST, v->ins_charge_status);
|
||||
FieldCodec::putU8(p, PS_DYN_CHARGE_ST, v->dyn_charge_status);
|
||||
|
||||
FieldCodec::putU16(p, PS_INS_V_LINK, v->ins_voltage_link);
|
||||
FieldCodec::putU16(p, PS_INS_V_PACK, v->ins_voltage_pack);
|
||||
FieldCodec::putU16(p, PS_INS_CURRENT, v->ins_current);
|
||||
FieldCodec::putU16(p, PS_INS_RES_POS, v->ins_resistance_pos);
|
||||
FieldCodec::putU16(p, PS_INS_RES_NEG, v->ins_resistance_neg);
|
||||
FieldCodec::putU16(p, PS_DYN_V_LINK, v->dyn_voltage_link);
|
||||
FieldCodec::putU16(p, PS_DYN_V_PACK, v->dyn_voltage_pack);
|
||||
FieldCodec::putU16(p, PS_DYN_CURRENT, v->dyn_current);
|
||||
FieldCodec::putU16(p, PS_DYN_RES_POS, v->dyn_resistance_pos);
|
||||
FieldCodec::putU16(p, PS_DYN_RES_NEG, v->dyn_resistance_neg);
|
||||
|
||||
FieldCodec::putU8(p, PS_INS_EMERGENCY, v->ins_emergency_status);
|
||||
FieldCodec::putU8(p, PS_DYN_EMERGENCY, v->dyn_emergency_status);
|
||||
|
||||
FieldCodec::putU8(p, PS_INS_SOC_H1, v->ins_soc_threshold1);
|
||||
FieldCodec::putU8(p, PS_INS_SOC_H2, v->ins_soc_threshold2);
|
||||
FieldCodec::putU8(p, PS_INS_SOC_H3, v->ins_soc_threshold3);
|
||||
FieldCodec::putU8(p, PS_INS_PWR_L1, v->ins_power_limit1);
|
||||
FieldCodec::putU8(p, PS_INS_PWR_L2, v->ins_power_limit2);
|
||||
FieldCodec::putU8(p, PS_DYN_SOC_H1, v->dyn_soc_threshold1);
|
||||
FieldCodec::putU8(p, PS_DYN_SOC_H2, v->dyn_soc_threshold2);
|
||||
FieldCodec::putU8(p, PS_DYN_SOC_H3, v->dyn_soc_threshold3);
|
||||
FieldCodec::putU8(p, PS_DYN_PWR_L1, v->dyn_power_limit1);
|
||||
FieldCodec::putU8(p, PS_DYN_PWR_L2, v->dyn_power_limit2);
|
||||
|
||||
FieldCodec::putU32(p, PS_ONLINE_FLAG1, v->device_online_flag1);
|
||||
FieldCodec::putU32(p, PS_ONLINE_FLAG2, v->device_online_flag2);
|
||||
FieldCodec::putU32(p, PS_RESV4, v->pmReserved4);
|
||||
|
||||
FieldCodec::putU8(p, PS_HEARTBEAT, v->heartbeat);
|
||||
FieldCodec::putU8(p, PS_EMERGENCY_CMD, v->emergency_cmd);
|
||||
|
||||
return Frame::build(id(), p, checksum());
|
||||
}
|
||||
|
||||
bool PmStatusMessage::decode(const std::vector<uint8_t>& frame, void* obj) const {
|
||||
if (!validateFrame(frame, id(), checksum(), totalLength())) return false;
|
||||
PmStatusValue* v = static_cast<PmStatusValue*>(obj);
|
||||
size_t o = FRAME_HEADER_LEN;
|
||||
|
||||
v->fc_mode = FieldCodec::getU8(frame, o + PS_MODE);
|
||||
v->fc_status = FieldCodec::getU8(frame, o + PS_STATUS);
|
||||
v->fault_level_1 = FieldCodec::getU16(frame, o + PS_FL1);
|
||||
v->fault_level_2 = FieldCodec::getU16(frame, o + PS_FL2);
|
||||
v->fault_level_3 = FieldCodec::getU16(frame, o + PS_FL3);
|
||||
v->fault_level_4 = FieldCodec::getU16(frame, o + PS_FL4);
|
||||
v->total_generation_time = FieldCodec::getU16(frame, o + PS_GEN_TIME);
|
||||
v->fc_fault_level = FieldCodec::getU8(frame, o + PS_FAULT_LEVEL);
|
||||
v->output_power_limit = FieldCodec::getU16(frame, o + PS_OUT_POWER_LIMIT);
|
||||
v->generation_power = FieldCodec::getU16(frame, o + PS_GEN_POWER);
|
||||
v->hydrogen_capacity = FieldCodec::getU8(frame, o + PS_H2_CAP);
|
||||
v->liquid_oxygen_capacity = FieldCodec::getU8(frame, o + PS_LO2_CAP);
|
||||
|
||||
v->fc1_min_cell_voltage = FieldCodec::getU8(frame, o + PS_FC1_MIN_V);
|
||||
v->fc1_min_cell_pos = FieldCodec::getU8(frame, o + PS_FC1_MIN_POS);
|
||||
v->fc1_avg_cell_voltage = FieldCodec::getU8(frame, o + PS_FC1_AVG_V);
|
||||
v->fc2_min_cell_voltage = FieldCodec::getU8(frame, o + PS_FC2_MIN_V);
|
||||
v->fc2_min_cell_pos = FieldCodec::getU8(frame, o + PS_FC2_MIN_POS);
|
||||
v->fc2_avg_cell_voltage = FieldCodec::getU8(frame, o + PS_FC2_AVG_V);
|
||||
|
||||
v->palladium_temp = FieldCodec::getU16(frame, o + PS_PALLADIUM);
|
||||
v->buffer_tank_pressure = FieldCodec::getU16(frame, o + PS_BUFFER_PRES);
|
||||
v->flue_total_emission = FieldCodec::getU16(frame, o + PS_FLUE_TOTAL);
|
||||
v->flue_pressure = FieldCodec::getU16(frame, o + PS_FLUE_PRES);
|
||||
v->reactor_pressure = FieldCodec::getU16(frame, o + PS_REACTOR_PRES);
|
||||
v->electric_valve_open = FieldCodec::getU16(frame, o + PS_EVALVE_OPEN);
|
||||
v->main_pipe_pressure = FieldCodec::getU16(frame, o + PS_MAIN_PIPE_PRES);
|
||||
v->aux_pipe_pressure = FieldCodec::getU16(frame, o + PS_AUX_PIPE_PRES);
|
||||
|
||||
v->dcdc1_in_voltage = FieldCodec::getU16(frame, o + PS_DCDC1_IN_V);
|
||||
v->dcdc1_in_current = FieldCodec::getU16(frame, o + PS_DCDC1_IN_I);
|
||||
v->dcdc2_in_voltage = FieldCodec::getU16(frame, o + PS_DCDC2_IN_V);
|
||||
v->dcdc2_in_current = FieldCodec::getU16(frame, o + PS_DCDC2_IN_I);
|
||||
v->dcdc_out_voltage = FieldCodec::getU16(frame, o + PS_DCDC_OUT_V);
|
||||
v->dcdc_out_current = FieldCodec::getU16(frame, o + PS_DCDC_OUT_I);
|
||||
v->dcdc_ctrl_voltage = FieldCodec::getU8(frame, o + PS_DCDC_CTRL_V);
|
||||
v->dcdc_aux_voltage = FieldCodec::getU8(frame, o + PS_DCDC_AUX_V);
|
||||
|
||||
v->methanol_total_use = FieldCodec::getU16(frame, o + PS_METHANOL_TOTAL);
|
||||
v->methanol_feed = FieldCodec::getU16(frame, o + PS_METHANOL_FEED);
|
||||
v->oxygen_side_water_level = FieldCodec::getU16(frame, o + PS_O2_WATER);
|
||||
v->hydrogen_side_water_level = FieldCodec::getU16(frame, o + PS_H2_WATER);
|
||||
v->ballast_water_level = FieldCodec::getU16(frame, o + PS_BALLAST_WATER);
|
||||
|
||||
v->exhaust_inlet_pressure = FieldCodec::getU16(frame, o + PS_EXH_IN_PRES);
|
||||
v->exhaust_outlet_pressure = FieldCodec::getU16(frame, o + PS_EXH_OUT_PRES);
|
||||
v->exhaust_run_freq = FieldCodec::getU16(frame, o + PS_EXH_FREQ);
|
||||
v->exhaust_inlet_temp = FieldCodec::getU16(frame, o + PS_EXH_IN_TEMP);
|
||||
v->exhaust_outlet_temp = FieldCodec::getU16(frame, o + PS_EXH_OUT_TEMP);
|
||||
v->exhaust_water_in_pressure = FieldCodec::getU16(frame, o + PS_EXH_WIN_PRES);
|
||||
v->exhaust_water_out_pressure = FieldCodec::getU16(frame, o + PS_EXH_WOUT_PRES);
|
||||
|
||||
v->tank_lo2_pressure = FieldCodec::getU16(frame, o + PS_TANK_LO2_PRES);
|
||||
v->tank_co2_pressure = FieldCodec::getU16(frame, o + PS_TANK_CO2_PRES);
|
||||
v->tank_lo2_level = FieldCodec::getU16(frame, o + PS_TANK_LO2_LEVEL);
|
||||
v->alloy_h2_flow = FieldCodec::getU16(frame, o + PS_ALLOY_H2_FLOW);
|
||||
v->fc_h2_flow = FieldCodec::getU16(frame, o + PS_FC_H2_FLOW);
|
||||
v->fc_o2_flow = FieldCodec::getU16(frame, o + PS_FC_O2_FLOW);
|
||||
|
||||
v->emergency_float_depth = FieldCodec::getU16(frame, o + PS_EMER_DEPTH);
|
||||
v->emergency_float_time = FieldCodec::getU16(frame, o + PS_EMER_TIME);
|
||||
|
||||
v->cabin_pressure1 = FieldCodec::getU16(frame, o + PS_CABIN_P1);
|
||||
v->cabin_pressure2 = FieldCodec::getU16(frame, o + PS_CABIN_P2);
|
||||
v->cabin_temp1 = FieldCodec::getU16(frame, o + PS_CABIN_T1);
|
||||
v->cabin_temp2 = FieldCodec::getU16(frame, o + PS_CABIN_T2);
|
||||
v->cabin_humidity1 = FieldCodec::getU16(frame, o + PS_CABIN_H1);
|
||||
v->cabin_humidity2 = FieldCodec::getU16(frame, o + PS_CABIN_H2);
|
||||
v->h2_concentration1 = FieldCodec::getU16(frame, o + PS_H2_C1);
|
||||
v->h2_concentration2 = FieldCodec::getU16(frame, o + PS_H2_C2);
|
||||
v->h2_concentration3 = FieldCodec::getU16(frame, o + PS_H2_C3);
|
||||
v->o2_concentration1 = FieldCodec::getU16(frame, o + PS_O2_C1);
|
||||
v->o2_concentration2 = FieldCodec::getU16(frame, o + PS_O2_C2);
|
||||
v->ch3oh_concentration1 = FieldCodec::getU16(frame, o + PS_CH3OH_C1);
|
||||
v->ch3oh_concentration2 = FieldCodec::getU16(frame, o + PS_CH3OH_C2);
|
||||
|
||||
v->flame_detector1 = FieldCodec::getU8(frame, o + PS_FLAME1);
|
||||
v->flame_detector2 = FieldCodec::getU8(frame, o + PS_FLAME2);
|
||||
v->pmReserved1 = FieldCodec::getU16(frame, o + PS_RESV1);
|
||||
v->pmReserved2 = FieldCodec::getU16(frame, o + PS_RESV2);
|
||||
|
||||
v->emergency_battery1_voltage = FieldCodec::getU16(frame, o + PS_EB1_VOLTAGE);
|
||||
v->emergency_battery1_current = FieldCodec::getU16(frame, o + PS_EB1_CURRENT);
|
||||
v->emergency_battery1_max_temp = FieldCodec::getU16(frame, o + PS_EB1_MAX_TEMP);
|
||||
v->emergency_battery1_fault_word = FieldCodec::getU16(frame, o + PS_EB1_FAULT);
|
||||
v->emergency_battery2_voltage = FieldCodec::getU16(frame, o + PS_EB2_VOLTAGE);
|
||||
v->emergency_battery2_current = FieldCodec::getU16(frame, o + PS_EB2_CURRENT);
|
||||
v->emergency_battery2_max_temp = FieldCodec::getU16(frame, o + PS_EB2_MAX_TEMP);
|
||||
v->emergency_battery2_fault_word = FieldCodec::getU16(frame, o + PS_EB2_FAULT);
|
||||
|
||||
v->ins_cabin_ox_concentration = FieldCodec::getU16(frame, o + PS_INS_CABIN_OX);
|
||||
v->ins_cabin_temperature = FieldCodec::getU16(frame, o + PS_INS_CABIN_TEMP);
|
||||
v->ins_cabin_humidity = FieldCodec::getU16(frame, o + PS_INS_CABIN_HUM);
|
||||
v->ins_cabin_pressure = FieldCodec::getU16(frame, o + PS_INS_CABIN_PRES);
|
||||
v->dyn_cabin_ox_concentration = FieldCodec::getU16(frame, o + PS_DYN_CABIN_OX);
|
||||
v->dyn_cabin_temperature = FieldCodec::getU16(frame, o + PS_DYN_CABIN_TEMP);
|
||||
v->dyn_cabin_humidity = FieldCodec::getU16(frame, o + PS_DYN_CABIN_HUM);
|
||||
v->dyn_cabin_pressure = FieldCodec::getU16(frame, o + PS_DYN_CABIN_PRES);
|
||||
v->pmReserved3 = FieldCodec::getU8(frame, o + PS_RESV3);
|
||||
|
||||
getU8Arr(frame, o + PS_DYN_ALARM, v->dyn_alarm_flag, 6);
|
||||
getU8Arr(frame, o + PS_INS_ALARM, v->ins_alarm_flag, 6);
|
||||
|
||||
v->ins_relay_status1 = FieldCodec::getU8(frame, o + PS_INS_RELAY1);
|
||||
v->ins_relay_status2 = FieldCodec::getU8(frame, o + PS_INS_RELAY2);
|
||||
v->dyn_relay_status1 = FieldCodec::getU8(frame, o + PS_DYN_RELAY1);
|
||||
v->dyn_relay_status2 = FieldCodec::getU8(frame, o + PS_DYN_RELAY2);
|
||||
|
||||
v->ins_max_discharge_power = FieldCodec::getU16(frame, o + PS_INS_MAX_PWR);
|
||||
v->dyn_max_discharge_power = FieldCodec::getU16(frame, o + PS_DYN_MAX_PWR);
|
||||
v->ins_soc = FieldCodec::getU8(frame, o + PS_INS_SOC);
|
||||
v->dyn_soc = FieldCodec::getU8(frame, o + PS_DYN_SOC);
|
||||
v->ins_total_energy = FieldCodec::getU16(frame, o + PS_INS_TOTAL_ENERGY);
|
||||
v->dyn_total_energy = FieldCodec::getU16(frame, o + PS_DYN_TOTAL_ENERGY);
|
||||
v->ins_power_input = FieldCodec::getU16(frame, o + PS_INS_PWR_IN);
|
||||
v->dyn_power_input = FieldCodec::getU16(frame, o + PS_DYN_PWR_IN);
|
||||
|
||||
v->ins_charge_status = FieldCodec::getU8(frame, o + PS_INS_CHARGE_ST);
|
||||
v->dyn_charge_status = FieldCodec::getU8(frame, o + PS_DYN_CHARGE_ST);
|
||||
|
||||
v->ins_voltage_link = FieldCodec::getU16(frame, o + PS_INS_V_LINK);
|
||||
v->ins_voltage_pack = FieldCodec::getU16(frame, o + PS_INS_V_PACK);
|
||||
v->ins_current = FieldCodec::getU16(frame, o + PS_INS_CURRENT);
|
||||
v->ins_resistance_pos = FieldCodec::getU16(frame, o + PS_INS_RES_POS);
|
||||
v->ins_resistance_neg = FieldCodec::getU16(frame, o + PS_INS_RES_NEG);
|
||||
v->dyn_voltage_link = FieldCodec::getU16(frame, o + PS_DYN_V_LINK);
|
||||
v->dyn_voltage_pack = FieldCodec::getU16(frame, o + PS_DYN_V_PACK);
|
||||
v->dyn_current = FieldCodec::getU16(frame, o + PS_DYN_CURRENT);
|
||||
v->dyn_resistance_pos = FieldCodec::getU16(frame, o + PS_DYN_RES_POS);
|
||||
v->dyn_resistance_neg = FieldCodec::getU16(frame, o + PS_DYN_RES_NEG);
|
||||
|
||||
v->ins_emergency_status = FieldCodec::getU8(frame, o + PS_INS_EMERGENCY);
|
||||
v->dyn_emergency_status = FieldCodec::getU8(frame, o + PS_DYN_EMERGENCY);
|
||||
|
||||
v->ins_soc_threshold1 = FieldCodec::getU8(frame, o + PS_INS_SOC_H1);
|
||||
v->ins_soc_threshold2 = FieldCodec::getU8(frame, o + PS_INS_SOC_H2);
|
||||
v->ins_soc_threshold3 = FieldCodec::getU8(frame, o + PS_INS_SOC_H3);
|
||||
v->ins_power_limit1 = FieldCodec::getU8(frame, o + PS_INS_PWR_L1);
|
||||
v->ins_power_limit2 = FieldCodec::getU8(frame, o + PS_INS_PWR_L2);
|
||||
v->dyn_soc_threshold1 = FieldCodec::getU8(frame, o + PS_DYN_SOC_H1);
|
||||
v->dyn_soc_threshold2 = FieldCodec::getU8(frame, o + PS_DYN_SOC_H2);
|
||||
v->dyn_soc_threshold3 = FieldCodec::getU8(frame, o + PS_DYN_SOC_H3);
|
||||
v->dyn_power_limit1 = FieldCodec::getU8(frame, o + PS_DYN_PWR_L1);
|
||||
v->dyn_power_limit2 = FieldCodec::getU8(frame, o + PS_DYN_PWR_L2);
|
||||
|
||||
v->device_online_flag1 = FieldCodec::getU32(frame, o + PS_ONLINE_FLAG1);
|
||||
v->device_online_flag2 = FieldCodec::getU32(frame, o + PS_ONLINE_FLAG2);
|
||||
v->pmReserved4 = FieldCodec::getU32(frame, o + PS_RESV4);
|
||||
|
||||
v->heartbeat = FieldCodec::getU8(frame, o + PS_HEARTBEAT);
|
||||
v->emergency_cmd = FieldCodec::getU8(frame, o + PS_EMERGENCY_CMD);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,317 @@
|
||||
#ifndef PCCU_PM_PROTOCOL_H
|
||||
#define PCCU_PM_PROTOCOL_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include "Message.h"
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// PM 协议(复合管控器 <-> 控制主机 pPowerManger)
|
||||
// 依据:docs/控制主机与复合管控器通信协议 - 0824.xlsx
|
||||
//
|
||||
// 帧格式统一:0x40 0x40 + 域标识符(2B) + 域字节数(2B) + 数据域 + uint32 校验和
|
||||
// 校验和为从域起始符到校验和之前所有数据的字节和。
|
||||
//
|
||||
// 四条消息(均带 uint32 校验和):
|
||||
// 0x0001 操控指令域 (控制主机->复合管控器)payload 35B,总长 45B
|
||||
// 0x0002 参数设定指令域 (控制主机->复合管控器)payload 18B,总长 28B
|
||||
// 0x0003 参数设定反馈域 (复合管控器->控制主机)payload 2B,总长 12B
|
||||
// 0x0004 状态报文域 (复合管控器->控制主机)payload 238B,总长 248B
|
||||
//============================================================================
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 0x0001 操控指令域
|
||||
// payload 35 字节,对应文档字节 7~41
|
||||
//--------------------------------------------------------------------------
|
||||
struct PmControlValue {
|
||||
// 系统时间
|
||||
uint16_t year = 0;
|
||||
uint8_t month = 0;
|
||||
uint8_t day = 0;
|
||||
uint8_t hour = 0;
|
||||
uint8_t minute = 0;
|
||||
uint8_t second = 0;
|
||||
uint8_t millisecond10 = 0; // 10毫秒
|
||||
// 指令
|
||||
uint8_t mode = 0; // 模式设定 00~03
|
||||
uint8_t cmd = 0; // 操控指令 00~0B
|
||||
uint8_t outputPower = 0; // 输出功率指令 0~250,0.1kW
|
||||
int16_t pitch = 0; // 纵倾姿态(数据1=低8位 / 数据2=高8位,int16),0.1°
|
||||
int16_t roll = 0; // 横倾姿态(数据1=低8位 / 数据2=高8位,int16),0.1°
|
||||
uint8_t emergencyAllow = 0; // 应急允许
|
||||
uint16_t depth = 0; // 潜深深度 m
|
||||
uint8_t supplyCmd = 0; // 补给/排放指令
|
||||
uint8_t reservedCmd5 = 0; // 预留指令5
|
||||
uint8_t reservedCmd6 = 0; // 预留指令6
|
||||
uint8_t insBatCmd = 0; // 仪表锂电池启停指令 00/10/20
|
||||
uint8_t dynBatCmd = 0; // 动力锂电池启停指令 00/10/20
|
||||
uint16_t dynBatPower = 0; // 动力锂电池功率配置值 kW
|
||||
uint8_t heartbeat = 0; // 通信心跳,每次+1
|
||||
uint8_t hostState = 0; // 主机状态 运行AAH/关机FFH/故障其他
|
||||
uint32_t reserved1 = 0; // 预留
|
||||
uint32_t reserved2 = 0; // 预留
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 0x0002 参数设定指令域
|
||||
// payload 18 字节,对应文档字节 7~24
|
||||
//--------------------------------------------------------------------------
|
||||
struct PmParamSetValue {
|
||||
uint8_t insSocHold1 = 0; // 仪表电池充电一级功率SOC门限
|
||||
uint8_t insSocHold2 = 0; // 仪表电池充电二级功率SOC门限
|
||||
uint8_t insSocHold3 = 0; // 仪表电池充电三级功率SOC门限
|
||||
uint8_t insOutputPower = 0; // 仪表锂电池输出功率 %
|
||||
uint8_t insReserved = 0; // 预留
|
||||
uint8_t dynSocHold1 = 0; // 动力电池充电一级功率SOC门限
|
||||
uint8_t dynSocHold2 = 0; // 动力电池充电二级功率SOC门限
|
||||
uint8_t dynSocHold3 = 0; // 动力电池充电三级功率SOC门限
|
||||
uint8_t dynOutputPower = 0; // 动力电池输出功率 %
|
||||
uint8_t dynReserved = 0; // 预留
|
||||
uint32_t reserved1 = 0; // 预留
|
||||
uint32_t reserved2 = 0; // 预留
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 0x0003 参数设定反馈域
|
||||
// payload 2 字节,对应文档字节 7~8
|
||||
//--------------------------------------------------------------------------
|
||||
struct PmParamSetFbValue {
|
||||
uint8_t flag = 0; // 设定标志:00H无效 / 10H成功 / 20H失败
|
||||
uint8_t failCode = 0; // 设定失败原因
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 0x0004 状态报文域
|
||||
// payload 238 字节,对应文档字节 7~244
|
||||
//--------------------------------------------------------------------------
|
||||
struct PmStatusValue {
|
||||
uint8_t fc_mode = 0; // 燃料电池系统运行模式
|
||||
uint8_t fc_status = 0; // 燃料电池系统运行状态
|
||||
uint16_t fault_level_1 = 0; // 一级故障码
|
||||
uint16_t fault_level_2 = 0; // 二级故障码
|
||||
uint16_t fault_level_3 = 0; // 三级故障码
|
||||
uint16_t fault_level_4 = 0; // 四级故障码
|
||||
uint16_t total_generation_time = 0; // 累积发电时间 0.1h
|
||||
uint8_t fc_fault_level = 0; // 系统故障等级 00~04
|
||||
uint16_t output_power_limit = 0; // 系统输出功率限定功率 W
|
||||
uint16_t generation_power = 0; // 系统发电功率 0.01kW
|
||||
uint8_t hydrogen_capacity = 0; // 储氢剩余容量 %
|
||||
uint8_t liquid_oxygen_capacity = 0; // 液氧剩余容量 %
|
||||
|
||||
uint8_t fc1_min_cell_voltage = 0; // I#FC最低单片电压 10mV
|
||||
uint8_t fc1_min_cell_pos = 0; // I#FC最低单片电压位置
|
||||
uint8_t fc1_avg_cell_voltage = 0; // I#FC平均单片电压
|
||||
uint8_t fc2_min_cell_voltage = 0; // 2#FC最低单片电压
|
||||
uint8_t fc2_min_cell_pos = 0; // 2#FC最低单片电压位置
|
||||
uint8_t fc2_avg_cell_voltage = 0; // 2#FC平均单片电压
|
||||
|
||||
uint16_t palladium_temp = 0; // 甲醇制氢装置钯膜最高温度 0.1℃
|
||||
uint16_t buffer_tank_pressure = 0; // 缓冲罐压力 0.1kPa
|
||||
uint16_t flue_total_emission = 0; // 烟气累计排放量 0.1kg
|
||||
uint16_t flue_pressure = 0; // 烟气压力 0.001MPa
|
||||
uint16_t reactor_pressure = 0; // 反应器压力 0.001MPa
|
||||
uint16_t electric_valve_open = 0; // 电动阀开度 0.1%
|
||||
uint16_t main_pipe_pressure = 0; // 主水路盘管侧压力 0.01kPa
|
||||
uint16_t aux_pipe_pressure = 0; // 辅水路盘管侧压力 0.01kPa
|
||||
|
||||
uint16_t dcdc1_in_voltage = 0; // DC/DC通道1输入电压 0.1V
|
||||
uint16_t dcdc1_in_current = 0; // DC/DC通道1输入电流 0.1A
|
||||
uint16_t dcdc2_in_voltage = 0; // DC/DC通道2输入电压 0.1V
|
||||
uint16_t dcdc2_in_current = 0; // DC/DC通道2输入电流 0.1A
|
||||
uint16_t dcdc_out_voltage = 0; // DC/DC输出电压 0.1V
|
||||
uint16_t dcdc_out_current = 0; // DC/DC输出电流 0.1A
|
||||
uint8_t dcdc_ctrl_voltage = 0; // DC/DC控制电源电压 0.25V
|
||||
uint8_t dcdc_aux_voltage = 0; // DC/DC辅电输出电压 0.125V
|
||||
|
||||
uint16_t methanol_total_use = 0; // 甲醇累计使用量 0.1kg
|
||||
uint16_t methanol_feed = 0; // 甲醇溶液进料量 1mL/min
|
||||
uint16_t oxygen_side_water_level = 0; // 氧侧生成水箱液位 0.01mm
|
||||
uint16_t hydrogen_side_water_level = 0; // 氢侧生成水箱液位
|
||||
uint16_t ballast_water_level = 0; // 配重水箱液位
|
||||
|
||||
uint16_t exhaust_inlet_pressure = 0; // 尾气装置进气压力 0.01MPa
|
||||
uint16_t exhaust_outlet_pressure = 0; // 尾气装置排气压力 0.01MPa
|
||||
uint16_t exhaust_run_freq = 0; // 尾气装置运行频率 0.01Hz
|
||||
uint16_t exhaust_inlet_temp = 0; // 尾气装置进气温度 0.01℃
|
||||
uint16_t exhaust_outlet_temp = 0; // 尾气装置排气温度 0.01℃
|
||||
uint16_t exhaust_water_in_pressure = 0; // 尾气装置进水压力 0.01kPa
|
||||
uint16_t exhaust_water_out_pressure = 0; // 尾气装置排水压力 0.01kPa
|
||||
|
||||
uint16_t tank_lo2_pressure = 0; // 一体化罐装置液氧罐压力 0.01MPa
|
||||
uint16_t tank_co2_pressure = 0; // 一体化罐装置二氧化碳压力 0.01MPa
|
||||
uint16_t tank_lo2_level = 0; // 一体化罐装置液氧罐液位 0.01mm
|
||||
|
||||
uint16_t alloy_h2_flow = 0; // 合金供氢流量 0.01L/min
|
||||
uint16_t fc_h2_flow = 0; // FC供氢流量 0.01L/min
|
||||
uint16_t fc_o2_flow = 0; // FC供氧流量 0.01L/min
|
||||
|
||||
uint16_t emergency_float_depth = 0; // 应急上浮深度 1m
|
||||
uint16_t emergency_float_time = 0; // 应急上浮时间 1min
|
||||
|
||||
uint16_t cabin_pressure1 = 0; // 舱室压力1 0.01kPa
|
||||
uint16_t cabin_pressure2 = 0; // 舱室压力2
|
||||
uint16_t cabin_temp1 = 0; // 舱室温度1 0.01℃
|
||||
uint16_t cabin_temp2 = 0; // 舱室温度2
|
||||
uint16_t cabin_humidity1 = 0; // 舱室湿度1 0.01%RH
|
||||
uint16_t cabin_humidity2 = 0; // 舱室湿度2
|
||||
uint16_t h2_concentration1 = 0; // 舱室H2浓度1 0.01%LEL
|
||||
uint16_t h2_concentration2 = 0; // 舱室H2浓度2
|
||||
uint16_t h2_concentration3 = 0; // 舱室H2浓度3
|
||||
uint16_t o2_concentration1 = 0; // 舱室O2浓度1 0.01%Vol
|
||||
uint16_t o2_concentration2 = 0; // 舱室O2浓度2
|
||||
uint16_t ch3oh_concentration1 = 0; // 舱室甲醇浓度1 0.01%LEL
|
||||
uint16_t ch3oh_concentration2 = 0; // 舱室甲醇浓度2
|
||||
|
||||
uint8_t flame_detector1 = 0; // 火焰探测器1状态
|
||||
uint8_t flame_detector2 = 0; // 火焰探测器2状态
|
||||
uint16_t pmReserved1 = 0; // 预留
|
||||
uint16_t pmReserved2 = 0; // 预留
|
||||
|
||||
uint16_t emergency_battery1_voltage = 0; // 应急电池1总电压
|
||||
uint16_t emergency_battery1_current = 0; // 应急电池1总电流
|
||||
uint16_t emergency_battery1_max_temp = 0; // 应急电池1最高温度
|
||||
uint16_t emergency_battery1_fault_word = 0;// 应急电池1故障字
|
||||
uint16_t emergency_battery2_voltage = 0; // 应急电池2总电压
|
||||
uint16_t emergency_battery2_current = 0; // 应急电池2总电流
|
||||
uint16_t emergency_battery2_max_temp = 0; // 应急电池2最高温度
|
||||
uint16_t emergency_battery2_fault_word = 0;// 应急电池2故障字
|
||||
|
||||
uint16_t ins_cabin_ox_concentration = 0; // 仪表舱内氧气浓度 %
|
||||
uint16_t ins_cabin_temperature = 0; // 仪表舱内温度 ℃
|
||||
uint16_t ins_cabin_humidity = 0; // 仪表舱内湿度 %RH
|
||||
uint16_t ins_cabin_pressure = 0; // 仪表舱内大气压 kPa
|
||||
uint16_t dyn_cabin_ox_concentration = 0; // 动力舱内氧气浓度 %
|
||||
uint16_t dyn_cabin_temperature = 0; // 动力舱内温度 ℃
|
||||
uint16_t dyn_cabin_humidity = 0; // 动力舱内湿度 %RH
|
||||
uint16_t dyn_cabin_pressure = 0; // 动力舱内大气压 kPa
|
||||
uint8_t pmReserved3 = 0; // 预留
|
||||
|
||||
uint8_t dyn_alarm_flag[6] = {0}; // 动力锂电池报警标识字1~6
|
||||
uint8_t ins_alarm_flag[6] = {0}; // 仪表锂电池报警标识字1~6
|
||||
|
||||
uint8_t ins_relay_status1 = 0; // 仪表电池正极+充电继电器状态
|
||||
uint8_t ins_relay_status2 = 0; // 仪表电池预充+负极继电器状态
|
||||
uint8_t dyn_relay_status1 = 0; // 动力电池正极+充电继电器状态
|
||||
uint8_t dyn_relay_status2 = 0; // 动力电池预充+负极继电器状态
|
||||
|
||||
uint16_t ins_max_discharge_power = 0; // 仪表电池允许最高放电功率 0.05kW
|
||||
uint16_t dyn_max_discharge_power = 0; // 动力电池允许最高放电功率 0.05kW
|
||||
uint8_t ins_soc = 0; // 仪表锂电池SOC %
|
||||
uint8_t dyn_soc = 0; // 动力锂电池SOC %
|
||||
uint16_t ins_total_energy = 0; // 仪表锂电池总能量 0.1kWh
|
||||
uint16_t dyn_total_energy = 0; // 动力锂电池总电量 0.1kWh
|
||||
uint16_t ins_power_input = 0; // 仪表电池当前接入功率 kW
|
||||
uint16_t dyn_power_input = 0; // 动力电池当前接入功率 kW
|
||||
|
||||
uint8_t ins_charge_status = 0; // 仪表电池充电状态
|
||||
uint8_t dyn_charge_status = 0; // 动力电池充电状态
|
||||
|
||||
uint16_t ins_voltage_link = 0; // 仪表电池电压LINK端 0.01V
|
||||
uint16_t ins_voltage_pack = 0; // 仪表电池电压PACK端 0.01V
|
||||
uint16_t ins_current = 0; // 仪表电池放电电流 0.05A
|
||||
uint16_t ins_resistance_pos = 0; // 仪表电池正端绝缘电阻 kΩ
|
||||
uint16_t ins_resistance_neg = 0; // 仪表电池负端绝缘电阻 kΩ
|
||||
uint16_t dyn_voltage_link = 0; // 动力电池电压LINK端 0.01V
|
||||
uint16_t dyn_voltage_pack = 0; // 动力电池电压PACK端 0.01V
|
||||
uint16_t dyn_current = 0; // 动力电池放电电流 0.05A
|
||||
uint16_t dyn_resistance_pos = 0; // 动力电池正端绝缘电阻 kΩ
|
||||
uint16_t dyn_resistance_neg = 0; // 动力电池负端绝缘电阻 kΩ
|
||||
|
||||
uint8_t ins_emergency_status = 0; // 仪表电池紧急状态
|
||||
uint8_t dyn_emergency_status = 0; // 动力电池紧急状态
|
||||
|
||||
uint8_t ins_soc_threshold1 = 0; // 仪表电池充电一级功率SOC门限当前值
|
||||
uint8_t ins_soc_threshold2 = 0; // 仪表电池充电二级
|
||||
uint8_t ins_soc_threshold3 = 0; // 仪表电池充电三级
|
||||
uint8_t ins_power_limit1 = 0; // 仪表通路FC输出功率一级限制当前值
|
||||
uint8_t ins_power_limit2 = 0; // 仪表通路FC输出功率二级限制当前值
|
||||
uint8_t dyn_soc_threshold1 = 0; // 动力电池充电一级功率SOC门限当前值
|
||||
uint8_t dyn_soc_threshold2 = 0; // 动力电池充电二级
|
||||
uint8_t dyn_soc_threshold3 = 0; // 动力电池充电三级
|
||||
uint8_t dyn_power_limit1 = 0; // 动力通路FC输出功率一级限制当前值
|
||||
uint8_t dyn_power_limit2 = 0; // 动力通路FC输出功率二级限制当前值
|
||||
|
||||
uint32_t device_online_flag1 = 0; // 设备在线状态标志字1
|
||||
uint32_t device_online_flag2 = 0; // 设备在线状态标志字2
|
||||
uint32_t pmReserved4 = 0; // 预留
|
||||
|
||||
uint8_t heartbeat = 0; // 通信心跳,每次+1
|
||||
uint8_t emergency_cmd = 0; // 应急指令
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 消息类
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
class PmControlMessage : public Message {
|
||||
public:
|
||||
uint16_t id() const override { return 0x0001; }
|
||||
const char* name() const override { return "pm_control"; }
|
||||
const ChecksumPolicy& checksum() const override { return m_checksum; }
|
||||
size_t payloadLength() const override { return 35; }
|
||||
std::vector<uint8_t> encode(const void* obj) const override;
|
||||
bool decode(const std::vector<uint8_t>& frame, void* obj) const override;
|
||||
std::vector<uint8_t> encode(const PmControlValue& v) const { return encode(&v); }
|
||||
bool decode(const std::vector<uint8_t>& frame, PmControlValue& v) const {
|
||||
return decode(frame, static_cast<void*>(&v));
|
||||
}
|
||||
private:
|
||||
ChecksumPolicy m_checksum{ChecksumType::Sum32};
|
||||
};
|
||||
|
||||
class PmParamSetMessage : public Message {
|
||||
public:
|
||||
uint16_t id() const override { return 0x0002; }
|
||||
const char* name() const override { return "pm_param_set"; }
|
||||
const ChecksumPolicy& checksum() const override { return m_checksum; }
|
||||
size_t payloadLength() const override { return 18; }
|
||||
std::vector<uint8_t> encode(const void* obj) const override;
|
||||
bool decode(const std::vector<uint8_t>& frame, void* obj) const override;
|
||||
std::vector<uint8_t> encode(const PmParamSetValue& v) const { return encode(&v); }
|
||||
bool decode(const std::vector<uint8_t>& frame, PmParamSetValue& v) const {
|
||||
return decode(frame, static_cast<void*>(&v));
|
||||
}
|
||||
private:
|
||||
ChecksumPolicy m_checksum{ChecksumType::Sum32};
|
||||
};
|
||||
|
||||
class PmParamSetFbMessage : public Message {
|
||||
public:
|
||||
uint16_t id() const override { return 0x0003; }
|
||||
const char* name() const override { return "pm_param_set_fb"; }
|
||||
const ChecksumPolicy& checksum() const override { return m_checksum; }
|
||||
size_t payloadLength() const override { return 2; }
|
||||
std::vector<uint8_t> encode(const void* obj) const override;
|
||||
bool decode(const std::vector<uint8_t>& frame, void* obj) const override;
|
||||
std::vector<uint8_t> encode(const PmParamSetFbValue& v) const { return encode(&v); }
|
||||
bool decode(const std::vector<uint8_t>& frame, PmParamSetFbValue& v) const {
|
||||
return decode(frame, static_cast<void*>(&v));
|
||||
}
|
||||
private:
|
||||
ChecksumPolicy m_checksum{ChecksumType::Sum32};
|
||||
};
|
||||
|
||||
class PmStatusMessage : public Message {
|
||||
public:
|
||||
uint16_t id() const override { return 0x0004; }
|
||||
const char* name() const override { return "pm_status"; }
|
||||
const ChecksumPolicy& checksum() const override { return m_checksum; }
|
||||
size_t payloadLength() const override { return 238; }
|
||||
std::vector<uint8_t> encode(const void* obj) const override;
|
||||
bool decode(const std::vector<uint8_t>& frame, void* obj) const override;
|
||||
std::vector<uint8_t> encode(const PmStatusValue& v) const { return encode(&v); }
|
||||
bool decode(const std::vector<uint8_t>& frame, PmStatusValue& v) const {
|
||||
return decode(frame, static_cast<void*>(&v));
|
||||
}
|
||||
private:
|
||||
ChecksumPolicy m_checksum{ChecksumType::Sum32};
|
||||
};
|
||||
|
||||
// 注册 PM 消息到给定注册表
|
||||
void registerPmMessages(class MessageRegistry& reg);
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_PM_PROTOCOL_H
|
||||
@@ -0,0 +1,178 @@
|
||||
#include "DbStore.h"
|
||||
#include "../protocol/Message.h"
|
||||
#include "sqlite3.h"
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
namespace {
|
||||
std::string toHex(const std::vector<uint8_t>& data) {
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
std::string s;
|
||||
s.reserve(data.size() * 2);
|
||||
for (uint8_t b : data) {
|
||||
s += hex[(b >> 4) & 0xF];
|
||||
s += hex[b & 0xF];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
DbStore::DbStore(const std::string& dbPath) : m_dbPath(dbPath) {}
|
||||
|
||||
DbStore::~DbStore() {
|
||||
close();
|
||||
}
|
||||
|
||||
bool DbStore::exec(const char* sql) {
|
||||
char* err = nullptr;
|
||||
int rc = sqlite3_exec(m_db, sql, nullptr, nullptr, &err);
|
||||
if (rc != SQLITE_OK) {
|
||||
m_lastError = err ? err : "sqlite error";
|
||||
sqlite3_free(err);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DbStore::open() {
|
||||
if (m_db) return true;
|
||||
if (sqlite3_open(m_dbPath.c_str(), &m_db) != SQLITE_OK) {
|
||||
m_lastError = sqlite3_errmsg(m_db);
|
||||
sqlite3_close(m_db);
|
||||
m_db = nullptr;
|
||||
return false;
|
||||
}
|
||||
sqlite3_busy_timeout(m_db, 2000);
|
||||
exec("PRAGMA journal_mode=WAL;");
|
||||
exec("PRAGMA synchronous=NORMAL;");
|
||||
|
||||
// comm_log:收发全部原始帧
|
||||
if (!exec(
|
||||
"CREATE TABLE IF NOT EXISTS comm_log ("
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
" time REAL NOT NULL,"
|
||||
" link TEXT NOT NULL,"
|
||||
" direction INTEGER NOT NULL," // 0=收 1=发
|
||||
" msg_id INTEGER NOT NULL,"
|
||||
" msg_name TEXT NOT NULL,"
|
||||
" length INTEGER NOT NULL,"
|
||||
" checksum_ok INTEGER NOT NULL,"
|
||||
" hex TEXT NOT NULL"
|
||||
");")) return false;
|
||||
exec("CREATE INDEX IF NOT EXISTS idx_comm_log_time ON comm_log(time);");
|
||||
|
||||
return prepareInsert();
|
||||
}
|
||||
|
||||
bool DbStore::prepareInsert() {
|
||||
const char* sql =
|
||||
"INSERT INTO comm_log (time, link, direction, msg_id, msg_name, length, checksum_ok, hex) "
|
||||
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8);";
|
||||
if (sqlite3_prepare_v2(m_db, sql, -1, &m_stmtInsert, nullptr) != SQLITE_OK) {
|
||||
m_lastError = sqlite3_errmsg(m_db);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DbStore::close() {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (m_stmtInsert) {
|
||||
sqlite3_finalize(m_stmtInsert);
|
||||
m_stmtInsert = nullptr;
|
||||
}
|
||||
if (m_db) {
|
||||
sqlite3_close(m_db);
|
||||
m_db = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void DbStore::onRawFrame(int direction, uint16_t msgId, const std::string& link,
|
||||
const std::vector<uint8_t>& data, bool checksumOk) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (!m_db || !m_stmtInsert) return;
|
||||
|
||||
// 消息名:尝试从链路名无法直接获取,这里用 msgId 的十六进制作为名称的一部分
|
||||
char name[32];
|
||||
std::snprintf(name, sizeof(name), "msg_0x%04X", msgId);
|
||||
std::string hex = toHex(data);
|
||||
|
||||
sqlite3_reset(m_stmtInsert);
|
||||
sqlite3_clear_bindings(m_stmtInsert);
|
||||
// 时间:使用当前 unix 时间(秒),供网页展示
|
||||
sqlite3_bind_double(m_stmtInsert, 1, static_cast<double>(::time(nullptr)));
|
||||
sqlite3_bind_text(m_stmtInsert, 2, link.c_str(), -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(m_stmtInsert, 3, direction);
|
||||
sqlite3_bind_int(m_stmtInsert, 4, msgId);
|
||||
sqlite3_bind_text(m_stmtInsert, 5, name, -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(m_stmtInsert, 6, static_cast<int>(data.size()));
|
||||
sqlite3_bind_int(m_stmtInsert, 7, checksumOk ? 1 : 0);
|
||||
sqlite3_bind_text(m_stmtInsert, 8, hex.c_str(), -1, SQLITE_TRANSIENT);
|
||||
|
||||
int rc = sqlite3_step(m_stmtInsert);
|
||||
if (rc != SQLITE_DONE) {
|
||||
m_lastError = sqlite3_errmsg(m_db);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<CommLogRow> DbStore::queryRecent(const std::string& link, int direction,
|
||||
uint16_t msgId, int limit) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
std::vector<CommLogRow> rows;
|
||||
if (!m_db) return rows;
|
||||
if (limit <= 0) limit = 100;
|
||||
|
||||
std::string sql = "SELECT id, time, link, direction, msg_id, msg_name, length, checksum_ok, hex "
|
||||
"FROM comm_log WHERE 1=1";
|
||||
if (!link.empty()) {
|
||||
sql += " AND link='" + link + "'";
|
||||
}
|
||||
if (direction >= 0) {
|
||||
sql += " AND direction=" + std::to_string(direction);
|
||||
}
|
||||
if (msgId != 0) {
|
||||
char buf[16];
|
||||
std::snprintf(buf, sizeof(buf), "%u", msgId);
|
||||
sql += " AND msg_id=" + std::string(buf);
|
||||
}
|
||||
sql += " ORDER BY id DESC LIMIT " + std::to_string(limit) + ";";
|
||||
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
m_lastError = sqlite3_errmsg(m_db);
|
||||
return rows;
|
||||
}
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
CommLogRow r;
|
||||
r.id = sqlite3_column_int64(stmt, 0);
|
||||
r.time = sqlite3_column_double(stmt, 1);
|
||||
r.link = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
r.direction = sqlite3_column_int(stmt, 3);
|
||||
r.msgId = static_cast<uint16_t>(sqlite3_column_int(stmt, 4));
|
||||
r.msgName = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 5));
|
||||
r.length = sqlite3_column_int(stmt, 6);
|
||||
r.checksumOk = sqlite3_column_int(stmt, 7);
|
||||
r.hex = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8));
|
||||
rows.push_back(std::move(r));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return rows;
|
||||
}
|
||||
|
||||
long long DbStore::count() const {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (!m_db) return 0;
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
if (sqlite3_prepare_v2(m_db, "SELECT COUNT(*) FROM comm_log;", -1, &stmt, nullptr) != SQLITE_OK)
|
||||
return 0;
|
||||
long long n = 0;
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) n = sqlite3_column_int64(stmt, 0);
|
||||
sqlite3_finalize(stmt);
|
||||
return n;
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef PCCU_DB_STORE_H
|
||||
#define PCCU_DB_STORE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include "../comm/LinkManager.h"
|
||||
|
||||
struct sqlite3;
|
||||
struct sqlite3_stmt;
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// DbStore:SQLite 存储。
|
||||
//
|
||||
// 设计要点(应对协议变更):
|
||||
// - 以“原始帧日志”为主表(comm_log),收发全部数据按帧落库,
|
||||
// 协议字段变化不会导致建表失败。
|
||||
// - 提供按 link / direction / msg_id 的查询接口供网页展示。
|
||||
// 复用仓库内 src/pPowerManger/sqlit3/sqlite3.c 与 sqlite3.h。
|
||||
//============================================================================
|
||||
|
||||
struct CommLogRow {
|
||||
long long id;
|
||||
double time;
|
||||
std::string link; // fc / pm
|
||||
int direction; // 0=收 1=发
|
||||
uint16_t msgId;
|
||||
std::string msgName;
|
||||
int length;
|
||||
int checksumOk;
|
||||
std::string hex;
|
||||
};
|
||||
|
||||
class DbStore : public ILinkLogSink {
|
||||
public:
|
||||
explicit DbStore(const std::string& dbPath);
|
||||
~DbStore();
|
||||
|
||||
bool open();
|
||||
void close();
|
||||
bool isOpen() const { return m_db != nullptr; }
|
||||
std::string lastError() const { return m_lastError; }
|
||||
|
||||
// 实现 ILinkLogSink:原始帧落库(收发双向)
|
||||
void onRawFrame(int direction, uint16_t msgId, const std::string& link,
|
||||
const std::vector<uint8_t>& data, bool checksumOk) override;
|
||||
|
||||
// 查询最近 N 条记录(可限定链路/方向/消息id;limit<=0 表示默认 100)
|
||||
std::vector<CommLogRow> queryRecent(const std::string& link, int direction,
|
||||
uint16_t msgId, int limit);
|
||||
|
||||
long long count() const;
|
||||
|
||||
private:
|
||||
bool exec(const char* sql);
|
||||
bool prepareInsert();
|
||||
|
||||
std::string m_dbPath;
|
||||
sqlite3* m_db = nullptr;
|
||||
sqlite3_stmt* m_stmtInsert = nullptr;
|
||||
mutable std::mutex m_mutex;
|
||||
std::string m_lastError;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_DB_STORE_H
|
||||
@@ -0,0 +1,132 @@
|
||||
#include "WebServer.h"
|
||||
#include "pages/index.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//---------------------------------------------------------
|
||||
// 事件处理:HTTP 路由 + WebSocket 收发
|
||||
|
||||
void WebServer::handleEvent(struct mg_connection* c, int ev, void* ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message* hm = (struct mg_http_message*)ev_data;
|
||||
|
||||
// WebSocket 升级
|
||||
if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
// /api/* 接口:交给宿主程序处理,返回 JSON
|
||||
std::string path(hm->uri.buf, hm->uri.len);
|
||||
if (path.rfind("/api/", 0) == 0) {
|
||||
std::string body;
|
||||
if (m_apiHandler) {
|
||||
std::string query(hm->query.buf, hm->query.len);
|
||||
body = m_apiHandler(path, query);
|
||||
}
|
||||
if (!body.empty()) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"%.*s", (int)body.size(), body.c_str());
|
||||
} else {
|
||||
mg_http_reply(c, 404, "Content-Type: text/plain\r\n", "Not Found\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 监控页面
|
||||
if (path == "/" || path == "/index.html") {
|
||||
mg_http_reply(c, 200, "Content-Type: text/html; charset=utf-8\r\n",
|
||||
"%.*s", (int)INDEX_HTML.size(), INDEX_HTML.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
mg_http_reply(c, 404, "Content-Type: text/plain\r\n", "Not Found\n");
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
std::lock_guard<std::mutex> lock(m_wsMutex);
|
||||
m_wsConnections.push_back(c);
|
||||
if (m_onOpen) {
|
||||
std::string welcome = m_onOpen();
|
||||
if (!welcome.empty()) {
|
||||
mg_ws_send(c, welcome.c_str(), welcome.size(), WEBSOCKET_OP_TEXT);
|
||||
}
|
||||
}
|
||||
} else if (ev == MG_EV_CLOSE || ev == MG_EV_ERROR) {
|
||||
std::lock_guard<std::mutex> lock(m_wsMutex);
|
||||
for (auto it = m_wsConnections.begin(); it != m_wsConnections.end(); ++it) {
|
||||
if (*it == c) {
|
||||
m_wsConnections.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (ev == MG_EV_WS_MSG) {
|
||||
// 纯监控,忽略客户端消息
|
||||
}
|
||||
}
|
||||
|
||||
WebServer::WebServer() {
|
||||
mg_mgr_init(&m_mgr);
|
||||
}
|
||||
|
||||
WebServer::~WebServer() {
|
||||
stop();
|
||||
mg_mgr_free(&m_mgr);
|
||||
}
|
||||
|
||||
void WebServer::setOnOpen(std::function<std::string()> handler) {
|
||||
m_onOpen = std::move(handler);
|
||||
}
|
||||
|
||||
void WebServer::setApiHandler(std::function<std::string(const std::string&, const std::string&)> handler) {
|
||||
m_apiHandler = std::move(handler);
|
||||
}
|
||||
|
||||
bool WebServer::start(int port) {
|
||||
if (m_running) return true;
|
||||
m_port = port;
|
||||
|
||||
std::string addr = "http://0.0.0.0:" + std::to_string(port);
|
||||
mg_http_listen(&m_mgr, addr.c_str(), [](mg_connection* c, int ev, void* ev_data) {
|
||||
WebServer* server = static_cast<WebServer*>(c->fn_data);
|
||||
if (server) server->handleEvent(c, ev, ev_data);
|
||||
}, this);
|
||||
|
||||
m_running = true;
|
||||
m_thread = std::thread([this]() { serverThreadFunc(); });
|
||||
|
||||
std::cout << "pCCU web UI listening on " << addr << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebServer::stop() {
|
||||
if (!m_running) return;
|
||||
m_running = false;
|
||||
if (m_thread.joinable()) m_thread.join();
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_wsMutex);
|
||||
for (auto c : m_wsConnections) {
|
||||
mg_ws_send(c, "", 0, WEBSOCKET_OP_CLOSE);
|
||||
}
|
||||
m_wsConnections.clear();
|
||||
}
|
||||
|
||||
bool WebServer::serverThreadCB(void* pParam) {
|
||||
WebServer* pThis = static_cast<WebServer*>(pParam);
|
||||
return pThis->serverThreadFunc();
|
||||
}
|
||||
|
||||
bool WebServer::serverThreadFunc() {
|
||||
while (m_running) {
|
||||
mg_mgr_poll(&m_mgr, 200);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebServer::broadcast(const std::string& text) {
|
||||
std::lock_guard<std::mutex> lock(m_wsMutex);
|
||||
for (auto c : m_wsConnections) {
|
||||
mg_ws_send(c, text.c_str(), text.size(), WEBSOCKET_OP_TEXT);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ccu
|
||||
@@ -0,0 +1,61 @@
|
||||
#ifndef PCCU_WEB_SERVER_H
|
||||
#define PCCU_WEB_SERVER_H
|
||||
|
||||
#define UNIX
|
||||
#include "mongoose.h"
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
namespace ccu {
|
||||
|
||||
//============================================================================
|
||||
// WebServer:HTTP + WebSocket 监控服务器(基于 mongoose)。
|
||||
//
|
||||
// 路由:
|
||||
// / -> 内嵌监控页面(纯展示)
|
||||
// /ws -> WebSocket,推送 JSON 快照(由 CCU 周期调用 broadcast)
|
||||
// /api/logs -> 最近原始帧日志(JSON)
|
||||
// 采用与仓库内 pPowerMangerHost/WebServer 一致的回调模式。
|
||||
//============================================================================
|
||||
|
||||
class WebServer {
|
||||
public:
|
||||
WebServer();
|
||||
~WebServer();
|
||||
|
||||
bool start(int port);
|
||||
void stop();
|
||||
bool isRunning() const { return m_running; }
|
||||
|
||||
// 向所有 WebSocket 客户端广播文本
|
||||
void broadcast(const std::string& text);
|
||||
|
||||
// 新客户端连接时调用,返回初始快照
|
||||
void setOnOpen(std::function<std::string()> handler);
|
||||
|
||||
// 处理 /api/* 请求,返回 JSON 响应体(空串表示 404)
|
||||
void setApiHandler(std::function<std::string(const std::string& uri, const std::string& query)> handler);
|
||||
|
||||
private:
|
||||
static bool serverThreadCB(void* pParam);
|
||||
bool serverThreadFunc();
|
||||
void handleEvent(struct mg_connection* c, int ev, void* ev_data);
|
||||
|
||||
struct mg_mgr m_mgr;
|
||||
int m_port = 0;
|
||||
std::atomic<bool> m_running{false};
|
||||
std::thread m_thread;
|
||||
|
||||
std::mutex m_wsMutex;
|
||||
std::vector<mg_connection*> m_wsConnections;
|
||||
std::function<std::string()> m_onOpen;
|
||||
std::function<std::string(const std::string&, const std::string&)> m_apiHandler;
|
||||
};
|
||||
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_WEB_SERVER_H
|
||||
@@ -0,0 +1,490 @@
|
||||
#ifndef PCCU_PAGE_INDEX_H
|
||||
#define PCCU_PAGE_INDEX_H
|
||||
|
||||
#include <string>
|
||||
|
||||
//============================================================================
|
||||
// pCCU 监控页面(纯前端,内嵌 HTML)。
|
||||
//
|
||||
// - 通过 WebSocket /ws 接收服务器推送的 JSON 快照(每秒)
|
||||
// - 每条消息一个标签页(接收/发送/系统分组),数据按消息归类展示
|
||||
// - 故障码/故障等级突出高亮;每条协议数据单独一行
|
||||
// - 纯监控展示,不提供反向控制
|
||||
//============================================================================
|
||||
|
||||
namespace ccu {
|
||||
const std::string INDEX_HTML = R"HTML(<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>pCCU 复合管控器监控</title>
|
||||
<style>
|
||||
:root{--bg:#0f1420;--card:#1a2130;--line:#2a3447;--fg:#e6edf3;--dim:#8b98ab;--ok:#2ea043;--warn:#d29922;--bad:#f85149;}
|
||||
*{margin:0;padding:0;box-sizing:border-box;}
|
||||
body{background:var(--bg);color:var(--fg);font-family:'Segoe UI',system-ui,sans-serif;font-size:13px;padding:16px;}
|
||||
h1{font-size:18px;margin-bottom:12px;display:flex;align-items:center;gap:10px;}
|
||||
h2{font-size:13px;color:var(--dim);text-transform:uppercase;letter-spacing:.5px;margin-bottom:10px;border-bottom:1px solid var(--line);padding-bottom:6px;}
|
||||
.badge{font-size:11px;padding:2px 8px;border-radius:10px;background:var(--card);border:1px solid var(--line);}
|
||||
.badge.ok{color:var(--ok);border-color:var(--ok);}
|
||||
.badge.warn{color:var(--warn);border-color:var(--warn);}
|
||||
.badge.err{color:var(--bad);border-color:var(--bad);}
|
||||
.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(340px,1fr));gap:12px;}
|
||||
.card{background:var(--card);border:1px solid var(--line);border-radius:8px;padding:12px;}
|
||||
.row{display:flex;justify-content:space-between;padding:3px 0;border-bottom:1px dashed #222c3d;}
|
||||
.row:last-child{border-bottom:none;}
|
||||
.row .k{color:var(--dim);}
|
||||
.row .v{font-family:Consolas,monospace;color:var(--fg);}
|
||||
.raw{font-family:Consolas,monospace;font-size:11px;color:var(--dim);word-break:break-all;max-height:200px;overflow-y:auto;}
|
||||
.raw-group{margin-bottom:12px;border:1px solid var(--line);border-radius:6px;padding:8px;}
|
||||
.raw-group:last-child{margin-bottom:0;}
|
||||
.raw-group-hdr{display:flex;align-items:center;gap:8px;margin-bottom:6px;flex-wrap:wrap;}
|
||||
.raw-id{font-family:Consolas,monospace;color:#79c0ff;font-weight:700;}
|
||||
.raw-name{color:var(--fg);font-weight:600;}
|
||||
.raw-count{color:var(--dim);font-size:11px;margin-left:auto;}
|
||||
table{width:100%;border-collapse:collapse;font-size:12px;}
|
||||
th,td{text-align:left;padding:4px 6px;border-bottom:1px solid #222c3d;}
|
||||
th{color:var(--dim);font-weight:500;}
|
||||
.status-dot{display:inline-block;width:8px;height:8px;border-radius:50%;margin-right:6px;}
|
||||
.dot-ok{background:var(--ok);} .dot-warn{background:var(--warn);} .dot-bad{background:var(--bad);}
|
||||
#status{color:var(--dim);font-size:12px;}
|
||||
.tabs{display:flex;flex-wrap:wrap;gap:10px;margin-bottom:16px;align-items:center;}
|
||||
.tab-group{display:flex;align-items:center;gap:6px;padding-right:16px;margin-right:16px;border-right:1px solid var(--line);}
|
||||
.tab-group:last-child{border-right:none;margin-right:0;padding-right:0;}
|
||||
.tg-label{font-size:11px;color:var(--dim);font-weight:700;letter-spacing:.5px;}
|
||||
.tab{padding:6px 14px;border:1px solid var(--line);border-radius:16px;background:var(--card);color:var(--dim);cursor:pointer;font-size:12px;transition:all .15s;}
|
||||
.tab:hover{color:var(--fg);border-color:#3b4a63;}
|
||||
.tab.active{background:#1f6feb;border-color:#1f6feb;color:#fff;font-weight:600;}
|
||||
.page-hdr{display:flex;align-items:center;gap:8px;margin-bottom:12px;padding:10px 14px;background:#141b29;border:1px solid var(--line);border-radius:8px;font-size:14px;}
|
||||
/* 故障等级高亮 */
|
||||
.fault-lvl{display:inline-block;padding:3px 12px;border-radius:6px;font-weight:700;font-size:13px;letter-spacing:.5px;}
|
||||
.lvl0{background:rgba(46,160,67,.15);color:#3fb950;border:1px solid rgba(46,160,67,.45);}
|
||||
.lvl1{background:rgba(210,153,34,.15);color:#d29922;border:1px solid rgba(210,153,34,.45);}
|
||||
.lvl2{background:rgba(219,97,20,.18);color:#e3841d;border:1px solid rgba(219,97,20,.55);}
|
||||
.lvl3{background:rgba(248,81,73,.22);color:#f85149;border:1px solid rgba(248,81,73,.55);}
|
||||
.lvl4{background:rgba(248,81,73,.38);color:#ff7b72;border:1px solid #f85149;}
|
||||
.fc-bad{color:#f85149;font-weight:700;background:rgba(248,81,73,.12);padding:1px 6px;border-radius:4px;}
|
||||
.fc-ok{color:#3fb950;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>⚡ pCCU 复合管控器监控 <span id="status" class="badge">连接中...</span></h1>
|
||||
<div class="tabs">
|
||||
<div class="tab-group"><span class="tg-label">接收</span>
|
||||
<button class="tab active" data-tab="fcStatus">FC状态反馈</button>
|
||||
<button class="tab" data-tab="pmCmd">PM操控指令</button>
|
||||
<button class="tab" data-tab="pmParam">PM参数设定</button>
|
||||
</div>
|
||||
<div class="tab-group"><span class="tg-label">发送</span>
|
||||
<button class="tab" data-tab="fcCmd">FC控制指令</button>
|
||||
<button class="tab" data-tab="pmStatus">PM状态报文</button>
|
||||
<button class="tab" data-tab="pmFb">PM参数反馈</button>
|
||||
</div>
|
||||
<div class="tab-group"><span class="tg-label">系统</span>
|
||||
<button class="tab" data-tab="links">链路状态</button>
|
||||
<button class="tab" data-tab="raw">原始报文</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="pages">加载中...</div>
|
||||
|
||||
<script>
|
||||
const $ = id => document.getElementById(id);
|
||||
const pagesEl = $('pages');
|
||||
const statusEl = $('status');
|
||||
const TABS = ['fcStatus','pmCmd','pmParam','fcCmd','pmStatus','pmFb','links','raw'];
|
||||
let activeTab = 'fcStatus';
|
||||
|
||||
function dot(ok){ return ok ? 'dot-ok' : (ok === undefined ? 'dot-warn' : 'dot-bad'); }
|
||||
function row(k, v){ return '<div class="row"><span class="k">'+k+'</span><span class="v">'+v+'</span></div>'; }
|
||||
function hex(v){ return '0x' + (v & 0xFF).toString(16).toUpperCase().padStart(2,'0'); }
|
||||
function hex16(v){ return '0x' + (v & 0xFFFF).toString(16).toUpperCase().padStart(4,'0'); }
|
||||
function hex32(v){ return '0x' + (v >>> 0).toString(16).toUpperCase().padStart(8,'0'); }
|
||||
|
||||
//------------------ 指令码解析 ------------------
|
||||
function modeText(m){ return {0:'无效',1:'调试',2:'自动',3:'补给/排放'}[m] || ('未知('+hex(m)+')'); }
|
||||
function cmdText(c){
|
||||
const map={0x00:'无效',0x01:'自检',0x02:'启动',0x03:'停机',0x04:'复位',0x05:'紧急停机',
|
||||
0x06:'补给',0x07:'制氢预热',0x08:'停机维护',0x0A:'开机',0x0B:'关机'};
|
||||
return map[c] || ('未知('+hex(c)+')');
|
||||
}
|
||||
function batCmdText(b){ return {0x00:'无效',0x10:'启动',0x20:'关闭'}[b] || ('未知('+hex(b)+')'); }
|
||||
function hostStateText(s){ return s===0xAA?'运行':s===0xFF?'关机':'故障'; }
|
||||
function supplyCmdText(s){
|
||||
const map={0x00:'无效',0x01:'液氧加注',0x02:'甲醇加注',0x03:'液氧排空',0x04:'甲醇排空',
|
||||
0x05:'CO2排空',0x07:'合金加注',0x08:'氮气加注',0x09:'纯水加注'};
|
||||
return map[s] || ('未知('+hex(s)+')');
|
||||
}
|
||||
function emerAllowText(v){
|
||||
const bits=[];
|
||||
if(v & 0x01) bits.push('允许降载');
|
||||
if(v & 0x02) bits.push('允许排气');
|
||||
return bits.length ? bits.join('+') : '无';
|
||||
}
|
||||
function statusText(s){
|
||||
const m={1:'初始化',2:'待机',3:'自检异常',4:'就绪',5:'启动',6:'运行',7:'停机',8:'故障',
|
||||
9:'补给',10:'制氢预热',11:'制氢预热完成',12:'停机维护',13:'维护完成'};
|
||||
return m[s] || ('未知('+hex(s)+')');
|
||||
}
|
||||
function flameText(f){ return {0:'无效',1:'火焰报警',2:'探测器故障'}[f] || ('未知('+hex(f)+')'); }
|
||||
// 带解析的值:hex 码 + 括号解析
|
||||
function parsed(v, text){ return hex(v)+' ('+text+')'; }
|
||||
// 故障码高亮:0 显示绿色正常,非 0 显示红色
|
||||
function faultHex(c){ return c===0 ? '<span class="fc-ok">'+hex16(c)+' 正常</span>'
|
||||
: '<span class="fc-bad">'+hex16(c)+'</span>'; }
|
||||
// 故障等级高亮徽标
|
||||
function faultLvlBadge(level){
|
||||
const names=['无故障','一级故障','二级故障','三级故障','四级故障'];
|
||||
const n = names[level] || ('未知('+level+')');
|
||||
const cls = ['lvl0','lvl1','lvl2','lvl3','lvl4'][level] || 'lvl4';
|
||||
return '<span class="fault-lvl '+cls+'">'+n+'</span>';
|
||||
}
|
||||
|
||||
function card(title, html){ return '<div class="card"><h2>'+title+'</h2>'+html+'</div>'; }
|
||||
|
||||
//------------------ FC 状态反馈(0x0002) 卡片 ------------------
|
||||
function fcCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('运行模式', modeText(d.fc_mode));
|
||||
h+=row('运行状态', statusText(d.fc_status));
|
||||
h+=row('系统故障等级', faultLvlBadge(d.fc_fault_level));
|
||||
h+=row('一级故障码', faultHex(d.fault_level_1));
|
||||
h+=row('二级故障码', faultHex(d.fault_level_2));
|
||||
h+=row('三级故障码', faultHex(d.fault_level_3));
|
||||
h+=row('四级故障码', faultHex(d.fault_level_4));
|
||||
h+=row('累积发电时间', (d.total_generation_time*0.1).toFixed(1)+' h');
|
||||
h+=row('系统发电功率', (d.generation_power*0.01).toFixed(2)+' kW');
|
||||
h+=row('储氢剩余容量', d.hydrogen_capacity+'%');
|
||||
h+=row('液氧剩余容量', d.liquid_oxygen_capacity+'%');
|
||||
h+=row('通信心跳', d.heartbeat);
|
||||
h+=row('应急指令', hex(d.emergency_cmd));
|
||||
return card('燃料电池运行', h);
|
||||
}
|
||||
|
||||
function hydroCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('钯膜最高温度', (d.palladium_temp*0.1).toFixed(1)+' ℃');
|
||||
h+=row('缓冲罐压力', (d.buffer_tank_pressure*0.1).toFixed(1)+' kPa');
|
||||
h+=row('烟气累计排放量', (d.flue_total_emission*0.1).toFixed(1)+' kg');
|
||||
h+=row('烟气压力', (d.flue_pressure*0.001).toFixed(3)+' MPa');
|
||||
h+=row('反应器压力', (d.reactor_pressure*0.001).toFixed(3)+' MPa');
|
||||
h+=row('电动阀开度', (d.electric_valve_open*0.1).toFixed(1)+'%');
|
||||
h+=row('甲醇累计使用量', (d.methanol_total_use*0.1).toFixed(1)+' kg');
|
||||
h+=row('甲醇溶液进料量', d.methanol_feed+' mL/min');
|
||||
h+=row('I#FC最低单片电压', (d.fc1_min_cell_voltage*10)+' mV (位置'+d.fc1_min_cell_pos+')');
|
||||
h+=row('I#FC平均单片电压', (d.fc1_avg_cell_voltage*10)+' mV');
|
||||
h+=row('2#FC最低单片电压', (d.fc2_min_cell_voltage*10)+' mV (位置'+d.fc2_min_cell_pos+')');
|
||||
h+=row('2#FC平均单片电压', (d.fc2_avg_cell_voltage*10)+' mV');
|
||||
return card('甲醇制氢装置', h);
|
||||
}
|
||||
|
||||
function dcdcCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('通道1输入电压', (d.dcdc1_in_voltage*0.1).toFixed(1)+' V');
|
||||
h+=row('通道1输入电流', (d.dcdc1_in_current*0.1).toFixed(1)+' A');
|
||||
h+=row('通道2输入电压', (d.dcdc2_in_voltage*0.1).toFixed(1)+' V');
|
||||
h+=row('通道2输入电流', (d.dcdc2_in_current*0.1).toFixed(1)+' A');
|
||||
h+=row('输出电压', (d.dcdc_out_voltage*0.1).toFixed(1)+' V');
|
||||
h+=row('输出电流', (d.dcdc_out_current*0.1).toFixed(1)+' A');
|
||||
h+=row('控制电源电压', (d.dcdc_ctrl_voltage*0.25).toFixed(2)+' V');
|
||||
h+=row('辅电输出电压', (d.dcdc_aux_voltage*0.125).toFixed(3)+' V');
|
||||
return card('DC/DC', h);
|
||||
}
|
||||
|
||||
function exhaustCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('进气压力', (d.exhaust_inlet_pressure*0.01).toFixed(2)+' MPa');
|
||||
h+=row('排气压力', (d.exhaust_outlet_pressure*0.01).toFixed(2)+' MPa');
|
||||
h+=row('运行频率', (d.exhaust_run_freq*0.01).toFixed(2)+' Hz');
|
||||
h+=row('进气温度', (d.exhaust_inlet_temp*0.01).toFixed(2)+' ℃');
|
||||
h+=row('排气温度', (d.exhaust_outlet_temp*0.01).toFixed(2)+' ℃');
|
||||
h+=row('进水压力', (d.exhaust_water_in_pressure*0.01).toFixed(2)+' kPa');
|
||||
h+=row('排水压力', (d.exhaust_water_out_pressure*0.01).toFixed(2)+' kPa');
|
||||
return card('尾气装置', h);
|
||||
}
|
||||
|
||||
function tankCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('液氧罐压力', (d.tank_lo2_pressure*0.01).toFixed(2)+' MPa');
|
||||
h+=row('二氧化碳压力', (d.tank_co2_pressure*0.01).toFixed(2)+' MPa');
|
||||
h+=row('液氧罐液位', (d.tank_lo2_level*0.01).toFixed(2)+' mm');
|
||||
h+=row('合金供氢流量', (d.alloy_h2_flow*0.01).toFixed(2)+' L/min');
|
||||
h+=row('FC供氢流量', (d.fc_h2_flow*0.01).toFixed(2)+' L/min');
|
||||
h+=row('FC供氧流量', (d.fc_o2_flow*0.01).toFixed(2)+' L/min');
|
||||
h+=row('应急上浮深度', d.emergency_float_depth+' m');
|
||||
h+=row('应急上浮时间', d.emergency_float_time+' min');
|
||||
return card('一体化罐装置', h);
|
||||
}
|
||||
|
||||
function cabinCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('舱室压力1', (d.cabin_pressure1*0.01).toFixed(2)+' kPa');
|
||||
h+=row('舱室压力2', (d.cabin_pressure2*0.01).toFixed(2)+' kPa');
|
||||
h+=row('舱室温度1', (d.cabin_temp1*0.01).toFixed(2)+' ℃');
|
||||
h+=row('舱室温度2', (d.cabin_temp2*0.01).toFixed(2)+' ℃');
|
||||
h+=row('舱室湿度1', (d.cabin_humidity1*0.01).toFixed(2)+' %RH');
|
||||
h+=row('舱室湿度2', (d.cabin_humidity2*0.01).toFixed(2)+' %RH');
|
||||
h+=row('舱室H2浓度1', (d.h2_concentration1*0.01).toFixed(2)+' %LEL');
|
||||
h+=row('舱室H2浓度2', (d.h2_concentration2*0.01).toFixed(2)+' %LEL');
|
||||
h+=row('舱室H2浓度3', (d.h2_concentration3*0.01).toFixed(2)+' %LEL');
|
||||
h+=row('舱室O2浓度1', (d.o2_concentration1*0.01).toFixed(2)+' %Vol');
|
||||
h+=row('舱室O2浓度2', (d.o2_concentration2*0.01).toFixed(2)+' %Vol');
|
||||
h+=row('舱室甲醇浓度1', (d.ch3oh_concentration1*0.01).toFixed(2)+' %LEL');
|
||||
h+=row('舱室甲醇浓度2', (d.ch3oh_concentration2*0.01).toFixed(2)+' %LEL');
|
||||
h+=row('火焰探测器1', flameText(d.flame_detector1));
|
||||
h+=row('火焰探测器2', flameText(d.flame_detector2));
|
||||
return card('舱室环境', h);
|
||||
}
|
||||
|
||||
//------------------ PM 状态报文(0x0004) 的锂电池 / 应急电池卡片 ------------------
|
||||
function batteryCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('仪表电池SOC', d.ins_soc+'%');
|
||||
h+=row('仪表电池总能量', (d.ins_total_energy*0.1).toFixed(1)+' kWh');
|
||||
h+=row('动力电池SOC', d.dyn_soc+'%');
|
||||
h+=row('动力电池总能量', (d.dyn_total_energy*0.1).toFixed(1)+' kWh');
|
||||
h+=row('仪表LINK电压', (d.ins_voltage_link*0.01).toFixed(2)+' V');
|
||||
h+=row('仪表PACK电压', (d.ins_voltage_pack*0.01).toFixed(2)+' V');
|
||||
h+=row('动力LINK电压', (d.dyn_voltage_link*0.01).toFixed(2)+' V');
|
||||
h+=row('动力PACK电压', (d.dyn_voltage_pack*0.01).toFixed(2)+' V');
|
||||
h+=row('仪表电流', (d.ins_current*0.05).toFixed(2)+' A');
|
||||
h+=row('动力电流', (d.dyn_current*0.05).toFixed(2)+' A');
|
||||
h+=row('仪表充电状态', hex(d.ins_charge_status));
|
||||
h+=row('动力充电状态', hex(d.dyn_charge_status));
|
||||
h+=row('仪表紧急状态', hex(d.ins_emergency_status));
|
||||
h+=row('动力紧急状态', hex(d.dyn_emergency_status));
|
||||
h+=row('仪表最高放电功率', (d.ins_max_discharge_power*0.05).toFixed(2)+' kW');
|
||||
h+=row('动力最高放电功率', (d.dyn_max_discharge_power*0.05).toFixed(2)+' kW');
|
||||
return card('锂电池', h);
|
||||
}
|
||||
|
||||
function emergCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('应急电池1总电压', (d.emergency_battery1_voltage*0.1).toFixed(1)+' V');
|
||||
h+=row('应急电池1总电流', (d.emergency_battery1_current*0.05).toFixed(2)+' A');
|
||||
h+=row('应急电池1最高温度', (d.emergency_battery1_max_temp*0.1).toFixed(1)+' ℃');
|
||||
h+=row('应急电池1故障字', hex(d.emergency_battery1_fault_word));
|
||||
h+=row('应急电池2总电压', (d.emergency_battery2_voltage*0.1).toFixed(1)+' V');
|
||||
h+=row('应急电池2总电流', (d.emergency_battery2_current*0.05).toFixed(2)+' A');
|
||||
h+=row('应急电池2最高温度', (d.emergency_battery2_max_temp*0.1).toFixed(1)+' ℃');
|
||||
h+=row('应急电池2故障字', hex(d.emergency_battery2_fault_word));
|
||||
return card('应急电池', h);
|
||||
}
|
||||
|
||||
function linkCard(s){
|
||||
if(!s || !s.links) return '';
|
||||
const l = s.links;
|
||||
const online = (t)=> (Date.now()/1000 - t) < 10;
|
||||
let h='';
|
||||
h+=row('<span><span class="status-dot '+dot(online(l.fc.lastRx))+'"></span>FC 链路</span>',
|
||||
'收:'+l.fc.rx+' 发:'+l.fc.tx+' 误:'+l.fc.err);
|
||||
h+=row('<span><span class="status-dot '+dot(online(l.pm.lastRx))+'"></span>PM 链路</span>',
|
||||
'收:'+l.pm.rx+' 发:'+l.pm.tx+' 误:'+l.pm.err);
|
||||
h+=row('FC 状态接收计数', s.fcStatusCount);
|
||||
h+=row('PM 指令接收计数', s.pmControlCount);
|
||||
h+=row('FC 控制转发计数', s.fcControlCount);
|
||||
h+=row('PM 状态发送计数', s.pmStatusCount);
|
||||
return card('链路状态', h);
|
||||
}
|
||||
|
||||
//------------------ 控制指令卡片(代码 + 解析) ------------------
|
||||
function cmdCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('系统时间', d.year+'/'+d.month+'/'+d.day+' '+d.hour+':'+d.minute+':'+d.second);
|
||||
h+=row('模式设定', parsed(d.mode, modeText(d.mode)));
|
||||
h+=row('操控指令', parsed(d.cmd, cmdText(d.cmd)));
|
||||
h+=row('输出功率指令', d.outputPower+' (×0.1kW)');
|
||||
h+=row('纵倾姿态', (d.pitch*0.1).toFixed(1)+'°');
|
||||
h+=row('横倾姿态', (d.roll*0.1).toFixed(1)+'°');
|
||||
h+=row('应急允许', parsed(d.emergencyAllow, emerAllowText(d.emergencyAllow)));
|
||||
h+=row('潜深深度', d.depth+' m');
|
||||
h+=row('补给/排放指令', parsed(d.supplyCmd, supplyCmdText(d.supplyCmd)));
|
||||
h+=row('仪表锂电池启停', parsed(d.insBatCmd, batCmdText(d.insBatCmd)));
|
||||
h+=row('动力锂电池启停', parsed(d.dynBatCmd, batCmdText(d.dynBatCmd)));
|
||||
h+=row('动力锂电池功率配置', d.dynBatPower+' kW');
|
||||
h+=row('通信心跳', d.heartbeat);
|
||||
h+=row('主机状态', parsed(d.hostState, hostStateText(d.hostState)));
|
||||
return card('指令内容', h);
|
||||
}
|
||||
|
||||
function fcCmdCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('模式设定', parsed(d.mode, modeText(d.mode)));
|
||||
h+=row('操控指令', parsed(d.cmd, cmdText(d.cmd)));
|
||||
h+=row('输出功率指令', d.outputPower+' (×0.1kW)');
|
||||
h+=row('纵倾姿态', (d.pitch*0.1).toFixed(1)+'°');
|
||||
h+=row('横倾姿态', (d.roll*0.1).toFixed(1)+'°');
|
||||
h+=row('应急允许', parsed(d.emergencyAllow, emerAllowText(d.emergencyAllow)));
|
||||
h+=row('潜深深度', d.depth+' m');
|
||||
h+=row('补给/排放指令', parsed(d.supplyCmd, supplyCmdText(d.supplyCmd)));
|
||||
h+=row('预留指令5', hex(d.reservedCmd5));
|
||||
h+=row('预留指令6', hex(d.reservedCmd6));
|
||||
h+=row('通信心跳', d.heartbeat);
|
||||
return card('指令内容', h);
|
||||
}
|
||||
|
||||
function pmParamCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('仪表SOC门限1', d.insSocHold1);
|
||||
h+=row('仪表SOC门限2', d.insSocHold2);
|
||||
h+=row('仪表SOC门限3', d.insSocHold3);
|
||||
h+=row('仪表输出功率', d.insOutputPower+'%');
|
||||
h+=row('动力SOC门限1', d.dynSocHold1);
|
||||
h+=row('动力SOC门限2', d.dynSocHold2);
|
||||
h+=row('动力SOC门限3', d.dynSocHold3);
|
||||
h+=row('动力输出功率', d.dynOutputPower+'%');
|
||||
return card('门限参数', h);
|
||||
}
|
||||
|
||||
function pmFbCard(d){
|
||||
if(!d) return '';
|
||||
const flagMap = {0x00:'无效', 0x10:'设定成功', 0x20:'设定失败'};
|
||||
let h='';
|
||||
h+=row('设定标志', (flagMap[d.flag]||hex(d.flag)));
|
||||
h+=row('失败原因', hex(d.failCode));
|
||||
return card('设定结果', h);
|
||||
}
|
||||
|
||||
function pmStatusCard(d){
|
||||
if(!d) return '';
|
||||
let h='';
|
||||
h+=row('运行模式', modeText(d.fc_mode));
|
||||
h+=row('运行状态', statusText(d.fc_status));
|
||||
h+=row('系统故障等级', faultLvlBadge(d.fc_fault_level));
|
||||
h+=row('一级故障码', faultHex(d.fault_level_1));
|
||||
h+=row('二级故障码', faultHex(d.fault_level_2));
|
||||
h+=row('三级故障码', faultHex(d.fault_level_3));
|
||||
h+=row('四级故障码', faultHex(d.fault_level_4));
|
||||
h+=row('系统输出功率限定', d.output_power_limit+' W');
|
||||
h+=row('系统发电功率', (d.generation_power*0.01).toFixed(2)+' kW');
|
||||
h+=row('储氢剩余容量', d.hydrogen_capacity+'%');
|
||||
h+=row('液氧剩余容量', d.liquid_oxygen_capacity+'%');
|
||||
h+=row('钯膜最高温度', (d.palladium_temp*0.1).toFixed(1)+' ℃');
|
||||
h+=row('缓冲罐压力', (d.buffer_tank_pressure*0.1).toFixed(1)+' kPa');
|
||||
h+=row('主水路盘管压力', (d.main_pipe_pressure*0.01).toFixed(2)+' kPa');
|
||||
h+=row('辅水路盘管压力', (d.aux_pipe_pressure*0.01).toFixed(2)+' kPa');
|
||||
h+=row('DC/DC输出电压', (d.dcdc_out_voltage*0.1).toFixed(1)+' V');
|
||||
h+=row('DC/DC输出电流', (d.dcdc_out_current*0.1).toFixed(1)+' A');
|
||||
h+=row('甲醇累计使用量', (d.methanol_total_use*0.1).toFixed(1)+' kg');
|
||||
h+=row('甲醇溶液进料量', d.methanol_feed+' mL/min');
|
||||
h+=row('舱室温度1', (d.cabin_temp1*0.01).toFixed(2)+' ℃');
|
||||
h+=row('舱室温度2', (d.cabin_temp2*0.01).toFixed(2)+' ℃');
|
||||
h+=row('舱室H2浓度1', (d.h2_concentration1*0.01).toFixed(2)+' %LEL');
|
||||
h+=row('舱室O2浓度1', (d.o2_concentration1*0.01).toFixed(2)+' %Vol');
|
||||
h+=row('仪表电池SOC', d.ins_soc+'%');
|
||||
h+=row('动力电池SOC', d.dyn_soc+'%');
|
||||
h+=row('仪表LINK电压', (d.ins_voltage_link*0.01).toFixed(2)+' V');
|
||||
h+=row('动力LINK电压', (d.dyn_voltage_link*0.01).toFixed(2)+' V');
|
||||
h+=row('仪表电流', (d.ins_current*0.05).toFixed(2)+' A');
|
||||
h+=row('动力电流', (d.dyn_current*0.05).toFixed(2)+' A');
|
||||
h+=row('设备在线标志字1', hex32(d.device_online_flag1));
|
||||
h+=row('设备在线标志字2', hex32(d.device_online_flag2));
|
||||
h+=row('通信心跳', d.heartbeat);
|
||||
h+=row('应急指令', hex(d.emergency_cmd));
|
||||
return card('整合后状态', h);
|
||||
}
|
||||
|
||||
//------------------ 标签页头部 ------------------
|
||||
function pageHeader(id, name, dir){
|
||||
const idHex = '0x'+id.toString(16).toUpperCase().padStart(4,'0');
|
||||
const isTx = dir.indexOf('CCU→')===0;
|
||||
return '<div class="page-hdr"><span class="raw-id">'+idHex+'</span> '+
|
||||
'<span class="raw-name">'+name+'</span> '+
|
||||
'<span class="badge '+(isTx?'warn':'ok')+'">'+dir+'</span></div>';
|
||||
}
|
||||
|
||||
function rawCard(list){
|
||||
if(!list || !list.length) return '<div class="card"><h2>原始报文 · 按消息ID分组</h2><div class="raw">暂无</div></div>';
|
||||
const groups = {};
|
||||
for(const r of list){
|
||||
if(!groups[r.msgId]) groups[r.msgId] = { rows: [] };
|
||||
groups[r.msgId].rows.push(r);
|
||||
}
|
||||
let html = '<div class="card"><h2>原始报文 · 按消息ID分组(最近 '+list.length+' 条)</h2>';
|
||||
for(const id in groups){
|
||||
const g = groups[id];
|
||||
const first = g.rows[0];
|
||||
const idHex = '0x'+Number(id).toString(16).toUpperCase().padStart(4,'0');
|
||||
const name = first.name || '';
|
||||
const dirText = first.dirText || (first.direction===0?'接收':'发送');
|
||||
html += '<div class="raw-group">';
|
||||
html += '<div class="raw-group-hdr"><span class="raw-id">'+idHex+'</span> '+
|
||||
'<span class="raw-name">'+name+'</span> '+
|
||||
'<span class="badge '+(first.direction===0?'ok':'warn')+'">'+dirText+'</span> '+
|
||||
'<span class="raw-count">'+g.rows.length+' 帧</span></div>';
|
||||
html += '<table><tr><th>时间</th><th>方向</th><th>校验</th><th>帧数据</th></tr>';
|
||||
for(const r of g.rows){
|
||||
const t=new Date(r.time*1000).toLocaleTimeString();
|
||||
const dText = r.dirText || (r.direction===0?'接收':'发送');
|
||||
const dCls = r.direction===0?'ok':'warn';
|
||||
html += '<tr><td>'+t+'</td>'+
|
||||
'<td><span class="badge '+dCls+'">'+dText+'</span></td>'+
|
||||
'<td>'+(r.checksumOk?'✓':'✗')+'</td>'+
|
||||
'<td class="raw">'+r.hex+'</td></tr>';
|
||||
}
|
||||
html += '</table></div>';
|
||||
}
|
||||
html += '</div>';
|
||||
return html;
|
||||
}
|
||||
|
||||
function render(snap){
|
||||
const pages = {
|
||||
// 0x0002 FC 状态反馈(FC→CCU):仅含 FC 消息字段
|
||||
fcStatus: pageHeader(0x0002,'FC状态反馈','FC→CCU')+'<div class="grid">'+
|
||||
fcCard(snap.fc)+hydroCard(snap.fc)+dcdcCard(snap.fc)+exhaustCard(snap.fc)
|
||||
+tankCard(snap.fc)+cabinCard(snap.fc)+'</div>',
|
||||
pmCmd: pageHeader(0x0001,'PM操控指令','PM→CCU')+'<div class="grid">'+cmdCard(snap.pmCmd)+'</div>',
|
||||
pmParam: pageHeader(0x0002,'PM参数设定','PM→CCU')+'<div class="grid">'+pmParamCard(snap.pmParam)+'</div>',
|
||||
fcCmd: pageHeader(0x0001,'FC控制指令','CCU→FC')+'<div class="grid">'+fcCmdCard(snap.fcCmd)+'</div>',
|
||||
// 0x0004 PM 状态报文(CCU→PM):含锂电池/应急电池数据
|
||||
pmStatus: pageHeader(0x0004,'PM状态报文','CCU→PM')+'<div class="grid">'+
|
||||
pmStatusCard(snap.pmStatus)+batteryCard(snap.pmStatus)+emergCard(snap.pmStatus)+'</div>',
|
||||
pmFb: pageHeader(0x0003,'PM参数反馈','CCU→PM')+'<div class="grid">'+pmFbCard(snap.pmFb)+'</div>',
|
||||
links: '<div class="grid">'+linkCard(snap)+'</div>',
|
||||
raw: rawCard(snap.logs)
|
||||
};
|
||||
|
||||
let html = '';
|
||||
for (const t of TABS) {
|
||||
html += '<div class="page" id="page-'+t+'"'+(t===activeTab?'':' style="display:none"')+'>'+pages[t]+'</div>';
|
||||
}
|
||||
pagesEl.innerHTML = html;
|
||||
}
|
||||
|
||||
// 标签页切换
|
||||
document.addEventListener('click', function(e){
|
||||
const t = e.target;
|
||||
const btn = (t && t.closest) ? t.closest('.tab') : null;
|
||||
if (!btn) return;
|
||||
activeTab = btn.getAttribute('data-tab');
|
||||
document.querySelectorAll('.tab').forEach(function(b){ b.classList.toggle('active', b===btn); });
|
||||
document.querySelectorAll('.page').forEach(function(p){ p.style.display = (p.id==='page-'+activeTab)?'':'none'; });
|
||||
});
|
||||
|
||||
function connect(){
|
||||
const proto = location.protocol==='https:'?'wss':'ws';
|
||||
const ws = new WebSocket(proto+'://'+location.host+'/ws');
|
||||
ws.onopen = ()=>{ statusEl.textContent='已连接'; statusEl.className='badge ok'; };
|
||||
ws.onclose = ()=>{ statusEl.textContent='已断开,重连中...'; statusEl.className='badge err'; setTimeout(connect, 2000); };
|
||||
ws.onerror = ()=>{ ws.close(); };
|
||||
ws.onmessage = (ev)=>{ try{ render(JSON.parse(ev.data)); }catch(e){} };
|
||||
}
|
||||
connect();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
)HTML";
|
||||
} // namespace ccu
|
||||
|
||||
#endif // PCCU_PAGE_INDEX_H
|
||||
@@ -443,131 +443,6 @@ char LowerCommManager::getBreakerOption(char cmd)
|
||||
return 0x00;
|
||||
}
|
||||
|
||||
void LowerCommManager::coutMsg(const msg_CcuStateFbMsg& p, std::ostream& os){
|
||||
os << std::hex << std::setfill('0') << std::uppercase;
|
||||
|
||||
os << "起始符1 (start1): 0x" << std::setw(2) << static_cast<int>(p.header.start1) << "\n";
|
||||
os << "起始符2 (start2): 0x" << std::setw(2) << static_cast<int>(p.header.start2) << "\n";
|
||||
os << "标识符 (identifier): 0x" << std::setw(4) << p.header.id << "\n";
|
||||
os << std::dec << "数据长度 (length): " << p.header.length << "\n";
|
||||
|
||||
|
||||
os << "运行模式 (fc_mode): 0x" << std::hex << std::setw(2) << static_cast<int>(p.data.fc_mode) << "\n";
|
||||
os << "燃料电池状态 (fc_status): 0x" << std::setw(2) << static_cast<int>(p.data.fc_status) << "\n";
|
||||
os << "故障等级 (fc_fault_level): 0x" << std::setw(2) << static_cast<int>(p.data.fc_fault_level) << "\n";
|
||||
|
||||
os << std::dec;
|
||||
os << "输出功率限制 (output_power_limit): " << p.data.output_power_limit << " W\n";
|
||||
os << "累计发电时间 (total_generation_time): " << p.data.total_generation_time * 0.1 << " 小时\n";
|
||||
os << "发电总功率 (total_generation_power): " << p.data.total_generation_power << " W\n";
|
||||
os << "一级故障字 (fault_level_1): 0x" << std::hex << p.data.fault_level_1 << "\n";
|
||||
os << "二级故障字 (fault_level_2): 0x" << p.data.fault_level_2 << "\n";
|
||||
os << "三级故障字 (fault_level_3): 0x" << p.data.fault_level_3 << "\n";
|
||||
os << "四级故障字 (fault_level_4): 0x" << p.data.fault_level_4 << "\n";
|
||||
|
||||
os << std::dec;
|
||||
os << "储氢剩余容量 (hydrogen_capacity): " << static_cast<int>(p.data.hydrogen_capacity) << "%\n";
|
||||
os << "液氧剩余容量 (liquid_oxygen_capacity): " << static_cast<int>(p.data.liquid_oxygen_capacity) << "%\n";
|
||||
os << "钯膜最高温度 (palladium_temp): " << p.data.palladium_temp << " ℃\n";
|
||||
os << "主水路压力 (main_pipe_pressure): " << p.data.main_pipe_pressure * 0.01 << " kPa\n";
|
||||
os << "辅水路压力 (aux_pipe_pressure): " << p.data.aux_pipe_pressure * 0.01 << " kPa\n";
|
||||
|
||||
os << "舱室压力1 (cabin_pressure1): " << p.data.cabin_pressure1 * 0.01 << " kPa\n";
|
||||
os << "舱室压力2 (cabin_pressure2): " << p.data.cabin_pressure2 * 0.01 << " kPa\n";
|
||||
os << "舱室温度1 (cabin_temp1): " << p.data.cabin_temp1 * 0.01 << " ℃\n";
|
||||
os << "舱室温度2 (cabin_temp2): " << p.data.cabin_temp2 * 0.01 << " ℃\n";
|
||||
os << "舱室湿度1 (cabin_humidity1): " << p.data.cabin_humidity1 * 0.01 << " %RH\n";
|
||||
os << "舱室湿度2 (cabin_humidity2): " << p.data.cabin_humidity2 * 0.01 << " %RH\n";
|
||||
|
||||
os << "火焰探测器1状态 (flame_detector1): 0x" << std::hex << static_cast<int>(p.data.flame_detector1) << "\n";
|
||||
os << "火焰探测器2状态 (flame_detector2): 0x" << static_cast<int>(p.data.flame_detector2) << "\n";
|
||||
|
||||
os << std::dec;
|
||||
os << "氢气浓度1 (h2_concentration1): " << p.data.h2_concentration1 * 0.01 << "% LEL\n";
|
||||
os << "氢气浓度2 (h2_concentration2): " << p.data.h2_concentration2 * 0.01 << "% LEL\n";
|
||||
os << "氢气浓度3 (h2_concentration3): " << p.data.h2_concentration3 * 0.01 << "% LEL\n";
|
||||
os << "氧气浓度1 (o2_concentration1): " << p.data.o2_concentration1 * 0.01 << "% LEL\n";
|
||||
os << "氧气浓度2 (o2_concentration2): " << p.data.o2_concentration2 * 0.01 << "% LEL\n";
|
||||
os << "甲醇浓度1 (ch3oh_concentration1): " << p.data.ch3oh_concentration1 * 0.01 << "% LEL\n";
|
||||
os << "甲醇浓度2 (ch3oh_concentration2): " << p.data.ch3oh_concentration2 * 0.01 << "% LEL\n";
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
os << "保留字段 reserved1[" << i << "]: 0x" << std::hex << p.data.reserved1[i] << "\n";
|
||||
|
||||
os << "仪表舱 氧气浓度 (cabin_ox_concentration): " << p.data.cabin_ox_concentration * 0.1 << "%\n";
|
||||
os << "仪表舱 温度 (cabin_temperature): " << static_cast<int16_t> (p.data.cabin_temperature) * 0.1 << "°C\n";
|
||||
os << "仪表舱 湿度 (cabin_humidity): " << p.data.cabin_humidity * 0.1 << "% RH\n";
|
||||
os << "仪表舱 大气压 (cabin_pressure): " << p.data.cabin_pressure * 0.2 << " kPa\n";
|
||||
|
||||
os << "动力舱 氧气浓度 (power_cabin_ox_concentration): " << p.data.power_cabin_ox_concentration * 0.1 << "%\n";
|
||||
os << "动力舱 温度 (power_cabin_temperature): " << static_cast<int16_t> (p.data.power_cabin_temperature) * 0.1 << "°C\n";
|
||||
os << "动力舱 湿度 (power_cabin_humidity): " << p.data.power_cabin_humidity * 0.1 << "% RH\n";
|
||||
os << "动力舱 大气压 (power_cabin_pressure): " << p.data.power_cabin_pressure * 0.2 << " kPa\n";
|
||||
|
||||
for (int i = 0; i < 6; ++i)
|
||||
os << "动力电池报警标志 dyn_alarm_flag[" << i << "]: 0x" << std::hex << static_cast<int>(p.data.dyn_alarm_flag[i]) << "\n";
|
||||
for (int i = 0; i < 6; ++i)
|
||||
os << "仪表电池报警标志 ins_alarm_flag[" << i << "]: 0x" << std::hex << static_cast<int>(p.data.ins_alarm_flag[i]) << "\n";
|
||||
|
||||
|
||||
os << "仪表正极/充电继电器状态 (ins_relay_status1): 0x" << std::hex << static_cast<int>(p.data.ins_relay_status1) << "\n";
|
||||
os << "仪表预充/负极继电器状态 (ins_relay_status2): 0x" << static_cast<int>(p.data.ins_relay_status2) << "\n";
|
||||
os << "动力正极/充电继电器状态 (dyn_relay_status1): 0x" << static_cast<int>(p.data.dyn_relay_status1) << "\n";
|
||||
os << "动力预充/负极继电器状态 (dyn_relay_status2): 0x" << static_cast<int>(p.data.dyn_relay_status2) << "\n";
|
||||
|
||||
os << std::dec;
|
||||
os << "仪表最大放电功率 (ins_max_discharge_power): " << p.data.ins_max_discharge_power * 0.05 << " kW\n";
|
||||
os << "动力最大放电功率 (dyn_max_discharge_power): " << p.data.dyn_max_discharge_power * 0.05 << " kW\n";
|
||||
|
||||
os << "仪表SOC (ins_soc): " << static_cast<int>(p.data.ins_soc) << "%\n";
|
||||
os << "动力SOC (dyn_soc): " << static_cast<int>(p.data.dyn_soc) << "%\n";
|
||||
|
||||
os << "仪表电池总能量 (ins_total_energy): " << p.data.ins_total_energy * 0.1 << " kWh\n";
|
||||
os << "动力电池总能量 (dyn_total_energy): " << p.data.dyn_total_energy * 0.1 << " kWh\n";
|
||||
|
||||
os << "仪表输入功率 (ins_power_input): " << p.data.ins_power_input * 0.1 << " kW\n";
|
||||
os << "动力输入功率 (dyn_power_input): " << p.data.dyn_power_input * 0.1 << " kW\n";
|
||||
|
||||
os << "仪表充电状态 (ins_charge_status): 0x" << std::hex << static_cast<int>(p.data.ins_charge_status) << "\n";
|
||||
os << "动力充电状态 (dyn_charge_status): 0x" << static_cast<int>(p.data.dyn_charge_status) << "\n";
|
||||
|
||||
os << std::dec;
|
||||
os << "仪表LINK电压: " << p.data.ins_voltage_link * 0.01 << " V\n";
|
||||
os << "仪表PACK电压: " << p.data.ins_voltage_pack * 0.01 << " V\n";
|
||||
os << "仪表电流: " << static_cast<int16_t> (p.data.ins_current) * 0.05 << " A\n";
|
||||
os << "仪表正端绝缘电阻: " << p.data.ins_resistance_pos << " kΩ\n";
|
||||
os << "仪表负端绝缘电阻: " << p.data.ins_resistance_neg << " kΩ\n";
|
||||
os << "动力LINK电压: " << p.data.dyn_voltage_link * 0.01 << " V\n";
|
||||
os << "动力PACK电压: " << p.data.dyn_voltage_pack * 0.01 << " V\n";
|
||||
os << "动力电流: " << static_cast<int16_t> (p.data.dyn_current) * 0.05 << " A\n";
|
||||
os << "动力正端绝缘电阻: " << p.data.dyn_resistance_pos << " kΩ\n";
|
||||
os << "动力负端绝缘电阻: " << p.data.dyn_resistance_neg << " kΩ\n";
|
||||
|
||||
os << "仪表紧急状态 (ins_emergency_status): 0x" << std::hex << static_cast<int>(p.data.ins_emergency_status) << "\n";
|
||||
os << "动力紧急状态 (dyn_emergency_status): 0x" << static_cast<int>(p.data.dyn_emergency_status) << "\n";
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
os << "保留字段 reserved2[" << i << "]: 0x" << std::hex << static_cast<int>(p.data.reserved2[i]) << "\n";
|
||||
os << "SOC门限值 ins_soc_threshold1: 0x" << std::hex << static_cast<int>(p.data.ins_soc_threshold1) << "\n";
|
||||
os << "SOC门限值 ins_soc_threshold2: 0x" << std::hex << static_cast<int>(p.data.ins_soc_threshold2) << "\n";
|
||||
os << "SOC门限值 ins_soc_threshold3: 0x" << std::hex << static_cast<int>(p.data.ins_soc_threshold3) << "\n";
|
||||
os << "SOC门限值 dyn_soc_threshold1: 0x" << std::hex << static_cast<int>(p.data.dyn_soc_threshold1) << "\n";
|
||||
os << "SOC门限值 dyn_soc_threshold2: 0x" << std::hex << static_cast<int>(p.data.dyn_soc_threshold2) << "\n";
|
||||
os << "SOC门限值 dyn_soc_threshold3: 0x" << std::hex << static_cast<int>(p.data.dyn_soc_threshold3) << "\n";
|
||||
os << "功率限制 ins_power_limit1: 0x" << std::hex << static_cast<int>(p.data.ins_power_limit1) << "\n";
|
||||
os << "功率限制 ins_power_limit2: 0x" << std::hex << static_cast<int>(p.data.ins_power_limit2) << "\n";
|
||||
os << "功率限制 dyn_power_limit1: 0x" << std::hex << static_cast<int>(p.data.dyn_power_limit1) << "\n";
|
||||
os << "功率限制 dyn_power_limit2: 0x" << std::hex << static_cast<int>(p.data.dyn_power_limit2) << "\n";
|
||||
|
||||
os << "设备在线状态1 (device_online_flag1): 0x" << std::hex << p.data.device_online_flag1 << "\n";
|
||||
os << "设备在线状态2 (device_online_flag2): 0x" << p.data.device_online_flag2 << "\n";
|
||||
|
||||
// os << "保留字段 reserved3: 0x" << p.data.reserved1 << "\n";
|
||||
os << "主机心跳 (reserved4): 0x" << p.data.heartbeat << "\n";
|
||||
os << "校验和 (checksum): 0x" << p.checkCode << "\n";
|
||||
|
||||
os << std::dec;
|
||||
}
|
||||
//================================================================================================================
|
||||
// 高电压母线断路器操作函数
|
||||
bool LowerCommManager::operate_HV_fuelCellCircuit_Breaker(char opt){
|
||||
|
||||
@@ -95,7 +95,6 @@ public:
|
||||
// 启动/停止监听线程
|
||||
void startListenThread();
|
||||
void stopListenThread();
|
||||
void coutMsg(const msg_CcuStateFbMsg& p, std::ostream& os = std::cout);
|
||||
|
||||
//转换函数
|
||||
char getBreakerOption(char cmd);
|
||||
|
||||
@@ -43,6 +43,7 @@ PowerManger::PowerManger()
|
||||
m_leakage = 0;
|
||||
m_thrustRpm = 0;
|
||||
m_thrustEnable = 0;
|
||||
m_insData = {};
|
||||
faultCode.clear();
|
||||
|
||||
m_skew = 10;
|
||||
@@ -259,6 +260,7 @@ void PowerManger::registerVariables()
|
||||
Register(m_upperCommManager->m_secdistStateMsg.key_fb,0);
|
||||
Register(DB_TO_PM_MISSION,0);
|
||||
Register(DB_TO_PM_DEEPTH,0);
|
||||
Register(DB_TO_PM_INSDATA,0);
|
||||
Register(DB_TO_PM_SETOPCONDITION,0);
|
||||
Register(DB_TO_PM_SETOPMODE,0);
|
||||
Register(DB_TO_PM_SETPMSYS,0);
|
||||
|
||||
@@ -97,6 +97,7 @@ class PowerManger : public AppCastingMOOSApp
|
||||
DriverTable m_subDisSysState;
|
||||
DriverTable m_subDisSysFault;
|
||||
int m_depth;
|
||||
InsDataState m_insData;
|
||||
MissionState m_missionState;
|
||||
//===========================STATE=========================================
|
||||
unsigned char workCondition;
|
||||
|
||||
@@ -8,6 +8,7 @@ UpperCommManager::UpperCommManager(PowerManger* pm) :
|
||||
m_secdistStateMsg(),
|
||||
m_setOpConditionMsg(),
|
||||
m_depthMsg(),
|
||||
m_insDataMsg(),
|
||||
m_taskStartMsg(),
|
||||
m_equInsStateMsg(),
|
||||
m_equStateMsg(),
|
||||
@@ -77,6 +78,15 @@ bool UpperCommManager::processUpperMsg(const string& key, const string& strJson)
|
||||
m_pm->m_depth = m_depthMsg.getDepth(key, strJson);
|
||||
return true;
|
||||
}
|
||||
// 惯导姿态与深度获取
|
||||
else if(key == m_insDataMsg.key)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
m_insDataMsg_st_debug = std::make_pair(key, strJson);
|
||||
#endif
|
||||
m_insDataMsg.parseMsg(key, strJson, m_pm->m_insData);
|
||||
return true;
|
||||
}
|
||||
else if(key == m_taskStartMsg.key)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "upmsg/uPower_fcsState.h"
|
||||
#include "upmsg/uPower_liBatState.h"
|
||||
#include "upmsg/uDevice_depth.h"
|
||||
#include "upmsg/uDevice_insData.h"
|
||||
#include "upmsg/uExternComm_setOpCondition.h"
|
||||
#include "upmsg/uExternComm_setOpMode.h"
|
||||
#include "upmsg/uPlan_taskStart.h"
|
||||
@@ -39,6 +40,7 @@ using namespace std;
|
||||
|
||||
//决策消息
|
||||
#define DB_TO_PM_DEEPTH "uDevice_depth_st"
|
||||
#define DB_TO_PM_INSDATA "uDevice_insData_st"
|
||||
// #define DB_TO_PM_MISSION "uPlan_taskStart_cmd"
|
||||
#define DB_TO_PM_TASKSTOP "uPlan_planStop_st"
|
||||
|
||||
@@ -77,6 +79,7 @@ public:
|
||||
|
||||
//上位机订阅消息
|
||||
uDevice_depthMsg m_depthMsg;
|
||||
uDevice_insDataMsg m_insDataMsg;
|
||||
uExternComm_setOpConditionMsg m_setOpConditionMsg;
|
||||
uExternComm_setOpModeMsg m_setOpModeMsg;
|
||||
uPlan_taskStartMsg m_taskStartMsg;
|
||||
@@ -104,6 +107,7 @@ public:
|
||||
std::pair<string, string> m_liBatStateMsg_st_debug;
|
||||
|
||||
std::pair<string, string> m_depthMsg_st_debug;
|
||||
std::pair<string, string> m_insDataMsg_st_debug;
|
||||
std::pair<string, string> m_setOpConditionMsg_st_debug;
|
||||
std::pair<string, string> m_setOpModeMsg_st_debug;
|
||||
std::pair<string, string> m_taskStartMsg_st_debug;
|
||||
|
||||
@@ -524,6 +524,18 @@ void HttpServer::pushSystemData() {
|
||||
//帧尾
|
||||
j["emergency_command"] = Json::Value(m_systemData->emergency_command);
|
||||
|
||||
//惯导姿态/深度数据(uDevice_insData_st)
|
||||
if (m_powerManger) {
|
||||
j["ins_heading_angle"] = Json::Value(m_powerManger->m_insData.headingAngle_int32); // 航向角
|
||||
j["ins_roll_angle"] = Json::Value(m_powerManger->m_insData.rollAngle_int32); // 横摇角
|
||||
j["ins_pitch_angle"] = Json::Value(m_powerManger->m_insData.pitchAngle_int32); // 纵摇角
|
||||
j["ins_depth"] = Json::Value(m_powerManger->m_insData.depth_int32); // 平台深度
|
||||
j["ins_heading_angular_velocity"] = Json::Value(m_powerManger->m_insData.headingAngularVelocity_int32);
|
||||
j["ins_roll_angular_velocity"] = Json::Value(m_powerManger->m_insData.rollAngularVelocity_int32);
|
||||
j["ins_pitch_angular_velocity"] = Json::Value(m_powerManger->m_insData.longAngularVelocity_int32);
|
||||
j["depth_pressure_avg"] = Json::Value(m_powerManger->m_depth); // 深度计压力平均
|
||||
}
|
||||
|
||||
//温度压力参数
|
||||
j["palladium_temp"] = Json::Value(m_systemData->palladium_temp); // 钯膜最高温度,单位:℃
|
||||
j["main_pipe_pressure"] = Json::Value(m_systemData->main_pipe_pressure); // 主水路压力,单位:0.01kPa
|
||||
@@ -871,6 +883,9 @@ void HttpServer::pushSystemData() {
|
||||
j["debug"]["m_depthMsg_st_debug"]["name"] = Json::Value(m_powerManger->m_upperCommManager->m_depthMsg_st_debug.first);
|
||||
j["debug"]["m_depthMsg_st_debug"]["content"] = Json::Value(m_powerManger->m_upperCommManager->m_depthMsg_st_debug.second);
|
||||
|
||||
j["debug"]["m_insDataMsg_st_debug"]["name"] = Json::Value(m_powerManger->m_upperCommManager->m_insDataMsg_st_debug.first);
|
||||
j["debug"]["m_insDataMsg_st_debug"]["content"] = Json::Value(m_powerManger->m_upperCommManager->m_insDataMsg_st_debug.second);
|
||||
|
||||
j["debug"]["m_setOpConditionMsg_st_debug"]["name"] = Json::Value(m_powerManger->m_upperCommManager->m_setOpConditionMsg_st_debug.first);
|
||||
j["debug"]["m_setOpConditionMsg_st_debug"]["content"] = Json::Value(m_powerManger->m_upperCommManager->m_setOpConditionMsg_st_debug.second);
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
//决策消息
|
||||
//惯导导航信息 uDevice_insData_st,包含航向角/横摇角/纵摇角及平台深度等数据
|
||||
#ifndef UDEVICE_INSDATA_ST_H
|
||||
#define UDEVICE_INSDATA_ST_H
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "../logc/loguru.hpp"
|
||||
#include "json/json.h"
|
||||
|
||||
#define DB_TO_PM_INSDATA "uDevice_insData_st"
|
||||
|
||||
typedef struct {
|
||||
int32_t headingAngle_int32; // 航向角,类型:int32_t
|
||||
int32_t rollAngle_int32; // 横摇角,类型:int32_t
|
||||
int32_t pitchAngle_int32; // 纵摇角,类型:int32_t
|
||||
int32_t depth_int32; // 平台深度,类型:int32_t
|
||||
int32_t headingAngularVelocity_int32; // 航向角速度,类型:int32_t
|
||||
int32_t rollAngularVelocity_int32; // 横摇角速度,类型:int32_t
|
||||
int32_t longAngularVelocity_int32; // 纵倾角速度,类型:int32_t
|
||||
int32_t lon_int32; // 经度,类型:int32_t
|
||||
int32_t lat_int32; // 纬度,类型:int32_t
|
||||
int16_t northSpeed_int16; // 北速,类型:int16_t
|
||||
int16_t eastSpeed_int16; // 东速,类型:int16_t
|
||||
int16_t zSpeed_int16; // 垂速,类型:int16_t
|
||||
} InsDataState;
|
||||
|
||||
class uDevice_insDataMsg
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
std::string key = "uDevice_insData_st";
|
||||
uDevice_insDataMsg(/* args */){};
|
||||
~uDevice_insDataMsg(){};
|
||||
|
||||
int parseMsg(const std::string& msgkey, const std::string& msg, InsDataState& state){
|
||||
if (msgkey != DB_TO_PM_INSDATA)
|
||||
{
|
||||
LOG_F(WARNING, "消息类型错误 %s", msgkey.c_str());
|
||||
return 0;
|
||||
}
|
||||
state = {};
|
||||
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 -1;
|
||||
}
|
||||
// 姿态角与深度(必含)
|
||||
if (root.isMember("headingAngle_int32") && root["headingAngle_int32"].isInt())
|
||||
state.headingAngle_int32 = root["headingAngle_int32"].asInt();
|
||||
if (root.isMember("rollAngle_int32") && root["rollAngle_int32"].isInt())
|
||||
state.rollAngle_int32 = root["rollAngle_int32"].asInt();
|
||||
if (root.isMember("pitchAngle_int32") && root["pitchAngle_int32"].isInt())
|
||||
state.pitchAngle_int32 = root["pitchAngle_int32"].asInt();
|
||||
if (root.isMember("depth_int32") && root["depth_int32"].isInt())
|
||||
state.depth_int32 = root["depth_int32"].asInt();
|
||||
// 角速度(可选)
|
||||
if (root.isMember("headingAngularVelocity_int32") && root["headingAngularVelocity_int32"].isInt())
|
||||
state.headingAngularVelocity_int32 = root["headingAngularVelocity_int32"].asInt();
|
||||
if (root.isMember("rollAngularVelocity_int32") && root["rollAngularVelocity_int32"].isInt())
|
||||
state.rollAngularVelocity_int32 = root["rollAngularVelocity_int32"].asInt();
|
||||
if (root.isMember("longAngularVelocity_int32") && root["longAngularVelocity_int32"].isInt())
|
||||
state.longAngularVelocity_int32 = root["longAngularVelocity_int32"].asInt();
|
||||
// 位置与速度(可选)
|
||||
if (root.isMember("lon_int32") && root["lon_int32"].isInt())
|
||||
state.lon_int32 = root["lon_int32"].asInt();
|
||||
if (root.isMember("lat_int32") && root["lat_int32"].isInt())
|
||||
state.lat_int32 = root["lat_int32"].asInt();
|
||||
if (root.isMember("northSpeed_int16") && root["northSpeed_int16"].isInt())
|
||||
state.northSpeed_int16 = root["northSpeed_int16"].asInt();
|
||||
if (root.isMember("eastSpeed_int16") && root["eastSpeed_int16"].isInt())
|
||||
state.eastSpeed_int16 = root["eastSpeed_int16"].asInt();
|
||||
if (root.isMember("zSpeed_int16") && root["zSpeed_int16"].isInt())
|
||||
state.zSpeed_int16 = root["zSpeed_int16"].asInt();
|
||||
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -363,4 +363,7 @@ void HostSim::registerFeedbackVars()
|
||||
Register("uPower_equInsState_fb", 0); // 设备仪表状态
|
||||
Register("uPower_secdistState_cmd", 0);// 次级配电状态
|
||||
Register("uPower_subDisFault_st", 0); // 次级配电故障状态(_DEBUG 下发布)
|
||||
// 姿态/深度数据(航行控制器→数据中心,供监视)
|
||||
Register("uDevice_depth_st", 0); // 深度信息
|
||||
Register("uDevice_insData_st", 0); // 惯导导航信息(姿态/深度)
|
||||
}
|
||||
|
||||
@@ -275,6 +275,7 @@
|
||||
<nav id="tabs">
|
||||
<button data-tab="home" class="active">主页</button>
|
||||
<button data-tab="equipment">设备控制</button>
|
||||
<button data-tab="attitude">姿态/深度</button>
|
||||
<button data-tab="monitor">反馈监控</button>
|
||||
<button data-tab="chart">曲线</button>
|
||||
<button data-tab="log">日志</button>
|
||||
@@ -344,6 +345,61 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<!-- 姿态/深度 -->
|
||||
<div id="page-attitude" class="page">
|
||||
<section>
|
||||
<h2>深度信息 <span class="muted">(uDevice_depth_st · 航行控制器→数据中心)</span></h2>
|
||||
<div class="row">
|
||||
<label>深度计1</label><input id="depthVal1" type="number" min="0" max="1000000" value="0" />
|
||||
<label>深度计2</label><input id="depthVal2" type="number" min="0" max="1000000" value="0" />
|
||||
<label>深度计3</label><input id="depthVal3" type="number" min="0" max="1000000" value="0" />
|
||||
<button id="btnAttDepthSend">下发深度</button>
|
||||
</div>
|
||||
<div class="muted">单位:0.001dbar,范围 0~1000000;约 1000 ≈ 1m 水深</div>
|
||||
<div class="muted" id="depthEcho"></div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h2>惯导导航信息 <span class="muted">(uDevice_insData_st · 航行控制器→数据中心)</span></h2>
|
||||
<div class="row">
|
||||
<label>航向角</label><input id="insHeading" type="number" value="0" />
|
||||
<label>横摇角</label><input id="insRoll" type="number" value="0" />
|
||||
<label>纵摇角</label><input id="insPitch" type="number" value="0" />
|
||||
<label>平台深度</label><input id="insDepth" type="number" value="0" />
|
||||
</div>
|
||||
<div class="row">
|
||||
<label>航向角速度</label><input id="insHeadingRate" type="number" value="0" />
|
||||
<label>横摇角速度</label><input id="insRollRate" type="number" value="0" />
|
||||
<label>纵倾角速度</label><input id="insPitchRate" type="number" value="0" />
|
||||
</div>
|
||||
<div class="row">
|
||||
<label>经度</label><input id="insLon" type="number" value="0" />
|
||||
<label>纬度</label><input id="insLat" type="number" value="0" />
|
||||
<label>北速</label><input id="insNorth" type="number" value="0" />
|
||||
<label>东速</label><input id="insEast" type="number" value="0" />
|
||||
<label>垂速</label><input id="insZ" type="number" value="0" />
|
||||
</div>
|
||||
<div class="row">
|
||||
<button id="btnInsSend">下发姿态</button>
|
||||
<label><input type="checkbox" id="insAuto" /> 循环发送</label>
|
||||
<label>周期</label>
|
||||
<select id="insAutoPeriod">
|
||||
<option value="500">0.5s</option>
|
||||
<option value="1000" selected>1s</option>
|
||||
<option value="2000">2s</option>
|
||||
</select>
|
||||
<label><input type="checkbox" id="insAutoInc" /> 航向自动递增(0.01°/次)</label>
|
||||
</div>
|
||||
<div class="muted">角度/角速度单位遵循惯导协议(通常 0.01° 或 0.001° 分辨率,整型原始值)</div>
|
||||
<div class="muted" id="insEcho"></div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h2>最近下发展示</h2>
|
||||
<div id="attLastSent"><span class="muted">暂无</span></div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<!-- 反馈监控 -->
|
||||
<div id="page-monitor" class="page">
|
||||
<section>
|
||||
@@ -568,6 +624,37 @@ const FCS_LABELS = {
|
||||
faultCode4_uint16:"四级故障位", fcCommState_uint8:"通信状态"
|
||||
};
|
||||
|
||||
const INS_LABELS = {
|
||||
timeStamp_uint32:"时间戳(ms)", timeStart_uint32:"开机时间", source_int8:"数据源", state_int8:"工作状态",
|
||||
headingAngle_int32:"航向角", rollAngle_int32:"横摇角", pitchAngle_int32:"纵摇角", depth_int32:"平台深度",
|
||||
headingAngularVelocity_int32:"航向角速度", rollAngularVelocity_int32:"横摇角速度", longAngularVelocity_int32:"纵倾角速度",
|
||||
lon_int32:"经度", lat_int32:"纬度", northSpeed_int16:"北速", eastSpeed_int16:"东速", zSpeed_int16:"垂速",
|
||||
frontSpeed_int16:"前速", rightSpeed_int16:"右速", heavenlySpeed_int16:"天速"
|
||||
};
|
||||
|
||||
function renderInsData(d) { return renderKv(d, INS_LABELS); }
|
||||
|
||||
function renderDepth(d) {
|
||||
const arr = Array.isArray(d.value_array_int32) ? d.value_array_int32 : [];
|
||||
let html = '<table class="kv"><tbody>';
|
||||
if (!arr.length) {
|
||||
html += '<tr><td class="k">数据</td><td class="v">—</td></tr>';
|
||||
} else {
|
||||
arr.forEach((v, i) => {
|
||||
html += `<tr><td class="k">深度计${i+1} (0.001dbar)</td><td class="v">${esc(v)} ≈ ${(v * 0.00102).toFixed(2)}m</td></tr>`;
|
||||
});
|
||||
}
|
||||
html += "</tbody></table>";
|
||||
if (arr.length) {
|
||||
const avg = arr.reduce((a, b) => a + b, 0) / arr.length;
|
||||
html += '<div class="stats">' +
|
||||
stat("平均(原始)", avg.toFixed(0), "acc") +
|
||||
stat("约水深", (avg * 0.00102).toFixed(2) + " m", "acc") +
|
||||
"</div>";
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 4. 全局状态
|
||||
// ============================================================
|
||||
@@ -823,6 +910,109 @@ function setAllDevices(v) {
|
||||
updateEquipmentSelects();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 9.5 姿态/深度(uDevice_insData_st / uDevice_depth_st)
|
||||
// ============================================================
|
||||
let lastDepthSent = null;
|
||||
let lastInsSent = null;
|
||||
let insAutoTimer = null;
|
||||
let insHeadingCounter = 0;
|
||||
|
||||
function num(id) { return Number(el(id).value) || 0; }
|
||||
|
||||
function buildDepthJson() {
|
||||
return { value_array_int32: [num("depthVal1"), num("depthVal2"), num("depthVal3")] };
|
||||
}
|
||||
|
||||
function buildInsJson() {
|
||||
return {
|
||||
timeStamp_uint32: Date.now(),
|
||||
source_int8: 1,
|
||||
state_int8: 4,
|
||||
headingAngle_int32: num("insHeading"),
|
||||
rollAngle_int32: num("insRoll"),
|
||||
pitchAngle_int32: num("insPitch"),
|
||||
depth_int32: num("insDepth"),
|
||||
headingAngularVelocity_int32: num("insHeadingRate"),
|
||||
rollAngularVelocity_int32: num("insRollRate"),
|
||||
longAngularVelocity_int32: num("insPitchRate"),
|
||||
lon_int32: num("insLon"),
|
||||
lat_int32: num("insLat"),
|
||||
northSpeed_int16: num("insNorth"),
|
||||
eastSpeed_int16: num("insEast"),
|
||||
zSpeed_int16: num("insZ")
|
||||
};
|
||||
}
|
||||
|
||||
function sendAttDepth() {
|
||||
const obj = buildDepthJson();
|
||||
lastDepthSent = obj;
|
||||
sendMsg("uDevice_depth_st", obj);
|
||||
const avg = obj.value_array_int32.reduce((a, b) => a + b, 0) / 3;
|
||||
el("depthEcho").textContent =
|
||||
"已下发: " + obj.value_array_int32.join(", ") + " (0.001dbar) · 平均约 " + (avg * 0.00102).toFixed(2) + " m";
|
||||
renderLastSent();
|
||||
}
|
||||
|
||||
function sendAttIns() {
|
||||
let obj = buildInsJson();
|
||||
if (el("insAutoInc").checked) {
|
||||
obj.headingAngle_int32 = insHeadingCounter;
|
||||
el("insHeading").value = insHeadingCounter;
|
||||
}
|
||||
lastInsSent = obj;
|
||||
sendMsg("uDevice_insData_st", obj);
|
||||
const t = new Date(obj.timeStamp_uint32).toLocaleTimeString();
|
||||
el("insEcho").textContent =
|
||||
"已下发: 航向=" + obj.headingAngle_int32 + " 横摇=" + obj.rollAngle_int32 +
|
||||
" 纵摇=" + obj.pitchAngle_int32 + " 深度=" + obj.depth_int32 + " @" + t;
|
||||
renderLastSent();
|
||||
}
|
||||
|
||||
function toggleInsAuto() {
|
||||
if (insAutoTimer) { clearInterval(insAutoTimer); insAutoTimer = null; }
|
||||
if (el("insAuto").checked) {
|
||||
insHeadingCounter = num("insHeading");
|
||||
const period = Number(el("insAutoPeriod").value);
|
||||
insAutoTimer = setInterval(() => {
|
||||
if (el("insAutoInc").checked) {
|
||||
insHeadingCounter = (insHeadingCounter + 1) % 36000;
|
||||
}
|
||||
sendAttIns();
|
||||
}, period);
|
||||
el("insEcho").textContent += " · 循环发送已启动";
|
||||
}
|
||||
}
|
||||
|
||||
function renderLastSent() {
|
||||
const box = el("attLastSent");
|
||||
let html = "";
|
||||
if (lastInsSent) {
|
||||
html += "<h3>惯导导航信息 (uDevice_insData_st)</h3>" + renderInsData(lastInsSent);
|
||||
}
|
||||
if (lastDepthSent) {
|
||||
html += "<h3>深度信息 (uDevice_depth_st)</h3>" + renderDepth(lastDepthSent);
|
||||
}
|
||||
if (lastInsSent || lastDepthSent) {
|
||||
html += '<div class="row" style="margin-top:8px"><button class="sec" id="btnShowRaw">查看原始 JSON</button><pre id="attRaw" style="display:none"></pre></div>';
|
||||
}
|
||||
box.innerHTML = html || '<span class="muted">暂无</span>';
|
||||
const btn = el("btnShowRaw");
|
||||
if (btn) {
|
||||
btn.onclick = () => {
|
||||
const pre = el("attRaw");
|
||||
if (pre.style.display === "none") {
|
||||
pre.textContent = JSON.stringify({ uDevice_depth_st: lastDepthSent, uDevice_insData_st: lastInsSent }, null, 2);
|
||||
pre.style.display = "block";
|
||||
btn.textContent = "收起原始 JSON";
|
||||
} else {
|
||||
pre.style.display = "none";
|
||||
btn.textContent = "查看原始 JSON";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 10. 反馈监控页
|
||||
// ============================================================
|
||||
@@ -943,7 +1133,9 @@ const FEEDBACKS = [
|
||||
["uPower_equState_fb","设备状态", renderEquState],
|
||||
["uPower_equInsState_fb","设备仪表状态", renderEquIns],
|
||||
["uPower_secdistState_cmd","次级配电状态", renderSecdist],
|
||||
["uPower_subDisFault_st","次级配电故障", renderSubFault]
|
||||
["uPower_subDisFault_st","次级配电故障", renderSubFault],
|
||||
["uDevice_depth_st","深度信息", renderDepth],
|
||||
["uDevice_insData_st","惯导导航信息", renderInsData]
|
||||
];
|
||||
|
||||
let currentMonitorKey = FEEDBACKS[0][0];
|
||||
@@ -1219,6 +1411,11 @@ function init() {
|
||||
sendMsg(k, v);
|
||||
};
|
||||
|
||||
// 姿态/深度页
|
||||
el("btnAttDepthSend").onclick = sendAttDepth;
|
||||
el("btnInsSend").onclick = sendAttIns;
|
||||
el("insAuto").onchange = toggleInsAuto;
|
||||
|
||||
// 设备控制页
|
||||
el("btnSendTable").onclick = () => {
|
||||
sendMsg("uPower_equState_cmd", deviceCmdState);
|
||||
@@ -1245,6 +1442,7 @@ function init() {
|
||||
renderEquipmentStructure();
|
||||
renderMonitor();
|
||||
renderPmOverview();
|
||||
renderLastSent();
|
||||
renderSeriesBox();
|
||||
drawChart();
|
||||
connect();
|
||||
|
||||
Reference in New Issue
Block a user