zhunk 8 meses atrás
pai
commit
d6169beb9b

+ 54 - 0
Application_cbd_apm32f1/applications/ cpu_usage_cmd.c

@@ -0,0 +1,54 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2020-12-04     tyx          first implementation
+ * 2021-01-17     Meco Man     beautify cmd
+ */
+#include <stdlib.h>
+#include "cpu_usage.h"
+
+static volatile rt_thread_t tid;
+
+static void load_thread(void *param)
+{
+    while (tid != RT_NULL && (int)tid->user_data)
+    {
+        while ((int)tid->user_data >= rt_tick_get() % 100);
+        rt_thread_delay(1);
+    }
+    tid = RT_NULL;
+}
+
+void cpu_usage_msh(int argc, char *argv[])
+{
+    if (argc == 3 && rt_strcmp(argv[1], "-l") == 0)
+    {
+        if (tid)
+        {
+            tid->user_data = strtoul(argv[2], RT_NULL, 10);
+        }
+        else
+        {
+            tid = rt_thread_create("usage", load_thread, RT_NULL, 256,
+                RT_THREAD_PRIORITY_MAX - 2, 20);
+            if (tid == RT_NULL)
+            {
+                rt_kprintf("create thread failed.");
+            }
+            tid->user_data = strtoul(argv[2], RT_NULL, 10);
+            rt_thread_startup(tid);
+        }
+        rt_kprintf("CPU usage adjusted to %d%c\n", tid->user_data, '%');
+    }
+    else if (argc == 1)
+    {
+        rt_kprintf("cpu: %d\n", (int)cpu_load_average());
+    }
+    else
+    {
+        rt_kprintf("%s\n", argv[0]);
+    }
+}
+MSH_CMD_EXPORT_ALIAS(cpu_usage_msh, usage, cpu usage);

+ 79 - 18
Application_cbd_apm32f1/applications/control.c

@@ -23,6 +23,7 @@
 #define DBG_TAG "control"
 #define DBG_LEVEL         DBG_LOG
 #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
+#include "sys_monitor.h"
 //工作检测
 #define WORK_CHECK_THREAD_PRIORITY        23      //线程优先级
 #define WORK_CHECK_THREAD_STACK_SIZE      1024    //栈大小
@@ -35,6 +36,8 @@
 #define PWM_EN    1   //0关闭,1-打开
 //设备状态表结构体
 Device_Status_typedef device_status;
+System_stats_t system_status;
+
 
 rt_thread_t tid_workloop = RT_NULL;
 rt_thread_t tid_wkstc = RT_NULL;
@@ -217,12 +220,38 @@ uint8_t In_Rain_status(void)
     rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
     return rt_pin_read(RAIN_PIN);
 }
+
 /*加热*/
 uint8_t ctr_Heat(uint8_t status_heat)
 {
+    rt_bool_t is_currently_on;
+    rt_tick_t now;
+    now = rt_tick_get();
+    is_currently_on = (system_status.heat_start_time != 0);
     device_status.heat_status = status_heat;
     rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
     rt_pin_write(Heat_Pin,status_heat);
+
+    if(status_heat==ON){
+        // 已经开着 → 不重复计时
+        if (is_currently_on) {
+            return ON;
+        }
+
+        // 关 → 开,记录起始时间
+        system_status.heat_start_time = now;
+    }else{
+        // 已经关着 → 不重复结算
+        if (!is_currently_on) {
+           return OFF;
+        }
+        // 开 → 关,结算运行时间
+        system_status.heater_running_time +=
+            (now - system_status.heat_start_time);
+
+        system_status.heat_start_time = 0;
+    }
+
     return status_heat;
 }
 /*制冷*/
@@ -381,50 +410,80 @@ uint8_t ctr_cangmen_down(uint8_t status_down)
 }
 
 //镇流器
-uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
+//镇流器
+uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
 {
     rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
     rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
     rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
-    if(status_lamp==ON)
+
+    rt_bool_t is_currently_on;
+    rt_tick_t now;
+
+    now = rt_tick_get();
+    is_currently_on = (system_status.lamp_start_time != 0);
+
+    if (status_lamp == ON)
     {
-        switch(lamp_num){
+        // ====== 开灯 ======
+        switch (lamp_num) {
         case 1:
-            rt_pin_write(LAMP_SW,ON);
-            device_status.lamp_status = status_lamp;
+            rt_pin_write(LAMP_SW, ON);
+            device_status.lamp_status = ON;
             break;
         case 2:
-            rt_pin_write(LAMP_1_SW,ON);
-            device_status.lamp_status = status_lamp;
+            rt_pin_write(LAMP_1_SW, ON);
+            device_status.lamp1_status = ON;
             break;
         case 3:
-            rt_pin_write(LAMP_2_SW,ON);
-            device_status.lamp_status = status_lamp;
+            rt_pin_write(LAMP_2_SW, ON);
+            device_status.lamp2_status = ON;
             break;
         default:
             break;
         }
 
+        // 已经开着 → 不重复计时
+        if (is_currently_on) {
+            return ON;
+        }
+
+        // 关 → 开,记录起始时间
+        system_status.lamp_start_time = now;
     }
-    else {
-        switch(lamp_num){
+    else
+    {
+        // ====== 关灯 ======
+        switch (lamp_num) {
         case 1:
-            rt_pin_write(LAMP_SW,OFF);
-            device_status.lamp_status = status_lamp;
+            rt_pin_write(LAMP_SW, OFF);
+            device_status.lamp_status = OFF;
             break;
         case 2:
-            rt_pin_write(LAMP_1_SW,OFF);
-            device_status.lamp1_status = status_lamp;
+            rt_pin_write(LAMP_1_SW, OFF);
+            device_status.lamp1_status = OFF;
             break;
         case 3:
-            rt_pin_write(LAMP_2_SW,OFF);
-            device_status.lamp2_status = status_lamp;
+            rt_pin_write(LAMP_2_SW, OFF);
+            device_status.lamp2_status = OFF;
             break;
         default:
             break;
         }
+
+        // 已经关着 → 不重复结算
+        if (!is_currently_on) {
+            return OFF;
+        }
+
+        // 开 → 关,结算运行时间
+        system_status.lamp_running_time +=
+            (now - system_status.lamp_start_time);
+
+        system_status.lamp_start_time = 0;
     }
-    HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
+
+    HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
     return device_status.lamp_status;
 }
 
@@ -450,6 +509,7 @@ uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
         }
         rt_pin_write(SHAKE_SW,OFF);
         HMI_WRITE("cangWei.switchShake.val=%d",0);
+        system_status.vibration_count += 1;
     }
     else
     {
@@ -1866,6 +1926,7 @@ static void Control_Clear_thread_entry(void *parameter)
             }else {
                 ctr_TG_Clear(350);
             }
+            system_status.cleaning_count +=1;   //清扫次数
         }
         if(device_status.clear_status==2)
         {

+ 5 - 5
Application_cbd_apm32f1/applications/control.h

@@ -17,7 +17,7 @@
 
 
 #define LAMP_EN          0             //李新3灯管:0关闭,1打开
-#define PCB_V1           1             //1-旧PCB,0-新PCB(模块化)
+#define PCB_V1           0             //1-旧PCB,0-新PCB(模块化)
 #define MATCH_DEVICE     0             //比赛设备
 #define SENSOR_EN        0             //温湿度:0关闭,1打开
 #define UART_TEST_EN     0             ///串口测试0-关闭,1-启用
@@ -36,16 +36,16 @@
     #define VERSION_HK          "1.(3ledV9).41(HK)"      //4.0海康-三色光源
     #define VERSION_HK_2000W    "1.3led.40(2000W)"       //水稻
 #else
-    #define VERSION_HK          "2.6.41(HK)"            //4.0海康-1200W
+    #define VERSION_HK          "2.35.41(HK)"            //4.0海康-1200W
     #if TG_MODULE == 0
-    #define VERSION_HK_2000W    "2.34.4(2000W)"         //4.0水稻
+    #define VERSION_HK_2000W    "2.35.4(2000W)"         //4.0水稻
     #else
     #define VERSION_HK_2000W    "2.6.4(TG)"             //推杆结构
     #endif
 #endif
 
-#define VERSION_ZP          "2.29.1(ZP)"            //1.0转盘
-#define VERSION_GK          "2.29.1(GK)"            //1.0高空
+#define VERSION_ZP          "2.35.1(ZP)"            //1.0转盘
+#define VERSION_GK          "2.35.1(GK)"            //1.0高空
 
 extern rt_event_t event_485_play;
 

+ 135 - 0
Application_cbd_apm32f1/applications/cpu_usage.c

@@ -0,0 +1,135 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2020-12-04     tyx          first implementation
+ */
+
+#include "cpu_usage.h"
+
+#ifndef CPU_USAGE_PERIOD_TICK
+#define CPU_USAGE_PERIOD_TICK RT_TICK_PER_SECOND
+#endif
+
+cpu_usage_t *cpu_usage_obj(void)
+{
+    static struct cpu_usage _usage;
+    return &_usage;
+}
+
+static void timeout(void *param)
+{
+    cpu_usage_t *obj = param;
+
+    if (obj->state == CPU_USAGE_STATE_ACTIVATED)
+    {
+        if (++obj->period == CPU_USAGE_PERIOD_TICK)
+        {
+            obj->period = 0;
+            obj->idle_stat[0].load = CPU_USAGE_PERIOD_TICK -
+                (obj->idle_stat[0].idle_tick - obj->idle_stat[0].last_tick);
+            obj->idle_stat[0].last_tick = obj->idle_stat[0].idle_tick;
+        }
+
+        if (rt_thread_self() == obj->idle_stat[0].tid)
+        {
+            obj->idle_stat[0].idle_tick++;
+        }
+    }
+}
+
+int cpu_usage_init(void)
+{
+    cpu_usage_t *obj = cpu_usage_obj();
+    rt_timer_t t = &obj->time;
+    char idle_name[RT_NAME_MAX];
+    int i;
+
+    if (rt_object_get_type(&t->parent) != RT_Object_Class_Timer)
+    {
+        /* init time */
+        rt_timer_init(t, "usage", timeout, obj, 1,
+            RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER);
+        /* set cpus */
+        obj->cpus = sizeof(obj->idle_stat) / sizeof(obj->idle_stat[0]);
+        /* get idle thread handle */
+        for (i = 0; i < obj->cpus; i++)
+        {
+            rt_snprintf(idle_name, sizeof(idle_name), "tidle%d", i);
+            obj->idle_stat[i].tid = rt_thread_find(idle_name);
+        }
+        /* set flags */
+        obj->state = CPU_USAGE_STATE_ACTIVATED;
+        /* start */
+        rt_timer_start(t);
+    }
+    return 0;
+}
+
+void cpu_usage_deinit(void)
+{
+    cpu_usage_t *obj = cpu_usage_obj();
+    rt_timer_t t = &obj->time;
+
+    if (rt_object_get_type(&t->parent) == RT_Object_Class_Timer)
+    {
+        rt_timer_stop(t);
+        rt_timer_detach(t);
+        rt_memset(obj, 0, sizeof(*obj));
+    }
+}
+
+void cpu_usage_suspend(void)
+{
+    cpu_usage_t *obj = cpu_usage_obj();
+    rt_timer_t t = &obj->time;
+
+    if (obj->state == CPU_USAGE_STATE_ACTIVATED)
+    {
+        rt_timer_stop(t);
+        obj->state = CPU_USAGE_STATE_SUSPEND;
+        obj->suspend_tick = rt_tick_get();
+    }
+}
+
+void cpu_usage_resume(void)
+{
+    cpu_usage_t *obj = cpu_usage_obj();
+    rt_timer_t t = &obj->time;
+    rt_tick_t tick;
+    int i;
+
+    if (obj->state == CPU_USAGE_STATE_SUSPEND)
+    {
+        tick = rt_tick_get() - obj->suspend_tick;
+        for (i = 0; i < obj->cpus; i++)
+        {
+            obj->idle_stat[i].idle_tick += tick;
+        }
+        obj->state = CPU_USAGE_STATE_ACTIVATED;
+        rt_timer_start(t);
+    }
+}
+
+float cpu_load_average(void)
+{
+    cpu_usage_t *obj = cpu_usage_obj();
+    rt_tick_t usage = 0;
+    float load = 0.0;
+    int i;
+
+    if (obj->state == CPU_USAGE_STATE_ACTIVATED)
+    {
+        for (i = 0; i < obj->cpus; i++)
+        {
+            usage += obj->idle_stat[i].load;
+        }
+        load = 100.0 * usage / (CPU_USAGE_PERIOD_TICK * obj->cpus);
+    }
+    return load;
+}
+
+#ifdef RT_USING_COMPONENTS_INIT
+INIT_APP_EXPORT(cpu_usage_init);
+#endif

+ 52 - 0
Application_cbd_apm32f1/applications/cpu_usage.h

@@ -0,0 +1,52 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2020-12-04     tyx          first implementation
+ */
+
+#ifndef __CPU_USAGE_H__
+#define __CPU_USAGE_H__
+
+#include "rtthread.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define CPU_USAGE_STATE_DEACTIVATED  (0x0)
+#define CPU_USAGE_STATE_ACTIVATED    (0x1)
+#define CPU_USAGE_STATE_SUSPEND      (0x2)
+
+struct cpu_usage
+{
+    struct rt_timer time;           /* Period timer */
+    rt_uint8_t state;               /* Running state */
+    rt_uint8_t cpus;                /* Number of CPUs */
+    rt_uint16_t reserved;
+    rt_tick_t suspend_tick;         /* CPU suspend timestamp */
+    rt_tick_t period;               /* Stat Period */
+    struct _idle_stat
+    {
+        rt_tick_t idle_tick;
+        rt_tick_t last_tick;
+        rt_tick_t load;
+        rt_thread_t tid;
+    }
+    idle_stat[1];
+};
+typedef struct cpu_usage cpu_usage_t;
+
+int cpu_usage_init(void);
+void cpu_usage_deinit(void);
+cpu_usage_t *cpu_usage_obj(void);
+void cpu_usage_suspend(void);
+void cpu_usage_resume(void);
+float cpu_load_average(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 161 - 19
Application_cbd_apm32f1/applications/dtu_uart2.c

@@ -31,6 +31,8 @@
 #define DBG_LEVEL   DBG_LOG
 #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
 #include <lwjson.h>
+#include "sys_monitor.h"
+
 
 #define DTU_THREAD_PRIORITY         6          //线程优先级
 #define DTU_THREAD_STACK_SIZE       4096        //线程栈空间大小
@@ -38,6 +40,7 @@
 
 
 Device_Status_typedef device_status;
+System_stats_t system_status;
 DataTemp data_arry;
 
 void list_mem(void);
@@ -138,6 +141,7 @@ void DTU_Write(char *fmt, ...)
     if (str_length > 1024 - 1)
         str_length = 1024 - 1;
     rt_device_write(dtuserial, 0, dtu_str, str_length);
+    system_status.uart2_tx_bytes +=str_length;
     LOG_D("DTU_Write:%s",dtu_str);
 }
 /* 接收数据回调函数 */
@@ -1129,6 +1133,114 @@ void recover_param_from_rtu(void)
     }
 }
 
