diff --git a/data/msg/uPower_fcsState_st.json b/data/msg/uPower_fcsState_st.json index 659e8c6..25b2bd9 100644 --- a/data/msg/uPower_fcsState_st.json +++ b/data/msg/uPower_fcsState_st.json @@ -1,30 +1,30 @@ { - "backpressure1_fp32": 2.0723892223585928e-36, - "backpressure2_fp32": 4.5823861081885843e-41, - "ch4oConcent1_fp32": 2.0653300860562609e-36, - "ch4oConcent2_fp32": 4.5823861081885843e-41, - "ch4o_fp32": 4.5823861081885843e-41, - "excTemp_fp32": 4.5823861081885843e-41, - "faultCode1_uint16": 12207, - "faultCode2_uint16": 48388, + "backpressure1_fp32": 2.3670901456028489e+36, + "backpressure2_fp32": 4.5630481893809018e-41, + "ch4oConcent1_fp32": 4.2038953929744512e-45, + "ch4oConcent2_fp32": 0.0, + "ch4o_fp32": 0.0, + "excTemp_fp32": 4.5630481893809018e-41, + "faultCode1_uint16": 20381, + "faultCode2_uint16": 13180, "faultCode3_uint16": 127, - "fcuCurIns_fp32": 0.0, + "fcuCurIns_fp32": 5.7521569093081287e-30, "fcuCurPower_fp32": 0.0, - "fcuCur_fp32": 1.3331306974422932e-36, - "fcuFaultLeve_uint8": 0, - "fcuOutIns_fp32": 4.5916346780531281e-41, - "fcuOutPower_fp32": 1.0758022868002197e+20, - "fcuState_uint8": 0, - "fcuVolIns_fp32": 1.0758019349564988e+20, - "fcuVolPower_fp32": 0.0, + "fcuCur_fp32": -4.4122612457458051e+32, + "fcuFaultLeve_uint8": 124, + "fcuOutIns_fp32": 4.5630481893809018e-41, + "fcuOutPower_fp32": 3.4750156610080462e+36, + "fcuState_uint8": 39, + "fcuVolIns_fp32": 4.3786375736699354e+36, + "fcuVolPower_fp32": 4.5630481893809018e-41, "fcuVol_fp32": 0.0, - "flameDet_uint8": 96, - "h2Concent1_fp32": 0.0, + "flameDet_uint8": 88, + "h2Concent1_fp32": -644240900096.0, "h2Concent2_fp32": 0.0, - "o2Concent1_fp32": 1.3337242202094549e-36, - "o2Concent2_fp32": 4.5823861081885843e-41, - "o2Level_fp32": 4.5823861081885843e-41, - "o2_fp32": 2.6989008422895977e-42, - "powLimit_fp32": 4.5916346780531281e-41, - "powerGen_fp32": 2.0725797092666392e-36 + "o2Concent1_fp32": 0.0, + "o2Concent2_fp32": 0.0, + "o2Level_fp32": 3.0722067531857289e-41, + "o2_fp32": 0.0, + "powLimit_fp32": 4.5630481893809018e-41, + "powerGen_fp32": 9.8090892502737195e-45 } \ No newline at end of file diff --git a/data/msg/uPower_liBatState_st.json b/data/msg/uPower_liBatState_st.json index e8f22e5..869273d 100644 --- a/data/msg/uPower_liBatState_st.json +++ b/data/msg/uPower_liBatState_st.json @@ -2,37 +2,37 @@ "fcuCurToInsLimit1_uint8": 0, "fcuCurToinsLimit2_uint8": 0, "insBatCurElect_uint16": 127, - "insBatLimit_uint16": 48, + "insBatLimit_uint16": 13180, "insBatSoc_uint8": 0, - "insBatTotalElect_uint16": 57470, - "insChargeState_uint8": 144, + "insBatTotalElect_uint16": 20377, + "insChargeState_uint8": 85, "insCurSocHold1_uint8": 0, "insCurSocHold2_uint8": 0, "insCurSocHold3_uint8": 0, - "insCurrent_uint16": 46080, - "insLinkVol_uint16": 48387, - "insNegRelayState_uint8": 218, - "insNegRes_uint16": 65376, - "insPackVol_uint16": 127, + "insCurrent_uint16": 0, + "insLinkVol_uint16": 0, + "insNegRelayState_uint8": 208, + "insNegRes_uint16": 65235, + "insPackVol_uint16": 0, "insPosRelayState_uint8": 0, - "insPosRes_uint16": 47775, - "insfaultCode_uint8": 19, - "powBatCurElect_uint16": 4096, - "powBatSoc_uint8": 152, - "powBatTotalElect_uint16": 48387, - "powChargeState_uint8": 224, - "powCurSocHold1_uint8": 164, - "powCurSocHold2_uint8": 76, - "powCurSocHold3_uint8": 48, - "powCurToInsLimit1_uint8": 4, - "powCurToinsLimit2_uint8": 189, - "powCur_uint16": 12368, + "insPosRes_uint16": 5632, + "insfaultCode_uint8": 0, + "powBatCurElect_uint16": 58368, + "powBatSoc_uint8": 208, + "powBatTotalElect_uint16": 13180, + "powChargeState_uint8": 48, + "powCurSocHold1_uint8": 208, + "powCurSocHold2_uint8": 97, + "powCurSocHold3_uint8": 39, + "powCurToInsLimit1_uint8": 124, + "powCurToinsLimit2_uint8": 51, + "powCur_uint16": 20538, "powLinkVol_uint16": 127, - "powNegRelayState_uint8": 135, + "powNegRelayState_uint8": 39, "powNegRes_uint16": 127, - "powPosRelayState_uint8": 22, - "powPosRes_uint16": 48388, - "powerBatLimit_uint16": 0, - "powerPackVol_uint16": 51712, + "powPosRelayState_uint8": 97, + "powPosRes_uint16": 13180, + "powerBatLimit_uint16": 127, + "powerPackVol_uint16": 18432, "powfaultCode_uint8": 0 } \ No newline at end of file diff --git a/src/pPowerManger/PowerManger.cpp b/src/pPowerManger/PowerManger.cpp index 630fd49..2ce45a3 100644 --- a/src/pPowerManger/PowerManger.cpp +++ b/src/pPowerManger/PowerManger.cpp @@ -49,6 +49,7 @@ PowerManger::PowerManger() m_db = new SQLiteDB(dbPath); m_db->createPowerDataTables(); m_db->createStateTables(); + m_db->createDisCcuSysTables(); //注册状态机 m_fsm.RegisterState("Init", 1, m_Inite_s1); @@ -288,33 +289,35 @@ bool PowerManger::updatePmState() double time; m_udpComm.popMsgFormQueue(time,m_CcuCurrentState); m_pmCurrentState.ccustate = m_CcuCurrentState.data; - double df; - IsSkewed(time, &df); - cout << "CcuState Skew:" << df << endl; + m_db->insertCcuData(m_CcuCurrentState.data); //写入数据库 } 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); //写入数据库 } 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); //写入数据库 } 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); //写入数据库 } 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); //写入数据库 } return true; } @@ -459,7 +462,9 @@ bool PowerManger::writeDataToDB() m_stateData.note = "No fault"; if(!m_db->insertStateData(m_stateData)) return false; - + // //写入配电系统状态 + // if(!m_db->insertDisCcuData(m_pmCurrentState)) + // return false; return true; } @@ -479,7 +484,6 @@ void PowerManger::printState(FSM_State st, string desp){ int PowerManger::handleInite_s1(FSM_State &fsm, void* pData) { - cout << fsm.name << endl; PowerManger* pm = (PowerManger*)pData; //1. 检查复合管控器连接 如果10S则进入故障状态 double StartTime = pm->GetAppStartTime(); @@ -522,6 +526,7 @@ int PowerManger::handleInite_s1(FSM_State &fsm, void* pData) int PowerManger::handleStandby_s2(FSM_State &fsm, void* pData) { PowerManger* pm = (PowerManger*)pData; + pm->m_stateData.note = "待机状态"; while (true) { string desp = "待机状态"; @@ -551,31 +556,6 @@ int PowerManger::handleFault_s99(FSM_State &fsm, void* pData) } -// int PowerManger::handleState1(FSM_State &fsm,void* pData) -// { -// cout << fsm.name << endl; -// PowerManger* pm = (PowerManger*)pData; -// fsm = pm->m_fsm.getState("PM_STATE2"); -// // pm->m_pmEvent = "a"; -// return 0; -// } -// int PowerManger::handleState2(FSM_State &fsm,void* pData) -// { -// cout << fsm.name << endl; -// PowerManger* pm = (PowerManger*)pData; -// fsm = pm->m_fsm.getState("PM_STATE3"); -// // pm->m_pmEvent = "b"; -// return 0; -// } -// int PowerManger::handleState3(FSM_State &fsm,void* pData) -// { -// cout << fsm.name << endl; -// PowerManger* pm = (PowerManger*)pData; -// fsm = pm->m_fsm.getState("PM_STATE"); -// // pm->m_pmEvent = "c"; -// return 0; -// } - void PowerManger::initTargetDisBreakerState() { m_targetDisBreakerState.bus1Breaker.fuelCellCircuitBreaker = 0; diff --git a/src/pPowerManger/PowerManger.h b/src/pPowerManger/PowerManger.h index d5bf260..84b7e9c 100644 --- a/src/pPowerManger/PowerManger.h +++ b/src/pPowerManger/PowerManger.h @@ -125,17 +125,13 @@ class PowerManger : public AppCastingMOOSApp public: //状态实现 void printState(FSM_State st, string desp); bool FSM_RUN_IS_OK = true; + StateData m_stateData; FSM_State m_pmState; //当前状态 string m_pmEvent; FSM m_fsm; - //系统状态定义 - FSM_State m_state1; - FSM_State m_state2; - FSM_State m_state3; - FSM_State startState; - FSM_State stopState; + //系统状态定义 //初始化状态 FSM_State m_Inite_s1; static int handleInite_s1(FSM_State &fsm, void* pData); diff --git a/src/pPowerManger/SQLiteDB.cpp b/src/pPowerManger/SQLiteDB.cpp index 441c152..023a7a9 100644 --- a/src/pPowerManger/SQLiteDB.cpp +++ b/src/pPowerManger/SQLiteDB.cpp @@ -11,6 +11,213 @@ SQLiteDB::SQLiteDB(const std::string& dbPath) { m_dbPath = dbPath; } +template +bool SQLiteDB::bindParameters(sqlite3_stmt* stmt, const T& data) { + // 需要为每个具体类型特化实现 + return false; +} + +// 为ccuState特化实现 +template<> +bool SQLiteDB::bindParameters(sqlite3_stmt* stmt, const ccuState& data) { + int index = 1; + sqlite3_bind_int(stmt, index++, data.currentWorkCondition); + sqlite3_bind_int(stmt, index++, data.fcuState); + sqlite3_bind_int(stmt, index++, data.fcuFaultLeve); + sqlite3_bind_double(stmt, index++, data.powerLimit); + sqlite3_bind_double(stmt, index++, data.totalPowerGeneration); + sqlite3_bind_double(stmt, index++, data.ch4o); + sqlite3_bind_double(stmt, index++, data.o2); + sqlite3_bind_double(stmt, index++, data.fcuVoltage); + sqlite3_bind_double(stmt, index++, data.fcuCurrent); + sqlite3_bind_double(stmt, index++, data.o2Level); + sqlite3_bind_double(stmt, index++, data.placeholder1); + sqlite3_bind_double(stmt, index++, data.exchangerTemperature); + sqlite3_bind_double(stmt, index++, data.fcuOutPower); + sqlite3_bind_double(stmt, index++, data.fcuOutIns); + sqlite3_bind_double(stmt, index++, data.fcuVoltageIns); + sqlite3_bind_double(stmt, index++, data.fcuVoltagePower); + sqlite3_bind_double(stmt, index++, data.fcuCurrentIns); + sqlite3_bind_double(stmt, index++, data.fcuCurrentPower); + sqlite3_bind_double(stmt, index++, data.h2Concentration1); + sqlite3_bind_double(stmt, index++, data.h2Concentration2); + sqlite3_bind_double(stmt, index++, data.o2Concentration1); + sqlite3_bind_double(stmt, index++, data.o2Concentration2); + sqlite3_bind_double(stmt, index++, data.ch4oConcentration1); + sqlite3_bind_double(stmt, index++, data.ch4oConcentration2); + sqlite3_bind_double(stmt, index++, data.backpressure1); + sqlite3_bind_double(stmt, index++, data.backpressure2); + sqlite3_bind_int(stmt, index++, data.flameDetection); + sqlite3_bind_int(stmt, index++, data.fcuGZ_1L); + sqlite3_bind_int(stmt, index++, data.fcuGZ_1H); + sqlite3_bind_int(stmt, index++, data.fcuGZ_2L); + sqlite3_bind_int(stmt, index++, data.fcuGZ_2H); + sqlite3_bind_int(stmt, index++, data.fcuGZ_3L); + sqlite3_bind_int(stmt, index++, data.fcuGZ_3H); + sqlite3_bind_int(stmt, index++, data.insPosRelayState); + sqlite3_bind_int(stmt, index++, data.insNegRelayState); + sqlite3_bind_int(stmt, index++, data.powerPosRelayState); + sqlite3_bind_int(stmt, index++, data.powerNegRelayState); + sqlite3_bind_int(stmt, index++, data.insBatLimit); + sqlite3_bind_int(stmt, index++, data.powerBatLimit); + sqlite3_bind_int(stmt, index++, data.insBatSoc); + sqlite3_bind_int(stmt, index++, data.powerBatSoc); + sqlite3_bind_int(stmt, index++, data.insBatTotalElectricity); + sqlite3_bind_int(stmt, index++, data.powerBatTotalElectricity); + sqlite3_bind_int(stmt, index++, data.insBatCurrentElectricity); + sqlite3_bind_int(stmt, index++, data.powerBatCurrentElectricity); + sqlite3_bind_int(stmt, index++, data.insChargeState); + sqlite3_bind_int(stmt, index++, data.powerChargeState); + sqlite3_bind_int(stmt, index++, data.insLinkVoltage); + sqlite3_bind_int(stmt, index++, data.insPackVoltage); + sqlite3_bind_int(stmt, index++, data.insCurrent); + sqlite3_bind_int(stmt, index++, data.insPosInsulationResistance); + sqlite3_bind_int(stmt, index++, data.insNegInsulationResistance); + sqlite3_bind_int(stmt, index++, data.powerLinkVoltage); + sqlite3_bind_int(stmt, index++, data.powerPackVoltage); + sqlite3_bind_int(stmt, index++, data.powerCurrent); + sqlite3_bind_int(stmt, index++, data.powerPosInsulationResistance); + sqlite3_bind_int(stmt, index++, data.powerNegInsulationResistance); + sqlite3_bind_int(stmt, index++, data.insEmergencyStatusWord); + sqlite3_bind_int(stmt, index++, data.powerEmergencyStatusWord); + sqlite3_bind_int(stmt, index++, data.insAlarmIdentificationWord); + sqlite3_bind_int(stmt, index++, data.powerAlarmIdentificationWord); + sqlite3_bind_int(stmt, index++, data.insCurrentSocHold1); + sqlite3_bind_int(stmt, index++, data.insCurrentSocHold2); + sqlite3_bind_int(stmt, index++, data.insCurrentSocHold3); + sqlite3_bind_int(stmt, index++, data.fcuCurrentToInsLimit1); + sqlite3_bind_int(stmt, index++, data.fcuCurrentToinsLimit2); + sqlite3_bind_int(stmt, index++, data.powerCurrentSocHold1); + sqlite3_bind_int(stmt, index++, data.powerCurrentSocHold2); + sqlite3_bind_int(stmt, index++, data.powerCurrentSocHold3); + sqlite3_bind_int(stmt, index++, data.powerCurrentToInsLimit1); + sqlite3_bind_int(stmt, index++, data.powerCurrentToinsLimit2); + sqlite3_bind_double(stmt, index++, data.placeholder2); + sqlite3_bind_double(stmt, index++, data.placeholder3); + sqlite3_bind_double(stmt, index++, data.placeholder4); + sqlite3_bind_double(stmt, index++, data.placeholder5); + return true; +} + +// 为disHighVolBusState特化实现 +template<> +bool SQLiteDB::bindParameters(sqlite3_stmt* stmt, const disHighVolBusState& data) { + int index = 1; + sqlite3_bind_int(stmt, index++, data.fuelCellCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.powerLithiumBatteryCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.propulsionMotorCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.lithiumBatteryGroupInstrumentCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.dcDc5ModuleCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.bowHighVoltageDistributionBoxCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.sternFTDevice45CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.sternRudderSwitch1CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.sternRudderSwitch2CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.fbReservedCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.tyzReservedCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.reservedCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.busInsulationLow); + sqlite3_bind_int(stmt, index++, data.busAInsulationHigh); + sqlite3_bind_int(stmt, index++, data.lostPower); + sqlite3_bind_int(stmt, index++, data.lostEmerPower); + sqlite3_bind_int(stmt, index++, data.powerBusVoltage); + sqlite3_bind_int(stmt, index++, data.dcDc5ModuleCurrent); + sqlite3_bind_int(stmt, index++, data.powerBusCurrent); + sqlite3_bind_int(stmt, index++, data.busbarAVoltage); + sqlite3_bind_int(stmt, index++, data.propulsionMotorControlBoxCurrent); + sqlite3_bind_int(stmt, index++, data.busbarACurrent); + sqlite3_bind_int(stmt, index++, data.lithiumBatteryGroupMeterCurrent); + sqlite3_bind_int(stmt, index++, data.bowHighVoltageDistributionBoxCurrent); + sqlite3_bind_int(stmt, index++, data.sternFTDevice45Current); + sqlite3_bind_int(stmt, index++, data.tyzReservedSwitchCurrent); + sqlite3_bind_int(stmt, index++, data.sternRudderSwitch1Current); + sqlite3_bind_int(stmt, index++, data.reservedCurrent1); + sqlite3_bind_int(stmt, index++, data.sternRudderSwitch2Current); + sqlite3_bind_int(stmt, index++, data.reservedCurrent2); + sqlite3_bind_int(stmt, index++, data.fbReservedSwitchCurrent); + sqlite3_bind_int(stmt, index++, data.reservedCurrent3); + return true; +} + +// 为disHighAVolBusState特化实现 +template<> +bool SQLiteDB::bindParameters(sqlite3_stmt* stmt, const disHighAVolBusState& data) { + int index = 1; + sqlite3_bind_int(stmt, index++, data.bowFTDevice123CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.actuatorCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.mastSteeringGearControlBoxCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.xczCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.bowRudderControlBoxCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.openWaterCoverStartCylinderCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.instrumentPowerFailureSignal); + sqlite3_bind_int(stmt, index++, data.emergencyPowerFailureSignal); + sqlite3_bind_int(stmt, index++, data.waterIngressionAlarm); + sqlite3_bind_int(stmt, index++, data.busbarBVoltage); + sqlite3_bind_int(stmt, index++, data.busbarBCurrent); + sqlite3_bind_int(stmt, index++, data.bowFTDevice123Current); + sqlite3_bind_int(stmt, index++, data.actuatorCurrent); + sqlite3_bind_int(stmt, index++, data.mastSteeringGearControlBoxCurrent); + sqlite3_bind_int(stmt, index++, data.xczCurrent); + sqlite3_bind_int(stmt, index++, data.bowRudderControlBoxCurrent); + sqlite3_bind_int(stmt, index++, data.openWaterCoverStartCylinderCurrent); + return true; +} + +// 为disHighBVolBusState特化实现 +template<> +bool SQLiteDB::bindParameters(sqlite3_stmt* stmt, const disHighBVolBusState& data) { + int index = 1; + sqlite3_bind_int(stmt, index++, data.lithiumBatteryGroupInstrumentCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.bowLowVoltageDistributionBoxCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.unit4InstrumentDC48VCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.dcC1DCDistributionPanelCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.reservedCircuitBreaker1); + sqlite3_bind_int(stmt, index++, data.reservedCircuitBreaker2); + sqlite3_bind_int(stmt, index++, data.emergencyLithiumBatteryGroup2CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.instrumentPowerFailureSignal); + sqlite3_bind_int(stmt, index++, data.emergencyPowerFailureSignal); + sqlite3_bind_int(stmt, index++, data.instrumentBusbarInsulationLow); + sqlite3_bind_int(stmt, index++, data.instrumentBusbarVoltage); + sqlite3_bind_int(stmt, index++, data.bowLowVoltageDistributionBoxCurrent); + sqlite3_bind_int(stmt, index++, data.dcC1InstrumentDC48VCurrent); + sqlite3_bind_int(stmt, index++, data.reservedCurrent1); + sqlite3_bind_int(stmt, index++, data.emergencyLithiumBatteryGroup2Current); + sqlite3_bind_int(stmt, index++, data.lithiumBatteryGroupInstrumentCurrent); + sqlite3_bind_int(stmt, index++, data.unit4InstrumentDC48VCurrent); + sqlite3_bind_int(stmt, index++, data.reservedCurrent2); + sqlite3_bind_int(stmt, index++, data.emergency2BusbarVoltage); + sqlite3_bind_int(stmt, index++, data.compositeEnergyManagementSystemEmergencyDC48VCurrent); + sqlite3_bind_int(stmt, index++, data.fuelCellSecuritySystemEmergencyDC48VCurrent); + sqlite3_bind_int(stmt, index++, data.plcControlPowerCurrent); + return true; +} + +// 为disLowBusState特化实现 +template<> +bool SQLiteDB::bindParameters(sqlite3_stmt* stmt, const disLowBusState& data) { + int index = 1; + sqlite3_bind_int(stmt, index++, data.unit1CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.unit2CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.unit3CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.unit5CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.bowPZDeviceCircuitBreaker); + sqlite3_bind_int(stmt, index++, data.reservedCircuitBreaker1); + sqlite3_bind_int(stmt, index++, data.reservedCircuitBreaker2); + sqlite3_bind_int(stmt, index++, data.emergencyLithiumBatteryGroup1CircuitBreaker); + sqlite3_bind_int(stmt, index++, data.powerFailureSignal); + sqlite3_bind_int(stmt, index++, data.waterIngressionAlarm); + sqlite3_bind_int(stmt, index++, data.instrumentBusbarVoltage); + sqlite3_bind_int(stmt, index++, data.instrumentBusbarCurrent); + sqlite3_bind_int(stmt, index++, data.unit1Current); + sqlite3_bind_int(stmt, index++, data.unit2Current); + sqlite3_bind_int(stmt, index++, data.unit3Current); + sqlite3_bind_int(stmt, index++, data.unit5Current); + sqlite3_bind_int(stmt, index++, data.bowPZDeviceCurrent); + sqlite3_bind_int(stmt, index++, data.emergencyLithiumBatteryGroup1Current); + sqlite3_bind_int(stmt, index++, data.emergency1BusbarVoltage); + return true; +} + + SQLiteDB::~SQLiteDB() { if (m_db) { sqlite3_close(m_db); @@ -201,3 +408,465 @@ bool SQLiteDB::clearStateData() { } return true; } + +bool SQLiteDB::insertDisCcuData(pmState s, bool filter) { + if (!m_db) return false; + + // 检查是否需要过滤 + if (filter) { + // TODO: 实现过滤逻辑,检查是否与上一条记录相同 + } + // 插入ccu_state表 + const char* ccuSql = "INSERT INTO ccu_state (" + "currentWorkCondition, fcuState, fcuFaultLeve, powerLimit, totalPowerGeneration, " + "ch4o, o2, fcuVoltage, fcuCurrent, o2Level, placeholder1, exchangerTemperature, " + "fcuOutPower, fcuOutIns, fcuVoltageIns, fcuVoltagePower, fcuCurrentIns, fcuCurrentPower, " + "h2Concentration1, h2Concentration2, o2Concentration1, o2Concentration2, " + "ch4oConcentration1, ch4oConcentration2, backpressure1, backpressure2, flameDetection, " + "fcuGZ_1L, fcuGZ_1H, fcuGZ_2L, fcuGZ_2H, fcuGZ_3L, fcuGZ_3H, insPosRelayState, " + "insNegRelayState, powerPosRelayState, powerNegRelayState, insBatLimit, powerBatLimit, " + "insBatSoc, powerBatSoc, insBatTotalElectricity, powerBatTotalElectricity, " + "insBatCurrentElectricity, powerBatCurrentElectricity, insChargeState, powerChargeState, " + "insLinkVoltage, insPackVoltage, insCurrent, insPosInsulationResistance, " + "insNegInsulationResistance, powerLinkVoltage, powerPackVoltage, powerCurrent, " + "powerPosInsulationResistance, powerNegInsulationResistance, insEmergencyStatusWord, " + "powerEmergencyStatusWord, insAlarmIdentificationWord, powerAlarmIdentificationWord, " + "insCurrentSocHold1, insCurrentSocHold2, insCurrentSocHold3, fcuCurrentToInsLimit1, " + "fcuCurrentToinsLimit2, powerCurrentSocHold1, powerCurrentSocHold2, powerCurrentSocHold3, " + "powerCurrentToInsLimit1, powerCurrentToinsLimit2, placeholder2, placeholder3, " + "placeholder4, placeholder5) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + // 插入dis_high_vol_bus_state表 + const char* bus1Sql = "INSERT INTO dis_high_vol_bus_state (" + "fuelCellCircuitBreaker, powerLithiumBatteryCircuitBreaker, propulsionMotorCircuitBreaker, " + "lithiumBatteryGroupInstrumentCircuitBreaker, dcDc5ModuleCircuitBreaker, " + "bowHighVoltageDistributionBoxCircuitBreaker, sternFTDevice45CircuitBreaker, " + "sternRudderSwitch1CircuitBreaker, sternRudderSwitch2CircuitBreaker, fbReservedCircuitBreaker, " + "tyzReservedCircuitBreaker, reservedCircuitBreaker, busInsulationLow, busAInsulationHigh, " + "lostPower, lostEmerPower, powerBusVoltage, dcDc5ModuleCurrent, powerBusCurrent, " + "busbarAVoltage, propulsionMotorControlBoxCurrent, busbarACurrent, " + "lithiumBatteryGroupMeterCurrent, bowHighVoltageDistributionBoxCurrent, " + "sternFTDevice45Current, tyzReservedSwitchCurrent, sternRudderSwitch1Current, " + "reservedCurrent1, sternRudderSwitch2Current, reservedCurrent2, fbReservedSwitchCurrent, " + "reservedCurrent3) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?);"; + + // 插入dis_high_a_vol_bus_state表 + const char* bus2Sql = "INSERT INTO dis_high_a_vol_bus_state (" + "bowFTDevice123CircuitBreaker, actuatorCircuitBreaker, mastSteeringGearControlBoxCircuitBreaker, " + "xczCircuitBreaker, bowRudderControlBoxCircuitBreaker, openWaterCoverStartCylinderCircuitBreaker, " + "instrumentPowerFailureSignal, emergencyPowerFailureSignal, waterIngressionAlarm, " + "busbarBVoltage, busbarBCurrent, bowFTDevice123Current, actuatorCurrent, " + "mastSteeringGearControlBoxCurrent, xczCurrent, bowRudderControlBoxCurrent, " + "openWaterCoverStartCylinderCurrent) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + // 插入dis_high_b_vol_bus_state表 + const char* bus3Sql = "INSERT INTO dis_high_b_vol_bus_state (" + "lithiumBatteryGroupInstrumentCircuitBreaker, bowLowVoltageDistributionBoxCircuitBreaker, " + "unit4InstrumentDC48VCircuitBreaker, dcC1DCDistributionPanelCircuitBreaker, " + "reservedCircuitBreaker1, reservedCircuitBreaker2, emergencyLithiumBatteryGroup2CircuitBreaker, " + "instrumentPowerFailureSignal, emergencyPowerFailureSignal, instrumentBusbarInsulationLow, " + "instrumentBusbarVoltage, bowLowVoltageDistributionBoxCurrent, dcC1InstrumentDC48VCurrent, " + "reservedCurrent1, emergencyLithiumBatteryGroup2Current, lithiumBatteryGroupInstrumentCurrent, " + "unit4InstrumentDC48VCurrent, reservedCurrent2, emergency2BusbarVoltage, " + "compositeEnergyManagementSystemEmergencyDC48VCurrent, fuelCellSecuritySystemEmergencyDC48VCurrent, " + "plcControlPowerCurrent) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);"; + + // 插入dis_low_bus_state表 + const char* bus4Sql = "INSERT INTO dis_low_bus_state (" + "unit1CircuitBreaker, unit2CircuitBreaker, unit3CircuitBreaker, unit5CircuitBreaker, " + "bowPZDeviceCircuitBreaker, reservedCircuitBreaker1, reservedCircuitBreaker2, " + "emergencyLithiumBatteryGroup1CircuitBreaker, powerFailureSignal, waterIngressionAlarm, " + "instrumentBusbarVoltage, instrumentBusbarCurrent, unit1Current, unit2Current, " + "unit3Current, unit5Current, bowPZDeviceCurrent, emergencyLithiumBatteryGroup1Current, " + "emergency1BusbarVoltage) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + // 执行所有插入操作 + bool result = true; + result = result && executeInsert(ccuSql, s.ccustate); + result = result && executeInsert(bus1Sql, s.bus1State); + result = result && executeInsert(bus2Sql, s.bus2State); + result = result && executeInsert(bus3Sql, s.bus3State); + result = result && executeInsert(bus4Sql, s.bus4State); + + return result; +} + +bool SQLiteDB::insertCcuData(ccuState s) +{ + if (!m_db) return false; + + + // 插入ccu_state表 + const char* ccuSql = "INSERT INTO ccu_state (" + "currentWorkCondition, fcuState, fcuFaultLeve, powerLimit, totalPowerGeneration, " + "ch4o, o2, fcuVoltage, fcuCurrent, o2Level, placeholder1, exchangerTemperature, " + "fcuOutPower, fcuOutIns, fcuVoltageIns, fcuVoltagePower, fcuCurrentIns, fcuCurrentPower, " + "h2Concentration1, h2Concentration2, o2Concentration1, o2Concentration2, " + "ch4oConcentration1, ch4oConcentration2, backpressure1, backpressure2, flameDetection, " + "fcuGZ_1L, fcuGZ_1H, fcuGZ_2L, fcuGZ_2H, fcuGZ_3L, fcuGZ_3H, insPosRelayState, " + "insNegRelayState, powerPosRelayState, powerNegRelayState, insBatLimit, powerBatLimit, " + "insBatSoc, powerBatSoc, insBatTotalElectricity, powerBatTotalElectricity, " + "insBatCurrentElectricity, powerBatCurrentElectricity, insChargeState, powerChargeState, " + "insLinkVoltage, insPackVoltage, insCurrent, insPosInsulationResistance, " + "insNegInsulationResistance, powerLinkVoltage, powerPackVoltage, powerCurrent, " + "powerPosInsulationResistance, powerNegInsulationResistance, insEmergencyStatusWord, " + "powerEmergencyStatusWord, insAlarmIdentificationWord, powerAlarmIdentificationWord, " + "insCurrentSocHold1, insCurrentSocHold2, insCurrentSocHold3, fcuCurrentToInsLimit1, " + "fcuCurrentToinsLimit2, powerCurrentSocHold1, powerCurrentSocHold2, powerCurrentSocHold3, " + "powerCurrentToInsLimit1, powerCurrentToinsLimit2, placeholder2, placeholder3, " + "placeholder4, placeholder5) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + // 执行所有插入操作 + bool result = true; + result = executeInsert(ccuSql, s); + return result; +} +bool SQLiteDB::insertDisHighVolBusData(disHighVolBusState s) +{ + if (!m_db) return false; + + // 插入dis_high_vol_bus_state表 + const char* bus1Sql = "INSERT INTO dis_high_vol_bus_state (" + "fuelCellCircuitBreaker, powerLithiumBatteryCircuitBreaker, propulsionMotorCircuitBreaker, " + "lithiumBatteryGroupInstrumentCircuitBreaker, dcDc5ModuleCircuitBreaker, " + "bowHighVoltageDistributionBoxCircuitBreaker, sternFTDevice45CircuitBreaker, " + "sternRudderSwitch1CircuitBreaker, sternRudderSwitch2CircuitBreaker, fbReservedCircuitBreaker, " + "tyzReservedCircuitBreaker, reservedCircuitBreaker, busInsulationLow, busAInsulationHigh, " + "lostPower, lostEmerPower, powerBusVoltage, dcDc5ModuleCurrent, powerBusCurrent, " + "busbarAVoltage, propulsionMotorControlBoxCurrent, busbarACurrent, " + "lithiumBatteryGroupMeterCurrent, bowHighVoltageDistributionBoxCurrent, " + "sternFTDevice45Current, tyzReservedSwitchCurrent, sternRudderSwitch1Current, " + "reservedCurrent1, sternRudderSwitch2Current, reservedCurrent2, fbReservedSwitchCurrent, " + "reservedCurrent3) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + "?, ?, ?, ?, ?, ?);"; + + // 执行所有插入操作 + bool result = true; + result = executeInsert(bus1Sql, s); + + return result; +} +bool SQLiteDB::insertDisHighAVolBusData(disHighAVolBusState s) +{ + if (!m_db) return false; + + // 插入dis_high_a_vol_bus_state表 + const char* bus2Sql = "INSERT INTO dis_high_a_vol_bus_state (" + "bowFTDevice123CircuitBreaker, actuatorCircuitBreaker, mastSteeringGearControlBoxCircuitBreaker, " + "xczCircuitBreaker, bowRudderControlBoxCircuitBreaker, openWaterCoverStartCylinderCircuitBreaker, " + "instrumentPowerFailureSignal, emergencyPowerFailureSignal, waterIngressionAlarm, " + "busbarBVoltage, busbarBCurrent, bowFTDevice123Current, actuatorCurrent, " + "mastSteeringGearControlBoxCurrent, xczCurrent, bowRudderControlBoxCurrent, " + "openWaterCoverStartCylinderCurrent) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + // 执行所有插入操作 + bool result = true; + result = executeInsert(bus2Sql, s); + + return result; +} +bool SQLiteDB::insertDisHighBVolBusData(disHighBVolBusState s) +{ + if (!m_db) return false; + + const char* bus3Sql = "INSERT INTO dis_high_b_vol_bus_state (" + "lithiumBatteryGroupInstrumentCircuitBreaker, bowLowVoltageDistributionBoxCircuitBreaker, " + "unit4InstrumentDC48VCircuitBreaker, dcC1DCDistributionPanelCircuitBreaker, " + "reservedCircuitBreaker1, reservedCircuitBreaker2, emergencyLithiumBatteryGroup2CircuitBreaker, " + "instrumentPowerFailureSignal, emergencyPowerFailureSignal, instrumentBusbarInsulationLow, " + "instrumentBusbarVoltage, bowLowVoltageDistributionBoxCurrent, dcC1InstrumentDC48VCurrent, " + "reservedCurrent1, emergencyLithiumBatteryGroup2Current, lithiumBatteryGroupInstrumentCurrent, " + "unit4InstrumentDC48VCurrent, reservedCurrent2, emergency2BusbarVoltage, " + "compositeEnergyManagementSystemEmergencyDC48VCurrent, fuelCellSecuritySystemEmergencyDC48VCurrent, " + "plcControlPowerCurrent) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);"; + + // 执行所有插入操作 + bool result = true; + result = executeInsert(bus3Sql, s); + + return result; +} +bool SQLiteDB::insertDisLowBusData(disLowBusState s) +{ + if (!m_db) return false; + + // 插入dis_low_bus_state表 + const char* bus4Sql = "INSERT INTO dis_low_bus_state (" + "unit1CircuitBreaker, unit2CircuitBreaker, unit3CircuitBreaker, unit5CircuitBreaker, " + "bowPZDeviceCircuitBreaker, reservedCircuitBreaker1, reservedCircuitBreaker2, " + "emergencyLithiumBatteryGroup1CircuitBreaker, powerFailureSignal, waterIngressionAlarm, " + "instrumentBusbarVoltage, instrumentBusbarCurrent, unit1Current, unit2Current, " + "unit3Current, unit5Current, bowPZDeviceCurrent, emergencyLithiumBatteryGroup1Current, " + "emergency1BusbarVoltage) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + // 执行所有插入操作 + bool result = true; + result = executeInsert(bus4Sql, s); + + return result; +} + +template +bool SQLiteDB::executeInsert(const char* sql, const T& data) { + sqlite3_stmt* stmt; + if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) != SQLITE_OK) { + std::cerr << "Error preparing statement: " << sqlite3_errmsg(m_db) << std::endl; + return false; + } + + // 绑定参数 + if (!bindParameters(stmt, data)) { + sqlite3_finalize(stmt); + return false; + } + + if (sqlite3_step(stmt) != SQLITE_DONE) { + std::cerr << "Error inserting data: " << sqlite3_errmsg(m_db) << std::endl; + sqlite3_finalize(stmt); + return false; + } + + sqlite3_finalize(stmt); + return true; +} + + + +bool SQLiteDB::createDisCcuSysTables() { + if (!m_db) return false; + + // 创建ccu_state表 + const char* ccuStateSql = "CREATE TABLE IF NOT EXISTS ccu_state (" + "timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))," + "currentWorkCondition INTEGER NOT NULL," + "fcuState INTEGER NOT NULL," + "fcuFaultLeve INTEGER NOT NULL," + "powerLimit REAL NOT NULL," + "totalPowerGeneration REAL NOT NULL," + "ch4o REAL NOT NULL," + "o2 REAL NOT NULL," + "fcuVoltage REAL NOT NULL," + "fcuCurrent REAL NOT NULL," + "o2Level REAL NOT NULL," + "placeholder1 REAL NOT NULL," + "exchangerTemperature REAL NOT NULL," + "fcuOutPower REAL NOT NULL," + "fcuOutIns REAL NOT NULL," + "fcuVoltageIns REAL NOT NULL," + "fcuVoltagePower REAL NOT NULL," + "fcuCurrentIns REAL NOT NULL," + "fcuCurrentPower REAL NOT NULL," + "h2Concentration1 REAL NOT NULL," + "h2Concentration2 REAL NOT NULL," + "o2Concentration1 REAL NOT NULL," + "o2Concentration2 REAL NOT NULL," + "ch4oConcentration1 REAL NOT NULL," + "ch4oConcentration2 REAL NOT NULL," + "backpressure1 REAL NOT NULL," + "backpressure2 REAL NOT NULL," + "flameDetection INTEGER NOT NULL," + "fcuGZ_1L INTEGER NOT NULL," + "fcuGZ_1H INTEGER NOT NULL," + "fcuGZ_2L INTEGER NOT NULL," + "fcuGZ_2H INTEGER NOT NULL," + "fcuGZ_3L INTEGER NOT NULL," + "fcuGZ_3H INTEGER NOT NULL," + "insPosRelayState INTEGER NOT NULL," + "insNegRelayState INTEGER NOT NULL," + "powerPosRelayState INTEGER NOT NULL," + "powerNegRelayState INTEGER NOT NULL," + "insBatLimit INTEGER NOT NULL," + "powerBatLimit INTEGER NOT NULL," + "insBatSoc INTEGER NOT NULL," + "powerBatSoc INTEGER NOT NULL," + "insBatTotalElectricity INTEGER NOT NULL," + "powerBatTotalElectricity INTEGER NOT NULL," + "insBatCurrentElectricity INTEGER NOT NULL," + "powerBatCurrentElectricity INTEGER NOT NULL," + "insChargeState INTEGER NOT NULL," + "powerChargeState INTEGER NOT NULL," + "insLinkVoltage INTEGER NOT NULL," + "insPackVoltage INTEGER NOT NULL," + "insCurrent INTEGER NOT NULL," + "insPosInsulationResistance INTEGER NOT NULL," + "insNegInsulationResistance INTEGER NOT NULL," + "powerLinkVoltage INTEGER NOT NULL," + "powerPackVoltage INTEGER NOT NULL," + "powerCurrent INTEGER NOT NULL," + "powerPosInsulationResistance INTEGER NOT NULL," + "powerNegInsulationResistance INTEGER NOT NULL," + "insEmergencyStatusWord INTEGER NOT NULL," + "powerEmergencyStatusWord INTEGER NOT NULL," + "insAlarmIdentificationWord INTEGER NOT NULL," + "powerAlarmIdentificationWord INTEGER NOT NULL," + "insCurrentSocHold1 INTEGER NOT NULL," + "insCurrentSocHold2 INTEGER NOT NULL," + "insCurrentSocHold3 INTEGER NOT NULL," + "fcuCurrentToInsLimit1 INTEGER NOT NULL," + "fcuCurrentToinsLimit2 INTEGER NOT NULL," + "powerCurrentSocHold1 INTEGER NOT NULL," + "powerCurrentSocHold2 INTEGER NOT NULL," + "powerCurrentSocHold3 INTEGER NOT NULL," + "powerCurrentToInsLimit1 INTEGER NOT NULL," + "powerCurrentToinsLimit2 INTEGER NOT NULL," + "placeholder2 REAL NOT NULL," + "placeholder3 REAL NOT NULL," + "placeholder4 REAL NOT NULL," + "placeholder5 REAL NOT NULL);"; + + // 创建dis_high_vol_bus_state表 + const char* disHighVolBusStateSql = "CREATE TABLE IF NOT EXISTS dis_high_vol_bus_state (" + "timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))," + "fuelCellCircuitBreaker INTEGER NOT NULL," + "powerLithiumBatteryCircuitBreaker INTEGER NOT NULL," + "propulsionMotorCircuitBreaker INTEGER NOT NULL," + "lithiumBatteryGroupInstrumentCircuitBreaker INTEGER NOT NULL," + "dcDc5ModuleCircuitBreaker INTEGER NOT NULL," + "bowHighVoltageDistributionBoxCircuitBreaker INTEGER NOT NULL," + "sternFTDevice45CircuitBreaker INTEGER NOT NULL," + "sternRudderSwitch1CircuitBreaker INTEGER NOT NULL," + "sternRudderSwitch2CircuitBreaker INTEGER NOT NULL," + "fbReservedCircuitBreaker INTEGER NOT NULL," + "tyzReservedCircuitBreaker INTEGER NOT NULL," + "reservedCircuitBreaker INTEGER NOT NULL," + "busInsulationLow INTEGER NOT NULL," + "busAInsulationHigh INTEGER NOT NULL," + "lostPower INTEGER NOT NULL," + "lostEmerPower INTEGER NOT NULL," + "powerBusVoltage INTEGER NOT NULL," + "dcDc5ModuleCurrent INTEGER NOT NULL," + "powerBusCurrent INTEGER NOT NULL," + "busbarAVoltage INTEGER NOT NULL," + "propulsionMotorControlBoxCurrent INTEGER NOT NULL," + "busbarACurrent INTEGER NOT NULL," + "lithiumBatteryGroupMeterCurrent INTEGER NOT NULL," + "bowHighVoltageDistributionBoxCurrent INTEGER NOT NULL," + "sternFTDevice45Current INTEGER NOT NULL," + "tyzReservedSwitchCurrent INTEGER NOT NULL," + "sternRudderSwitch1Current INTEGER NOT NULL," + "reservedCurrent1 INTEGER NOT NULL," + "sternRudderSwitch2Current INTEGER NOT NULL," + "reservedCurrent2 INTEGER NOT NULL," + "fbReservedSwitchCurrent INTEGER NOT NULL," + "reservedCurrent3 INTEGER NOT NULL);"; + + // 创建dis_high_a_vol_bus_state表 + const char* disHighAVolBusStateSql = "CREATE TABLE IF NOT EXISTS dis_high_a_vol_bus_state (" + "timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))," + "bowFTDevice123CircuitBreaker INTEGER NOT NULL," + "actuatorCircuitBreaker INTEGER NOT NULL," + "mastSteeringGearControlBoxCircuitBreaker INTEGER NOT NULL," + "xczCircuitBreaker INTEGER NOT NULL," + "bowRudderControlBoxCircuitBreaker INTEGER NOT NULL," + "openWaterCoverStartCylinderCircuitBreaker INTEGER NOT NULL," + "instrumentPowerFailureSignal INTEGER NOT NULL," + "emergencyPowerFailureSignal INTEGER NOT NULL," + "waterIngressionAlarm INTEGER NOT NULL," + "busbarBVoltage INTEGER NOT NULL," + "busbarBCurrent INTEGER NOT NULL," + "bowFTDevice123Current INTEGER NOT NULL," + "actuatorCurrent INTEGER NOT NULL," + "mastSteeringGearControlBoxCurrent INTEGER NOT NULL," + "xczCurrent INTEGER NOT NULL," + "bowRudderControlBoxCurrent INTEGER NOT NULL," + "openWaterCoverStartCylinderCurrent INTEGER NOT NULL);"; + + // 创建dis_high_b_vol_bus_state表 + const char* disHighBVolBusStateSql = "CREATE TABLE IF NOT EXISTS dis_high_b_vol_bus_state (" + "timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))," + "lithiumBatteryGroupInstrumentCircuitBreaker INTEGER NOT NULL," + "bowLowVoltageDistributionBoxCircuitBreaker INTEGER NOT NULL," + "unit4InstrumentDC48VCircuitBreaker INTEGER NOT NULL," + "dcC1DCDistributionPanelCircuitBreaker INTEGER NOT NULL," + "reservedCircuitBreaker1 INTEGER NOT NULL," + "reservedCircuitBreaker2 INTEGER NOT NULL," + "emergencyLithiumBatteryGroup2CircuitBreaker INTEGER NOT NULL," + "instrumentPowerFailureSignal INTEGER NOT NULL," + "emergencyPowerFailureSignal INTEGER NOT NULL," + "instrumentBusbarInsulationLow INTEGER NOT NULL," + "instrumentBusbarVoltage INTEGER NOT NULL," + "bowLowVoltageDistributionBoxCurrent INTEGER NOT NULL," + "dcC1InstrumentDC48VCurrent INTEGER NOT NULL," + "reservedCurrent1 INTEGER NOT NULL," + "emergencyLithiumBatteryGroup2Current INTEGER NOT NULL," + "lithiumBatteryGroupInstrumentCurrent INTEGER NOT NULL," + "unit4InstrumentDC48VCurrent INTEGER NOT NULL," + "reservedCurrent2 INTEGER NOT NULL," + "emergency2BusbarVoltage INTEGER NOT NULL," + "compositeEnergyManagementSystemEmergencyDC48VCurrent INTEGER NOT NULL," + "fuelCellSecuritySystemEmergencyDC48VCurrent INTEGER NOT NULL," + "plcControlPowerCurrent INTEGER NOT NULL);"; + + // 创建dis_low_bus_state表 + const char* disLowBusStateSql = "CREATE TABLE IF NOT EXISTS dis_low_bus_state (" + "timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))," + "unit1CircuitBreaker INTEGER NOT NULL," + "unit2CircuitBreaker INTEGER NOT NULL," + "unit3CircuitBreaker INTEGER NOT NULL," + "unit5CircuitBreaker INTEGER NOT NULL," + "bowPZDeviceCircuitBreaker INTEGER NOT NULL," + "reservedCircuitBreaker1 INTEGER NOT NULL," + "reservedCircuitBreaker2 INTEGER NOT NULL," + "emergencyLithiumBatteryGroup1CircuitBreaker INTEGER NOT NULL," + "powerFailureSignal INTEGER NOT NULL," + "waterIngressionAlarm INTEGER NOT NULL," + "instrumentBusbarVoltage INTEGER NOT NULL," + "instrumentBusbarCurrent INTEGER NOT NULL," + "unit1Current INTEGER NOT NULL," + "unit2Current INTEGER NOT NULL," + "unit3Current INTEGER NOT NULL," + "unit5Current INTEGER NOT NULL," + "bowPZDeviceCurrent INTEGER NOT NULL," + "emergencyLithiumBatteryGroup1Current INTEGER NOT NULL," + "emergency1BusbarVoltage INTEGER NOT NULL);"; + + char* errMsg = nullptr; + bool result = true; + + // 执行所有表创建语句 + if (sqlite3_exec(m_db, ccuStateSql, nullptr, nullptr, &errMsg) != SQLITE_OK) { + std::cerr << "SQL error creating ccu_state table: " << errMsg << std::endl; + sqlite3_free(errMsg); + result = false; + } + + if (sqlite3_exec(m_db, disHighVolBusStateSql, nullptr, nullptr, &errMsg) != SQLITE_OK) { + std::cerr << "SQL error creating dis_high_vol_bus_state table: " << errMsg << std::endl; + sqlite3_free(errMsg); + result = false; + } + + if (sqlite3_exec(m_db, disHighAVolBusStateSql, nullptr, nullptr, &errMsg) != SQLITE_OK) { + std::cerr << "SQL error creating dis_high_a_vol_bus_state table: " << errMsg << std::endl; + sqlite3_free(errMsg); + result = false; + } + + if (sqlite3_exec(m_db, disHighBVolBusStateSql, nullptr, nullptr, &errMsg) != SQLITE_OK) { + std::cerr << "SQL error creating dis_high_b_vol_bus_state table: " << errMsg << std::endl; + sqlite3_free(errMsg); + result = false; + } + + if (sqlite3_exec(m_db, disLowBusStateSql, nullptr, nullptr, &errMsg) != SQLITE_OK) { + std::cerr << "SQL error creating dis_low_bus_state table: " << errMsg << std::endl; + sqlite3_free(errMsg); + result = false; + } + + return result; +} diff --git a/src/pPowerManger/SQLiteDB.h b/src/pPowerManger/SQLiteDB.h index fe7fe9c..010ded6 100644 --- a/src/pPowerManger/SQLiteDB.h +++ b/src/pPowerManger/SQLiteDB.h @@ -5,6 +5,8 @@ #include #include #include "json/json.h" +#include "ccuUdpMsg.h" +#include "pmSysvariable.h" using namespace std; struct PowerData { @@ -23,20 +25,44 @@ struct StateData { std::string note; }; +// 前向声明 +struct ccuState; +struct disHighVolBusState; +struct disHighAVolBusState; +struct disHighBVolBusState; +struct disLowBusState; + class SQLiteDB { public: SQLiteDB(const std::string& dbPath); ~SQLiteDB(); - + + // 模板函数声明 + template + bool executeInsert(const char* sql, const T& data); + + template + bool bindParameters(sqlite3_stmt* stmt, const T& data); + // 数据库操作函数 bool createPowerDataTables(); bool createStateTables(); + bool createDisCcuSysTables(); bool clearPowerData(); bool clearStateData(); + // 插入数据函数 bool insertPowerData(const PowerData& powerData); bool insertStateData(StateData stateData,bool filter=true); + bool insertCcuData(ccuState s); + bool insertDisHighVolBusData(disHighVolBusState s); + bool insertDisHighAVolBusData(disHighAVolBusState s); + bool insertDisHighBVolBusData(disHighBVolBusState s); + bool insertDisLowBusData(disLowBusState s); + + bool insertDisCcuData(pmState s,bool filter=true); + // 获取最新的状态数据 bool getStateData(StateData &stateData); diff --git a/src/pPowerManger/udpComm.cpp b/src/pPowerManger/udpComm.cpp index 1c0b715..15edb38 100644 --- a/src/pPowerManger/udpComm.cpp +++ b/src/pPowerManger/udpComm.cpp @@ -85,7 +85,7 @@ bool udpComm::getMsgFromBuff(char *m, msg_disHighVolBusFbMsg &s) return false; } s = *p; - cout << s.data.busbarAVoltage << endl; + // cout << s.data.busbarAVoltage << endl; return true; } @@ -382,7 +382,7 @@ bool udpComm::sendDisSysCmd(const disHighVolBusCmd cmd) msgBuf = reinterpret_cast(msg); cc = calculateChecksum(msgBuf,sizeof(msg_disHVBCmd)); msg->checkCode = static_cast(cc & 0xFF); - std::cout << (int)sizeof(msg_disHVBCmd) << endl; + // std::cout << (int)sizeof(msg_disHVBCmd) << endl; try { sendMsg(msgBuf,sizeof(msg_disHVBCmd));