task.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. char *pubJsonStringCopy = 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,pubJsonStringCopy); //发送数据
  53. jsonCunt = 0;
  54. memset(buf,0,strlen(buf));
  55. current_device=get->device_params;
  56. OSTimeDly(1);
  57. }
  58. myfree(SRAMEX, buf);
  59. myfree(SRAMEX,pubJsonStringCopy);
  60. }
  61. /*
  62. *********************************************************************************************************
  63. * 函 数 名: void mqtt_to_device()
  64. * 功能说明: 将接收到的数据发送至设备
  65. * 形 参:
  66. * 返 回 值:
  67. *********************************************************************************************************
  68. */
  69. void mqtt_to_device(){
  70. uint8_t err;
  71. StringInfo *message;
  72. message = (StringInfo*)OSMboxPend(mqtt_recvMseeageMbox, 1000, &err);
  73. if(message != NULL) //包含消息
  74. {
  75. write_modbus_data(message->p); //写入数据
  76. myfree(SRAMEX ,message->p);//释放内部数据
  77. //OSTimeDly(1000);
  78. }
  79. }
  80. /*
  81. *********************************************************************************************************
  82. * 函 数 名: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  83. * 功能说明: 读取当前节点上的modbus数据
  84. * 形 参: DEVICE_PARAMS *device 当前设备
  85. * 返 回 值: 1: 成功 0:失败
  86. *********************************************************************************************************
  87. */
  88. int read_device_data(DEVICE_PARAMS *device, char* buf)
  89. {
  90. DEVICE_PARAMS *current_device=device;
  91. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  92. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  93. while(current_device->params != NULL)
  94. {
  95. if (current_device->protocol == MODBUS_READ)
  96. {
  97. protocol_485=1;
  98. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
  99. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  100. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  101. {
  102. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  103. currentModbusParams->registerAddress,
  104. currentModbusParams->registerByteNum /2,
  105. data);
  106. if (success)
  107. {
  108. uint32_t value;
  109. if (currentModbusParams->registerByteNum == 4)
  110. {
  111. value = (uint32_t)data[0] | data[1];
  112. }
  113. else if (currentModbusParams->registerByteNum == 2)
  114. {
  115. value = data[0];
  116. }
  117. if (currentModbusParams->decimalPoint == 0)
  118. {
  119. currentModbusParams->value[0] = value;
  120. currentModbusParams->value[1] = value << 8;
  121. currentModbusParams->value[2] = value << 16;
  122. currentModbusParams->value[3] = value << 24;
  123. }
  124. else
  125. {
  126. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  127. memcpy(currentModbusParams->value, &convertedValue, 4);
  128. }
  129. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  130. current_device->deviceID, currentModbusParams->keyword, value);
  131. }
  132. // else
  133. // {
  134. // printf("read modbus register fail\n");
  135. // return 0;
  136. // }
  137. /* 每读完一个寄存器,进行message判断 */
  138. mqtt_to_device();
  139. currentModbusParams = currentModbusParams->nextParams;
  140. if (currentModbusParams == NULL)
  141. {
  142. current_device = current_device->nextDevice;
  143. currentModbusParams = current_device->params->gateway_read_modbus_command;
  144. if(current_device == NULL)
  145. {
  146. sprintf(buf + strlen(buf) - 1, "}");
  147. return 1;
  148. }
  149. }
  150. }
  151. }
  152. else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
  153. {
  154. protocol_485=2;
  155. uint8_t read_buf[10];
  156. uint32_t dltValue;
  157. currentDLT645Params->rxLen = 0;
  158. memset(read_buf, 0, 10);
  159. memset(currentDLT645Params->data, 0, 10);
  160. dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
  161. int8_t rs;
  162. if (current_device->protocol == DLT645_2007)
  163. {
  164. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  165. }
  166. else if (current_device->protocol == DLT645_1997)
  167. {
  168. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  169. }
  170. if (rs != -1)
  171. {
  172. if (rs <= 4)
  173. {
  174. memcpy(currentDLT645Params->data, read_buf, 4);
  175. currentDLT645Params->rxLen = 4;
  176. }
  177. else if (rs == 5)
  178. {
  179. memcpy(currentDLT645Params->data, read_buf, 5);
  180. currentDLT645Params->rxLen = 5;
  181. }
  182. else if (rs > 5)
  183. {
  184. memcpy(currentDLT645Params->data, read_buf, 9);
  185. currentDLT645Params->rxLen = 9;
  186. }
  187. dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
  188. currentDLT645Params->data[2] << 8 | currentDLT645Params->data[3];
  189. sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
  190. currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
  191. currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
  192. currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
  193. currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
  194. }
  195. // else
  196. // {
  197. // currentDLT645Params->rxLen = 0;
  198. // printf("read DLT current data fail\n");
  199. // }
  200. currentDLT645Params = currentDLT645Params->nextParams;
  201. if (currentDLT645Params == NULL)
  202. {
  203. current_device = current_device->nextDevice;
  204. currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  205. if(current_device == NULL)
  206. {
  207. sprintf(buf + strlen(buf) - 1, "}");
  208. return 1;
  209. }
  210. }
  211. }
  212. }
  213. return 1;
  214. }
  215. /*
  216. *********************************************************************************************************
  217. * 函 数 名:void WRITE_MODBUS_DATA(char* cJSONstring)
  218. * 功能说明: 接收mqtt数据并写入modbus寄存器
  219. * 形 参:char* cJSONstring mqtt接收到的数据
  220. * 返 回 值: 无
  221. *********************************************************************************************************
  222. */
  223. void write_modbus_data(char* cJSONstring)
  224. {
  225. GATEWAY_PARAMS* get;
  226. get = get_gateway_config_params();
  227. DEVICE_PARAMS* current_device = get->device_params;
  228. /* 利用cJSOn_Parse解析数据,获取各类型数据 */
  229. cJSON *root = cJSON_Parse(cJSONstring);
  230. const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
  231. const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
  232. const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
  233. const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
  234. const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
  235. while(current_device)
  236. {
  237. char* device_ID = (char*)current_device->deviceID;
  238. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  239. if(!strcmp(device_ID,deviceId)) //匹配ID
  240. {
  241. OSTimeDly(100);
  242. /* 写入寄存器操作 */
  243. if(power)
  244. {
  245. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  246. currentModbusParams->registerAddress,
  247. power->valueint);
  248. }
  249. OSTimeDly(100);
  250. if(temp)
  251. {
  252. currentModbusParams = currentModbusParams->nextParams;
  253. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  254. currentModbusParams->registerAddress,
  255. temp->valueint);
  256. }
  257. OSTimeDly(100);
  258. if(mode)
  259. {
  260. currentModbusParams = currentModbusParams->nextParams;
  261. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  262. currentModbusParams->registerAddress,
  263. mode->valueint);
  264. }
  265. OSTimeDly(100);
  266. if(fan)
  267. {
  268. currentModbusParams = currentModbusParams->nextParams;
  269. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  270. currentModbusParams->registerAddress,
  271. fan->valueint);
  272. }
  273. }
  274. current_device = current_device->nextDevice;
  275. }
  276. cJSON_Delete(root);
  277. }
  278. /*
  279. *********************************************************************************************************
  280. * 函 数 名: void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  281. * 功能说明: 比较出参数1和参数2的不同处
  282. * 形 参: 参数1:新数据 参数2:旧数据 参数3:输出参数
  283. * 返 回 值: 无
  284. *********************************************************************************************************
  285. */
  286. void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  287. {
  288. const char* delimiter = "{}";
  289. char* saveptr1;
  290. char* saveptr2;
  291. char* data1 = malloc(strlen(buf) + 1);
  292. char* data2 = malloc(strlen(pubJsonStringCopy) + 1); ;
  293. memcpy(data1, buf, strlen(buf));
  294. memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
  295. // 利用strtok_r函数分割字符串,并逐一比较
  296. char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
  297. char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
  298. while (token1 != NULL && token2 != NULL)
  299. {
  300. if (strcmp(token1, token2) != 0)
  301. {
  302. memcpy(string + strlen(string), token1, strlen(token1));
  303. }
  304. token1 = strtok_r(NULL, delimiter, &saveptr1);
  305. token2 = strtok_r(NULL, delimiter, &saveptr2);
  306. }
  307. // // 如果有剩余字符串未比较,则打印剩余字符串
  308. // while (token1 != NULL) {
  309. // sprintf(string + strlen(string),"%s,", token1);
  310. // token1 = strtok_r(NULL, delimiter, &saveptr1);
  311. // }
  312. // while (token2 != NULL) {
  313. // //sprintf(string + strlen(string),"{%s},", token2);
  314. // token2 = strtok_r(NULL, delimiter, &saveptr2);
  315. // }
  316. free(data1);
  317. free(data2);
  318. }
  319. /*
  320. *********************************************************************************************************
  321. * 函 数 名: void send_mqtt(char*buf, int jsonCunt)
  322. * 功能说明: 将数据发送到mqtt
  323. * 形 参: 参数1:读取数据 参数2:第一次发送标志
  324. * 返 回 值: 无
  325. *********************************************************************************************************
  326. */
  327. void send_mqtt(char*buf, int jsonCunt, char*pubJsonStringCopy){
  328. GATEWAY_PARAMS *get;
  329. get= get_gateway_config_params();
  330. if(get->device_params->protocol == MODBUS_READ)
  331. send_modbus_data(get, buf, jsonCunt,pubJsonStringCopy);// modbus数据通道
  332. else if(get->device_params->protocol == DLT645_2007 || get->device_params->protocol == DLT645_1997)
  333. send_dlt645_data(get, buf, jsonCunt,pubJsonStringCopy);// DLT645数据通道
  334. }
  335. /*
  336. *********************************************************************************************************
  337. * 函 数 名: void send_modbus_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy)
  338. * 功能说明: 发送modubs数据需求的通道
  339. * 形 参: 参数1:网关信息 参数2: 读取的信息 参数3: 第一次发送标志 参数4: 备份信息
  340. * 返 回 值: 无
  341. *********************************************************************************************************
  342. */
  343. void send_modbus_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy){
  344. time1 = GetCurrentTime();// 获取当前时间
  345. if(jsonCunt || time2 <= time1 - (20 * 1000)) // 20s进行一次
  346. {
  347. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  348. memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
  349. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf); // 组成要发送的json语句
  350. int msg = MBOX_USER_PUBLISHQOS0;
  351. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  352. time2 = GetCurrentTime(); // 获取当前时间
  353. }
  354. else
  355. {
  356. if(strcmp(buf,pubJsonStringCopy)) // 比较两次数据是否不同
  357. {
  358. memset(string, 0 , strlen(string));
  359. find_difference(buf, pubJsonStringCopy, string);// 比较两次采集的数据不同之处
  360. memset(pubJsonString,0, strlen(pubJsonString));
  361. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);// 组成要发送的json语句
  362. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  363. sprintf(pubJsonStringCopy, buf, strlen(buf));// 备份当前数据
  364. int msg = MBOX_USER_PUBLISHQOS0;
  365. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  366. }
  367. }
  368. }
  369. /*
  370. *********************************************************************************************************
  371. * 函 数 名: void send_dlt645_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy)
  372. * 功能说明: 发送modubs数据需求的通道
  373. * 形 参: 参数1:网关信息 参数2: 读取的信息 参数3: 第一次发送标志 参数4: 备份信息
  374. * 返 回 值: 无
  375. *********************************************************************************************************
  376. */
  377. void send_dlt645_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy){
  378. memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
  379. memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
  380. sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf); // 组成要发送的json语句
  381. int msg = MBOX_USER_PUBLISHQOS0;
  382. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  383. }