|
|
@@ -29,7 +29,7 @@
|
|
|
#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 //线程优先级
|
|
|
@@ -38,6 +38,7 @@
|
|
|
|
|
|
|
|
|
Device_Status_typedef device_status;
|
|
|
+System_stats_t system_status;
|
|
|
DataTemp data_arry;
|
|
|
|
|
|
void list_mem(void);
|
|
|
@@ -101,6 +102,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);
|
|
|
rt_thread_delay(10);
|
|
|
}
|
|
|
@@ -112,8 +114,6 @@ static rt_err_t dtu_input(rt_device_t dev, rt_size_t size)
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
static void dtu_thread_entry_recv(void *parameter)
|
|
|
{
|
|
|
static char accum_buf[MAX_DTU_SIZE]; // 累积缓冲区(根据实际数据量调整)
|
|
|
@@ -126,6 +126,7 @@ static void dtu_thread_entry_recv(void *parameter)
|
|
|
/* 阻塞等待接收信号量,等到信号量后再次读取数据 */
|
|
|
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
|
|
|
}
|
|
|
+ system_status.uart2_rx_bytes+=1;
|
|
|
accum_buf[accum_len++]=ch;
|
|
|
if (accum_len >= sizeof(accum_buf)) {
|
|
|
// 防止超范围
|
|
|
@@ -165,7 +166,11 @@ static void dtu_thread_entry_recv(void *parameter)
|
|
|
/*入口函数-指令发送*/
|
|
|
static void dtu_thread_entry_send(void *parameter)
|
|
|
{
|
|
|
-
|
|
|
+ if(device_status.iFlag==0){
|
|
|
+ backup_param_to_rtu();
|
|
|
+ }else{
|
|
|
+ recover_param_from_rtu();
|
|
|
+ }
|
|
|
device_status.power_status = 1;
|
|
|
device_status.num_updata = 0;
|
|
|
rt_thread_delay(10000);
|
|
|
@@ -713,6 +718,329 @@ char* makeJsonRpc2_0(const char* method, cJSON* params, int id)
|
|
|
return json_str;
|
|
|
}
|
|
|
|
|
|
+void parse_param_to_mcu(cJSON* Params)
|
|
|
+{
|
|
|
+ if(Params!=NULL)
|
|
|
+ {
|
|
|
+ cJSON* value = NULL;
|
|
|
+ value = cJSON_GetObjectItem(Params,"reboot_time");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.rebootcnt = value->valueint;
|
|
|
+ ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"ds");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.ds = value->valueint;
|
|
|
+ ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"tt");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.sunBar = value->valueint;
|
|
|
+ ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"st");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.startBar = value->valueint;
|
|
|
+ ef_set_env_blob("sunBar", &device_status.startBar, sizeof(device_status.startBar));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"et");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.stopBar = value->valueint;
|
|
|
+ ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"ts");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.timeSwitch = value->valueint;
|
|
|
+ ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"tpl");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.tpl = value->valueint;
|
|
|
+ ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"tph");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.tph = value->valueint;
|
|
|
+ ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"stime");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.clt_time = value->valueint;
|
|
|
+ ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"jtime");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.heattime = value->valueint;
|
|
|
+ ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"hrt");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.heatvalue = value->valueint;
|
|
|
+ ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"shake_sec");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.shake_sec = value->valueint;
|
|
|
+ ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"shake_ws");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.shake_ws = value->valueint;
|
|
|
+ ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"version_type");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.version_type = value->valueint;
|
|
|
+ ef_set_env_blob("version_type", &device_status.version_type, sizeof(device_status.version_type));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"heat_sw");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.heat_sw = value->valueint;
|
|
|
+ ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"dattim");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.dattim = value->valueint;
|
|
|
+ ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"deviceId");
|
|
|
+ if(value!=NULL){
|
|
|
+ rt_snprintf(device_status.deviceId, sizeof(device_status.deviceId),"%s",value->string);
|
|
|
+ ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
|
|
|
+ }
|
|
|
+
|
|
|
+ value = cJSON_GetObjectItem(Params,"handle_bugger");
|
|
|
+ if(value!=NULL){
|
|
|
+ device_status.handle_bugger = value->valueint;
|
|
|
+ ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ LOG_E("Params is NULL!");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//备份MCU参数到RTU
|
|
|
+void backup_param_to_rtu(void)
|
|
|
+{
|
|
|
+ cJSON* proot = cJSON_CreateObject();
|
|
|
+ if (proot == NULL) {
|
|
|
+ LOG_E("Error: Failed to create JSON object\n");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加开关机
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "reboot_time", device_status.rebootcnt)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加开关机
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "ds", device_status.ds)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //光控定时时长
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "tt", device_status.sunBar)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //时控开始时间
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "st", device_status.startBar)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //时控结束时间
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "et", device_status.stopBar)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //时控模式
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "ts", device_status.timeSwitch)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //低温保护值
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "tpl", device_status.tpl)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //高温保护值
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "tph", device_status.tph)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //收集时间
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "stime", device_status.clt_time)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //加热时间
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "jtime", device_status.heattime)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //加热设定温度
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "hrt", device_status.heatvalue)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //震动时间
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "shake_sec", device_status.shake_sec)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //震动开关
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "shake_ws", device_status.shake_ws)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //版本类型
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "version_type", device_status.version_type)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //常加热开关
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "heat_sw", device_status.heat_sw)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //上传间隔
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "dattim", device_status.dattim)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //北大荒设备ID
|
|
|
+ if (!cJSON_AddStringToObject(proot, "deviceId", device_status.deviceId)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //handle_bugger
|
|
|
+ if (!cJSON_AddNumberToObject(proot, "handle_bugger", device_status.handle_bugger)) {
|
|
|
+ cJSON_Delete(proot);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ char* temp = makeJsonRpc2_0("sync_param", proot, 1);
|
|
|
+ if(temp){
|
|
|
+ DTU_Write(temp);
|
|
|
+ free(temp);
|
|
|
+ }
|
|
|
+ //cJSON_Delete(proot);
|
|
|
+}
|
|
|
+
|
|
|
+void recover_param_from_rtu(void)
|
|
|
+{
|
|
|
+ char* temp1 = makeJsonRpc2_0("recover_param", NULL, 1);
|
|
|
+ if(temp1){
|
|
|
+ DTU_Write(temp1);
|
|
|
+ rt_free(temp1);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ char* temp = makeJsonRpc2_0("sys_monitor", proot, 1);
|
|
|
+ if(temp){
|
|
|
+ DTU_Write(temp);
|
|
|
+ free(temp);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/*生成JSON数据*/
|
|
|
char * makeJson()
|
|
|
{
|
|
|
@@ -1017,6 +1345,7 @@ char * makeJson()
|
|
|
return NULL;
|
|
|
}
|
|
|
cJSON_Delete(pJsonRoot);
|
|
|
+ system_status.json_upload_count += 1;
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
@@ -1158,7 +1487,14 @@ int parseDtu(char *json_string)
|
|
|
rt_thread_delete(dtu_send_data);
|
|
|
}
|
|
|
}
|
|
|
- }else{
|
|
|
+ }else if(strcmp(METHOD->valuestring,"sync_param")==0) {
|
|
|
+ if(PARAMS!=NULL&&PARAMS->type==cJSON_Object){
|
|
|
+ parse_param_to_mcu(PARAMS);
|
|
|
+ }
|
|
|
+ }else if(strcmp(METHOD->valuestring,"systeminfo")==0) {
|
|
|
+ upload_sys_monitor_info();
|
|
|
+ }
|
|
|
+ else{
|
|
|
LOG_W("未知的命令: %s", METHOD->valuestring);
|
|
|
}
|
|
|
|