+void upload_sys_monitor_info(void)
+{
+    char value_str[32];
+    cJSON* proot = cJSON_CreateObject();
+    if (proot == NULL) {
+        LOG_E("Error: Failed to create JSON object\n");
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "cpu_tempture", system_status.cpu_tempture)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "cpu_usage", system_status.cpu_usage)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "cpu_usage_max", system_status.cpu_usage_max)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    rt_snprintf(value_str, sizeof(value_str), "%dh:%dm:%ds",system_status.system_uptime/3600,(system_status.system_uptime%3600)/60,system_status.system_uptime%60);
+    if (!cJSON_AddStringToObject(proot, "system_uptime", value_str)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "uart2_rx_bytes", system_status.uart2_rx_bytes)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "uart2_tx_bytes", system_status.uart2_tx_bytes)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "uart4_tx_bytes", system_status.uart4_tx_bytes)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "uart4_rx_bytes", system_status.uart4_rx_bytes)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "json_upload_count", system_status.json_upload_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "photo_taken_count", system_status.photo_taken_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "cleaning_count", system_status.cleaning_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "vibration_count", system_status.vibration_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+
+    if (!cJSON_AddNumberToObject(proot, "udoc", system_status.upper_door_open_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "udcc", system_status.upper_door_close_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "ldoc", system_status.lower_door_open_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "ldcc", system_status.lower_door_close_count)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+
+    if (!cJSON_AddNumberToObject(proot, "lpt", system_status.lamp_running_time/RT_TICK_PER_SECOND)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    if (!cJSON_AddNumberToObject(proot, "hrt", system_status.heater_running_time/RT_TICK_PER_SECOND)) {
+        cJSON_Delete(proot);
+        return;
+    }
+
+    char* temp = makeJsonRpc2_0("sys_monitor", proot, 1);
+    if(temp){
+        DTU_Write(temp);
+        free(temp);
+    }
+}
 
 /*生成JSON数据*/
 char * makeJson()
@@ -1403,6 +1515,7 @@ char * makeJson()
         return NULL;
     }
     cJSON_Delete(pJsonRoot);
