|
@@ -21,22 +21,17 @@
|
|
|
#include "time_count.h"
|
|
|
|
|
|
|
|
|
-void master_task(uint8_t *string,uint16_t stringlength);
|
|
|
-void slave_task();
|
|
|
-int READ_MODBUS_DATA(DEVICE_PARAMS *current_device,char* buf);
|
|
|
+
|
|
|
+int READ_DATA(DEVICE_PARAMS *current_device,char* buf);
|
|
|
+void WRITE_MODBUS_DATA(char* cJSONstring);
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
char string[512];
|
|
|
void findDifference(char* data1, char* data2, char* string) ;
|
|
|
int jsoncunt = 1;
|
|
|
+int commd = 1;
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
-uint16_t BufferSize;
|
|
|
-uint8_t Buffer[256];
|
|
|
-uint32_t rx_num = 0;
|
|
|
|
|
|
-uint8_t PingMsg[] = "PING\0";
|
|
|
-uint8_t PongMsg[] = "PONG\0";
|
|
|
-tRadioDriver *Radio=NULL;
|
|
|
/*
|
|
|
*********************************************************************************************************
|
|
|
* 函 数 名: void data_task(void *pdata)
|
|
@@ -50,75 +45,79 @@ void data_task(void *pdata)
|
|
|
{
|
|
|
OS_CPU_SR cpu_sr;
|
|
|
pdata = pdata;
|
|
|
-// Radio = RadioDriverInit();
|
|
|
-// Radio->Init();
|
|
|
mmodbus_init(1);
|
|
|
+ char buf[1000]; // 接收modbus读取的数据
|
|
|
+
|
|
|
char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
|
|
|
read_file("device.txt", device_config_json);
|
|
|
addGatewayParams(device_config_json);
|
|
|
myfree(SRAMEX, device_config_json);
|
|
|
|
|
|
-GATEWAY_PARAMS *get;
|
|
|
+ GATEWAY_PARAMS *get;
|
|
|
get= get_gateway_config_params();
|
|
|
int deviceIndex=0;
|
|
|
DEVICE_PARAMS *current_device=get->device_params;
|
|
|
|
|
|
-// uint32_t baudrate = get->baudrate;
|
|
|
-// uint8_t databits = get->dataBits;
|
|
|
-// uint8_t stopbits = get->stopBit;
|
|
|
-// uint8_t parity = get->parity;
|
|
|
-// uint8_t flowcontrol = get->flowControl;
|
|
|
-// Config_485_Port(baudrate, databits, stopbits, parity, flowcontrol);
|
|
|
+// Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
|
|
|
|
|
|
-
|
|
|
- uint16_t bufferLength;
|
|
|
- OS_Q_DATA Qnum;
|
|
|
- StringInfo message;
|
|
|
- char *mqttRecv;
|
|
|
- uint8_t err;
|
|
|
-
|
|
|
while (current_device!=NULL)
|
|
|
- {
|
|
|
- char buf[1000]; // 接收modbus读取的数据
|
|
|
- READ_MODBUS_DATA(current_device, buf);
|
|
|
- if(current_device->nextDevice!=NULL)
|
|
|
- {
|
|
|
- current_device=current_device->nextDevice;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- sprintf(buf + strlen(buf) - 1, "}");
|
|
|
- time1 = OSTimeGet();
|
|
|
- if( jsoncunt || time2 <= time1 - 10000)
|
|
|
- {
|
|
|
- memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
|
- memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
|
|
|
- sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
|
|
|
- int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
- if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
- current_device=get->device_params;
|
|
|
- jsoncunt = 0;
|
|
|
- time2 = OSTimeGet();
|
|
|
- }//10s进行一次
|
|
|
-
|
|
|
- else
|
|
|
- {
|
|
|
- if(strcmp(buf,pubJsonStringCopy))
|
|
|
+ {
|
|
|
+ uint8_t err;
|
|
|
+ StringInfo *message=NULL; //接收队列消息结构体
|
|
|
+ message=(StringInfo *)OSQPend(JsonQ,1000,&err);
|
|
|
+ if(commd)
|
|
|
+ {
|
|
|
+ READ_DATA(current_device, buf);
|
|
|
+ if(current_device->nextDevice!=NULL)
|
|
|
+ {
|
|
|
+ current_device=current_device->nextDevice;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sprintf(buf + strlen(buf) - 1, "}");
|
|
|
+ time1 = OSTimeGet();
|
|
|
+ if( jsoncunt || time2 <= time1 - 20000)
|
|
|
{
|
|
|
- memset(string, 0 , strlen(string));
|
|
|
- findDifference(buf, pubJsonStringCopy, string);
|
|
|
- memset(pubJsonString,0, strlen(pubJsonString));
|
|
|
- sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
|
|
|
memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
|
- sprintf(pubJsonStringCopy, buf, strlen(buf));
|
|
|
-
|
|
|
+ memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
|
|
|
+ sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
|
|
|
int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
- if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
- }
|
|
|
- }
|
|
|
- memset(buf,0,strlen(buf));
|
|
|
- current_device=get->device_params;
|
|
|
- }
|
|
|
+ if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
+ current_device=get->device_params;
|
|
|
+ jsoncunt = 0;
|
|
|
+ time2 = OSTimeGet();
|
|
|
+ }//20s进行一次
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(strcmp(buf,pubJsonStringCopy))
|
|
|
+ {
|
|
|
+ memset(string, 0 , strlen(string));
|
|
|
+ findDifference(buf, pubJsonStringCopy, string);
|
|
|
+ memset(pubJsonString,0, strlen(pubJsonString));
|
|
|
+ sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
|
|
|
+ memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
|
+ sprintf(pubJsonStringCopy, buf, strlen(buf));
|
|
|
+
|
|
|
+ int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
+ if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ memset(buf,0,strlen(buf));
|
|
|
+ current_device=get->device_params;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(message != NULL)
|
|
|
+ {
|
|
|
+ WRITE_MODBUS_DATA(message->p);
|
|
|
+ }
|
|
|
+ printf("sx1278_task_recv : %s",message->p);
|
|
|
+ myfree(SRAMIN ,message->p);
|
|
|
+ commd = 1;
|
|
|
+ }
|
|
|
+ OSTimeDly(1000);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -140,7 +139,7 @@ GATEWAY_PARAMS *get;
|
|
|
*********************************************************************************************************
|
|
|
*/
|
|
|
|
|
|
-int READ_MODBUS_DATA(DEVICE_PARAMS *device, char* buf)
|
|
|
+int READ_DATA(DEVICE_PARAMS *device, char* buf)
|
|
|
{
|
|
|
DEVICE_PARAMS *current_device=device;
|
|
|
GATEWAY_PARAMS* get;
|
|
@@ -211,67 +210,70 @@ void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
uint16_t data;
|
|
|
uint16_t number;
|
|
|
uint16_t slaveAddress;
|
|
|
-
|
|
|
GATEWAY_PARAMS* get;
|
|
|
get = get_gateway_config_params();
|
|
|
DEVICE_PARAMS* current_device = get->device_params;
|
|
|
-
|
|
|
- cJSON *root = cJSON_Parse(cJSONstring);
|
|
|
- const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
|
|
|
- const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
|
|
|
- const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
|
|
|
- const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
|
|
|
- const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
|
|
|
-
|
|
|
- while(current_device)
|
|
|
- {
|
|
|
- char* device_ID = (char*)current_device->deviceID;
|
|
|
- GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
|
|
|
- if(!strcmp(device_ID,deviceId))
|
|
|
+ if(cJSONstring[0] != '\0'){
|
|
|
+ cJSON *root = cJSON_Parse(cJSONstring);
|
|
|
+ const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
|
|
|
+ const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
|
|
|
+ const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
|
|
|
+ const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
|
|
|
+ const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
|
|
|
+
|
|
|
+ while(current_device)
|
|
|
{
|
|
|
- OSTimeDlyHMSM(0, 0, 0, 70);
|
|
|
- OSIntEnter();
|
|
|
- if(power)
|
|
|
+ char* device_ID = (char*)current_device->deviceID;
|
|
|
+ GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
|
|
|
+ if(!strcmp(device_ID,deviceId))
|
|
|
{
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
- data = power->valueint;
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
- }
|
|
|
- if(temp)
|
|
|
- {
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
- data = temp->valueint;
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
- }
|
|
|
- if(mode)
|
|
|
- {
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
- data = mode->valueint;
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
- }
|
|
|
- if(fan)
|
|
|
- {
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
- data = fan->valueint;
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, 100);
|
|
|
+ //OSIntEnter();
|
|
|
+ if(power)
|
|
|
+ {
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
+ data = power->valueint;
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
+ }
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, 100);
|
|
|
+ if(temp)
|
|
|
+ {
|
|
|
+ currentModbusParams = currentModbusParams->nextParams;
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
+ data = temp->valueint;
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
+ }
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, 100);
|
|
|
+ if(mode)
|
|
|
+ {
|
|
|
+ currentModbusParams = currentModbusParams->nextParams;
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
+ data = mode->valueint;
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
+ }
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, 100);
|
|
|
+ if(fan)
|
|
|
+ {
|
|
|
+ currentModbusParams = currentModbusParams->nextParams;
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
+ data = fan->valueint;
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
+ }
|
|
|
+ //OSIntExit();
|
|
|
}
|
|
|
- OSIntExit();
|
|
|
+ current_device = current_device->nextDevice;
|
|
|
}
|
|
|
- current_device = current_device->nextDevice;
|
|
|
+ cJSON_Delete(root);
|
|
|
+
|
|
|
}
|
|
|
- cJSON_Delete(root);
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-// 比较两个 JSONItem 数组,找到变化的部分并打印
|
|
|
+// 比较两个 JSONItem 数组,找到变化的部分
|
|
|
void findDifference(char* buf, char* pubJsonStringCopy, char* string)
|
|
|
{
|
|
|
const char* delimiter = "{}";
|