task.c 14 KB

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