From abd91d0e88fae7f3b506eab718922e4707425a75 Mon Sep 17 00:00:00 2001 From: zhaojingkui <1553836110@qq.com> Date: Thu, 21 Nov 2024 18:13:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86UDP=E9=80=9A?= =?UTF-8?q?=E4=BF=A1=E4=BB=A3=E7=A0=81,=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=9A=84=E6=97=B6=E9=97=B4=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pPowerManger/PowerManger.cpp | 71 ++++++++++++--- src/pPowerManger/PowerManger.h | 8 +- src/pPowerManger/udpComm.cpp | 149 ++++++++++++++++++++++++++----- src/pPowerManger/udpComm.h | 32 +++++-- test/scripts/test.py | 2 +- test/scripts/udpmsg.py | 5 +- 6 files changed, 219 insertions(+), 48 deletions(-) diff --git a/src/pPowerManger/PowerManger.cpp b/src/pPowerManger/PowerManger.cpp index d2b51d7..7a9d47d 100644 --- a/src/pPowerManger/PowerManger.cpp +++ b/src/pPowerManger/PowerManger.cpp @@ -83,19 +83,16 @@ bool PowerManger::OnConnectToServer() bool PowerManger::Iterate() { AppCastingMOOSApp::Iterate(); - // Do your thing here! - if(!m_udpComm.m_qReceiveCcuStateBuffer.empty()) - { - m_CcuCurrentState = m_udpComm.m_qReceiveCcuStateBuffer.front(); - cout << m_CcuCurrentState.checkCode << endl; - m_udpComm.m_qReceiveCcuStateBuffer.pop(); - } + // 更新当前状态 + updatePmState(); + updateTime(); + //输出错误信息 if(!m_udpComm.sError.empty()) { for(int i=0;i SKEW_TOLERANCE) ? true : false; +} + +bool PowerManger::buildPowerSysReport() +{ + //TODO: 构建能源系统报告 + // string suffix; + // Json::Value json; + return true; } \ No newline at end of file diff --git a/src/pPowerManger/PowerManger.h b/src/pPowerManger/PowerManger.h index b96d533..9357292 100644 --- a/src/pPowerManger/PowerManger.h +++ b/src/pPowerManger/PowerManger.h @@ -23,6 +23,7 @@ #include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h" #include "MOOS/libMOOS/Utils/MOOSThread.h" #include "MOOS/libMOOS/Comms/XPCUdpSocket.h" +#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h" #include "ccuUdpMsg.h" #include "json/json.h" #include "udpComm.h" @@ -30,7 +31,7 @@ #define IPORT 5000 #define MAX_UDP_PKT_SIZE 65535 - +#define SKEW_TOLERANCE 5 class PowerManger : public AppCastingMOOSApp { public: @@ -52,8 +53,10 @@ class PowerManger : public AppCastingMOOSApp XPCUdpSocket* setUdpScoket(long lPort); bool updatePmState(); + bool updateTime(){m_current_time = MOOSTime();} + bool IsSkewed(double dfTimeNow, double * pdfSkew = NULL); - bool buildEquipmentReport(); + bool buildPowerSysReport(); bool buildDisributionReport(); bool buildSubDistributionReport(); bool buildPredictionReport(); @@ -73,6 +76,7 @@ class PowerManger : public AppCastingMOOSApp long m_lPort; unsigned int m_nReceiveBufferSizeKB; unsigned int m_nSendBufferSizeKB; + double m_current_time; // CMOOSThread m_SendThread; private: // State variables diff --git a/src/pPowerManger/udpComm.cpp b/src/pPowerManger/udpComm.cpp index 2c4db23..8e272dc 100644 --- a/src/pPowerManger/udpComm.cpp +++ b/src/pPowerManger/udpComm.cpp @@ -34,7 +34,6 @@ bool udpComm::getCcuState(char *m, msg_CcuStateFbMsg &s) { Checksum sum; msg_CcuStateFbMsg *p; p = reinterpret_cast(m); - h = p->header; // 判断消息是否有效 @@ -209,11 +208,15 @@ bool udpComm::pushMsgToQueue(unsigned char *m) switch (id) { case CCU_STATE_FB_ID: //CCU_STATE_FB 0x0051 - msg_CcuStateFbMsg s; + { msg_CcuStateFbMsg s; if(getCcuState((char *)Buff,s)) { if(m_qReceiveCcuStateBuffer.size()data = msg; + p->timeStamp = MOOSTime(); + return p; +} + +int udpComm::getMsgFormQueue(queue &q,double &time,void* data) +{ + if (!q.empty()) + { + if(q.front().data!= NULL) + { + data = q.front().data; + time = q.front().timeStamp; + q.pop(); + return 0; + } + else + return 2; + } + else + return 3; +} + +int udpComm::getCcuStateMsgFromQueue(double &time,msg_CcuStateFbMsg &m) +{ + void* p; + int ret; + ret = getMsgFormQueue(m_qReceiveCcuStateBuffer,time,p); + m = *reinterpret_cast(p); + return ret; +} + +int udpComm::getCcuSetParmMsgFromQueue(double &time,msg_CcuSetParmFbMsg &m) +{ + void* p; + int ret; + ret = getMsgFormQueue(m_qReceiveCcuSetParmBuffer,time,p); + m = *reinterpret_cast(p); + return ret; +} + +int udpComm::getDisSysHVBusFbMsgFromQueue(double &time,msg_disHighVolBusFbMsg &m) +{ + void* p; + int ret; + ret = getMsgFormQueue(m_qReceiveDisHighVolBusBuffer,time,p); + m = *reinterpret_cast(p); + return ret; +} + +int udpComm::getDisSysHVABusFbMsgFromQueue(double &time,msg_disHighAVolBusFbMsg &m) +{ + void* p; + int ret; + ret = getMsgFormQueue(m_qReceiveDisHighAVolBusBuffer,time,p); + m = *reinterpret_cast(p); + return ret; +} +int udpComm::getDisSysHVBBusFbMsgFromQueue(double &time,msg_disHighBVolBusFbMsg &m) +{ + void* p; + int ret; + ret = getMsgFormQueue(m_qReceiveDisHighBVolBusBuffer,time,p); + m = *reinterpret_cast(p); + return ret; +} +int udpComm::getDisSysLVBusFbMsgFromQueue(double &time,msg_disLowBusFbMsg &m) +{ + void* p; + int ret; + ret = getMsgFormQueue(m_qReceiveDisLowBusBuffer,time,p); + m = *reinterpret_cast(p); + return ret; } \ No newline at end of file diff --git a/src/pPowerManger/udpComm.h b/src/pPowerManger/udpComm.h index 94efd91..9cba343 100644 --- a/src/pPowerManger/udpComm.h +++ b/src/pPowerManger/udpComm.h @@ -5,8 +5,15 @@ #include #include #include - +#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h" +#include +using namespace std; #define QUENUE_SIZE 100 +typedef struct { + double timeStamp; + void* data; +} msgWithTime; + class udpComm { private: /* data */ @@ -15,8 +22,15 @@ public: ~udpComm(); bool pushMsgToQueue(unsigned char *m); + int getCcuStateMsgFromQueue(double &time,msg_CcuStateFbMsg &m); + int getCcuSetParmMsgFromQueue(double &time,msg_CcuSetParmFbMsg &m); + int getDisSysHVBusFbMsgFromQueue(double &time,msg_disHighVolBusFbMsg &m); + int getDisSysHVABusFbMsgFromQueue(double &time,msg_disHighAVolBusFbMsg &m); + int getDisSysHVBBusFbMsgFromQueue(double &time,msg_disHighBVolBusFbMsg &m); + int getDisSysLVBusFbMsgFromQueue(double &time,msg_disLowBusFbMsg &m); + bool clearMsgQueue(); - + bool getMsgId(char *m,unsigned short &id); bool getCcuState(char *m, msg_CcuStateFbMsg &s); @@ -30,15 +44,17 @@ public: bool sendCcuColCmd(const msg_CcuColCmdMsg m); bool sendCcuSetParmCmd(const msg_CcuSetParmMsg m); + msgWithTime* stampTimeStamp(void* msg); Checksum calculateChecksum(const unsigned char* data, int size); + int getMsgFormQueue(queue &q,double &time,void* m); std::vector sError; - std::queue m_qReceiveCcuStateBuffer; - std::queue m_qReceiveCcuSetParmBuffer; - std::queue m_qReceiveDisHighVolBusBuffer; - std::queue m_qReceiveDisHighAVolBusBuffer; - std::queue m_qReceiveDisHighBVolBusBuffer; - std::queue m_qReceiveDisLowBusBuffer; + std::queue m_qReceiveCcuStateBuffer; + std::queue m_qReceiveCcuSetParmBuffer; + std::queue m_qReceiveDisHighVolBusBuffer; + std::queue m_qReceiveDisHighAVolBusBuffer; + std::queue m_qReceiveDisHighBVolBusBuffer; + std::queue m_qReceiveDisLowBusBuffer; private: msg_CcuColCmdMsg m_ccuColCmd; diff --git a/test/scripts/test.py b/test/scripts/test.py index 8ab1ce8..20624e5 100644 --- a/test/scripts/test.py +++ b/test/scripts/test.py @@ -25,7 +25,7 @@ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # message = "Hello, this is a test message." message = valid_msg3 # 发送信息的次数 -num_messages = 10 +num_messages = 100 # 发送信息的时间间隔(秒) interval = 0.1 diff --git a/test/scripts/udpmsg.py b/test/scripts/udpmsg.py index 9ec31c9..7e33ec5 100644 --- a/test/scripts/udpmsg.py +++ b/test/scripts/udpmsg.py @@ -13,7 +13,7 @@ class msg_CcuStateFbMsg: self.totalPowerGeneration = 0x00 self.ch4o = 0x00 self.o2 = 0x00 - self.fcuVoltage = 0x00 + self.fcuVoltage = 0x1230 self.fcuCurrent = 0x00 self.o2Level = 0x00 self.placeholder1 = 0x00 @@ -83,9 +83,6 @@ class msg_CcuStateFbMsg: self.placeholder5 = 0x00 self.checkCode = 0x00 - - - def pack(self): # 初始化数据 self.length = 0x0000