代码暂存 使用Lwrb库优化4G上报多条数据时的冲突问题

This commit is contained in:
常正强 2025-06-06 09:16:28 +08:00
parent e3016cca9c
commit 7d7f2f1dab
5 changed files with 95 additions and 111 deletions

View File

@ -3,7 +3,8 @@
#include "CH58x_common.h"
#include "lwrb.h"
#include "bsp_valve.h"
/*4G 친욥왠齡多신*/
#define ENABLE_3_8_V GPIO_Pin_3 //PB3
@ -110,7 +111,7 @@ static void BSP_Ml307_Power_Off(void);
void BSP_Ml307r_Init(void);
void Iot_Send_Data(void);
void Iot_Send_Data(TeFrameCmd cmd);
void Ml307r_Loop(void);
void BSP_Module_Emergency_Send(uint8_t* data, uint8_t len);
void BSP_ML307_StartTimeoutTimer(void);
@ -120,6 +121,15 @@ static void BSP_Read_Module(void);
static void BSP_simcom_init(void);
static void BSP_Get_Module_Info(void);
static void BSP_Module_Connect_CtWing(void);
// 状态上报请求结构体
typedef struct {
TeFrameCmd cmd; // 命令类型
} TsIotSendRequest;
// 声明状态上报队列
extern lwrb_t iot_send_queue;
extern uint8_t iot_send_queue_buffer[sizeof(TsIotSendRequest) * 5]; // 可以存储5个请求
#endif //!@__BSP_ML307R_H__

View File

