Browse Source

网关备份

xhj 2 tháng trước cách đây
mục cha
commit
a65ec7b083

+ 42 - 1
modules/gateway-scheduler/src/tcp_server/TcpServer.cpp

@@ -202,7 +202,7 @@ void TcpServer::handle_client(int client_socket) {
             // 湿度对象
             nlohmann::json humidity_data;
             humidity_data["name"] = "湿度";
-            humidity_data["value"] = ((buffer[9] << 9) + buffer[10]);
+            humidity_data["value"] = ((buffer[9] << 8) + buffer[10]);
             humidity_data["format"] = "%RH";
             humidity_data["format_id"] = 40;
             json_save["data"].push_back(humidity_data);
@@ -232,6 +232,47 @@ void TcpServer::handle_client(int client_socket) {
             smoke_data["format_id"] = 0;
             json_save["data"].push_back(smoke_data);
         }
+//        else if (dev_type == 7) { // 烟感
+//          device_name = "空调-" + std::to_string(dev_addr);
+//          json_save["device_name"] = "空调-" + std::to_string(dev_addr);
+//          json_save["device_id"] = dev_addr;
+//
+//          // 开关对象
+//          nlohmann::json switch_data;
+//          switch_data["name"] = "开关";
+//          switch_data["value"] = ((buffer[7] << 8) + buffer[8]);
+//          switch_data["format"] = "";
+//          switch_data["format_id"] = 0;
+//          json_save["data"].push_back(switch_data);
+//          // 模式对象
+//          nlohmann::json mode_data;
+//          mode_data["name"] = "模式";
+//          mode_data["value"] = ((buffer[9] << 8) + buffer[10]);
+//          mode_data["format"] = "";
+//          mode_data["format_id"] = 0;
+//          json_save["data"].push_back(mode_data);
+//          // 模式对象
+//          nlohmann::json set_temperature_data;
+//          set_temperature_data["name"] = "设定温度";
+//          set_temperature_data["value"] = ((buffer[11] << 8) + buffer[12]);
+//          set_temperature_data["format"] = "℃";
+//          set_temperature_data["format_id"] = 40;
+//          json_save["data"].push_back(set_temperature_data);
+//          // 模式对象
+//          nlohmann::json wind_speed_data;
+//          wind_speed_data["name"] = "风速";
+//          wind_speed_data["value"] = ((buffer[13] << 8) + buffer[14]);
+//          wind_speed_data["format"] = "m/s";
+//          wind_speed_data["format_id"] = 40;
+//          json_save["data"].push_back(wind_speed_data);
+//          // 模式对象
+//          nlohmann::json wind_direction_data;
+//          wind_direction_data["name"] = "风向";
+//          wind_direction_data["value"] = ((buffer[15] << 8) + buffer[16]);
+//          wind_direction_data["format"] = "";
+//          wind_direction_data["format_id"] = 0;
+//          json_save["data"].push_back(wind_direction_data);
+//        }
 
         // 创建 Redis 连接
         redisContext *context = redisConnect("127.0.0.1", 6379);

+ 66 - 1
modules/gateway-server/src/large_screen_display/devices_info.cpp

@@ -15,7 +15,64 @@ std::vector<Device> createDeviceInfo() {
           {"水浸-4", {{ "水浸状态", "正常" }}},
           {"温湿度-4", {{ "温度", "28℃" }, { "湿度", "56%" }}},
           {"水浸-5", {{ "水浸状态", "正常" }}},
-          {"温湿度-5", {{ "温度", "28℃" }, { "湿度", "56%" }}}
+          {"温湿度-5", {{ "温度", "28℃" }, { "湿度", "56%" }}},
+          {"空调-1",{
+                  {"开关", "0"},
+                  {"模式", "0"},
+                  {"设定温度", "30"},
+                  {"风速", "40"},
+                  {"风向", "0"}}
+      },
+  };
+}
+
+std::vector<AllowDevice> allowDevice() {
+  return {
+      {"水浸-1"},
+      {"温湿度-1"},
+      {"水浸-2"},
+      {"温湿度-2"},
+      {"水浸-3"},
+      {"温湿度-3"},
+      {"水浸-4"},
+      {"温湿度-4"},
+      {"水浸-5"},
+      {"温湿度-5"},
+      {"空调-1"},
+  };
+}
+
+std::vector<Device> control_devices_status() {
+  return {
+//      {"空调-1", {{ "水浸状态", "正常" }}},
+//      {"空调-2", {{ "水浸状态", "正常" }}},
+//      {"温湿度-2", {{ "温度", "28℃" }, { "湿度", "56%" }}},
+//      {"水浸-3", {{ "水浸状态", "正常" }}},
+//      {"温湿度-3", {{ "温度", "28℃" }, { "湿度", "56%" }}},
+//      {"水浸-4", {{ "水浸状态", "正常" }}},
+//      {"温湿度-4", {{ "温度", "28℃" }, { "湿度", "56%" }}},
+//      {"水浸-5", {{ "水浸状态", "正常" }}},
+//      {"温湿度-5", {{ "温度", "28℃" }, { "湿度", "56%" }}},
+        {"空调-1",{
+                       {"开关", "0"},
+                       {"模式", "0"},
+                       {"温度", "30"},
+                       {"风速", "强风"},
+                       {"风向", "0"}}
+        },
+        {"空调-2",{
+                       {"开关", "1"},
+                       {"模式", "2"},
+                       {"设定温度", "20"},
+                       {"风速", "弱风"},
+                       {"风向", "2"}}
+        },
+        {"门禁控制器-1",{{"开关", "1"}},},
+        {"门禁控制器-2",{{"开关", "1"}},},
+        {"照明控制器-1",{{"开关", "0"}},},
+        {"照明控制器-2",{{"开关", "1"}},},
+        {"风机控制器-1",{{"开关", "0"}},},
+        {"风机控制器-2",{{"开关", "1"}},},
   };
 }
 
