task.c 11 KB

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