task.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "task.h"
  2. #include "cjson.h"
  3. #include "myFile.h"
  4. #include "gateway_message.h"
  5. #include "log.h"
  6. #include "malloc.h"
  7. #include "sx1276.h"
  8. #include "protocol.h"
  9. #include "usart.h"
  10. #include "node_data_acquisition.h"
  11. #include "sys_mqtt.h"
  12. #include "sys_http.h"
  13. #include "node_message.h"
  14. #include "usart.h"
  15. #include "mmodbus.h"
  16. #include "sys_mqtt.h"
  17. #include "gateway_message.h"
  18. #include "MQTTClient.h"
  19. void master_task(uint8_t *string,uint16_t stringlength);
  20. void slave_task();
  21. int READ_MODBUS_DATA(DEVICE_PARAMS *current_device);
  22. uint16_t BufferSize;
  23. uint8_t Buffer[256];
  24. uint32_t rx_num = 0;
  25. uint8_t PingMsg[] = "PING\0";
  26. uint8_t PongMsg[] = "PONG\0";
  27. tRadioDriver *Radio=NULL;
  28. /*
  29. *********************************************************************************************************
  30. * 函 数 名: void data_task(void *pdata)
  31. * 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
  32. * 形 参:无
  33. * 返 回 值: 无
  34. *********************************************************************************************************
  35. */
  36. void data_task(void *pdata)
  37. {
  38. OS_CPU_SR cpu_sr;
  39. pdata = pdata;
  40. // Radio = RadioDriverInit();
  41. // Radio->Init();
  42. // mmodbus_init(1);
  43. char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
  44. read_file("device.txt", device_config_json);
  45. addGatewayParams(device_config_json);
  46. myfree(SRAMEX, device_config_json);
  47. // GATEWAY_PARAMS *get;
  48. // get= get_gateway_config_params();
  49. // int deviceIndex=0;
  50. // DEVICE_PARAMS *current_device=get->device_params;
  51. //
  52. // uint32_t baudrate = get->baudrate;
  53. // uint8_t databits = get->dataBits;
  54. // uint8_t stopbits = get->stopBit;
  55. // uint8_t parity = get->parity;
  56. // uint8_t flowcontrol = get->flowControl;
  57. // Config_485_Port(baudrate, databits, stopbits, parity, flowcontrol);
  58. //
  59. // uint8_t string[256];
  60. // uint16_t bufferLength;
  61. // OS_Q_DATA Qnum;
  62. // StringInfo message;
  63. // char *mqttRecv;
  64. // uint8_t err;
  65. // while (current_device!=NULL)
  66. // {
  67. // READ_MODBUS_DATA(current_device);
  68. // if(current_device->nextDevice!=NULL)
  69. // {
  70. // current_device=current_device->nextDevice;
  71. // }
  72. // else
  73. // {
  74. // int msg = MBOX_USER_PUBLISHQOS2;
  75. // if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  76. // memset(pubJsonString,0,strlen(pubJsonString));
  77. // current_device=get->device_params;
  78. // }
  79. // }
  80. }
  81. #if 0 //测试代码不经过转发直接进行相应的解析
  82. SlaveProtocolAnalysis(string,bufferLength);
  83. data_acquisition();
  84. uint8_t node_string[256];
  85. uint16_t node_string_Length;
  86. nodeSendReaddValue(node_string,&node_string_Length);
  87. GatewayProtocolAnalysis(node_string,node_string_Length);
  88. #endif
  89. int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  90. {
  91. DEVICE_PARAMS *current_device=device;
  92. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  93. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  94. while(current_device->params != NULL)
  95. {
  96. if (current_device->protocol == MODBUS_READ)
  97. {
  98. protocol_485=1;
  99. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
  100. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  101. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  102. {
  103. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  104. currentModbusParams->registerAddress,
  105. currentModbusParams->registerByteNum /2,
  106. data);
  107. if (success)
  108. {
  109. uint32_t value;
  110. if (currentModbusParams->registerByteNum == 4)
  111. {
  112. value = (uint32_t)data[0] | data[1];
  113. }
  114. else if (currentModbusParams->registerByteNum == 2)
  115. {
  116. value = data[0];
  117. }
  118. if (currentModbusParams->decimalPoint == 0)
  119. {
  120. currentModbusParams->value[0] = value;
  121. currentModbusParams->value[1] = value << 8;
  122. currentModbusParams->value[2] = value << 16;
  123. currentModbusParams->value[3] = value << 24;
  124. }
  125. else
  126. {
  127. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  128. memcpy(currentModbusParams->value, &convertedValue, 4);
  129. }
  130. sprintf(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%d}]},",
  131. current_device->deviceID, currentModbusParams->keyword, value);
  132. currentModbusParams = currentModbusParams->nextParams;
  133. if (currentModbusParams == NULL)
  134. {
  135. sprintf(pubJsonString + strlen(pubJsonString) - 1, "");
  136. return 0;
  137. }
  138. }
  139. }
  140. }
  141. else if (current_device->protocol == MODBUS_WRITE)
  142. {
  143. protocol_485=1;
  144. }
  145. else if (current_device->protocol == DLT645_07 || current_device->protocol == DLT645_97)
  146. {
  147. protocol_485=2;
  148. uint8_t read_buf[10];
  149. currentDLT645Params->rxLen = 0;
  150. memset(read_buf, 0, 10);
  151. memset(currentDLT645Params->data, 0, 10);
  152. dlt645_set_addr(&dlt645, current_device->params->gateway_read_dlt645_command->deviceID645);
  153. int8_t rs;
  154. if (current_device->protocol == DLT645_07)
  155. {
  156. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  157. }
  158. else if (current_device->protocol == DLT645_97)
  159. {
  160. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  161. }
  162. if (rs != -1)
  163. {
  164. if (rs <= 4)
  165. {
  166. memcpy(currentDLT645Params->data, read_buf, 4);
  167. currentDLT645Params->rxLen = 4;
  168. }
  169. else if (rs == 5)
  170. {
  171. memcpy(currentDLT645Params->data, read_buf, 5);
  172. currentDLT645Params->rxLen = 5;
  173. }
  174. else if (rs > 5)
  175. {
  176. memcpy(currentDLT645Params->data, read_buf, 9);
  177. currentDLT645Params->rxLen = 9;
  178. }
  179. }
  180. else
  181. {
  182. currentDLT645Params->rxLen =0;
  183. }
  184. sprintf(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%s},]}",
  185. current_device->deviceID, currentDLT645Params->keyword, currentDLT645Params->data);
  186. currentDLT645Params = currentDLT645Params->nextParams;
  187. if (currentDLT645Params == NULL)
  188. {
  189. return 0;
  190. }
  191. }
  192. }
  193. return 1;
  194. }
  195. void WRITE_MODBUS_DATA(char* buf){
  196. GATEWAY_PARAMS* get;
  197. get = get_gateway_config_params();
  198. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = get->device_params->params->gateway_write_modbus_command;
  199. value data;
  200. data.PowerValue = parseIntField(buf, "\"power\":");
  201. data.TempValue = parseIntField(buf, "\"temp\":");
  202. data.ModeValue = parseIntField(buf, "\"mode\":");
  203. data.FanValue = parseIntField(buf, "\"fan\":");
  204. data.RoomTempValue = parseIntField(buf, "\"roomTemp\":");
  205. uint16_t slaveAddress;
  206. uint16_t number;
  207. if(data.PowerValue){
  208. slaveAddress = currentModbusParams->slaveAddress;
  209. number =currentModbusParams->registerAddress;
  210. mmodbus_writeHoldingRegister16i(slaveAddress, number, data.PowerValue);
  211. }
  212. if(data.TempValue){
  213. currentModbusParams = currentModbusParams->nextParams;
  214. slaveAddress = currentModbusParams->slaveAddress;
  215. number =currentModbusParams->registerAddress;
  216. mmodbus_writeHoldingRegister16i(slaveAddress, number, data.TempValue);
  217. }
  218. if(data.ModeValue){
  219. currentModbusParams = currentModbusParams->nextParams;
  220. slaveAddress = currentModbusParams->slaveAddress;
  221. number =currentModbusParams->registerAddress;
  222. mmodbus_writeHoldingRegister16i(slaveAddress, number, data.ModeValue);
  223. }
  224. if(data.FanValue){
  225. currentModbusParams = currentModbusParams->nextParams;
  226. slaveAddress = currentModbusParams->slaveAddress;
  227. number =currentModbusParams->registerAddress;
  228. mmodbus_writeHoldingRegister16i(slaveAddress, number, data.FanValue);
  229. }
  230. if(data.RoomTempValue){
  231. currentModbusParams = currentModbusParams->nextParams;
  232. slaveAddress = currentModbusParams->slaveAddress;
  233. number =currentModbusParams->registerAddress;
  234. mmodbus_writeHoldingRegister16i(slaveAddress, number, data.RoomTempValue);
  235. }
  236. }