Переглянути джерело

add mcu module & record heating state

xuqiang 4 місяців тому
батько
коміт
b4bea5f2e0
8 змінених файлів з 176 додано та 1 видалено
  1. 3 0
      include/appcontext.h
  2. 7 0
      include/logger.h
  3. 37 0
      include/mcumodule.h
  4. 5 0
      src/appcontext.cpp
  5. 26 1
      src/logger.cpp
  6. 2 0
      src/main.cpp
  7. 85 0
      src/mcumodule.cpp
  8. 11 0
      src/serialport.cpp

+ 3 - 0
include/appcontext.h

@@ -12,6 +12,7 @@
 #include "calprocess.h"
 #include "datadict.h"
 #include "sysstate.h"
+#include "mcumodule.h"
 
 class AppContext : public QObject
 {
@@ -32,6 +33,7 @@ public:
     CalProcess &calProcess();
     DataDict &dataDict();
     SysState &sysState();
+    MCUModule &mcuModule();
 
 private:
     PluginManager m_pluginMgr;
@@ -44,6 +46,7 @@ private:
     CalProcess m_calProcess;
     DataDict m_dataDict;
     SysState m_sysState;
+    MCUModule m_mcuModule;
 };
 
 #endif  // APPCONTEXT_H

+ 7 - 0
include/logger.h

@@ -17,6 +17,13 @@
 #define LOG_WARN(...)       SPDLOG_LOGGER_CALL(spdlog::default_logger().get(), spdlog::level::warn, __VA_ARGS__)
 #define LOG_CRITICAL(...)   SPDLOG_LOGGER_CALL(spdlog::default_logger().get(), spdlog::level::critical, __VA_ARGS__)
 
+#define CRITICAL_LOG(level, fmt, ...) \
+    do { \
+        if (auto logger = spdlog::get("critical"); logger) { \
+            logger->log(level, fmt, ##__VA_ARGS__); \
+        } \
+    } while(0)
+
 
 class Logger
 {

+ 37 - 0
include/mcumodule.h

@@ -0,0 +1,37 @@
+#ifndef __MCUMODULE_H__
+#define __MCUMODULE_H__
+
+#include <QObject>
+
+class MCUModule : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit MCUModule(QObject *parent = nullptr);
+
+    bool isHeating() const;
+    void setIsHeatng(const bool &newIsHeating);
+
+    int screenBrightness() const;
+    void setScreenBrightness(const int &newScreenBrightness);
+
+    int keyBrightness() const;
+    void setKeyBrightness(const int &newKeyBrightness);
+    
+    int screenTemp() const;
+    void setScreenTemp(const int &newScreenTemp);
+
+    int interfaceTemp() const;
+    void setInterfaceTemp(const int &newInterfaceTemp);
+
+private:
+    bool m_isHeating;       // 加热状态
+    int m_screenBrightness; // 屏幕亮度
+    int m_keyBrightness;    // 按键亮度
+    int m_screenTemp;       // 屏幕温度
+    int m_interfaceTemp;    // 接口板载温度
+
+};
+
+#endif  // MCUMODULE_H

+ 5 - 0
src/appcontext.cpp

@@ -76,3 +76,8 @@ SysState &AppContext::sysState()
 {
     return m_sysState;
 }
+
+MCUModule &AppContext::mcuModule()
+{
+    return m_mcuModule;
+}

+ 26 - 1
src/logger.cpp

@@ -1,10 +1,14 @@
 #include "logger.h"
 #include <spdlog/async.h>
 #include <spdlog/sinks/rotating_file_sink.h>
-#include "spdlog/sinks/stdout_color_sinks.h"
+#include <spdlog/sinks/stdout_color_sinks.h>
+#include <spdlog/sinks/basic_file_sink.h>
 
 #define MAX_ROTATING_FILE_SIZE  1048576 * 5
 #define MAX_ROTATING_FILES      3
+
+#define CRITICAL_MAX_ROTATING_FILE_SIZE 100 * 1024 * 1024
+#define CRITICAL_MAX_ROTATING_FILES 5
     
 Logger::Logger()
 {
@@ -49,4 +53,25 @@ void Logger::setup(LOG_LEVEL level, const char *path)
     logger->set_level(static_cast<spdlog::level::level_enum>(level)); // 允许所有日志通过 logger 层
     spdlog::register_logger(logger);
     spdlog::set_default_logger(logger);
+
+    // ---------------- 重要信息日志 ----------------
+    // 使用 basic_file_sink_mt 保证同步写入
+    std::string critical_log_path(path);
+    auto pos = critical_log_path.rfind('.');
+    if (pos != std::string::npos) {
+        critical_log_path = critical_log_path.substr(0, pos) + ".critical.log";
+    } else {
+        critical_log_path += ".critical.log"; // 没有扩展名,直接追加
+    }
+    // auto critical_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(critical_log_path, true); // true 表示 append
+    auto critical_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
+        critical_log_path, CRITICAL_MAX_ROTATING_FILE_SIZE, CRITICAL_MAX_ROTATING_FILES
+    );
+    critical_sink->set_level(spdlog::level::info); // 只记录 info 及以上
+    auto critical_logger = std::make_shared<spdlog::logger>("critical", critical_sink);
+    critical_logger->flush_on(spdlog::level::info); // 每条日志立即刷盘
+    critical_logger->set_level(spdlog::level::info);
+    critical_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
+
+    spdlog::register_logger(critical_logger);
 }