@@ -63,6 +120,14 @@ std::vector<DeviceHistory> createDeviceHistoryData() {
 }
 
 
+json allowDeviceInfoToJson(const std::vector<AllowDevice>& devices) {
+  json j = json::array();
+  for (const auto& device : devices) {
+    j.push_back(device.device_name);
+  }
+  return j;
+}
+
 // 将设备信息转换为 JSON 格式
 json deviceInfoToJson(const std::vector<Device>& devices) {
   json j = json::array();

+ 6 - 3
modules/gateway-server/src/large_screen_display/devices_info.h

@@ -22,7 +22,9 @@ struct Device {
   std::string device_name;
   std::vector<Measure> measures;
 };
-
+struct AllowDevice {
+  std::string device_name;
+};
 
 // 定义 HistoryItem 结构体
 struct HistoryItem {
@@ -44,10 +46,11 @@ struct DeviceHistory {
 
 
 
-
+std::vector<Device> control_devices_status();
 json deviceInfoToJson(const std::vector<Device>& devices);
 std::vector<Device> createDeviceInfo();
-
+std::vector<AllowDevice> allowDevice();
 std::vector<DeviceHistory> createDeviceHistoryData();
 json deviceHistoryToJson(const std::vector<DeviceHistory>& devices_history);
+json allowDeviceInfoToJson(const std::vector<AllowDevice>& devices);
 #endif //DEVICES_INFO_H

+ 4 - 1
modules/gateway-server/src/server/BasicRouter.cpp

@@ -1,7 +1,8 @@
 #include "BasicRouter.h"
 
 #include <string>
-
+#include <iostream>
+#include <stdio.h>
 #include "../controller/CommunicationController.h"
 #include "../controller/DashboardDeviceController.h"
 #include "../controller/DashboardDeviceHistoryController.h"
@@ -64,6 +65,8 @@ void* BasicRouter::fetch_controller(BasicRoute& route) { /**/
 
 // TODO need to add more controllers
 void* BasicRouter::create_controller(std::string preifx) { /**/
+//  std::cout << "create_controller " << preifx << std::endl;
+
   if (preifx == "user") {
     return new controller::UserController();
   }

+ 63 - 4
modules/gateway-server/src/server/BasicServer.cpp

@@ -53,6 +53,32 @@ BasicRoute generate_route(std::string path, std::string method) { /**/
 } /**/
 
 MHD_Result BasicServer::requestHandler(void* cls, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** req_cls) { /**/
+  // 打印 cls
+//  std::cout << "cls: " << cls << std::endl;
+//
+//  // 打印 connection(指针地址)
+//  std::cout << "connection: " << connection << std::endl;
+//
+//  // 打印 URL
+//  std::cout << "url: " << (url ? url : "<null>") << std::endl;
+//
+//  // 打印 HTTP 方法
+//  std::cout << "method: " << (method ? method : "<null>") << std::endl;
+//
+//  // 打印 HTTP 版本
+//  std::cout << "version: " << (version ? version : "<null>") << std::endl;
+//
+//  // 打印 upload_data 和 upload_data_size
+//  if (upload_data && *upload_data_size > 0) {
+//    std::cout << "upload_data: " << std::string(upload_data, *upload_data_size) << std::endl;
+//  } else {
+//    std::cout << "upload_data: <empty or null>" << std::endl;
+//  }
+//  std::cout << "upload_data_size: " << *upload_data_size << std::endl;
+//
+//  // 打印 req_cls
+//  std::cout << "req_cls: " << *req_cls << std::endl;
+
   if (*req_cls == NULL) {
     *req_cls = new BasicRequest();
     return MHD_YES;
@@ -85,14 +111,14 @@ MHD_Result BasicServer::requestHandler(void* cls, struct MHD_Connection* connect
     std::string url_string(url);
     std::cout << "url " << url << std::endl;
 
-    if (url_string.rfind("/api/largeScreenDisplay/", 0) == 0) { // 确保从字符串的开始处匹配
+    if (url_string.rfind("/api/largeScreenDisplay/", 0) == 0) { // 确保从字符串的开始处匹配  http://localhost:8000/api/largeScreenDisplay/devices_info
       if (url_string.find("/video") != std::string::npos) {
-        std::cout << "largeScreenDisplay video" << std::endl;
+//        std::cout << "largeScreenDisplay video" << std::endl;
       } else if (url_string.find("/history_data") != std::string::npos) {
         std::cout << "largeScreenDisplay history_data" << std::endl;
         auto devices_history = createDeviceHistoryData();
         Json response_json = deviceHistoryToJson(devices_history);
-        std::cout << response_json.dump(4) << std::endl;
+//        std::cout << response_json.dump(4) << std::endl;
         response = response_json.dump();
       } else if (url_string.find("/devices_info") != std::string::npos) {
         std::cout << "largeScreenDisplay devices_info" << std::endl;
@@ -101,7 +127,40 @@ MHD_Result BasicServer::requestHandler(void* cls, struct MHD_Connection* connect
         // std::cout << response_json.dump() << std::endl;
         response = response_json.dump();
       }
-    }else {
+    }else if (url_string.find("/control-devices-status") != std::string::npos) {
+      std::cout << "largeScreenDisplay control-devices-status" << std::endl;
+      auto devices = control_devices_status();
+      Json response_json = deviceInfoToJson(devices);
+      std::cout << response_json.dump() << std::endl;
+      response = response_json.dump();
+    }else if (url_string.find("/allow-device") != std::string::npos) {
+      std::cout << "largeScreenDisplay allow-device" << std::endl;
+      auto devices = allowDevice();
+      Json response_json = allowDeviceInfoToJson(devices);
+      std::cout << response_json.dump() << std::endl;
+      response = response_json.dump();
+    }else if (url_string.find("/add-allow-device") != std::string::npos) {
+
+//      static std::string post_data; // 存储请求体数据
+//      std::cout << post_data << std::endl;
+//
+//      if (*upload_data_size != 0) {
+//        // 拼接上传的数据块
+//        post_data.append(upload_data, *upload_data_size);
+//        *upload_data_size = 0; // 告诉 MicroHTTPD 数据已处理
+//        return MHD_YES; // 继续接收数据
+//      }
+//
+//      if (!post_data.empty()) {
+//        json request_body = json::parse(post_data);
+//        std::string sensorName = request_body["sensorName"];
+//        std::cout << "Received sensorName: " << sensorName << std::endl;
+//
+//        const char* response = "{\"message\":\"添加成功\"}";
+//        post_data.clear();
+//      }
+    }
+    else {
       BasicRoute route = generate_route(url, method);
       route.request = (BasicRequest*)*req_cls;
       BasicRouter router;