dtu_uart2.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-28 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include "hmi.h"
  12. #include <board.h>
  13. #include <rtdevice.h>
  14. #include "stdarg.h"
  15. #include "string.h"
  16. #include "stdio.h"
  17. #include "dtu_uart2.h"
  18. #include <cJSON.h>
  19. #include "control.h"
  20. #include "sht30_collect.h"
  21. #include <easyflash.h>
  22. #include "sound.h"
  23. #include <ymodem.h>
  24. #include "beep.h"
  25. #define DBG_ENABLE
  26. #define DBG_COLOR
  27. #define DBG_TAG "dtu"
  28. #define DBG_LEVEL DBG_LOG
  29. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  30. #define DTU_THREAD_PRIORITY 6 // 线程优先级
  31. #define DTU_THREAD_STACK_SIZE 2048 // 线程栈空间大小
  32. #define DTU_THREAD_TIMESLICE 5 // 时间片
  33. Device_Status_typedef device_status;
  34. DataTemp data_arry;
  35. void list_mem(void);
  36. /* 串口接收消息结构*/
  37. struct rx_msg
  38. {
  39. rt_device_t dev;
  40. rt_size_t size;
  41. };
  42. /* 消息队列句柄 */
  43. static struct rt_messagequeue rx_mq_dtu;
  44. rt_thread_t dtu_recv_data = RT_NULL;
  45. rt_thread_t dtu_send_data = RT_NULL;
  46. rt_device_t dtuserial; /*串口设备句柄*/
  47. struct serial_configure dtuconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  48. static uint8_t makejson_sta = 0;
  49. /**
  50. * @brief 初始化用于与上层dtu通信的串口 - 串口2
  51. *
  52. * @return int 成功返回RT_EOK
  53. */
  54. static int DTU_uart2_init(void)
  55. {
  56. dtuserial = rt_device_find(DTU_UART_NAME);
  57. /*修改串口配置参数*/
  58. dtuconfig.baud_rate = BAUD_RATE_115200; // 修改波特率9600
  59. dtuconfig.data_bits = DATA_BITS_8; // 数据位8c
  60. dtuconfig.stop_bits = STOP_BITS_1; // 停止位1
  61. dtuconfig.bufsz = 1024; // 缓冲区1k
  62. dtuconfig.parity = PARITY_NONE; // 奇校验
  63. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  64. rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
  65. /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
  66. rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);
  67. return RT_EOK;
  68. }
  69. /*数据发送*/
  70. /**
  71. * @brief 将组包完成后的json字符串发送到dtu
  72. *
  73. * @param fmt 字符串模型
  74. * @param ... 可变参数
  75. */
  76. void DTU_Write(char *fmt, ...)
  77. {
  78. const char dtu_conf[4] = {device_status.vol, 0x00, 0x00, 0x00};
  79. va_list arg;
  80. static char dtu_str[800];
  81. rt_size_t str_length;
  82. va_start(arg, fmt);
  83. str_length = rt_vsnprintf(dtu_str, sizeof(dtu_str) - 1, fmt, arg);
  84. if (str_length > 800 - 1)
  85. str_length = 800 - 1;
  86. rt_device_write(dtuserial, 0, dtu_str, str_length);
  87. if (strstr(dtu_str, "volume") != RT_NULL)
  88. {
  89. rt_device_write(dtuserial, 0, dtu_conf, 4);
  90. }
  91. else if (strstr(dtu_str, "reset") != RT_NULL)
  92. {
  93. const char dtu_conf[4] = {7, 0x00, 0x00, 0x00};
  94. rt_device_write(dtuserial, 0, dtu_conf, 4);
  95. }
  96. va_end(arg);
  97. memset(dtu_str, '\0', sizeof(dtu_str));
  98. }
  99. /* 接收数据回调函数 */
  100. static rt_err_t dtu_input(rt_device_t dev, rt_size_t size)
  101. {
  102. struct rx_msg msg;
  103. rt_err_t result;
  104. msg.dev = dev;
  105. msg.size = size;
  106. result = rt_mq_send(&rx_mq_dtu, &msg, sizeof(struct rx_msg));
  107. if (result == -RT_EFULL)
  108. {
  109. /* 消息队列满 */
  110. rt_kprintf("message queue full!\n");
  111. }
  112. return result;
  113. }
  114. /* 入口函数-指令接收*/
  115. /**
  116. * @brief dtu数据的接收线程
  117. *
  118. * @param parameter NULL
  119. */
  120. static void dtu_thread_entry_recv(void *parameter)
  121. {
  122. struct rx_msg msg;
  123. rt_err_t result;
  124. rt_uint32_t rx_length;
  125. rt_uint32_t rx_err = 0;
  126. static char str_check[2] = {'{', '}'};
  127. static char rx_buffer[512];
  128. static char rx_buffer_tmp[512];
  129. static char rx_buffer_tmp2[512];
  130. while (1)
  131. {
  132. rt_memset(&msg, 0, sizeof(msg));
  133. // 从消息队列中获取被回调函数放入消息队列中的内容
  134. result = rt_mq_recv(&rx_mq_dtu, &msg, sizeof(struct rx_msg), 100);
  135. if (result == RT_EOK)
  136. {
  137. rx_err = 0;
  138. rx_length = (sizeof(rx_buffer) - 1) > msg.size ? msg.size : sizeof(rx_buffer) - 1;
  139. rx_length = rt_device_read(msg.dev, 0, &rx_buffer[0], rx_length);
  140. rx_buffer[rx_length] = '\0';
  141. LOG_D("recv data:%s", rx_buffer);
  142. if (strstr(rx_buffer, "{") == RT_NULL || strstr(rx_buffer, "}") == RT_NULL)
  143. {
  144. HMI_WRITE("sysTest.result.txt=\"%s\"", rx_buffer);
  145. }
  146. // 过滤异常数据
  147. char str_check_head[2];
  148. char str_check_end[2];
  149. rt_sprintf(str_check_head, "%c", rx_buffer[0]);
  150. rt_sprintf(str_check_end, "%c", rx_buffer[rx_length - 1]);
  151. if (str_check_end[0] == str_check[1] && str_check_head[0] == str_check[0])
  152. {
  153. parseDtu(rx_buffer);
  154. rt_memset(rx_buffer, 0, sizeof(rx_buffer));
  155. rt_memset(rx_buffer_tmp, 0, sizeof(rx_buffer_tmp));
  156. rt_memset(rx_buffer_tmp2, 0, sizeof(rx_buffer_tmp2));
  157. }
  158. else
  159. {
  160. if (rx_buffer[rx_length - 1] != '}')
  161. {
  162. rt_sprintf(rx_buffer_tmp, "%s", rx_buffer);
  163. }
  164. else
  165. {
  166. rt_sprintf(rx_buffer_tmp2, "%s", rx_buffer);
  167. rt_sprintf(rx_buffer, "%s%s", rx_buffer_tmp, rx_buffer_tmp2);
  168. // rt_kprintf("rxbuffer:%s",rx_buffer);
  169. parseDtu(rx_buffer);
  170. rt_memset(rx_buffer, 0, sizeof(rx_buffer));
  171. rt_memset(rx_buffer_tmp, 0, sizeof(rx_buffer_tmp));
  172. rt_memset(rx_buffer_tmp2, 0, sizeof(rx_buffer_tmp2));
  173. }
  174. // rt_kprintf("%s",rx_buffer);
  175. }
  176. }
  177. else
  178. {
  179. rx_err++;
  180. if (rx_err == 54000) // 90分钟未接收到数据
  181. {
  182. if (device_status.version_type == 6)
  183. {
  184. LOG_E("RK3399 transter Error!");
  185. ctr_photo(OFF);
  186. }
  187. else
  188. {
  189. ctr_RTU_DTU(OFF);
  190. }
  191. }
  192. else if (rx_err == 54100 && device_status.bat_status == 0)
  193. {
  194. LOG_W("通信中断,RTU重新上电");
  195. rx_err = 0;
  196. ctr_RTU_DTU(ON);
  197. ctr_photo(ON);
  198. }
  199. }
  200. // rt_kprintf("%s\n",rx_json_buffer);
  201. // 将内容写回到串口1
  202. // rt_device_write(dtuserial, 0, &rx_json_buffer[0], rx_length);
  203. }
  204. }
  205. /*入口函数-指令发送*/
  206. /**
  207. * @brief dtu数据的发送线程
  208. *
  209. * @param parameter NULL
  210. */
  211. static void dtu_thread_entry_send(void *parameter)
  212. {
  213. device_status.power_status = 1;
  214. device_status.num_updata = 0;
  215. rt_thread_delay(10000);
  216. static rt_uint16_t wait_i = 0;
  217. static rt_uint8_t check_i = 0;
  218. static rt_uint8_t power_i = 0;
  219. uint8_t log_en_inr = 30; // 打印log的速度 数值越小 打印越频繁
  220. static int8_t log_en_cnt = -1;
  221. while (1)
  222. {
  223. if (atoi(device_status.net) == 0)
  224. {
  225. if (device_status.rain_status == 1 || device_status.temp_status == 1)
  226. {
  227. if (check_i == 0)
  228. {
  229. check_i = 1;
  230. device_status.num_updata = 0;
  231. }
  232. }
  233. else
  234. {
  235. check_i = 0;
  236. }
  237. if (device_status.num_updata != 0)
  238. {
  239. device_status.num_updata--;
  240. // 屏幕休眠
  241. if (device_status.num_updata == (device_status.dattim * 60 - 10))
  242. {
  243. HMI_WRITE("sleep=%d", 1);
  244. }
  245. // 检测到电源掉电
  246. if (atoi(device_status.voltage) == 0 && device_status.power_status == 1)
  247. {
  248. power_i++;
  249. // 确定网络状态
  250. if (power_i == 10)
  251. {
  252. rt_sprintf(device_status.net, "1");
  253. HMI_WRITE("sleep=%d", 1); // 屏幕休眠
  254. DTU_Write("%s", makeJson_Warn("power down", "power"));
  255. device_status.power_status = 0;
  256. rt_thread_delay(5000);
  257. ctr_router(OFF);
  258. ctr_RTU_DTU(OFF);
  259. power_i = 0;
  260. }
  261. }
  262. else
  263. {
  264. power_i = 0;
  265. ctr_router(ON);
  266. ctr_RTU_DTU(ON);
  267. }
  268. }
  269. else
  270. {
  271. char *readinfo = makeJson_ReadInfo();
  272. rt_kprintf("upload data:%s\n", readinfo);
  273. DTU_Write("%s", readinfo);
  274. rt_free(readinfo);
  275. rt_thread_delay(1000);
  276. UpLoad_Data();
  277. if (atoi(device_status.fuse_voltage) < 1)
  278. {
  279. rt_thread_delay(500);
  280. static char res[30];
  281. sprintf(res, "Fuse is fusing:%s", device_status.fuse_voltage);
  282. char *res_fuse_warn = makeJson_Warn(res, "device");
  283. DTU_Write("%s", res_fuse_warn);
  284. rt_free(res_fuse_warn);
  285. }
  286. device_status.num_updata = device_status.dattim * 60;
  287. if (device_status.dattim == 0)
  288. device_status.dattim = 20; // 数据上传时间间隔设置为0?
  289. if (device_status.bat_status == 0)
  290. device_status.power_status = 1; // 解除欠压,清除标志位
  291. }
  292. }
  293. else
  294. {
  295. wait_i++;
  296. if (chk_log_time)
  297. {
  298. LOG_I("wait connect internet......%d", wait_i);
  299. }
  300. // 检测到电源掉电
  301. if (atoi(device_status.voltage) == 0 && device_status.power_status == 1 && wait_i > 200)
  302. {
  303. check_i++;
  304. LOG_W("断网状态下检测到设备断电!%d", atoi(device_status.voltage));
  305. if (check_i == 10)
  306. {
  307. device_status.power_status = 0;
  308. rt_thread_delay(5000);
  309. ctr_router(OFF);
  310. ctr_RTU_DTU(OFF);
  311. check_i = 0;
  312. }
  313. }
  314. if (Read_Real_Voltage() / 100 > 10 && device_status.power_status == 0)
  315. {
  316. check_i = 0;
  317. LOG_I("电源恢复正常,打开路由器和RTU供电!");
  318. device_status.power_status = 1;
  319. // 打开路由器和RTU供电
  320. ctr_router(ON);
  321. ctr_RTU_DTU(ON);
  322. }
  323. }
  324. rt_thread_mdelay(1000);
  325. }
  326. }
  327. /* 创建线程 */
  328. /**
  329. * @brief 用于创建dtu通信相关的线程
  330. *
  331. * 该线程在主线程中-读取完保存的设备参数之后调用
  332. *
  333. * @return int 完成后返回RT_EOK
  334. */
  335. int dtu_data_play(void)
  336. {
  337. rt_err_t rm_dtu_result;
  338. DTU_uart2_init();
  339. // 初始化消息队列
  340. rm_dtu_result = rt_mq_init(&rx_mq_dtu, "dtu", &data_arry.msg_pool[0], 128 - sizeof(void *), sizeof(data_arry.msg_pool), RT_IPC_FLAG_FIFO);
  341. if (rm_dtu_result == RT_EOK)
  342. {
  343. rt_kprintf("消息队列创建成功!!");
  344. }
  345. else
  346. {
  347. rt_kprintf("消息队列创建失败!!");
  348. }
  349. /* 设置接收回调函数 */
  350. rt_device_set_rx_indicate(dtuserial, dtu_input);
  351. /*创建线程-指令发送*/
  352. dtu_send_data = rt_thread_create("DTU_Uart_thread_send",
  353. dtu_thread_entry_send, RT_NULL,
  354. DTU_THREAD_STACK_SIZE,
  355. 3, // 优先级高是为了保证数据上传的频率的精确度
  356. 20 // 时间片
  357. );
  358. /* 创建线程-指令接收解析*/
  359. dtu_recv_data = rt_thread_create("DTU_Uart_thread_recv",
  360. dtu_thread_entry_recv, RT_NULL,
  361. DTU_THREAD_STACK_SIZE,
  362. DTU_THREAD_PRIORITY,
  363. DTU_THREAD_TIMESLICE);
  364. /* 如果获得线程控制块,启动这个线程 */
  365. if (dtu_send_data != RT_NULL)
  366. rt_thread_startup(dtu_send_data);
  367. /* 如果获得线程控制块,启动这个线程 */
  368. if (dtu_recv_data != RT_NULL)
  369. rt_thread_startup(dtu_recv_data);
  370. return RT_EOK;
  371. }
  372. /**
  373. * @brief 组包读取设备信息的json
  374. *
  375. * @return char* 组包完成后的字符串指针
  376. */
  377. char *makeJson_ReadInfo()
  378. {
  379. cJSON *rJsonRoot = NULL;
  380. rJsonRoot = cJSON_CreateObject();
  381. if (NULL == rJsonRoot)
  382. {
  383. return NULL;
  384. }
  385. cJSON_AddStringToObject(rJsonRoot, "uart", "getinfo");
  386. char *r = cJSON_PrintUnformatted(rJsonRoot);
  387. if (NULL == r)
  388. {
  389. cJSON_Delete(rJsonRoot);
  390. return NULL;
  391. }
  392. cJSON_Delete(rJsonRoot);
  393. return r;
  394. }
  395. //{"cmd": "platform", "ext": {"type": "120.27.222.26"}}
  396. /**
  397. * @brief 组包平台参数的json
  398. *
  399. * @param plt 平台参数的内容
  400. * @return char* 组包完成后的字符串指针
  401. */
  402. char *makeJson_platform(char *plt)
  403. {
  404. cJSON *platform = NULL;
  405. platform = cJSON_CreateObject();
  406. if (NULL == platform)
  407. {
  408. return NULL;
  409. }
  410. cJSON_AddStringToObject(platform, "cmd", "platform");
  411. cJSON *extstr = NULL;
  412. extstr = cJSON_CreateObject();
  413. if (NULL == extstr)
  414. {
  415. return NULL;
  416. }
  417. cJSON_AddItemToObject(platform, "ext", extstr);
  418. cJSON_AddStringToObject(extstr, "type", plt);
  419. char *p_str = cJSON_PrintUnformatted(platform);
  420. if (NULL == p_str)
  421. {
  422. cJSON_Delete(platform);
  423. return NULL;
  424. }
  425. cJSON_Delete(platform);
  426. return p_str;
  427. }
  428. /**
  429. * @brief 组包拍照指令的json
  430. *
  431. * @return char* 完成后的指针
  432. */
  433. char *makeJson_takephoto()
  434. {
  435. cJSON *takephoto = NULL;
  436. takephoto = cJSON_CreateObject();
  437. if (NULL == takephoto)
  438. {
  439. return NULL;
  440. }
  441. cJSON_AddStringToObject(takephoto, "cmd", "takephoto");
  442. char *takephoto_str = cJSON_PrintUnformatted(takephoto);
  443. if (NULL == takephoto_str)
  444. {
  445. cJSON_Delete(takephoto);
  446. return NULL;
  447. }
  448. cJSON_Delete(takephoto);
  449. return takephoto_str;
  450. }
  451. /**
  452. * @brief 组包RTU拍照的json
  453. *
  454. * @return char* 完成后的指针
  455. */
  456. char *makeJson_Rtutakephoto()
  457. {
  458. cJSON *tJsonRoot = NULL;
  459. tJsonRoot = cJSON_CreateObject();
  460. if (NULL == tJsonRoot)
  461. {
  462. return NULL;
  463. }
  464. cJSON_AddStringToObject(tJsonRoot, "cmd", "ctrl");
  465. cJSON *tpubstring = NULL;
  466. tpubstring = cJSON_CreateObject();
  467. if (NULL == tpubstring)
  468. {
  469. LOG_E("cJSON tpubstring NULL!!!\n");
  470. return NULL;
  471. }
  472. cJSON_AddItemToObject(tJsonRoot, "ext", tpubstring);
  473. cJSON_AddStringToObject(tpubstring, "takephoto", "2");
  474. char *t = cJSON_PrintUnformatted(tJsonRoot);
  475. if (NULL == t)
  476. {
  477. cJSON_Delete(tJsonRoot);
  478. return NULL;
  479. }
  480. cJSON_Delete(tJsonRoot);
  481. return t;
  482. }
  483. // 预警消息
  484. /**
  485. * @brief 组包预警信息的json
  486. *
  487. * @param s status
  488. * @param t type
  489. * @return char* 完成后的指针
  490. */
  491. char *makeJson_Warn(char *s, char *t)
  492. {
  493. cJSON *wJsonRoot = NULL;
  494. wJsonRoot = cJSON_CreateObject();
  495. if (NULL == wJsonRoot)
  496. {
  497. return NULL;
  498. }
  499. cJSON_AddStringToObject(wJsonRoot, "cmd", "warn");
  500. cJSON *pubstring = NULL;
  501. pubstring = cJSON_CreateObject();
  502. if (NULL == pubstring)
  503. {
  504. rt_kprintf("cJSON pubstring NULL!!!\n");
  505. return NULL;
  506. }
  507. cJSON_AddItemToObject(wJsonRoot, "ext", pubstring);
  508. cJSON_AddStringToObject(pubstring, "status", s); //
  509. cJSON_AddStringToObject(pubstring, "type", t);
  510. char *w = cJSON_PrintUnformatted(wJsonRoot);
  511. if (NULL == w)
  512. {
  513. cJSON_Delete(wJsonRoot);
  514. rt_kprintf("wJsonRoot cJSON_Delete NULL!!!\n");
  515. return NULL;
  516. }
  517. cJSON_Delete(wJsonRoot);
  518. return w;
  519. }
  520. /*生成JSON数据*/
  521. /**
  522. * @brief 组包设备状态信息的json
  523. *
  524. * @return char* 完成后的指针
  525. */
  526. char *makeJson()
  527. {
  528. char utos[5];
  529. if (device_status.gps == NULL)
  530. rt_sprintf(device_status.gps, "0");
  531. // 如果设备工作并且电流小于500,并且不是上电启动灯管异常
  532. if (atoi(device_status.power) < 400 && atoi(device_status.power) != 0 && device_status.work_status == 1 && makejson_sta != 0)
  533. {
  534. #if LAMP_EN == 0
  535. LOG_I("Current is:%d", atoi(device_status.power));
  536. if (atoi(device_status.power) > 10) // 过滤电流检测芯片损坏导致电流直为0 的异常
  537. device_status.lamp_status = 1;
  538. #endif
  539. }
  540. else
  541. {
  542. device_status.lamp_status = 0;
  543. }
  544. if (atoi(device_status.power) > 5000)
  545. rt_sprintf(device_status.power, "0.01");
  546. makejson_sta = 1;
  547. time_t stamp = time(RT_NULL);
  548. static struct tm tm;
  549. gmtime_r(&stamp, &tm);
  550. rt_sprintf(device_status.stamp, "%04d%02d%02d%02d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  551. cJSON *pJsonRoot = NULL;
  552. pJsonRoot = cJSON_CreateObject();
  553. if (NULL == pJsonRoot)
  554. {
  555. rt_kprintf("cJSON pJsonRoot NULL!!!\n");
  556. return NULL;
  557. }
  558. if (strstr(device_status.project, "A7_DTU_V3") != RT_NULL)
  559. {
  560. cJSON_AddStringToObject(pJsonRoot, "cmd", "read_data");
  561. }
  562. else
  563. {
  564. cJSON_AddStringToObject(pJsonRoot, "cmd", "data");
  565. }
  566. cJSON *pSubJson = NULL;
  567. pSubJson = cJSON_CreateObject();
  568. if (NULL == pSubJson)
  569. {
  570. rt_kprintf("cJSON pSubJson NULL!!!\n");
  571. return NULL;
  572. }
  573. memset(device_status.driver, 0, sizeof(device_status.driver));
  574. if (device_status.version_type == 1)
  575. {
  576. rt_sprintf(device_status.driver, "%s-%s", VERSION_ZP, device_status.driver_4g);
  577. }
  578. else if (device_status.version_type == 4)
  579. {
  580. rt_sprintf(device_status.driver, "%s-%s", VERSION_HK_2000W, device_status.driver_4g);
  581. }
  582. else if (device_status.version_type == 41)
  583. {
  584. rt_sprintf(device_status.driver, "%s-%s", VERSION_HK, device_status.driver_4g);
  585. }
  586. else if (device_status.version_type == 11)
  587. {
  588. rt_sprintf(device_status.driver, "%s-%s", VERSION_GK, device_status.driver_4g);
  589. }
  590. else if (device_status.version_type == 5)
  591. {
  592. rt_sprintf(device_status.driver, "%s-%s", VERSION_V5, device_status.driver_4g);
  593. }
  594. else if (device_status.version_type == 6)
  595. {
  596. rt_sprintf(device_status.driver, "%s-%s", VERSION_XCT, device_status.driver_4g);
  597. }
  598. else if (device_status.version_type == vt_BZY || device_status.version_type == vt_BZY_JD)
  599. {
  600. rt_sprintf(device_status.driver, "%s-%s", VERSION_BZY, device_status.driver_4g);
  601. }
  602. if (device_status.sensor_485_sta != 0)
  603. {
  604. if (strstr(device_status.driver, "485") == RT_NULL)
  605. strcat(device_status.driver, ".485");
  606. }
  607. cJSON_AddItemToObject(pJsonRoot, "ext", pSubJson);
  608. cJSON_AddStringToObject(pSubJson, "dver", device_status.driver); // 版本号,字符型
  609. cJSON_AddStringToObject(pSubJson, "proj", device_status.project); // 项目类型
  610. // 设备类型
  611. rt_sprintf(utos, "%d", device_status.dtype);
  612. cJSON_AddStringToObject(pSubJson, "dtype", utos);
  613. // 类型细分
  614. if (device_status.version_type == 4) // 水稻
  615. {
  616. rt_sprintf(utos, "4");
  617. cJSON_AddStringToObject(pSubJson, "vtype", utos);
  618. }
  619. else if (device_status.version_type == 1)
  620. { // 1.0
  621. rt_sprintf(utos, "1");
  622. cJSON_AddStringToObject(pSubJson, "vtype", utos);
  623. }
  624. else if (device_status.version_type == 11)
  625. { // 高空
  626. rt_sprintf(utos, "11");
  627. cJSON_AddStringToObject(pSubJson, "vtype", utos);
  628. }
  629. else if (device_status.version_type == 5)
  630. { // 5.0
  631. rt_sprintf(utos, "5");
  632. cJSON_AddStringToObject(pSubJson, "vtype", utos);
  633. }
  634. else if (device_status.version_type == 6)
  635. { // 吸虫塔
  636. rt_sprintf(utos, "6");
  637. cJSON_AddStringToObject(pSubJson, "vtype", utos);
  638. }
  639. else if (device_status.version_type == 6)
  640. { // 孢子仪
  641. rt_sprintf(utos, "A");
  642. cJSON_AddStringToObject(pSubJson, "vtype", utos);
  643. }
  644. if (device_status.version_type != 6 && device_status.dtype != 7)
  645. {
  646. // 光控定时
  647. rt_sprintf(utos, "%d", device_status.sunBar);
  648. cJSON_AddStringToObject(pSubJson, "tt", utos);
  649. // 定时模式
  650. rt_sprintf(utos, "%d", device_status.timeSwitch);
  651. cJSON_AddStringToObject(pSubJson, "ts", utos);
  652. // 温控
  653. rt_sprintf(utos, "%d", device_status.temp_status);
  654. cJSON_AddStringToObject(pSubJson, "tps", utos);
  655. // 光控状态
  656. rt_sprintf(utos, "%d", device_status.sun_status);
  657. cJSON_AddStringToObject(pSubJson, "lps", utos);
  658. // 雨控状态
  659. rt_sprintf(utos, "%d", device_status.rain_status);
  660. cJSON_AddStringToObject(pSubJson, "rps", utos);
  661. // 电池状态
  662. rt_sprintf(utos, "%d", device_status.bat_status);
  663. cJSON_AddStringToObject(pSubJson, "batStatus", utos);
  664. // 灯管状态
  665. rt_sprintf(utos, "%d", device_status.lamp_status);
  666. cJSON_AddStringToObject(pSubJson, "lamp", utos);
  667. // 上仓门状态 1-打开,0-关闭-(通道) 打开为落虫,关闭为排水
  668. rt_sprintf(utos, "%d", device_status.up_status);
  669. cJSON_AddStringToObject(pSubJson, "upds", utos);
  670. // 下仓门状态 1-打开,0-关闭
  671. rt_sprintf(utos, "%d", device_status.down_status);
  672. cJSON_AddStringToObject(pSubJson, "dnds", utos);
  673. // 加热仓温度
  674. if (device_status.run_sta > device_status.heattem)
  675. {
  676. rt_sprintf(utos, "%d", device_status.run_sta);
  677. device_status.run_sta = 0;
  678. }
  679. else
  680. {
  681. rt_sprintf(utos, "%d", device_status.heattem);
  682. }
  683. cJSON_AddStringToObject(pSubJson, "hrt", utos);
  684. // 加热状态
  685. rt_sprintf(utos, "%d", device_status.heat_status);
  686. cJSON_AddStringToObject(pSubJson, "hs", utos);
  687. // 收集时间(参数)
  688. rt_sprintf(utos, "%d", device_status.clt_time);
  689. cJSON_AddStringToObject(pSubJson, "collt", utos);
  690. // 加热时间(参数)
  691. rt_sprintf(utos, "%d", device_status.heattime);
  692. cJSON_AddStringToObject(pSubJson, "htim", utos);
  693. // 加热温度(参数)
  694. rt_sprintf(utos, "%d", device_status.heatvalue);
  695. cJSON_AddStringToObject(pSubJson, "hst", utos);
  696. // 高温保护
  697. rt_sprintf(utos, "%d", device_status.tph);
  698. cJSON_AddStringToObject(pSubJson, "tph", utos);
  699. // 低温保护
  700. rt_sprintf(utos, "%d", device_status.tpl);
  701. cJSON_AddStringToObject(pSubJson, "tpl", utos);
  702. }
  703. if (device_status.dtype == 3)
  704. {
  705. // 工作状态
  706. rt_sprintf(utos, "%d", device_status.work_status);
  707. cJSON_AddStringToObject(pSubJson, "ws", utos);
  708. // 保险丝电压
  709. cJSON_AddStringToObject(pSubJson, "fuse_voltage", device_status.fuse_voltage);
  710. // 开始时间
  711. rt_sprintf(utos, "%d", device_status.startBar);
  712. cJSON_AddStringToObject(pSubJson, "st", utos);
  713. // 结束时间
  714. rt_sprintf(utos, "%d", device_status.stopBar);
  715. cJSON_AddStringToObject(pSubJson, "et", utos);
  716. }
  717. else if (device_status.dtype == 7)
  718. {
  719. char ct1[10], ct2[10];
  720. rt_sprintf(ct1, "%d-%d", device_status.collecting_sh1, device_status.collecting_eh1);
  721. rt_sprintf(ct2, "%d-%d", device_status.collecting_sh2, device_status.collecting_eh2);
  722. rt_kprintf("ct1-ct2:%s\n", ct1);
  723. device_status.coll_time[0] = ct1;
  724. device_status.coll_time[1] = ct2;
  725. cJSON *data_clt = NULL;
  726. data_clt = cJSON_CreateStringArray(device_status.coll_time, 2);
  727. // 工作状态
  728. rt_sprintf(utos, "%d", device_status.work_status);
  729. cJSON_AddStringToObject(pSubJson, "work_sta", utos);
  730. // 运行状态
  731. rt_sprintf(utos, "%d", device_status.run_sta);
  732. cJSON_AddStringToObject(pSubJson, "run_sta", utos);
  733. // 箱体温度-box_tem
  734. rt_sprintf(utos, "%d", device_status.box_temp);
  735. cJSON_AddStringToObject(pSubJson, "box_tem", utos);
  736. // 培养时间-cul_time
  737. rt_sprintf(utos, "%d", device_status.cul_time);
  738. cJSON_AddStringToObject(pSubJson, "cul_time", utos);
  739. // 制冷状态
  740. rt_sprintf(utos, "%d", device_status.cold_sw);
  741. cJSON_AddStringToObject(pSubJson, "cold_sw", utos);
  742. // 电压状态-batStatus
  743. rt_sprintf(utos, "%d", device_status.bat_status);
  744. cJSON_AddStringToObject(pSubJson, "batStatus", utos);
  745. // 已培养时间-staytime
  746. rt_sprintf(utos, "%d", device_status.stay_time);
  747. cJSON_AddStringToObject(pSubJson, "stay_time", utos);
  748. // 风机开关
  749. rt_sprintf(utos, "%d", device_status.wind_sw);
  750. cJSON_AddStringToObject(pSubJson, "wind_sw", utos);
  751. // 培养仓温度
  752. rt_sprintf(utos, "%d", device_status.pre_temp);
  753. cJSON_AddStringToObject(pSubJson, "pre_temp", utos);
  754. // 分辨率
  755. cJSON_AddStringToObject(pSubJson, "imgres", "0");
  756. // usb
  757. cJSON_AddStringToObject(pSubJson, "usb_sta", "0");
  758. // 雨控状态-rps
  759. rt_sprintf(utos, "%d", device_status.rain_status);
  760. cJSON_AddStringToObject(pSubJson, "rps", utos);
  761. // 收集时间
  762. cJSON_AddItemToObject(pSubJson, "coll_time", data_clt);
  763. // 设定温度
  764. rt_sprintf(utos, "%d", device_status.heatvalue);
  765. cJSON_AddStringToObject(pSubJson, "set_temp", utos);
  766. }
  767. if (device_status.dtype != 7)
  768. {
  769. // 震动开关
  770. rt_sprintf(utos, "%d", device_status.shake_ws);
  771. cJSON_AddStringToObject(pSubJson, "shake", utos);
  772. }
  773. // 震动时间
  774. rt_sprintf(utos, "%d", device_status.shake_sec);
  775. cJSON_AddStringToObject(pSubJson, "shake_sec", utos);
  776. cJSON_AddStringToObject(pSubJson, "gps", device_status.gps); // 定位状态
  777. // 开关状态
  778. rt_sprintf(utos, "%d", device_status.ds);
  779. cJSON_AddStringToObject(pSubJson, "ds", utos);
  780. // 湿度
  781. cJSON_AddStringToObject(pSubJson, "ah", device_status.humidity);
  782. // 温度
  783. cJSON_AddStringToObject(pSubJson, "at", device_status.tempture);
  784. cJSON_AddStringToObject(pSubJson, "imei", device_status.imei); // IMEI
  785. cJSON_AddStringToObject(pSubJson, "iccid", device_status.iccid); // ICCID
  786. cJSON_AddStringToObject(pSubJson, "stamp", device_status.stamp); // 时间
  787. // 经度
  788. cJSON_AddStringToObject(pSubJson, "lng", device_status.lng);
  789. // 纬度
  790. cJSON_AddStringToObject(pSubJson, "lat", device_status.lat);
  791. // 是否为上电开机
  792. cJSON_AddNumberToObject(pSubJson, "rcnt", device_status.rebootcnt);
  793. // 电流(当前)
  794. cJSON_AddStringToObject(pSubJson, "current", device_status.power);
  795. // 电压
  796. cJSON_AddStringToObject(pSubJson, "vbat", device_status.voltage);
  797. // 数据上传间隔
  798. rt_sprintf(utos, "%d", device_status.dattim);
  799. cJSON_AddStringToObject(pSubJson, "dat_f", utos);
  800. // 信号强度
  801. cJSON_AddStringToObject(pSubJson, "csq", device_status.csq);
  802. char *p = cJSON_PrintUnformatted(pJsonRoot);
  803. if (NULL == p)
  804. {
  805. cJSON_Delete(pJsonRoot);
  806. rt_kprintf("cJSON_Delete NULL!!!\n");
  807. return NULL;
  808. }
  809. cJSON_Delete(pJsonRoot);
  810. return p;
  811. }
  812. // JSON数据解析
  813. /**
  814. * @brief 解析dtu发来的指令
  815. *
  816. * @param json_string 收到的json字符串
  817. * @return int 0 完成
  818. */
  819. int parseDtu(char *json_string)
  820. {
  821. extern int rt_hw_rtc_init(void);
  822. char *json_s = NULL;
  823. cJSON *item_cmd = NULL; // 嵌套ext
  824. cJSON *item_ext = NULL; // 嵌套ext
  825. cJSON *hst = NULL; // 加热温度
  826. cJSON *collt = NULL; // 收集时间
  827. cJSON *et = NULL; // 结束时间
  828. cJSON *datt = NULL; // 数据上传间隔
  829. cJSON *shake_sec = NULL; // 震动时间
  830. cJSON *tpl = NULL; // 低温保护阈值
  831. cJSON *shake = NULL; // 震动开关
  832. cJSON *tph = NULL; // 高温保护阈值
  833. cJSON *ts = NULL; // 工作模式
  834. cJSON *st = NULL; // 开始时间
  835. cJSON *tt = NULL; // 光控定时时长
  836. cJSON *htim = NULL; // 加热时间
  837. cJSON *gps = NULL; // 定位状态
  838. cJSON *datetime = NULL;
  839. cJSON *lat = NULL;
  840. cJSON *lng = NULL;
  841. cJSON *csq = NULL;
  842. cJSON *net = NULL;
  843. cJSON *iccid = NULL;
  844. cJSON *imei = NULL;
  845. cJSON *sim = NULL;
  846. cJSON *volume = NULL;
  847. cJSON *driver_4g = NULL; // 版本号
  848. cJSON *project = NULL; // Project
  849. cJSON *ds = NULL; // 设备状态
  850. cJSON *vt = NULL; // 版本类型
  851. // cJSON* account_status = NULL; //卡状态
  852. // cJSON* data_plan = NULL; //套餐
  853. // cJSON* data_usage = NULL; //已使用
  854. // cJSON* expiry_date = NULL; //到期时间
  855. cJSON *mqttcfg1 = NULL;
  856. cJSON *mqttcfg2 = NULL;
  857. cJSON *type = NULL;
  858. cJSON *hum = NULL;
  859. cJSON *tmp = NULL;
  860. cJSON *link_sta = NULL;
  861. cJSON *value_commn = NULL;
  862. rt_kprintf("%s\n", json_string);
  863. cJSON *root = cJSON_Parse(json_string);
  864. if (!root)
  865. {
  866. cJSON_Delete(root);
  867. rt_kprintf("parse json err:%s", json_string);
  868. return -1;
  869. }
  870. else
  871. {
  872. item_cmd = cJSON_GetObjectItem(root, "cmd");
  873. item_ext = cJSON_GetObjectItem(root, "ext");
  874. // item_data = cJSON_GetObjectItem(root,"data");
  875. if (item_cmd != RT_NULL)
  876. {
  877. if (strstr(item_cmd->valuestring, "takephoto") != RT_NULL)
  878. {
  879. // HK_TakePhoto();
  880. device_status.photo_ws = 1;
  881. char *photo = makeJson_Warn("takephoto", "control");
  882. DTU_Write("%s", photo);
  883. rt_free(photo);
  884. rt_thread_delay(2000);
  885. }
  886. else if (strstr(item_cmd->valuestring, "json_w_clock") != RT_NULL)
  887. {
  888. rt_kprintf("查询时间配置!\n");
  889. json_s = makeJson_Warn(device_status.json_w_clock, "主动查询");
  890. DTU_Write("%s", json_s);
  891. rt_free(json_s);
  892. rt_thread_delay(1000);
  893. }
  894. else if (strstr(item_cmd->valuestring, "json_w_reset") != RT_NULL)
  895. {
  896. rt_kprintf("查询重置配置!\n");
  897. json_s = makeJson_Warn(device_status.json_w_reset, "主动查询");
  898. DTU_Write("%s", json_s);
  899. rt_free(json_s);
  900. rt_thread_delay(1000);
  901. }
  902. else if (strstr(item_cmd->valuestring, "json_w_platform") != RT_NULL)
  903. {
  904. rt_kprintf("查询平台配置!\n");
  905. json_s = makeJson_Warn(device_status.json_w_platform, "主动查询");
  906. DTU_Write("%s", json_s);
  907. rt_free(json_s);
  908. rt_thread_delay(1000);
  909. }
  910. else if (strstr(item_cmd->valuestring, "shake") != RT_NULL)
  911. {
  912. ctr_shake(ON, 30);
  913. char *shake = makeJson_Warn("shake", "control");
  914. DTU_Write("%s", shake);
  915. rt_free(shake);
  916. rt_thread_delay(2000);
  917. }
  918. // 远程给rtu重新上电
  919. else if (strstr(item_cmd->valuestring, "ctrl") != RT_NULL)
  920. {
  921. type = cJSON_GetObjectItem(item_ext, "type");
  922. if (strstr(type->valuestring, "rtu_off_on") != RT_NULL)
  923. {
  924. LOG_W("DTU 12V Power down");
  925. ctr_RTU_DTU(OFF);
  926. rt_thread_delay(5000);
  927. ctr_RTU_DTU(ON);
  928. }
  929. // 对焦拍照
  930. else if (strstr(type->valuestring, "auto_takephoto") != RT_NULL)
  931. {
  932. device_status.autotakepic = 1;
  933. rt_thread_delay(2000);
  934. char *auto_takephoto = makeJson_Warn("send cmd success:auto_takephoto", "control");
  935. DTU_Write("%s", auto_takephoto);
  936. rt_free(auto_takephoto);
  937. }
  938. else if (strstr(type->valuestring, "takephoto") != RT_NULL)
  939. {
  940. HK_TakePhoto();
  941. char *photo = makeJson_Warn("takephoto", "control");
  942. DTU_Write("%s", photo);
  943. rt_free(photo);
  944. rt_thread_delay(2000);
  945. }
  946. //{"ext": {"type": "turn"}, "cmd": "ctrl"} 转仓
  947. else if (strstr(type->valuestring, "turn") != RT_NULL)
  948. {
  949. #if Color_EN == 0
  950. char *turn_zp = makeJson_Warn("send cmd success:turn_zp", "control");
  951. DTU_Write("%s", turn_zp);
  952. rt_free(turn_zp);
  953. ctr_turn(90, 0);
  954. #else
  955. DTU_Write("黄板复位:%d", ctr_Color_board(RESET, 800));
  956. #endif
  957. }
  958. }
  959. // 切换平台
  960. else if (strstr(item_cmd->valuestring, "platform") != RT_NULL)
  961. {
  962. type = cJSON_GetObjectItem(item_ext, "type");
  963. if (strstr(type->valuestring, "8.136.98.49") != RT_NULL)
  964. {
  965. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  966. ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
  967. // 给RTU发送切换指令
  968. DTU_Write("%s", makeJson_platform("8.136.98.49"));
  969. rt_free(makeJson_platform("8.136.98.49"));
  970. rt_thread_delay(1000);
  971. rt_sprintf(device_status.json_w_platform, "平台操作:切换平台>大数据,操作时间:%s", device_status.stamp);
  972. json_s = makeJson_Warn(device_status.json_w_platform, "切换平台");
  973. ef_set_env_blob("json_w_platform", &device_status.json_w_platform, sizeof(device_status.json_w_platform)); // 将修改屏幕参数操作记录保存到Flash中
  974. if (atoi(device_status.net) == 0)
  975. DTU_Write("%s", json_s);
  976. rt_free(json_s);
  977. rt_thread_delay(1000);
  978. MA_save();
  979. }
  980. else if (strstr(type->valuestring, "120.27.222.26") != RT_NULL)
  981. {
  982. rt_sprintf(device_status.mqttcfg1, "120.27.222.26");
  983. ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
  984. // 给RTU发送切换指令
  985. DTU_Write("%s", makeJson_platform("120.27.222.26"));
  986. rt_free(makeJson_platform("120.27.222.26"));
  987. rt_thread_delay(1000);
  988. rt_sprintf(device_status.json_w_platform, "平台操作:切换平台>四情,操作时间:%s", device_status.stamp);
  989. json_s = makeJson_Warn(device_status.json_w_platform, "切换平台");
  990. ef_set_env_blob("json_w_platform", &device_status.json_w_platform, sizeof(device_status.json_w_platform)); // 将修改屏幕参数操作记录保存到Flash中
  991. if (atoi(device_status.net) == 0)
  992. DTU_Write("%s", json_s);
  993. rt_free(json_s);
  994. rt_thread_delay(1000);
  995. MA_save();
  996. }
  997. }
  998. // 水稻2000万像素的串口拍照指令
  999. else if (strstr(item_cmd->valuestring, "mvs_photo") != RT_NULL)
  1000. {
  1001. HK_TakePhoto();
  1002. char *photo = makeJson_Warn("mvs_takephoto", "control");
  1003. DTU_Write("%s", photo);
  1004. rt_free(photo);
  1005. rt_thread_delay(2000);
  1006. }
  1007. else if (strstr(item_cmd->valuestring, "reboot") != RT_NULL)
  1008. {
  1009. char *restart = makeJson_Warn("sysrem will restart", "control");
  1010. rt_kprintf("%s\n", restart);
  1011. rt_thread_delay(2000);
  1012. DTU_Write("%s", restart);
  1013. rt_hw_cpu_reset();
  1014. }
  1015. else if (strstr(item_cmd->valuestring, "reset") != RT_NULL)
  1016. {
  1017. ResetParamDefault();
  1018. char *restart = makeJson_Warn("sysrem will reset", "control");
  1019. DTU_Write("%s", restart);
  1020. rt_free(restart);
  1021. rt_thread_delay(2000);
  1022. }
  1023. else if (strstr(item_cmd->valuestring, "update") != RT_NULL)
  1024. {
  1025. rt_kprintf("准备升级!!!");
  1026. device_status.update_flag = 1;
  1027. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1028. rt_kprintf("%s\n", makeJson_Warn("system will update", "control"));
  1029. rt_thread_delay(2000);
  1030. DTU_Write("%s", makeJson_Warn("system will update", "control"));
  1031. rt_thread_delay(20);
  1032. rt_hw_cpu_reset();
  1033. }
  1034. else if (strstr(item_cmd->valuestring, "read") != RT_NULL)
  1035. {
  1036. char *readinfo = makeJson_ReadInfo();
  1037. DTU_Write("%s", readinfo);
  1038. rt_kprintf("%s\n", readinfo);
  1039. rt_free(readinfo);
  1040. device_status.hmi_fresh = 2;
  1041. device_status.refresh_num = 15;
  1042. }
  1043. else if (strstr(item_cmd->valuestring, "poweron") != RT_NULL)
  1044. {
  1045. device_status.ds = 1;
  1046. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds)); //
  1047. UpLoad_Data();
  1048. }
  1049. else if (strstr(item_cmd->valuestring, "poweroff") != RT_NULL)
  1050. {
  1051. device_status.ds = 0;
  1052. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds)); //
  1053. UpLoad_Data();
  1054. }
  1055. // else if (strstr(item_cmd->valuestring,"log")!=RT_NULL) {
  1056. // rt_thread_mdelay(2000);
  1057. // //read_all_flash_log();
  1058. // }
  1059. // 设置温湿度
  1060. else if (strstr(item_cmd->valuestring, "sensor") != RT_NULL)
  1061. {
  1062. // 2022-04-12add:485传感器正常,不再解析服务器下发的温湿度数据
  1063. // if(device_status.sensor_485_sta!=0) return 0;
  1064. hum = cJSON_GetObjectItem(item_ext, "new_hum");
  1065. tmp = cJSON_GetObjectItem(item_ext, "new_tem");
  1066. if (hum != NULL && tmp != NULL)
  1067. {
  1068. // 先判断是否是空字符再判断数值是否合理
  1069. if (strcmp(hum->valuestring, "") != 0 && atoi(hum->valuestring) >= 0 && atoi(hum->valuestring) <= 100 && atoi(hum->valuestring) != 30 && device_status.sensor_485_sta == 0)
  1070. {
  1071. if (atoi(hum->valuestring) != atoi(device_status.humidity))
  1072. {
  1073. rt_sprintf(device_status.humidity, "%s", hum->valuestring);
  1074. ef_set_env_blob("env_h", &device_status.humidity, sizeof(device_status.humidity)); // 湿度
  1075. rt_kprintf("保存湿度成功:%d\n", atoi(device_status.humidity));
  1076. }
  1077. rt_kprintf("设置湿度成功:%d\n", atoi(device_status.humidity));
  1078. }
  1079. else
  1080. {
  1081. rt_kprintf("设置湿度失败:%s!!!\n", hum->valuestring);
  1082. }
  1083. // 先判断是否是空字符再判断数值是否合理
  1084. if (strcmp(tmp->valuestring, "") != 0 && atoi(tmp->valuestring) > -50 && atoi(tmp->valuestring) <= 100 && device_status.sensor_485_sta == 0)
  1085. {
  1086. if (atoi(tmp->valuestring) != atoi(device_status.tempture))
  1087. {
  1088. rt_sprintf(device_status.tempture, "%s", tmp->valuestring);
  1089. ef_set_env_blob("env_t", &device_status.tempture, sizeof(device_status.tempture)); // 湿度
  1090. rt_kprintf("保存温度成功:%d\n", atoi(device_status.tempture));
  1091. }
  1092. rt_kprintf("设置温度成功:%d\n", atoi(device_status.tempture));
  1093. }
  1094. else
  1095. {
  1096. rt_kprintf("设置温度失败:%s!!!\n", tmp->valuestring);
  1097. }
  1098. }
  1099. }
  1100. else if (strstr(item_cmd->valuestring, "color") != RT_NULL)
  1101. {
  1102. if (item_ext != RT_NULL && strstr(item_ext->valuestring, "turn") != RT_NULL)
  1103. {
  1104. device_status.run_sta = 5;
  1105. }
  1106. else
  1107. {
  1108. LOG_W("Unknow cmd value!");
  1109. }
  1110. }
  1111. else if (strstr(item_cmd->valuestring, "synchronization") != RT_NULL)
  1112. {
  1113. #if Color_EN == 0
  1114. if (device_status.dtype == 7)
  1115. {
  1116. DTU_Write("%s", "SwversionV7");
  1117. }
  1118. else if (device_status.dtype == 3)
  1119. {
  1120. DTU_Write("%s", "SwversionV4");
  1121. }
  1122. #endif
  1123. device_status.refresh_timeout = 0;
  1124. // driver_4g = cJSON_GetObjectItem(item_ext,"version");
  1125. driver_4g = cJSON_GetObjectItem(item_ext, "dver");
  1126. // project = cJSON_GetObjectItem(item_ext,"project");
  1127. project = cJSON_GetObjectItem(item_ext, "proj");
  1128. datetime = cJSON_GetObjectItem(item_ext, "datetime");
  1129. lat = cJSON_GetObjectItem(item_ext, "lat");
  1130. lng = cJSON_GetObjectItem(item_ext, "lng");
  1131. csq = cJSON_GetObjectItem(item_ext, "csq");
  1132. net = cJSON_GetObjectItem(item_ext, "net");
  1133. sim = cJSON_GetObjectItem(item_ext, "sim");
  1134. iccid = cJSON_GetObjectItem(item_ext, "iccid");
  1135. imei = cJSON_GetObjectItem(item_ext, "imei");
  1136. gps = cJSON_GetObjectItem(item_ext, "gps");
  1137. volume = cJSON_GetObjectItem(item_ext, "volume");
  1138. mqttcfg1 = cJSON_GetObjectItem(item_ext, "mqttcfg1");
  1139. mqttcfg2 = cJSON_GetObjectItem(item_ext, "mqttcfg2");
  1140. link_sta = cJSON_GetObjectItem(item_ext, "link_sta");
  1141. cJSON *gps_inuse = cJSON_GetObjectItem(item_ext, "gps_inuse");
  1142. cJSON *gps_view = cJSON_GetObjectItem(item_ext, "gps_view");
  1143. // 如果DTU的IMEI和FLash中的不一样,保存到FLASH
  1144. value_commn = cJSON_GetObjectItem(item_ext, "photo_sta");
  1145. if (value_commn != RT_NULL)
  1146. {
  1147. HMI_WRITE("menu.eth1_sta.val=%d", atoi(value_commn->valuestring));
  1148. }
  1149. value_commn = cJSON_GetObjectItem(item_ext, "cpu_tmp");
  1150. if (value_commn != RT_NULL)
  1151. {
  1152. HMI_WRITE("menu.cpu_temp.txt=\"%d.%d℃\"", atoi(value_commn->valuestring) / 1000, atoi(value_commn->valuestring) % 1000);
  1153. }
  1154. if (strstr(device_status.imei, imei->valuestring) == RT_NULL)
  1155. {
  1156. rt_kprintf("Change IMEI:new:%s-old:%s", imei->valuestring, device_status.imei);
  1157. rt_sprintf(device_status.imei, "%s", imei->valuestring);
  1158. ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei)); // IMEI
  1159. }
  1160. else
  1161. {
  1162. rt_sprintf(device_status.imei, "%s", imei->valuestring);
  1163. rt_kprintf("IMEI not change:%s-%s", imei->valuestring, device_status.imei);
  1164. }
  1165. // 如果DTU的ICCID和FLash中的不一样,保存到FLASH
  1166. if (strstr(device_status.iccid, iccid->valuestring) == RT_NULL)
  1167. {
  1168. rt_kprintf("Change ICCID:new:%s-old:%s", iccid->valuestring, device_status.iccid);
  1169. rt_sprintf(device_status.iccid, "%s", iccid->valuestring);
  1170. ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid)); // Iccid
  1171. }
  1172. else
  1173. {
  1174. rt_sprintf(device_status.iccid, "%s", iccid->valuestring);
  1175. rt_kprintf("ICCID not change:%s-%s", iccid->valuestring, device_status.iccid);
  1176. }
  1177. LOG_I("DTU时间:%s", datetime->valuestring);
  1178. static char year_s[5], month_s[3], day_s[3], hour_s[3], min_s[3], sec_s[3];
  1179. strncpy(year_s, datetime->valuestring, 4);
  1180. strncpy(month_s, datetime->valuestring + 4, 2);
  1181. strncpy(day_s, datetime->valuestring + 6, 2);
  1182. strncpy(hour_s, datetime->valuestring + 8, 2);
  1183. strncpy(min_s, datetime->valuestring + 10, 2);
  1184. strncpy(sec_s, datetime->valuestring + 12, 2);
  1185. if (atoi(year_s) >= 2022 && atoi(year_s) < 3000 && atoi(month_s) > 0 && atoi(month_s) < 13 && atoi(day_s) > 0 && atoi(day_s) < 32)
  1186. {
  1187. if (atoi(net->valuestring) == 0 && atoi(year_s) >= 2022)
  1188. {
  1189. if (set_date(atoi(year_s), atoi(month_s), atoi(day_s)) == RT_EOK)
  1190. {
  1191. LOG_I("设置日期:%d年%d月%d日", atoi(year_s), atoi(month_s), atoi(day_s));
  1192. if (atoi(hour_s) > 0 && atoi(hour_s) <= 24 && atoi(min_s) >= 0 && atoi(min_s) <= 60 && atoi(sec_s) >= 0 && atoi(sec_s) <= 60)
  1193. {
  1194. LOG_I("设置时间:%d时%d分%d秒", atoi(hour_s), atoi(min_s), atoi(sec_s));
  1195. set_time(atoi(hour_s), atoi(min_s), atoi(sec_s));
  1196. }
  1197. }
  1198. else
  1199. {
  1200. LOG_E("set_date failed!");
  1201. rt_hw_rtc_init();
  1202. }
  1203. }
  1204. else
  1205. {
  1206. LOG_E("网络连接失败,同步时间失败!");
  1207. }
  1208. }
  1209. LOG_I("参数同步:lat:%s lng:%s", lat->valuestring, lng->valuestring);
  1210. // CSQ
  1211. if (csq != NULL && atoi(csq->valuestring) >= 0 && atoi(csq->valuestring) <= 31)
  1212. {
  1213. rt_sprintf(device_status.csq, "%s", csq->valuestring);
  1214. }
  1215. else
  1216. {
  1217. rt_sprintf(device_status.csq, "0");
  1218. }
  1219. // 更新网线接入状态
  1220. if (link_sta != NULL && atoi(link_sta->valuestring) >= 0 && atoi(link_sta->valuestring) <= 1)
  1221. {
  1222. device_status.link_sta = atoi(link_sta->valuestring);
  1223. LOG_I(">>>>>>>>>>>>>有线接入状态>>>>>>>>>>>>>>:%d", device_status.link_sta);
  1224. }
  1225. // Volume
  1226. if (volume != NULL && atoi(volume->valuestring) >= 0 && atoi(volume->valuestring) <= 31)
  1227. {
  1228. device_status.vol = atoi(volume->valuestring);
  1229. LOG_I("设置音量:%d", device_status.vol);
  1230. }
  1231. else
  1232. {
  1233. device_status.vol = 7;
  1234. }
  1235. // NET
  1236. if (net != NULL && atoi(net->valuestring) >= 0 && atoi(net->valuestring) <= 1)
  1237. {
  1238. LOG_I("网络状态:%s", net->valuestring);
  1239. rt_sprintf(device_status.net, "%s", net->valuestring);
  1240. }
  1241. else
  1242. {
  1243. rt_sprintf(device_status.net, "1");
  1244. }
  1245. // SIM
  1246. if (sim != NULL && atoi(sim->valuestring) >= 0 && atoi(sim->valuestring) <= 1)
  1247. {
  1248. rt_sprintf(device_status.sim, "%s", sim->valuestring);
  1249. }
  1250. else
  1251. {
  1252. rt_sprintf(device_status.sim, "1");
  1253. }
  1254. // GPS
  1255. if (gps != NULL && atoi(gps->valuestring) >= 0 && atoi(gps->valuestring) <= 3)
  1256. {
  1257. rt_sprintf(device_status.gps, "%s", gps->valuestring);
  1258. if (atoi(gps->valuestring) != 0)
  1259. {
  1260. #if BOOT_ERR == 1
  1261. if (atoi(gps->valuestring) == 1)
  1262. rt_sprintf(device_status.gps_state, "%s", "GPS+BD");
  1263. if (gps_inuse != NULL)
  1264. {
  1265. device_status.gps_inuse = atoi(gps_inuse->valuestring);
  1266. }
  1267. if (gps_view != NULL)
  1268. {
  1269. device_status.gps_view = atoi(gps_view->valuestring);
  1270. }
  1271. #endif
  1272. if (strstr(device_status.lat, lat->valuestring) == NULL)
  1273. {
  1274. memset(device_status.lat, 0, sizeof(device_status.lat));
  1275. memset(device_status.lng, 0, sizeof(device_status.lng));
  1276. rt_sprintf(device_status.lat, "%s", lat->valuestring);
  1277. rt_sprintf(device_status.lng, "%s", lng->valuestring);
  1278. ef_set_env_blob("lat", &device_status.lat, sizeof(device_status.lat)); // Iccid
  1279. ef_set_env_blob("lng", &device_status.lng, sizeof(device_status.lng)); // Iccid
  1280. }
  1281. else
  1282. {
  1283. LOG_I("经纬度信息同步:lat-%s,lng-%s", lat->valuestring, lng->valuestring);
  1284. }
  1285. }
  1286. else if (atoi(gps->valuestring) == 0)
  1287. {
  1288. memset(device_status.lat, 0, sizeof(device_status.lat));
  1289. memset(device_status.lng, 0, sizeof(device_status.lng));
  1290. rt_sprintf(device_status.lat, "%s", lat->valuestring);
  1291. rt_sprintf(device_status.lng, "%s", lng->valuestring);
  1292. }
  1293. }
  1294. else
  1295. {
  1296. rt_sprintf(device_status.gps, "0");
  1297. memset(device_status.lat, 0, sizeof(device_status.lat));
  1298. memset(device_status.lng, 0, sizeof(device_status.lng));
  1299. if (sizeof(device_status.lat) < 3)
  1300. {
  1301. sprintf(device_status.lat, "%s", "0");
  1302. sprintf(device_status.lng, "%s", "0");
  1303. }
  1304. else
  1305. {
  1306. sprintf(device_status.lat, "%s", lat->valuestring);
  1307. sprintf(device_status.lng, "%s", lng->valuestring);
  1308. }
  1309. }
  1310. if (driver_4g != RT_NULL)
  1311. sprintf(device_status.driver_4g, "%s", driver_4g->valuestring);
  1312. if (project != RT_NULL)
  1313. sprintf(device_status.project, "%s", project->valuestring);
  1314. if (mqttcfg1 != NULL)
  1315. {
  1316. if (strstr("8.136.98.49", mqttcfg1->valuestring) != RT_NULL)
  1317. {
  1318. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  1319. }
  1320. else if (strstr("120.27.222.26", mqttcfg1->valuestring) != RT_NULL)
  1321. {
  1322. rt_sprintf(device_status.mqttcfg1, "120.27.222.26");
  1323. }
  1324. else
  1325. {
  1326. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  1327. }
  1328. LOG_I("当前平台1:%s", mqttcfg1->valuestring);
  1329. }
  1330. if (mqttcfg2 != NULL)
  1331. {
  1332. sprintf(device_status.mqttcfg2, "%s", mqttcfg2->valuestring);
  1333. LOG_I("当前平台2:%s", mqttcfg2->valuestring);
  1334. }
  1335. if (device_status.num_updata == 0)
  1336. {
  1337. Beep_Tip(100);
  1338. }
  1339. else if (device_status.num_updata != 0 && device_status.hmi_fresh == 2)
  1340. {
  1341. if (strstr(device_status.project, "A7_DTU_V3") != RT_NULL)
  1342. {
  1343. UpLoad_Data();
  1344. }
  1345. else
  1346. {
  1347. rt_thread_delay(2000);
  1348. UpLoad_Data();
  1349. }
  1350. }
  1351. device_status.hmi_fresh = 0;
  1352. }
  1353. if (item_ext != RT_NULL && strstr(item_cmd->valuestring, "paramconf") != RT_NULL)
  1354. {
  1355. if (device_status.dtype == 3)
  1356. hst = cJSON_GetObjectItem(item_ext, "hst");
  1357. if (device_status.dtype == 7)
  1358. hst = cJSON_GetObjectItem(item_ext, "set_temp");
  1359. et = cJSON_GetObjectItem(item_ext, "et");
  1360. datt = cJSON_GetObjectItem(item_ext, "datt");
  1361. shake_sec = cJSON_GetObjectItem(item_ext, "shake_sec");
  1362. tpl = cJSON_GetObjectItem(item_ext, "tpl");
  1363. shake = cJSON_GetObjectItem(item_ext, "shake");
  1364. tph = cJSON_GetObjectItem(item_ext, "tph");
  1365. ts = cJSON_GetObjectItem(item_ext, "ts");
  1366. st = cJSON_GetObjectItem(item_ext, "st");
  1367. tt = cJSON_GetObjectItem(item_ext, "tt");
  1368. htim = cJSON_GetObjectItem(item_ext, "htim");
  1369. if (device_status.dtype == 3)
  1370. ds = cJSON_GetObjectItem(item_ext, "ds");
  1371. vt = cJSON_GetObjectItem(item_ext, "vt");
  1372. if (device_status.dtype == 3)
  1373. collt = cJSON_GetObjectItem(item_ext, "collt");
  1374. if (device_status.dtype == 7)
  1375. ds = cJSON_GetObjectItem(item_ext, "on_off");
  1376. if (device_status.dtype == 7)
  1377. collt = cJSON_GetObjectItem(item_ext, "coll_time"); // 收集(数组格式)
  1378. cJSON *cul_time = cJSON_GetObjectItem(item_ext, "cul_time"); // 培养时间1~24
  1379. cJSON *drop_sec = cJSON_GetObjectItem(item_ext, "drop_time");
  1380. if (drop_sec != NULL)
  1381. {
  1382. if (drop_sec->valueint >= 1 && drop_sec->valueint <= 300)
  1383. {
  1384. device_status.shake_sec = drop_sec->valueint;
  1385. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); // 培养时间
  1386. LOG_I("下发设置滴液时间:%d", drop_sec->valueint);
  1387. }
  1388. }
  1389. if (cul_time != NULL)
  1390. {
  1391. if (cul_time->valueint >= 1 && cul_time->valueint <= 24)
  1392. {
  1393. device_status.cul_time = cul_time->valueint;
  1394. ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time)); // 培养时间
  1395. LOG_I("下发设置培养时间:%d", cul_time->valueint);
  1396. }
  1397. }
  1398. /*加热温度*/
  1399. if (hst != NULL)
  1400. {
  1401. if (hst->type == cJSON_Number)
  1402. {
  1403. if (hst->valueint >= 10 && hst->valueint <= 150)
  1404. {
  1405. device_status.heatvalue = hst->valueint;
  1406. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); // 开始时间
  1407. LOG_I("加热温度hst:%d", hst->valueint);
  1408. }
  1409. else
  1410. {
  1411. LOG_E("加热温度hst设置失败参数错误!!!");
  1412. }
  1413. }
  1414. else
  1415. {
  1416. if (atoi(hst->valuestring) > 10 && atoi(hst->valuestring) < 150)
  1417. {
  1418. device_status.heatvalue = atoi(hst->valuestring);
  1419. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); // 加热温度
  1420. LOG_I("加热温度hst:%d", atoi(hst->valuestring));
  1421. }
  1422. else
  1423. {
  1424. LOG_E("加热温度hst设置失败参数错误!!!");
  1425. }
  1426. }
  1427. }
  1428. #if LAMP_EN == 0 // 三色光源无法修改开始和结束时间
  1429. if (st != NULL)
  1430. {
  1431. if (st->type == cJSON_Number)
  1432. {
  1433. if (st->valueint >= 0 && st->valueint <= 23)
  1434. {
  1435. device_status.startBar = st->valueint;
  1436. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); // 开始时间
  1437. LOG_I("开始时间st:%d", st->valueint);
  1438. }
  1439. else
  1440. {
  1441. LOG_E("开始时间st设置失败参数错误!!!");
  1442. }
  1443. }
  1444. else
  1445. {
  1446. if (atoi(st->valuestring) >= 0 && atoi(st->valuestring) <= 23)
  1447. {
  1448. device_status.startBar = atoi(st->valuestring);
  1449. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); // 开始时间
  1450. LOG_I("开始时间st:%d", atoi(st->valuestring));
  1451. }
  1452. else
  1453. {
  1454. LOG_E("开始时间st设置失败参数错误!!!");
  1455. }
  1456. }
  1457. }
  1458. if (et != NULL)
  1459. {
  1460. if (et->type == cJSON_Number)
  1461. {
  1462. if (et->valueint >= 0 && et->valueint <= 23)
  1463. {
  1464. device_status.stopBar = et->valueint;
  1465. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); // 结束时间
  1466. LOG_I("结束时间et:%d", et->valueint);
  1467. }
  1468. else
  1469. {
  1470. LOG_E("结束时间et设置失败参数错误!!!");
  1471. }
  1472. }
  1473. else if (et->type == cJSON_String)
  1474. {
  1475. if (atoi(et->valuestring) >= 0 && atoi(et->valuestring) <= 23)
  1476. {
  1477. device_status.stopBar = atoi(et->valuestring);
  1478. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); // 结束时间
  1479. LOG_I("结束时间et:%d", atoi(et->valuestring));
  1480. }
  1481. else
  1482. {
  1483. LOG_E("结束时间et设置失败参数错误!!!");
  1484. }
  1485. }
  1486. }
  1487. #endif
  1488. if (collt != NULL)
  1489. {
  1490. if (collt->type == cJSON_Number)
  1491. {
  1492. if (collt->valueint >= 1 && collt->valueint <= 60)
  1493. {
  1494. device_status.clt_time = collt->valueint;
  1495. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); // 收集时间
  1496. LOG_I("收集时间collt:%d", collt->valueint);
  1497. }
  1498. else
  1499. {
  1500. LOG_E("收集时间collt设置失败参数错误!!!");
  1501. }
  1502. }
  1503. else if (collt->type == cJSON_String)
  1504. {
  1505. if (atoi(collt->valuestring) >= 1 && atoi(collt->valuestring) <= 60)
  1506. {
  1507. device_status.clt_time = atoi(collt->valuestring);
  1508. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); // 收集时间
  1509. LOG_I("收集时间collt:%d", atoi(collt->valuestring));
  1510. }
  1511. else
  1512. {
  1513. LOG_E("收集时间collt设置失败参数错误!!!");
  1514. }
  1515. }
  1516. else if (collt->type == cJSON_Array)
  1517. {
  1518. int icnt = 0;
  1519. for (icnt = 0; icnt < cJSON_GetArraySize(collt); icnt++)
  1520. {
  1521. cJSON *pSub = cJSON_GetArrayItem(collt, icnt);
  1522. if (NULL == pSub)
  1523. break;
  1524. char ivalue[10];
  1525. rt_sprintf(ivalue, "%s", pSub->valuestring);
  1526. if (icnt == 0)
  1527. {
  1528. if (ivalue[1] == '-')
  1529. {
  1530. device_status.collecting_sh1 = atoi(&ivalue[0]);
  1531. if (strlen(ivalue) == 3)
  1532. device_status.collecting_eh1 = atoi(&ivalue[2]);
  1533. if (strlen(ivalue) == 4)
  1534. device_status.collecting_eh1 = atoi(&ivalue[3]) << 8 | atoi(&ivalue[2]);
  1535. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  1536. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  1537. }
  1538. if (ivalue[2] == '-')
  1539. {
  1540. device_status.collecting_sh1 = atoi(&ivalue[1]) << 8 | atoi(&ivalue[0]);
  1541. if (strlen(ivalue) == 3)
  1542. device_status.collecting_eh1 = atoi(&ivalue[3]);
  1543. if (strlen(ivalue) == 5)
  1544. device_status.collecting_eh1 = atoi(&ivalue[4]) << 8 | atoi(&ivalue[3]);
  1545. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  1546. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  1547. }
  1548. LOG_I("开始结束时间1:%d-%d", device_status.collecting_sh1, device_status.collecting_eh1);
  1549. }
  1550. else if (icnt == 1)
  1551. {
  1552. if ((ivalue[1] == '-'))
  1553. {
  1554. device_status.collecting_sh2 = atoi(&ivalue[0]);
  1555. if (strlen(ivalue) == 3)
  1556. device_status.collecting_eh2 = atoi(&ivalue[2]);
  1557. if (strlen(ivalue) == 4)
  1558. device_status.collecting_eh2 = atoi(&ivalue[3]) << 8 | atoi(&ivalue[2]);
  1559. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  1560. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  1561. }
  1562. if ((ivalue[2] == '-'))
  1563. {
  1564. device_status.collecting_sh2 = atoi(&ivalue[1]) << 8 | atoi(&ivalue[0]);
  1565. if (strlen(ivalue) == 3)
  1566. device_status.collecting_eh2 = atoi(&ivalue[2]);
  1567. if (strlen(ivalue) == 5)
  1568. device_status.collecting_eh2 = atoi(&ivalue[4]) << 8 | atoi(&ivalue[3]);
  1569. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  1570. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  1571. }
  1572. LOG_I("开始结束时间2:%d-%d", device_status.collecting_sh2, device_status.collecting_eh2);
  1573. }
  1574. LOG_I("下发设置收集:%d-%s", icnt, ivalue);
  1575. }
  1576. }
  1577. }
  1578. if (datt != NULL)
  1579. {
  1580. if (datt->type == cJSON_Number)
  1581. {
  1582. if (datt->valueint >= 1 && datt->valueint <= 120)
  1583. {
  1584. device_status.dattim = datt->valueint;
  1585. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim)); // 数据上传间隔
  1586. LOG_I("数据上传间隔datt:%d", datt->valueint);
  1587. }
  1588. else
  1589. {
  1590. LOG_E("数据上传间隔datt设置失败参数错误!!!");
  1591. }
  1592. }
  1593. else if (datt->type == cJSON_String)
  1594. {
  1595. if (atoi(datt->valuestring) >= 1 && atoi(datt->valuestring) <= 120)
  1596. {
  1597. device_status.dattim = atoi(datt->valuestring);
  1598. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim)); // 数据上传间隔
  1599. LOG_I("数据上传间隔datt:%d", atoi(datt->valuestring));
  1600. }
  1601. else
  1602. {
  1603. LOG_E("数据上传间隔datt设置失败参数错误!!!");
  1604. }
  1605. }
  1606. }
  1607. if (shake_sec != NULL)
  1608. {
  1609. if (shake_sec->type == cJSON_Number)
  1610. {
  1611. if (shake_sec->valueint >= 1 && shake_sec->valueint <= 1000)
  1612. {
  1613. device_status.shake_sec = shake_sec->valueint;
  1614. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); // 震动时间
  1615. LOG_I("震动时间shake_sec:%d", shake_sec->valueint);
  1616. }
  1617. else
  1618. {
  1619. LOG_E("震动时间shake_sec设置失败参数错误!!!");
  1620. }
  1621. }
  1622. else if (shake_sec->type == cJSON_String)
  1623. {
  1624. if (atoi(shake_sec->valuestring) >= 1 && atoi(shake_sec->valuestring) <= 1000)
  1625. {
  1626. device_status.shake_sec = atoi(shake_sec->valuestring);
  1627. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); // 震动时间
  1628. LOG_I("震动时间shake_sec:%d", atoi(shake_sec->valuestring));
  1629. }
  1630. else
  1631. {
  1632. LOG_E("震动时间shake_sec设置失败参数错误!!!");
  1633. }
  1634. }
  1635. }
  1636. if (shake != NULL)
  1637. {
  1638. if (shake->type == cJSON_Number)
  1639. {
  1640. if (shake->valueint == 0 || shake->valueint == 1)
  1641. {
  1642. device_status.shake_ws = shake->valueint;
  1643. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); // 震动开关
  1644. LOG_I("震动开关shake:%d", shake->valueint);
  1645. }
  1646. else
  1647. {
  1648. LOG_E("震动开关shake设置失败参数错误!!!");
  1649. }
  1650. }
  1651. else if (shake->type == cJSON_String)
  1652. {
  1653. if (atoi(shake->valuestring) == 0 || atoi(shake->valuestring) == 1)
  1654. {
  1655. device_status.shake_ws = atoi(shake->valuestring);
  1656. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); // 震动开关
  1657. LOG_I("震动开关shake:%d", atoi(shake->valuestring));
  1658. }
  1659. else
  1660. {
  1661. LOG_E("震动开关shake设置失败参数错误!!!");
  1662. }
  1663. }
  1664. }
  1665. if (tt != NULL)
  1666. {
  1667. if (tt->type == cJSON_Number)
  1668. {
  1669. if (tt->valueint >= 1 && tt->valueint <= 10)
  1670. {
  1671. #if LAMP_EN == 0
  1672. device_status.sunBar = tt->valueint;
  1673. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); // 光控定时
  1674. rt_kprintf("光控定时tt:%d\n", tt->valueint);
  1675. #else
  1676. if (tt->valueint == 3 || tt->valueint == 6 || tt->valueint == 9)
  1677. {
  1678. device_status.sunBar = tt->valueint;
  1679. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); // 光控定时
  1680. rt_kprintf("光控定时tt:%d\n", tt->valueint);
  1681. }
  1682. else
  1683. {
  1684. device_status.sunBar = 3;
  1685. rt_kprintf("三光源模式,禁止设置定时时长!!!\n");
  1686. }
  1687. #endif
  1688. }
  1689. else
  1690. {
  1691. rt_kprintf("光控定时tt设置失败参数错误!!!\n");
  1692. }
  1693. }
  1694. else if (tt->type == cJSON_String)
  1695. {
  1696. if (atoi(tt->valuestring) >= 1 && atoi(tt->valuestring) <= 10)
  1697. {
  1698. #if LAMP_EN == 0
  1699. device_status.sunBar = atoi(tt->valuestring);
  1700. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); // 光控定时
  1701. rt_kprintf("光控定时tt:%d\n", atoi(tt->valuestring));
  1702. #else
  1703. if (atoi(tt->valuestring) % 3 == 0)
  1704. {
  1705. device_status.sunBar = atoi(tt->valuestring);
  1706. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); // 光控定时
  1707. LOG_I("光控定时tt:%d", atoi(tt->valuestring));
  1708. }
  1709. else
  1710. {
  1711. device_status.sunBar = 3;
  1712. LOG_W("三光源模式,禁止设置定时时长!!!");
  1713. }
  1714. #endif
  1715. }
  1716. else
  1717. {
  1718. LOG_E("光控定时tt设置失败参数错误!!!");
  1719. }
  1720. }
  1721. }
  1722. if (htim != NULL)
  1723. {
  1724. if (htim->type == cJSON_Number)
  1725. {
  1726. if (htim->valueint >= 1 && htim->valueint <= 30)
  1727. {
  1728. device_status.heattime = htim->valueint;
  1729. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); // 加热时间
  1730. LOG_I("加热时长htim:%d", htim->valueint);
  1731. }
  1732. else
  1733. {
  1734. LOG_E("加热时长htim设置失败参数错误!!!");
  1735. }
  1736. }
  1737. else if (htim->type == cJSON_String)
  1738. {
  1739. if (atoi(htim->valuestring) >= 1 && atoi(htim->valuestring) <= 30)
  1740. {
  1741. device_status.heattime = atoi(htim->valuestring);
  1742. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); // 加热时间
  1743. LOG_I("加热时长htim:%d", atoi(htim->valuestring));
  1744. }
  1745. else
  1746. {
  1747. LOG_E("加热时长htim设置失败参数错误!!!");
  1748. }
  1749. }
  1750. }
  1751. if (ts != NULL)
  1752. {
  1753. if (ts->type == cJSON_Number)
  1754. {
  1755. if (ts->valueint == 0 || ts->valueint == 1)
  1756. {
  1757. device_status.timeSwitch = ts->valueint;
  1758. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); // 工作模式
  1759. // time_t now;
  1760. // time(&now);
  1761. // static struct tm nowstamp;
  1762. // nowstamp =*localtime(&now);
  1763. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1764. // static rt_uint32_t nowstamp_num = 0;
  1765. // nowstamp_num = mktime(&nowstamp);
  1766. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  1767. LOG_I("定时模式ts:%d", ts->valueint);
  1768. }
  1769. else
  1770. {
  1771. LOG_E("定时模式ts设置失败参数错误!!!");
  1772. }
  1773. }
  1774. else if (ts->type == cJSON_String)
  1775. {
  1776. if (atoi(ts->valuestring) == 0 || atoi(ts->valuestring) == 1)
  1777. {
  1778. device_status.timeSwitch = atoi(ts->valuestring);
  1779. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); // 工作模式
  1780. // time_t now;
  1781. // time(&now);
  1782. // static struct tm nowstamp;
  1783. // nowstamp =*localtime(&now);
  1784. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1785. // static rt_uint32_t nowstamp_num = 0;
  1786. // nowstamp_num = mktime(&nowstamp);
  1787. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  1788. LOG_I("定时模式ts:%d", atoi(ts->valuestring));
  1789. }
  1790. else
  1791. {
  1792. LOG_I("定时模式ts设置失败参数错误!!!");
  1793. }
  1794. }
  1795. }
  1796. if (tpl != NULL)
  1797. {
  1798. if (tpl->type == cJSON_Number)
  1799. {
  1800. if (tpl->valueint >= -50 && tpl->valueint <= 50)
  1801. {
  1802. device_status.tpl = tpl->valueint;
  1803. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); // 低温保护
  1804. rt_kprintf("低温保护tpl:%d\n", tpl->valueint);
  1805. }
  1806. else
  1807. {
  1808. rt_kprintf("低温保护tpl设置失败参数错误!!!\n");
  1809. }
  1810. }
  1811. else if (tpl->type == cJSON_String)
  1812. {
  1813. if (atoi(tpl->valuestring) >= -50 && atoi(tpl->valuestring) <= 50)
  1814. {
  1815. device_status.tpl = atoi(tpl->valuestring);
  1816. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); // 低温保护
  1817. rt_kprintf("低温保护tpl:%d\n", atoi(tpl->valuestring));
  1818. }
  1819. else
  1820. {
  1821. rt_kprintf("低温保护tpl设置失败参数错误!!!\n");
  1822. }
  1823. }
  1824. }
  1825. if (tph != NULL)
  1826. {
  1827. if (tph->type == cJSON_Number)
  1828. {
  1829. if (tph->valueint >= 50 && tph->valueint <= 80)
  1830. {
  1831. device_status.tph = tph->valueint;
  1832. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); // 高温保护
  1833. rt_kprintf("高温保护tph:%d\n", tph->valueint);
  1834. }
  1835. else
  1836. {
  1837. rt_kprintf("高温保护tph设置失败参数错误!!!\n");
  1838. }
  1839. }
  1840. else if (tph->type == cJSON_String)
  1841. {
  1842. if (atoi(tph->valuestring) >= 50 && atoi(tph->valuestring) <= 80)
  1843. {
  1844. device_status.tph = atoi(tph->valuestring);
  1845. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); // 高温保护
  1846. rt_kprintf("高温保护tph:%d\n", atoi(tph->valuestring));
  1847. }
  1848. else
  1849. {
  1850. rt_kprintf("高温保护tph设置失败参数错误!!!\n");
  1851. }
  1852. }
  1853. }
  1854. if (ds != NULL)
  1855. {
  1856. if (ds->type == cJSON_Number)
  1857. {
  1858. if (ds->valueint >= 0 && ds->valueint <= 1)
  1859. {
  1860. device_status.ds = ds->valueint;
  1861. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1862. LOG_I("开关机状态:%d", ds->valueint);
  1863. }
  1864. else
  1865. {
  1866. LOG_E("开关机状态设置失败参数错误!!!");
  1867. }
  1868. }
  1869. else if (ds->type == cJSON_String)
  1870. {
  1871. if (atoi(ds->valuestring) >= 0 && atoi(ds->valuestring) <= 1)
  1872. {
  1873. device_status.ds = atoi(ds->valuestring);
  1874. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1875. LOG_I("开关机状态:%d\n", atoi(ds->valuestring));
  1876. }
  1877. else
  1878. {
  1879. LOG_E("开关机状态设置失败参数错误!!!");
  1880. }
  1881. }
  1882. }
  1883. if (vt != NULL)
  1884. {
  1885. if (atoi(vt->valuestring) >= 0 && atoi(vt->valuestring) <= 100)
  1886. {
  1887. device_status.version_type = atoi(vt->valuestring);
  1888. ef_set_env_blob("version_type", &device_status.version_type, sizeof(device_status.version_type));
  1889. if (device_status.version_type == 7)
  1890. {
  1891. device_status.dtype = 7;
  1892. ef_set_env_blob("dtype", &device_status.dtype, sizeof(device_status.dtype));
  1893. }
  1894. rt_kprintf("版本类型vt:%d\n", atoi(vt->valuestring));
  1895. }
  1896. else
  1897. {
  1898. rt_kprintf("版本类型设置失败参数错误!!!\n");
  1899. }
  1900. }
  1901. UpLoad_Data();
  1902. rt_thread_delay(2000);
  1903. // 2022/06/23,add:
  1904. if (device_status.dtype == 3)
  1905. {
  1906. rt_sprintf(device_status.json_w_clock, "平台操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)-%s", device_status.startBar, device_status.stopBar, device_status.timeSwitch, device_status.stamp);
  1907. json_s = makeJson_Warn(device_status.json_w_clock, "修改时间");
  1908. ef_set_env_blob("json_w_clock", &device_status.json_w_clock, sizeof(device_status.json_w_clock)); // 将修改屏幕参数操作记录保存到Flash中
  1909. if (atoi(device_status.net) == 0)
  1910. DTU_Write("%s", json_s);
  1911. rt_free(json_s);
  1912. }
  1913. rt_thread_delay(1000);
  1914. MA_save();
  1915. rt_thread_delay(1000);
  1916. }
  1917. }
  1918. cJSON_Delete(root);
  1919. }
  1920. return 0;
  1921. }
  1922. /**
  1923. * @brief 执行上报状态信息json的动作
  1924. *
  1925. */
  1926. void UpLoad_Data(void)
  1927. {
  1928. char *updata = makeJson();
  1929. LOG_I("upload data:%d%s", rt_strlen(updata), updata);
  1930. rt_free(updata);
  1931. #if Color_EN == 0
  1932. DTU_Write("%s", updata);
  1933. #endif
  1934. list_mem();
  1935. // 播放数据上传中语音
  1936. }