task.c 18 KB

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