data_task.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include "data_task.h"
  2. #include "usart.h"
  3. #include "sys_mqtt.h"
  4. #include "sys_http.h"
  5. #include "mmodbus.h"
  6. #include "dlt645.h"
  7. #include "dlt645_1997_private.h"
  8. #include "dlt645_port.h"
  9. #include "gd32_flash.h"
  10. #include "protocol.h"
  11. #include "sys_http.h"
  12. #include "jsmn.h"
  13. #include "timer.h"
  14. uint8_t time_count = 0, jsonCunt = 0, count = 0;
  15. uint8_t protocol_485;
  16. int time1,time2;
  17. int ID = 0;
  18. void data_task(void *pdata)
  19. {
  20. dlt645_init(1);
  21. mmodbus_init(1);
  22. while(!load_http_config)
  23. {
  24. vTaskDelay(100);
  25. }
  26. portENTER_CRITICAL();
  27. char *device_config_json=pvPortMalloc( 10 *1024 );
  28. read_data_from_flash(device_config_json);
  29. addGatewayParams(device_config_json);
  30. vPortFree(device_config_json);
  31. device_config_json = NULL;
  32. portEXIT_CRITICAL();
  33. GATEWAY_PARAMS *get;
  34. get= get_gateway_config_params();
  35. DEVICE_PARAMS *current_device=get->device_params;
  36. char *buf = pvPortMalloc(1024); // 接收读取的数据
  37. char *string = pvPortMalloc(1024); // 接收读取的数据
  38. memset(buf,0,strlen(buf));
  39. memset(string,0,strlen(string));
  40. while (current_device!=NULL)
  41. {
  42. time1 = GetCurrentTime();
  43. if(mqtt_connectFlag)
  44. {
  45. if(jsonCunt && time2 <= time1 - ( 10 * 1000))// 60s进行一次全数据发送
  46. {
  47. read_device_data(current_device, buf, string);
  48. send_mqtt(buf);
  49. memset(buf,0,strlen(buf));
  50. time2 = GetCurrentTime();
  51. current_device=get->device_params;
  52. }
  53. else
  54. {
  55. read_device_data(current_device, buf, string);
  56. if(count > 0)// count检测是否含有数据
  57. {
  58. send_mqtt(string);
  59. memset(string,0,strlen(string));
  60. current_device=get->device_params;
  61. count = 0;
  62. if(!time_count)
  63. {
  64. jsonCunt = 1;
  65. time_count = 1;
  66. time2 = GetCurrentTime();
  67. }
  68. }
  69. }
  70. memset(buf,0,strlen(buf));
  71. }
  72. vTaskDelay(100);
  73. }
  74. vPortFree(buf);
  75. buf = NULL;
  76. }
  77. /*
  78. *********************************************************************************************************
  79. * 函 数 名: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  80. * 功能说明: 读取当前节点上的modbus数据
  81. * 形 参: DEVICE_PARAMS *device 当前设备
  82. * 返 回 值: 1: 成功 0:失败
  83. *********************************************************************************************************
  84. */
  85. int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
  86. {
  87. DEVICE_PARAMS *current_device=device;
  88. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  89. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  90. while(current_device->params != NULL)
  91. {
  92. if (current_device->protocol == MODBUS_READ)
  93. {
  94. protocol_485=1;
  95. uint8_t state;
  96. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
  97. uint8_t data1[currentModbusParams->registerByteNum /2];
  98. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  99. // 读水阀状态
  100. if(currentModbusParams->functionCode == 0x01)
  101. {
  102. bool success = mmodbus_readCoil(currentModbusParams->slaveAddress,
  103. currentModbusParams->registerByteNum /2,
  104. data1);
  105. if(success)
  106. {
  107. uint8_t value;
  108. value = data1[0];
  109. if(value == 0)
  110. {
  111. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":close},",
  112. current_device->deviceID, currentModbusParams->keyword);
  113. }else{
  114. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":open},",
  115. current_device->deviceID, currentModbusParams->keyword);
  116. }
  117. }
  118. currentModbusParams = currentModbusParams->nextParams;
  119. if (currentModbusParams == NULL)
  120. {
  121. current_device = current_device->nextDevice;
  122. currentModbusParams = current_device->params->gateway_read_modbus_command;
  123. if(current_device == NULL)
  124. {
  125. sprintf(buf + strlen(buf) - 1, "");
  126. return 1;
  127. }
  128. }
  129. }
  130. // 读单个寄存器
  131. if (currentModbusParams->functionCode == 0x03)
  132. {
  133. // bool success = mmodbus_readHoldingRegisters16i(0x17,0x00,0x02,data);
  134. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  135. currentModbusParams->registerAddress,
  136. currentModbusParams->registerByteNum /2,
  137. data);
  138. if (success)
  139. {
  140. uint32_t value;
  141. if (currentModbusParams->registerByteNum == 4)
  142. {
  143. value = (uint32_t)data[0] | data[1];
  144. }
  145. else if (currentModbusParams->registerByteNum == 2)
  146. {
  147. value = data[0];
  148. }
  149. if((value - currentModbusParams->value) != 0)
  150. {
  151. count++;
  152. sprintf(string + strlen(string), "{\"deviceId\":\"%s\",\"%s\":%d},",
  153. current_device->deviceID, currentModbusParams->keyword, value);
  154. }
  155. else
  156. {
  157. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  158. current_device->deviceID, currentModbusParams->keyword, value);
  159. }
  160. if (currentModbusParams->decimalPoint == 0)
  161. {
  162. currentModbusParams->value = value;
  163. }
  164. else
  165. {
  166. // float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  167. // currentModbusParams->value=convertedValue;
  168. }
  169. }
  170. currentModbusParams = currentModbusParams->nextParams;
  171. if (currentModbusParams == NULL)
  172. {
  173. current_device = current_device->nextDevice;
  174. currentModbusParams = current_device->params->gateway_read_modbus_command;
  175. if(current_device == NULL)
  176. {
  177. sprintf(buf + strlen(buf) - 1, "");
  178. sprintf(string + strlen(string) - 1, "");
  179. return 1;
  180. }
  181. }
  182. }
  183. // 开关水阀
  184. if(currentModbusParams->functionCode == 0x05)
  185. {
  186. bool success = mmodbus_writeCoil(currentModbusParams->slaveAddress,
  187. currentModbusParams->registerByteNum /2,
  188. state);
  189. if(success)
  190. {
  191. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": success},",
  192. current_device->deviceID, currentModbusParams->keyword);
  193. }
  194. else{
  195. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": fail},",
  196. current_device->deviceID, currentModbusParams->keyword);
  197. }
  198. currentModbusParams = currentModbusParams->nextParams;
  199. if (currentModbusParams == NULL)
  200. {
  201. current_device = current_device->nextDevice;
  202. currentModbusParams = current_device->params->gateway_read_modbus_command;
  203. if(current_device == NULL)
  204. {
  205. sprintf(buf + strlen(buf) - 1, "");
  206. return 1;
  207. }
  208. }
  209. }
  210. // 写单个寄存器
  211. if(currentModbusParams->functionCode == 0x06)
  212. {
  213. bool success = mmodbus_writeHoldingRegisters16i(currentModbusParams->slaveAddress,
  214. currentModbusParams->registerAddress,
  215. currentModbusParams->registerByteNum /2,
  216. data);
  217. if(success)
  218. {
  219. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":write success},",
  220. current_device->deviceID, currentModbusParams->keyword);
  221. }
  222. currentModbusParams = currentModbusParams->nextParams;
  223. if (currentModbusParams == NULL)
  224. {
  225. current_device = current_device->nextDevice;
  226. currentModbusParams = current_device->params->gateway_read_modbus_command;
  227. if(current_device == NULL)
  228. {
  229. sprintf(buf + strlen(buf) - 1, "");
  230. return 1;
  231. }
  232. }
  233. }
  234. }
  235. else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
  236. {
  237. protocol_485=2;
  238. uint8_t read_buf[10];
  239. uint32_t dltValue;
  240. currentDLT645Params->rxLen = 0;
  241. memset(read_buf, 0, 10);
  242. memset(currentDLT645Params->data, 0, 10);
  243. dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
  244. int8_t rs;
  245. if (current_device->protocol == DLT645_2007)
  246. {
  247. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  248. }
  249. else if (current_device->protocol == DLT645_1997)
  250. {
  251. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  252. }
  253. if (rs != -1)
  254. {
  255. if (rs <= 4)
  256. {
  257. memcpy(currentDLT645Params->data, read_buf, 4);
  258. currentDLT645Params->rxLen = 4;
  259. }
  260. else if (rs == 5)
  261. {
  262. memcpy(currentDLT645Params->data, read_buf, 5);
  263. currentDLT645Params->rxLen = 5;
  264. }
  265. else if (rs > 5)
  266. {
  267. memcpy(currentDLT645Params->data, read_buf, 9);
  268. currentDLT645Params->rxLen = 9;
  269. }
  270. dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
  271. currentDLT645Params->data[2] << 8 | currentDLT645Params->data[3];
  272. sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
  273. currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
  274. currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
  275. currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
  276. currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
  277. count++;
  278. }
  279. currentDLT645Params = currentDLT645Params->nextParams;
  280. if (currentDLT645Params == NULL)
  281. {
  282. current_device = current_device->nextDevice;
  283. currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  284. if(current_device == NULL)
  285. {
  286. sprintf(buf + strlen(buf) - 1, "");
  287. return 1;
  288. }
  289. }
  290. }
  291. }
  292. return 1;
  293. }
  294. /*
  295. *********************************************************************************************************
  296. * 函 数 名: void send_mqtt(char*buf, int jsonCunt)
  297. * 功能说明: 将数据发送到mqtt
  298. * 形 参: 参数1:读取数据 参数2:第一次发送标志
  299. * 返 回 值: 无
  300. *********************************************************************************************************
  301. */
  302. void send_mqtt(char*buf){
  303. GATEWAY_PARAMS *get;
  304. get= get_gateway_config_params();
  305. // char *pubJsonString = pvPortMalloc(strlen(buf));
  306. char pubJsonString[500];
  307. sprintf(pubJsonString,"ID: %d {\"DEVICEID\":\"%s\",\"data\":[%s]}",ID++, get->deviceId, buf); // 组成要发送的json语句
  308. mqtt_qos_t qos = QOS0;
  309. uint16_t pub_length = strlen(pubJsonString);
  310. mqtt_publish_data(pubJsonString, qos, pub_length, (char*)&get->messageTopic);
  311. // vPortFree(pubJsonString);
  312. // pubJsonString = NULL;
  313. }
  314. /*
  315. *********************************************************************************************************
  316. * 函 数 名:void WRITE_MODBUS_DATA(char* cJSONstring)
  317. * 功能说明: 接收mqtt数据并写入modbus寄存器
  318. * 形 参:char* cJSONstring mqtt接收到的数据
  319. * 返 回 值: 无
  320. *********************************************************************************************************
  321. */
  322. void write_modbus_data(char* JSON_STRING)
  323. {
  324. JSON_CMD jsonMsg;
  325. GATEWAY_PARAMS* get;
  326. get = get_gateway_config_params();
  327. DEVICE_PARAMS* current_device = get->device_params;
  328. parseStringField(JSON_STRING, "\"deviceId\":\"", jsonMsg.deviceId);
  329. jsonMsg.function = parseIntField(JSON_STRING, "\"function\":");
  330. jsonMsg.cmd = parseIntField(JSON_STRING, "\"cmd\":");
  331. jsonMsg.power = parseIntField(JSON_STRING, "\"power\":");
  332. jsonMsg.temp = parseIntField(JSON_STRING, "\"temp\":");
  333. jsonMsg.mode = parseIntField(JSON_STRING, "\"mode\":");
  334. jsonMsg.fan = parseIntField(JSON_STRING, "\"fan\":");
  335. while(current_device)
  336. {
  337. char* device_ID = (char*)current_device->deviceID;
  338. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  339. if(!strcmp(device_ID,jsonMsg.deviceId)) //匹配ID
  340. {
  341. delay_ms(200);
  342. if(jsonMsg.function == 5)
  343. // 开关阀门
  344. {
  345. mmodbus_writeCoil(jsonMsg.slaveAddress,jsonMsg.registerAddress,jsonMsg.cmd);
  346. }
  347. if(jsonMsg.function == 6)
  348. {
  349. /* 写入寄存器操作 */
  350. if(jsonMsg.power)
  351. {
  352. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  353. currentModbusParams->registerAddress,
  354. jsonMsg.power);
  355. }
  356. delay_ms(100);
  357. if(jsonMsg.temp)
  358. {
  359. currentModbusParams = currentModbusParams->nextParams;
  360. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  361. currentModbusParams->registerAddress,
  362. jsonMsg.temp);
  363. }
  364. delay_ms(100);
  365. if(jsonMsg.mode)
  366. {
  367. currentModbusParams = currentModbusParams->nextParams;
  368. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  369. currentModbusParams->registerAddress,
  370. jsonMsg.mode);
  371. }
  372. delay_ms(100);
  373. if(jsonMsg.fan)
  374. {
  375. currentModbusParams = currentModbusParams->nextParams;
  376. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  377. currentModbusParams->registerAddress,
  378. jsonMsg.fan);
  379. }
  380. delay_ms(100);
  381. }
  382. }
  383. current_device = current_device->nextDevice;
  384. }
  385. }