Merge pull request 'refactor-power-manager' (#1) from refactor-power-manager into main

Reviewed-on: https://gitea.zhaojingkui.xyz/zjk/H100PowerManger/pulls/1
This commit is contained in:
zjk
2025-04-09 23:31:04 +08:00
24 changed files with 1613 additions and 1360 deletions
+26
View File
@@ -0,0 +1,26 @@
@startuml PowerManagerFsm状态图
[*] --> InitState
state InitState {
[*] --> 等待复合管控消息
等待复合管控消息 --> 等待关键断路器闭合: 连接成功
等待复合管控消息 --> FaultState: 超时(10秒)
等待关键断路器闭合 --> StandbyState: 关键断路器闭合
等待关键断路器闭合 --> FaultState: 系统错误
}
state StandbyState {
[*] --> 待机
待机 --> FaultState: 检测到错误
待机 --> 处理电源命令: 收到命令
处理电源命令 --> 待机: 完成处理
}
state FaultState {
[*] --> 故障处理
}
InitState --> FaultState : FaultEvent(faultCode=1/2)
StandbyState --> FaultState : FaultEvent(faultCode)
InitState --> StandbyState : StandbyEvent
@enduml
+1 -4
View File
@@ -1,8 +1,5 @@
//------------------------------------------------
// pPowerManger config block
ProcessConfig = pPowerManger
{
AppTick = 5
AppTick = 4
CommsTick = 4
}
+3
View File
@@ -0,0 +1,3 @@
{
"value_array_uint16" :[2,1,3]
}
+1 -1
View File
@@ -6,7 +6,7 @@
"HVABbusBVol_uint16": 0,
"HVABmastSteGeaCur_uint16": 0,
"HVABopenWaterCur_uint16": 0,
"HVABxczCur_uint16": 0,
"HVABxczCur_uint16": 1,
"HVBBCCUCur_uint16": 0,
"HVBBFCSSecurSysCur_uint16": 0,
"HVBBbatInsCur_uint16": 0,
+3 -3
View File
@@ -1,8 +1,8 @@
{
"HVolABusBreaker_unit16": 0,
"HVolBBusBreaker_unit16": 0,
"HVolBusBreaker_uint16": 0,
"LVolBusBreaker_uint16": 0,
"HVolBBusBreaker_unit16": 79,
"HVolBusBreaker_uint16": 58,
"LVolBusBreaker_uint16": 159,
"faultCode_array":
[
0
+16 -16
View File
@@ -1,30 +1,30 @@
{
"backpressure1_fp32": 0.0,
"backpressure2_fp32": 0.0,
"ch4oConcent1_fp32": 0.0,
"ch4oConcent2_fp32": 0.0,
"backpressure1_fp32": 1.0,
"backpressure2_fp32": 2.0,
"ch4oConcent1_fp32": 29.0,
"ch4oConcent2_fp32": 96.0,
"ch4o_fp32": 0.0,
"excTemp_fp32": 0.0,
"excTemp_fp32": 23.0,
"faultCode1_uint16": 0,
"faultCode2_uint16": 0,
"faultCode3_uint16": 0,
"fcuCurIns_fp32": 0.0,
"fcuCurPower_fp32": 0.0,
"fcuCur_fp32": 0.0,
"fcuCur_fp32": 84.0,
"fcuFaultLeve_uint8": 0,
"fcuOutIns_fp32": 0.0,
"fcuOutPower_fp32": 0.0,
"fcuOutIns_fp32": 53.0,
"fcuOutPower_fp32": 100.0,
"fcuState_uint8": 0,
"fcuVolIns_fp32": 0.0,
"fcuVolPower_fp32": 0.0,
"fcuVol_fp32": 0.0,
"flameDet_uint8": 0,
"h2Concent1_fp32": 0.0,
"h2Concent2_fp32": 0.0,
"o2Concent1_fp32": 0.0,
"o2Concent2_fp32": 0.0,
"fcuVol_fp32": 377.0,
"flameDet_uint8": 1,
"h2Concent1_fp32": 89.0,
"h2Concent2_fp32": 62.0,
"o2Concent1_fp32": 74.0,
"o2Concent2_fp32": 21.0,
"o2Level_fp32": 0.0,
"o2_fp32": 0.0,
"powLimit_fp32": 0.0,
"powerGen_fp32": 0.0
"powLimit_fp32": 88.0,
"powerGen_fp32": 622.0
}
+11 -11
View File
@@ -2,37 +2,37 @@
"fcuCurToInsLimit1_uint8": 0,
"fcuCurToinsLimit2_uint8": 0,
"insBatCurElect_uint16": 0,
"insBatLimit_uint16": 0,
"insBatSoc_uint8": 0,
"insBatTotalElect_uint16": 0,
"insBatLimit_uint16": 41,
"insBatSoc_uint8": 42,
"insBatTotalElect_uint16": 617,
"insChargeState_uint8": 0,
"insCurSocHold1_uint8": 0,
"insCurSocHold2_uint8": 0,
"insCurSocHold3_uint8": 0,
"insCurrent_uint16": 0,
"insCurrent_uint16": 2,
"insLinkVol_uint16": 0,
"insNegRelayState_uint8": 0,
"insNegRes_uint16": 0,
"insPackVol_uint16": 0,
"insPosRelayState_uint8": 0,
"insPosRelayState_uint8": 1,
"insPosRes_uint16": 0,
"insfaultCode_uint8": 0,
"powBatCurElect_uint16": 0,
"powBatSoc_uint8": 0,
"powBatTotalElect_uint16": 0,
"powBatSoc_uint8": 31,
"powBatTotalElect_uint16": 763,
"powChargeState_uint8": 0,
"powCurSocHold1_uint8": 0,
"powCurSocHold2_uint8": 0,
"powCurSocHold3_uint8": 0,
"powCurToInsLimit1_uint8": 0,
"powCurToinsLimit2_uint8": 0,
"powCur_uint16": 0,
"powCur_uint16": 21,
"powLinkVol_uint16": 0,
"powNegRelayState_uint8": 0,
"powNegRelayState_uint8": 1,
"powNegRes_uint16": 0,
"powPosRelayState_uint8": 0,
"powPosRelayState_uint8": 1,
"powPosRes_uint16": 0,
"powerBatLimit_uint16": 0,
"powerBatLimit_uint16": 13,
"powerPackVol_uint16": 0,
"powfaultCode_uint8": 0
}
+128
View File
@@ -0,0 +1,128 @@
# PowerManager系统架构优化设计方案
## 1. 总体架构
```mermaid
classDiagram
class PowerManagerCore {
+startup()
+shutdown()
+getSystemState()
}
class StateMachineService {
+triggerEvent()
+getCurrentState()
+addStateListener()
}
class CommunicationMediator {
+registerHandler()
+sendMessage()
+addMessageListener()
}
PowerManagerCore --> StateMachineService
PowerManagerCore --> CommunicationMediator
```
## 2. 状态机重构方案
### 2.1 状态设计模式
```cpp
class IPowerState {
public:
virtual void onEntry(PowerContext&) = 0;
virtual void handleEvent(PowerContext&, const Event&) = 0;
virtual void onExit(PowerContext&) = 0;
};
```
### 2.2 状态转换规则
```yaml
# states_config.yaml
transitions:
- from: STANDBY
event: START_CMD
to: OPERATING
- from: OPERATING
event: STOP_CMD
to: STANDBY
```
## 3. 通信中介者设计
### 3.1 核心架构
```mermaid
sequenceDiagram
participant Upper as 上位机
participant Mediator as 通信中介者
participant Lower as 下位机
Upper->>Mediator: 控制命令(JSON)
Mediator->>Lower: 设备指令(ProtocolBuffer)
Lower->>Mediator: 状态更新
Mediator->>Upper: 系统报告
```
### 3.2 关键实现
```cpp
class CommunicationMediator {
// 线程安全消息队列
moodycamel::ConcurrentQueue<Message> queue;
// 消息处理线程
void processMessages() {
while (running) {
Message msg;
if (queue.try_dequeue(msg)) {
handlers[msg.type]->handle(msg);
}
}
}
};
```
## 4. 数据库服务设计
### 4.1 日志策略模式
```cpp
class ILogStrategy {
public:
virtual bool log(const SystemLog& log) = 0;
};
class SQLiteLogStrategy : public ILogStrategy {
// SQLite实现...
};
class FileLogStrategy : public ILogStrategy {
// 文件系统实现...
};
```
## 5. 实施计划
### 5.1 阶段划分
```mermaid
gantt
title 重构实施甘特图
section 核心框架
接口定义 :2025-04-08, 3d
基础实现 :2025-04-11, 5d
section 模块重构
状态机 :2025-04-16, 5d
通信模块 :2025-04-21, 5d
section 测试验证
单元测试 :2025-04-26, 3d
集成测试 :2025-04-29, 5d
```
## 6. 性能指标
| 模块 | 当前QPS | 目标QPS | 优化手段 |
|----------------|---------|---------|-----------------------|
| 状态机处理 | 100 | 500 | 无锁状态转换 |
| 消息处理 | 200 | 1000 | 批量处理+线程池 |
| 数据库写入 | 50 | 300 | 异步写入+批量提交 |
+5
View File
@@ -0,0 +1,5 @@
ProcessConfig = pPowerManger
{
AppTick = 4
CommsTick = 4
}
+12 -9
View File
@@ -19,13 +19,15 @@ endif (${WIN32})
find_package(SQLite3 REQUIRED)
SET(SRC
PowerManagerFsm.cpp
PowerManger.cpp
PowerManger_Info.cpp
udpComm.cpp
main.cpp
SQLiteDB.cpp
driver.cpp
PowerManagerFsm.cpp
LowerCommManager.cpp
UpperCommManager.cpp
)
ADD_EXECUTABLE(pPowerManger ${SRC})
@@ -56,17 +58,18 @@ SET(FSMLIB_SRC
SET(DRIVER_SRC
driver.cpp)
SET(POWERMANGE_SRC
PowerManger.cpp
PowerManger_Info.cpp
udpComm.cpp
SQLiteDB.cpp
driver.cpp
PowerManagerFsm.cpp)
# SET(POWERMANGE_SRC
# PowerManger.cpp
# PowerManger_Info.cpp
# udpComm.cpp
# SQLiteDB.cpp
# driver.cpp
# PowerManagerFsm.cpp)
ADD_LIBRARY(SQLiteDB STATIC ${SQLITEDB_SRC})
ADD_LIBRARY(UDPComm STATIC ${UDPCOMM_SRC})
ADD_LIBRARY(FSMLib STATIC ${FSMLIB_SRC})
ADD_LIBRARY(Driver STATIC ${DRIVER_SRC})
ADD_LIBRARY(PowerMange STATIC ${POWERMANGE_SRC})
# ADD_LIBRARY(PowerMange STATIC ${POWERMANGE_SRC})
+326
View File
@@ -0,0 +1,326 @@
#include "LowerCommManager.h"
#include "PowerManger.h"
#include "driver.h"
#include "udpComm.h"
bool _ListenCB(void * pParam)
{
LowerCommManager* pMe = (LowerCommManager* )pParam;
return pMe->listenThreadFunc();
}
LowerCommManager::LowerCommManager(PowerManger* pm, long myPort, long ccuPort, const std::string& cccHost){
m_pm = pm;
m_udpComm = new udpComm();
m_port = myPort;
m_udpComm->ccuHost = cccHost;
m_udpComm->ccuPort = ccuPort;
m_udpComm->udpScoket = new XPCUdpSocket(m_port);
std::cout << "PORT = " << m_port << std::endl;
std::cout << "CCUHOST = " << m_udpComm->ccuHost << std::endl;
std::cout << "CCUPORT = " << m_udpComm->ccuPort << std::endl;
}
LowerCommManager::~LowerCommManager(){
try {
stopListenThread();
if (m_udpComm) {
delete m_udpComm;
m_udpComm = nullptr;
}
} catch (...) {
cerr << "Error during cleanup" << endl;
}
}
bool LowerCommManager::initUdpComm(){
//设置UDP监听端口
if(m_udpComm->udpScoket)
{
try
{
m_udpComm->udpScoket->vBindSocket();
cout<<MOOS::ConsoleColours::green()<< "UDP set succeed!"<< MOOS::ConsoleColours::reset() << endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
cout<<MOOS::ConsoleColours::red()<< "UDP set Fild!"<< MOOS::ConsoleColours::reset() << endl;
return false;
}
}
else
{
cout<<MOOS::ConsoleColours::red()<< "UDP set Fild!"<< MOOS::ConsoleColours::reset() << endl;
return false;
}
return true;
}
void LowerCommManager::processPeriodicTasks() {
sendCcuCommand(m_pm->m_CcuColCmd);
sendDisSysCommand(m_pm->m_targetDisBreakerState);
m_udpComm->clearMsgQueue();
}
bool LowerCommManager::sendCcuCommand(const ccuColCmd cmd){
// m_pm->m_db->insertCcuSysCmd(cmd);
if(!m_udpComm->sendCcuColCmd(cmd))
cout << "send ccu command failed!" << endl;
return true;
}
bool LowerCommManager::sendDisSysCommand(const disSysBreakerList& cmd){
//根据目标状态进行断路器操作
//a. 高压母线的断路器
disHighVolBusCmd a;
a.fuelCellCircuitBreaker = getBreakerOption(cmd.bus1Breaker.fuelCellCircuitBreaker);
a.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(cmd.bus1Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
a.dcDc5ModuleCircuitBreaker = getBreakerOption(cmd.bus1Breaker.dcDc5ModuleCircuitBreaker);
a.powerLithiumBatteryCircuitBreaker = getBreakerOption(cmd.bus1Breaker.powerLithiumBatteryCircuitBreaker);
a.propulsionMotorCircuitBreaker = getBreakerOption(cmd.bus1Breaker.propulsionMotorCircuitBreaker);
a.bowHighVoltageDistributionBoxCircuitBreaker = getBreakerOption(cmd.bus1Breaker.bowHighVoltageDistributionBoxCircuitBreaker);
a.tyzReservedCircuitBreaker = getBreakerOption(cmd.bus1Breaker.tyzReservedCircuitBreaker);
a.sternFTDevice45CircuitBreaker = getBreakerOption(cmd.bus1Breaker.sternFTDevice45CircuitBreaker);
a.sternRudderSwitch1CircuitBreaker = getBreakerOption(cmd.bus1Breaker.sternRudderSwitch1CircuitBreaker);
a.sternRudderSwitch2CircuitBreaker = getBreakerOption(cmd.bus1Breaker.sternRudderSwitch2CircuitBreaker);
a.reservedCircuitBreaker = getBreakerOption(cmd.bus1Breaker.reservedCircuitBreaker);
a.fbReservedCircuitBreaker = getBreakerOption(cmd.bus1Breaker.fbReservedCircuitBreaker);
m_udpComm->sendDisSysCmd(a);
//b. 高压汇流排A断路器
disHighAVolBusCmd b;
b.bowFTDevice123CircuitBreaker = getBreakerOption(cmd.bus2Breaker.bowFTDevice123CircuitBreaker);
b.actuatorCircuitBreaker = getBreakerOption(cmd.bus2Breaker.actuatorCircuitBreaker);
b.mastSteeringGearControlBoxCircuitBreaker = getBreakerOption(cmd.bus2Breaker.mastSteeringGearControlBoxCircuitBreaker);
b.xczCircuitBreaker = getBreakerOption(cmd.bus2Breaker.xczCircuitBreaker);
b.bowRudderControlBoxCircuitBreaker = getBreakerOption(cmd.bus2Breaker.bowRudderControlBoxCircuitBreaker);
b.openWaterCoverStartCylinderCircuitBreaker = getBreakerOption(cmd.bus2Breaker.openWaterCoverStartCylinderCircuitBreaker);
m_udpComm->sendDisSysCmd(b);
//c. 高压汇流排B断路器
disHighBVolBusCmd c;
c.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(cmd.bus3Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
c.bowLowVoltageDistributionBoxCircuitBreaker = getBreakerOption(cmd.bus3Breaker.bowLowVoltageDistributionBoxCircuitBreaker);
c.unit4InstrumentDC48VCircuitBreaker = getBreakerOption(cmd.bus3Breaker.unit4InstrumentDC48VCircuitBreaker);
c.dcC1DCDistributionPanelCircuitBreaker = getBreakerOption(cmd.bus3Breaker.dcC1DCDistributionPanelCircuitBreaker);
c.reservedCircuitBreaker1 = getBreakerOption(cmd.bus3Breaker.reservedCircuitBreaker1);
c.reservedCircuitBreaker2 = getBreakerOption(cmd.bus3Breaker.reservedCircuitBreaker2);
c.emergencyLithiumBatteryGroup2CircuitBreaker = getBreakerOption(cmd.bus3Breaker.emergencyLithiumBatteryGroup2CircuitBreaker);
m_udpComm->sendDisSysCmd(c);
//d. 闭合低压汇流排断路器
disLowBusCmd d;
d.unit1CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit1CircuitBreaker);
d.unit2CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit2CircuitBreaker);
d.unit3CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit3CircuitBreaker);
d.unit5CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit5CircuitBreaker);
d.bowPZDeviceCircuitBreaker = getBreakerOption(cmd.bus4Breaker.bowPZDeviceCircuitBreaker);
d.reservedCircuitBreaker1 = getBreakerOption(cmd.bus4Breaker.reservedCircuitBreaker1);
d.reservedCircuitBreaker2 = getBreakerOption(cmd.bus4Breaker.reservedCircuitBreaker2);
d.emergencyLithiumBatteryGroup1CircuitBreaker = getBreakerOption(cmd.bus4Breaker.emergencyLithiumBatteryGroup1CircuitBreaker);
m_udpComm->sendDisSysCmd(d);
//将指令写入数据库
m_pm->m_db->insertDisSysCmd(a);
m_pm->m_db->insertDisSysCmd(b);
m_pm->m_db->insertDisSysCmd(c);
m_pm->m_db->insertDisSysCmd(d);
return true;
}
void LowerCommManager::clearMsgQueue(){
m_udpComm->clearMsgQueue();
}
bool LowerCommManager::updatePoweSystemStates(){
// std::cout << "updatePoweSystemStates" << std::endl;
if(!m_udpComm->m_qReceiveCcuStateBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_CcuCurrentState);
m_pm->m_pmCurrentState.ccustate = m_CcuCurrentState.data;
// m_db->insertCcuData(m_CcuCurrentState.data); //写入数据库
msg_CcuStateFbMsg_Update_time = time;
}
if(!m_udpComm->m_qReceiveDisHighVolBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disHighVolBusCurrentState);
m_pm->m_pmCurrentState.bus1State = m_disHighVolBusCurrentState.data;
// m_db->insertDisHighVolBusData(m_disHighVolBusCurrentState.data); //写入数据库
msg_disHighVolBusFbMsg_Update_time = time;
disBus1Breaker* pdis1=new disBus1Breaker;
(m_pm->m_pmCurrentState.bus1State.fuelCellCircuitBreaker == BREAK_ON) ? pdis1 -> fuelCellCircuitBreaker = 1 : pdis1 -> fuelCellCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.powerLithiumBatteryCircuitBreaker == BREAK_ON) ? pdis1 -> powerLithiumBatteryCircuitBreaker = 1 : pdis1 -> powerLithiumBatteryCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.propulsionMotorCircuitBreaker == BREAK_ON) ? pdis1 -> propulsionMotorCircuitBreaker = 1 : pdis1 -> propulsionMotorCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.lithiumBatteryGroupInstrumentCircuitBreaker == BREAK_ON) ? pdis1 -> lithiumBatteryGroupInstrumentCircuitBreaker = 1 : pdis1 -> lithiumBatteryGroupInstrumentCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.dcDc5ModuleCircuitBreaker == BREAK_ON) ? pdis1 -> dcDc5ModuleCircuitBreaker = 1 : pdis1 -> dcDc5ModuleCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCircuitBreaker == BREAK_ON) ? pdis1 -> bowHighVoltageDistributionBoxCircuitBreaker = 1 : pdis1 -> bowHighVoltageDistributionBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.sternFTDevice45CircuitBreaker == BREAK_ON) ? pdis1 -> sternFTDevice45CircuitBreaker = 1 : pdis1 -> sternFTDevice45CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch1CircuitBreaker == BREAK_ON) ? pdis1 -> sternRudderSwitch1CircuitBreaker = 1 : pdis1 -> sternRudderSwitch1CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch2CircuitBreaker == BREAK_ON) ? pdis1 -> sternRudderSwitch2CircuitBreaker = 1 : pdis1 -> sternRudderSwitch2CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.fbReservedCircuitBreaker == BREAK_ON) ? pdis1 -> fbReservedCircuitBreaker = 1 : pdis1 -> fbReservedCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.tyzReservedCircuitBreaker == BREAK_ON) ? pdis1 -> tyzReservedCircuitBreaker = 1 : pdis1 -> tyzReservedCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.reservedCircuitBreaker == BREAK_ON) ? pdis1 -> reservedCircuitBreaker = 1 : pdis1 -> reservedCircuitBreaker = 0;
pdis1 -> paceholder = 0;
m_pm->m_currentDisBreakerState.bus1Breaker = *pdis1;
delete pdis1;
}
if(!m_udpComm->m_qReceiveDisHighAVolBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disHighAVolBusCurrentState);
m_pm->m_pmCurrentState.bus2State = m_disHighAVolBusCurrentState.data;
// m_db->insertDisHighAVolBusData(m_disHighAVolBusCurrentState.data); //写入数据库
msg_disHighAVolBusFbMsg_Update_time = time;
disBus2Breaker* pdis2=new disBus2Breaker;
(m_pm->m_pmCurrentState.bus2State.bowFTDevice123CircuitBreaker == BREAK_ON)? pdis2 -> bowFTDevice123CircuitBreaker = 1 : pdis2 -> bowFTDevice123CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.actuatorCircuitBreaker == BREAK_ON)? pdis2 -> actuatorCircuitBreaker = 1 : pdis2 -> actuatorCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.mastSteeringGearControlBoxCircuitBreaker == BREAK_ON)? pdis2 -> mastSteeringGearControlBoxCircuitBreaker = 1 : pdis2 -> mastSteeringGearControlBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.xczCircuitBreaker == BREAK_ON)? pdis2 -> xczCircuitBreaker = 1 : pdis2 -> xczCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.bowRudderControlBoxCircuitBreaker == BREAK_ON)? pdis2 -> bowRudderControlBoxCircuitBreaker = 1 : pdis2 -> bowRudderControlBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.openWaterCoverStartCylinderCircuitBreaker == BREAK_ON)? pdis2 -> openWaterCoverStartCylinderCircuitBreaker = 1 : pdis2 -> openWaterCoverStartCylinderCircuitBreaker = 0;
pdis2 -> paceholder = 0;
m_pm->m_currentDisBreakerState.bus2Breaker = *pdis2;
delete pdis2;
}
if(!m_udpComm->m_qReceiveDisHighBVolBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disHighBVolBusCurrentState);
m_pm->m_pmCurrentState.bus3State = m_disHighBVolBusCurrentState.data;
// m_db->insertDisHighBVolBusData(m_disHighBVolBusCurrentState.data); //写入数据库
msg_disHighBVolBusFbMsg_Update_time = time;
disBus3Breaker* pdis3=new disBus3Breaker;
(m_pm->m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCircuitBreaker == BREAK_ON)? pdis3 -> lithiumBatteryGroupInstrumentCircuitBreaker = 1 : pdis3 -> lithiumBatteryGroupInstrumentCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCircuitBreaker == BREAK_ON)? pdis3 -> bowLowVoltageDistributionBoxCircuitBreaker = 1 : pdis3 -> bowLowVoltageDistributionBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.unit4InstrumentDC48VCircuitBreaker == BREAK_ON)? pdis3 -> unit4InstrumentDC48VCircuitBreaker = 1 : pdis3 -> unit4InstrumentDC48VCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.dcC1DCDistributionPanelCircuitBreaker == BREAK_ON)? pdis3 -> dcC1DCDistributionPanelCircuitBreaker = 1 : pdis3 -> dcC1DCDistributionPanelCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.reservedCircuitBreaker1 == BREAK_ON)? pdis3 -> reservedCircuitBreaker1 = 1 : pdis3 -> reservedCircuitBreaker1 = 0;
(m_pm->m_pmCurrentState.bus3State.reservedCircuitBreaker2 == BREAK_ON)? pdis3 -> reservedCircuitBreaker2 = 1 : pdis3 -> reservedCircuitBreaker2 = 0;
(m_pm->m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2CircuitBreaker == BREAK_ON)? pdis3 -> emergencyLithiumBatteryGroup2CircuitBreaker = 1 : pdis3 -> emergencyLithiumBatteryGroup2CircuitBreaker = 0;
pdis3->paceholder = 0;
m_pm->m_currentDisBreakerState.bus3Breaker = *pdis3;
delete pdis3;
}
if(!m_udpComm->m_qReceiveDisLowBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disLowBusState);
m_pm->m_pmCurrentState.bus4State = m_disLowBusState.data;
// m_db->insertDisLowBusData(m_disLowBusState.data); //写入数据库
msg_disLowBusFbMsg_Update_time = time;
disBus4Breaker* pdis4=new disBus4Breaker;
(m_pm->m_pmCurrentState.bus4State.unit1CircuitBreaker == BREAK_ON)? pdis4 -> unit1CircuitBreaker = 1 : pdis4 -> unit1CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.unit2CircuitBreaker == BREAK_ON)? pdis4 -> unit2CircuitBreaker = 1 : pdis4 -> unit2CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.unit3CircuitBreaker == BREAK_ON)? pdis4 -> unit3CircuitBreaker = 1 : pdis4 -> unit3CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.unit5CircuitBreaker == BREAK_ON)? pdis4 -> unit5CircuitBreaker = 1 : pdis4 -> unit5CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.bowPZDeviceCircuitBreaker == BREAK_ON)? pdis4 -> bowPZDeviceCircuitBreaker = 1 : pdis4 -> bowPZDeviceCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.reservedCircuitBreaker1 == BREAK_ON)? pdis4 -> reservedCircuitBreaker1 = 1 : pdis4 -> reservedCircuitBreaker1 = 0;
(m_pm->m_pmCurrentState.bus4State.reservedCircuitBreaker2 == BREAK_ON)? pdis4 -> reservedCircuitBreaker2 = 1 : pdis4 -> reservedCircuitBreaker2 = 0;
(m_pm->m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1CircuitBreaker == BREAK_ON)? pdis4 -> emergencyLithiumBatteryGroup1CircuitBreaker = 1 : pdis4 -> emergencyLithiumBatteryGroup1CircuitBreaker = 0;
pdis4 -> paceholder = 0;
m_pm->m_currentDisBreakerState.bus4Breaker = *pdis4;
delete pdis4;
}
return true;
}
bool LowerCommManager::listenThreadFunc()
{
cout<<MOOS::ConsoleColours::green()<< "LowerCommManager::listenThreadFunc" << MOOS::ConsoleColours::reset() << endl;
unsigned char Buff[1024];
while(!m_ListenThread.IsQuitRequested())
{
try
{
int nRead = m_udpComm->udpScoket->iRecieveMessage(Buff, sizeof(Buff));
//cout<<MOOS::ConsoleColours::yellow()<< "RECEVE DATA COUNT:" << nRead << MOOS::ConsoleColours::reset() << endl;
if(nRead > 0)
{
try {
if(!m_udpComm->pushMsgToQueue(Buff))
{
for(int i=0; i<m_udpComm->sError.size(); i++)
{
cout<<MOOS::ConsoleColours::red()<< m_udpComm->sError[i] << MOOS::ConsoleColours::reset() << endl;
}
}
//更新当前状态
if(!updatePoweSystemStates())
{
std::cerr << "Error updating power system states" << std::endl;
}
}
catch(const std::exception& e) {
cerr << "Error processing message: " << e.what() << endl;
}
}
// 打印通信错误信息
if(!m_udpComm->sError.empty())
{
for(int i=0; i<m_udpComm->sError.size(); i++)
{
cout<<MOOS::ConsoleColours::red() << m_udpComm->sError[i] << MOOS::ConsoleColours::reset() << endl;
}
m_udpComm->sError.clear();
}
}
catch(const XPCException& e)
{
cerr << "XPCException in listenThreadFunc" << endl;
// 短暂休眠后继续尝试
MOOSPause(100);
}
catch(const std::exception& e)
{
cerr << "Exception in listenThreadFunc: " << e.what() << endl;
MOOSPause(100);
}
catch(...)
{
cerr << "Unknown exception in listenThreadFunc" << endl;
MOOSPause(100);
}
}
return true;
}
void LowerCommManager::startListenThread()
{
if(!m_ListenThread.IsThreadRunning())
{
m_ListenThread.Initialise(_ListenCB,this);
m_ListenThread.Start();
std::cout << "LowerCommManager::startListenThread" << std::endl;
}
}
void LowerCommManager::stopListenThread()
{
if(m_ListenThread.IsThreadRunning())
{
m_ListenThread.Stop();
std::cout << "LowerCommManager::stopListenThread" << std::endl;
}
}
char LowerCommManager::getBreakerOption(char cmd)
{
if(cmd ==1)
return 0x55;
else if(cmd ==0)
return 0xAA;
else
return 0x00;
}
//TODO:根据心跳检查连接状态
bool LowerCommManager::checkConnection()
{
m_pm->connectState =5;
}
+82
View File
@@ -0,0 +1,82 @@
#ifndef LOWER_COMM_MANAGER_H
#define LOWER_COMM_MANAGER_H
#define UNIX
#include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
#include "MOOS/libMOOS/Utils/MOOSThread.h"
#include "MOOS/libMOOS/Utils/MOOSLock.h"
#include "ccuUdpMsg.h"
#include "pmSysvariable.h"
#include <queue>
#include <map>
#include <functional>
#include <atomic>
#include <vector>
#include "udpComm.h"
class PowerManger; // 前向声明
class LowerCommManager {
public:
LowerCommManager(PowerManger* pm, long myPort, long ccuPort, const std::string& cccHost);
~LowerCommManager();
//processPeriodicTasks
void processPeriodicTasks();
//clearMsgQueue
void clearMsgQueue();
public:
// 初始化UDP通信
bool initUdpComm();
// TODO:实现连接检查功能
bool checkConnection();
// 更新设备状态
bool updatePoweSystemStates();
// 发送CCU控制命令
bool sendCcuCommand(const ccuColCmd cmd);
bool sendDisSysCommand(const disSysBreakerList& cmd);
// 获取通信状态
int getConnectionState() const { return m_connectState; }
// 启动/停止监听线程
void startListenThread();
void stopListenThread();
//转换函数
char getBreakerOption(char cmd);
// 配电和CCU的反馈状态
msg_CcuStateFbMsg m_CcuCurrentState;
msg_disHighVolBusFbMsg m_disHighVolBusCurrentState;
msg_disHighAVolBusFbMsg m_disHighAVolBusCurrentState;
msg_disHighBVolBusFbMsg m_disHighBVolBusCurrentState;
msg_disLowBusFbMsg m_disLowBusState;
// 配电和CCU的反馈状态时间戳
double msg_CcuStateFbMsg_Update_time;
// double msg_CcuSetParmFbMsg_Update_time;
double msg_disHighVolBusFbMsg_Update_time;
double msg_disHighAVolBusFbMsg_Update_time;
double msg_disHighBVolBusFbMsg_Update_time;
double msg_disLowBusFbMsg_Update_time;
public:
long m_port;
long m_ccuPort;
std::string m_ccuHost;
std::string m_host;
PowerManger* m_pm;
udpComm* m_udpComm;
CMOOSThread m_ListenThread;
int m_connectState;
// UDP监听线程函数
bool listenThreadFunc();
};
#endif // LOWER_COMM_MANAGER_H
+54 -18
View File
@@ -1,29 +1,65 @@
#include "PowerManagerFsm.hpp"
#include "PowerManger.h"
#include <string>
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
// 初始化静态成员
// 定义静态成员
PowerManger* PowerManagerFsm::pm = nullptr;
// 状态实例化
InitState initState;
StandbyState standbyState;
FaultState faultState;
// 状态机初始化
void PowerManagerFsm::init(PowerManger* powerManager) {
pm = powerManager;
start();
// InitState实现
void InitState::entry() {
PowerManagerFsm::entry();
std::cout << "初始化配电指令" << std::endl;
pm->initControlCmd();
// PowerManagerFsm::pm->printState("InitState", "进入初始化状态");
transit<StandbyState>();
}
// 状态转换接口实现
void PowerManagerFsm::triggerInitEvent() {
dispatch(InitEvent());
// StandbyState实现
void StandbyState::entry() {
PowerManagerFsm::entry();
pm->m_driver.getSTANDBYTable(&pm->m_subDisSysCmd);
pm->m_CcuColCmd.fcuCmd = FcuCommand::Stop;
pm->m_CcuColCmd.powerBatCmd = BatteryCommand::Stop;
}
void PowerManagerFsm::triggerStandbyEvent() {
dispatch(StandbyEvent());
void StandbyState::react(StandbyEvent const &) {
transit<StandbyState>();
}
void PowerManagerFsm::triggerFaultEvent(unsigned int faultCode) {
dispatch(FaultEvent(faultCode));
}
void StandbyState::handlePowerCommands() {
switch (PowerManagerFsm::pm->m_msCmd.powerCMD) {
case 0: PowerManagerFsm::pm->m_pmEvent = "收到上位机控制指令:关机"; break;
case 1: handlePowerOn(); break;
}
}
void StandbyState::handlePowerOn() {
PowerManagerFsm::pm->m_pmEvent = "动力电上电";
PowerManagerFsm::pm->m_CcuColCmd.powerBatCmd = 0x10;
if(PowerManagerFsm::pm->m_pmCurrentState.ccustate.fcuState == 0x04)
PowerManagerFsm::pm->m_CcuColCmd.fcuCmd = 0x02;
}
// FaultState实现
void FaultState::entry() {
PowerManagerFsm::entry();
PowerManagerFsm::pm->printState("FaultState", "进入故障状态");
}
void FaultState::react(FaultEvent const & e) {
switch (e.faultCode) {
case 1:
PowerManagerFsm::pm->m_pmEvent = "复合管控连接超时";
PowerManagerFsm::pm->printState("FaultState", "通信故障");
break;
case 2:
PowerManagerFsm::pm->m_pmEvent = "能源系统故障";
PowerManagerFsm::pm->printState("FaultState", "能源故障");
break;
}
}
// 设置初始状态
FSM_INITIAL_STATE(PowerManagerFsm, InitState);
+69 -120
View File
@@ -1,157 +1,106 @@
#ifndef POWER_MANAGER_FSM_HPP
#define POWER_MANAGER_FSM_HPP
#include "tinyfsm.hpp"
#include <iostream>
#include <chrono> // 新增头文件
#include <iomanip> // 新增头文件
#include <ctime> // 新增头文件,用于localtime函数
#include <cxxabi.h> // 新增头文件,用于demangle功能
#include "pmSysvariable.h"
#include "driver.h"
// 前向声明
class PowerManger;
// 定义事件类型
enum class PowerSystemState {
INIT,
STANDBY,
FAULT,
};
//==================状态切换事件=============================
struct InitEvent : tinyfsm::Event { };
struct StandbyEvent : tinyfsm::Event { };
struct FaultEvent : tinyfsm::Event {
struct FaultEvent : tinyfsm::Event {
unsigned int faultCode;
FaultEvent(unsigned int code) : faultCode(code) {}
};
//==================状态流程事件============================
struct MasterCommandEvent : tinyfsm::Event {};
struct TaskStart : tinyfsm::Event { };
struct DeviceCmd : tinyfsm::Event { };
// 定义状态机基类
class PowerManagerFsm : public tinyfsm::Fsm<PowerManagerFsm> {
protected:
static void* pm;
static PowerManger* pm;
static DriverTable m_currentDriver;
public:
// 初始化状态机
static void init(void* powerManager) {
static void init(PowerManger* powerManager) {
pm = powerManager;
}
// 类型安全转换接口
static PowerManger* getPowerManager() {
return static_cast<PowerManger*>(pm);
}
// 响应事件默认实现
virtual void react(tinyfsm::Event const &) { }
virtual void react(tinyfsm::Event const &) { std::cout << "[ Reseve Envt ] " << "No Idear" << std::endl; }
virtual void react(InitEvent const &) {};
virtual void react(StandbyEvent const &) {};
virtual void react(FaultEvent const &) {};
virtual void react(MasterCommandEvent const &) {};
virtual void react(TaskStart const &) {};
virtual void react(DeviceCmd const &) {};
// 状态转换接口
void triggerInitEvent() { dispatch(InitEvent()); }
void triggerStandbyEvent() { dispatch(StandbyEvent()); }
void triggerFaultEvent(unsigned int faultCode) { dispatch(FaultEvent(faultCode)); }
// 状态机生命周期方法
virtual void entry() {
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
int status = 0;
char* demangled = abi::__cxa_demangle(typeid(*this).name(), nullptr, nullptr, &status);
std::string typeName = (status == 0) ? demangled : typeid(*this).name();
if (demangled) free(demangled);
std::cout << "\033[1;38;5;51m╭──────────────────────────────────────────────────────\n"
<< "│ \033[1;38;5;226m⮞⮞⮞ \033[0;38;5;51m[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S")
<< "] \033[1;38;5;255m进入状态: \033[1;38;5;213m" << typeName << "\n"
<< "│ \033[0m" << std::endl;
}
virtual void exit() {
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
int status = 0;
char* demangled = abi::__cxa_demangle(typeid(*this).name(), nullptr, nullptr, &status);
std::string typeName = (status == 0) ? demangled : typeid(*this).name();
if (demangled) free(demangled);
std::cout << "\033[1;38;5;203m│ \n"
<< "│ \033[1;38;5;208m⮜⮜⮜ \033[0;38;5;203m[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S")
<< "] \033[1;38;5;255m离开状态: \033[1;38;5;213m" << typeName << "\n"
<< "╰──────────────────────────────────────────────────────\033[0m" << std::endl;
}
};
// 初始化静态成员
void* PowerManagerFsm::pm = nullptr;
// 定义状态类
class InitState : public PowerManagerFsm {
public:
void entry() {
PowerManagerFsm::getPowerManager()->m_pmEvent = "初始化状态";
PowerManagerFsm::getPowerManager()->printState("InitState", "进入初始化状态");
}
void react(InitEvent const &) {
// 1. 检查通信是否正常
double StartTime = PowerManagerFsm::getPowerManager()->GetAppStartTime();
while (PowerManagerFsm::getPowerManager()->connectState < 5) {
MOOSPause(1000);
PowerManagerFsm::getPowerManager()->updateTime();
double dft = abs(PowerManagerFsm::getPowerManager()->m_current_time - StartTime);
if(dft > 10) {
transit<FaultState>();
PowerManagerFsm::pm->m_faultNum = 1;
return;
}
string desp = "等待复合管控消息 : " + to_string(10-dft);
PowerManagerFsm::pm->printState("InitState", desp);
}
// 2. 检查复合管控器是否上报故障
if(PowerManagerFsm::getPowerManager()->checkError() < 0) {
transit<FaultState>();
PowerManagerFsm::getPowerManager()->m_faultNum = 2;
return;
}
// 3. 初始化系统为待机状态
PowerManagerFsm::pm->initControlCmd();
// 4. 判断关键断路器是否闭合
while (true) {
if(PowerManagerFsm::getPowerManager()->m_currentDisBreakerState.bus3Breaker.unit4InstrumentDC48VCircuitBreaker == 1 ||
PowerManagerFsm::getPowerManager()->m_currentDisBreakerState.bus4Breaker.unit5CircuitBreaker == 1 ||
PowerManagerFsm::getPowerManager()->m_currentDisBreakerState.bus4Breaker.unit2CircuitBreaker == 1 ||
PowerManagerFsm::getPowerManager()->m_currentDisBreakerState.bus4Breaker.unit3CircuitBreaker == 1 ||
PowerManagerFsm::getPowerManager()->m_currentDisBreakerState.bus4Breaker.unit1CircuitBreaker == 1) {
transit<StandbyState>();
return;
}
PowerManagerFsm::getPowerManager()->m_pmEvent = "等待关键断路器闭合";
MOOSPause(1000);
PowerManagerFsm::getPowerManager()->printState("InitState", PowerManagerFsm::getPowerManager()->m_pmEvent);
}
}
void entry() override;
};
class StandbyState : public PowerManagerFsm {
public:
void entry() {
PowerManagerFsm::getPowerManager()->m_stateData.note = "待机状态";
PowerManagerFsm::getPowerManager()->printState("StandbyState", "进入待机状态");
}
void react(StandbyEvent const &) {
while (true) {
// 1. 检查当前是否存在故障
if(PowerManagerFsm::pm->checkError() < 0) {
transit<FaultState>();
return;
}
// 2. 处理上位机控制指令
handlePowerCommands();
MOOSPause(1000);
}
}
void entry() override;
void react(StandbyEvent const &) override;
// void react(MasterCommandEvent const &) override;
// void react(DeviceCmd const &) override;
private:
void handlePowerCommands() {
switch (PowerManagerFsm::pm->m_msCmd.powerCMD) {
case 0: PowerManagerFsm::pm->m_pmEvent = "收到上位机控制指令:关机"; break;
case 1: handlePowerOn(); break;
// 其他处理函数省略...
}
}
void handlePowerOn() {
PowerManagerFsm::pm->m_pmEvent = "动力电上电";
PowerManagerFsm::pm->m_CcuColCmd.powerBatCmd = 0x10;
if(PowerManagerFsm::pm->m_pmCurrentState.ccustate.fcuState == 0x04)
PowerManagerFsm::pm->m_CcuColCmd.fcuCmd = 0x02;
}
void handlePowerCommands();
void handlePowerOn();
};
class FaultState : public PowerManagerFsm {
public:
void entry() {
PowerManagerFsm::getPowerManager()->printState("FaultState", "进入故障状态");
}
void react(FaultEvent const & e) {
switch (e.faultCode) {
case 1:
PowerManagerFsm::pm->m_pmEvent = "复合管控连接超时";
PowerManagerFsm::pm->printState("FaultState", "通信故障");
break;
case 2:
PowerManagerFsm::pm->m_pmEvent = "能源系统故障";
PowerManagerFsm::pm->printState("FaultState", "能源故障");
break;
}
}
void entry() override;
void react(FaultEvent const &);
};
// 设置初始状态
FSM_INITIAL_STATE(PowerManagerFsm, InitState);
#endif // POWER_MANAGER_FSM_HPP
+39 -870
View File
@@ -14,12 +14,6 @@
using namespace std;
bool _ListenCB(void * pParam)
{
PowerManger* pMe = (PowerManger* )pParam;
return pMe->ListenLoopUDP();
}
bool _FsmCB(void * pParam)
{
PowerManger* pMe = (PowerManger* )pParam;
@@ -41,15 +35,6 @@ PowerManger::PowerManger()
m_ticker = 0;
faultCode.push_back(0);
msg_CcuStateFbMsg_Update_time = 0;
// msg_CcuSetParmFbMsg_Update_time = 0;
msg_disHighVolBusFbMsg_Update_time = 0;
msg_disHighAVolBusFbMsg_Update_time = 0;
msg_disHighBVolBusFbMsg_Update_time = 0;
msg_disLowBusFbMsg_Update_time = 0;
connectState = 0;
m_pmCurrentState = {};
m_currentDisBreakerState = {};
m_targetDisBreakerState = {};
@@ -57,7 +42,7 @@ PowerManger::PowerManger()
m_MSDriverCmd = {};
m_subDisSysCmd = {};
m_subDisSysState = {};
//初始化断路器状态
//初始化控制指令
initControlCmd();
//初始化数据库
@@ -70,10 +55,7 @@ PowerManger::PowerManger()
m_db->createDriversTables(VAR_TO_STR(m_subDisSysState));
m_db->createDriversTables(VAR_TO_STR(m_subDisSysCmd));
m_db->createDriversTables(VAR_TO_STR(m_MSDriverCmd));
// 初始化状态机
m_fsm.init(this);
PowerManagerFsm::start();
}
//---------------------------------------------------------
@@ -81,6 +63,8 @@ PowerManger::PowerManger()
PowerManger::~PowerManger()
{
delete m_db;
delete m_lowerCommManager;
}
//---------------------------------------------------------
@@ -97,32 +81,9 @@ bool PowerManger::OnNewMail(MOOSMSG_LIST &NewMail)
string key = msg.GetKey();
string strJson = msg.GetString();
//处理上位机发送的控制指令
if(key == DB_TO_PM_MSCMD)
if(!m_upperCommManager->processUpperMsg(key,strJson))
{
if(getMsCmdFromJson(m_msCmd,strJson))
{
cout << "get ms cmd:" << strJson << endl;
}
else
{
cout << MOOS::ConsoleColours::Red << "get ms cmd error:" << strJson << MOOS::ConsoleColours::reset << endl;
}
}
//处理设备上电指令
if (key == DB_TO_PM_EQUIPMENTCMD)
{
if(m_driver.loadDriverTableFromJson(strJson,m_subDisSysCmd) < 0)
{
cout << MOOS::ConsoleColours::Red << "get sub dis cmd error:" << strJson << MOOS::ConsoleColours::reset << endl;
}
}
//处理设备状态反馈
if(key == DB_TO_PM_SUBDISSTATE)
{
if(m_driver.loadDriverTableFromJson(strJson,m_subDisSysState) < 0)
{
cout << MOOS::ConsoleColours::Red << "get sub dis state error:" << strJson << MOOS::ConsoleColours::reset << endl;
}
cout << "processUpperMsg failed" << endl;
}
#if 0 // Keep these around just for template
@@ -161,42 +122,15 @@ bool PowerManger::OnConnectToServer()
bool PowerManger::Iterate()
{
AppCastingMOOSApp::Iterate();
//1. 更新当前状态
updatePmState();
updateConnectState();
//2. 运行状态机
if (FSM_RUN_IS_OK)
m_FsmThread.Start();
else
m_FsmThread.Stop();
//3. 进行操作 每秒执行一次
// 打印当前状态
// cout << " Current State : " << (int)PowerManagerFsm::current_state_ptr->getCurrentState();
if (m_ticker++ % 5 == 0)
{
//a. 配电系统操作
disSysOption();
//b. ccu操作
ccuOption();
//c. 上位机反馈
buildPowerSysReport();
Notify(PM_TO_DB_SUBDISCMD,m_driver.saveDriverTableToJson(&m_subDisSysCmd));
Notify(PM_TO_DB_EQUIPMENTSTATE,m_driver.saveDriverTableToJson(&m_currentDriverState));
//d.保存到数据库
m_CcuColCmd.heartbeat++;
m_lowerCommManager->processPeriodicTasks();
m_upperCommManager->processPeriodicTasks();
writeDataToDB();
}
///////////////////////
// string a = m_driver.getSubDisCmdJson(&m_subDisSysCmd);
// saveStringToJsonFile(a,"/home/zjk/project/H100/H100PowerManger/data/subDisSysCmd.json");
///////////////////////
//4. 写状态信息到数据库
m_stateData.state_id = m_pmState.id;
m_stateData.state_name = m_pmState.name;
m_stateData.event = m_pmEvent;
m_stateData.fault_code = m_faultNum;
if(!m_db->insertStateData(m_stateData))
return false;
//AppCastingMOOSApp::PostReport();
//5. 清空UDP消息队列
m_udpComm.clearMsgQueue();
return(true);
}
@@ -234,23 +168,19 @@ bool PowerManger::OnStartUp()
}
registerVariables();
//启动UDP监听线程
if(m_pCcuUdpComm = setUdpScoket(IPORT))
{
cout<<MOOS::ConsoleColours::green()<< "UDP set succeed!"<< MOOS::ConsoleColours::reset() << endl;
m_udpComm.ccuHost = CCUHOST;
m_udpComm.ccuPort = CCUPORT;
m_udpComm.udpScoket = m_pCcuUdpComm;
MOOSTrace("UDP set succeed");
}
else
{
cout<<MOOS::ConsoleColours::red()<< "UDP set Fild!"<< MOOS::ConsoleColours::reset() << endl;
MOOSTrace("UDP set faile");
}
m_ListenThread.Initialise(_ListenCB,this);
m_ListenThread.Start();
m_FsmThread.Initialise(_FsmCB,this);
//初始化UDP通信
m_lowerCommManager = new LowerCommManager(this,IPORT,CCUPORT,CCUHOST);
m_lowerCommManager->initUdpComm();
m_lowerCommManager->startListenThread();
//初始化上位机通信
m_upperCommManager = new UpperCommManager(this);
// 初始化状态机
PowerManagerFsm::init(this);
PowerManagerFsm::start();
InitEvent initEvent;
PowerManagerFsm::dispatch(initEvent);
return(true);
}
@@ -284,291 +214,29 @@ bool PowerManger::buildReport()
return(true);
}
//------------------------------------------------------------
// Procedure: setUdpScoket()
XPCUdpSocket* PowerManger::setUdpScoket(long lPort)
{
XPCUdpSocket* pUdpSocket = new XPCUdpSocket(lPort);
try
{
pUdpSocket->vBindSocket();
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
return NULL;
}
return pUdpSocket;
}
bool PowerManger::ListenLoopUDP()
{
unsigned char Buff[MAX_UDP_PKT_SIZE];
while(!m_ListenThread.IsQuitRequested())
{
int nRead = m_pCcuUdpComm->iRecieveMessage(Buff,sizeof(Buff));
cout<<MOOS::ConsoleColours::yellow()<< "RECEVE DATA COUNT:" << nRead << MOOS::ConsoleColours::reset() << endl;
if(nRead > 0)
{
if(!m_udpComm.pushMsgToQueue(Buff))
{
for(int i=0;i<m_udpComm.sError.size();i++)
{
cout<<MOOS::ConsoleColours::red()<< m_udpComm.sError[i] << MOOS::ConsoleColours::reset() << endl;
}
}
}
// 打印通信错误信息
if(!m_udpComm.sError.empty())
{
for(int i=0;i<m_udpComm.sError.size();i++)
{
cout<<MOOS::ConsoleColours::red() << m_udpComm.sError[i] << MOOS::ConsoleColours::reset() << endl;
}
m_udpComm.sError.clear();
}
}
return true;
}
bool PowerManger::FsmLoop()
{
//状态机循环
while (!m_FsmThread.IsQuitRequested())
{
// 检查故障并触发事件
if(int error = checkError()) {
triggerFaultEvent(error);
} else {
// 自动状态转换由状态机内部处理
MOOSPause(1000);
}
// while (!m_FsmThread.IsQuitRequested())
// {
// // // 检查故障并触发事件
// // if(int error = checkError()) {
// // m_fsm.triggerFaultEvent(error);
// // } else {
// // // 自动状态转换由状态机内部处理
// // MOOSPause(1000);
// // }
//延时1s
usleep(1000000);
}
// //延时1s
// usleep(1000000);
// }
return true;
}
bool PowerManger::updateConnectState()
{
double seck = 0;
updateTime();
connectState = 0;
if(!IsSkewed(msg_CcuStateFbMsg_Update_time,&seck))
connectState++;
// cout << "seck:" << seck << endl;
if(!IsSkewed(msg_disHighVolBusFbMsg_Update_time,&seck))
connectState++;
// cout << "seck:" << seck << endl;
if(!IsSkewed(msg_disHighAVolBusFbMsg_Update_time,&seck))
connectState++;
// cout << "seck:" << seck << endl;
if(!IsSkewed(msg_disHighBVolBusFbMsg_Update_time,&seck))
connectState++;
// cout << "seck:" << seck << endl;
if(!IsSkewed(msg_disLowBusFbMsg_Update_time,&seck))
connectState++;
// cout << "seck:" << seck << endl;
return true;
}
bool PowerManger::updatePmState()
{
//1. 更新下位机状态
if(!m_udpComm.m_qReceiveCcuStateBuffer.empty())
{
double time;
m_udpComm.popMsgFormQueue(time,m_CcuCurrentState);
m_pmCurrentState.ccustate = m_CcuCurrentState.data;
m_db->insertCcuData(m_CcuCurrentState.data); //写入数据库
msg_CcuStateFbMsg_Update_time = time;
}
if(!m_udpComm.m_qReceiveDisHighVolBusBuffer.empty())
{
double time;
m_udpComm.popMsgFormQueue(time,m_disHighVolBusCurrentState);
m_pmCurrentState.bus1State = m_disHighVolBusCurrentState.data;
m_db->insertDisHighVolBusData(m_disHighVolBusCurrentState.data); //写入数据库
msg_disHighVolBusFbMsg_Update_time = time;
disBus1Breaker* pdis1=new disBus1Breaker;
(m_pmCurrentState.bus1State.fuelCellCircuitBreaker == BREAK_ON) ? pdis1 -> fuelCellCircuitBreaker = 1 : pdis1 -> fuelCellCircuitBreaker = 0;
(m_pmCurrentState.bus1State.powerLithiumBatteryCircuitBreaker == BREAK_ON) ? pdis1 -> powerLithiumBatteryCircuitBreaker = 1 : pdis1 -> powerLithiumBatteryCircuitBreaker = 0;
(m_pmCurrentState.bus1State.propulsionMotorCircuitBreaker == BREAK_ON) ? pdis1 -> propulsionMotorCircuitBreaker = 1 : pdis1 -> propulsionMotorCircuitBreaker = 0;
(m_pmCurrentState.bus1State.lithiumBatteryGroupInstrumentCircuitBreaker == BREAK_ON) ? pdis1 -> lithiumBatteryGroupInstrumentCircuitBreaker = 1 : pdis1 -> lithiumBatteryGroupInstrumentCircuitBreaker = 0;
(m_pmCurrentState.bus1State.dcDc5ModuleCircuitBreaker == BREAK_ON) ? pdis1 -> dcDc5ModuleCircuitBreaker = 1 : pdis1 -> dcDc5ModuleCircuitBreaker = 0;
(m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCircuitBreaker == BREAK_ON) ? pdis1 -> bowHighVoltageDistributionBoxCircuitBreaker = 1 : pdis1 -> bowHighVoltageDistributionBoxCircuitBreaker = 0;
(m_pmCurrentState.bus1State.sternFTDevice45CircuitBreaker == BREAK_ON) ? pdis1 -> sternFTDevice45CircuitBreaker = 1 : pdis1 -> sternFTDevice45CircuitBreaker = 0;
(m_pmCurrentState.bus1State.sternRudderSwitch1CircuitBreaker == BREAK_ON) ? pdis1 -> sternRudderSwitch1CircuitBreaker = 1 : pdis1 -> sternRudderSwitch1CircuitBreaker = 0;
(m_pmCurrentState.bus1State.sternRudderSwitch2CircuitBreaker == BREAK_ON) ? pdis1 -> sternRudderSwitch2CircuitBreaker = 1 : pdis1 -> sternRudderSwitch2CircuitBreaker = 0;
(m_pmCurrentState.bus1State.fbReservedCircuitBreaker == BREAK_ON) ? pdis1 -> fbReservedCircuitBreaker = 1 : pdis1 -> fbReservedCircuitBreaker = 0;
(m_pmCurrentState.bus1State.tyzReservedCircuitBreaker == BREAK_ON) ? pdis1 -> tyzReservedCircuitBreaker = 1 : pdis1 -> tyzReservedCircuitBreaker = 0;
(m_pmCurrentState.bus1State.reservedCircuitBreaker == BREAK_ON) ? pdis1 -> reservedCircuitBreaker = 1 : pdis1 -> reservedCircuitBreaker = 0;
pdis1 -> paceholder = 0;
m_currentDisBreakerState.bus1Breaker = *pdis1;
delete pdis1;
}
if(!m_udpComm.m_qReceiveDisHighAVolBusBuffer.empty())
{
double time;
m_udpComm.popMsgFormQueue(time,m_disHighAVolBusCurrentState);
m_pmCurrentState.bus2State = m_disHighAVolBusCurrentState.data;
m_db->insertDisHighAVolBusData(m_disHighAVolBusCurrentState.data); //写入数据库
msg_disHighAVolBusFbMsg_Update_time = time;
disBus2Breaker* pdis2=new disBus2Breaker;
(m_pmCurrentState.bus2State.bowFTDevice123CircuitBreaker == BREAK_ON)? pdis2 -> bowFTDevice123CircuitBreaker = 1 : pdis2 -> bowFTDevice123CircuitBreaker = 0;
(m_pmCurrentState.bus2State.actuatorCircuitBreaker == BREAK_ON)? pdis2 -> actuatorCircuitBreaker = 1 : pdis2 -> actuatorCircuitBreaker = 0;
(m_pmCurrentState.bus2State.mastSteeringGearControlBoxCircuitBreaker == BREAK_ON)? pdis2 -> mastSteeringGearControlBoxCircuitBreaker = 1 : pdis2 -> mastSteeringGearControlBoxCircuitBreaker = 0;
(m_pmCurrentState.bus2State.xczCircuitBreaker == BREAK_ON)? pdis2 -> xczCircuitBreaker = 1 : pdis2 -> xczCircuitBreaker = 0;
(m_pmCurrentState.bus2State.bowRudderControlBoxCircuitBreaker == BREAK_ON)? pdis2 -> bowRudderControlBoxCircuitBreaker = 1 : pdis2 -> bowRudderControlBoxCircuitBreaker = 0;
(m_pmCurrentState.bus2State.openWaterCoverStartCylinderCircuitBreaker == BREAK_ON)? pdis2 -> openWaterCoverStartCylinderCircuitBreaker = 1 : pdis2 -> openWaterCoverStartCylinderCircuitBreaker = 0;
pdis2 -> paceholder = 0;
m_currentDisBreakerState.bus2Breaker = *pdis2;
delete pdis2;
}
if(!m_udpComm.m_qReceiveDisHighBVolBusBuffer.empty())
{
double time;
m_udpComm.popMsgFormQueue(time,m_disHighBVolBusCurrentState);
m_pmCurrentState.bus3State = m_disHighBVolBusCurrentState.data;
m_db->insertDisHighBVolBusData(m_disHighBVolBusCurrentState.data); //写入数据库
msg_disHighBVolBusFbMsg_Update_time = time;
disBus3Breaker* pdis3=new disBus3Breaker;
(m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCircuitBreaker == BREAK_ON)? pdis3 -> lithiumBatteryGroupInstrumentCircuitBreaker = 1 : pdis3 -> lithiumBatteryGroupInstrumentCircuitBreaker = 0;
(m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCircuitBreaker == BREAK_ON)? pdis3 -> bowLowVoltageDistributionBoxCircuitBreaker = 1 : pdis3 -> bowLowVoltageDistributionBoxCircuitBreaker = 0;
(m_pmCurrentState.bus3State.unit4InstrumentDC48VCircuitBreaker == BREAK_ON)? pdis3 -> unit4InstrumentDC48VCircuitBreaker = 1 : pdis3 -> unit4InstrumentDC48VCircuitBreaker = 0;
(m_pmCurrentState.bus3State.dcC1DCDistributionPanelCircuitBreaker == BREAK_ON)? pdis3 -> dcC1DCDistributionPanelCircuitBreaker = 1 : pdis3 -> dcC1DCDistributionPanelCircuitBreaker = 0;
(m_pmCurrentState.bus3State.reservedCircuitBreaker1 == BREAK_ON)? pdis3 -> reservedCircuitBreaker1 = 1 : pdis3 -> reservedCircuitBreaker1 = 0;
(m_pmCurrentState.bus3State.reservedCircuitBreaker2 == BREAK_ON)? pdis3 -> reservedCircuitBreaker2 = 1 : pdis3 -> reservedCircuitBreaker2 = 0;
(m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2CircuitBreaker == BREAK_ON)? pdis3 -> emergencyLithiumBatteryGroup2CircuitBreaker = 1 : pdis3 -> emergencyLithiumBatteryGroup2CircuitBreaker = 0;
pdis3->paceholder = 0;
m_currentDisBreakerState.bus3Breaker = *pdis3;
delete pdis3;
}
if(!m_udpComm.m_qReceiveDisLowBusBuffer.empty())
{
double time;
m_udpComm.popMsgFormQueue(time,m_disLowBusState);
m_pmCurrentState.bus4State = m_disLowBusState.data;
m_db->insertDisLowBusData(m_disLowBusState.data); //写入数据库
msg_disLowBusFbMsg_Update_time = time;
disBus4Breaker* pdis4=new disBus4Breaker;
(m_pmCurrentState.bus4State.unit1CircuitBreaker == BREAK_ON)? pdis4 -> unit1CircuitBreaker = 1 : pdis4 -> unit1CircuitBreaker = 0;
(m_pmCurrentState.bus4State.unit2CircuitBreaker == BREAK_ON)? pdis4 -> unit2CircuitBreaker = 1 : pdis4 -> unit2CircuitBreaker = 0;
(m_pmCurrentState.bus4State.unit3CircuitBreaker == BREAK_ON)? pdis4 -> unit3CircuitBreaker = 1 : pdis4 -> unit3CircuitBreaker = 0;
(m_pmCurrentState.bus4State.unit5CircuitBreaker == BREAK_ON)? pdis4 -> unit5CircuitBreaker = 1 : pdis4 -> unit5CircuitBreaker = 0;
(m_pmCurrentState.bus4State.bowPZDeviceCircuitBreaker == BREAK_ON)? pdis4 -> bowPZDeviceCircuitBreaker = 1 : pdis4 -> bowPZDeviceCircuitBreaker = 0;
(m_pmCurrentState.bus4State.reservedCircuitBreaker1 == BREAK_ON)? pdis4 -> reservedCircuitBreaker1 = 1 : pdis4 -> reservedCircuitBreaker1 = 0;
(m_pmCurrentState.bus4State.reservedCircuitBreaker2 == BREAK_ON)? pdis4 -> reservedCircuitBreaker2 = 1 : pdis4 -> reservedCircuitBreaker2 = 0;
(m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1CircuitBreaker == BREAK_ON)? pdis4 -> emergencyLithiumBatteryGroup1CircuitBreaker = 1 : pdis4 -> emergencyLithiumBatteryGroup1CircuitBreaker = 0;
pdis4 -> paceholder = 0;
m_currentDisBreakerState.bus4Breaker = *pdis4;
delete pdis4;
}
return true;
}
bool PowerManger::IsSkewed(double dfTimeNow, double * pdfSkew)
{
double dfSkew = fabs(dfTimeNow - m_current_time);
if(pdfSkew != NULL)
{
*pdfSkew = dfSkew;
}
return (dfSkew > SKEW_TOLERANCE) ? true : false;
}
void PowerManger::ccuOption()
{
m_db->insertCcuSysCmd(m_CcuColCmd);
if(!m_udpComm.sendCcuColCmd(m_CcuColCmd))
cout << "send ccu col cmd failed!" << endl;
}
void PowerManger::disSysOption()
{
//根据目标状态进行断路器操作
//a. 高压母线的断路器
disHighVolBusCmd a;
a.fuelCellCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.fuelCellCircuitBreaker);
a.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
a.dcDc5ModuleCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.dcDc5ModuleCircuitBreaker);
a.powerLithiumBatteryCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.powerLithiumBatteryCircuitBreaker);
a.propulsionMotorCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.propulsionMotorCircuitBreaker);
a.bowHighVoltageDistributionBoxCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.bowHighVoltageDistributionBoxCircuitBreaker);
a.tyzReservedCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.tyzReservedCircuitBreaker);
a.sternFTDevice45CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.sternFTDevice45CircuitBreaker);
a.sternRudderSwitch1CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.sternRudderSwitch1CircuitBreaker);
a.sternRudderSwitch2CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.sternRudderSwitch2CircuitBreaker);
a.reservedCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.reservedCircuitBreaker);
a.fbReservedCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.fbReservedCircuitBreaker);
m_udpComm.sendDisSysCmd(a);
//b. 高压汇流排A断路器
disHighAVolBusCmd b;
b.bowFTDevice123CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus2Breaker.bowFTDevice123CircuitBreaker);
b.actuatorCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus2Breaker.actuatorCircuitBreaker);
b.mastSteeringGearControlBoxCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus2Breaker.mastSteeringGearControlBoxCircuitBreaker);
b.xczCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus2Breaker.xczCircuitBreaker);
b.bowRudderControlBoxCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus2Breaker.bowRudderControlBoxCircuitBreaker);
b.openWaterCoverStartCylinderCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus2Breaker.openWaterCoverStartCylinderCircuitBreaker);
m_udpComm.sendDisSysCmd(b);
//c. 高压汇流排B断路器
disHighBVolBusCmd c;
c.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus3Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
c.bowLowVoltageDistributionBoxCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus3Breaker.bowLowVoltageDistributionBoxCircuitBreaker);
c.unit4InstrumentDC48VCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus3Breaker.unit4InstrumentDC48VCircuitBreaker);
c.dcC1DCDistributionPanelCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus3Breaker.dcC1DCDistributionPanelCircuitBreaker);
c.reservedCircuitBreaker1 = getBreakerOption(m_targetDisBreakerState.bus3Breaker.reservedCircuitBreaker1);
c.reservedCircuitBreaker2 = getBreakerOption(m_targetDisBreakerState.bus3Breaker.reservedCircuitBreaker2);
c.emergencyLithiumBatteryGroup2CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus3Breaker.emergencyLithiumBatteryGroup2CircuitBreaker);
m_udpComm.sendDisSysCmd(c);
//d. 闭合低压汇流排断路器
disLowBusCmd d;
d.unit1CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus4Breaker.unit1CircuitBreaker);
d.unit2CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus4Breaker.unit2CircuitBreaker);
d.unit3CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus4Breaker.unit3CircuitBreaker);
d.unit5CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus4Breaker.unit5CircuitBreaker);
d.bowPZDeviceCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus4Breaker.bowPZDeviceCircuitBreaker);
d.reservedCircuitBreaker1 = getBreakerOption(m_targetDisBreakerState.bus4Breaker.reservedCircuitBreaker1);
d.reservedCircuitBreaker2 = getBreakerOption(m_targetDisBreakerState.bus4Breaker.reservedCircuitBreaker2);
d.emergencyLithiumBatteryGroup1CircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus4Breaker.emergencyLithiumBatteryGroup1CircuitBreaker);
m_udpComm.sendDisSysCmd(d);
//将指令写入数据库
m_db->insertDisSysCmd(a);
m_db->insertDisSysCmd(b);
m_db->insertDisSysCmd(c);
m_db->insertDisSysCmd(d);
}
bool PowerManger::saveStringToJsonFile(const string &strJson, const string &filePath)
{
std::ofstream file(filePath, std::ios::trunc);
if (file.is_open()) {
file << strJson;
file.close();
// std::cout << "字符串已成功保存到文件中。" << std::endl;
return true;
} else {
std::cerr << "无法打开文件!" << std::endl;
return false;
}
}
bool PowerManger::writeDataToDB()
{
// //写入配电系统状态
// if(!m_db->insertDisCcuData(m_pmCurrentState))
// return false;
//写入设备状态
if(!m_db->insertDriverTables(m_currentDriverState,VAR_TO_STR(m_currentDriverState)))
return false;
@@ -597,25 +265,9 @@ int PowerManger::checkError()
//2.检查断路器状态
}
void PowerManger::triggerInitEvent()
{
PowerManagerFsm::dispatch(InitEvent());
}
void PowerManger::triggerStandbyEvent()
{
PowerManagerFsm::dispatch(StandbyEvent());
}
void PowerManger::triggerFaultEvent(unsigned int faultCode)
{
PowerManagerFsm::dispatch(FaultEvent(faultCode));
}
void PowerManger::printState(const std::string& stateName, const std::string& desp){
cout << MOOS::ConsoleColours::Yellow() << "STATE " << st.id << " [" << st.name << "] " << MOOS::ConsoleColours::reset();
cout << MOOS::ConsoleColours::Blue() << " -> " << MOOS::ConsoleColours::reset();
cout << MOOS::ConsoleColours::Yellow() << "STATE " << stateName << " " << MOOS::ConsoleColours::reset();
cout << MOOS::ConsoleColours::Blue() << " -> " << MOOS::ConsoleColours::reset();
// 检查desp是否有“故障”
if (desp.find("故障") != string::npos) {
cout << MOOS::ConsoleColours::Red() << desp << MOOS::ConsoleColours::reset() << endl;
@@ -625,7 +277,6 @@ void PowerManger::printState(const std::string& stateName, const std::string& de
}
//==============================系统操作函数====================
bool PowerManger::shutdown()
{
@@ -689,486 +340,4 @@ void PowerManger::initControlCmd()
m_msCmd.workCMD = 99;
//================初始化设备驱动指令========================
}
char PowerManger::getBreakerOption(char cmd)
{
if(cmd ==1)
return 0x55;
else if(cmd ==0)
return 0xAA;
else
return 0x00;
}
bool PowerManger::getMsCmdFromJson(MSControlCmd &cmd, const std::string &strJson)
{
Json::CharReaderBuilder builder;
Json::Value root;
std::string errs;
// 解析JSON字符串
std::istringstream s(strJson);
if (!Json::parseFromStream(builder, s, &root, &errs))
{
std::cerr << "JSON解析失败: " << errs << std::endl;
return false; // 解析失败
}
if (root.isMember("powerCMD") && root["powerCMD"].isUInt())
{
cmd.powerCMD = root["powerCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: powerCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("modCMD") && root["modCMD"].isUInt())
{
cmd.modCMD = root["modCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: modCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("workCMD") && root["workCMD"].isUInt())
{
cmd.workCMD = root["workCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: workCMD" << std::endl;
return false; // 缺少或类型不匹配
}
return true; // 成功
}
string PowerManger::getTypeName(const string typeName)
{
// 映射类型名称到指定的字符串
if (typeName == "i" || typeName == "c") {
return "_int8";
} else if (typeName == "h" || typeName == "C") {
return "_uint8";
} else if (typeName == "s" || typeName == "S") {
return "_int16";
} else if (typeName == "t" || typeName == "T") {
return "_uint16";
} else if (typeName == "i" || typeName == "I") {
return "_int32";
} else if (typeName == "j" || typeName == "J") {
return "_uint32";
} else if (typeName == "x" || typeName == "X") {
return "_int64";
} else if (typeName == "f" || typeName == "F") {
return "_fp32";
} else if (typeName == "d" || typeName == "D") {
return "_fp64";
} else if (typeName == "b" || typeName == "B") {
return "_bool";
} else if (typeName == "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
return "_str";
} else if (typeName == "St6vector" || typeName == "St5deque" || typeName == "St4list" || typeName == "St4pair") {
return "_array";
} else {
// 默认情况
return "_obj";
}
}
bool PowerManger::buildPowerSysReport()
{
//TODO: 构建能源系统报告
string suffix;
Json::Value pms;
Json::Value fcs;
Json::Value lbs;
Json::Value dis;
// 构建能源系统报告
//1.构建系统状态报告
suffix = getTypeName(typeid(workCondition).name());
pms[VAR_TO_STR(workCondition)+suffix] = workCondition;
suffix = getTypeName(typeid(currentMod).name());
pms[VAR_TO_STR(currentMod)+suffix] = currentMod;
suffix = getTypeName(typeid(powerState).name());
pms[VAR_TO_STR(powerState)+suffix] = powerState;
suffix = getTypeName(typeid(falutLevel).name());
pms[VAR_TO_STR(falutLevel)+suffix] = falutLevel;
Json::Value faultCode_array;
for(auto val : faultCode)
{
faultCode_array.append(val);
}
pms[VAR_TO_STR(faultCode_array)] = faultCode_array;
suffix = getTypeName(typeid(soc).name());
pms[VAR_TO_STR(soc)+suffix] = soc;
suffix = getTypeName(typeid(sustainableTime).name());
pms[VAR_TO_STR(sustainableTime)+suffix] = sustainableTime;
//2.构建燃料电池状态报告
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuState).name());
fcs[VAR_TO_STR(fcuState)+suffix] = m_pmCurrentState.ccustate.fcuState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuFaultLeve).name());
fcs[VAR_TO_STR(fcuFaultLeve)+suffix] = m_pmCurrentState.ccustate.fcuFaultLeve;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerLimit).name());
fcs[VAR_TO_STR(powLimit)+suffix] = m_pmCurrentState.ccustate.powerLimit;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.totalPowerGeneration).name());
fcs[VAR_TO_STR(powerGen)+suffix] = m_pmCurrentState.ccustate.totalPowerGeneration;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.ch4o).name());
fcs[VAR_TO_STR(ch4o)+suffix] = m_pmCurrentState.ccustate.ch4o;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.o2).name());
fcs[VAR_TO_STR(o2)+suffix] = m_pmCurrentState.ccustate.o2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuVoltage).name());
fcs[VAR_TO_STR(fcuVol)+suffix] = m_pmCurrentState.ccustate.fcuVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuCurrent).name());
fcs[VAR_TO_STR(fcuCur)+suffix] = m_pmCurrentState.ccustate.fcuCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.o2Level).name());
fcs[VAR_TO_STR(o2Level)+suffix] = m_pmCurrentState.ccustate.o2Level;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.exchangerTemperature).name());
fcs[VAR_TO_STR(excTemp)+suffix] = m_pmCurrentState.ccustate.exchangerTemperature;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuOutPower).name());
fcs[VAR_TO_STR(fcuOutPower)+suffix] = m_pmCurrentState.ccustate.fcuOutPower;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuOutIns).name());
fcs[VAR_TO_STR(fcuOutIns)+suffix] = m_pmCurrentState.ccustate.fcuOutIns;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuVoltageIns).name());
fcs[VAR_TO_STR(fcuVolIns)+suffix] = m_pmCurrentState.ccustate.fcuVoltageIns;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuVoltagePower).name());
fcs[VAR_TO_STR(fcuVolPower)+suffix] = m_pmCurrentState.ccustate.fcuVoltagePower;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuCurrentIns).name());
fcs[VAR_TO_STR(fcuCurIns)+suffix] = m_pmCurrentState.ccustate.fcuCurrentIns;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuCurrentPower).name());
fcs[VAR_TO_STR(fcuCurPower)+suffix] = m_pmCurrentState.ccustate.fcuCurrentPower;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.h2Concentration1).name());
fcs[VAR_TO_STR(h2Concent1)+suffix] = m_pmCurrentState.ccustate.h2Concentration1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.h2Concentration2).name());
fcs[VAR_TO_STR(h2Concent2)+suffix] = m_pmCurrentState.ccustate.h2Concentration2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.o2Concentration1).name());
fcs[VAR_TO_STR(o2Concent1)+suffix] = m_pmCurrentState.ccustate.o2Concentration1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.o2Concentration2).name());
fcs[VAR_TO_STR(o2Concent2)+suffix] = m_pmCurrentState.ccustate.o2Concentration2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.ch4oConcentration1).name());
fcs[VAR_TO_STR(ch4oConcent1)+suffix] = m_pmCurrentState.ccustate.ch4oConcentration1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.ch4oConcentration2).name());
fcs[VAR_TO_STR(ch4oConcent2)+suffix] = m_pmCurrentState.ccustate.ch4oConcentration2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.backpressure1).name());
fcs[VAR_TO_STR(backpressure1)+suffix] = m_pmCurrentState.ccustate.backpressure1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.backpressure2).name());
fcs[VAR_TO_STR(backpressure2)+suffix] = m_pmCurrentState.ccustate.backpressure2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.flameDetection).name());
fcs[VAR_TO_STR(flameDet)+suffix] = m_pmCurrentState.ccustate.flameDetection;
unsigned short faultCode1;
unsigned short faultCode2;
unsigned short faultCode3;
faultCode1 = (m_pmCurrentState.ccustate.fcuGZ_1H << 8) | m_pmCurrentState.ccustate.fcuGZ_1L;
faultCode2 = (m_pmCurrentState.ccustate.fcuGZ_2H << 8) | m_pmCurrentState.ccustate.fcuGZ_2L;
faultCode3 = (m_pmCurrentState.ccustate.fcuGZ_3H << 8) | m_pmCurrentState.ccustate.fcuGZ_3L;
suffix = getTypeName(typeid(faultCode1).name());
fcs[VAR_TO_STR(faultCode1)+suffix] = faultCode1;
suffix = getTypeName(typeid(faultCode2).name());
fcs[VAR_TO_STR(faultCode2)+suffix] = faultCode2;
suffix = getTypeName(typeid(faultCode3).name());
fcs[VAR_TO_STR(faultCode3)+suffix] = faultCode3;
//4.构建动力锂电池状态报告
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insPosRelayState).name());
lbs[VAR_TO_STR(insPosRelayState)+suffix] = m_pmCurrentState.ccustate.insPosRelayState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insNegRelayState).name());
lbs[VAR_TO_STR(insNegRelayState)+suffix] = m_pmCurrentState.ccustate.insNegRelayState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerPosRelayState).name());
lbs[VAR_TO_STR(powPosRelayState)+suffix] = m_pmCurrentState.ccustate.powerPosRelayState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerNegRelayState).name());
lbs[VAR_TO_STR(powNegRelayState)+suffix] = m_pmCurrentState.ccustate.powerNegRelayState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insBatLimit).name());
lbs[VAR_TO_STR(insBatLimit)+suffix] = m_pmCurrentState.ccustate.insBatLimit;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerBatLimit).name());
lbs[VAR_TO_STR(powerBatLimit)+suffix] = m_pmCurrentState.ccustate.powerBatLimit;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insBatSoc).name());
lbs[VAR_TO_STR(insBatSoc)+suffix] = m_pmCurrentState.ccustate.insBatSoc;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerBatSoc).name());
lbs[VAR_TO_STR(powBatSoc)+suffix] = m_pmCurrentState.ccustate.powerBatSoc;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insBatTotalElectricity).name());
lbs[VAR_TO_STR(insBatTotalElect)+suffix] = m_pmCurrentState.ccustate.insBatTotalElectricity;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerBatTotalElectricity).name());
lbs[VAR_TO_STR(powBatTotalElect)+suffix] = m_pmCurrentState.ccustate.powerBatTotalElectricity;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insBatCurrentElectricity).name());
lbs[VAR_TO_STR(insBatCurElect)+suffix] = m_pmCurrentState.ccustate.insBatCurrentElectricity;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerBatCurrentElectricity).name());
lbs[VAR_TO_STR(powBatCurElect)+suffix] = m_pmCurrentState.ccustate.powerBatCurrentElectricity;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insChargeState).name());
lbs[VAR_TO_STR(insChargeState)+suffix] = m_pmCurrentState.ccustate.insChargeState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerChargeState).name());
lbs[VAR_TO_STR(powChargeState)+suffix] = m_pmCurrentState.ccustate.powerChargeState;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insLinkVoltage).name());
lbs[VAR_TO_STR(insLinkVol)+suffix] = m_pmCurrentState.ccustate.insLinkVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insPackVoltage).name());
lbs[VAR_TO_STR(insPackVol)+suffix] = m_pmCurrentState.ccustate.insPackVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insCurrent).name());
lbs[VAR_TO_STR(insCurrent)+suffix] = m_pmCurrentState.ccustate.insCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insPosInsulationResistance).name());
lbs[VAR_TO_STR(insPosRes)+suffix] = m_pmCurrentState.ccustate.insPosInsulationResistance;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insNegInsulationResistance).name());
lbs[VAR_TO_STR(insNegRes)+suffix] = m_pmCurrentState.ccustate.insNegInsulationResistance;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerLinkVoltage).name());
lbs[VAR_TO_STR(powLinkVol)+suffix] = m_pmCurrentState.ccustate.powerLinkVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerPackVoltage).name());
lbs[VAR_TO_STR(powerPackVol)+suffix] = m_pmCurrentState.ccustate.powerPackVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerCurrent).name());
lbs[VAR_TO_STR(powCur)+suffix] = m_pmCurrentState.ccustate.powerCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerPosInsulationResistance).name());
lbs[VAR_TO_STR(powPosRes)+suffix] = m_pmCurrentState.ccustate.powerPosInsulationResistance;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerNegInsulationResistance).name());
lbs[VAR_TO_STR(powNegRes)+suffix] = m_pmCurrentState.ccustate.powerNegInsulationResistance;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insCurrentSocHold1).name());
lbs[VAR_TO_STR(insCurSocHold1)+suffix] = m_pmCurrentState.ccustate.insCurrentSocHold1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insCurrentSocHold2).name());
lbs[VAR_TO_STR(insCurSocHold2)+suffix] = m_pmCurrentState.ccustate.insCurrentSocHold2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insCurrentSocHold3).name());
lbs[VAR_TO_STR(insCurSocHold3)+suffix] = m_pmCurrentState.ccustate.insCurrentSocHold3;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuCurrentToInsLimit1).name());
lbs[VAR_TO_STR(fcuCurToInsLimit1)+suffix] = m_pmCurrentState.ccustate.fcuCurrentToInsLimit1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.fcuCurrentToinsLimit2).name());
lbs[VAR_TO_STR(fcuCurToinsLimit2)+suffix] = m_pmCurrentState.ccustate.fcuCurrentToinsLimit2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerCurrentSocHold1).name());
lbs[VAR_TO_STR(powCurSocHold1)+suffix] = m_pmCurrentState.ccustate.powerCurrentSocHold1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerCurrentSocHold2).name());
lbs[VAR_TO_STR(powCurSocHold2)+suffix] = m_pmCurrentState.ccustate.powerCurrentSocHold2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerCurrentSocHold3).name());
lbs[VAR_TO_STR(powCurSocHold3)+suffix] = m_pmCurrentState.ccustate.powerCurrentSocHold3;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerCurrentToInsLimit1).name());
lbs[VAR_TO_STR(powCurToInsLimit1)+suffix] = m_pmCurrentState.ccustate.powerCurrentToInsLimit1;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerCurrentToinsLimit2).name());
lbs[VAR_TO_STR(powCurToinsLimit2)+suffix] = m_pmCurrentState.ccustate.powerCurrentToinsLimit2;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.insAlarmIdentificationWord).name());
lbs[VAR_TO_STR(insfaultCode)+suffix] = m_pmCurrentState.ccustate.insAlarmIdentificationWord;
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.powerAlarmIdentificationWord).name());
lbs[VAR_TO_STR(powfaultCode)+suffix] = m_pmCurrentState.ccustate.powerAlarmIdentificationWord;
//5.构建配电系统状态报告
disBus1Breaker* pdis1=new disBus1Breaker;
disBus2Breaker* pdis2=new disBus2Breaker;
disBus3Breaker* pdis3=new disBus3Breaker;
disBus4Breaker* pdis4=new disBus4Breaker;
*pdis1 = m_currentDisBreakerState.bus1Breaker;
*pdis2 = m_currentDisBreakerState.bus2Breaker;
*pdis3 = m_currentDisBreakerState.bus3Breaker;
*pdis4 = m_currentDisBreakerState.bus4Breaker;
dis["HVolBusBreaker_uint16"] = *(unsigned short*)pdis1;
dis["HVolABusBreaker_unit16"] = *(unsigned short*)pdis2;
dis["HVolBBusBreaker_unit16"] = *(unsigned short*)pdis3;
dis["LVolBusBreaker_uint16"] = *(unsigned short*)pdis4;
//TODO: 构建配电系统故障码
Json::Value faultCodeDis_array;
for(auto val : faultCode)
{
faultCodeDis_array.append(val);
}
dis[VAR_TO_STR(faultCode_array)] = faultCodeDis_array;
Json::Value currentBusState;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.powerBusVoltage).name());
currentBusState[VAR_TO_STR(HVBpowerBusVol)+suffix] = m_pmCurrentState.bus1State.powerBusVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.dcDc5ModuleCurrent).name());
currentBusState[VAR_TO_STR(HVBdcDc5Cur)+suffix] = m_pmCurrentState.bus1State.dcDc5ModuleCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.powerBusCurrent).name());
currentBusState[VAR_TO_STR(HVBpowerBusCur)+suffix] = m_pmCurrentState.bus1State.powerBusCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.busbarAVoltage).name());
currentBusState[VAR_TO_STR(HVBbusAVol)+suffix] = m_pmCurrentState.bus1State.busbarAVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.propulsionMotorControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBmotorCur)+suffix] = m_pmCurrentState.bus1State.propulsionMotorControlBoxCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.busbarACurrent).name());
currentBusState[VAR_TO_STR(HVBbusACur)+suffix] = m_pmCurrentState.bus1State.busbarACurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.lithiumBatteryGroupMeterCurrent).name());
currentBusState[VAR_TO_STR(HVBbatGroupCur)+suffix] = m_pmCurrentState.bus1State.lithiumBatteryGroupMeterCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBbowHVolDisBoxCur)+suffix] = m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.sternFTDevice45Current).name());
currentBusState[VAR_TO_STR(HVBFTDev45Cur)+suffix] = m_pmCurrentState.bus1State.sternFTDevice45Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.tyzReservedSwitchCurrent).name());
currentBusState[VAR_TO_STR(HVBtyzCur)+suffix] = m_pmCurrentState.bus1State.tyzReservedSwitchCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.sternRudderSwitch1Current).name());
currentBusState[VAR_TO_STR(HVBsternRud1Cur)+suffix] = m_pmCurrentState.bus1State.sternRudderSwitch1Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.reservedCurrent1).name());
currentBusState[VAR_TO_STR(HVBreservedCur1)+suffix] = m_pmCurrentState.bus1State.reservedCurrent1;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.sternRudderSwitch2Current).name());
currentBusState[VAR_TO_STR(HVBsternRud2Cur)+suffix] = m_pmCurrentState.bus1State.sternRudderSwitch2Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.reservedCurrent2).name());
currentBusState[VAR_TO_STR(HVBreservedCur2)+suffix] = m_pmCurrentState.bus1State.reservedCurrent2;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.fbReservedSwitchCurrent).name());
currentBusState[VAR_TO_STR(HVBfbCur)+suffix] = m_pmCurrentState.bus1State.fbReservedSwitchCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus1State.reservedCurrent3).name());
currentBusState[VAR_TO_STR(HVBreservedCur3)+suffix] = m_pmCurrentState.bus1State.reservedCurrent3;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.busbarBVoltage).name());
currentBusState[VAR_TO_STR(HVABbusBVol)+suffix] = m_pmCurrentState.bus2State.busbarBVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.busbarBCurrent).name());
currentBusState[VAR_TO_STR(HVABbusBCur)+suffix] = m_pmCurrentState.bus2State.busbarBCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.bowFTDevice123Current).name());
currentBusState[VAR_TO_STR(HVABFTDev123Cur)+suffix] = m_pmCurrentState.bus2State.bowFTDevice123Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.actuatorCurrent).name());
currentBusState[VAR_TO_STR(HVABactCur)+suffix] = m_pmCurrentState.bus2State.actuatorCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.mastSteeringGearControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVABmastSteGeaCur)+suffix] = m_pmCurrentState.bus2State.mastSteeringGearControlBoxCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.xczCurrent).name());
currentBusState[VAR_TO_STR(HVABxczCur)+suffix] = m_pmCurrentState.bus2State.xczCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.bowRudderControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVABbowRudCur)+suffix] = m_pmCurrentState.bus2State.bowRudderControlBoxCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus2State.openWaterCoverStartCylinderCurrent).name());
currentBusState[VAR_TO_STR(HVABopenWaterCur)+suffix] = m_pmCurrentState.bus2State.openWaterCoverStartCylinderCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.instrumentBusbarVoltage).name());
currentBusState[VAR_TO_STR(HVBBinsBusVol)+suffix] = m_pmCurrentState.bus3State.instrumentBusbarVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBBbowLowVolDisBoxCur)+suffix] = m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.dcC1InstrumentDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBdcC1InsCur)+suffix] = m_pmCurrentState.bus3State.dcC1InstrumentDC48VCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.reservedCurrent1).name());
currentBusState[VAR_TO_STR(HVBBreservedCur1)+suffix] = m_pmCurrentState.bus3State.reservedCurrent1;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2Current).name());
currentBusState[VAR_TO_STR(HVBBemerBatCur)+suffix] = m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCurrent).name());
currentBusState[VAR_TO_STR(HVBBbatInsCur)+suffix] = m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.unit4InstrumentDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBunit4Cur)+suffix] = m_pmCurrentState.bus3State.unit4InstrumentDC48VCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.reservedCurrent2).name());
currentBusState[VAR_TO_STR(HVBBreservedCur2)+suffix] = m_pmCurrentState.bus3State.reservedCurrent2;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.emergency2BusbarVoltage).name());
currentBusState[VAR_TO_STR(HVBBemer2BusVol)+suffix] = m_pmCurrentState.bus3State.emergency2BusbarVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.compositeEnergyManagementSystemEmergencyDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBCCUCur)+suffix] = m_pmCurrentState.bus3State.compositeEnergyManagementSystemEmergencyDC48VCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.fuelCellSecuritySystemEmergencyDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBFCSSecurSysCur)+suffix] = m_pmCurrentState.bus3State.fuelCellSecuritySystemEmergencyDC48VCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus3State.plcControlPowerCurrent).name());
currentBusState[VAR_TO_STR(HVBBplcCur)+suffix] = m_pmCurrentState.bus3State.plcControlPowerCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.instrumentBusbarVoltage).name());
currentBusState[VAR_TO_STR(LVBinsBusVol)+suffix] = m_pmCurrentState.bus4State.instrumentBusbarVoltage;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.instrumentBusbarCurrent).name());
currentBusState[VAR_TO_STR(LVBBinsBusCur)+suffix] = m_pmCurrentState.bus4State.instrumentBusbarCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.unit1Current).name());
currentBusState[VAR_TO_STR(LVBBunit1Cur)+suffix] = m_pmCurrentState.bus4State.unit1Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.unit2Current).name());
currentBusState[VAR_TO_STR(LVBBunit2Cur)+suffix] = m_pmCurrentState.bus4State.unit2Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.unit3Current).name());
currentBusState[VAR_TO_STR(LVBBunit3Cur)+suffix] = m_pmCurrentState.bus4State.unit3Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.unit5Current).name());
currentBusState[VAR_TO_STR(LVBBunit5Cur)+suffix] = m_pmCurrentState.bus4State.unit5Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.bowPZDeviceCurrent).name());
currentBusState[VAR_TO_STR(LVBBbowPZDevCur)+suffix] = m_pmCurrentState.bus4State.bowPZDeviceCurrent;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1Current).name());
currentBusState[VAR_TO_STR(LVBBemerBatCur)+suffix] = m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1Current;
suffix = getTypeName(typeid(m_pmCurrentState.bus4State.emergency1BusbarVoltage).name());
currentBusState[VAR_TO_STR(LVBBemerBusVol)+suffix] = m_pmCurrentState.bus4State.emergency1BusbarVoltage;
// dis["currentBusState"] = currentBusState;
// pms[VAR_TO_STR(fcs_obj)] = fcs;
// pms[VAR_TO_STR(lbs_obj)] = lbs;
// pms[VAR_TO_STR(dis_obj)] = dis;
Notify(PM_TO_DB_STATE, pms.toStyledString());
Notify(PM_TO_DB_FCSSTATE, fcs.toStyledString());
Notify(PM_TO_DB_LIBATSTATE, lbs.toStyledString());
Notify(PM_TO_DB_DISSTATE, dis.toStyledString());
Notify(PM_TO_DB_CURRENTSTATE, currentBusState.toStyledString());
// 使用StreamWriterBuilder来设置保持插入顺序
Json::StreamWriterBuilder builder;
builder["indentation"] = " "; // 设置不缩进,保持紧凑格式
builder["enableYAMLCompatibility"] = true; // 保持插入顺序
// 将Json::Value对象转换为字符串
string pathFile = "/home/zjk/project/H100/H100PowerManger/data/msg/";
string jsonStr = Json::writeString(builder, pms);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_STATE+".json");
jsonStr = Json::writeString(builder, fcs);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_FCSSTATE+".json");
jsonStr = Json::writeString(builder, lbs);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_LIBATSTATE+".json");
jsonStr = Json::writeString(builder, dis);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_DISSTATE + ".json");
jsonStr = Json::writeString(builder, currentBusState);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_CURRENTSTATE + ".json");
return true;
}
}
+54 -95
View File
@@ -23,7 +23,7 @@
#include <queue>
#include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h"
#include "MOOS/libMOOS/Utils/MOOSThread.h"
#include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
// #include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Utils/ConsoleColours.h"
#include "ccuUdpMsg.h"
@@ -33,8 +33,9 @@
#include "PowerManagerFsm.hpp"
#include "SQLiteDB.h"
#include "driver.h"
#include "LowerCommManager.h"
#include "UpperCommManager.h"
#define VAR_TO_STR(var) #var
#define IPORT 5000
#define CCUPORT 5001
@@ -42,29 +43,12 @@
#define MAX_UDP_PKT_SIZE 65535
#define SKEW_TOLERANCE 5
//能源管理反馈的消息
#define PM_TO_DB_STATE "uPower_pmState_st"
#define PM_TO_DB_CURRENTSTATE "uPower_currentState_st"
#define PM_TO_DB_DISSTATE "uPower_disSysState_st"
#define PM_TO_DB_LIBATSTATE "uPower_liBatState_st"
#define PM_TO_DB_FCSSTATE "uPower_fcsState_st"
#define PM_TO_DB_SUBDISCMD "uPower_secdistState_cmd"
#define PM_TO_DB_EQUIPMENTSTATE "uPower_equState_fb"
//控制消息
//TODO:实现上位机交互控制功能
#define DB_TO_PM_MSCMD "uPower_sysState_cmd"
#define DB_TO_PM_EQUIPMENTCMD "uPower_equState_cmd"
#define DB_TO_PM_SUBDISSTATE "uDevice_secdistState_st"
class PowerManger : public AppCastingMOOSApp
{
public:
PowerManger();
~PowerManger();
public:
bool ListenLoopUDP();
bool FsmLoop();
protected: // Standard MOOSApp functions to overload
@@ -75,103 +59,78 @@ class PowerManger : public AppCastingMOOSApp
bool buildReport();
void registerVariables();
protected:
public:
void updateTime(){m_current_time = MOOSTime();}
bool IsSkewed(double dfTimeNow, double * pdfSkew = NULL);
string getTypeName(const string typeName);
bool inline saveStringToJsonFile(const string &strJson, const string &strFileName);
//数据库
SQLiteDB* m_db;
bool writeDataToDB();
//通信状态变量及操作函数
XPCUdpSocket* setUdpScoket(long lPort);
bool updatePmState();
bool updateConnectState();
int connectState;
double msg_CcuStateFbMsg_Update_time;
// double msg_CcuSetParmFbMsg_Update_time;
double msg_disHighVolBusFbMsg_Update_time;
double msg_disHighAVolBusFbMsg_Update_time;
double msg_disHighBVolBusFbMsg_Update_time;
double msg_disLowBusFbMsg_Update_time;
protected:
//===========================交互状态变量==================================
Driver m_driver; //设备驱动
/************************************************************/
/* |<------------FEEDBACK--------<-- */
/* | | */
/* IN-->+-------[STATE CONTROL]---------+--->OUT */
/* */
/************************************************************/
public:
//===========================IN============================================
DriverTable m_MSDriverCmd;
MSControlCmd m_msCmd;
//===========================OUT===========================================
DriverTable m_subDisSysCmd;
ccuColCmd m_CcuColCmd;
disSysBreakerList m_targetDisBreakerState;
//===========================FEEDBACK======================================
pmState m_pmCurrentState; //能源系统的当前状态
disSysBreakerList m_currentDisBreakerState; //配电断路器状态
disSysBreakerList m_targetDisBreakerState;
DriverTable m_currentDriverState;
DriverTable m_MSDriverCmd;
DriverTable m_subDisSysCmd;
DriverTable m_subDisSysState;
//===========================================================================
//系统运行状态
unsigned char workCondition;
unsigned char currentMod;
unsigned char powerState;
unsigned char falutLevel;
vector<unsigned int> faultCode;
unsigned int soc;
unsigned int sustainableTime;
// 配电和CCU的反馈状态
msg_CcuStateFbMsg m_CcuCurrentState;
msg_disHighVolBusFbMsg m_disHighVolBusCurrentState;
msg_disHighAVolBusFbMsg m_disHighAVolBusCurrentState;
msg_disHighBVolBusFbMsg m_disHighBVolBusCurrentState;
msg_disLowBusFbMsg m_disLowBusState;
//下位机操作函数
ccuColCmd m_CcuColCmd;
void disSysOption();
void ccuOption();
char getBreakerOption(char cmd);
void initControlCmd();
//上位机操作函数
MSControlCmd m_msCmd;
bool buildPowerSysReport();
bool getMsCmdFromJson(MSControlCmd &cmd,const string &strJson);
int m_depth;
int m_missionCode;
//===========================STATE=========================================
unsigned char workCondition;
unsigned char currentMod;
unsigned char powerState;
unsigned char falutLevel;
vector<unsigned int> faultCode;
unsigned int soc;
unsigned int sustainableTime;
unsigned int m_faultNum;
Driver m_driver; //设备表处理
std::string m_pmEvent; // 状态机事件描述
//===========================CONFIG========================================
long m_lPort;
unsigned int m_nReceiveBufferSizeKB;
unsigned int m_nSendBufferSizeKB;
double m_current_time;
unsigned int m_ticker;
int connectState;
//=============================过程函数======================================
void initControlCmd();
private:
udpComm m_udpComm;
XPCUdpSocket *m_pCcuUdpComm;
CMOOSThread m_ListenThread;
CMOOSThread m_FsmThread;
long m_lPort;
unsigned int m_nReceiveBufferSizeKB;
unsigned int m_nSendBufferSizeKB;
double m_current_time;
unsigned int m_faultNum;
unsigned int m_ticker;
// 状态机实例
PowerManagerFsm m_fsm;
// CMOOSThread m_FsmThread;
LowerCommManager* m_lowerCommManager;
UpperCommManager* m_upperCommManager;
// // 状态机实例
// PowerManagerFsm m_fsm;
public:
//===========================状态机接口=======================================
void printState(const std::string& stateName, const std::string& desp);
bool FSM_RUN_IS_OK = true;
// 初始化状态机
void initFsm() {
m_fsm.init(this);
}
// 状态机事件触发接口
void triggerInitEvent() { m_fsm.triggerInitEvent(); }
void triggerStandbyEvent() { m_fsm.triggerStandbyEvent(); }
void triggerFaultEvent(unsigned int faultCode) { m_fsm.triggerFaultEvent(faultCode); }
//==================================================================================
int checkError();
bool shutdown();
bool powerOn();
bool powerOff();
bool sendNotification(const std::string& key, const std::string& value) {
return Notify(key, value);
}
};
#endif
+586
View File
@@ -0,0 +1,586 @@
#include "UpperCommManager.h"
#include "PowerManger.h"
#include "driver.h"
UpperCommManager::UpperCommManager(PowerManger* pm) : m_pm(pm) {}
bool UpperCommManager::processUpperMsg(const string& key, const string& strJson) {
if(key == DB_TO_PM_MSCMD) {
m_pm->m_msCmd = getMasterControlCmd(key, strJson);
return true;
}
else if(key == DB_TO_PM_EQUIPMENTCMD) {
m_pm->m_MSDriverCmd = getEquipmentCmd(key, strJson);
return true;
}
else if(key == DB_TO_PM_SUBDISSTATE) {
m_pm->m_subDisSysState = getSubDisState(key, strJson);
return true;
}
else if(key == DB_TO_PM_DEEPTH) {
m_pm->m_depth = getDeepth(key, strJson);
return true;
}
std::cout << "Unknown key: " << key << std::endl;
return false;
}
const MSControlCmd UpperCommManager::getMasterControlCmd(const std::string& key, const std::string& msg)
{
MSControlCmd cmd;
if (parseControlCmdFromJson(cmd, msg)) {
return cmd;
}
else
{
cout << MOOS::ConsoleColours::Red << "parse control cmd error:" << msg
<< MOOS::ConsoleColours::reset << endl;
}
}
const DriverTable UpperCommManager::getEquipmentCmd(const std::string& key, const std::string& msg)
{
DriverTable cmd;
if (m_pm->m_driver.loadDriverTableFromJson(msg, cmd) < 0) {
cout << MOOS::ConsoleColours::Red << "get equipment cmd error:" << msg
<< MOOS::ConsoleColours::reset << endl;
}
return cmd;
}
const DriverTable UpperCommManager::getSubDisState(const std::string& key, const std::string& msg)
{
DriverTable state;
if (m_pm->m_driver.loadDriverTableFromJson(msg, state) < 0) {
cout << MOOS::ConsoleColours::Red << "get sub dis state error:" << msg
<< MOOS::ConsoleColours::reset << endl;
}
}
const int UpperCommManager::getDeepth(const std::string& key, const std::string& msg){
int deepth;
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
std::cerr << "JSON解析失败: " << errs << std::endl;
return -1;
}
// 检查并读取深度数组
if (root.isMember("value_array_uint16") && root["value_array_uint16"].isArray()) {
const Json::Value& array = root["value_array_uint16"];
if (array.size() >= 3) {
// 计算三个深度值的平均值
deepth = (array[0].asInt() + array[1].asInt() + array[2].asInt()) / 3;
} else {
std::cerr << "深度数组元素不足3个" << std::endl;
return -1;
}
} else {
std::cerr << "JSON格式错误" << std::endl;
return -1;
}
return deepth;
}
const std::string UpperCommManager::getCurrentEquState(DriverTable& driver)
{
return m_pm->m_driver.saveDriverTableToJson(&driver);
}
const std::string UpperCommManager::getSubDisCmd(DriverTable& driver)
{
return m_pm->m_driver.saveDriverTableToJson(&driver);
}
void UpperCommManager::processPeriodicTasks() {
buildSystemReport();
std::string strJson;
strJson = getCurrentEquState(m_pm->m_currentDriverState);
m_pm->sendNotification(PM_TO_DB_EQUIPMENTSTATE, strJson);
strJson = getSubDisCmd(m_pm->m_subDisSysCmd);
m_pm->sendNotification(PM_TO_DB_SUBDISCMD, strJson);
}
bool UpperCommManager::parseControlCmdFromJson(MSControlCmd &cmd, const std::string &strJson)
{
Json::CharReaderBuilder builder;
Json::Value root;
std::string errs;
// 解析JSON字符串
std::istringstream s(strJson);
if (!Json::parseFromStream(builder, s, &root, &errs))
{
std::cerr << "JSON解析失败: " << errs << std::endl;
return false; // 解析失败
}
if (root.isMember("powerCMD") && root["powerCMD"].isUInt())
{
cmd.powerCMD = root["powerCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: powerCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("modCMD") && root["modCMD"].isUInt())
{
cmd.modCMD = root["modCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: modCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("workCMD") && root["workCMD"].isUInt())
{
cmd.workCMD = root["workCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: workCMD" << std::endl;
return false; // 缺少或类型不匹配
}
return true; // 成功
}
void UpperCommManager::buildSystemReport() {
string suffix;
Json::Value pms;
Json::Value fcs;
Json::Value lbs;
Json::Value dis;
// 构建能源系统报告
//1.构建系统状态报告
suffix = getTypeName(typeid(m_pm->workCondition).name());
pms[VAR_TO_STR(workCondition)+suffix] = m_pm->workCondition;
suffix = getTypeName(typeid(m_pm->currentMod).name());
pms[VAR_TO_STR(currentMod)+suffix] = m_pm->currentMod;
suffix = getTypeName(typeid(m_pm->powerState).name());
pms[VAR_TO_STR(powerState)+suffix] = m_pm->powerState;
suffix = getTypeName(typeid(m_pm->falutLevel).name());
pms[VAR_TO_STR(falutLevel)+suffix] = m_pm->falutLevel;
Json::Value faultCode_array;
for(auto val : m_pm->faultCode)
{
faultCode_array.append(val);
}
pms[VAR_TO_STR(faultCode_array)] = faultCode_array;
suffix = getTypeName(typeid(m_pm->soc).name());
pms[VAR_TO_STR(soc)+suffix] = m_pm->soc;
suffix = getTypeName(typeid(m_pm->sustainableTime).name());
pms[VAR_TO_STR(sustainableTime)+suffix] = m_pm->sustainableTime;
//2.构建燃料电池状态报告
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuState).name());
fcs[VAR_TO_STR(fcuState)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuFaultLeve).name());
fcs[VAR_TO_STR(fcuFaultLeve)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuFaultLeve;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerLimit).name());
fcs[VAR_TO_STR(powLimit)+suffix] = m_pm->m_pmCurrentState.ccustate.powerLimit;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.totalPowerGeneration).name());
fcs[VAR_TO_STR(powerGen)+suffix] = m_pm->m_pmCurrentState.ccustate.totalPowerGeneration;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.ch4o).name());
fcs[VAR_TO_STR(ch4o)+suffix] = m_pm->m_pmCurrentState.ccustate.ch4o;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2).name());
fcs[VAR_TO_STR(o2)+suffix] = m_pm->m_pmCurrentState.ccustate.o2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuVoltage).name());
fcs[VAR_TO_STR(fcuVol)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrent).name());
fcs[VAR_TO_STR(fcuCur)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2Level).name());
fcs[VAR_TO_STR(o2Level)+suffix] = m_pm->m_pmCurrentState.ccustate.o2Level;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.exchangerTemperature).name());
fcs[VAR_TO_STR(excTemp)+suffix] = m_pm->m_pmCurrentState.ccustate.exchangerTemperature;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuOutPower).name());
fcs[VAR_TO_STR(fcuOutPower)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuOutPower;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuOutIns).name());
fcs[VAR_TO_STR(fcuOutIns)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuOutIns;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuVoltageIns).name());
fcs[VAR_TO_STR(fcuVolIns)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuVoltageIns;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuVoltagePower).name());
fcs[VAR_TO_STR(fcuVolPower)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuVoltagePower;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentIns).name());
fcs[VAR_TO_STR(fcuCurIns)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentIns;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentPower).name());
fcs[VAR_TO_STR(fcuCurPower)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentPower;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.h2Concentration1).name());
fcs[VAR_TO_STR(h2Concent1)+suffix] = m_pm->m_pmCurrentState.ccustate.h2Concentration1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.h2Concentration2).name());
fcs[VAR_TO_STR(h2Concent2)+suffix] = m_pm->m_pmCurrentState.ccustate.h2Concentration2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2Concentration1).name());
fcs[VAR_TO_STR(o2Concent1)+suffix] = m_pm->m_pmCurrentState.ccustate.o2Concentration1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2Concentration2).name());
fcs[VAR_TO_STR(o2Concent2)+suffix] = m_pm->m_pmCurrentState.ccustate.o2Concentration2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.ch4oConcentration1).name());
fcs[VAR_TO_STR(ch4oConcent1)+suffix] = m_pm->m_pmCurrentState.ccustate.ch4oConcentration1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.ch4oConcentration2).name());
fcs[VAR_TO_STR(ch4oConcent2)+suffix] = m_pm->m_pmCurrentState.ccustate.ch4oConcentration2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.backpressure1).name());
fcs[VAR_TO_STR(backpressure1)+suffix] = m_pm->m_pmCurrentState.ccustate.backpressure1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.backpressure2).name());
fcs[VAR_TO_STR(backpressure2)+suffix] = m_pm->m_pmCurrentState.ccustate.backpressure2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.flameDetection).name());
fcs[VAR_TO_STR(flameDet)+suffix] = m_pm->m_pmCurrentState.ccustate.flameDetection;
unsigned short faultCode1;
unsigned short faultCode2;
unsigned short faultCode3;
faultCode1 = (m_pm->m_pmCurrentState.ccustate.fcuGZ_1H << 8) | m_pm->m_pmCurrentState.ccustate.fcuGZ_1L;
faultCode2 = (m_pm->m_pmCurrentState.ccustate.fcuGZ_2H << 8) | m_pm->m_pmCurrentState.ccustate.fcuGZ_2L;
faultCode3 = (m_pm->m_pmCurrentState.ccustate.fcuGZ_3H << 8) | m_pm->m_pmCurrentState.ccustate.fcuGZ_3L;
suffix = getTypeName(typeid(faultCode1).name());
fcs[VAR_TO_STR(faultCode1)+suffix] = faultCode1;
suffix = getTypeName(typeid(faultCode2).name());
fcs[VAR_TO_STR(faultCode2)+suffix] = faultCode2;
suffix = getTypeName(typeid(faultCode3).name());
fcs[VAR_TO_STR(faultCode3)+suffix] = faultCode3;
//4.构建动力锂电池状态报告
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insPosRelayState).name());
lbs[VAR_TO_STR(insPosRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.insPosRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insNegRelayState).name());
lbs[VAR_TO_STR(insNegRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.insNegRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerPosRelayState).name());
lbs[VAR_TO_STR(powPosRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.powerPosRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerNegRelayState).name());
lbs[VAR_TO_STR(powNegRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.powerNegRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatLimit).name());
lbs[VAR_TO_STR(insBatLimit)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatLimit;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatLimit).name());
lbs[VAR_TO_STR(powerBatLimit)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatLimit;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatSoc).name());
lbs[VAR_TO_STR(insBatSoc)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatSoc;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatSoc).name());
lbs[VAR_TO_STR(powBatSoc)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatSoc;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatTotalElectricity).name());
lbs[VAR_TO_STR(insBatTotalElect)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatTotalElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatTotalElectricity).name());
lbs[VAR_TO_STR(powBatTotalElect)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatTotalElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatCurrentElectricity).name());
lbs[VAR_TO_STR(insBatCurElect)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatCurrentElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatCurrentElectricity).name());
lbs[VAR_TO_STR(powBatCurElect)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatCurrentElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insChargeState).name());
lbs[VAR_TO_STR(insChargeState)+suffix] = m_pm->m_pmCurrentState.ccustate.insChargeState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerChargeState).name());
lbs[VAR_TO_STR(powChargeState)+suffix] = m_pm->m_pmCurrentState.ccustate.powerChargeState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insLinkVoltage).name());
lbs[VAR_TO_STR(insLinkVol)+suffix] = m_pm->m_pmCurrentState.ccustate.insLinkVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insPackVoltage).name());
lbs[VAR_TO_STR(insPackVol)+suffix] = m_pm->m_pmCurrentState.ccustate.insPackVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrent).name());
lbs[VAR_TO_STR(insCurrent)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insPosInsulationResistance).name());
lbs[VAR_TO_STR(insPosRes)+suffix] = m_pm->m_pmCurrentState.ccustate.insPosInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insNegInsulationResistance).name());
lbs[VAR_TO_STR(insNegRes)+suffix] = m_pm->m_pmCurrentState.ccustate.insNegInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerLinkVoltage).name());
lbs[VAR_TO_STR(powLinkVol)+suffix] = m_pm->m_pmCurrentState.ccustate.powerLinkVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerPackVoltage).name());
lbs[VAR_TO_STR(powerPackVol)+suffix] = m_pm->m_pmCurrentState.ccustate.powerPackVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrent).name());
lbs[VAR_TO_STR(powCur)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerPosInsulationResistance).name());
lbs[VAR_TO_STR(powPosRes)+suffix] = m_pm->m_pmCurrentState.ccustate.powerPosInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerNegInsulationResistance).name());
lbs[VAR_TO_STR(powNegRes)+suffix] = m_pm->m_pmCurrentState.ccustate.powerNegInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrentSocHold1).name());
lbs[VAR_TO_STR(insCurSocHold1)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrentSocHold1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrentSocHold2).name());
lbs[VAR_TO_STR(insCurSocHold2)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrentSocHold2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrentSocHold3).name());
lbs[VAR_TO_STR(insCurSocHold3)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrentSocHold3;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentToInsLimit1).name());
lbs[VAR_TO_STR(fcuCurToInsLimit1)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentToInsLimit1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentToinsLimit2).name());
lbs[VAR_TO_STR(fcuCurToinsLimit2)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentToinsLimit2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold1).name());
lbs[VAR_TO_STR(powCurSocHold1)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold2).name());
lbs[VAR_TO_STR(powCurSocHold2)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold3).name());
lbs[VAR_TO_STR(powCurSocHold3)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold3;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentToInsLimit1).name());
lbs[VAR_TO_STR(powCurToInsLimit1)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentToInsLimit1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentToinsLimit2).name());
lbs[VAR_TO_STR(powCurToinsLimit2)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentToinsLimit2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insAlarmIdentificationWord).name());
lbs[VAR_TO_STR(insfaultCode)+suffix] = m_pm->m_pmCurrentState.ccustate.insAlarmIdentificationWord;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerAlarmIdentificationWord).name());
lbs[VAR_TO_STR(powfaultCode)+suffix] = m_pm->m_pmCurrentState.ccustate.powerAlarmIdentificationWord;
//5.构建配电系统状态报告
disBus1Breaker* pdis1=new disBus1Breaker;
disBus2Breaker* pdis2=new disBus2Breaker;
disBus3Breaker* pdis3=new disBus3Breaker;
disBus4Breaker* pdis4=new disBus4Breaker;
*pdis1 = m_pm->m_currentDisBreakerState.bus1Breaker;
*pdis2 = m_pm->m_currentDisBreakerState.bus2Breaker;
*pdis3 = m_pm->m_currentDisBreakerState.bus3Breaker;
*pdis4 = m_pm->m_currentDisBreakerState.bus4Breaker;
dis["HVolBusBreaker_uint16"] = *(unsigned short*)pdis1;
dis["HVolABusBreaker_unit16"] = *(unsigned short*)pdis2;
dis["HVolBBusBreaker_unit16"] = *(unsigned short*)pdis3;
dis["LVolBusBreaker_uint16"] = *(unsigned short*)pdis4;
//TODO: 构建配电系统故障码
Json::Value faultCodeDis_array;
for(auto val : m_pm->faultCode)
{
faultCodeDis_array.append(val);
}
dis[VAR_TO_STR(faultCode_array)] = faultCodeDis_array;
Json::Value currentBusState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.powerBusVoltage).name());
currentBusState[VAR_TO_STR(HVBpowerBusVol)+suffix] = m_pm->m_pmCurrentState.bus1State.powerBusVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.dcDc5ModuleCurrent).name());
currentBusState[VAR_TO_STR(HVBdcDc5Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.dcDc5ModuleCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.powerBusCurrent).name());
currentBusState[VAR_TO_STR(HVBpowerBusCur)+suffix] = m_pm->m_pmCurrentState.bus1State.powerBusCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.busbarAVoltage).name());
currentBusState[VAR_TO_STR(HVBbusAVol)+suffix] = m_pm->m_pmCurrentState.bus1State.busbarAVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.propulsionMotorControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBmotorCur)+suffix] = m_pm->m_pmCurrentState.bus1State.propulsionMotorControlBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.busbarACurrent).name());
currentBusState[VAR_TO_STR(HVBbusACur)+suffix] = m_pm->m_pmCurrentState.bus1State.busbarACurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.lithiumBatteryGroupMeterCurrent).name());
currentBusState[VAR_TO_STR(HVBbatGroupCur)+suffix] = m_pm->m_pmCurrentState.bus1State.lithiumBatteryGroupMeterCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBbowHVolDisBoxCur)+suffix] = m_pm->m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.sternFTDevice45Current).name());
currentBusState[VAR_TO_STR(HVBFTDev45Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.sternFTDevice45Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.tyzReservedSwitchCurrent).name());
currentBusState[VAR_TO_STR(HVBtyzCur)+suffix] = m_pm->m_pmCurrentState.bus1State.tyzReservedSwitchCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch1Current).name());
currentBusState[VAR_TO_STR(HVBsternRud1Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.sternRudderSwitch1Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.reservedCurrent1).name());
currentBusState[VAR_TO_STR(HVBreservedCur1)+suffix] = m_pm->m_pmCurrentState.bus1State.reservedCurrent1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch2Current).name());
currentBusState[VAR_TO_STR(HVBsternRud2Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.sternRudderSwitch2Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.reservedCurrent2).name());
currentBusState[VAR_TO_STR(HVBreservedCur2)+suffix] = m_pm->m_pmCurrentState.bus1State.reservedCurrent2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.fbReservedSwitchCurrent).name());
currentBusState[VAR_TO_STR(HVBfbCur)+suffix] = m_pm->m_pmCurrentState.bus1State.fbReservedSwitchCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.reservedCurrent3).name());
currentBusState[VAR_TO_STR(HVBreservedCur3)+suffix] = m_pm->m_pmCurrentState.bus1State.reservedCurrent3;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.busbarBVoltage).name());
currentBusState[VAR_TO_STR(HVABbusBVol)+suffix] = m_pm->m_pmCurrentState.bus2State.busbarBVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.busbarBCurrent).name());
currentBusState[VAR_TO_STR(HVABbusBCur)+suffix] = m_pm->m_pmCurrentState.bus2State.busbarBCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.bowFTDevice123Current).name());
currentBusState[VAR_TO_STR(HVABFTDev123Cur)+suffix] = m_pm->m_pmCurrentState.bus2State.bowFTDevice123Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.actuatorCurrent).name());
currentBusState[VAR_TO_STR(HVABactCur)+suffix] = m_pm->m_pmCurrentState.bus2State.actuatorCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.mastSteeringGearControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVABmastSteGeaCur)+suffix] = m_pm->m_pmCurrentState.bus2State.mastSteeringGearControlBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.xczCurrent).name());
currentBusState[VAR_TO_STR(HVABxczCur)+suffix] = m_pm->m_pmCurrentState.bus2State.xczCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.bowRudderControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVABbowRudCur)+suffix] = m_pm->m_pmCurrentState.bus2State.bowRudderControlBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.openWaterCoverStartCylinderCurrent).name());
currentBusState[VAR_TO_STR(HVABopenWaterCur)+suffix] = m_pm->m_pmCurrentState.bus2State.openWaterCoverStartCylinderCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.instrumentBusbarVoltage).name());
currentBusState[VAR_TO_STR(HVBBinsBusVol)+suffix] = m_pm->m_pmCurrentState.bus3State.instrumentBusbarVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBBbowLowVolDisBoxCur)+suffix] = m_pm->m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.dcC1InstrumentDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBdcC1InsCur)+suffix] = m_pm->m_pmCurrentState.bus3State.dcC1InstrumentDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.reservedCurrent1).name());
currentBusState[VAR_TO_STR(HVBBreservedCur1)+suffix] = m_pm->m_pmCurrentState.bus3State.reservedCurrent1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2Current).name());
currentBusState[VAR_TO_STR(HVBBemerBatCur)+suffix] = m_pm->m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCurrent).name());
currentBusState[VAR_TO_STR(HVBBbatInsCur)+suffix] = m_pm->m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.unit4InstrumentDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBunit4Cur)+suffix] = m_pm->m_pmCurrentState.bus3State.unit4InstrumentDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.reservedCurrent2).name());
currentBusState[VAR_TO_STR(HVBBreservedCur2)+suffix] = m_pm->m_pmCurrentState.bus3State.reservedCurrent2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.emergency2BusbarVoltage).name());
currentBusState[VAR_TO_STR(HVBBemer2BusVol)+suffix] = m_pm->m_pmCurrentState.bus3State.emergency2BusbarVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.compositeEnergyManagementSystemEmergencyDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBCCUCur)+suffix] = m_pm->m_pmCurrentState.bus3State.compositeEnergyManagementSystemEmergencyDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.fuelCellSecuritySystemEmergencyDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBFCSSecurSysCur)+suffix] = m_pm->m_pmCurrentState.bus3State.fuelCellSecuritySystemEmergencyDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.plcControlPowerCurrent).name());
currentBusState[VAR_TO_STR(HVBBplcCur)+suffix] = m_pm->m_pmCurrentState.bus3State.plcControlPowerCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.instrumentBusbarVoltage).name());
currentBusState[VAR_TO_STR(LVBinsBusVol)+suffix] = m_pm->m_pmCurrentState.bus4State.instrumentBusbarVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.instrumentBusbarCurrent).name());
currentBusState[VAR_TO_STR(LVBBinsBusCur)+suffix] = m_pm->m_pmCurrentState.bus4State.instrumentBusbarCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit1Current).name());
currentBusState[VAR_TO_STR(LVBBunit1Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit1Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit2Current).name());
currentBusState[VAR_TO_STR(LVBBunit2Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit2Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit3Current).name());
currentBusState[VAR_TO_STR(LVBBunit3Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit3Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit5Current).name());
currentBusState[VAR_TO_STR(LVBBunit5Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit5Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.bowPZDeviceCurrent).name());
currentBusState[VAR_TO_STR(LVBBbowPZDevCur)+suffix] = m_pm->m_pmCurrentState.bus4State.bowPZDeviceCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1Current).name());
currentBusState[VAR_TO_STR(LVBBemerBatCur)+suffix] = m_pm->m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.emergency1BusbarVoltage).name());
currentBusState[VAR_TO_STR(LVBBemerBusVol)+suffix] = m_pm->m_pmCurrentState.bus4State.emergency1BusbarVoltage;
// dis["currentBusState"] = currentBusState;
// pms[VAR_TO_STR(fcs_obj)] = fcs;
// pms[VAR_TO_STR(lbs_obj)] = lbs;
// pms[VAR_TO_STR(dis_obj)] = dis;
m_pm->sendNotification(PM_TO_DB_STATE, pms.toStyledString());
m_pm->sendNotification(PM_TO_DB_FCSSTATE, fcs.toStyledString());
m_pm->sendNotification(PM_TO_DB_LIBATSTATE, lbs.toStyledString());
m_pm->sendNotification(PM_TO_DB_DISSTATE, dis.toStyledString());
m_pm->sendNotification(PM_TO_DB_CURRENTSTATE, currentBusState.toStyledString());
// 使用StreamWriterBuilder来设置保持插入顺序
Json::StreamWriterBuilder builder;
builder["indentation"] = " "; // 设置不缩进,保持紧凑格式
builder["enableYAMLCompatibility"] = true; // 保持插入顺序
// 将Json::Value对象转换为字符串
string pathFile = "/home/zjk/project/H100/H100PowerManger/data/msg/";
string jsonStr = Json::writeString(builder, pms);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_STATE+".json");
jsonStr = Json::writeString(builder, fcs);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_FCSSTATE+".json");
jsonStr = Json::writeString(builder, lbs);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_LIBATSTATE+".json");
jsonStr = Json::writeString(builder, dis);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_DISSTATE + ".json");
jsonStr = Json::writeString(builder, currentBusState);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_CURRENTSTATE + ".json");
// return true;
}
bool UpperCommManager::saveStringToJsonFile(const string &strJson, const string &filePath)
{
std::ofstream file(filePath, std::ios::trunc);
if (file.is_open()) {
file << strJson;
file.close();
// std::cout << "字符串已成功保存到文件中。" << std::endl;
return true;
} else {
std::cerr << "无法打开文件!" << std::endl;
return false;
}
}
string UpperCommManager::getTypeName(const string typeName)
{
// 映射类型名称到指定的字符串
if (typeName == "i" || typeName == "c") {
return "_int8";
} else if (typeName == "h" || typeName == "C") {
return "_uint8";
} else if (typeName == "s" || typeName == "S") {
return "_int16";
} else if (typeName == "t" || typeName == "T") {
return "_uint16";
} else if (typeName == "i" || typeName == "I") {
return "_int32";
} else if (typeName == "j" || typeName == "J") {
return "_uint32";
} else if (typeName == "x" || typeName == "X") {
return "_int64";
} else if (typeName == "f" || typeName == "F") {
return "_fp32";
} else if (typeName == "d" || typeName == "D") {
return "_fp64";
} else if (typeName == "b" || typeName == "B") {
return "_bool";
} else if (typeName == "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
return "_str";
} else if (typeName == "St6vector" || typeName == "St5deque" || typeName == "St4list" || typeName == "St4pair") {
return "_array";
} else {
// 默认情况
return "_obj";
}
}
+70
View File
@@ -0,0 +1,70 @@
#ifndef UPPER_COMM_MANAGER_H
#define UPPER_COMM_MANAGER_H
#include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h"
#include "pmSysvariable.h"
#include "driver.h"
#include "json/json.h"
#include <string>
#include <vector>
#include <map>
using namespace std;
#define VAR_TO_STR(var) #var
//能源管理反馈的消息
#define PM_TO_DB_STATE "uPower_pmState_st"
#define PM_TO_DB_CURRENTSTATE "uPower_currentState_st"
#define PM_TO_DB_DISSTATE "uPower_disSysState_st"
#define PM_TO_DB_LIBATSTATE "uPower_liBatState_st"
#define PM_TO_DB_FCSSTATE "uPower_fcsState_st"
#define PM_TO_DB_SUBDISCMD "uPower_secdistState_cmd"
#define PM_TO_DB_EQUIPMENTSTATE "uPower_equState_fb"
//控制消息
#define DB_TO_PM_MSCMD "uPower_sysState_cmd"
#define DB_TO_PM_EQUIPMENTCMD "uPower_equState_cmd"
#define DB_TO_PM_SUBDISSTATE "uDevice_secdistState_st"
//决策消息
#define DB_TO_PM_DEEPTH "uDevice_depth_st"
#define DB_TO_PM_MISSION "uPlan_taskStart_cmd"
class PowerManger; // 前向声明
class UpperCommManager {
public:
UpperCommManager(PowerManger* powerManager);
~UpperCommManager();
// 周期调用
void processPeriodicTasks();
// 处理上位机消息
bool processUpperMsg(const std::string& key, const std::string& msg);
// 生成系统报告
void buildSystemReport();
// 消息处理函数
const MSControlCmd getMasterControlCmd(const std::string& key, const std::string& msg);
const DriverTable getEquipmentCmd(const std::string& key, const std::string& msg);
const DriverTable getSubDisState(const std::string& key, const std::string& msg);
const int getDeepth(const std::string& key, const std::string& msg);
const std::string getCurrentEquState(DriverTable& driver);
const std::string getSubDisCmd(DriverTable& driver);
// 其他公共函数
string getTypeName(const string typeName);
bool saveStringToJsonFile(const string &strJson, const string &filePath);
private:
PowerManger* m_pm;
MSControlCmd m_msCmd;
Driver m_driver;
// 从JSON解析控制命令
bool parseControlCmdFromJson(MSControlCmd &cmd, const std::string &strJson);
};
#endif // UPPER_COMM_MANAGER_H
+62 -62
View File
@@ -59,72 +59,72 @@ int Driver::getDriverTable(workCondition condition, DriverTable* driverTable)
void Driver::getSTANDBYTable(DriverTable* driverTable)
{
//打开所有传感器
driverTable->depth1 = DRIVER_FULL_POWER_ON;
driverTable->depth2 = DRIVER_FULL_POWER_ON;
driverTable->depth3 = DRIVER_FULL_POWER_ON;
driverTable->overDeep1 = DRIVER_FULL_POWER_ON;
driverTable->overDeep2 = DRIVER_FULL_POWER_ON;
driverTable->ctd1 = DRIVER_FULL_POWER_ON;
driverTable->ctd2 = DRIVER_FULL_POWER_ON;
driverTable->soundVeloc = DRIVER_FULL_POWER_ON;
driverTable->commMast = DRIVER_FULL_POWER_ON;
driverTable->ins = DRIVER_FULL_POWER_ON;
driverTable->ntp = DRIVER_FULL_POWER_ON;
driverTable->inputWaterSensor1 = DRIVER_FULL_POWER_ON;
driverTable->inputWaterSensor2 = DRIVER_FULL_POWER_ON;
driverTable->inputWaterSensor3 = DRIVER_FULL_POWER_ON;
driverTable->depth1 = 1;
driverTable->depth2 = 1;
driverTable->depth3 = 1;
driverTable->overDeep1 = 1;
driverTable->overDeep2 = 1;
driverTable->ctd1 = 1;
driverTable->ctd2 = 1;
driverTable->soundVeloc = 1;
driverTable->commMast = 1;
driverTable->ins = 1;
driverTable->ntp = 1;
driverTable->inputWaterSensor1 = 1;
driverTable->inputWaterSensor2 = 1;
driverTable->inputWaterSensor3 = 1;
//关闭所有负载
driverTable->photoSensor = DRIVER_STANDBY;
driverTable->electSensor = DRIVER_STANDBY;
driverTable->launchControler = DRIVER_STANDBY;
driverTable->sideSonar = DRIVER_POWER_OFF;
driverTable->buoyage = DRIVER_POWER_OFF;
driverTable->photoSensor = 0;
driverTable->electSensor = 0;
driverTable->launchControler = 0;
driverTable->sideSonar = 0;
driverTable->buoyage = 0;
//关闭所有水声设备
driverTable->singleBeamSonar1 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar2 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar3 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar4 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar5 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar6 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar7 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar8 = DRIVER_POWER_OFF;
driverTable->DVL = DRIVER_POWER_OFF;
driverTable->forwardSonar = DRIVER_POWER_OFF;
driverTable->lowFreqSonar = DRIVER_POWER_OFF;
driverTable->USBL = DRIVER_POWER_OFF;
driverTable->singleBeamSonar1 = 0;
driverTable->singleBeamSonar2 = 0;
driverTable->singleBeamSonar3 = 0;
driverTable->singleBeamSonar4 = 0;
driverTable->singleBeamSonar5 = 0;
driverTable->singleBeamSonar6 = 0;
driverTable->singleBeamSonar7 = 0;
driverTable->singleBeamSonar8 = 0;
driverTable->DVL = 0;
driverTable->forwardSonar = 0;
driverTable->lowFreqSonar = 0;
driverTable->USBL = 0;
//关闭所有执行机构
driverTable->bowCover1 = DRIVER_POWER_OFF;
driverTable->bowCover2 = DRIVER_POWER_OFF;
driverTable->bowCover3 = DRIVER_POWER_OFF;
driverTable->bowCover4 = DRIVER_POWER_OFF;
driverTable->bowCover5 = DRIVER_POWER_OFF;
driverTable->bowCover6 = DRIVER_POWER_OFF;
driverTable->bowRudderDeploy = DRIVER_POWER_OFF;
driverTable->bowRudderServo1 = DRIVER_POWER_OFF;
driverTable->bowRudderServo2 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet1 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet2 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet3 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet4 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet5 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet6 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet7 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet8 = DRIVER_POWER_OFF;
driverTable->waterInject = DRIVER_POWER_OFF;
driverTable->mastLiftingServo = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust1 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust2 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust3 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust4 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust5 = DRIVER_POWER_OFF;
driverTable->sternRudderServo1 = DRIVER_POWER_OFF;
driverTable->sternRudderServo2 = DRIVER_POWER_OFF;
driverTable->sternRudderServo3 = DRIVER_POWER_OFF;
driverTable->sternRudderServo4 = DRIVER_POWER_OFF;
driverTable->thruster = DRIVER_POWER_OFF;
driverTable->bowThrow = DRIVER_POWER_OFF;
driverTable->sternThrow = DRIVER_POWER_OFF;
driverTable->bowCover1 = 0;
driverTable->bowCover2 = 0;
driverTable->bowCover3 = 0;
driverTable->bowCover4 = 0;
driverTable->bowCover5 = 0;
driverTable->bowCover6 = 0;
driverTable->bowRudderDeploy = 0;
driverTable->bowRudderServo1 = 0;
driverTable->bowRudderServo2 = 0;
driverTable->bowWaterlnet1 = 0;
driverTable->bowWaterlnet2 = 0;
driverTable->bowWaterlnet3 = 0;
driverTable->bowWaterlnet4 = 0;
driverTable->bowWaterlnet5 = 0;
driverTable->bowWaterlnet6 = 0;
driverTable->bowWaterlnet7 = 0;
driverTable->bowWaterlnet8 = 0;
driverTable->waterInject = 0;
driverTable->mastLiftingServo = 0;
driverTable->buoyancyAdjust1 = 0;
driverTable->buoyancyAdjust2 = 0;
driverTable->buoyancyAdjust3 = 0;
driverTable->buoyancyAdjust4 = 0;
driverTable->buoyancyAdjust5 = 0;
driverTable->sternRudderServo1 = 0;
driverTable->sternRudderServo2 = 0;
driverTable->sternRudderServo3 = 0;
driverTable->sternRudderServo4 = 0;
driverTable->thruster = 0;
driverTable->bowThrow = 0;
driverTable->sternThrow = 0;
}
void Driver::getSHORE_BASED_READYable(DriverTable* driverTable)
-103
View File
@@ -1,103 +0,0 @@
#include "fsm.h"
#include <iostream>
FSM::FSM(/* args */)
{
idStateMap.clear();
handleMap.clear();
stateMap.clear();
};
FSM::~FSM()
{
};
int FSM::run(FSM_State &fsm,void* data)
{
FSM_Handle handle = handleMap[fsm.id];
return handle(fsm,data);
}
int FSM::RegisterHandleFunction(FSM_State* fsm, FSM_Handle handle)
{
if(fsm == NULL)
return -1;
if(handleMap.count(fsm->id) > 0)
return -1;
handleMap[fsm->id] = handle;
return 0;
}
int FSM::unRegisterHandleFunction(FSM_State* fsm)
{
if(fsm == NULL)
return -1;
if(handleMap.count(fsm->id) <= 0)
return -1;
handleMap.erase(fsm->id);
return 0;
}
int FSM::RegisterState(std::string name, unsigned int id,FSM_State &state)
{
if(stateMap.count(name) > 0)
return -1;
if(idStateMap.count(id) > 0)
return -1;
state.name = name;
state.id = id;
stateMap[name] = state;
idStateMap[id] = state;
return 0;
}
unsigned int FSM::getStateId(std::string name)
{
return stateMap[name].id;
}
FSM_State FSM::getState(std::string name)
{
return stateMap[name];
}
int FSM::RegisterEvent(FSM_State* fsm, std::string event, FSM_State *state)
{
if(fsm == NULL)
return -1;
fsm->event[event] = state;
return 0;
}
int FSM::unRegisterEvent(FSM_State* fsm, std::string event)
{
if(fsm == NULL)
return -1;
if(fsm->event.count(event) <= 0)
return -1;
fsm->event.erase(event);
return 0;
}
int FSM::run(FSM_State *fsm,void* data, std::string event)
{
if(fsm->event.count(event) <= 0)
{
std::cout << "no event" << std::endl;
return run(*(fsm),data);
}
else
{
int r = run(*(fsm),data);
// fsm = fsm->event[event];
*fsm = *fsm->event[event];
// std::cout << "event " << event <<std::endl;
// std::cout << "fsm" << fsm->name << std::endl;
return r;
}
}
// int FSM::run(FSM_State *fsm,void* data)
// {
// return run(*fsm,data);
// }
-46
View File
@@ -1,46 +0,0 @@
#ifndef FSM_H
#define FSM_H
#include <string>
#include <map>
typedef struct FSM_State
{
std::string name;
unsigned int id;
std::map<std::string,FSM_State*> event;
}FSM_State;
typedef int (*FSM_Handle)(FSM_State &fsm, void *data);
// typedef int (*FSM_EventHandle)(FSM_State &fsm,std::string event,void *data);
class FSM
{
public:
FSM();
~FSM();
std::map<unsigned int, FSM_Handle> handleMap;
std::map<std::string, FSM_State> stateMap;
std::map<unsigned int, FSM_State> idStateMap;
int run(FSM_State &fsm,void* data);
int run(FSM_State *fsm,void* data, std::string event);
// 事件处理机制
int RegisterEvent(FSM_State* fsm, const std::string& event, FSM_State *state);
int unRegisterEvent(FSM_State* fsm, const std::string& event);
// 状态管理
int RegisterState(const std::string& name, unsigned int id, FSM_State &state);
FSM_State getState(const std::string& name) const;
unsigned int getStateId(const std::string& name) const;
// 处理函数管理
int RegisterHandleFunction(FSM_State* fsm, FSM_Handle handle);
int unRegisterHandleFunction(FSM_State* fsm);
// 事件触发
int triggerEvent(FSM_State* currentState, const std::string& event, void* data);
};
#endif
+49
View File
@@ -360,5 +360,54 @@ typedef struct pmState
unsigned int workCMD;
}MSControlCmd;
namespace FcuCommand {
enum Type : unsigned char {
SelfCheck = 0x01,
Start = 0x02,
Stop = 0x03,
Reset = 0x04,
EmergencyStop = 0x05,
Supply = 0x06,
WarmUp = 0x07,
Maintenance = 0x08
};
}
enum class FcuState : unsigned char {
//初始化
Init = 0x01,
//待机
Standby = 0x02,
//自检异常
SelfCheckError = 0x03,
//就绪
Ready = 0x04,
//启动
Start = 0x05,
//运行
Running = 0x06,
//停机
Stop = 0x07,
//故障
Fault = 0x08,
//补给
Supply = 0x09,
//预热
WarmUp = 0x0A,
//预热完成
WarmUpComplete = 0x0B,
//维护
Maintenance = 0x0C,
//维护完成
MaintenanceComplete = 0x0D
};
namespace BatteryCommand {
enum Type : unsigned char {
Start = 0x10,
Stop = 0x20
};
}
#endif
+15 -2
View File
@@ -4,10 +4,17 @@
udpComm::udpComm()
{
udpScoket = nullptr;
ccuHost = "";
ccuPort = 0;
}
udpComm::~udpComm() {}
udpComm::~udpComm() {
if (udpScoket) {
delete udpScoket;
udpScoket = nullptr;
}
}
Checksum udpComm::calculateChecksum(const unsigned char* data, int size)
@@ -490,6 +497,12 @@ bool udpComm::sendCcuColCmd(const ccuColCmd cmd)
msg->checkCode = static_cast<cChecksum>(cc & 0xFF);
try
{
if(udpScoket == nullptr) {
throw std::runtime_error("UDP socket not initialized");
}
if(ccuHost.empty() || ccuPort <= 0 || ccuPort > 65535) {
throw std::runtime_error("Invalid CCU address: " + ccuHost + ":" + std::to_string(ccuPort));
}
sendMsg(msgBuf,sizeof(msg_CcuColCmdMsg));
}
catch(const std::exception& e)
+1
View File
@@ -50,6 +50,7 @@ public:
bool sendDisSysCmd(const disHighBVolBusCmd cmd);
bool sendDisSysCmd(const disLowBusCmd cmd);
bool sendMsg(const unsigned char* msg,unsigned int size){
// cout << "sendMsgTo" << ccuHost << ":" << ccuPort << endl;
udpScoket->iSendMessageTo((void*) msg, size, ccuPort, ccuHost);
return true;
}