dtu_uart2.c 93 KB

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