91 lines
2.6 KiB
C++
91 lines
2.6 KiB
C++
#ifndef HTTP_SERVER_H
|
|
#define HTTP_SERVER_H
|
|
#include "MOOS/libMOOS/Utils/MOOSThread.h"
|
|
#include "mongoose.h"
|
|
#include "../loguru.hpp"
|
|
#include "../systemData.h"
|
|
#include <vector>
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
#include <fstream>
|
|
#include <deque>
|
|
#include "../LowerCommManager.h"
|
|
#include <string>
|
|
#include <thread>
|
|
#include <atomic>
|
|
|
|
class HttpServer {
|
|
public:
|
|
// 构造函数,传入PowerManger指针和端口号
|
|
HttpServer(int port = 8000);
|
|
|
|
// 析构函数
|
|
~HttpServer();
|
|
|
|
// 启动HTTP服务器线程
|
|
void start();
|
|
|
|
// 停止HTTP服务器线程
|
|
void stop();
|
|
|
|
// 检查服务器是否正在运行
|
|
bool isRunning() const;
|
|
|
|
// 设置系统数据
|
|
void setSystemData(SystemData* systemData){
|
|
m_systemData = systemData;
|
|
}
|
|
void setLowerCommManager(LowerCommManager* lowerCommManager){
|
|
m_lowerCommManager = lowerCommManager;
|
|
}
|
|
void setPowerManger(PowerManger* powerManger){
|
|
m_powerManger = powerManger;
|
|
}
|
|
|
|
// 推送系统数据到所有WebSocket客户端
|
|
void pushSystemData();
|
|
|
|
// 监控日志文件变化并推送新内容
|
|
void monitorLogFile();
|
|
|
|
// 处理日志请求
|
|
void handleLogRequest(struct mg_connection* c, const std::string& msg);
|
|
|
|
// 读取日志文件
|
|
std::vector<std::string> readLogFile(int page, int pageSize, const std::string& level, const std::string& search);
|
|
|
|
// 测试页面请求处理函数
|
|
void handleTestPage(struct mg_connection* c, const std::string& msg);
|
|
|
|
private:
|
|
// WebSocket事件处理函数
|
|
void handleWebSocketEvent(struct mg_connection* c, int ev, void* ev_data, void* fn_data);
|
|
|
|
// 线程回调函数
|
|
static bool serverThreadCB(void* pParam);
|
|
bool serverThreadFunc();
|
|
|
|
// 后台系统数据
|
|
SystemData *m_systemData = nullptr;
|
|
LowerCommManager *m_lowerCommManager = nullptr;
|
|
PowerManger *m_powerManger = nullptr;
|
|
|
|
struct mg_mgr mgr_; // Mongoose管理器
|
|
int port_; // 监听端口
|
|
bool running_; // 运行状态标志
|
|
CMOOSThread m_ServerThread; // 服务器线程
|
|
std::mutex ws_connections_mutex_; // WebSocket连接锁
|
|
std::vector<mg_connection*> ws_connections_; // WebSocket连接列表
|
|
std::unordered_map<mg_connection*, uint64_t> ws_last_io_time_; // 连接最后活动时间
|
|
|
|
// 日志文件相关
|
|
std::string m_logFilePath = "pPowerManger.log";
|
|
std::ifstream m_logFile;
|
|
std::streampos m_lastLogPosition = 0;
|
|
std::deque<std::string> m_logCache; // 缓存最近的日志
|
|
static const size_t MAX_CACHE_SIZE = 1000; // 最大缓存条数
|
|
|
|
};
|
|
|
|
#endif // HTTP_SERVER_H
|