|
@@ -17,6 +17,7 @@
|
|
#include "sys_mqtt.h"
|
|
#include "sys_mqtt.h"
|
|
#include "gateway_message.h"
|
|
#include "gateway_message.h"
|
|
#include "MQTTClient.h"
|
|
#include "MQTTClient.h"
|
|
|
|
+#include "cJSON.h"
|
|
|
|
|
|
void master_task(uint8_t *string,uint16_t stringlength);
|
|
void master_task(uint8_t *string,uint16_t stringlength);
|
|
void slave_task();
|
|
void slave_task();
|
|
@@ -46,45 +47,45 @@ void data_task(void *pdata)
|
|
pdata = pdata;
|
|
pdata = pdata;
|
|
// Radio = RadioDriverInit();
|
|
// Radio = RadioDriverInit();
|
|
// Radio->Init();
|
|
// Radio->Init();
|
|
-// mmodbus_init(1);
|
|
|
|
|
|
+ mmodbus_init(1);
|
|
|
|
|
|
char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
|
|
char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
|
|
read_file("device.txt", device_config_json);
|
|
read_file("device.txt", device_config_json);
|
|
addGatewayParams(device_config_json);
|
|
addGatewayParams(device_config_json);
|
|
myfree(SRAMEX, device_config_json);
|
|
myfree(SRAMEX, device_config_json);
|
|
-// 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);
|
|
|
|
-//
|
|
|
|
-// uint8_t string[256];
|
|
|
|
-// uint16_t bufferLength;
|
|
|
|
-// OS_Q_DATA Qnum;
|
|
|
|
-// StringInfo message;
|
|
|
|
-// char *mqttRecv;
|
|
|
|
-// uint8_t err;
|
|
|
|
-// while (current_device!=NULL)
|
|
|
|
-// {
|
|
|
|
-// READ_MODBUS_DATA(current_device);
|
|
|
|
-// if(current_device->nextDevice!=NULL)
|
|
|
|
-// {
|
|
|
|
-// current_device=current_device->nextDevice;
|
|
|
|
-// }
|
|
|
|
-// else
|
|
|
|
-// {
|
|
|
|
-// int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
|
-// if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
|
-// memset(pubJsonString,0,strlen(pubJsonString));
|
|
|
|
-// current_device=get->device_params;
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ uint8_t string[256];
|
|
|
|
+ uint16_t bufferLength;
|
|
|
|
+ OS_Q_DATA Qnum;
|
|
|
|
+ StringInfo message;
|
|
|
|
+ char *mqttRecv;
|
|
|
|
+ uint8_t err;
|
|
|
|
+ while (current_device!=NULL)
|
|
|
|
+ {
|
|
|
|
+ READ_MODBUS_DATA(current_device);
|
|
|
|
+ if(current_device->nextDevice!=NULL)
|
|
|
|
+ {
|
|
|
|
+ current_device=current_device->nextDevice;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
|
+ if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
|
+ memset(pubJsonString,0,strlen(pubJsonString));
|
|
|
|
+ current_device=get->device_params;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
#if 0 //测试代码不经过转发直接进行相应的解析
|
|
#if 0 //测试代码不经过转发直接进行相应的解析
|
|
@@ -95,7 +96,16 @@ void data_task(void *pdata)
|
|
nodeSendReaddValue(node_string,&node_string_Length);
|
|
nodeSendReaddValue(node_string,&node_string_Length);
|
|
GatewayProtocolAnalysis(node_string,node_string_Length);
|
|
GatewayProtocolAnalysis(node_string,node_string_Length);
|
|
#endif
|
|
#endif
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+*********************************************************************************************************
|
|
|
|
+* 函 数 名: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
|
|
+* 功能说明: 读取当前节点上的modbus数据
|
|
|
|
+* 形 参:DEVICE_PARAMS *device 当前设备
|
|
|
|
+* 返 回 值: 1 :成功 0:失败
|
|
|
|
+*********************************************************************************************************
|
|
|
|
+*/
|
|
|
|
+
|
|
int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
{
|
|
{
|
|
DEVICE_PARAMS *current_device=device;
|
|
DEVICE_PARAMS *current_device=device;
|
|
@@ -205,48 +215,78 @@ int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
}
|
|
}
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
+ /*
|
|
|
|
+*********************************************************************************************************
|
|
|
|
+* 函 数 名:void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
|
+* 功能说明: 接收mqtt数据并写入modbus寄存器
|
|
|
|
+* 形 参:char* cJSONstring mqtt接收到的数据
|
|
|
|
+* 返 回 值: 无
|
|
|
|
+*********************************************************************************************************
|
|
|
|
+*/
|
|
|
|
|
|
-void WRITE_MODBUS_DATA(char* buf){
|
|
|
|
|
|
+value data = {0};
|
|
|
|
+void WRITE_MODBUS_DATA(char* cJSONstring){
|
|
GATEWAY_PARAMS* get;
|
|
GATEWAY_PARAMS* get;
|
|
get = get_gateway_config_params();
|
|
get = get_gateway_config_params();
|
|
- GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = get->device_params->params->gateway_write_modbus_command;
|
|
|
|
-
|
|
|
|
- value data;
|
|
|
|
- data.PowerValue = parseIntField(buf, "\"power\":");
|
|
|
|
- data.TempValue = parseIntField(buf, "\"temp\":");
|
|
|
|
- data.ModeValue = parseIntField(buf, "\"mode\":");
|
|
|
|
- data.FanValue = parseIntField(buf, "\"fan\":");
|
|
|
|
- data.RoomTempValue = parseIntField(buf, "\"roomTemp\":");
|
|
|
|
|
|
+ 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");
|
|
|
|
+
|
|
uint16_t slaveAddress;
|
|
uint16_t slaveAddress;
|
|
uint16_t number;
|
|
uint16_t number;
|
|
-
|
|
|
|
- if(data.PowerValue){
|
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data.PowerValue);
|
|
|
|
- }
|
|
|
|
- if(data.TempValue){
|
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data.TempValue);
|
|
|
|
- }
|
|
|
|
- if(data.ModeValue){
|
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data.ModeValue);
|
|
|
|
- }
|
|
|
|
- if(data.FanValue){
|
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data.FanValue);
|
|
|
|
- }
|
|
|
|
- if(data.RoomTempValue){
|
|
|
|
- currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
- slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
- number =currentModbusParams->registerAddress;
|
|
|
|
- mmodbus_writeHoldingRegister16i(slaveAddress, number, data.RoomTempValue);
|
|
|
|
|
|
+ uint16_t data;
|
|
|
|
+
|
|
|
|
+ while(current_device){
|
|
|
|
+ char* device_ID = current_device->deviceID;
|
|
|
|
+ GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
|
|
|
|
+ int time = 900;//60
|
|
|
|
+ if(!strcmp(device_ID,deviceId)){
|
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, time);
|
|
|
|
+ OSIntEnter();
|
|
|
|
+ if(power){
|
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
|
+ data = power->valueint;
|
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, time);
|
|
|
|
+ }
|
|
|
|
+ if(temp){
|
|
|
|
+ currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
|
+ data = temp->valueint;
|
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, time);
|
|
|
|
+ }
|
|
|
|
+ if(mode){
|
|
|
|
+ currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
|
+ data = mode->valueint;
|
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, time);
|
|
|
|
+ }
|
|
|
|
+ if(fan){
|
|
|
|
+ currentModbusParams = currentModbusParams->nextParams;
|
|
|
|
+ slaveAddress = currentModbusParams->slaveAddress;
|
|
|
|
+ number =currentModbusParams->registerAddress;
|
|
|
|
+ data = fan->valueint;
|
|
|
|
+ mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, time);
|
|
|
|
+ }
|
|
|
|
+ OSIntExit();
|
|
|
|
+ }
|
|
|
|
+ current_device = current_device->nextDevice;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ cJSON_Delete(root);
|
|
}
|
|
}
|