task.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /*
  24. *********************************************************************************************************
  25. * �� �� ��: void data_task(void *pdata)
  26. * ����˵��: ��Ҫ��data_task�����̣߳����ȼ��ߡ��������߼��ǽ�nandflash�е����ݽ���������ѯ��������
  27. * �� ����
  28. * �� �� ֵ: ��
  29. *********************************************************************************************************
  30. */
  31. void data_task(void *pdata)
  32. {
  33. <<<<<<< HEAD
  34. OS_CPU_SR cpu_sr;
  35. pdata = pdata;
  36. dlt645_init(100);
  37. //mmodbus_init(1);
  38. int jsonCunt = 1;
  39. char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
  40. read_file("device.txt", device_config_json);
  41. addGatewayParams(device_config_json);
  42. myfree(SRAMEX, device_config_json);
  43. GATEWAY_PARAMS *get;
  44. get= get_gateway_config_params();
  45. DEVICE_PARAMS *current_device=get->device_params;
  46. // Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
  47. char *buf = mymalloc(SRAMEX, 9 * 1024); // ���ն�ȡ������
  48. memset(buf, 0, 9 * 1024);
  49. while (current_device!=NULL)
  50. {
  51. read_device_data(current_device, buf); //��ȡ����
  52. send_mqtt(buf, jsonCunt); //��������
  53. jsonCunt = 0;
  54. memset(buf,0,strlen(buf));
  55. current_device=get->device_params;
  56. OSTimeDly(1);
  57. }
  58. myfree(SRAMEX, buf);
  59. }
  60. /*
  61. *********************************************************************************************************
  62. * �� �� ��: void mqtt_to_device()
  63. * ����˵��: �����յ������ݷ������豸
  64. * �� ��:
  65. * �� �� ֵ:
  66. *********************************************************************************************************
  67. */
  68. void mqtt_to_device(){
  69. uint8_t err;
  70. StringInfo *message;
  71. message = (StringInfo*)OSMboxPend(mqtt_recvMseeageMbox, 1000, &err);
  72. if(message != NULL) //������Ϣ
  73. {
  74. write_modbus_data(message->p); //�����
  75. myfree(SRAMEX ,message->p);//�ͷ��ڲ�����
  76. //OSTimeDly(1000);
  77. }
  78. =======
  79. OS_CPU_SR cpu_sr;
  80. pdata = pdata;
  81. mmodbus_init(1);
  82. char buf[1000]; // ����modbus��ȡ������
  83. char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
  84. read_file("device.txt", device_config_json);
  85. addGatewayParams(device_config_json);
  86. myfree(SRAMEX, device_config_json);
  87. GATEWAY_PARAMS *get;
  88. get= get_gateway_config_params();
  89. int deviceIndex=0;
  90. DEVICE_PARAMS *current_device=get->device_params;
  91. // Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
  92. while (current_device!=NULL)
  93. {
  94. uint8_t err;
  95. StringInfo *message=NULL;
  96. message=OSQPend(JsonQ,1000,&err);
  97. if(message==NULL)//没有消息
  98. {
  99. OSTimeDly(1000);
  100. }
  101. else //包含消息
  102. {
  103. myfree(SRAMIN ,message->p);//释放内部数据
  104. myfree(SRAMIN,message);
  105. OSTimeDly(1000);
  106. }
  107. // uint8_t err;
  108. // StringInfo *message=NULL; //���ն�����Ϣ�ṹ��
  109. // message=OSQPend(JsonQ,1000,&err);
  110. // if(message==NULL)
  111. // {
  112. // READ_DATA(current_device, buf);
  113. // if(current_device->nextDevice!=NULL)
  114. // {
  115. // current_device=current_device->nextDevice;
  116. // }
  117. // else
  118. // {
  119. // sprintf(buf + strlen(buf) - 1, "}");
  120. // time1 = OSTimeGet();
  121. // if( jsoncunt || time2 <= time1 - 20000)
  122. // {
  123. // memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  124. // memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//������һ�ε�����
  125. // sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
  126. // int msg = MBOX_USER_PUBLISHQOS2;
  127. // if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  128. // current_device=get->device_params;
  129. // jsoncunt = 0;
  130. // time2 = OSTimeGet();
  131. // }//20s����һ��
  132. //
  133. // else
  134. // {
  135. // if(strcmp(buf,pubJsonStringCopy))
  136. // {
  137. // memset(string, 0 , strlen(string));
  138. // findDifference(buf, pubJsonStringCopy, string);
  139. // memset(pubJsonString,0, strlen(pubJsonString));
  140. // sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
  141. // memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  142. // sprintf(pubJsonStringCopy, buf, strlen(buf));
  143. //
  144. // int msg = MBOX_USER_PUBLISHQOS2;
  145. // if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  146. // }
  147. // }
  148. // memset(buf,0,strlen(buf));
  149. // current_device=get->device_params;
  150. // }
  151. // }
  152. // else
  153. // {
  154. // WRITE_MODBUS_DATA(message->p);
  155. // printf("sx1278_task_recv : %s",message->p);
  156. // myfree(SRAMIN ,message->p);
  157. // myfree(SRAMIN,message);
  158. //
  159. // commd = 1;
  160. // }
  161. // OSTimeDly(1000);
  162. }
  163. >>>>>>> 841e7987800910a57cf5275c331a8acbfec24198
  164. }
  165. /*
  166. *********************************************************************************************************
  167. * �� �� ��: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  168. * ����˵��: ��ȡ��ǰ�ڵ��ϵ�modbus����
  169. * �� ��: DEVICE_PARAMS *device ��ǰ�豸
  170. * �� �� ֵ: 1: �ɹ� 0:ʧ��
  171. *********************************************************************************************************
  172. */
  173. int read_device_data(DEVICE_PARAMS *device, char* buf)
  174. {
  175. DEVICE_PARAMS *current_device=device;
  176. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  177. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  178. while(current_device->params != NULL)
  179. {
  180. if (current_device->protocol == MODBUS_READ)
  181. {
  182. protocol_485=1;
  183. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus�Ĵ�������
  184. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  185. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  186. {
  187. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  188. currentModbusParams->registerAddress,
  189. currentModbusParams->registerByteNum /2,
  190. data);
  191. if (success)
  192. {
  193. uint32_t value;
  194. if (currentModbusParams->registerByteNum == 4)
  195. {
  196. value = (uint32_t)data[0] | data[1];
  197. }
  198. else if (currentModbusParams->registerByteNum == 2)
  199. {
  200. value = data[0];
  201. }
  202. if (currentModbusParams->decimalPoint == 0)
  203. {
  204. currentModbusParams->value[0] = value;
  205. currentModbusParams->value[1] = value << 8;
  206. currentModbusParams->value[2] = value << 16;
  207. currentModbusParams->value[3] = value << 24;
  208. }
  209. else
  210. {
  211. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  212. memcpy(currentModbusParams->value, &convertedValue, 4);
  213. }
  214. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  215. current_device->deviceID, currentModbusParams->keyword, value);
  216. }
  217. else
  218. {
  219. printf("read modbus register fail\n");
  220. return 0;
  221. }
  222. /* ÿ����һ���Ĵ���������message�ж� */
  223. mqtt_to_device();
  224. currentModbusParams = currentModbusParams->nextParams;
  225. if (currentModbusParams == NULL)
  226. {
  227. current_device = current_device->nextDevice;
  228. currentModbusParams = current_device->params->gateway_read_modbus_command;
  229. if(current_device == NULL)
  230. {
  231. sprintf(buf + strlen(buf) - 1, "}");
  232. return 1;
  233. }
  234. }
  235. }
  236. }
  237. else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
  238. {
  239. protocol_485=2;
  240. uint8_t read_buf[10];
  241. currentDLT645Params->rxLen = 0;
  242. memset(read_buf, 0, 10);
  243. memset(currentDLT645Params->data, 0, 10);
  244. dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
  245. int8_t rs;
  246. if (current_device->protocol == DLT645_2007)
  247. {
  248. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  249. }
  250. else if (current_device->protocol == DLT645_1997)
  251. {
  252. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  253. }
  254. if (rs != -1)
  255. {
  256. if (rs <= 4)
  257. {
  258. memcpy(currentDLT645Params->data, read_buf, 4);
  259. currentDLT645Params->rxLen = 4;
  260. }
  261. else if (rs == 5)
  262. {
  263. memcpy(currentDLT645Params->data, read_buf, 5);
  264. currentDLT645Params->rxLen = 5;
  265. }
  266. else if (rs > 5)
  267. {
  268. memcpy(currentDLT645Params->data, read_buf, 9);
  269. currentDLT645Params->rxLen = 9;
  270. }
  271. sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d}",
  272. currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
  273. currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
  274. currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
  275. currentDLT645Params->deviceID645[5],currentDLT645Params->Identification);
  276. }
  277. // else
  278. // {
  279. // currentDLT645Params->rxLen = 0;
  280. // printf("read DLT current data fail\n");
  281. // }
  282. /* ÿ����һ���Ĵ���������message�ж� */
  283. mqtt_to_device();
  284. currentDLT645Params = currentDLT645Params->nextParams;
  285. if (currentDLT645Params == NULL)
  286. {
  287. current_device = current_device->nextDevice;
  288. currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  289. if(current_device == NULL)
  290. {
  291. sprintf(buf + strlen(buf) - 1, "}");
  292. return 1;
  293. }
  294. }
  295. }
  296. }
  297. return 1;
  298. }
  299. /*
  300. *********************************************************************************************************
  301. * �� �� ��:void WRITE_MODBUS_DATA(char* cJSONstring)
  302. * ����˵��: ����mqtt���ݲ�д��modbus�Ĵ���
  303. * �� �Σ�char* cJSONstring mqtt���յ�������
  304. * �� �� ֵ: ��
  305. *********************************************************************************************************
  306. */
  307. void write_modbus_data(char* cJSONstring)
  308. {
  309. uint16_t data;
  310. uint16_t number;
  311. uint16_t slaveAddress;
  312. GATEWAY_PARAMS* get;
  313. get = get_gateway_config_params();
  314. DEVICE_PARAMS* current_device = get->device_params;
  315. cJSON *root = cJSON_Parse(cJSONstring);
  316. const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
  317. const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
  318. const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
  319. const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
  320. const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
  321. while(current_device)
  322. {
  323. char* device_ID = (char*)current_device->deviceID;
  324. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  325. if(!strcmp(device_ID,deviceId))
  326. {
  327. OSTimeDly(100);
  328. if(power)
  329. {
  330. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  331. currentModbusParams->registerAddress,
  332. power->valueint);
  333. }
  334. OSTimeDly(100);
  335. if(temp)
  336. {
  337. currentModbusParams = currentModbusParams->nextParams;
  338. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  339. currentModbusParams->registerAddress,
  340. temp->valueint);
  341. }
  342. OSTimeDly(100);
  343. if(mode)
  344. {
  345. currentModbusParams = currentModbusParams->nextParams;
  346. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  347. currentModbusParams->registerAddress,
  348. mode->valueint);
  349. }
  350. OSTimeDly(100);
  351. if(fan)
  352. {
  353. currentModbusParams = currentModbusParams->nextParams;
  354. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  355. currentModbusParams->registerAddress,
  356. fan->valueint);
  357. }
  358. }
  359. current_device = current_device->nextDevice;
  360. }
  361. cJSON_Delete(root);
  362. }
  363. /*
  364. *********************************************************************************************************
  365. * �� �� ��: void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  366. * ����˵��: �Ƚϳ�����1�Ͳ���2�IJ�ͬ��
  367. * �� ��: ����1:������ ����2:������ ����3���������
  368. * �� �� ֵ: ��
  369. *********************************************************************************************************
  370. */
  371. void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  372. {
  373. const char* delimiter = "{}";
  374. char* saveptr1;
  375. char* saveptr2;
  376. char* data1 = malloc(strlen(buf) + 1);
  377. char* data2 = malloc(strlen(pubJsonStringCopy) + 1); ;
  378. memcpy(data1, buf, strlen(buf));
  379. memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
  380. // ����strtok_r�����ָ��ַ���������һ�Ƚ�
  381. char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
  382. char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
  383. while (token1 != NULL && token2 != NULL)
  384. {
  385. if (strcmp(token1, token2) != 0)
  386. {
  387. memcpy(string + strlen(string), token1, strlen(token1));
  388. }
  389. token1 = strtok_r(NULL, delimiter, &saveptr1);
  390. token2 = strtok_r(NULL, delimiter, &saveptr2);
  391. }
  392. // // �����ʣ���ַ���δ�Ƚϣ����ӡʣ���ַ���
  393. // while (token1 != NULL) {
  394. // sprintf(string + strlen(string),"%s,", token1);
  395. // token1 = strtok_r(NULL, delimiter, &saveptr1);
  396. // }
  397. // while (token2 != NULL) {
  398. // //sprintf(string + strlen(string),"{%s},", token2);
  399. // token2 = strtok_r(NULL, delimiter, &saveptr2);
  400. // }
  401. free(data1);
  402. free(data2);
  403. }
  404. /*
  405. *********************************************************************************************************
  406. * �� �� ��: void send_mqtt(char*buf, int jsonCunt)
  407. * ����˵��: �����ݷ��͵�mqtt
  408. * �� ��: ����1:��ȡ���� ����2:��һ�η��ͱ�־
  409. * �� �� ֵ: ��
  410. *********************************************************************************************************
  411. */
  412. void send_mqtt(char*buf, int jsonCunt){
  413. GATEWAY_PARAMS *get;
  414. get= get_gateway_config_params();
  415. time1 = GetCurrentTime();
  416. if(jsonCunt || time2 <= time1 - (3 * 1000)) // 20s����һ��
  417. {
  418. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  419. memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//������һ�ε�����
  420. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
  421. int msg = MBOX_USER_PUBLISHQOS0;
  422. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  423. time2 = GetCurrentTime();
  424. }
  425. else
  426. {
  427. if(strcmp(buf,pubJsonStringCopy))
  428. {
  429. memset(string, 0 , strlen(string));
  430. find_difference(buf, pubJsonStringCopy, string);
  431. memset(pubJsonString,0, strlen(pubJsonString));
  432. sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
  433. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  434. sprintf(pubJsonStringCopy, buf, strlen(buf));
  435. int msg = MBOX_USER_PUBLISHQOS0;
  436. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  437. }
  438. }
  439. }