zhunk 8 месяцев назад
Родитель
Сommit
f8244019d0

+ 135 - 0
Application_CBD/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/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

+ 54 - 0
Application_CBD/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);

+ 31 - 0
Application_CBD/applications/dtu_uart2.c

@@ -1034,6 +1034,37 @@ void upload_sys_monitor_info(void)
     }
 
 
+    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);

+ 1 - 1
Application_CBD/applications/hmi.c

@@ -1766,7 +1766,7 @@ static void Ctr_Mortor_entry(void* parameter)
                 ctr_led(OFF);
                 photo_led_sec = 900;
                 device_status.photo_ws = 0;
-                if(device_status.work_status = 1){
+                if(device_status.work_status == 1){
                     device_status.clear_status = 1;
                 }
                 HMI_WRITE("sysTest.switchPhoto.val=0");

+ 89 - 0
Application_CBD/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/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/readme.md

@@ -4,6 +4,12 @@
 	#define VERSION_ZP          "1.65.1(ZP)"            //1.0转盘
 	#define VERSION_GK          "1.65.1(GK)"            //1.0高空
 	1. 增加系统运行状态监测;
+		查询指令:
+		{
+		  "jsonrpc": "2.0",
+		  "method": "systeminfo",
+		  "id": 1
+		}
 # 2026-02-03 更新说明
 	#define VERSION_HK          "1.64.41(HK)"            //4.0海康-1200W
     #define VERSION_HK_2000W    "1.64.40(2000W)"         //4.0水稻