+    system_status.json_upload_count += 1;
     return p;
 }
 
@@ -1539,6 +1652,31 @@ int parseDtu(char *json_string)
                       if(PARAMS!=NULL&&PARAMS->type==cJSON_Object){
                           parse_param_to_mcu(PARAMS);
                       }
+                  }else if (strcmp(METHOD->valuestring, "upgradehmi") == 0) {
+                      if(PARAMS !=NULL&&PARAMS->type==cJSON_Object){
+                            cJSON* FS = cJSON_GetObjectItem(PARAMS, "fs");
+                            // 收到数据包
+                            cJSON* hex_payload = cJSON_GetObjectItem(PARAMS, "hex_payload");
+                            if (hex_payload && hex_payload->type == cJSON_String) {
+                                process_hex_payload(hex_payload->valuestring, 0);
+                            }
+                            if (FS != NULL && FS->type == cJSON_Number) {
+                                char post_hmi_upgrade[64];
+                                total_file_size = FS->valueint;
+                                // 安全格式化
+                                rt_snprintf(post_hmi_upgrade, sizeof(post_hmi_upgrade),
+                                           "whmi-wri %d,115200,0", FS->valueint);
+                                HMI_WRITE(post_hmi_upgrade);
+                                LOG_I("HMI升级命令已发送: %s", post_hmi_upgrade);
+                                thread_delete_workloop();
+                                rt_thread_delete(dtu_send_data);
+                            }
+                        }
+                  }else if(strcmp(METHOD->valuestring,"systeminfo")==0) {
+                      upload_sys_monitor_info();
+                  }
+                  else{
+                      LOG_W("未知的命令: %s", METHOD->valuestring);
                   }
 
               }
@@ -2104,26 +2242,30 @@ int parseDtu(char *json_string)
                       //HMI_WRITE("menu.t0.txt=\"%s\"",link_ip->valuestring);
                   }
 
-                  if(strstr(device_status.imei,imei->valuestring)==RT_NULL)
+                  if(imei!=RT_NULL)
                   {
-                       rt_kprintf("Change IMEI:new:%s-old:%s",imei->valuestring,device_status.imei);
-                       rt_sprintf(device_status.imei, "%s",imei->valuestring);
-                       ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei));  //IMEI
+                      if(strstr(device_status.imei,imei->valuestring)==RT_NULL&&strcmp(imei->valuestring, "") != 0)
+                      {
+                          LOG_I("Change IMEI:new:%s-old:%s",imei->valuestring,device_status.imei);
+                          rt_sprintf(device_status.imei, "%s",imei->valuestring);
+                          ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei));  //IMEI
+                      }
+                      else {
+                          LOG_I("IMEI not change:%s-%s",imei->valuestring,device_status.imei);
+                      }
                   }
