dtu_uart2.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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. #define DBG_TAG "dtu_uart2.c"
  25. #define DBG_LVL LOG_LVL_INFO
  26. #include "beep.h"
  27. #define DTU_THREAD_PRIORITY 6 //线程优先级
  28. #define DTU_THREAD_STACK_SIZE 2048 //线程栈空间大小
  29. #define DTU_THREAD_TIMESLICE 5 //时间片
  30. Device_Status_typedef device_status;
  31. DataTemp data_arry;
  32. void list_mem(void);
  33. /* 串口接收消息结构*/
  34. struct rx_msg
  35. {
  36. rt_device_t dev;
  37. rt_size_t size;
  38. };
  39. /* 消息队列句柄 */
  40. static struct rt_messagequeue rx_mq_dtu;
  41. rt_thread_t dtu_recv_data = RT_NULL;
  42. rt_thread_t dtu_send_data = RT_NULL;
  43. rt_device_t dtuserial; /*串口设备句柄*/
  44. struct serial_configure dtuconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  45. static uint8_t makejson_sta = 0;
  46. static int DTU_uart2_init(void)
  47. {
  48. dtuserial = rt_device_find(DTU_UART_NAME);
  49. /*修改串口配置参数*/
  50. dtuconfig.baud_rate = BAUD_RATE_115200; //修改波特率9600
  51. dtuconfig.data_bits = DATA_BITS_8; //数据位8
  52. dtuconfig.stop_bits = STOP_BITS_1; //停止位1
  53. dtuconfig.bufsz = 512; //缓冲区1k
  54. dtuconfig.parity = PARITY_NONE; //奇校验
  55. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  56. rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
  57. /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
  58. rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);
  59. return RT_EOK;
  60. }
  61. /*数据发送*/
  62. void DTU_Write(char *fmt, ...)
  63. {
  64. const char dtu_conf[4] = {device_status.vol,0x00,0x00,0x00};
  65. va_list arg;
  66. static char dtu_str[800];
  67. rt_size_t str_length;
  68. va_start(arg,fmt);
  69. str_length = rt_vsnprintf(dtu_str, sizeof(dtu_str) - 1, fmt, arg);
  70. if (str_length > 800 - 1)
  71. str_length = 800 - 1;
  72. rt_device_write(dtuserial, 0, dtu_str, str_length);
  73. if(strstr(dtu_str,"volume")!=RT_NULL)
  74. {
  75. rt_device_write(dtuserial,0,dtu_conf,4);
  76. }
  77. else if (strstr(dtu_str,"reset")!=RT_NULL) {
  78. const char dtu_conf[4] = {7,0x00,0x00,0x00};
  79. rt_device_write(dtuserial,0,dtu_conf,4);
  80. }
  81. va_end(arg);
  82. memset(dtu_str,'\0',sizeof(dtu_str));
  83. }
  84. /* 接收数据回调函数 */
  85. static rt_err_t dtu_input(rt_device_t dev, rt_size_t size)
  86. {
  87. struct rx_msg msg;
  88. rt_err_t result;
  89. msg.dev = dev;
  90. msg.size = size;
  91. result = rt_mq_send(&rx_mq_dtu,&msg, sizeof(struct rx_msg));
  92. if ( result == -RT_EFULL)
  93. {
  94. /* 消息队列满 */
  95. rt_kprintf("message queue full!\n");
  96. }
  97. return result;
  98. }
  99. /* 入口函数-指令接收*/
  100. static void dtu_thread_entry_recv(void *parameter)
  101. {
  102. struct rx_msg msg;
  103. rt_err_t result;
  104. rt_uint32_t rx_length;
  105. rt_uint32_t rx_err = 0;
  106. static char str_check[2]={'{','}'};
  107. static char rx_buffer[512];
  108. static char rx_buffer_tmp[512];
  109. static char rx_buffer_tmp2[512];
  110. while(1)
  111. {
  112. rt_memset(&msg, 0, sizeof(msg));
  113. // 从消息队列中获取被回调函数放入消息队列中的内容
  114. result = rt_mq_recv(&rx_mq_dtu, &msg, sizeof(struct rx_msg), 100);
  115. if(result == RT_EOK)
  116. {
  117. rx_err = 0;
  118. rx_length =(sizeof(rx_buffer) - 1) > msg.size ?
  119. msg.size : sizeof(rx_buffer) - 1;
  120. rx_length = rt_device_read(msg.dev, 0, &rx_buffer[0], rx_length);
  121. rx_buffer[rx_length] = '\0';
  122. rt_kprintf("recv data:%s\n",rx_buffer);
  123. //过滤异常数据
  124. char str_check_head[2];
  125. char str_check_end[2];
  126. rt_sprintf(str_check_head, "%c",rx_buffer[0]);
  127. rt_sprintf(str_check_end, "%c",rx_buffer[rx_length-1]);
  128. if(str_check_end[0]==str_check[1]&&str_check_head[0]==str_check[0])
  129. {
  130. parseDtu(rx_buffer);
  131. rt_memset(rx_buffer,0,sizeof(rx_buffer));
  132. rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
  133. rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
  134. }
  135. else {
  136. if(rx_buffer[rx_length-1]!='}')
  137. {
  138. rt_sprintf(rx_buffer_tmp, "%s",rx_buffer);
  139. }
  140. else {
  141. rt_sprintf(rx_buffer_tmp2,"%s",rx_buffer);
  142. rt_sprintf(rx_buffer, "%s%s",rx_buffer_tmp,rx_buffer_tmp2);
  143. //rt_kprintf("rxbuffer:%s",rx_buffer);
  144. parseDtu(rx_buffer);
  145. rt_memset(rx_buffer,0,sizeof(rx_buffer));
  146. rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
  147. rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
  148. }
  149. //rt_kprintf("%s",rx_buffer);
  150. }
  151. }
  152. else {
  153. rx_err ++;
  154. if(rx_err==54000)//90分钟未接收到数据
  155. {
  156. if(device_status.version_type==6)
  157. {
  158. rt_kprintf("RK3399 transter Error!\n");
  159. ctr_photo(OFF);
  160. }
  161. else {
  162. ctr_DTU_Power(OFF);
  163. }
  164. }
  165. else if (rx_err==54100) {
  166. rx_err = 0;
  167. ctr_photo(ON);
  168. ctr_DTU_Power(ON);
  169. }
  170. }
  171. //rt_kprintf("%s\n",rx_json_buffer);
  172. // 将内容写回到串口1
  173. //rt_device_write(dtuserial, 0, &rx_json_buffer[0], rx_length);
  174. }
  175. }
  176. /*入口函数-指令发送*/
  177. static void dtu_thread_entry_send(void *parameter)
  178. {
  179. device_status.power_status = 1;
  180. device_status.num_updata = 0;
  181. rt_thread_delay(10000);
  182. while(1)
  183. {
  184. if(atoi(device_status.net)==0)
  185. {
  186. if(device_status.num_updata!=0)
  187. {
  188. device_status.num_updata--;
  189. //屏幕休眠
  190. if(device_status.num_updata==(device_status.dattim * 60-10))
  191. {
  192. HMI_WRITE("sleep=%d",1);
  193. }
  194. if(Read_Real_Voltage()/100<12&&device_status.power_status==1)
  195. {
  196. DTU_Write("%s",makeJson_Warn("power down","power"));
  197. device_status.power_status = 0;
  198. rt_thread_delay(1500);
  199. }
  200. }
  201. else {
  202. char *readinfo = makeJson_ReadInfo();
  203. rt_kprintf("upload data:%s\n",readinfo);
  204. UpLoad_Data();
  205. rt_free(readinfo);
  206. if(atoi(device_status.fuse_voltage)<1)
  207. {
  208. rt_thread_delay(500);
  209. static char res[30];
  210. sprintf(res, "Fuse is fusing:%s",device_status.fuse_voltage);
  211. char * res_fuse_warn = makeJson_Warn(res,"device");
  212. DTU_Write("%s",res_fuse_warn);
  213. rt_free(res_fuse_warn);
  214. }
  215. device_status.num_updata = device_status.dattim * 60;
  216. if(device_status.dattim==0) device_status.dattim=20; //数据上传时间间隔设置为0?
  217. if(device_status.bat_status==0) device_status.power_status = 1; //解除欠压,清除标志位
  218. }
  219. }
  220. else {
  221. rt_kprintf("wait connect internet......\n");
  222. }
  223. rt_thread_mdelay(1000);
  224. }
  225. }
  226. /* 创建线程 */
  227. int dtu_data_play(void)
  228. {
  229. rt_err_t rm_dtu_result;
  230. DTU_uart2_init();
  231. //初始化消息队列
  232. 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);
  233. if(rm_dtu_result==RT_EOK)
  234. {
  235. rt_kprintf("消息队列创建成功!!");
  236. }
  237. else {
  238. rt_kprintf("消息队列创建失败!!");
  239. }
  240. /* 设置接收回调函数 */
  241. rt_device_set_rx_indicate(dtuserial, dtu_input);
  242. /*创建线程-指令发送*/
  243. dtu_send_data = rt_thread_create("DTU_Uart_thread_send",
  244. dtu_thread_entry_send,RT_NULL,
  245. DTU_THREAD_STACK_SIZE,
  246. 19,
  247. DTU_THREAD_TIMESLICE
  248. );
  249. /* 创建线程-指令接收解析*/
  250. dtu_recv_data = rt_thread_create("DTU_Uart_thread_recv",
  251. dtu_thread_entry_recv, RT_NULL,
  252. DTU_THREAD_STACK_SIZE,
  253. DTU_THREAD_PRIORITY,
  254. DTU_THREAD_TIMESLICE);
  255. /* 如果获得线程控制块,启动这个线程 */
  256. if(dtu_send_data!=RT_NULL)
  257. rt_thread_startup(dtu_send_data);
  258. /* 如果获得线程控制块,启动这个线程 */
  259. if (dtu_recv_data != RT_NULL)
  260. rt_thread_startup(dtu_recv_data);
  261. return RT_EOK;
  262. }
  263. char * makeJson_ReadInfo()
  264. {
  265. cJSON * rJsonRoot = NULL;
  266. rJsonRoot = cJSON_CreateObject();
  267. if(NULL==rJsonRoot)
  268. {
  269. return NULL;
  270. }
  271. cJSON_AddStringToObject(rJsonRoot, "uart", "getinfo");
  272. char * r = cJSON_PrintUnformatted(rJsonRoot);
  273. if(NULL == r)
  274. {
  275. cJSON_Delete(rJsonRoot);
  276. return NULL;
  277. }
  278. cJSON_Delete(rJsonRoot);
  279. return r;
  280. }
  281. //{"cmd": "platform", "ext": {"type": "120.27.222.26"}}
  282. char * makeJson_platform(char *plt)
  283. {
  284. cJSON * platform = NULL;
  285. platform = cJSON_CreateObject();
  286. if(NULL==platform)
  287. {
  288. return NULL;
  289. }
  290. cJSON_AddStringToObject(platform, "cmd", "platform");
  291. cJSON * extstr = NULL;
  292. extstr = cJSON_CreateObject();
  293. if(NULL==extstr)
  294. {
  295. return NULL;
  296. }
  297. cJSON_AddItemToObject(platform, "ext", extstr);
  298. cJSON_AddStringToObject(extstr, "type",plt);
  299. char * p_str = cJSON_PrintUnformatted(platform);
  300. if(NULL == p_str)
  301. {
  302. cJSON_Delete(platform);
  303. return NULL;
  304. }
  305. cJSON_Delete(platform);
  306. return p_str;
  307. }
  308. char * makeJson_takephoto()
  309. {
  310. cJSON * takephoto = NULL;
  311. takephoto = cJSON_CreateObject();
  312. if(NULL==takephoto)
  313. {
  314. return NULL;
  315. }
  316. cJSON_AddStringToObject(takephoto, "cmd", "takephoto");
  317. char * takephoto_str = cJSON_PrintUnformatted(takephoto);
  318. if(NULL == takephoto_str)
  319. {
  320. cJSON_Delete(takephoto);
  321. return NULL;
  322. }
  323. cJSON_Delete(takephoto);
  324. return takephoto_str;
  325. }
  326. char * makeJson_Rtutakephoto()
  327. {
  328. cJSON * tJsonRoot = NULL;
  329. tJsonRoot = cJSON_CreateObject();
  330. if(NULL==tJsonRoot)
  331. {
  332. return NULL;
  333. }
  334. cJSON_AddStringToObject(tJsonRoot, "cmd", "ctrl");
  335. cJSON * tpubstring = NULL;
  336. tpubstring = cJSON_CreateObject();
  337. if(NULL==tpubstring)
  338. {
  339. rt_kprintf("cJSON tpubstring NULL!!!\n");
  340. return NULL;
  341. }
  342. cJSON_AddItemToObject(tJsonRoot, "ext", tpubstring);
  343. cJSON_AddStringToObject(tpubstring, "takephoto", "2");
  344. char * t = cJSON_PrintUnformatted(tJsonRoot);
  345. if(NULL == t)
  346. {
  347. cJSON_Delete(tJsonRoot);
  348. return NULL;
  349. }
  350. cJSON_Delete(tJsonRoot);
  351. return t;
  352. }
  353. //预警消息
  354. char *makeJson_Warn(char * s,char *t)
  355. {
  356. cJSON * wJsonRoot = NULL;
  357. wJsonRoot = cJSON_CreateObject();
  358. if(NULL==wJsonRoot)
  359. {
  360. return NULL;
  361. }
  362. cJSON_AddStringToObject(wJsonRoot, "cmd", "warn");
  363. cJSON * pubstring = NULL;
  364. pubstring = cJSON_CreateObject();
  365. if(NULL==pubstring)
  366. {
  367. rt_kprintf("cJSON pubstring NULL!!!\n");
  368. return NULL;
  369. }
  370. cJSON_AddItemToObject(wJsonRoot, "ext", pubstring);
  371. cJSON_AddStringToObject(pubstring,"status",s);//
  372. cJSON_AddStringToObject(pubstring,"type",t);
  373. char * w = cJSON_PrintUnformatted(wJsonRoot);
  374. if(NULL == w)
  375. {
  376. cJSON_Delete(wJsonRoot);
  377. rt_kprintf("wJsonRoot cJSON_Delete NULL!!!\n");
  378. return NULL;
  379. }
  380. cJSON_Delete(wJsonRoot);
  381. return w;
  382. }
  383. /*生成JSON数据*/
  384. char * makeJson()
  385. {
  386. char utos[4];
  387. if(device_status.gps==NULL)
  388. rt_sprintf(device_status.gps, "0");
  389. //如果设备工作并且电流小于500,并且不是上电启动灯管异常
  390. if(atoi(device_status.power)<400&&device_status.work_status==1&&makejson_sta!=0)
  391. {
  392. rt_kprintf("power is:%d\n",atoi(device_status.power));
  393. device_status.lamp_status = 1;
  394. }
  395. else {
  396. device_status.lamp_status = 0;
  397. }
  398. makejson_sta = 1;
  399. time_t stamp=time(RT_NULL);
  400. static struct tm tm;
  401. gmtime_r(&stamp, &tm);
  402. 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);
  403. cJSON * pJsonRoot = NULL;
  404. pJsonRoot = cJSON_CreateObject();
  405. if(NULL==pJsonRoot)
  406. {
  407. rt_kprintf("cJSON pJsonRoot NULL!!!\n");
  408. return NULL;
  409. }
  410. if(strstr(device_status.project,"A7_DTU_V3")!=RT_NULL)
  411. {
  412. cJSON_AddStringToObject(pJsonRoot, "cmd", "read_data");
  413. }
  414. else
  415. {
  416. cJSON_AddStringToObject(pJsonRoot, "cmd", "data");
  417. }
  418. cJSON * pSubJson = NULL;
  419. pSubJson = cJSON_CreateObject();
  420. if(NULL==pSubJson)
  421. {
  422. rt_kprintf("cJSON pSubJson NULL!!!\n");
  423. return NULL;
  424. }
  425. if(device_status.version_type==1)
  426. {
  427. rt_sprintf(device_status.driver,"%s-%s",VERSION_ZP,device_status.driver_4g);
  428. }
  429. else if (device_status.version_type==4) {
  430. rt_sprintf(device_status.driver,"%s-%s",VERSION_HK_2000W,device_status.driver_4g);
  431. }
  432. else if (device_status.version_type==41) {
  433. rt_sprintf(device_status.driver,"%s-%s",VERSION_HK,device_status.driver_4g);
  434. }
  435. else if (device_status.version_type==11) {
  436. rt_sprintf(device_status.driver,"%s-%s",VERSION_GK,device_status.driver_4g);
  437. }
  438. else if (device_status.version_type==5) {
  439. rt_sprintf(device_status.driver,"%s-%s",VERSION_V5,device_status.driver_4g);
  440. }
  441. else if (device_status.version_type==6) {
  442. rt_sprintf(device_status.driver,"%s-%s",VERSION_XCT,device_status.driver_4g);
  443. }
  444. cJSON_AddItemToObject(pJsonRoot, "ext", pSubJson);
  445. cJSON_AddStringToObject(pSubJson, "dver", device_status.driver); //版本号,字符型
  446. cJSON_AddStringToObject(pSubJson,"proj",device_status.project); //项目类型
  447. //设备类型
  448. device_status.dtype=3;
  449. rt_sprintf(utos, "%d",device_status.dtype);
  450. cJSON_AddStringToObject(pSubJson,"dtype",utos);
  451. //类型细分
  452. if(device_status.version_type==4) //水稻
  453. {
  454. rt_sprintf(utos,"4");
  455. cJSON_AddStringToObject(pSubJson,"vtype",utos);
  456. }
  457. else if (device_status.version_type==1) { //1.0
  458. rt_sprintf(utos,"1");
  459. cJSON_AddStringToObject(pSubJson,"vtype",utos);
  460. }
  461. else if (device_status.version_type==11) { //高空
  462. rt_sprintf(utos,"11");
  463. cJSON_AddStringToObject(pSubJson,"vtype",utos);
  464. }
  465. else if (device_status.version_type==5) { //5.0
  466. rt_sprintf(utos,"5");
  467. cJSON_AddStringToObject(pSubJson,"vtype",utos);
  468. }
  469. else if (device_status.version_type==6) { //吸虫塔
  470. rt_sprintf(utos,"6");
  471. cJSON_AddStringToObject(pSubJson,"vtype",utos);
  472. }
  473. if(device_status.version_type!=6)
  474. {
  475. //光控定时
  476. rt_sprintf(utos, "%d",device_status.sunBar);
  477. cJSON_AddStringToObject(pSubJson,"tt",utos);
  478. //定时模式
  479. rt_sprintf(utos, "%d",device_status.timeSwitch);
  480. cJSON_AddStringToObject(pSubJson,"ts",utos);
  481. //温控
  482. rt_sprintf(utos, "%d",device_status.temp_status);
  483. cJSON_AddStringToObject(pSubJson,"tps",utos);
  484. //光控状态
  485. rt_sprintf(utos, "%d",device_status.sun_status);
  486. cJSON_AddStringToObject(pSubJson,"lps",utos);
  487. //雨控状态
  488. rt_sprintf(utos, "%d",device_status.rain_status);
  489. cJSON_AddStringToObject(pSubJson,"rps",utos);
  490. //电池状态
  491. rt_sprintf(utos, "%d",device_status.bat_status);
  492. cJSON_AddStringToObject(pSubJson,"batStatus",utos);
  493. //灯管状态
  494. rt_sprintf(utos, "%d",device_status.lamp_status);
  495. cJSON_AddStringToObject(pSubJson,"lamp",utos);
  496. //上仓门状态 1-打开,0-关闭-(通道) 打开为落虫,关闭为排水
  497. rt_sprintf(utos, "%d",device_status.up_status);
  498. cJSON_AddStringToObject(pSubJson,"upds",utos);
  499. //下仓门状态 1-打开,0-关闭
  500. rt_sprintf(utos, "%d",device_status.down_status);
  501. cJSON_AddStringToObject(pSubJson,"dnds",utos);
  502. //加热仓温度
  503. rt_sprintf(utos, "%d",device_status.heattem);
  504. cJSON_AddStringToObject(pSubJson,"hrt",utos);
  505. //加热状态
  506. rt_sprintf(utos, "%d",device_status.heat_status);
  507. cJSON_AddStringToObject(pSubJson,"hs",utos);
  508. //收集时间(参数)
  509. rt_sprintf(utos, "%d",device_status.clt_time);
  510. cJSON_AddStringToObject(pSubJson,"collt",utos);
  511. //加热时间(参数)
  512. rt_sprintf(utos, "%d",device_status.heattime);
  513. cJSON_AddStringToObject(pSubJson,"htim",utos);
  514. //加热温度(参数)
  515. rt_sprintf(utos, "%d",device_status.heatvalue);
  516. cJSON_AddStringToObject(pSubJson,"hst",utos);
  517. //高温保护
  518. rt_sprintf(utos, "%d",device_status.tph);
  519. cJSON_AddStringToObject(pSubJson,"tph",utos);
  520. //低温保护
  521. rt_sprintf(utos, "%d",device_status.tpl);
  522. cJSON_AddStringToObject(pSubJson,"tpl",utos);
  523. }
  524. //震动开关
  525. rt_sprintf(utos, "%d",device_status.shake_ws);
  526. cJSON_AddStringToObject(pSubJson,"shake",utos);
  527. //震动时间
  528. rt_sprintf(utos, "%d",device_status.shake_sec);
  529. cJSON_AddStringToObject(pSubJson,"shake_sec",utos);
  530. cJSON_AddStringToObject(pSubJson,"gps",device_status.gps); //定位状态
  531. //开关状态
  532. rt_sprintf(utos, "%d",device_status.ds);
  533. cJSON_AddStringToObject(pSubJson,"ds",utos);
  534. //湿度
  535. cJSON_AddStringToObject(pSubJson,"ah",device_status.humidity);
  536. //温度
  537. cJSON_AddStringToObject(pSubJson,"at",device_status.tempture);
  538. cJSON_AddStringToObject(pSubJson,"imei",device_status.imei); //IMEI
  539. cJSON_AddStringToObject(pSubJson,"iccid",device_status.iccid); //ICCID
  540. cJSON_AddStringToObject(pSubJson,"stamp",device_status.stamp); //时间
  541. //经度
  542. cJSON_AddStringToObject(pSubJson,"lng",device_status.lng);
  543. //纬度
  544. cJSON_AddStringToObject(pSubJson,"lat",device_status.lat);
  545. //是否为上电开机
  546. cJSON_AddNumberToObject(pSubJson,"rcnt",device_status.rebootcnt);
  547. //电流(当前)
  548. cJSON_AddStringToObject(pSubJson,"current",device_status.power);
  549. //电压
  550. cJSON_AddStringToObject(pSubJson,"vbat",device_status.voltage);
  551. //保险丝电压
  552. cJSON_AddStringToObject(pSubJson,"fuse_voltage",device_status.fuse_voltage);
  553. //数据上传间隔
  554. rt_sprintf(utos, "%d",device_status.dattim);
  555. cJSON_AddStringToObject(pSubJson,"dat_f",utos);
  556. //开始时间
  557. rt_sprintf(utos, "%d",device_status.startBar);
  558. cJSON_AddStringToObject(pSubJson,"st",utos);
  559. //结束时间
  560. rt_sprintf(utos, "%d",device_status.stopBar);
  561. cJSON_AddStringToObject(pSubJson,"et",utos);
  562. //信号强度
  563. cJSON_AddStringToObject(pSubJson,"csq",device_status.csq);
  564. //工作状态
  565. rt_sprintf(utos, "%d",device_status.work_status);
  566. cJSON_AddStringToObject(pSubJson,"ws",utos);
  567. char * p = cJSON_PrintUnformatted(pJsonRoot);
  568. if(NULL == p)
  569. {
  570. cJSON_Delete(pJsonRoot);
  571. rt_kprintf("cJSON_Delete NULL!!!\n");
  572. return NULL;
  573. }
  574. cJSON_Delete(pJsonRoot);
  575. return p;
  576. }
  577. //JSON数据解析
  578. void parseDtu(char *json_string)
  579. {
  580. extern int rt_hw_rtc_init(void);
  581. char* json_s = NULL;
  582. cJSON* item_cmd = NULL; //嵌套ext
  583. cJSON* item_ext = NULL; //嵌套ext
  584. cJSON* hst = NULL; //加热温度
  585. cJSON* collt = NULL; //收集时间
  586. cJSON* et = NULL; //结束时间
  587. cJSON* datt = NULL; //数据上传间隔
  588. cJSON* shake_sec = NULL; //震动时间
  589. cJSON* tpl = NULL; //低温保护阈值
  590. cJSON* shake = NULL; //震动开关
  591. cJSON* tph = NULL; //高温保护阈值
  592. cJSON* ts = NULL; //工作模式
  593. cJSON* st = NULL; //开始时间
  594. cJSON* tt = NULL; //光控定时时长
  595. cJSON* htim = NULL; //加热时间
  596. cJSON* gps = NULL; //定位状态
  597. cJSON* datetime = NULL;
  598. cJSON* lat = NULL;
  599. cJSON* lng = NULL;
  600. cJSON* csq = NULL;
  601. cJSON* net = NULL;
  602. cJSON* iccid = NULL;
  603. cJSON* imei = NULL;
  604. cJSON* sim = NULL;
  605. cJSON* volume = NULL;
  606. cJSON* driver_4g = NULL; //版本号
  607. cJSON* project = NULL; //Project
  608. cJSON* ds = NULL; //设备状态
  609. cJSON* vt = NULL; //版本类型
  610. // cJSON* account_status = NULL; //卡状态
  611. // cJSON* data_plan = NULL; //套餐
  612. // cJSON* data_usage = NULL; //已使用
  613. // cJSON* expiry_date = NULL; //到期时间
  614. cJSON* mqttcfg1 = NULL;
  615. cJSON* mqttcfg2 = NULL;
  616. cJSON* type =NULL;
  617. cJSON* hum = NULL;
  618. cJSON* tmp = NULL;
  619. cJSON* link_sta = NULL;
  620. rt_kprintf("%s\n",json_string);
  621. cJSON *root = cJSON_Parse(json_string);
  622. if(!root)
  623. {
  624. cJSON_Delete(root);
  625. rt_kprintf("parse json err:%s",json_string);
  626. return;
  627. }
  628. else
  629. {
  630. item_cmd = cJSON_GetObjectItem(root,"cmd");
  631. item_ext=cJSON_GetObjectItem(root,"ext");
  632. //item_data = cJSON_GetObjectItem(root,"data");
  633. if(item_cmd!=RT_NULL)
  634. {
  635. if(strstr(item_cmd->valuestring,"takephoto")!=RT_NULL)
  636. {
  637. HK_TakePhoto();
  638. char *photo = makeJson_Warn("takephoto","control");
  639. DTU_Write("%s",photo);
  640. rt_free(photo);
  641. rt_thread_delay(2000);
  642. }
  643. else if (strstr(item_cmd->valuestring,"json_w_clock")!=RT_NULL) {
  644. rt_kprintf("查询时间配置!\n");
  645. json_s = makeJson_Warn(device_status.json_w_clock, "主动查询");
  646. DTU_Write("%s",json_s);
  647. rt_free(json_s);
  648. rt_thread_delay(1000);
  649. }
  650. else if (strstr(item_cmd->valuestring,"json_w_reset")!=RT_NULL) {
  651. rt_kprintf("查询重置配置!\n");
  652. json_s = makeJson_Warn(device_status.json_w_reset, "主动查询");
  653. DTU_Write("%s",json_s);
  654. rt_free(json_s);
  655. rt_thread_delay(1000);
  656. }
  657. else if (strstr(item_cmd->valuestring,"json_w_platform")!=RT_NULL) {
  658. rt_kprintf("查询平台配置!\n");
  659. json_s = makeJson_Warn(device_status.json_w_platform, "主动查询");
  660. DTU_Write("%s",json_s);
  661. rt_free(json_s);
  662. rt_thread_delay(1000);
  663. }
  664. else if (strstr(item_cmd->valuestring,"shake")!=RT_NULL)
  665. {
  666. ctr_shake(ON, 30);
  667. char *shake = makeJson_Warn("shake","control");
  668. DTU_Write("%s",shake);
  669. rt_free(shake);
  670. rt_thread_delay(2000);
  671. }
  672. //远程给rtu重新上电
  673. else if (strstr(item_cmd->valuestring,"ctrl")!=RT_NULL)
  674. {
  675. type = cJSON_GetObjectItem(item_ext,"type");
  676. if(strstr(type->valuestring,"rtu_off_on")!=RT_NULL)
  677. {
  678. rt_kprintf("DTU 12V Power down");
  679. ctr_RTU_DTU(OFF);
  680. rt_thread_delay(5000);
  681. ctr_RTU_DTU(ON);
  682. }
  683. if(strstr(type->valuestring,"takephoto")!=RT_NULL)
  684. {
  685. HK_TakePhoto();
  686. char *photo = makeJson_Warn("takephoto","control");
  687. DTU_Write("%s",photo);
  688. rt_free(photo);
  689. rt_thread_delay(2000);
  690. }
  691. }
  692. //切换平台
  693. else if (strstr(item_cmd->valuestring,"platform")!=RT_NULL)
  694. {
  695. type = cJSON_GetObjectItem(item_ext,"type");
  696. if(strstr(type->valuestring,"8.136.98.49")!=RT_NULL)
  697. {
  698. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  699. ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
  700. //给RTU发送切换指令
  701. DTU_Write("%s",makeJson_platform("8.136.98.49"));
  702. rt_free(makeJson_platform("8.136.98.49"));
  703. rt_thread_delay(1000);
  704. rt_sprintf(device_status.json_w_platform,"平台操作:切换平台>大数据,操作时间:%s",device_status.stamp);
  705. json_s = makeJson_Warn(device_status.json_w_platform, "切换平台");
  706. ef_set_env_blob("json_w_platform", &device_status.json_w_platform, sizeof(device_status.json_w_platform)); //将修改屏幕参数操作记录保存到Flash中
  707. if(atoi(device_status.net)==0)
  708. DTU_Write("%s",json_s);
  709. rt_free(json_s);
  710. rt_thread_delay(1000);
  711. MA_save();
  712. }
  713. else if (strstr(type->valuestring,"120.27.222.26")!=RT_NULL)
  714. {
  715. rt_sprintf(device_status.mqttcfg1, "120.27.222.26");
  716. ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
  717. //给RTU发送切换指令
  718. DTU_Write("%s",makeJson_platform("120.27.222.26"));
  719. rt_free(makeJson_platform("120.27.222.26"));
  720. rt_thread_delay(1000);
  721. rt_sprintf(device_status.json_w_platform,"平台操作:切换平台>四情,操作时间:%s",device_status.stamp);
  722. json_s = makeJson_Warn(device_status.json_w_platform, "切换平台");
  723. ef_set_env_blob("json_w_platform", &device_status.json_w_platform, sizeof(device_status.json_w_platform)); //将修改屏幕参数操作记录保存到Flash中
  724. if(atoi(device_status.net)==0)
  725. DTU_Write("%s",json_s);
  726. rt_free(json_s);
  727. rt_thread_delay(1000);
  728. MA_save();
  729. }
  730. }
  731. //水稻2000万像素的串口拍照指令
  732. else if (strstr(item_cmd->valuestring,"mvs_photo")!=RT_NULL)
  733. {
  734. HK_TakePhoto();
  735. char *photo = makeJson_Warn("mvs_takephoto","control");
  736. DTU_Write("%s",photo);
  737. rt_free(photo);
  738. rt_thread_delay(2000);
  739. }
  740. else if (strstr(item_cmd->valuestring,"reboot")!=RT_NULL)
  741. {
  742. char *restart = makeJson_Warn("sysrem will restart","control");
  743. rt_kprintf("%s\n",restart);
  744. rt_thread_delay(2000);
  745. DTU_Write("%s",restart);
  746. rt_hw_cpu_reset();
  747. }
  748. else if (strstr(item_cmd->valuestring,"reset")!=RT_NULL) {
  749. ResetParamDefault();
  750. char *restart = makeJson_Warn("sysrem will reset","control");
  751. DTU_Write("%s",restart);
  752. rt_free(restart);
  753. rt_thread_delay(2000);
  754. }
  755. else if (strstr(item_cmd->valuestring,"update")!=RT_NULL)
  756. {
  757. rt_kprintf("准备升级!!!");
  758. device_status.update_flag = 1;
  759. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  760. rt_kprintf("%s\n",makeJson_Warn("system will update","control"));
  761. rt_thread_delay(2000);
  762. DTU_Write("%s",makeJson_Warn("system will update","control"));
  763. rt_thread_delay(20);
  764. rt_hw_cpu_reset();
  765. }
  766. else if (strstr(item_cmd->valuestring,"read")!=RT_NULL)
  767. {
  768. char * readinfo = makeJson_ReadInfo();
  769. DTU_Write("%s",readinfo);
  770. rt_kprintf("%s\n",readinfo);
  771. rt_free(readinfo);
  772. device_status.hmi_fresh = 2;
  773. device_status.refresh_num = 15;
  774. }
  775. // else if (strstr(item_cmd->valuestring,"log")!=RT_NULL) {
  776. // rt_thread_mdelay(2000);
  777. // //read_all_flash_log();
  778. // }
  779. //设置温湿度
  780. else if (strstr(item_cmd->valuestring,"sensor")!=RT_NULL) {
  781. //2022-04-12add:485传感器正常,不再解析服务器下发的温湿度数据
  782. if(device_status.sensor_485_sta==1) return;
  783. hum = cJSON_GetObjectItem(item_ext,"new_hum");
  784. tmp = cJSON_GetObjectItem(item_ext,"new_tem");
  785. if(hum!=NULL&&tmp!=NULL)
  786. {
  787. device_status.sensor_sta = 1;
  788. //先判断是否是空字符再判断数值是否合理
  789. if(strcmp(hum->valuestring,"")!=0&&atoi(hum->valuestring)>=0&&atoi(hum->valuestring)<=100&&atoi(hum->valuestring)!=30)
  790. {
  791. if(atoi(hum->valuestring)!=atoi(device_status.humidity))
  792. {
  793. rt_sprintf(device_status.humidity, "%s",hum->valuestring);
  794. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  795. rt_kprintf("保存湿度成功:%d\n",atoi(device_status.humidity));
  796. }
  797. rt_sprintf(device_status.humidity, "%s",hum->valuestring);
  798. rt_kprintf("设置湿度成功:%d\n",atoi(device_status.humidity));
  799. }
  800. else
  801. {
  802. rt_kprintf("设置湿度失败:%s!!!\n",hum->valuestring);
  803. }
  804. //先判断是否是空字符再判断数值是否合理
  805. if(strcmp(tmp->valuestring,"")!=0&&atoi(tmp->valuestring)>-50&&atoi(tmp->valuestring)<=100)
  806. {
  807. if(atoi(tmp->valuestring)!=atoi(device_status.tempture))
  808. {
  809. rt_sprintf(device_status.tempture, "%s",tmp->valuestring);
  810. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //湿度
  811. rt_kprintf("保存温度成功:%d\n",atoi(device_status.tempture));
  812. }
  813. rt_sprintf(device_status.tempture, "%s",tmp->valuestring);
  814. rt_kprintf("设置温度成功:%d\n",atoi(device_status.tempture));
  815. }
  816. else {
  817. rt_kprintf("设置温度失败:%s!!!\n",tmp->valuestring);
  818. }
  819. }
  820. }
  821. else if (strstr(item_cmd->valuestring,"synchronization")!=RT_NULL)
  822. {
  823. device_status.refresh_timeout=0;
  824. //driver_4g = cJSON_GetObjectItem(item_ext,"version");
  825. driver_4g = cJSON_GetObjectItem(item_ext,"dver");
  826. //project = cJSON_GetObjectItem(item_ext,"project");
  827. project = cJSON_GetObjectItem(item_ext,"proj");
  828. datetime = cJSON_GetObjectItem(item_ext,"datetime");
  829. lat = cJSON_GetObjectItem(item_ext,"lat");
  830. lng = cJSON_GetObjectItem(item_ext,"lng");
  831. csq = cJSON_GetObjectItem(item_ext,"csq");
  832. net = cJSON_GetObjectItem(item_ext,"net");
  833. sim = cJSON_GetObjectItem(item_ext,"sim");
  834. iccid = cJSON_GetObjectItem(item_ext,"iccid");
  835. imei = cJSON_GetObjectItem(item_ext,"imei");
  836. gps = cJSON_GetObjectItem(item_ext,"gps");
  837. volume = cJSON_GetObjectItem(item_ext,"volume");
  838. mqttcfg1 = cJSON_GetObjectItem(item_ext,"mqttcfg1");
  839. mqttcfg2 = cJSON_GetObjectItem(item_ext,"mqttcfg2");
  840. link_sta = cJSON_GetObjectItem(item_ext,"link_sta");
  841. //如果DTU的IMEI和FLash中的不一样,保存到FLASH
  842. if(strstr(device_status.imei,imei->valuestring)==RT_NULL)
  843. {
  844. rt_kprintf("Change IMEI:new:%s-old:%s",imei->valuestring,device_status.imei);
  845. rt_sprintf(device_status.imei, "%s",imei->valuestring);
  846. ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei)); //IMEI
  847. }
  848. else {
  849. rt_sprintf(device_status.imei,"%s",imei->valuestring);
  850. rt_kprintf("IMEI not change:%s-%s",imei->valuestring,device_status.imei);
  851. }
  852. //如果DTU的ICCID和FLash中的不一样,保存到FLASH
  853. if(strstr(device_status.iccid,iccid->valuestring)==RT_NULL)
  854. {
  855. rt_kprintf("Change ICCID:new:%s-old:%s",iccid->valuestring,device_status.iccid);
  856. rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
  857. ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid)); //Iccid
  858. }
  859. else {
  860. rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
  861. rt_kprintf("ICCID not change:%s-%s",iccid->valuestring,device_status.iccid);
  862. }
  863. rt_kprintf("DTU时间:%s",datetime->valuestring);
  864. char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
  865. strncpy(year_s, datetime->valuestring, 4);
  866. strncpy(month_s,datetime->valuestring+4,2);
  867. strncpy(day_s,datetime->valuestring+6,2);
  868. strncpy(hour_s,datetime->valuestring+8,2);
  869. strncpy(min_s,datetime->valuestring+10,2);
  870. strncpy(sec_s,datetime->valuestring+12,2);
  871. if(atoi(year_s)>2020&&atoi(year_s)<3000&&atoi(month_s)>0&&atoi(month_s)<13&&atoi(day_s)>0&&atoi(day_s)<32)
  872. {
  873. rt_kprintf("设置时间:%d年%d月%d日\n",atoi(year_s),atoi(month_s),atoi(day_s));
  874. if(atoi(net->valuestring)==0&&atoi(year_s)>=2022)
  875. {
  876. if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
  877. {
  878. }
  879. else {
  880. rt_hw_rtc_init();
  881. }
  882. }
  883. else {
  884. rt_kprintf("网络连接失败,同步时间失败!!!\n");
  885. }
  886. }
  887. 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)
  888. {
  889. rt_kprintf("设置时间:%d时%d分%d秒\n",atoi(hour_s),atoi(min_s),atoi(sec_s));
  890. if(atoi(net->valuestring)==0&&atoi(year_s)>=2022)
  891. {
  892. set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
  893. }
  894. else {
  895. rt_kprintf("网络连接失败,同步时间失败!!!\n");
  896. }
  897. }
  898. rt_kprintf("参数同步:\nlat:%s\nlng:%s\n",lat->valuestring,lng->valuestring);
  899. //CSQ
  900. if(csq!=NULL&&atoi(csq->valuestring)>=0&&atoi(csq->valuestring)<=31)
  901. {
  902. rt_sprintf(device_status.csq,"%s",csq->valuestring);
  903. }
  904. else {
  905. rt_sprintf(device_status.csq,"0");
  906. }
  907. //更新网线接入状态
  908. if(link_sta!=NULL&&atoi(link_sta->valuestring)>=0&&atoi(link_sta->valuestring)<=1)
  909. {
  910. device_status.link_sta = atoi(link_sta->valuestring);
  911. rt_kprintf("\n>>>>>>>>>>>>>有线接入状态>>>>>>>>>>>>>>:%d\n",device_status.link_sta);
  912. }
  913. //Volume
  914. if(volume!=NULL&&atoi(volume->valuestring)>=0&&atoi(volume->valuestring)<=31)
  915. {
  916. device_status.vol = atoi(volume->valuestring);
  917. rt_kprintf("设置音量:%d\n",device_status.vol);
  918. }
  919. else {
  920. device_status.vol = 7;
  921. }
  922. //NET
  923. if (net!=NULL&&atoi(net->valuestring)>=0&&atoi(net->valuestring)<=1)
  924. {
  925. rt_kprintf("网络状态:%s\n",net->valuestring);
  926. rt_sprintf(device_status.net,"%s",net->valuestring);
  927. }
  928. else {
  929. rt_sprintf(device_status.net,"1");
  930. }
  931. //SIM
  932. if (sim!=NULL&&atoi(sim->valuestring)>=0&&atoi(sim->valuestring)<=1)
  933. {
  934. rt_sprintf(device_status.sim,"%s",sim->valuestring);
  935. }
  936. else {
  937. rt_sprintf(device_status.sim,"1");
  938. }
  939. //GPS
  940. if (gps!=NULL&&atoi(gps->valuestring)>=0&&atoi(gps->valuestring)<=3)
  941. {
  942. rt_sprintf(device_status.gps,"%s",gps->valuestring);
  943. if(atoi(gps->valuestring)!=0)
  944. {
  945. if(strstr(device_status.lat,lat->valuestring)==NULL)
  946. {
  947. rt_sprintf(device_status.lat,"%s",lat->valuestring);
  948. rt_sprintf(device_status.lng,"%s",lng->valuestring);
  949. ef_set_env_blob("lat", &device_status.lat, sizeof(device_status.lat)); //Iccid
  950. ef_set_env_blob("lng", &device_status.lng, sizeof(device_status.lng)); //Iccid
  951. }
  952. else {
  953. rt_kprintf("经纬度信息同步:lat-%s,lng-%s\n",lat->valuestring,lng->valuestring);
  954. }
  955. }else if(atoi(gps->valuestring)==0)
  956. {
  957. sprintf(device_status.lat,"%s",lat->valuestring);
  958. sprintf(device_status.lng,"%s",lng->valuestring);
  959. }
  960. }
  961. else {
  962. rt_sprintf(device_status.gps,"0");
  963. if(sizeof(device_status.lat)<3)
  964. {
  965. sprintf(device_status.lat,"%s","34.8088073");
  966. sprintf(device_status.lng,"%s","113.6624298");
  967. }
  968. else {
  969. sprintf(device_status.lat,"%s",lat->valuestring);
  970. sprintf(device_status.lng,"%s",lng->valuestring);
  971. }
  972. }
  973. sprintf(device_status.driver_4g,"%s",driver_4g->valuestring);
  974. sprintf(device_status.project,"%s",project->valuestring);
  975. if(mqttcfg1!=NULL)
  976. {
  977. if(strstr("8.136.98.49",mqttcfg1->valuestring)!=RT_NULL)
  978. {
  979. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  980. }
  981. else if (strstr("120.27.222.26",mqttcfg1->valuestring)!=RT_NULL) {
  982. rt_sprintf(device_status.mqttcfg1, "120.27.222.26");
  983. }
  984. else {
  985. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  986. }
  987. rt_kprintf("当前平台1:%s\n",mqttcfg1->valuestring);
  988. }
  989. if(mqttcfg2!=NULL)
  990. {
  991. sprintf(device_status.mqttcfg2,"%s",mqttcfg2->valuestring);
  992. rt_kprintf("当前平台2:%s\n",mqttcfg2->valuestring);
  993. }
  994. if( device_status.num_updata==0)
  995. {
  996. Beep_Tip(100);
  997. }
  998. else if(device_status.num_updata!=0&&device_status.hmi_fresh==2)
  999. {
  1000. if(strstr(device_status.project,"A7_DTU_V3")!=RT_NULL)
  1001. {
  1002. UpLoad_Data();
  1003. }
  1004. else
  1005. {
  1006. rt_thread_delay(2000);
  1007. UpLoad_Data();
  1008. }
  1009. }
  1010. device_status.hmi_fresh = 0;
  1011. }
  1012. if(item_ext!=RT_NULL&&strstr(item_cmd->valuestring,"paramconf")!=RT_NULL)
  1013. {
  1014. hst = cJSON_GetObjectItem(item_ext,"hst");
  1015. et = cJSON_GetObjectItem(item_ext,"et");
  1016. collt = cJSON_GetObjectItem(item_ext,"collt");
  1017. datt = cJSON_GetObjectItem(item_ext,"datt");
  1018. shake_sec = cJSON_GetObjectItem(item_ext,"shake_sec");
  1019. tpl = cJSON_GetObjectItem(item_ext,"tpl");
  1020. shake = cJSON_GetObjectItem(item_ext,"shake");
  1021. tph = cJSON_GetObjectItem(item_ext,"tph");
  1022. ts = cJSON_GetObjectItem(item_ext,"ts");
  1023. st = cJSON_GetObjectItem(item_ext,"st");
  1024. tt = cJSON_GetObjectItem(item_ext,"tt");
  1025. htim = cJSON_GetObjectItem(item_ext,"htim");
  1026. ds = cJSON_GetObjectItem(item_ext,"ds");
  1027. vt = cJSON_GetObjectItem(item_ext,"vt");
  1028. if(hst!=NULL)
  1029. {
  1030. if(hst->type==cJSON_Number)
  1031. {
  1032. if(hst->valueint>=10&&hst->valueint<=150)
  1033. {
  1034. device_status.heatvalue = hst->valueint;
  1035. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); //开始时间
  1036. rt_kprintf("加热温度hst:%d\n",hst->valueint);
  1037. }
  1038. else {
  1039. rt_kprintf("加热温度hst设置失败参数错误!!!\n");
  1040. }
  1041. }
  1042. else {
  1043. if(atoi(hst->valuestring)>10&&atoi(hst->valuestring)<150)
  1044. {
  1045. device_status.heatvalue=atoi(hst->valuestring);
  1046. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); //加热温度
  1047. rt_kprintf("加热温度hst:%d\n",atoi(hst->valuestring));
  1048. }
  1049. else {
  1050. rt_kprintf("加热温度hst设置失败参数错误!!!\n");
  1051. }
  1052. }
  1053. }
  1054. if (st!=NULL)
  1055. {
  1056. if(st->type==cJSON_Number)
  1057. {
  1058. if(st->valueint>=0&&st->valueint<=23)
  1059. {
  1060. device_status.startBar = st->valueint;
  1061. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); //开始时间
  1062. rt_kprintf("开始时间st:%d\n",st->valueint);
  1063. }
  1064. else {
  1065. rt_kprintf("开始时间st设置失败参数错误!!!\n");
  1066. }
  1067. }
  1068. else {
  1069. if(atoi(st->valuestring)>=0&&atoi(st->valuestring)<=23)
  1070. {
  1071. device_status.startBar = atoi(st->valuestring);
  1072. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); //开始时间
  1073. rt_kprintf("开始时间st:%d\n",atoi(st->valuestring));
  1074. }
  1075. else {
  1076. rt_kprintf("开始时间st设置失败参数错误!!!\n");
  1077. }
  1078. }
  1079. }
  1080. if(et!=NULL)
  1081. {
  1082. if(et->type==cJSON_Number)
  1083. {
  1084. if(et->valueint>=0&&et->valueint<=23)
  1085. {
  1086. device_status.stopBar = et->valueint;
  1087. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); //结束时间
  1088. rt_kprintf("结束时间et:%d\n",et->valueint);
  1089. }
  1090. else {
  1091. rt_kprintf("结束时间et设置失败参数错误!!!\n");
  1092. }
  1093. }
  1094. else if(et->type==cJSON_String){
  1095. if(atoi(et->valuestring)>=0&&atoi(et->valuestring)<=23)
  1096. {
  1097. device_status.stopBar = atoi(et->valuestring);
  1098. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); //结束时间
  1099. rt_kprintf("结束时间et:%d\n",atoi(et->valuestring));
  1100. }
  1101. else {
  1102. rt_kprintf("结束时间et设置失败参数错误!!!\n");
  1103. }
  1104. }
  1105. }
  1106. if(collt!=NULL)
  1107. {
  1108. if(collt->type==cJSON_Number)
  1109. {
  1110. if(collt->valueint>=1&&collt->valueint<=60)
  1111. {
  1112. device_status.clt_time = collt->valueint;
  1113. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); //收集时间
  1114. rt_kprintf("收集时间collt:%d\n",collt->valueint);
  1115. }
  1116. else {
  1117. rt_kprintf("收集时间collt设置失败参数错误!!!\n");
  1118. }
  1119. }
  1120. else if (collt->type==cJSON_String) {
  1121. if(atoi(collt->valuestring)>=1&&atoi(collt->valuestring)<=60)
  1122. {
  1123. device_status.clt_time = atoi(collt->valuestring);
  1124. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); //收集时间
  1125. rt_kprintf("收集时间collt:%d\n",atoi(collt->valuestring));
  1126. }
  1127. else {
  1128. rt_kprintf("收集时间collt设置失败参数错误!!!\n");
  1129. }
  1130. }
  1131. }
  1132. if(datt!=NULL)
  1133. {
  1134. if(datt->type==cJSON_Number)
  1135. {
  1136. if(datt->valueint>=1&&datt->valueint<=120)
  1137. {
  1138. device_status.dattim = datt->valueint;
  1139. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim)); //数据上传间隔
  1140. rt_kprintf("数据上传间隔datt:%d\n",datt->valueint);
  1141. }
  1142. else {
  1143. rt_kprintf("数据上传间隔datt设置失败参数错误!!!\n");
  1144. }
  1145. }
  1146. else if (datt->type==cJSON_String) {
  1147. if(atoi(datt->valuestring)>=1&&atoi(datt->valuestring)<=120)
  1148. {
  1149. device_status.dattim = atoi(datt->valuestring);
  1150. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim)); //数据上传间隔
  1151. rt_kprintf("数据上传间隔datt:%d\n",atoi(datt->valuestring));
  1152. }
  1153. else {
  1154. rt_kprintf("数据上传间隔datt设置失败参数错误!!!\n");
  1155. }
  1156. }
  1157. }
  1158. if(shake_sec!=NULL)
  1159. {
  1160. if(shake_sec->type==cJSON_Number)
  1161. {
  1162. if(shake_sec->valueint>=1&&shake_sec->valueint<=1000)
  1163. {
  1164. device_status.shake_sec = shake_sec->valueint;
  1165. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1166. rt_kprintf("震动时间shake_sec:%d\n",shake_sec->valueint);
  1167. }
  1168. else {
  1169. rt_kprintf("震动时间shake_sec设置失败参数错误!!!\n");
  1170. }
  1171. }
  1172. else if (shake_sec->type==cJSON_String) {
  1173. if(atoi(shake_sec->valuestring)>=1&&atoi(shake_sec->valuestring)<=1000)
  1174. {
  1175. device_status.shake_sec = atoi(shake_sec->valuestring);
  1176. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1177. rt_kprintf("震动时间shake_sec:%d\n",atoi(shake_sec->valuestring));
  1178. }
  1179. else {
  1180. rt_kprintf("震动时间shake_sec设置失败参数错误!!!\n");
  1181. }
  1182. }
  1183. }
  1184. if(shake!=NULL)
  1185. {
  1186. if(shake->type==cJSON_Number)
  1187. {
  1188. if(shake->valueint==0||shake->valueint==1)
  1189. {
  1190. device_status.shake_ws = shake->valueint;
  1191. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1192. rt_kprintf("震动开关shake:%d\n",shake->valueint);
  1193. }
  1194. else {
  1195. rt_kprintf("震动开关shake设置失败参数错误!!!\n");
  1196. }
  1197. }
  1198. else if (shake->type==cJSON_String) {
  1199. if(atoi(shake->valuestring)==0||atoi(shake->valuestring)==1)
  1200. {
  1201. device_status.shake_ws = atoi(shake->valuestring);
  1202. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1203. rt_kprintf("震动开关shake:%d\n",atoi(shake->valuestring));
  1204. }
  1205. else {
  1206. rt_kprintf("震动开关shake设置失败参数错误!!!\n");
  1207. }
  1208. }
  1209. }
  1210. if(tt!=NULL)
  1211. {
  1212. if(tt->type==cJSON_Number)
  1213. {
  1214. if(tt->valueint>=1&&tt->valueint<=10)
  1215. {
  1216. #if LAMP_EN == 0
  1217. device_status.sunBar = tt->valueint;
  1218. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); //光控定时
  1219. rt_kprintf("光控定时tt:%d\n",tt->valueint);
  1220. #else
  1221. rt_kprintf("三光源模式,禁止设置定时时长!!!\n");
  1222. #endif
  1223. }
  1224. else {
  1225. rt_kprintf("光控定时tt设置失败参数错误!!!\n");
  1226. }
  1227. }
  1228. else if (tt->type==cJSON_String) {
  1229. if(atoi(tt->valuestring)>=1&&atoi(tt->valuestring)<=10)
  1230. {
  1231. #if LAMP_EN == 0
  1232. device_status.sunBar = atoi(tt->valuestring);
  1233. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); //光控定时
  1234. rt_kprintf("光控定时tt:%d\n",atoi(tt->valuestring));
  1235. #else
  1236. rt_kprintf("三光源模式,禁止设置定时时长!!!\n");
  1237. #endif
  1238. }
  1239. else {
  1240. rt_kprintf("光控定时tt设置失败参数错误!!!\n");
  1241. }
  1242. }
  1243. }
  1244. if(htim!=NULL)
  1245. {
  1246. if(htim->type==cJSON_Number)
  1247. {
  1248. if(htim->valueint>=1&&htim->valueint<=30)
  1249. {
  1250. device_status.heattime = htim->valueint;
  1251. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); //加热时间
  1252. rt_kprintf("加热时长htim:%d\n",htim->valueint);
  1253. }
  1254. else {
  1255. rt_kprintf("加热时长htim设置失败参数错误!!!\n");
  1256. }
  1257. }
  1258. else if (htim->type==cJSON_String) {
  1259. if(atoi(htim->valuestring)>=1&&atoi(htim->valuestring)<=30)
  1260. {
  1261. device_status.heattime = atoi(htim->valuestring);
  1262. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); //加热时间
  1263. rt_kprintf("加热时长htim:%d\n",atoi(htim->valuestring));
  1264. }
  1265. else {
  1266. rt_kprintf("加热时长htim设置失败参数错误!!!\n");
  1267. }
  1268. }
  1269. }
  1270. if(ts!=NULL)
  1271. {
  1272. if(ts->type==cJSON_Number)
  1273. {
  1274. if(ts->valueint==0||ts->valueint==1)
  1275. {
  1276. #if LAMP_EN == 0
  1277. device_status.timeSwitch = ts->valueint;
  1278. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); //工作模式
  1279. // time_t now;
  1280. // time(&now);
  1281. // static struct tm nowstamp;
  1282. // nowstamp =*localtime(&now);
  1283. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1284. // static rt_uint32_t nowstamp_num = 0;
  1285. // nowstamp_num = mktime(&nowstamp);
  1286. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  1287. rt_kprintf("定时模式ts:%d\n",ts->valueint);
  1288. #else
  1289. rt_kprintf("三光源模式,禁止设置定时!!!\n");
  1290. #endif
  1291. }
  1292. else {
  1293. rt_kprintf("定时模式ts设置失败参数错误!!!\n");
  1294. }
  1295. }
  1296. else if (ts->type==cJSON_String) {
  1297. if(atoi(ts->valuestring)==0||atoi(ts->valuestring)==1)
  1298. {
  1299. #if LAMP_EN == 0
  1300. device_status.timeSwitch = atoi(ts->valuestring);
  1301. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); //工作模式
  1302. // time_t now;
  1303. // time(&now);
  1304. // static struct tm nowstamp;
  1305. // nowstamp =*localtime(&now);
  1306. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1307. // static rt_uint32_t nowstamp_num = 0;
  1308. // nowstamp_num = mktime(&nowstamp);
  1309. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  1310. rt_kprintf("定时模式ts:%d\n",atoi(ts->valuestring));
  1311. #else
  1312. rt_kprintf("三光源模式,禁止设置定时!!!\n");
  1313. #endif
  1314. }
  1315. else {
  1316. rt_kprintf("定时模式ts设置失败参数错误!!!\n");
  1317. }
  1318. }
  1319. }
  1320. if(tpl!=NULL)
  1321. {
  1322. if(tpl->type==cJSON_Number)
  1323. {
  1324. if(tpl->valueint>=-50&&tpl->valueint<=50)
  1325. {
  1326. device_status.tpl = tpl->valueint;
  1327. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); //低温保护
  1328. rt_kprintf("低温保护tpl:%d\n",tpl->valueint);
  1329. }
  1330. else {
  1331. rt_kprintf("低温保护tpl设置失败参数错误!!!\n");
  1332. }
  1333. }
  1334. else if (tpl->type==cJSON_String) {
  1335. if(atoi(tpl->valuestring)>=-50&&atoi(tpl->valuestring)<=50)
  1336. {
  1337. device_status.tpl = atoi(tpl->valuestring);
  1338. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); //低温保护
  1339. rt_kprintf("低温保护tpl:%d\n",atoi(tpl->valuestring));
  1340. }
  1341. else {
  1342. rt_kprintf("低温保护tpl设置失败参数错误!!!\n");
  1343. }
  1344. }
  1345. }
  1346. if(tph!=NULL)
  1347. {
  1348. if(tph->type==cJSON_Number)
  1349. {
  1350. if(tph->valueint>=50&&tph->valueint<=80)
  1351. {
  1352. device_status.tph = tph->valueint;
  1353. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); //高温保护
  1354. rt_kprintf("高温保护tph:%d\n",tph->valueint);
  1355. }
  1356. else {
  1357. rt_kprintf("高温保护tph设置失败参数错误!!!\n");
  1358. }
  1359. }
  1360. else if (tph->type==cJSON_String) {
  1361. if(atoi(tph->valuestring)>=50&&atoi(tph->valuestring)<=80)
  1362. {
  1363. device_status.tph = atoi(tph->valuestring);
  1364. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); //高温保护
  1365. rt_kprintf("高温保护tph:%d\n",atoi(tph->valuestring));
  1366. }
  1367. else {
  1368. rt_kprintf("高温保护tph设置失败参数错误!!!\n");
  1369. }
  1370. }
  1371. }
  1372. if(ds!=NULL)
  1373. {
  1374. if(ds->type==cJSON_Number)
  1375. {
  1376. if(ds->valueint>=0&&ds->valueint <=1)
  1377. {
  1378. device_status.ds = ds->valueint;
  1379. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1380. rt_kprintf("开关机状态:%d\n",ds->valueint);
  1381. }
  1382. else {
  1383. rt_kprintf("开关机状态设置失败参数错误!!!\n");
  1384. }
  1385. }
  1386. else if (ds->type==cJSON_String) {
  1387. if(atoi(ds->valuestring)>=0&&atoi(ds->valuestring) <=1)
  1388. {
  1389. device_status.ds = atoi(ds->valuestring);
  1390. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1391. rt_kprintf("开关机状态:%d\n",atoi(ds->valuestring));
  1392. }
  1393. else {
  1394. rt_kprintf("开关机状态设置失败参数错误!!!\n");
  1395. }
  1396. }
  1397. }
  1398. if(vt!=NULL)
  1399. {
  1400. if(atoi(vt->valuestring)>=0&&atoi(vt->valuestring)<=100)
  1401. {
  1402. device_status.version_type = atoi(vt->valuestring);
  1403. ef_set_env_blob("version_type", &device_status.version_type, sizeof(device_status.version_type));
  1404. rt_kprintf("版本类型vt:%d\n",atoi(vt->valuestring));
  1405. }
  1406. else {
  1407. rt_kprintf("版本类型设置失败参数错误!!!\n");
  1408. }
  1409. }
  1410. UpLoad_Data();
  1411. rt_thread_delay(2000);
  1412. //2022/06/23,add:
  1413. 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);
  1414. json_s = makeJson_Warn(device_status.json_w_clock, "修改时间");
  1415. ef_set_env_blob("json_w_clock", &device_status.json_w_clock, sizeof(device_status.json_w_clock)); //将修改屏幕参数操作记录保存到Flash中
  1416. if(atoi(device_status.net)==0)
  1417. DTU_Write("%s",json_s);
  1418. rt_free(json_s);
  1419. rt_thread_delay(1000);
  1420. MA_save();
  1421. rt_thread_delay(1000);
  1422. }
  1423. }
  1424. cJSON_Delete(root);
  1425. }
  1426. return;
  1427. }
  1428. void UpLoad_Data(void)
  1429. {
  1430. char *updata = makeJson();
  1431. rt_kprintf("upload data:%d\n%s\n",rt_strlen(updata),updata);
  1432. DTU_Write("%s",updata);
  1433. rt_free(updata);
  1434. list_mem();
  1435. //播放数据上传中语音
  1436. }