@ -42,57 +42,6 @@ void ADC_GPIO_Init(void)
R8_ADC_CFG |= RB_ADC_POWER_ON;
}
// void Send_Low_Battery_Message(float vbat)
// {
// char json_buffer[300];
// char time_str[20] = {0};
// snprintf(json_buffer, sizeof(json_buffer),
// "{"
// "\"device\":{"
// "\"id\":\"SCV001\","
// "\"type\":\"SelfClosingValve\","
// "\"time\":\"%s\""
// "},"
// "\"status\":\"closed\","
// "\"fault\":{"
// "\"lowBattery\":true,"
// "\"commError\":false,"
// "\"code\":1001"
// "},"
// "\"monitor\":{"
// "\"pressure\":0.8,"
// "\"voltage\":%.2f,"
// "\"lowVoltage\":true"
// "},"
// "\"network\":{"
// "\"sinr\":15.2,"
// "\"rsrp\":-95.5,"
// "\"signal\":4"
// "},"
// "\"standards\":{"
// "\"iotId\":\"IoT-SCV-12345\""
// "}"
// "}",
// time_str, vbat);
// BSP_Module_Emergency_Send((uint8_t*)json_buffer, strlen(json_buffer));
// logDebug("Send low battery message: %s\n", json_buffer);
// }
// // ´¦Àíµç³ØµçѹµÍʼþ
// void Handle_Low_Battery(float vbat)
// {
// if (!vbat_low_flag) {
// VALVE_CLOSE();
// LED_VALVE_CLOSE;
// Send_Low_Battery_Message(vbat);
// vbat_low_flag = 1;
// logDebug("Voltage is too low (%.2fV),Close valve\n", vbat);
// }
// LED_VBAT_OPEN;
// DelayMs(100);
// }
uint16_t VBAT_ProcessEvent(uint8_t task_id, uint16_t events)
{
if (events & VBAT_EVT_START)

View File

@ -1051,7 +1051,6 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
if (motor_flag == 1)
{
motor_flag = 0;
LED_VALVE_OPEN;
BSP_VALVE_Open(kValveCmdManualOpen); // 使用普通开阀命令
keydown_flag = 1;
IotFlag_t.Valve_Open_flag = 1;
@ -1061,7 +1060,6 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
else if (motor_flag == 2)
{
motor_flag = 0;
LED_VALVE_CLOSE;
// 直接使用普通关阀命令
BSP_VALVE_Close(kValveCmdManualClose);

View File

@ -12,13 +12,17 @@
#include "time.h"
#include <stdlib.h>
#include "cJSON.h"
#include "bsp_valve.h"
#include "bsp_adc.h"
#undef LOG_ENABLE
#define LOG_ENABLE 1
IotFlagStruct IotFlag_t;
// 创建环形缓冲区用于存储IoT发送请求
lwrb_t iot_send_queue;
uint8_t iot_send_queue_buffer[sizeof(TsIotSendRequest) * 5] = {0};
#define ML307_PERIODIC_EVT (0x0001 << 0)
@ -722,9 +726,17 @@ static void BSP_SendMessage(void)
void BSP_ML307_SendMessage(void)
{
// 根据系统状态确定命令类型
TeFrameCmd cmd = kCmdData; // 默认为数据命令
// 默认命令类型
TeFrameCmd cmd = kCmdData;
// 从队列中读取一个请求
TsIotSendRequest request;
if (lwrb_get_full(&iot_send_queue) >= sizeof(request)) {
lwrb_read(&iot_send_queue, &request, sizeof(request));
cmd = request.cmd;
logDebug("Get command type: %d\r\n", cmd);
} else {
// 如果队列为空,则使用默认逻辑
if (gValveData.switch_status == kClosed)
{
cmd = gValveCloseReason;
@ -732,6 +744,7 @@ void BSP_ML307_SendMessage(void)
{
cmd = gValveOpenReason;
}
}
// 生成JSON数据
BSP_4G_Generate_JsonData(&g4GJsonData, cmd);
@ -744,7 +757,7 @@ void BSP_ML307_SendMessage(void)
logDebug("json_data len %d\r\n", strlen(json_data));
BSP_SendMessage();
} else {
logDebug("JSON数据内存分配失败!\n");
logDebug("JSON Memory failure!\n");
IotFlag_t.send_error_flag = 1;
}
}
@ -1051,31 +1064,29 @@ static void BSP_Iot_Loop(void)
{
logDebug("send_complete_into_stop\r\n");
IotFlag_t.send_complete_flag = 0;
state = 0;
// 检查指针是否为NULL后再释放避免重复释放
if(json_data != NULL) {
tmos_msg_deallocate((uint8_t *)json_data);
json_data = NULL;
}
// 检查队列中是否还有待发送的请求
if (lwrb_get_full(&iot_send_queue) >= sizeof(TsIotSendRequest)) {
logDebug("The queue has data to be sent, continue\r\n");
IotFlag_t.ModInfo_success_flag = 1;
state = 5; // 直接跳转到发送状态
} else {
logDebug("send finish\r\n");
state = 0;
MultiTimerStart(&Multi_StopTask_t, STOP_TASK_INTERVAL_TIME, StopTask_CallBack, NULL); //关闭
}
}else
{
// if (IotFlag_t.Loop_count_flag > 2)
// {
// logDebug("Loop_count_flag_end %d\r\n",IotFlag_t.Loop_count_flag);
state = 0;
IotFlag_t.Iot_Retry_flag = 0;
IotFlag_t.module_init_flag = 0;
MultiTimerStart(&Multi_StopTask_t, STOP_TASK_INTERVAL_TIME, StopTask_CallBack, NULL);
// }
// if((IotFlag_t.Iot_Retry_flag == 0)&&(IotFlag_t.Loop_count_flag < 3))
// {
// logDebug("error_into_loop\r\n");
// logDebug("Loop_count_flag %d\r\n",IotFlag_t.Loop_count_flag);
// BSP_Ml307_Power_Off(); //关机
// MultiTimerStart(&Multi_Loop_t, LOOP_INTERVAL_TIME, Loop_CallBack, NULL);
// IotFlag_t.Iot_Retry_flag = 1;
// }
}
break;
}
@ -1105,6 +1116,8 @@ void BSP_Ml307r_Init(void)
{
if(IotFlag_t.Date_Sending_flag == 0)
{
// 初始化状态上报队列
lwrb_init(&iot_send_queue, iot_send_queue_buffer, sizeof(iot_send_queue_buffer));
BSP_UART1_Init();
Ml307r_Gpio_Init();
ml307r_obj = at_obj_create(&at_adapter);//Create AT communication object
@ -1125,11 +1138,19 @@ void BSP_Ml307r_Init(void)
}
}
void Iot_Send_Data(void)
void Iot_Send_Data(TeFrameCmd cmd)
{
// 创建请求结构体
TsIotSendRequest request;
request.cmd = cmd;
// 将请求添加到队列
if (lwrb_write(&iot_send_queue, &request, sizeof(request)) == sizeof(request)) {
logDebug("add to the queue\r\n");
// 如果当前没有发送任务在进行,则启动发送流程
if(IotFlag_t.Date_Sending_flag == 0)
{
logDebug("Iot_Send_Data!!!\r\n");
BSP_UART1_Init();
Ml307r_Gpio_Init();
logDebug("Ml307r_Init_Finish!!!\r\n");
@ -1139,14 +1160,14 @@ void Iot_Send_Data(void)
IotFlag_t.module_init_flag = 1;
BSP_BlockSleep();
ml307_task_id = TMOS_ProcessEventRegister(Ml307r_Handle);
// tmos_set_event(ml307_task_id, ML307_PERIODIC_EVT);
tmos_start_task(ml307_task_id, ML307_PERIODIC_EVT, MS1_TO_SYSTEM_TIME(300)); //电流稳定后再启动模组
tmos_start_task(ml307_task_id, ML307_PERIODIC_EVT, MS1_TO_SYSTEM_TIME(300));
}else
{
BSP_Ml307r_Init();
}
}else
{
logDebug("ERROR ,Iot_Send_Data!!!\r\n");
}
} else {
logDebug("The queue is full\r\n");
lwrb_reset(&iot_send_queue);
}
}

View File

@ -371,9 +371,12 @@ static uint16_t VAVLE_Task_ProcessEvent(uint8_t task_id, uint16_t events) // 阀
tmos_set_event(Peripheral_TaskID, SBP_REPLY_CMD_EVT);
if (IotFlag_t.Date_Sending_flag == 0)
{
BSP_RequestSleep();
}
logDebug("VAVLE_CLOSE_END_EVT");
Iot_Send_Data();
Iot_Send_Data(gValveCloseReason);
return (events ^ VAVLE_CLOSE_END_EVT);
}
@ -424,9 +427,12 @@ static uint16_t VAVLE_Task_ProcessEvent(uint8_t task_id, uint16_t events) // 阀
gValveOpenReason = kCmdOpenVavle;
tmos_set_event(Peripheral_TaskID, SBP_REPLY_CMD_EVT);
if (IotFlag_t.Date_Sending_flag == 0)
{
BSP_RequestSleep();
}
Iot_Send_Data();
Iot_Send_Data(gValveOpenReason);
// 启动微泄漏检测
startMicroLeakDetection();
@ -628,7 +634,7 @@ uint8_t BSP_VALVE_Close(TeFrameCmd close_reason)
// 设置关阀原因
gValveCloseReason = close_reason;
LED_VALVE_CLOSE;
// 触发关阀事件
tmos_set_event(vavle_task_id, VAVLE_CLOSE_START_EVT);
@ -702,7 +708,7 @@ uint8_t BSP_VALVE_Open(TeFrameCmd open_reason)
return 2;
}
gValveOpenReason = open_reason;
LED_VALVE_OPEN;
// 触发开阀事件
tmos_set_event(vavle_task_id, VAVLE_OPEN_START_EVT);