-                  else {
-                      rt_sprintf(device_status.imei,"%s",imei->valuestring);
-                      rt_kprintf("IMEI not change:%s-%s",imei->valuestring,device_status.imei);
-                 }
-                  //如果DTU的ICCID和FLash中的不一样,保存到FLASH
-                  if(strstr(device_status.iccid,iccid->valuestring)==RT_NULL)
+                  if(iccid!=RT_NULL)
                   {
-                      rt_kprintf("Change ICCID:new:%s-old:%s",iccid->valuestring,device_status.iccid);
-                      rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
-                      ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid));  //Iccid
-                  }
-                  else {
-                      rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
-                      rt_kprintf("ICCID not change:%s-%s",iccid->valuestring,device_status.iccid);
+                      //如果DTU的ICCID和FLash中的不一样,保存到FLASH
+                      if(strstr(device_status.iccid,iccid->valuestring)==RT_NULL&&strcmp(iccid->valuestring, "") != 0)
+                      {
+                          LOG_I("Change ICCID:new:%s-old:%s",iccid->valuestring,device_status.iccid);
+                          rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
+                          ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid));  //Iccid
+                      }
+                      else {
+                          LOG_I("ICCID not change:%s-%s",iccid->valuestring,device_status.iccid);
+                      }
                   }
 
                   if(datetime!=RT_NULL){
@@ -2236,9 +2378,9 @@ int parseDtu(char *json_string)
                     }
                   }
                   if(driver_4g!=RT_NULL)
-                  sprintf(device_status.driver_4g,"%s",driver_4g->valuestring);
+                      sprintf(device_status.driver_4g,"%s",driver_4g->valuestring);
                   if(project!=RT_NULL)
-                  sprintf(device_status.project,"%s",project->valuestring);
+                      sprintf(device_status.project,"%s",project->valuestring);
                   if(mqttcfg1!=NULL)
                   {
                       if(strstr("8.136.98.49",mqttcfg1->valuestring)!=RT_NULL)

+ 150 - 6
Application_cbd_apm32f1/applications/hmi.c

@@ -27,6 +27,7 @@
 #include <fal.h>
 #include "selfcheck.h"
 #include <user_mb_app.h>
+#include "sys_monitor.h"
 #define DBG_ENABLE
 #define DBG_COLOR
 #define DBG_TAG "hmi"
@@ -52,6 +53,7 @@ void list_mem(void);
 int mb_master_samlpe();
 /**/
 Device_Status_typedef device_status;
+System_stats_t system_status;
 
 extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
 
@@ -72,6 +74,12 @@ rt_device_t serial;   /*串口设备句柄*/
 
 rt_timer_t timer_uart2 = RT_NULL;
 
+static upgrade_state_t current_state = STATE_IDLE;
+uint32_t total_file_size = 0;
+static uint32_t bytes_received = 0;
+static uint8_t recv_buffer[RECV_BUFFER_SIZE];
+static uint32_t recv_write_pos = 0;
+
 /* 消息队列控制块 */
 static struct rt_messagequeue rx_mq;//接收数据消息队列
 static void HMI_Decode(void);
@@ -135,6 +143,7 @@ void HMI_WRITE(char *fmt, ...)
     memcpy(str + str_length, hmi_hex, sizeof(hmi_hex));
     rt_device_write(serial, 0, str, str_length + sizeof(hmi_hex));
     rt_thread_mdelay(1);    //2025-0308
+    system_status.uart4_tx_bytes+=str_length + sizeof(hmi_hex);
 }
 /* 接收数据回调函数 */
 static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
@@ -154,6 +163,124 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
     return result;
 }
 
