From 96102eba8085a70dddd5f9de7a843af9211a9adf Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Fri, 12 Jul 2024 06:23:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=8B=E5=8A=A8=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E9=A3=8E=E6=89=87=E9=80=89=E9=A1=B9=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=8E=A7=E5=88=B6=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 ++++-- src/data_types.h | 55 ++++++++++++++++++++++++------------------ src/request_task.h | 11 +++++++++ src/widget/main_page.h | 18 +++++++++++--- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8069ce..bcdb05a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,11 +15,15 @@ add_subdirectory(third_party/cpp-httplib) set(ALL_FILES "") retrieve_files(${CMAKE_CURRENT_SOURCE_DIR}/src ALL_FILES) -add_executable(${PROJECT_NAME} ${ALL_FILES}) +if (WIN32) + add_executable(${PROJECT_NAME} WIN32 ${ALL_FILES}) +else () + add_executable(${PROJECT_NAME} ${ALL_FILES}) +endif () target_link_libraries(${PROJECT_NAME} imgui httplib) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) # 关闭windows控制台 -add_link_options(-mwindows) +add_link_options(${PROJECT_NAME} PRIVATE -mwindows) # 拷贝resources文件夹到build目录 file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/resources DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/src/data_types.h b/src/data_types.h index fbdaa97..328d865 100644 --- a/src/data_types.h +++ b/src/data_types.h @@ -23,11 +23,11 @@ enum class fan_status { }; struct fan_info { - int id; - int present; - fan_status status; - int speed_percent; - int speed_rpm; + int id = -1; + int present = -1; + fan_status status = fan_status::absent; + int speed_percent = -1; + int speed_rpm = -1; static fan_info from_json(const rapidjson::Value& obj) { fan_info out{ .id = obj["Id"].GetInt(), @@ -47,9 +47,18 @@ struct fan_info { } }; +struct fan_mode_info { + bool manual_mode= false; + static fan_mode_info from_json(const rapidjson::Value& obj) { + return { + .manual_mode = obj["fanMode"].GetInt() == 1 + }; + } +}; + struct running_time { - int days; - int hours; + int days = -1; + int hours = -1; static running_time from_json(const rapidjson::Value& obj) { return { .days = obj["Days"].GetInt(), @@ -59,22 +68,22 @@ struct running_time { }; struct psu_status { - int id; - int present; - int power_status; - int temperature; - int pwr_in_watts; - int input_power; - int input_volt; - int output_volt; - int input_current; - int output_current; - int err_status; - std::string fw_version; - int output_power_max; - std::string mfr_model; - std::string mfr_id; - std::string sn; + int id = -1; + int present = -1; + int power_status = -1; + int temperature = -1; + int pwr_in_watts = -1; + int input_power = -1; + int input_volt = -1; + int output_volt = -1; + int input_current = -1; + int output_current = -1; + int err_status = -1; + std::string fw_version = "unknown"; + int output_power_max = -1; + std::string mfr_model = "unknown"; + std::string mfr_id = "unknown"; + std::string sn = "unknown"; static psu_status from_json(const rapidjson::Value& obj) { return { diff --git a/src/request_task.h b/src/request_task.h index 6245d8b..96549ef 100644 --- a/src/request_task.h +++ b/src/request_task.h @@ -137,7 +137,18 @@ public: } }; +class set_fan_mode_task : public post_task { +public: + set_fan_mode_task() : post_task("setfanmode") {} + void setup(bool manual_mode) { + httplib::Params params; + params.emplace("MODE", std::to_string(manual_mode ? 1 : 0)); + set_params(params); + } +}; + SIMPLE_GET_TASK(get_fan_info_task, "getfaninfo", std::vector) SIMPLE_GET_TASK(get_running_time_task, "getrunningtime", running_time) SIMPLE_GET_TASK(get_psu_info_task, "getallpsuinfo", std::vector) +SIMPLE_GET_TASK(get_fan_mode, "getfanmode", fan_mode_info) SIMPLE_GET_TASK(logout_task, "WEBSES/logout", void) diff --git a/src/widget/main_page.h b/src/widget/main_page.h index e041d3b..8b208b1 100644 --- a/src/widget/main_page.h +++ b/src/widget/main_page.h @@ -4,6 +4,8 @@ #include "imgui.h" #include "request_task.h" +inline std::vector> g_tasks; + #define FUTURE_PARAM(task_type, name) \ inline std::future name##_future; \ inline task_type::result_type name##_; \ @@ -15,16 +17,22 @@ } else { \ name##_future = task_executor::run_task(); \ } \ - } + } \ + struct name##_task_register { \ + name##_task_register() { \ + g_tasks.emplace_back(update_##name); \ + } \ + } name##_task_register_instance; FUTURE_PARAM(get_fan_info_task, fan_infos) +FUTURE_PARAM(get_fan_mode, fan_mode) FUTURE_PARAM(get_running_time_task, running_time) FUTURE_PARAM(get_psu_info_task, psu_status) inline void update_infos() { - update_fan_infos(); - update_running_time(); - update_psu_status(); + for (const auto& task : g_tasks) { + task(); + } } inline void draw_running_time() { @@ -45,6 +53,8 @@ inline ImU32 get_fan_status_color(fan_status status) { } inline void draw_fan_infos() { + if (ImGui::Checkbox("Manual Mode", &fan_mode_.manual_mode)) + task_executor::run_task(fan_mode_.manual_mode); for (auto& fan_info : fan_infos_) { // 根据风扇状态绘制不同的颜色 和 启用/禁用输入框 ImGui::PushStyleColor(ImGuiCol_Text, get_fan_status_color(fan_info.status));