task.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. uint8_t read_cnt = 0;
  24. uint8_t count = 0;
  25. uint8_t jsonCunt = 1;
  26. int ID = 0;
  27. /*
  28. *********************************************************************************************************
  29. * 函 数 名: void data_task(void *pdata)
  30. * 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
  31. * 形 参:无
  32. * 返 回 值: 无
  33. *********************************************************************************************************
  34. */
  35. void data_task(void *pdata)
  36. {
  37. OS_CPU_SR cpu_sr;
  38. pdata = pdata;
  39. dlt645_init(100);
  40. mmodbus_init(1);
  41. char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
  42. read_file("device.txt", device_config_json);
  43. addGatewayParams(device_config_json);
  44. myfree(SRAMEX, device_config_json);
  45. GATEWAY_PARAMS *get;
  46. get= get_gateway_config_params();
  47. DEVICE_PARAMS *current_device=get->device_params;
  48. // Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->checkBit, get->flowControl);
  49. char *buf = mymalloc(SRAMEX, 9 * 1024); // 接收读取的数据
  50. memset(buf, 0, 9 * 1024);
  51. while (current_device!=NULL)
  52. {
  53. time1 = GetCurrentTime();
  54. if(mqtt_connectFlag)
  55. {
  56. if(jsonCunt || time2 <= time1 - ( 60 * 1000))// 60s进行一次全数据发送
  57. {
  58. read_device_data(current_device, buf);
  59. send_mqtt(buf);
  60. current_device=get->device_params;
  61. time2 = GetCurrentTime();
  62. jsonCunt = 0;
  63. count = 0;
  64. }
  65. else
  66. {
  67. read_device_data(current_device, buf);
  68. if(count > 0)// count检测是否含有数据
  69. {
  70. send_mqtt(string);
  71. memset(string,0,strlen(string));
  72. current_device=get->device_params;
  73. count = 0;
  74. }else
  75. {
  76. // LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "no data");
  77. }
  78. }
  79. mqtt_to_device();
  80. memset(buf,0,strlen(buf));
  81. }
  82. OSTimeDly(100);
  83. }
  84. myfree(SRAMEX, buf);
  85. }
  86. /*
  87. *********************************************************************************************************
  88. * 函 数 名: void mqtt_to_device()
  89. * 功能说明: 将接收到的数据发送至设备
  90. * 形 参:
  91. * 返 回 值:
  92. *********************************************************************************************************
  93. */
  94. void mqtt_to_device(){
  95. uint8_t err;
  96. StringInfo *message;
  97. message = (StringInfo*)OSMboxPend(mqtt_recvMseeageMbox, 1000, &err);
  98. if(message != NULL) //包含消息
  99. {
  100. write_modbus_data(message->p); //写入数据
  101. myfree(SRAMEX ,message->p);//释放内部数据
  102. //OSTimeDly(1000);
  103. }
  104. }
  105. void find_diff(char* buf, char* string) {
  106. }
  107. /*
  108. *********************************************************************************************************
  109. * 函 数 名: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  110. * 功能说明: 读取当前节点上的modbus数据
  111. * 形 参: DEVICE_PARAMS *device 当前设备
  112. * 返 回 值: 1: 成功 0:失败
  113. *********************************************************************************************************
  114. */
  115. int read_device_data(DEVICE_PARAMS *device, char* buf)
  116. {
  117. DEVICE_PARAMS *current_device=device;
  118. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  119. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  120. while(current_device->params != NULL)
  121. {
  122. if (current_device->protocol == MODBUS_READ)
  123. {
  124. protocol_485=1;
  125. uint8_t state;
  126. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
  127. uint8_t data1[currentModbusParams->registerByteNum /2];
  128. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  129. // 读水阀状态
  130. if(currentModbusParams->functionCode == 0x01)
  131. {
  132. bool success = mmodbus_readCoil(currentModbusParams->slaveAddress,
  133. currentModbusParams->registerByteNum /2,
  134. data1);
  135. if(success)
  136. {
  137. uint8_t value;
  138. value = data1[0];
  139. if(value == 0)
  140. {
  141. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":close},",
  142. current_device->deviceID, currentModbusParams->keyword);
  143. }else{
  144. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":open},",
  145. current_device->deviceID, currentModbusParams->keyword);
  146. }
  147. }
  148. currentModbusParams = currentModbusParams->nextParams;
  149. if (currentModbusParams == NULL)
  150. {
  151. current_device = current_device->nextDevice;
  152. currentModbusParams = current_device->params->gateway_read_modbus_command;
  153. if(current_device == NULL)
  154. {
  155. sprintf(buf + strlen(buf) - 1, "");
  156. return 1;
  157. }
  158. }
  159. }
  160. // 读单个寄存器
  161. if (currentModbusParams->functionCode == 0x03)
  162. {
  163. // bool success = mmodbus_readHoldingRegisters16i(0x17,0x00,0x02,data);
  164. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  165. currentModbusParams->registerAddress,
  166. currentModbusParams->registerByteNum /2,
  167. data);
  168. if (success)
  169. {
  170. uint32_t value;
  171. if (currentModbusParams->registerByteNum == 4)
  172. {
  173. value = (uint32_t)data[0] | data[1];
  174. }
  175. else if (currentModbusParams->registerByteNum == 2)
  176. {
  177. value = data[0];
  178. }
  179. if((value - currentModbusParams->value) != 0)
  180. {
  181. count++;
  182. sprintf(string + strlen(string), "{\"deviceId\":\"%s\",\"%s\":%d},",
  183. current_device->deviceID, currentModbusParams->keyword, value);
  184. }
  185. else
  186. {
  187. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
  188. current_device->deviceID, currentModbusParams->keyword, value);
  189. }
  190. if (currentModbusParams->decimalPoint == 0)
  191. {
  192. currentModbusParams->value = value;
  193. }
  194. else
  195. {
  196. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  197. currentModbusParams->value=convertedValue;
  198. }
  199. }
  200. currentModbusParams = currentModbusParams->nextParams;
  201. if (currentModbusParams == NULL)
  202. {
  203. current_device = current_device->nextDevice;
  204. currentModbusParams = current_device->params->gateway_read_modbus_command;
  205. if(current_device == NULL)
  206. {
  207. sprintf(buf + strlen(buf) - 1, "");
  208. return 1;
  209. }
  210. }
  211. }
  212. // 开关水阀
  213. if(currentModbusParams->functionCode == 0x05)
  214. {
  215. bool success = mmodbus_writeCoil(currentModbusParams->slaveAddress,
  216. currentModbusParams->registerByteNum /2,
  217. state);
  218. if(success)
  219. {
  220. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": success},",
  221. current_device->deviceID, currentModbusParams->keyword);
  222. }
  223. else{
  224. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": fail},",
  225. current_device->deviceID, currentModbusParams->keyword);
  226. }
  227. currentModbusParams = currentModbusParams->nextParams;
  228. if (currentModbusParams == NULL)
  229. {
  230. current_device = current_device->nextDevice;
  231. currentModbusParams = current_device->params->gateway_read_modbus_command;
  232. if(current_device == NULL)
  233. {
  234. sprintf(buf + strlen(buf) - 1, "");
  235. return 1;
  236. }
  237. }
  238. }
  239. // 写单个寄存器
  240. if(currentModbusParams->functionCode == 0x06)
  241. {
  242. bool success = mmodbus_writeHoldingRegisters16i(currentModbusParams->slaveAddress,
  243. currentModbusParams->registerAddress,
  244. currentModbusParams->registerByteNum /2,
  245. data);
  246. if(success)
  247. {
  248. sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":write success},",
  249. current_device->deviceID, currentModbusParams->keyword);
  250. }
  251. currentModbusParams = currentModbusParams->nextParams;
  252. if (currentModbusParams == NULL)
  253. {
  254. current_device = current_device->nextDevice;
  255. currentModbusParams = current_device->params->gateway_read_modbus_command;
  256. if(current_device == NULL)
  257. {
  258. sprintf(buf + strlen(buf) - 1, "");
  259. return 1;
  260. }
  261. }
  262. }
  263. }
  264. else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
  265. {
  266. protocol_485=2;
  267. uint8_t read_buf[10];
  268. uint32_t dltValue;
  269. currentDLT645Params->rxLen = 0;
  270. memset(read_buf, 0, 10);
  271. memset(currentDLT645Params->data, 0, 10);
  272. dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
  273. int8_t rs;
  274. if (current_device->protocol == DLT645_2007)
  275. {
  276. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  277. }
  278. else if (current_device->protocol == DLT645_1997)
  279. {
  280. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  281. }
  282. if (rs != -1)
  283. {
  284. if (rs <= 4)
  285. {
  286. memcpy(currentDLT645Params->data, read_buf, 4);
  287. currentDLT645Params->rxLen = 4;
  288. }
  289. else if (rs == 5)
  290. {
  291. memcpy(currentDLT645Params->data, read_buf, 5);
  292. currentDLT645Params->rxLen = 5;
  293. }
  294. else if (rs > 5)
  295. {
  296. memcpy(currentDLT645Params->data, read_buf, 9);
  297. currentDLT645Params->rxLen = 9;
  298. }
  299. dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
  300. currentDLT645Params->data[2] << 8 | currentDLT645Params->data[3];
  301. sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
  302. currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
  303. currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
  304. currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
  305. currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
  306. count++;
  307. }
  308. currentDLT645Params = currentDLT645Params->nextParams;
  309. if (currentDLT645Params == NULL)
  310. {
  311. current_device = current_device->nextDevice;
  312. currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  313. if(current_device == NULL)
  314. {
  315. sprintf(buf + strlen(buf) - 1, "");
  316. return 1;
  317. }
  318. }
  319. }
  320. }
  321. return 1;
  322. }
  323. /*
  324. *********************************************************************************************************
  325. * 函 数 名:void WRITE_MODBUS_DATA(char* cJSONstring)
  326. * 功能说明: 接收mqtt数据并写入modbus寄存器
  327. * 形 参:char* cJSONstring mqtt接收到的数据
  328. * 返 回 值: 无
  329. *********************************************************************************************************
  330. */
  331. void write_modbus_data(char* cJSONstring)
  332. {
  333. GATEWAY_PARAMS* get;
  334. get = get_gateway_config_params();
  335. DEVICE_PARAMS* current_device = get->device_params;
  336. /* 利用cJSOn_Parse解析数据,获取各类型数据 */
  337. cJSON *root = cJSON_Parse(cJSONstring);
  338. const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
  339. const cJSON *func = cJSON_GetObjectItemCaseSensitive(root, "function");
  340. const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
  341. const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
  342. const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
  343. const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
  344. const cJSON *slaveAddress = cJSON_GetObjectItemCaseSensitive(root, "slaveAddress");
  345. const cJSON *registerAddress = cJSON_GetObjectItemCaseSensitive(root, "registerAddress");
  346. const cJSON *cmd = cJSON_GetObjectItemCaseSensitive(root, "cmd");
  347. while(current_device)
  348. {
  349. char* device_ID = (char*)current_device->deviceID;
  350. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  351. if(!strcmp(device_ID,deviceId)) //匹配ID
  352. {
  353. OSTimeDly(100);
  354. if(func->valueint == 5)
  355. // 开关阀门
  356. {
  357. bool success = mmodbus_writeCoil(slaveAddress->valueint,registerAddress->valueint,cmd->valueint);
  358. }
  359. if(func->valueint == 6)
  360. {
  361. /* 写入寄存器操作 */
  362. if(power)
  363. {
  364. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  365. currentModbusParams->registerAddress,
  366. power->valueint);
  367. }
  368. OSTimeDly(100);
  369. if(temp)
  370. {
  371. currentModbusParams = currentModbusParams->nextParams;
  372. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  373. currentModbusParams->registerAddress,
  374. temp->valueint);
  375. }
  376. OSTimeDly(100);
  377. if(mode)
  378. {
  379. currentModbusParams = currentModbusParams->nextParams;
  380. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  381. currentModbusParams->registerAddress,
  382. mode->valueint);
  383. }
  384. OSTimeDly(100);
  385. if(fan)
  386. {
  387. currentModbusParams = currentModbusParams->nextParams;
  388. mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
  389. currentModbusParams->registerAddress,
  390. fan->valueint);
  391. }
  392. }
  393. }
  394. current_device = current_device->nextDevice;
  395. }
  396. cJSON_Delete(root);
  397. }
  398. /*
  399. *********************************************************************************************************
  400. * 函 数 名: void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  401. * 功能说明: 比较出参数1和参数2的不同处
  402. * 形 参: 参数1:新数据 参数2:旧数据 参数3:输出参数
  403. * 返 回 值: 无
  404. *********************************************************************************************************
  405. */
  406. void find_difference(char* buf, char* pubJsonStringCopy, char* string)
  407. {
  408. const char* delimiter = "{}";
  409. char* saveptr1;
  410. char* saveptr2;
  411. char* data1 = mymalloc(SRAMEX, strlen(buf));
  412. char* data2 = mymalloc(SRAMEX, strlen(pubJsonStringCopy));
  413. memcpy(data1, buf, strlen(buf));
  414. memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
  415. // 利用strtok_r函数分割字符串,并逐一比较
  416. char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
  417. char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
  418. memset(string,0,strlen(string));
  419. while (token1 != NULL && token2 != NULL)
  420. {
  421. if (strcmp(token1, token2) != 0)
  422. {
  423. memcpy(string + strlen(string), token1, strlen(token1));
  424. }
  425. token1 = strtok_r(NULL, delimiter, &saveptr1);
  426. token2 = strtok_r(NULL, delimiter, &saveptr2);
  427. }
  428. // // 如果有剩余字符串未比较,则打印剩余字符串
  429. // while (token1 != NULL) {
  430. // sprintf(string + strlen(string),"%s,", token1);
  431. // token1 = strtok_r(NULL, delimiter, &saveptr1);
  432. // }
  433. // while (token2 != NULL) {
  434. // //sprintf(string + strlen(string),"{%s},", token2);
  435. // token2 = strtok_r(NULL, delimiter, &saveptr2);
  436. // }
  437. myfree(SRAMEX, data1);
  438. myfree(SRAMEX, data2);
  439. }
  440. /*
  441. *********************************************************************************************************
  442. * 函 数 名: void send_mqtt(char*buf, int jsonCunt)
  443. * 功能说明: 将数据发送到mqtt
  444. * 形 参: 参数1:读取数据 参数2:第一次发送标志
  445. * 返 回 值: 无
  446. *********************************************************************************************************
  447. */
  448. void send_mqtt(char*buf){
  449. GATEWAY_PARAMS *get;
  450. get= get_gateway_config_params();
  451. sprintf(pubJsonString,"ID: %d, {\"DEVICEID\":\"%s\",\"data\":[%s]}",ID++,get->deviceId, buf); // 组成要发送的json语句
  452. int msg = MBOX_USER_PUBLISHQOS0;
  453. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  454. }