+// 预计算转换表
+static const int8_t hex_to_byte[256] = {
+    [0 ... 255] = -1,
+    ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
+    ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9,
+    ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
+    ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15
+};
+
+/**
+ * @brief 快速HEX字符串转二进制
+ * @param hex HEX字符串
+ * @param bin 输出缓冲区
+ * @param max_len 最大输出长度
+ * @return 转换的字节数,<0表示错误
+ */
+static inline int fast_hex_to_bin(const char* hex, uint8_t* bin, int max_len)
+{
+    if (!hex || !bin || max_len <= 0) {
+        return -1;
+    }
+
+    int hex_len = strlen(hex);
+    if (hex_len % 2 != 0) {
+        return -1;  // HEX长度必须是偶数
+    }
+
+    int bin_len = hex_len / 2;
+    if (bin_len > max_len) {
+        return -1;  // 输出缓冲区太小
+    }
+
+    for (int i = 0; i < bin_len; i++) {
+        int high = hex_to_byte[(unsigned char)hex[i * 2]];
+        int low = hex_to_byte[(unsigned char)hex[i * 2 + 1]];
+
+        if (high < 0 || low < 0) {
+            return -1;  // 无效HEX字符
+        }
+
+        bin[i] = (high << 4) | low;
+    }
+
+    return bin_len;
+}
+
+
+// 发送原始数据到HMI
+static int send_raw_to_hmi(const uint8_t* data, uint32_t size)
+{
+    if (!serial || !data || size == 0) return -1;
+
+    rt_size_t sent = rt_device_write(serial, 0, data, size);
+    return (sent == size) ? 0 : -1;
+}
+
+
+// 最简洁版本 - 每4096字节直接清空
+void process_hex_payload(const char* hex_str, int req_id) {
+    if (!hex_str) return;
+    current_state = STATE_RECEIVING;
+    // 转换HEX
+    uint8_t temp[JSON_CHUNK_SIZE];
+    int data_len = fast_hex_to_bin(hex_str, temp, JSON_CHUNK_SIZE);
+    if (data_len <= 0) return;
+
+    // 添加到缓冲区
+    if (recv_write_pos + data_len > sizeof(recv_buffer)) {
+        LOG_E("缓冲区溢出");
+        return;
+    }
+
+    memcpy(recv_buffer + recv_write_pos, temp, data_len);
+    recv_write_pos += data_len;
+    bytes_received += data_len;
+
+    LOG_I("收到 %d 字节,缓冲: %u/%u",
+          data_len, recv_write_pos, sizeof(recv_buffer));
+
+    // 每4096字节发送一次
+    if (recv_write_pos >= 4096) {
+        LOG_I("发送 4096 字节到HMI...");
+
+        if (send_raw_to_hmi(recv_buffer, 4096) == 0) {
+            LOG_I("发送成功");
+            // ✅ 直接清空缓冲区
+            recv_write_pos = 0;
+        } else {
+            LOG_E("发送失败");
+        }
+        return;
+    }
+
+    // 检查完成
+    if (bytes_received >= total_file_size && total_file_size > 0) {
+        if (recv_write_pos > 0) {
+            LOG_I("发送最后 %u 字节", recv_write_pos);
+            send_raw_to_hmi(recv_buffer, recv_write_pos);
+            recv_write_pos = 0;  // 清空
+        }
+        current_state = STATE_COMPLETED;
+        LOG_I("升级完成!");
+        return;
+    }
+
+    // 发送进度
+    int progress = total_file_size > 0 ?
+                  (bytes_received * 100 / total_file_size) : 0;
+
+    char progress_json[256];
+    rt_snprintf(progress_json, sizeof(progress_json),
+               "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
+               "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
+               bytes_received, total_file_size, progress, req_id);
+
+    DTU_Write(progress_json);
+}
+
 
 /*入口函数*/
 static void Hmi_thread_entry(void *parameter)
@@ -174,12 +301,23 @@ static void Hmi_thread_entry(void *parameter)
         result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
         if (result == RT_EOK)
         {
-
-                rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
-                rx_buffer[rx_length] = '\0';
-                //LOG_I("%s",rx_buffer);
-                //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
+            rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
+            system_status.uart4_rx_bytes+=rx_length;
+            rx_buffer[rx_length] = '\0';
+            LOG_I("%s",rx_buffer);
+            //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
+            if(rx_length==1&&rx_buffer[rx_length-1]==0x05){
+                LOG_W("屏幕响应信号!");
+                if(current_state==STATE_COMPLETED) return;
+                //RTU可以发送升级文件了
+                char* upgrade_hmi = makeJsonRpc2_0("upgradehmi",NULL,1);
+                if(upgrade_hmi){
+                    DTU_Write(upgrade_hmi);
+                    free(upgrade_hmi);
+                }
+            }else {
                 HMI_Decode();
+            }
         }
         /*********************************************************************************************************************/
         //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
@@ -1371,6 +1509,7 @@ static void Ctr_Mortor_entry(void* parameter)
             {
                 ctr_cangmen_up(OFF);
                 num_up_open = 200;
+                system_status.upper_door_open_count +=1;
             }
             num_up_close = 200;
         }
@@ -1382,6 +1521,7 @@ static void Ctr_Mortor_entry(void* parameter)
             {
                 ctr_cangmen_up(OFF);
                 num_up_close= 200;
+                system_status.upper_door_close_count +=1;
             }
             num_up_open = 200;
         }
@@ -1405,6 +1545,7 @@ static void Ctr_Mortor_entry(void* parameter)
             {
                 ctr_cangmen_down(OFF);
                 num_down_open = 200;
+                system_status.lower_door_open_count +=1;
             }
             num_down_close = 200;
         }
@@ -1417,6 +1558,7 @@ static void Ctr_Mortor_entry(void* parameter)
             {
                ctr_cangmen_down(OFF);
                num_down_close = 200;
+               system_status.lower_door_close_count += 1;
             }
             num_down_open = 200;
         }