+ 2 - 0
src/main.cpp

@@ -23,6 +23,8 @@ int main(int argc, char *argv[])
     Logger::setup(Logger::TRACE, "storage/logs/log.txt");
 #endif
 
+    CRITICAL_LOG(spdlog::level::info, "app start...");
+
     char *ip = NULL;
     char *mask = NULL;
     bool result = Utils::ipv4("eth0", &ip, &mask);

+ 85 - 0
src/mcumodule.cpp

@@ -0,0 +1,85 @@
+#include "mcumodule.h"
+#include "logger.h"
+
+#include <QSettings>
+
+MCUModule::MCUModule(QObject *parent)
+    : QObject{parent}
+    , m_isHeating(false)
+    , m_screenBrightness(0)
+    , m_keyBrightness(0)
+    , m_screenTemp(0)
+    , m_interfaceTemp(0)
+{
+    QSettings settings("storage/conf/settings.ini", QSettings::IniFormat);
+    settings.setIniCodec("UTF-8");
+    settings.beginGroup("MCU");
+    if(settings.contains("SCREEN_BRIGHTNESS"))
+        m_screenBrightness = settings.value("SCREEN_BRIGHTNESS").toInt();
+    if(settings.contains("KEY_BRIGHTNESS"))
+        m_keyBrightness = settings.value("KEY_BRIGHTNESS").toInt();
+    settings.endGroup();
+}
+
+bool MCUModule::isHeating() const
+{
+    return m_isHeating;
+}
+
+void MCUModule::setIsHeatng(const bool &newIsHeating)
+{
+    if(newIsHeating != m_isHeating) {
+        m_isHeating = newIsHeating;
+        CRITICAL_LOG(spdlog::level::info, "heating state: {}", newIsHeating);
+    }
+}
+
+int MCUModule::screenBrightness() const
+{
+    return m_screenBrightness;
+}
+
+void MCUModule::setScreenBrightness(const int &newScreenBrightness)
+{
+    m_screenBrightness = newScreenBrightness;
+    QSettings settings("storage/conf/settings.ini", QSettings::IniFormat);
+    settings.setIniCodec("UTF-8");
+    settings.beginGroup("MCU");
+    settings.setValue("SCREEN_BRIGHTNESS", newScreenBrightness);
+    settings.endGroup();
+}
+
+int MCUModule::keyBrightness() const
+{
+    return m_keyBrightness;
+}
+
+void MCUModule::setKeyBrightness(const int &newKeyBrightness)
+{
+    m_keyBrightness = newKeyBrightness;
+    QSettings settings("storage/conf/settings.ini", QSettings::IniFormat);
+    settings.setIniCodec("UTF-8");
+    settings.beginGroup("MCU");
+    settings.setValue("KEY_BRIGHTNESS", newKeyBrightness);
+    settings.endGroup();
+}
+
+int MCUModule::screenTemp() const
+{
+    return m_screenTemp;
+}
+
+void MCUModule::setScreenTemp(const int &newScreenTemp)
+{
+    m_screenTemp = newScreenTemp;
+}
+
+int MCUModule::interfaceTemp() const
+{
+    return m_interfaceTemp;
+}
+
+void MCUModule::setInterfaceTemp(const int &newInterfaceTemp)
+{
+    m_interfaceTemp = newInterfaceTemp;
+}

+ 11 - 0
src/serialport.cpp

@@ -1,6 +1,7 @@
 #include "serialport.h"
 #include <QSerialPortInfo>
 #include "logger.h"
+#include "appcontext.h"
 
 #define SERIAL_PORT_DEVICE "/dev/ttyS9"
 
@@ -104,6 +105,16 @@ SerialPort::SerialPort(QObject *parent)
                             LOG_DEBUG("screen temperature           : {}", static_cast<int8_t>(buffer[6]));
                             LOG_DEBUG("Interface onboard temperature: {}", static_cast<int8_t>(buffer[7]));
                             ringbuffer_popup(&m_ringbuffer, size);
+
+                            MCUModule &mcuModule = AppContext::instance().mcuModule();
+
+                            if(buffer[5]) {
+                                mcuModule.setIsHeatng(true);
+                            } else {
+                                mcuModule.setIsHeatng(false);
+                            }
+                            mcuModule.setScreenTemp(static_cast<int8_t>(buffer[6]));
+                            mcuModule.setInterfaceTemp(static_cast<int8_t>(buffer[7]));
                         }
                     }
                 }