Files
H100PowerManger/src/pPowerMangerHost/HostSim.h
T

62 lines
1.7 KiB
C++

/************************************************************/
/* NAME: */
/* ORGN: MIT */
/* FILE: HostSim.h */
/* DATE: */
/************************************************************/
#ifndef HOSTSIM_HEADER
#define HOSTSIM_HEADER
#include "MOOS/libMOOS/MOOSLib.h"
#include <string>
#include <map>
#include <vector>
#include <mutex>
#include <queue>
#include <atomic>
class WebServer;
// 模拟 pPowerManger 上位机:网页 <-> MOOSDB 桥
class HostSim : public CMOOSApp
{
public:
HostSim();
~HostSim();
protected: // Standard MOOSApp functions to overload
bool OnNewMail(MOOSMSG_LIST &NewMail);
bool Iterate();
bool OnConnectToServer();
bool OnStartUp();
void RegisterVariables();
public: // 由 WebServer 线程调用
// 网页下发的指令:key 为 MOOS 变量名,jsonValue 为已序列化的 JSON 字符串
void pushCommand(const std::string& key, const std::string& jsonValue);
// 取当前所有反馈变量的 JSON 快照
std::string buildSnapshot();
private:
void registerFeedbackVars();
bool drainCommands();
void handleWebMessage(const std::string& msg);
private: // State variables
// 反馈缓存:key -> 最新 JSON 字符串
std::mutex m_feedbackMutex;
std::map<std::string, std::string> m_feedback;
bool m_feedbackDirty;
// 待发送指令队列
std::mutex m_cmdMutex;
std::queue<std::pair<std::string, std::string> > m_cmdQueue;
// 网页服务器
WebServer* m_webServer;
int m_webPort;
};
#endif