parseDeviceMessage.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #include "parseDeviceMessage.h"
  2. #include "cJson.h"
  3. #include "systick.h"
  4. #include "usart.h"
  5. #include "string.h"
  6. #include <math.h>
  7. #include "mqttRecv.h"
  8. #include "log.h"
  9. #include "ec800m.h"
  10. #include "main.h"
  11. #include "device_alive.h"
  12. #define MQTT_SUB_CMD_WRITE 0
  13. #define MQTT_SUB_CMD_READ 1
  14. ring_buffer mqttRecv;
  15. uint8_t protocol;
  16. void processHTTPjson(cJSON *json);
  17. char *processStringData(cJSON *data_obj, const char *key);
  18. int processIntData(cJSON *data_obj, const char *key);
  19. static void extract_data_from_buffer(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr);
  20. //static void extract_data_from_buffer232(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr);
  21. static uint16_t checksum(const char *str, uint16_t len);
  22. // json解析字符串
  23. char *processStringData(cJSON *data_obj, const char *key)
  24. {
  25. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  26. if (cJSON_IsString(json_data) && (json_data->valuestring != NULL))
  27. {
  28. return json_data->valuestring;
  29. }
  30. else
  31. {
  32. return NULL;
  33. }
  34. }
  35. // json解析int
  36. int processIntData(cJSON *data_obj, const char *key)
  37. {
  38. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  39. // if (cJSON_IsNumber(json_data) && (json_data->valueint != NULL)) 解决寄存器地址为0问题
  40. if (cJSON_IsNumber(json_data))
  41. {
  42. return json_data->valueint;
  43. }
  44. else
  45. {
  46. // gateway.data_valid_flag = 0; // 如果任意数据为NULL则此次数据为无效
  47. return -1;
  48. }
  49. }
  50. /*
  51. * 函数名:void WaitForUpData(void)
  52. * 输入参数:无
  53. * 输出参数:无
  54. * 返回值:无
  55. * 函数作用:等待更新本地flash信息
  56. */
  57. bool WaitForUpData(char *dmaBuffer)
  58. {
  59. if (UART0_RX_STAT > 0)
  60. {
  61. UART0_RX_STAT = 0;
  62. uint32_t len;
  63. uint16_t checkCode;
  64. char *temp;
  65. extract_data_from_buffer(dmaBuffer, &len, &checkCode);
  66. uint16_t jsonCheck = checksum(dmaBuffer, len);
  67. if (checkCode == jsonCheck)
  68. {
  69. addGatewayParams(dmaBuffer);
  70. return true;
  71. }
  72. else
  73. {
  74. return false;
  75. }
  76. }
  77. }
  78. /*
  79. * 函数名:static void extract_data_from_buffer(const char* buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  80. * 输入参数:buffer字符串
  81. * 输出参数:json有效字符串长度len_ptr,checkCode_ptr校验码指针
  82. * 返回值:无
  83. * 函数作用:eg. QFDWL: 621,3e23 从json信息最后端取出这段json的有效长度和校验码
  84. */
  85. static void extract_data_from_buffer(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  86. {
  87. char *start = strstr(buffer, "+QFDWL:");
  88. if (start != NULL)
  89. {
  90. start += 8; // 跳过"+QFDWL:"
  91. uint32_t len = 0;
  92. sscanf(start, "%u,", &len); // 读取长度
  93. start = strchr(start, ',') + 1; // 跳过逗号
  94. uint16_t checkCode = 0;
  95. sscanf(start, "%hx", &checkCode); // 读取16进制数据
  96. // 将提取的数据存入形参
  97. *len_ptr = len;
  98. *checkCode_ptr = checkCode;
  99. }
  100. }
  101. /*
  102. * 函数名:void dlt645_read(CONFIG_PARAMS *gateway)
  103. * 输入参数:设备地址*address 数据标识 readCode
  104. * 输出参数:无
  105. * 返回值:无
  106. * 函数作用:读取645协议内部数据
  107. 备注:dlt645协议为网上下载,返回值为数据长度
  108. 现在要加读日期信息YYMMDDhhmm
  109. 对源代码改动不多的方法是通过返回的数据长度去做判断
  110. 当数据长度为2、3、4时此时 read_buf数据包内仅含采集到的浮点数数据、 把float值4个字节直接拷贝出即可
  111. 当数据长度为5时 read_buf数据包为日期数据 其中5个字节分别为YYMMDDhhmm直接拷贝出来
  112. 当数据长度大于5时 read_buf内既包含浮点数又包含日期时间 其中前4个字节为浮点数数据后五个字节为日期信息
  113. */
  114. #if 0
  115. void dlt645_read(CONFIG_PARAMS *gateway)
  116. {
  117. uint8_t read_buf[10];
  118. for (int i = 0; i < gateway->device_read_data_num; i++)
  119. {
  120. gateway->device_read_data[i].rxLen = 0;
  121. memset(read_buf, 0, 10);
  122. memset(gateway->device_read_data[i].data, 0, 10);
  123. dlt645_set_addr(&dlt645, gateway->device_read_data[i].deviceID645);
  124. int8_t rs;
  125. if(gateway->dataType645==DLT645_2007){
  126. rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_2007);
  127. }
  128. else {
  129. rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_1997);
  130. }
  131. if (rs != -1)
  132. {
  133. if (rs <= 4)
  134. {
  135. memcpy(gateway->device_read_data[i].data, read_buf, 4);
  136. gateway->device_read_data[i].rxLen = rs;
  137. }
  138. else if (rs == 5)
  139. {
  140. memcpy(gateway->device_read_data[i].data, read_buf, 5);
  141. gateway->device_read_data[i].rxLen = rs;
  142. }
  143. else if (rs > 5)
  144. {
  145. memcpy(gateway->device_read_data[i].data, read_buf, 9);
  146. gateway->device_read_data[i].rxLen = rs;
  147. }
  148. }
  149. else
  150. {
  151. gateway->device_read_data[i].rxLen = 0;
  152. }
  153. task_fwdgt_reload();
  154. }
  155. }
  156. #endif
  157. /*
  158. * 函数名: void Read_Data(GATEWAY_PARAMS *gateway)
  159. * 输入参数:gateway网关存储的配置参数
  160. * 输出参数:网关结构体指针
  161. * 返回值:无
  162. * 函数作用:根据存储的设备信息发送modbus信息返回值存储到gateway->device_read_data[i].data中
  163. * modbusRead读数据只考虑读16位寄存器和32位寄存器这两种情况,但对其返回值进行处理后返回值一律为float型4个字节存储在内部
  164. dlt645根据其返回长度判断其具体是否挈带了日期数据
  165. */
  166. void Read_Data()
  167. {
  168. GATEWAY_PARAMS *gateway;
  169. gateway=get_gateway_config_params();
  170. DEVICE_PARAMS *currentDevice=gateway->device_params;
  171. READ_MODBUS_COMMAND *read_modbus_command=NULL;
  172. READ_DLT645_COMMAND *read_dlt645_command=NULL;
  173. switch(currentDevice->protocol)
  174. {
  175. case DLT645_07:
  176. case DLT645_97:
  177. protocol=1;
  178. read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  179. break;
  180. case MODBUS:
  181. protocol=2;
  182. read_modbus_command=currentDevice->params->node_read_modbus_command;
  183. mmodbus_set16bitOrder(currentDevice->MDBbigLittleFormat); //设置大小端
  184. break;
  185. }
  186. while(1)//进入轮询
  187. {
  188. __START__WHILE:
  189. delay_1ms(10);
  190. switch(currentDevice->protocol)
  191. {
  192. case DLT645_07:
  193. case DLT645_97:
  194. {
  195. uint8_t read_buf[10];
  196. memset(read_buf, 0, 10);
  197. dlt645_set_addr(&dlt645,read_dlt645_command->deviceID645);
  198. int8_t rs;
  199. if(currentDevice->protocol==DLT645_97)
  200. {
  201. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_1997);
  202. }
  203. else if(currentDevice->protocol==DLT645_07)
  204. {
  205. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_2007);
  206. }
  207. if (rs != -1)
  208. {
  209. if (rs <= 4)
  210. {
  211. if(memcmp(read_dlt645_command->data,read_buf,4)!=0|| device_time_flag==1)
  212. {
  213. memcpy(read_dlt645_command->data,read_buf,4);
  214. read_dlt645_command->rxLen = 4;
  215. }
  216. else
  217. {
  218. read_dlt645_command->rxLen = 0;
  219. }
  220. }
  221. else if (rs == 5)
  222. {
  223. if(memcmp(read_dlt645_command->data,read_buf,5)!=0 || device_time_flag==1)
  224. {
  225. memcpy(read_dlt645_command->data,read_buf,5);
  226. read_dlt645_command->rxLen = 5;
  227. }
  228. else
  229. {
  230. read_dlt645_command->rxLen = 0;
  231. }
  232. }
  233. else if (rs > 5)
  234. {
  235. if(memcmp(read_dlt645_command->data,read_buf,9)!=0|| device_time_flag==1)
  236. {
  237. memcpy(read_dlt645_command->data,read_buf,9);
  238. read_dlt645_command->rxLen = 9;
  239. }
  240. else
  241. {
  242. read_dlt645_command->rxLen = 0;
  243. }
  244. }
  245. }
  246. else
  247. {
  248. read_dlt645_command->rxLen = 0;
  249. }
  250. }
  251. break;
  252. case MODBUS:
  253. {
  254. uint16_t data[read_modbus_command->registerByteNum/2];
  255. switch(read_modbus_command->functionCode)
  256. {
  257. case 0x03://读寄存器
  258. {
  259. bool success=mmodbus_readHoldingRegisters16i(read_modbus_command->slaveAddress,
  260. read_modbus_command->registerAddress,
  261. read_modbus_command->registerByteNum/2,
  262. data);
  263. if(success)
  264. {
  265. uint32_t value;
  266. if(read_modbus_command->registerByteNum==4)
  267. {
  268. value = (uint32_t)data[0] << 16 | data[1];
  269. read_modbus_command->rxLen=4;
  270. }
  271. else if(read_modbus_command->registerByteNum==2)
  272. {
  273. value = data[0];
  274. read_modbus_command->rxLen=4;
  275. }
  276. if(read_modbus_command->decimalPoint!=0)
  277. {
  278. float convertedValue =(float)value/pow(10,read_modbus_command->decimalPoint);
  279. if(memcmp(read_modbus_command->value,&convertedValue,4)!=0|| device_time_flag==1)
  280. {
  281. memcpy(read_modbus_command->value,&convertedValue,4);
  282. }
  283. else
  284. {
  285. read_modbus_command->rxLen=0;
  286. }
  287. }
  288. else
  289. {
  290. if(memcmp(read_modbus_command->value,&value,4)!=0|| device_time_flag==1)
  291. {
  292. memcpy(read_modbus_command->value,&value,4);
  293. }
  294. else
  295. {
  296. read_modbus_command->rxLen=0;
  297. }
  298. }
  299. }
  300. else
  301. {
  302. read_modbus_command->rxLen=0;
  303. }
  304. }
  305. break;
  306. case 0x01://读线圈
  307. {
  308. uint8_t data[2];
  309. bool success = mmodbus_readCoils(read_modbus_command->slaveAddress,
  310. read_modbus_command->registerAddress,
  311. read_modbus_command->registerByteNum/2,
  312. data);
  313. if(success)
  314. {
  315. read_modbus_command->rxLen=4;
  316. uint32_t value = (uint32_t)data[0] << 8 | data[1];
  317. if(memcmp(read_modbus_command->value,&value,4)!=0|| device_time_flag==1)
  318. {
  319. memcpy(read_modbus_command->value,&value,4);
  320. }
  321. else
  322. {
  323. read_modbus_command->rxLen=0;
  324. }
  325. }
  326. else
  327. {
  328. read_modbus_command->rxLen=0;
  329. }
  330. }
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. break;
  337. }
  338. parseMQTTData(gateway);//是否有下发数据有下发优先处理
  339. switch(currentDevice->protocol)
  340. {
  341. case DLT645_07:
  342. case DLT645_97:
  343. read_dlt645_command=read_dlt645_command->nextParams;
  344. break;
  345. case MODBUS:
  346. read_modbus_command=read_modbus_command->nextParams;
  347. break;
  348. }
  349. if((read_dlt645_command==NULL&&protocol==1)||(read_modbus_command==NULL&&protocol==2))
  350. {
  351. currentDevice=currentDevice->nextDevice;
  352. switch(currentDevice->protocol)
  353. {
  354. case DLT645_97:
  355. case DLT645_07:
  356. protocol=1;
  357. break;
  358. case MODBUS:
  359. protocol=2;
  360. break;
  361. }
  362. if(currentDevice==NULL)
  363. {
  364. return;//一直到没有设备时跳出循环
  365. }
  366. else
  367. {
  368. read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  369. read_modbus_command=currentDevice->params->node_read_modbus_command;
  370. }
  371. }
  372. task_fwdgt_reload();//喂狗
  373. goto __START__WHILE;
  374. }
  375. }
  376. /*
  377. * 函数名:void parseMQTTData(CONFIG_PARAMS *gateway)
  378. * 输入参数:device存储的各类产品信息
  379. * 输出参数:无
  380. * 返回值:无
  381. * 函数作用:从环形缓冲区中一个个把json信息读出,与存储的gateway内的子设备信息匹配后将对应指令匹配执行并返回相应的值
  382. * 查询逻辑现在有两套数组read数组和write数组,当接收中断产生时先判断是进行读还是写、之后在查询此网关支持的协议MODBUS还是
  383. * 645,后根据协议去不同数组查询不同的本地存储信息、组装发送给子设备,然后根据查询的值进行采集上传数据
  384. */
  385. void parseMQTTData(GATEWAY_PARAMS *gateway)
  386. {
  387. while (UART0_RX_MQTT_SUB_STAT == 1)
  388. {
  389. UART0_RX_MQTT_SUB_STAT = 0;
  390. char payload_out[200];
  391. memset(payload_out,0,200);
  392. char mqtt_publish[200];
  393. char json[128];
  394. while (MQTT_BUFFER_READ(json))
  395. {
  396. LogPrint(LOG_INFO, __func__, __LINE__, "Received JSON: %s", json);
  397. cJSON *mqtt_sub_json = cJSON_Parse(json);
  398. if (mqtt_sub_json != NULL)
  399. {
  400. uint8_t action1[1];
  401. uint8_t action;
  402. strcpy(action1, processStringData(mqtt_sub_json, "action"));
  403. // 解决字符串为
  404. if (action1[0] == '0')
  405. {
  406. action = 0;
  407. }
  408. else if (action1[0] == '1')
  409. {
  410. action = 1;
  411. }
  412. char identifier[20];
  413. strcpy(identifier, processStringData(mqtt_sub_json, "identifier"));
  414. char deviceId[20];
  415. strcpy(deviceId, processStringData(mqtt_sub_json, "deviceId"));
  416. uint16_t value = processIntData(mqtt_sub_json, "parameter");
  417. char messageId[10];
  418. strcpy(messageId, processStringData(mqtt_sub_json, "messageId"));
  419. DEVICE_PARAMS *currentDevice=gateway->device_params;
  420. WRITE_MODBUS_COMMAND *write_modbus_command=NULL;
  421. READ_MODBUS_COMMAND *read_modbus_command=NULL;
  422. READ_DLT645_COMMAND *read_dlt645_command=NULL;
  423. // 判断其指令过来是读还是写
  424. if (action == MQTT_SUB_CMD_WRITE)//写指令目前只支持modbus
  425. {
  426. while(1)
  427. {
  428. if(strcmp((char *)&currentDevice->deviceID,deviceId)!=0)
  429. {
  430. currentDevice=currentDevice->nextDevice;
  431. if(currentDevice==NULL)
  432. {
  433. break; //如果找不到所属设备则跳出
  434. }
  435. }
  436. else
  437. {
  438. protocol=2;//决定当前使用modbus回调函数还是dlt645
  439. if(write_modbus_command==NULL)
  440. {
  441. write_modbus_command=currentDevice->params->node_write_modbus_command;
  442. }
  443. if(strcmp((char*)&write_modbus_command->keyword,identifier)!=0)//寻找设备层内所属属性名称
  444. {
  445. write_modbus_command=write_modbus_command->nextParams;
  446. if(write_modbus_command==NULL)
  447. {
  448. break;
  449. }
  450. }
  451. else //找到所属于的属性了
  452. {
  453. mmodbus_set16bitOrder(currentDevice->MDBbigLittleFormat);
  454. if(write_modbus_command->functionCode==0x05)
  455. {
  456. bool success = mmodbus_writeCoil(write_modbus_command->slaveAddress,
  457. write_modbus_command->registerAddress,
  458. (uint8_t)value);
  459. // 成功
  460. if (success)
  461. {
  462. sprintf((char *)payload_out,
  463. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  464. action1,
  465. write_modbus_command->keyword,
  466. currentDevice->deviceID, messageId, success);
  467. break;
  468. }
  469. else
  470. {
  471. sprintf((char *)payload_out,
  472. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  473. action1,
  474. write_modbus_command->keyword,
  475. currentDevice->deviceID, messageId, success);
  476. break;
  477. }
  478. }
  479. else if(write_modbus_command->functionCode==0x06)
  480. {
  481. bool success = mmodbus_writeHoldingRegister16i(write_modbus_command->slaveAddress,
  482. write_modbus_command->registerAddress,
  483. (uint16_t)value);
  484. if (success)
  485. {
  486. sprintf((char *)payload_out,
  487. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  488. action1,
  489. write_modbus_command->keyword,
  490. currentDevice->deviceID, messageId, success);
  491. break;
  492. }
  493. else
  494. {
  495. sprintf((char *)payload_out,
  496. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  497. action1,
  498. write_modbus_command->keyword,
  499. currentDevice->deviceID, messageId, success);
  500. break;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. // 指令是来读某属性值的
  508. else if (action == MQTT_SUB_CMD_READ)
  509. {
  510. while(1)
  511. {
  512. if(strcmp((char *)&currentDevice->deviceID,deviceId)!=0)
  513. {
  514. currentDevice=currentDevice->nextDevice;
  515. if(currentDevice==NULL)
  516. {
  517. break; //如果找不到所属设备则跳出
  518. }
  519. }
  520. else //找到了设备
  521. {
  522. if(currentDevice->protocol==MODBUS)
  523. {
  524. protocol=2;//决定当前使用modbus=2回调函数还是dlt645=1
  525. if(read_modbus_command==NULL)read_modbus_command=currentDevice->params->node_read_modbus_command;
  526. if(strcmp((char *)&read_modbus_command->keyword,identifier)!=0)
  527. {
  528. read_modbus_command=read_modbus_command->nextParams;
  529. if(read_modbus_command==NULL)break;
  530. }
  531. else//找到了属性
  532. {
  533. mmodbus_set16bitOrder(currentDevice->MDBbigLittleFormat);
  534. if(read_modbus_command->functionCode==0x01)
  535. {
  536. uint8_t data[2];
  537. bool success = mmodbus_readCoils(read_modbus_command->slaveAddress,
  538. read_modbus_command->registerAddress,
  539. read_modbus_command->registerByteNum/2,
  540. data);
  541. uint8_t value1;
  542. if (success)
  543. {
  544. uint16_t value1 = (uint16_t)data[0] << 8 | data[1];
  545. if (value1 == 0xFF00)
  546. {
  547. value1 = 1;
  548. }
  549. else if (value1 == 0x0000)
  550. {
  551. value1 = 0;
  552. }
  553. // 读线圈成功的json
  554. sprintf((char *)payload_out,
  555. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"parameter\":%d}",
  556. action1,
  557. read_modbus_command->keyword,
  558. currentDevice->deviceID, messageId, success, value);
  559. break;
  560. }
  561. // 读线圈失败的json
  562. else
  563. {
  564. sprintf((char *)payload_out,
  565. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  566. action1,
  567. read_modbus_command->keyword,
  568. currentDevice->deviceID, messageId, success);
  569. break;
  570. }
  571. }
  572. }
  573. }
  574. else//dlt645读取指令
  575. {
  576. protocol=1;//决定当前使用modbus=2回调函数还是dlt645=1
  577. float value;
  578. uint8_t minute, hour, day, month, year;
  579. uint8_t read_buf[10];
  580. memset(read_buf, 0, 10);
  581. if(read_dlt645_command==NULL)read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  582. dlt645_set_addr(&dlt645, read_dlt645_command->deviceID645);
  583. int8_t rs ;
  584. if(currentDevice->protocol==DLT645_07)
  585. {
  586. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_2007);
  587. }else if(currentDevice->protocol==DLT645_97)
  588. {
  589. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_1997);
  590. }
  591. if (rs != -1)
  592. {
  593. if (rs <= 4)
  594. {
  595. memcpy(&value, read_buf, 4);
  596. sprintf((char *)payload_out,
  597. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":%.2f}",
  598. action1,
  599. read_dlt645_command->keyword,
  600. currentDevice->deviceID, messageId, 1, value);
  601. break;
  602. }
  603. else if (rs == 5)
  604. {
  605. year = read_buf[4];
  606. month = read_buf[3];
  607. day = read_buf[2];
  608. hour = read_buf[1];
  609. minute = read_buf[0];
  610. sprintf((char *)payload_out,
  611. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":\"%02X%02X%02X%02X%02X\"}",
  612. action1,
  613. read_dlt645_command->keyword,
  614. currentDevice->deviceID, messageId, 1, year, month, day, hour, minute);
  615. break;
  616. }
  617. else if (rs > 5)
  618. {
  619. memcpy(&value, read_buf, 4);
  620. year = read_buf[8];
  621. month = read_buf[7];
  622. day = read_buf[6];
  623. hour = read_buf[5];
  624. minute = read_buf[4];
  625. sprintf((char *)payload_out,
  626. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":\"%02X%02X%02X%02X%02X%.2f\"}",
  627. action1,
  628. read_dlt645_command->keyword,
  629. currentDevice->deviceID, messageId, 1, year, month, day, hour, minute, value);
  630. break;
  631. }
  632. }
  633. else
  634. {
  635. // dlt645读取失败的json组成
  636. sprintf((char *)payload_out,
  637. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  638. action1,
  639. read_dlt645_command->keyword,
  640. currentDevice->deviceID, messageId, 0);
  641. break;
  642. }
  643. }
  644. }
  645. }
  646. }
  647. // 上传对应的响应信息
  648. sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  649. EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  650. WaitResponse("<", 3000);
  651. EC800MSendCmd(payload_out, strlen(payload_out));
  652. WaitResponse(RSP_OK, 3000);
  653. }
  654. cJSON_Delete(mqtt_sub_json);
  655. }
  656. }
  657. }
  658. // 模块下载download校验值
  659. static uint16_t checksum(const char *str, uint16_t len)
  660. {
  661. uint16_t sum = 0;
  662. uint8_t odd = 0;
  663. // 如果字符串长度为奇数,则将最后一个字符设置为高8位,低8位设置为0
  664. if (len % 2 == 1)
  665. {
  666. odd = 1;
  667. len--;
  668. }
  669. // 将每两个字符作为一个16位的数值进行异或操作
  670. for (uint16_t i = 0; i < len; i += 2)
  671. {
  672. sum ^= ((uint16_t)str[i] << 8) | (uint16_t)str[i + 1];
  673. }
  674. // 如果字符串长度为奇数,则还需要将最后一个字符与0xFF00异或
  675. if (odd)
  676. {
  677. sum ^= (uint16_t)str[len] << 8;
  678. }
  679. // 返回校验和
  680. return sum;
  681. }