Files
H100PowerManger/src/appPowerMangerWebUI/main.cpp
T

149 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <string>
#include "crow.h"
#include <iostream>
#include "json/json.h"
#include <sys/inotify.h>
#include <unistd.h>
#include <dirent.h>
#include <fstream>
#include <vector>
#include <thread>
#include <mutex>
#include "../pPowerManger/SQLiteDB.h"
#include "../pPowerManger/pmSysvariable.h"
#include <MOOS/libMOOS/Comms/MOOSAsyncCommClient.h>
using namespace std;
// 定义一个全局变量,用于存储状态信息
ccuState ccustate;
StateData stateData;
int main() {
crow::SimpleApp app;
SQLiteDB db("/home/zjk/project/H100/H100PowerManger/data/power_data.db");
MOOS::MOOSAsyncCommClient moosClient;
moosClient.Run("localhost", 9000, "appPowerMangerWebUI");
std::cout << " Current Path -> " << std::system("pwd") << std::endl;
// 定义路由,处理根路径的请求
CROW_ROUTE(app, "/")([]() {
auto page = crow::mustache::load_text("index.html");
return page;
});
// 定义路由,处理 /api/power-data 的请求
CROW_ROUTE(app, "/api/db-status")
([&db]() {
crow::json::wvalue response;
if (db.isDatabaseConnected("/home/zjk/project/H100/H100PowerManger/data", "power_data.db")) {
response["connected"] = true;
response["database"] = db.getDBPatch();
} else {
response["connected"] = false;
}
return response;
});
CROW_ROUTE(app, "/api/state")
([&db]() {
crow::json::wvalue response;
string time;
db.getStateData(stateData,time);//状态机的状态
response["state_id"] = stateData.state_id;
response["state_name"] = stateData.state_name;
response["event"] = stateData.event;
response["event_id"] = stateData.event_id;
response["fault_code"] = stateData.fault_code;
response["note"] = stateData.note;
response["time"] = time;
return response;
});
// 定义路由,处理 /api/power-data 的请求
CROW_ROUTE(app, "/api/download-db")
([&db]() {
std::ifstream file("/home/zjk/project/H100/H100PowerManger/src/appPowerMangerWebUI/index.html", std::ios::binary);
if (!file) {
return crow::response(404, "Database file not found");
}
// 获取文件大小
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// 创建响应对象
crow::response response;
response.code = 200;
response.set_header("Content-Type", "application/octet-stream");
response.set_header("Content-Disposition", "attachment; filename=power_data.db");
response.set_header("Content-Length", std::to_string(fileSize));
// 直接返回文件流
response.body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return response;
});
// 定义路由,处理 /api/moos-db 的请求
CROW_ROUTE(app, "/api/moos-db")
([&moosClient]() -> crow::json::wvalue {
crow::json::wvalue response;
if (moosClient.IsConnected()) {
response["connected"] = true;
response["host"] = moosClient.GetDBHostname();
response["port"] = std::to_string(moosClient.GetDBHostPort());
}
else {
response["connected"] = false;
}
return response;
});
// 定义路由,处理 /api/moos-notify 的请求(POST)
CROW_ROUTE(app, "/api/moos-notify").methods("POST"_method)
([&moosClient](const crow::request& req) {
auto json = crow::json::load(req.body);
if (!json) {
return crow::response(400, "Invalid JSON");
}
std::string var = json["var"].s();
std::string key = json["key"].s();
// 发送MOOS消息
bool notifyResult = moosClient.Notify(var, key);
std::cout << "MOOS Notify: var=" << var << ", key=" << key
<< ", result=" << (notifyResult ? "success" : "failed") << std::endl;
crow::response res;
if (notifyResult)
{
res.body = crow::json::wvalue({
{"success", true},
{"message", key + ":" + var}
}).dump();
}
else
{
res.body = crow::json::wvalue({
{"success", false},
{"message", key + ":" + var},
}).dump();
}
res.set_header("Content-Type", "application/json");
return res;
});
// 启动服务器,监听端口 18080 并启动多线程
std::cout << "Server started on port 18080" << std::endl;
// app.port(18080).multithreaded().run();
app.port(18080)
.multithreaded()
.concurrency(16) // 增加并发线程数
.run();
return 0;
}