dtu_uart2.c 65 KB

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