task.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 "dlt645.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. #include "cJSON.h"
  20. #include "time_count.h"
  21. #include "dlt645_1997_private.h"
  22. char string[512];
  23. uint8_t read_cnt = 0;
  24. uint8_t count = 0;
  25. uint8_t jsonCunt = 1;
  26. /*
  27. *********************************************************************************************************
  28. * 函 数 �: void data_task(void *pdata)
  29. * 功能说明: 主�是data_task处�线程,优先级高。其�行逻辑是将nandflash中的数�解�出�轮询��数�
  30. * 形 �:无
  31. * 返 回 值: 无
  32. *********************************************************************************************************
  33. */
  34. void data_task(void *pdata)
  35. {
  36. OS_CPU_SR cpu_sr;
  37. pdata = pdata;
  38. dlt645_init(100);
  39. mmodbus_init(1);
  40. char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
  41. read_file("device.txt", device_config_json);
  42. addGatewayParams(device_config_json);
  43. myfree(SRAMEX, device_config_json);
  44. GATEWAY_PARAMS *get;
  45. get= get_gateway_config_params();
  46. DEVICE_PARAMS *current_device=get->device_params;
  47. // Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
  48. <<<<<<< HEAD
  49. char *buf = mymalloc(SRAMEX, 9 * 1024); // ½ÓÊÕ¶ÁÈ¡µÄÊý¾Ý
  50. memset(buf, 0, 9 * 1024);
  51. while (current_device!=NULL)
  52. {
  53. time1 = GetCurrentTime();
  54. if(mqtt_connectFlag)
  55. {
  56. if(jsonCunt || time2 <= time1 - (10 * 1000))// 10s½øÐÐÒ»´ÎÈ«Êý¾Ý·¢ËÍ
  57. {
  58. read_device_data1(current_device, buf);
  59. send_mqtt(buf);
  60. jsonCunt = 0;
  61. memset(buf,0,strlen(buf));
  62. current_device=get->device_params;
  63. time2 = GetCurrentTime();
  64. // LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "data for all");
  65. }
  66. else
  67. {
  68. read_device_data2(current_device, buf);
  69. if(count > 0)// count¼ì²âbufÄÚÊÇ·ñº¬ÓÐÊý¾Ý
  70. {
  71. send_mqtt(buf);
  72. memset(buf,0,strlen(buf));
  73. current_device=get->device_params;
  74. count = 0;
  75. // LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "different data");
  76. }
  77. }
  78. }
  79. OSTimeDly(100);
  80. =======
  81. char *buf = mymalloc(SRAMEX, 9 * 1024); // 接收读�的数�
  82. memset(buf, 0, 9 * 1024);
  83. while (current_device!=NULL)
  84. {
  85. read_device_data(current_device, buf); //读�数�
  86. send_mqtt(buf, jsonCunt); //��数�
  87. jsonCunt = 0;
  88. memset(buf,0,strlen(buf));
  89. current_device=get->device_params;
  90. OSTimeDly(1);
  91. >>>>>>> d12097fdeb13131a2d24b22a2181d1fbd191264d
  92. }
  93. myfree(SRAMEX, buf);
  94. }
  95. /*
  96. *********************************************************************************************************
  97. * 函 数 �: void mqtt_to_device()
  98. * 功能说明: 将接收到的数���至设备
  99. * 形 �:
  100. * 返 回 值:
  101. *********************************************************************************************************
  102. */
  103. void mqtt_to_device(){
  104. uint8_t err;
  105. StringInfo *message;
  106. message = (StringInfo*)OSMboxPend(mqtt_recvMseeageMbox, 1000, &err);
  107. if(message != NULL) //包�消�
  108. {
  109. write_modbus_data(message->p); //写入数�
  110. myfree(SRAMEX ,message->p);//释放内部数�
  111. //OSTimeDly(1000);
  112. }
  113. }
  114. void find_diff(char* buf, char* string) {
  115. }
  116. /*
  117. *********************************************************************************************************
  118. * 函 数 �: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  119. * 功能说明: 读�当�节点上的modbus数�
  120. * 形 �: DEVICE_PARAMS *device 当�设备
  121. * 返 回 值: 1: �功 0:失败
  122. *********************************************************************************************************
  123. */
  124. int read_device_data1(DEVICE_PARAMS *device, char* buf)
  125. {
  126. DEVICE_PARAMS *current_device=device;
  127. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  128. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  129. while(current_device->params != NULL)
  130. {
  131. if (current_device->protocol == MODBUS_READ)
  132. {
  133. protocol_485=1;
  134. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
  135. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  136. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  137. {
  138. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  139. currentModbusParams->registerAddress,
  140. currentModbusParams->registerByteNum /2,
  141. data);
  142. if (success)
  143. {
  144. uint32_t value;
  145. if (currentModbusParams->registerByteNum == 4)
  146. {
  147. value = (uint32_t)data[0] | data[1];
  148. }
  149. else if (currentModbusParams->registerByteNum == 2)
  150. {
  151. value = data[0];
  152. }
  153. if (currentModbusParams->decimalPoint == 0)
  154. {
  155. currentModbusParams->value = value;
  156. }
  157. else
  158. {
  159. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  160. currentModbusParams->value=convertedValue;
  161. }
  162. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  163. current_device->deviceID, currentModbusParams->keyword, value);
  164. }
  165. // else
  166. // {
  167. // printf("read modbus register fail\n");
  168. // return 0;
  169. // }
  170. /* �读完一个寄存器,进行message判断 */
  171. mqtt_to_device();
  172. currentModbusParams = currentModbusParams->nextParams;
  173. if (currentModbusParams == NULL)
  174. {
  175. current_device = current_device->nextDevice;
  176. currentModbusParams = current_device->params->gateway_read_modbus_command;
  177. if(current_device == NULL)
  178. {
  179. sprintf(buf + strlen(buf) - 1, "");
  180. return 1;
  181. }
  182. }
  183. }
  184. }
  185. else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
  186. {
  187. protocol_485=2;
  188. uint8_t read_buf[10];
  189. uint32_t dltValue;
  190. currentDLT645Params->rxLen = 0;
  191. memset(read_buf, 0, 10);
  192. memset(currentDLT645Params->data, 0, 10);
  193. dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
  194. int8_t rs;
  195. if (current_device->protocol == DLT645_2007)
  196. {
  197. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  198. }
  199. else if (current_device->protocol == DLT645_1997)
  200. {
  201. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  202. }
  203. if (rs != -1)
  204. {
  205. if (rs <= 4)
  206. {
  207. memcpy(currentDLT645Params->data, read_buf, 4);
  208. currentDLT645Params->rxLen = 4;
  209. }
  210. else if (rs == 5)
  211. {
  212. memcpy(currentDLT645Params->data, read_buf, 5);
  213. currentDLT645Params->rxLen = 5;
  214. }
  215. else if (rs > 5)
  216. {
  217. memcpy(currentDLT645Params->data, read_buf, 9);
  218. currentDLT645Params->rxLen = 9;
  219. }
  220. dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
  221. currentDLT645Params->data[2] << 8 | currentDLT645Params->data[3];
  222. sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
  223. currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
  224. currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
  225. currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
  226. currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
  227. }
  228. // else
  229. // {
  230. // currentDLT645Params->rxLen = 0;
  231. // printf("read DLT current data fail\n");
  232. <<<<<<< HEAD
  233. // }
  234. =======
  235. // }
  236. /* �读完一个寄存器,进行message判断 */
  237. mqtt_to_device();
  238. >>>>>>> d12097fdeb13131a2d24b22a2181d1fbd191264d
  239. currentDLT645Params = currentDLT645Params->nextParams;
  240. if (currentDLT645Params == NULL)
  241. {
  242. current_device = current_device->nextDevice;
  243. currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  244. if(current_device == NULL)
  245. {
  246. sprintf(buf + strlen(buf) - 1, "");
  247. return 1;
  248. }
  249. }
  250. }
  251. }
  252. return 1;
  253. }
  254. int read_device_data2(DEVICE_PARAMS *device, char* buf)
  255. {
  256. DEVICE_PARAMS *current_device=device;
  257. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  258. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  259. while(current_device->params != NULL)
  260. {
  261. if (current_device->protocol == MODBUS_READ)
  262. {
  263. protocol_485=1;
  264. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus¼Ä´æÆ÷³¤¶È
  265. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  266. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  267. {
  268. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  269. currentModbusParams->registerAddress,
  270. currentModbusParams->registerByteNum /2,
  271. data);
  272. if (success)
  273. {
  274. uint32_t value;
  275. if (currentModbusParams->registerByteNum == 4)
  276. {
  277. value = (uint32_t)data[0] | data[1];
  278. }
  279. else if (currentModbusParams->registerByteNum == 2)
  280. {
  281. value = data[0];
  282. }
  283. if((value - currentModbusParams->value) != 0)
  284. {
  285. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  286. current_device->deviceID, currentModbusParams->keyword, value);
  287. count++;
  288. }
  289. if (currentModbusParams->decimalPoint == 0)
  290. {
  291. currentModbusParams->value = value;
  292. }
  293. else
  294. {
  295. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  296. currentModbusParams->value=convertedValue;
  297. }
  298. }
  299. currentModbusParams = currentModbusParams->nextParams;
  300. if (currentModbusParams == NULL)
  301. {
  302. current_device = current_device->nextDevice;
  303. currentModbusParams = current_device->params->gateway_read_modbus_command;
  304. if(current_device == NULL)
  305. {
  306. sprintf(buf + strlen(buf) - 1, "");
  307. return 1;
  308. }
  309. }
  310. }
  311. }
  312. else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
  313. {
  314. protocol_485=2;
  315. uint8_t read_buf[10];
  316. uint32_t dltValue;
  317. currentDLT645Params->rxLen = 0;
  318. memset(read_buf, 0, 10);
  319. memset(currentDLT645Params->data, 0, 10);
  320. dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
  321. int8_t rs;
  322. if (current_device->protocol == DLT645_2007)
  323. {
  324. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  325. }
  326. else if (current_device->protocol == DLT645_1997)
  327. {
  328. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  329. }
  330. if (rs != -1)
  331. {
  332. if (rs <= 4)
  333. {
  334. memcpy(currentDLT645Params->data, read_buf, 4);
  335. currentDLT645Params->rxLen = 4;
  336. }
  337. else if (rs == 5)
  338. {
  339. memcpy(currentDLT645Params->data, read_buf, 5);
  340. currentDLT645Params->rxLen = 5;
  341. }
  342. else if (rs > 5)
  343. {
  344. memcpy(currentDLT645Params->data, read_buf, 9);
  345. currentDLT645Params->rxLen = 9;
  346. }
  347. dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
  348. currentDLT645Params->data[2] << 8 | currentDLT645Params->data[3];
  349. sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
  350. currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
  351. currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
  352. currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
  353. currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
  354. }
  355. currentDLT645Params = currentDLT645Params->nextParams;
  356. if (currentDLT645Params == NULL)
  357. {
  358. current_device = current_device->nextDevice;
  359. currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  360. if(current_device == NULL)
  361. {
  362. sprintf(buf + strlen(buf) - 1, "");
  363. return 1;
  364. }
  365. }
  366. }
  367. }
  368. return 1;
  369. }
  370. /*
  371. *********************************************************************************************************
  372. * 函 数 �:void WRITE_MODBUS_DATA(char* cJSONstring)
  373. * 功能说明: 接收mqtt数�并写入modbus寄存器
  374. * 形 �:char* cJSONstring mqtt接收到的数�
  375. * 返 回 值: 无
  376. *********************************************************************************************************
  377. */
  378. void write_modbus_data(char* cJSONstring)
  379. {
  380. GATEWAY_PARAMS* get;
  381. get = get_gateway_config_params();
  382. DEVICE_PARAMS* current_device = get->device_params;
  383. /* ÀûÓÃcJSOn_Parse½âÎöÊý¾Ý£¬»ñÈ¡¸÷ÀàÐÍÊý¾Ý */
  384. cJSON *root = cJSON_Parse(cJSONstring);
  385. const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
  386. const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
  387. const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
  388. const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
  389. const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
  390. while(current_device)
  391. {
  392. char* device_ID = (char*)current_device->deviceID;
  393. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  394. if(!strcmp(device_ID,deviceId)) //Æ¥ÅäID
  395. {
  396. OSTimeDly(100);
  397. /* дÈë¼Ä´æÆ÷²Ù×÷ */
  398. if(power)
  399. {
  400. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  401. currentModbusParams->registerAddress,
  402. power->valueint);
  403. }
  404. OSTimeDly(100);
  405. if(temp)
  406. {
  407. currentModbusParams = currentModbusParams->nextParams;
  408. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  409. currentModbusParams->registerAddress,
  410. temp->valueint);
  411. }
  412. OSTimeDly(100);
  413. if(mode)
  414. {
  415. currentModbusParams = currentModbusParams->nextParams;
  416. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  417. currentModbusParams->registerAddress,
  418. mode->valueint);
  419. }
  420. OSTimeDly(100);
  421. if(fan)
  422. {
  423. currentModbusParams = currentModbusParams->nextParams;
  424. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  425. currentModbusParams->registerAddress,
  426. fan->valueint);
  427. }
  428. }
  429. current_device = current_device->nextDevice;
  430. }
  431. cJSON_Delete(root);
  432. }
  433. /*
  434. *********************************************************************************************************
  435. * 函 数 �: void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  436. * 功能说明: 比较出�数1和�数2的��处
  437. * 形 �: �数1:新数� �数2:旧数� �数3:输出�数
  438. * 返 回 值: 无
  439. *********************************************************************************************************
  440. */
  441. void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  442. {
  443. const char* delimiter = "{}";
  444. char* saveptr1;
  445. char* saveptr2;
  446. char* data1 = mymalloc(SRAMEX, strlen(buf));
  447. char* data2 = mymalloc(SRAMEX, strlen(pubJsonStringCopy));
  448. memcpy(data1, buf, strlen(buf));
  449. memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
  450. // 利用strtok_r函数分割字符串,并�一比较
  451. char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
  452. char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
  453. while (token1 != NULL && token2 != NULL)
  454. {
  455. if (strcmp(token1, token2) != 0)
  456. {
  457. memcpy(string + strlen(string), token1, strlen(token1));
  458. }
  459. token1 = strtok_r(NULL, delimiter, &saveptr1);
  460. token2 = strtok_r(NULL, delimiter, &saveptr2);
  461. }
  462. // // 如果有剩余字符串未比较,则打�剩余字符串
  463. // while (token1 != NULL) {
  464. // sprintf(string + strlen(string),"%s,", token1);
  465. // token1 = strtok_r(NULL, delimiter, &saveptr1);
  466. // }
  467. // while (token2 != NULL) {
  468. // //sprintf(string + strlen(string),"{%s},", token2);
  469. // token2 = strtok_r(NULL, delimiter, &saveptr2);
  470. // }
  471. myfree(SRAMEX, data1);
  472. myfree(SRAMEX, data2);
  473. }
  474. /*
  475. *********************************************************************************************************
  476. * 函 数 �: void send_mqtt(char*buf, int jsonCunt)
  477. * 功能说明: 将数���到mqtt
  478. * 形 �: �数1:读�数� �数2:第一次��标志
  479. * 返 回 值: 无
  480. *********************************************************************************************************
  481. */
  482. void send_mqtt(char*buf){
  483. GATEWAY_PARAMS *get;
  484. get= get_gateway_config_params();
  485. <<<<<<< HEAD
  486. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]}",get->deviceId, buf); // ×é³ÉÒª·¢Ë͵ÄjsonÓï¾ä
  487. int msg = MBOX_USER_PUBLISHQOS0;
  488. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  489. =======
  490. time1 = GetCurrentTime();
  491. if(jsonCunt || time2 <= time1 - (3 * 1000)) // 20s进行一次
  492. {
  493. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  494. memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数�
  495. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
  496. int msg = MBOX_USER_PUBLISHQOS0;
  497. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  498. time2 = GetCurrentTime();
  499. }
  500. else
  501. {
  502. if(strcmp(buf,pubJsonStringCopy))
  503. {
  504. memset(string, 0 , strlen(string));
  505. find_difference(buf, pubJsonStringCopy, string);
  506. memset(pubJsonString,0, strlen(pubJsonString));
  507. sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
  508. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  509. sprintf(pubJsonStringCopy, buf, strlen(buf));
  510. int msg = MBOX_USER_PUBLISHQOS0;
  511. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  512. }
  513. }
  514. >>>>>>> d12097fdeb13131a2d24b22a2181d1fbd191264d
  515. }