@@ -1435,6 +1577,7 @@ static void Ctr_Mortor_entry(void* parameter)
             //打开补光灯
             if(photo_led_sec==800)
             {
+                system_status.photo_taken_count+=1;
                 LOG_I("测试拍照!");
                 #if 1
                 char *takephotojson = makeJson_takephoto();
@@ -1700,7 +1843,7 @@ void Check_ParamConfig(void)
         {
             device_status.dattim = 10;
         }
-
+        if(device_status.thsp==0) device_status.thsp = 300;
         if(device_status.clt_time==0) device_status.clt_time = 10;
         if(device_status.heattime==0) device_status.heattime = 10;
         if(device_status.tph==0&&device_status.tpl==0){ //参数丢失
@@ -1788,6 +1931,7 @@ void Check_ParamConfig(void)
         rt_sprintf(device_status.net,"1");
         rt_sprintf(device_status.csq,"0");
         rt_sprintf(device_status.sim,"1");
+        Sys_Motor_Task();
 #ifdef RT_USING_ALARM
         alarm_app_router();
 #endif

+ 57 - 0
Application_cbd_apm32f1/applications/hmi.h

@@ -22,6 +22,63 @@ extern uint8_t dbg_flag;
 extern uint8_t DBG_EN;  //是否使能调试打印
 
 extern rt_timer_t timer_uart2;
+
+// 常量定义
+// 数据缓冲区
+#define RECV_BUFFER_SIZE   (10 * 1024)  // 10k接收缓冲区
+#define SEND_BUFFER_SIZE   4096           // 发送到HMI的缓冲区
+#define HMI_CHUNK_SIZE          4096    // 发送到屏幕的块大小
+#define JSON_CHUNK_SIZE         512    // JSON接收的块大小
+#define HMI_DEFAULT_BAUDRATE    115200
+#define HMI_ACK_BYTE            0x05
+#define HMI_TERMINATOR          "\xFF\xFF\xFF"
+#define HMI_RESPONSE_TIMEOUT    1000    // 响应超时(ms)
+#define HMI_TRANSFER_TIMEOUT    30000   // 传输超时(ms)
+uint32_t total_file_size;
+// 升级状态
+typedef enum {
+    STATE_IDLE = 0,
+    STATE_WAIT_SIZE,
+    STATE_WAIT_HMI_ACK,
+    STATE_RECEIVING,
+    STATE_SENDING_TO_HMI,
+    STATE_COMPLETED,
+    STATE_FAILED
+} upgrade_state_t;
+
+// 数据包管理
+typedef struct {
+    uint8_t *buffer;             // 数据缓冲区
+    uint32_t size;               // 缓冲区大小
+    uint32_t write_pos;          // 写入位置
+    uint32_t read_pos;           // 读取位置
+    uint32_t chunk_size;         // 分块大小(4096字节)
+} data_buffer_t;
+
+
+// 升级控制结构
+typedef struct {
+    rt_device_t rtu_serial;      // 连接RTU的串口
+    rt_device_t hmi_serial;      // 连接HMI的串口
+    rt_mutex_t lock;             // 互斥锁
+
+    upgrade_state_t state;           // 当前状态
+    uint32_t total_file_size;    // 总文件大小
+    uint32_t bytes_received;     // 已接收字节数
+    uint32_t bytes_sent_to_hmi;  // 已发送到HMI的字节数
+    uint32_t baudrate;           // 波特率
+
+    data_buffer_t recv_buffer;   // 接收缓冲区
+    data_buffer_t send_buffer;   // 发送缓冲区
+
+    rt_tick_t start_time;        // 开始时间
+    rt_tick_t timeout;           // 超时时间
+
+    uint8_t current_chunk[4096]; // 当前发送块
+    uint32_t chunk_filled;       // 当前块已填充大小
+} hmi_upgrade_protocol_t;
+void process_hex_payload(const char* hex_str, int req_id);
+
 void Check_ParamConfig(void);
 void HMI_WRITE(char *fmt,...);
 void Fresh_Hmi(void);

+ 89 - 0
Application_cbd_apm32f1/applications/sys_monitor.c

@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2026-02-04     Administrator       the first version
+ */
+#include "sys_monitor.h"
+#include "cpu_usage.h"
+#include "control.h"
+#include "sht30_collect.h"
+#define DBG_ENABLE
+#define DBG_COLOR
+#define DBG_TAG "sysmonitor"
+#define DBG_LEVEL         DBG_LOG
+#include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
+
+rt_thread_t tid_sys_monitor = RT_NULL;
+
+Device_Status_typedef device_status;
+System_stats_t system_status;
+
+
+static void sys_monitor_thread_entry(void *parameter)
+{
+    uint8_t sec_t;
+    system_status.lamp_start_time = 0;
+    system_status.heat_start_time = 0;
+    while(1)
+    {
+        sec_t++;
+        system_status.cpu_usage = (int)cpu_load_average();
+        if(system_status.cpu_usage>system_status.cpu_usage_max){
+            system_status.cpu_usage_max = system_status.cpu_usage;
+        }
+        rt_tick_t tick = rt_tick_get();
+        rt_uint32_t total_sec = (rt_uint32_t)(tick / RT_TICK_PER_SECOND);
+        system_status.system_uptime = total_sec;
+        system_status.cpu_tempture = (int)Get_Temprate()/100;
+        rt_uint32_t hours   = total_sec / 3600;
+        rt_uint32_t minutes = (total_sec % 3600) / 60;
+        rt_uint32_t seconds = total_sec % 60;
+        rt_thread_delay(1000);
+        if(sec_t%10==0){
+            LOG_D("CPU温度:%d℃",system_status.cpu_tempture);
+            LOG_D("CPU利用率 :%d% Max:%d%",system_status.cpu_usage,system_status.cpu_usage_max);
+            LOG_D("系统运行时长:%d时%d分%d秒",hours,minutes,seconds);
+            LOG_D("重启次数:%d",device_status.rebootcnt);
+
+            LOG_D("串口2累计发送字节:%d",system_status.uart2_tx_bytes);
+            LOG_D("串口2累计接收字节:%d",system_status.uart2_rx_bytes);
+
+            LOG_D("串口4累计发送字节:%d",system_status.uart4_tx_bytes);
+            LOG_D("串口4累计接收字节:%d",system_status.uart4_rx_bytes);
+
+            LOG_D("累计上传数据:%d 条",system_status.json_upload_count);
+            LOG_D("累计拍照次数:%d ",system_status.photo_taken_count);
+            LOG_D("累计清扫次数:%d ",system_status.cleaning_count);
+            LOG_D("累计震动次数:%d ",system_status.vibration_count);
+
+            LOG_D("上仓门累计打开:%d ",system_status.upper_door_open_count);
+            LOG_D("上仓门累计关闭:%d ",system_status.upper_door_close_count);
+            LOG_D("下仓门累计打开:%d ",system_status.lower_door_open_count);
+            LOG_D("下仓门累计关闭:%d ",system_status.lower_door_close_count);
+
+            LOG_D("灯管累计运行:%d S",system_status.lamp_running_time/RT_TICK_PER_SECOND);
+            LOG_D("加热累计运行:%d S",system_status.heater_running_time/RT_TICK_PER_SECOND);
+
+        }
+    }
+}
+
+int Sys_Motor_Task(void)
+{
+    /* 创建线程*/
+    tid_sys_monitor = rt_thread_create("sys_monitor",
+                                sys_monitor_thread_entry, RT_NULL,
+                                1024,       //2048
+                                28,         //12
+                                5);       //5
+
+    if(tid_sys_monitor!=RT_NULL)
+    {
+        rt_thread_startup(tid_sys_monitor);
+    }
+    return 0;
+}

+ 74 - 0
Application_cbd_apm32f1/applications/sys_monitor.h

@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2026-02-04     Administrator       the first version
+ */
+#ifndef APPLICATIONS_SYS_MONITOR_H_
+#define APPLICATIONS_SYS_MONITOR_H_
+
+#include <rtthread.h>
+
+/* 根据你的MCU字长选择合适的数据类型 */
+typedef uint32_t time_sec_t;    // 用于秒级的时间统计
+typedef uint64_t time_ms_t;     // 用于毫秒级的时间统计
+typedef uint32_t counter_t;     // 用于计数器
+typedef uint16_t small_counter_t; // 用于小计数器
+
+/* 系统运行统计结构体 */
+typedef struct
+{
+    /* 1. 系统运行时间相关 */
+    time_sec_t  system_uptime;         // 系统总运行时长(秒)
+    time_sec_t  network_connected_time; // 联网时间(秒)
+    time_sec_t  network_disconnected_time; // 断网时间(秒)
+
+    /* 2. CPU相关 */
+    uint8_t     cpu_tempture;
+    uint8_t     cpu_usage;             // CPU占用率(0-100%)
+    uint8_t     cpu_usage_max;         // 历史最大CPU占用率
+
+    /* 3. 通信统计 */
+    counter_t   uart2_rx_bytes;         // 串口接收字节数
+    counter_t   uart2_tx_bytes;         // 串口发送字节数
+    counter_t   json_upload_count;     // JSON数据上传次数
+
+    counter_t   uart4_rx_bytes;         // 串口接收字节数
+    counter_t   uart4_tx_bytes;         // 串口发送字节数
+
+    /* 4. 动作次数统计 */
+    counter_t   photo_taken_count;     // 拍照次数统计
+    counter_t   cleaning_count;         // 清扫次数统计
+    counter_t   vibration_count;        // 震动次数统计
+
+    /* 5. 仓门状态统计 */
+    counter_t   upper_door_open_count;  // 上仓门打开次数
+    counter_t   upper_door_close_count; // 上仓门关闭次数
+    counter_t   lower_door_open_count;  // 下仓门打开次数
+    counter_t   lower_door_close_count; // 下仓门关闭次数
+
+    /* 6. 传感器触发统计 */
+    counter_t   rain_control_triggers; // 雨控触发次数
+    counter_t   light_control_triggers; // 光控触发次数
+
+    /* 7. 设备运行时间统计 */
+    time_sec_t  lamp_start_time;
+    time_sec_t  lamp_running_time;     // 灯管运行时间
+    time_sec_t  heat_start_time;
+    time_sec_t  heater_running_time;  // 加热时间
+    time_sec_t  fill_light_running_time; // 补光灯运行时间
+
+    /* 8. 时间戳信息(可选) */
+    time_sec_t  last_reset_time;       // 上次重置时间戳
+    uint32_t    reset_count;           // 系统重置次数
+
+
+} System_stats_t;
+
+
+int Sys_Motor_Task(void);
+
+#endif /* APPLICATIONS_SYS_MONITOR_H_ */

+ 6 - 0
Application_cbd_apm32f1/readme.md

@@ -1,3 +1,9 @@
+# 2026-03-13 更新说明
+	"2.35.4(2000W)"
+	1. 增加系统状态监测
+	2. 增加imei和iccid同步的代码健壮性
+	3. 修复参数丢失thsp为0导致屏幕无法常亮
+	
 # 2026-02-02 更新说明
 	"2.34.4(2000W)"
 	增加MCU参数备份到RTU中