task.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include "cJSON.h"
  20. #include "time_count.h"
  21. int READ_DATA(DEVICE_PARAMS *current_device,char* buf);
  22. void WRITE_MODBUS_DATA(char* cJSONstring);
  23. ///////////////////////////////////////////////////////////////////////////////////////////
  24. char string[512];
  25. void findDifference(char* data1, char* data2, char* string) ;
  26. int jsoncunt = 1;
  27. int commd = 1;
  28. ////////////////////////////////////////////////////////////////////////////////////////////
  29. /*
  30. *********************************************************************************************************
  31. * �� �� ��: void data_task(void *pdata)
  32. * ����˵��: ��Ҫ��data_task�����̣߳����ȼ��ߡ��������߼��ǽ�nandflash�е����ݽ���������ѯ��������
  33. * �� ����
  34. * �� �� ֵ: ��
  35. *********************************************************************************************************
  36. */
  37. void data_task(void *pdata)
  38. {
  39. OS_CPU_SR cpu_sr;
  40. pdata = pdata;
  41. mmodbus_init(1);
  42. char buf[1000]; // ����modbus��ȡ������
  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. // Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
  52. while (current_device!=NULL)
  53. {
  54. uint8_t err;
  55. StringInfo *message=NULL; //���ն�����Ϣ�ṹ��
  56. message=(StringInfo *)OSQPend(JsonQ,1000,&err);
  57. if(commd)
  58. {
  59. READ_DATA(current_device, buf);
  60. if(current_device->nextDevice!=NULL)
  61. {
  62. current_device=current_device->nextDevice;
  63. }
  64. else
  65. {
  66. sprintf(buf + strlen(buf) - 1, "}");
  67. time1 = OSTimeGet();
  68. if( jsoncunt || time2 <= time1 - 20000)
  69. {
  70. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  71. memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//������һ�ε�����
  72. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
  73. int msg = MBOX_USER_PUBLISHQOS2;
  74. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  75. current_device=get->device_params;
  76. jsoncunt = 0;
  77. time2 = OSTimeGet();
  78. }//20s����һ��
  79. else
  80. {
  81. if(strcmp(buf,pubJsonStringCopy))
  82. {
  83. memset(string, 0 , strlen(string));
  84. findDifference(buf, pubJsonStringCopy, string);
  85. memset(pubJsonString,0, strlen(pubJsonString));
  86. sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
  87. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  88. sprintf(pubJsonStringCopy, buf, strlen(buf));
  89. int msg = MBOX_USER_PUBLISHQOS2;
  90. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  91. }
  92. }
  93. memset(buf,0,strlen(buf));
  94. current_device=get->device_params;
  95. }
  96. }
  97. else
  98. {
  99. if(message != NULL)
  100. {
  101. WRITE_MODBUS_DATA(message->p);
  102. }
  103. printf("sx1278_task_recv : %s",message->p);
  104. myfree(SRAMIN ,message->p);
  105. commd = 1;
  106. }
  107. OSTimeDly(1000);
  108. }
  109. }
  110. #if 0 //���Դ��벻����ת��ֱ�ӽ�����Ӧ�Ľ���
  111. SlaveProtocolAnalysis(string,bufferLength);
  112. data_acquisition();
  113. uint8_t node_string[256];
  114. uint16_t node_string_Length;
  115. nodeSendReaddValue(node_string,&node_string_Length);
  116. GatewayProtocolAnalysis(node_string,node_string_Length);
  117. #endif
  118. /*
  119. *********************************************************************************************************
  120. * �� �� ��: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  121. * ����˵��: ��ȡ��ǰ�ڵ��ϵ�modbus����
  122. * �� �Σ�DEVICE_PARAMS *device ��ǰ�豸
  123. * �� �� ֵ: 1 ���ɹ� 0��ʧ��
  124. *********************************************************************************************************
  125. */
  126. int READ_DATA(DEVICE_PARAMS *device, char* buf)
  127. {
  128. DEVICE_PARAMS *current_device=device;
  129. GATEWAY_PARAMS* get;
  130. get = get_gateway_config_params();
  131. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  132. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  133. while(current_device->params != NULL)
  134. {
  135. if (current_device->protocol == MODBUS_READ)
  136. {
  137. protocol_485=1;
  138. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus�Ĵ�������
  139. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  140. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  141. {
  142. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  143. currentModbusParams->registerAddress,
  144. currentModbusParams->registerByteNum /2,
  145. data);
  146. if (success)
  147. {
  148. uint32_t value;
  149. if (currentModbusParams->registerByteNum == 4)
  150. {
  151. value = (uint32_t)data[0] | data[1];
  152. }
  153. else if (currentModbusParams->registerByteNum == 2)
  154. {
  155. value = data[0];
  156. }
  157. if (currentModbusParams->decimalPoint == 0)
  158. {
  159. currentModbusParams->value[0] = value;
  160. currentModbusParams->value[1] = value << 8;
  161. currentModbusParams->value[2] = value << 16;
  162. currentModbusParams->value[3] = value << 24;
  163. }
  164. else
  165. {
  166. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  167. memcpy(currentModbusParams->value, &convertedValue, 4);
  168. }
  169. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  170. current_device->deviceID, currentModbusParams->keyword, value);
  171. currentModbusParams = currentModbusParams->nextParams;
  172. if (currentModbusParams == NULL)
  173. {
  174. sprintf(buf + strlen(buf) - 1, "");
  175. return 0;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. return 1;
  182. }
  183. /*
  184. *********************************************************************************************************
  185. * �� �� ��:void WRITE_MODBUS_DATA(char* cJSONstring)
  186. * ����˵��: ����mqtt���ݲ�д��modbus�Ĵ���
  187. * �� �Σ�char* cJSONstring mqtt���յ�������
  188. * �� �� ֵ: ��
  189. *********************************************************************************************************
  190. */
  191. void WRITE_MODBUS_DATA(char* cJSONstring)
  192. {
  193. uint16_t data;
  194. uint16_t number;
  195. uint16_t slaveAddress;
  196. GATEWAY_PARAMS* get;
  197. get = get_gateway_config_params();
  198. DEVICE_PARAMS* current_device = get->device_params;
  199. if(cJSONstring[0] != '\0'){
  200. cJSON *root = cJSON_Parse(cJSONstring);
  201. const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
  202. const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
  203. const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
  204. const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
  205. const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
  206. while(current_device)
  207. {
  208. char* device_ID = (char*)current_device->deviceID;
  209. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  210. if(!strcmp(device_ID,deviceId))
  211. {
  212. OSTimeDlyHMSM(0, 0, 0, 100);
  213. //OSIntEnter();
  214. if(power)
  215. {
  216. slaveAddress = currentModbusParams->slaveAddress;
  217. number =currentModbusParams->registerAddress;
  218. data = power->valueint;
  219. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  220. }
  221. OSTimeDlyHMSM(0, 0, 0, 100);
  222. if(temp)
  223. {
  224. currentModbusParams = currentModbusParams->nextParams;
  225. slaveAddress = currentModbusParams->slaveAddress;
  226. number =currentModbusParams->registerAddress;
  227. data = temp->valueint;
  228. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  229. }
  230. OSTimeDlyHMSM(0, 0, 0, 100);
  231. if(mode)
  232. {
  233. currentModbusParams = currentModbusParams->nextParams;
  234. slaveAddress = currentModbusParams->slaveAddress;
  235. number =currentModbusParams->registerAddress;
  236. data = mode->valueint;
  237. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  238. }
  239. OSTimeDlyHMSM(0, 0, 0, 100);
  240. if(fan)
  241. {
  242. currentModbusParams = currentModbusParams->nextParams;
  243. slaveAddress = currentModbusParams->slaveAddress;
  244. number =currentModbusParams->registerAddress;
  245. data = fan->valueint;
  246. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  247. }
  248. //OSIntExit();
  249. }
  250. current_device = current_device->nextDevice;
  251. }
  252. cJSON_Delete(root);
  253. }
  254. }
  255. // �Ƚ����� JSONItem ���飬�ҵ��仯�IJ���
  256. void findDifference(char* buf, char* pubJsonStringCopy, char* string)
  257. {
  258. const char* delimiter = "{}";
  259. char* saveptr1;
  260. char* saveptr2;
  261. char* data1 = malloc(strlen(buf) + 1);
  262. char* data2 = malloc(strlen(pubJsonStringCopy) + 1); ;
  263. memcpy(data1, buf, strlen(buf));
  264. memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
  265. // ����strtok_r�����ָ��ַ���������һ�Ƚ�
  266. char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
  267. char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
  268. while (token1 != NULL && token2 != NULL) {
  269. if (strcmp(token1, token2) != 0) {
  270. memcpy(string + strlen(string), token1, strlen(token1));
  271. }
  272. token1 = strtok_r(NULL, delimiter, &saveptr1);
  273. token2 = strtok_r(NULL, delimiter, &saveptr2);
  274. }
  275. // // �����ʣ���ַ���δ�Ƚϣ����ӡʣ���ַ���
  276. // while (token1 != NULL) {
  277. // sprintf(string + strlen(string),"%s,", token1);
  278. // token1 = strtok_r(NULL, delimiter, &saveptr1);
  279. // }
  280. // while (token2 != NULL) {
  281. // //sprintf(string + strlen(string),"{%s},", token2);
  282. // token2 = strtok_r(NULL, delimiter, &saveptr2);
  283. // }
  284. free(data1);
  285. free(data2);
  286. }