| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415 |
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-28 Administrator the first version
- */
- #include <rtthread.h>
- #include "hmi.h"
- #include <board.h>
- #include <rtdevice.h>
- #include "stdarg.h"
- #include "string.h"
- #include "stdio.h"
- #include "dtu_uart2.h"
- #include <cJSON.h>
- #include "control.h"
- #include "sht30_collect.h"
- #include <easyflash.h>
- #include "sound.h"
- #include <ymodem.h>
- #define DBG_TAG "dtu_uart2.c"
- #define DBG_LVL LOG_LVL_INFO
- #include "beep.h"
- #define DTU_THREAD_PRIORITY 6 //线程优先级
- #define DTU_THREAD_STACK_SIZE 2048 //线程栈空间大小
- #define DTU_THREAD_TIMESLICE 5 //时间片
- Device_Status_typedef device_status;
- DataTemp data_arry;
- void list_mem(void);
- /* 串口接收消息结构*/
- struct rx_msg
- {
- rt_device_t dev;
- rt_size_t size;
- };
- /* 消息队列句柄 */
- static struct rt_messagequeue rx_mq_dtu;
- rt_thread_t dtu_recv_data = RT_NULL;
- rt_thread_t dtu_send_data = RT_NULL;
- rt_device_t dtuserial; /*串口设备句柄*/
- struct serial_configure dtuconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
- static uint8_t makejson_sta = 0;
- static int DTU_uart2_init(void)
- {
- dtuserial = rt_device_find(DTU_UART_NAME);
- /*修改串口配置参数*/
- dtuconfig.baud_rate = BAUD_RATE_115200; //修改波特率9600
- dtuconfig.data_bits = DATA_BITS_8; //数据位8
- dtuconfig.stop_bits = STOP_BITS_1; //停止位1
- dtuconfig.bufsz = 512; //缓冲区1k
- dtuconfig.parity = PARITY_NONE; //奇校验
- /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
- rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
- /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
- rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);
- return RT_EOK;
- }
- /*数据发送*/
- void DTU_Write(char *fmt, ...)
- {
- const char dtu_conf[4] = {device_status.vol,0x00,0x00,0x00};
- va_list arg;
- static char dtu_str[800];
- rt_size_t str_length;
- va_start(arg,fmt);
- str_length = rt_vsnprintf(dtu_str, sizeof(dtu_str) - 1, fmt, arg);
- if (str_length > 800 - 1)
- str_length = 800 - 1;
- rt_device_write(dtuserial, 0, dtu_str, str_length);
- if(strstr(dtu_str,"volume")!=RT_NULL)
- {
- rt_device_write(dtuserial,0,dtu_conf,4);
- }
- else if (strstr(dtu_str,"reset")!=RT_NULL) {
- const char dtu_conf[4] = {7,0x00,0x00,0x00};
- rt_device_write(dtuserial,0,dtu_conf,4);
- }
- va_end(arg);
- memset(dtu_str,'\0',sizeof(dtu_str));
- }
- /* 接收数据回调函数 */
- static rt_err_t dtu_input(rt_device_t dev, rt_size_t size)
- {
- struct rx_msg msg;
- rt_err_t result;
- msg.dev = dev;
- msg.size = size;
- result = rt_mq_send(&rx_mq_dtu,&msg, sizeof(struct rx_msg));
- if ( result == -RT_EFULL)
- {
- /* 消息队列满 */
- rt_kprintf("message queue full!\n");
- }
- return result;
- }
- /* 入口函数-指令接收*/
- static void dtu_thread_entry_recv(void *parameter)
- {
- struct rx_msg msg;
- rt_err_t result;
- rt_uint32_t rx_length;
- rt_uint32_t rx_err = 0;
- static char str_check[2]={'{','}'};
- static char rx_buffer[512];
- static char rx_buffer_tmp[512];
- static char rx_buffer_tmp2[512];
- while(1)
- {
- rt_memset(&msg, 0, sizeof(msg));
- // 从消息队列中获取被回调函数放入消息队列中的内容
- result = rt_mq_recv(&rx_mq_dtu, &msg, sizeof(struct rx_msg), 100);
- if(result == RT_EOK)
- {
- rx_length =(sizeof(rx_buffer) - 1) > msg.size ?
- msg.size : sizeof(rx_buffer) - 1;
- rx_length = rt_device_read(msg.dev, 0, &rx_buffer[0], rx_length);
- rx_buffer[rx_length] = '\0';
- rt_kprintf("recv data:%s\n",rx_buffer);
- //过滤异常数据
- char str_check_head[2];
- char str_check_end[2];
- rt_sprintf(str_check_head, "%c",rx_buffer[0]);
- rt_sprintf(str_check_end, "%c",rx_buffer[rx_length-1]);
- if(str_check_end[0]==str_check[1]&&str_check_head[0]==str_check[0])
- {
- parseDtu(rx_buffer);
- rt_memset(rx_buffer,0,sizeof(rx_buffer));
- rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
- rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
- }
- else {
- if(rx_buffer[rx_length-1]!='}')
- {
- rt_sprintf(rx_buffer_tmp, "%s",rx_buffer);
- }
- else {
- rt_sprintf(rx_buffer_tmp2,"%s",rx_buffer);
- rt_sprintf(rx_buffer, "%s%s",rx_buffer_tmp,rx_buffer_tmp2);
- //rt_kprintf("rxbuffer:%s",rx_buffer);
- parseDtu(rx_buffer);
- rt_memset(rx_buffer,0,sizeof(rx_buffer));
- rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
- rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
- }
- //rt_kprintf("%s",rx_buffer);
- }
- }
- else {
- rx_err ++;
- if(rx_err==54000)//90分钟未接收到数据
- {
- if(device_status.version_type==6)
- {
- rt_kprintf("RK3399 transter Error!\n");
- ctr_photo(OFF);
- }
- }
- else if (rx_err==54100) {
- rx_err = 0;
- ctr_photo(ON);
- }
- }
- //rt_kprintf("%s\n",rx_json_buffer);
- // 将内容写回到串口1
- //rt_device_write(dtuserial, 0, &rx_json_buffer[0], rx_length);
- }
- }
- /*入口函数-指令发送*/
- static void dtu_thread_entry_send(void *parameter)
- {
- device_status.power_status = 1;
- device_status.num_updata = 0;
- rt_thread_delay(10000);
- while(1)
- {
- if(atoi(device_status.net)==0)
- {
- if(device_status.num_updata!=0)
- {
- device_status.num_updata--;
- //屏幕休眠
- if(device_status.num_updata==(device_status.dattim * 60-10))
- {
- HMI_WRITE("sleep=%d",1);
- }
- if(Read_Real_Voltage()/100<12&&device_status.power_status==1)
- {
- DTU_Write("%s",makeJson_Warn("power down","power"));
- device_status.power_status = 0;
- rt_thread_delay(1500);
- }
- }
- else {
- char *readinfo = makeJson_ReadInfo();
- rt_kprintf("upload data:%s\n",readinfo);
- UpLoad_Data();
- rt_free(readinfo);
- if(atoi(device_status.fuse_voltage)<1)
- {
- rt_thread_delay(500);
- static char res[30];
- sprintf(res, "Fuse is fusing:%s",device_status.fuse_voltage);
- char * res_fuse_warn = makeJson_Warn(res,"device");
- DTU_Write("%s",res_fuse_warn);
- rt_free(res_fuse_warn);
- }
- device_status.num_updata = device_status.dattim * 60;
- if(device_status.dattim==0) device_status.dattim=20; //数据上传时间间隔设置为0?
- if(device_status.bat_status==0) device_status.power_status = 1; //解除欠压,清除标志位
- }
- }
- else {
- rt_kprintf("wait connect internet......\n");
- }
- rt_thread_mdelay(1000);
- }
- }
- /* 创建线程 */
- int dtu_data_play(void)
- {
- rt_err_t rm_dtu_result;
- DTU_uart2_init();
- //初始化消息队列
- 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);
- if(rm_dtu_result==RT_EOK)
- {
- rt_kprintf("消息队列创建成功!!");
- }
- else {
- rt_kprintf("消息队列创建失败!!");
- }
- /* 设置接收回调函数 */
- rt_device_set_rx_indicate(dtuserial, dtu_input);
- /*创建线程-指令发送*/
- dtu_send_data = rt_thread_create("DTU_Uart_thread_send",
- dtu_thread_entry_send,RT_NULL,
- DTU_THREAD_STACK_SIZE,
- 19,
- DTU_THREAD_TIMESLICE
- );
- /* 创建线程-指令接收解析*/
- dtu_recv_data = rt_thread_create("DTU_Uart_thread_recv",
- dtu_thread_entry_recv, RT_NULL,
- DTU_THREAD_STACK_SIZE,
- DTU_THREAD_PRIORITY,
- DTU_THREAD_TIMESLICE);
- /* 如果获得线程控制块,启动这个线程 */
- if(dtu_send_data!=RT_NULL)
- rt_thread_startup(dtu_send_data);
- /* 如果获得线程控制块,启动这个线程 */
- if (dtu_recv_data != RT_NULL)
- rt_thread_startup(dtu_recv_data);
- return RT_EOK;
- }
- char * makeJson_ReadInfo()
- {
- cJSON * rJsonRoot = NULL;
- rJsonRoot = cJSON_CreateObject();
- if(NULL==rJsonRoot)
- {
- return NULL;
- }
- cJSON_AddStringToObject(rJsonRoot, "uart", "getinfo");
- char * r = cJSON_PrintUnformatted(rJsonRoot);
- if(NULL == r)
- {
- cJSON_Delete(rJsonRoot);
- return NULL;
- }
- cJSON_Delete(rJsonRoot);
- return r;
- }
- //{"cmd": "platform", "ext": {"type": "120.27.222.26"}}
- char * makeJson_platform(char *plt)
- {
- cJSON * platform = NULL;
- platform = cJSON_CreateObject();
- if(NULL==platform)
- {
- return NULL;
- }
- cJSON_AddStringToObject(platform, "cmd", "platform");
- cJSON * extstr = NULL;
- extstr = cJSON_CreateObject();
- if(NULL==extstr)
- {
- return NULL;
- }
- cJSON_AddItemToObject(platform, "ext", extstr);
- cJSON_AddStringToObject(extstr, "type",plt);
- char * p_str = cJSON_PrintUnformatted(platform);
- if(NULL == p_str)
- {
- cJSON_Delete(platform);
- return NULL;
- }
- cJSON_Delete(platform);
- return p_str;
- }
- char * makeJson_takephoto()
- {
- cJSON * takephoto = NULL;
- takephoto = cJSON_CreateObject();
- if(NULL==takephoto)
- {
- return NULL;
- }
- cJSON_AddStringToObject(takephoto, "cmd", "takephoto");
- char * takephoto_str = cJSON_PrintUnformatted(takephoto);
- if(NULL == takephoto_str)
- {
- cJSON_Delete(takephoto);
- return NULL;
- }
- cJSON_Delete(takephoto);
- return takephoto_str;
- }
- char * makeJson_Rtutakephoto()
- {
- cJSON * tJsonRoot = NULL;
- tJsonRoot = cJSON_CreateObject();
- if(NULL==tJsonRoot)
- {
- return NULL;
- }
- cJSON_AddStringToObject(tJsonRoot, "cmd", "ctrl");
- cJSON * tpubstring = NULL;
- tpubstring = cJSON_CreateObject();
- if(NULL==tpubstring)
- {
- rt_kprintf("cJSON tpubstring NULL!!!\n");
- return NULL;
- }
- cJSON_AddItemToObject(tJsonRoot, "ext", tpubstring);
- cJSON_AddStringToObject(tpubstring, "takephoto", "2");
- char * t = cJSON_PrintUnformatted(tJsonRoot);
- if(NULL == t)
- {
- cJSON_Delete(tJsonRoot);
- return NULL;
- }
- cJSON_Delete(tJsonRoot);
- return t;
- }
- //预警消息
- char *makeJson_Warn(char * s,char *t)
- {
- cJSON * wJsonRoot = NULL;
- wJsonRoot = cJSON_CreateObject();
- if(NULL==wJsonRoot)
- {
- return NULL;
- }
- cJSON_AddStringToObject(wJsonRoot, "cmd", "warn");
- cJSON * pubstring = NULL;
- pubstring = cJSON_CreateObject();
- if(NULL==pubstring)
- {
- rt_kprintf("cJSON pubstring NULL!!!\n");
- return NULL;
- }
- cJSON_AddItemToObject(wJsonRoot, "ext", pubstring);
- cJSON_AddStringToObject(pubstring,"status",s);//
- cJSON_AddStringToObject(pubstring,"type",t);
- char * w = cJSON_PrintUnformatted(wJsonRoot);
- if(NULL == w)
- {
- cJSON_Delete(wJsonRoot);
- rt_kprintf("wJsonRoot cJSON_Delete NULL!!!\n");
- return NULL;
- }
- cJSON_Delete(wJsonRoot);
- return w;
- }
- /*生成JSON数据*/
- char * makeJson()
- {
- char utos[4];
- if(device_status.gps==NULL)
- rt_sprintf(device_status.gps, "0");
- //如果设备工作并且电流小于500,并且不是上电启动灯管异常
- if(atoi(device_status.power)<400&&device_status.work_status==1&&makejson_sta!=0)
- {
- rt_kprintf("power is:%d\n",atoi(device_status.power));
- device_status.lamp_status = 1;
- }
- else {
- device_status.lamp_status = 0;
- }
- makejson_sta = 1;
- time_t stamp=time(RT_NULL);
- static struct tm tm;
- gmtime_r(&stamp, &tm);
- 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);
- cJSON * pJsonRoot = NULL;
- pJsonRoot = cJSON_CreateObject();
- if(NULL==pJsonRoot)
- {
- rt_kprintf("cJSON pJsonRoot NULL!!!\n");
- return NULL;
- }
- if(strstr(device_status.project,"A7_DTU_V3")!=RT_NULL)
- {
- cJSON_AddStringToObject(pJsonRoot, "cmd", "read_data");
- }
- else
- {
- cJSON_AddStringToObject(pJsonRoot, "cmd", "data");
- }
- cJSON * pSubJson = NULL;
- pSubJson = cJSON_CreateObject();
- if(NULL==pSubJson)
- {
- rt_kprintf("cJSON pSubJson NULL!!!\n");
- return NULL;
- }
- if(device_status.version_type==1)
- {
- rt_sprintf(device_status.driver,"%s-%s",VERSION_ZP,device_status.driver_4g);
- }
- else if (device_status.version_type==4) {
- rt_sprintf(device_status.driver,"%s-%s",VERSION_HK_2000W,device_status.driver_4g);
- }
- else if (device_status.version_type==41) {
- rt_sprintf(device_status.driver,"%s-%s",VERSION_HK,device_status.driver_4g);
- }
- else if (device_status.version_type==11) {
- rt_sprintf(device_status.driver,"%s-%s",VERSION_GK,device_status.driver_4g);
- }
- else if (device_status.version_type==5) {
- rt_sprintf(device_status.driver,"%s-%s",VERSION_V5,device_status.driver_4g);
- }
- else if (device_status.version_type==6) {
- rt_sprintf(device_status.driver,"%s-%s",VERSION_XCT,device_status.driver_4g);
- }
- cJSON_AddItemToObject(pJsonRoot, "ext", pSubJson);
- cJSON_AddStringToObject(pSubJson, "dver", device_status.driver); //版本号,字符型
- cJSON_AddStringToObject(pSubJson,"proj",device_status.project); //项目类型
- //设备类型
- device_status.dtype=3;
- rt_sprintf(utos, "%d",device_status.dtype);
- cJSON_AddStringToObject(pSubJson,"dtype",utos);
- //类型细分
- if(device_status.version_type==4) //水稻
- {
- rt_sprintf(utos,"4");
- cJSON_AddStringToObject(pSubJson,"vtype",utos);
- }
- else if (device_status.version_type==1) { //1.0
- rt_sprintf(utos,"1");
- cJSON_AddStringToObject(pSubJson,"vtype",utos);
- }
- else if (device_status.version_type==11) { //高空
- rt_sprintf(utos,"11");
- cJSON_AddStringToObject(pSubJson,"vtype",utos);
- }
- else if (device_status.version_type==5) { //5.0
- rt_sprintf(utos,"5");
- cJSON_AddStringToObject(pSubJson,"vtype",utos);
- }
- else if (device_status.version_type==6) { //吸虫塔
- rt_sprintf(utos,"6");
- cJSON_AddStringToObject(pSubJson,"vtype",utos);
- }
- if(device_status.version_type!=6)
- {
- //光控定时
- rt_sprintf(utos, "%d",device_status.sunBar);
- cJSON_AddStringToObject(pSubJson,"tt",utos);
- //定时模式
- rt_sprintf(utos, "%d",device_status.timeSwitch);
- cJSON_AddStringToObject(pSubJson,"ts",utos);
- //温控
- rt_sprintf(utos, "%d",device_status.temp_status);
- cJSON_AddStringToObject(pSubJson,"tps",utos);
- //光控状态
- rt_sprintf(utos, "%d",device_status.sun_status);
- cJSON_AddStringToObject(pSubJson,"lps",utos);
- //雨控状态
- rt_sprintf(utos, "%d",device_status.rain_status);
- cJSON_AddStringToObject(pSubJson,"rps",utos);
- //电池状态
- rt_sprintf(utos, "%d",device_status.bat_status);
- cJSON_AddStringToObject(pSubJson,"batStatus",utos);
- //灯管状态
- rt_sprintf(utos, "%d",device_status.lamp_status);
- cJSON_AddStringToObject(pSubJson,"lamp",utos);
- //上仓门状态 1-打开,0-关闭-(通道) 打开为落虫,关闭为排水
- rt_sprintf(utos, "%d",device_status.up_status);
- cJSON_AddStringToObject(pSubJson,"upds",utos);
- //下仓门状态 1-打开,0-关闭
- rt_sprintf(utos, "%d",device_status.down_status);
- cJSON_AddStringToObject(pSubJson,"dnds",utos);
- //加热仓温度
- rt_sprintf(utos, "%d",device_status.heattem);
- cJSON_AddStringToObject(pSubJson,"hrt",utos);
- //加热状态
- rt_sprintf(utos, "%d",device_status.heat_status);
- cJSON_AddStringToObject(pSubJson,"hs",utos);
- //收集时间(参数)
- rt_sprintf(utos, "%d",device_status.clt_time);
- cJSON_AddStringToObject(pSubJson,"collt",utos);
- //加热时间(参数)
- rt_sprintf(utos, "%d",device_status.heattime);
- cJSON_AddStringToObject(pSubJson,"htim",utos);
- //加热温度(参数)
- rt_sprintf(utos, "%d",device_status.heatvalue);
- cJSON_AddStringToObject(pSubJson,"hst",utos);
- //高温保护
- rt_sprintf(utos, "%d",device_status.tph);
- cJSON_AddStringToObject(pSubJson,"tph",utos);
- //低温保护
- rt_sprintf(utos, "%d",device_status.tpl);
- cJSON_AddStringToObject(pSubJson,"tpl",utos);
- }
- //震动开关
- rt_sprintf(utos, "%d",device_status.shake_ws);
- cJSON_AddStringToObject(pSubJson,"shake",utos);
- //震动时间
- rt_sprintf(utos, "%d",device_status.shake_sec);
- cJSON_AddStringToObject(pSubJson,"shake_sec",utos);
- cJSON_AddStringToObject(pSubJson,"gps",device_status.gps); //定位状态
- //开关状态
- rt_sprintf(utos, "%d",device_status.ds);
- cJSON_AddStringToObject(pSubJson,"ds",utos);
- //湿度
- cJSON_AddStringToObject(pSubJson,"ah",device_status.humidity);
- //温度
- cJSON_AddStringToObject(pSubJson,"at",device_status.tempture);
- cJSON_AddStringToObject(pSubJson,"imei",device_status.imei); //IMEI
- cJSON_AddStringToObject(pSubJson,"iccid",device_status.iccid); //ICCID
- cJSON_AddStringToObject(pSubJson,"stamp",device_status.stamp); //时间
- //经度
- cJSON_AddStringToObject(pSubJson,"lng",device_status.lng);
- //纬度
- cJSON_AddStringToObject(pSubJson,"lat",device_status.lat);
- //是否为上电开机
- cJSON_AddNumberToObject(pSubJson,"rcnt",device_status.rebootcnt);
- //电流(当前)
- cJSON_AddStringToObject(pSubJson,"current",device_status.power);
- //电压
- cJSON_AddStringToObject(pSubJson,"vbat",device_status.voltage);
- //保险丝电压
- cJSON_AddStringToObject(pSubJson,"fuse_voltage",device_status.fuse_voltage);
- //数据上传间隔
- rt_sprintf(utos, "%d",device_status.dattim);
- cJSON_AddStringToObject(pSubJson,"dat_f",utos);
- //开始时间
- rt_sprintf(utos, "%d",device_status.startBar);
- cJSON_AddStringToObject(pSubJson,"st",utos);
- //结束时间
- rt_sprintf(utos, "%d",device_status.stopBar);
- cJSON_AddStringToObject(pSubJson,"et",utos);
- //信号强度
- cJSON_AddStringToObject(pSubJson,"csq",device_status.csq);
- //工作状态
- rt_sprintf(utos, "%d",device_status.work_status);
- cJSON_AddStringToObject(pSubJson,"ws",utos);
- char * p = cJSON_PrintUnformatted(pJsonRoot);
- if(NULL == p)
- {
- cJSON_Delete(pJsonRoot);
- rt_kprintf("cJSON_Delete NULL!!!\n");
- return NULL;
- }
- cJSON_Delete(pJsonRoot);
- return p;
- }
- //JSON数据解析
- void parseDtu(char *json_string)
- {
- extern int rt_hw_rtc_init(void);
- uint8_t gps_sta = 0;
- cJSON* item_cmd = NULL; //嵌套ext
- cJSON* item_ext = NULL; //嵌套ext
- cJSON* hst = NULL; //加热温度
- cJSON* collt = NULL; //收集时间
- cJSON* et = NULL; //结束时间
- cJSON* datt = NULL; //数据上传间隔
- cJSON* shake_sec = NULL; //震动时间
- cJSON* tpl = NULL; //低温保护阈值
- cJSON* shake = NULL; //震动开关
- cJSON* tph = NULL; //高温保护阈值
- cJSON* ts = NULL; //工作模式
- cJSON* st = NULL; //开始时间
- cJSON* tt = NULL; //光控定时时长
- cJSON* htim = NULL; //加热时间
- cJSON* gps = NULL; //定位状态
- cJSON* datetime = NULL;
- cJSON* lat = NULL;
- cJSON* lng = NULL;
- cJSON* csq = NULL;
- cJSON* net = NULL;
- cJSON* iccid = NULL;
- cJSON* imei = NULL;
- cJSON* sim = NULL;
- cJSON* volume = NULL;
- cJSON* driver_4g = NULL; //版本号
- cJSON* project = NULL; //Project
- cJSON* ds = NULL; //设备状态
- cJSON* vt = NULL; //版本类型
- // cJSON* account_status = NULL; //卡状态
- // cJSON* data_plan = NULL; //套餐
- // cJSON* data_usage = NULL; //已使用
- // cJSON* expiry_date = NULL; //到期时间
- cJSON* mqttcfg1 = NULL;
- cJSON* mqttcfg2 = NULL;
- cJSON* type =NULL;
- cJSON* hum = NULL;
- cJSON* tmp = NULL;
- cJSON* link_sta = NULL;
- rt_kprintf("%s\n",json_string);
- cJSON *root = cJSON_Parse(json_string);
- if(!root)
- {
- cJSON_Delete(root);
- rt_kprintf("parse json err:%s",json_string);
- return;
- }
- else
- {
- item_cmd = cJSON_GetObjectItem(root,"cmd");
- item_ext=cJSON_GetObjectItem(root,"ext");
- //item_data = cJSON_GetObjectItem(root,"data");
- if(item_cmd!=RT_NULL)
- {
- if(strstr(item_cmd->valuestring,"takephoto")!=RT_NULL)
- {
- HK_TakePhoto();
- char *photo = makeJson_Warn("takephoto","control");
- DTU_Write("%s",photo);
- rt_free(photo);
- rt_thread_delay(2000);
- }
- else if (strstr(item_cmd->valuestring,"shake")!=RT_NULL)
- {
- ctr_shake(ON, 30);
- char *shake = makeJson_Warn("shake","control");
- DTU_Write("%s",shake);
- rt_free(shake);
- rt_thread_delay(2000);
- }
- //远程给rtu重新上电
- else if (strstr(item_cmd->valuestring,"ctrl")!=RT_NULL)
- {
- type = cJSON_GetObjectItem(item_ext,"type");
- if(strstr(type->valuestring,"rtu_off_on")!=RT_NULL)
- {
- rt_kprintf("DTU 12V Power down");
- ctr_RTU_DTU(OFF);
- rt_thread_delay(5000);
- ctr_RTU_DTU(ON);
- }
- }
- //切换平台
- else if (strstr(item_cmd->valuestring,"platform")!=RT_NULL)
- {
- type = cJSON_GetObjectItem(item_ext,"type");
- if(strstr(type->valuestring,"8.136.98.49")!=RT_NULL)
- {
- rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
- ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
- //给RTU发送切换指令
- DTU_Write("%s",makeJson_platform("8.136.98.49"));
- rt_free(makeJson_platform("8.136.98.49"));
- rt_thread_delay(2000);
- MA_save();
- }
- else if (strstr(type->valuestring,"120.27.222.26")!=RT_NULL)
- {
- rt_sprintf(device_status.mqttcfg1, "120.27.222.26");
- ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
- //给RTU发送切换指令
- DTU_Write("%s",makeJson_platform("120.27.222.26"));
- rt_free(makeJson_platform("120.27.222.26"));
- rt_thread_delay(2000);
- MA_save();
- }
- }
- //水稻2000万像素的串口拍照指令
- else if (strstr(item_cmd->valuestring,"mvs_photo")!=RT_NULL)
- {
- HK_TakePhoto();
- char *photo = makeJson_Warn("mvs_takephoto","control");
- DTU_Write("%s",photo);
- rt_free(photo);
- rt_thread_delay(2000);
- }
- else if (strstr(item_cmd->valuestring,"reboot")!=RT_NULL)
- {
- char *restart = makeJson_Warn("sysrem will restart","control");
- rt_kprintf("%s\n",restart);
- rt_thread_delay(2000);
- DTU_Write("%s",restart);
- rt_hw_cpu_reset();
- }
- else if (strstr(item_cmd->valuestring,"reset")!=RT_NULL) {
- ResetParamDefault();
- char *restart = makeJson_Warn("sysrem will reset","control");
- DTU_Write("%s",restart);
- rt_free(restart);
- rt_thread_delay(2000);
- }
- else if (strstr(item_cmd->valuestring,"update")!=RT_NULL)
- {
- rt_kprintf("准备升级!!!");
- device_status.update_flag = 1;
- ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
- rt_kprintf("%s\n",makeJson_Warn("system will update","control"));
- rt_thread_delay(2000);
- DTU_Write("%s",makeJson_Warn("system will update","control"));
- rt_thread_delay(20);
- rt_hw_cpu_reset();
- }
- else if (strstr(item_cmd->valuestring,"read")!=RT_NULL)
- {
- char * readinfo = makeJson_ReadInfo();
- DTU_Write("%s",readinfo);
- rt_kprintf("%s\n",readinfo);
- rt_free(readinfo);
- device_status.hmi_fresh = 2;
- device_status.refresh_num = 15;
- }
- // else if (strstr(item_cmd->valuestring,"log")!=RT_NULL) {
- // rt_thread_mdelay(2000);
- // //read_all_flash_log();
- // }
- //设置温湿度
- else if (strstr(item_cmd->valuestring,"sensor")!=RT_NULL) {
- //2022-04-12add:485传感器正常,不再解析服务器下发的温湿度数据
- if(device_status.sensor_485_sta==1) return;
- hum = cJSON_GetObjectItem(item_ext,"new_hum");
- tmp = cJSON_GetObjectItem(item_ext,"new_tem");
- if(hum!=NULL&&tmp!=NULL)
- {
- device_status.sensor_sta = 1;
- //先判断是否是空字符再判断数值是否合理
- if(strcmp(hum->valuestring,"")!=0&&atoi(hum->valuestring)>=0&&atoi(hum->valuestring)<=100&&atoi(hum->valuestring)!=30)
- {
- if(atoi(hum->valuestring)!=atoi(device_status.humidity))
- {
- rt_sprintf(device_status.humidity, "%s",hum->valuestring);
- ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
- rt_kprintf("保存湿度成功:%d\n",atoi(device_status.humidity));
- }
- rt_sprintf(device_status.humidity, "%s",hum->valuestring);
- rt_kprintf("设置湿度成功:%d\n",atoi(device_status.humidity));
- }
- else
- {
- rt_kprintf("设置湿度失败:%s!!!\n",hum->valuestring);
- }
- //先判断是否是空字符再判断数值是否合理
- if(strcmp(tmp->valuestring,"")!=0&&atoi(tmp->valuestring)>-50&&atoi(tmp->valuestring)<=100)
- {
- if(atoi(tmp->valuestring)!=atoi(device_status.tempture))
- {
- rt_sprintf(device_status.tempture, "%s",tmp->valuestring);
- ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //湿度
- rt_kprintf("保存温度成功:%d\n",atoi(device_status.tempture));
- }
- rt_sprintf(device_status.tempture, "%s",tmp->valuestring);
- rt_kprintf("设置温度成功:%d\n",atoi(device_status.tempture));
- }
- else {
- rt_kprintf("设置温度失败:%s!!!\n",tmp->valuestring);
- }
- }
- }
- else if (strstr(item_cmd->valuestring,"synchronization")!=RT_NULL)
- {
- device_status.refresh_timeout=0;
- //driver_4g = cJSON_GetObjectItem(item_ext,"version");
- driver_4g = cJSON_GetObjectItem(item_ext,"dver");
- //project = cJSON_GetObjectItem(item_ext,"project");
- project = cJSON_GetObjectItem(item_ext,"proj");
- datetime = cJSON_GetObjectItem(item_ext,"datetime");
- lat = cJSON_GetObjectItem(item_ext,"lat");
- lng = cJSON_GetObjectItem(item_ext,"lng");
- csq = cJSON_GetObjectItem(item_ext,"csq");
- net = cJSON_GetObjectItem(item_ext,"net");
- sim = cJSON_GetObjectItem(item_ext,"sim");
- iccid = cJSON_GetObjectItem(item_ext,"iccid");
- imei = cJSON_GetObjectItem(item_ext,"imei");
- gps = cJSON_GetObjectItem(item_ext,"gps");
- volume = cJSON_GetObjectItem(item_ext,"volume");
- mqttcfg1 = cJSON_GetObjectItem(item_ext,"mqttcfg1");
- mqttcfg2 = cJSON_GetObjectItem(item_ext,"mqttcfg2");
- link_sta = cJSON_GetObjectItem(item_ext,"link_sta");
- //如果DTU的IMEI和FLash中的不一样,保存到FLASH
- if(strstr(device_status.imei,imei->valuestring)==RT_NULL)
- {
- rt_kprintf("Change IMEI:new:%s-old:%s",imei->valuestring,device_status.imei);
- rt_sprintf(device_status.imei, "%s",imei->valuestring);
- ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei)); //IMEI
- }
- else {
- rt_sprintf(device_status.imei,"%s",imei->valuestring);
- rt_kprintf("IMEI not change:%s-%s",imei->valuestring,device_status.imei);
- }
- //如果DTU的ICCID和FLash中的不一样,保存到FLASH
- if(strstr(device_status.iccid,iccid->valuestring)==RT_NULL)
- {
- rt_kprintf("Change ICCID:new:%s-old:%s",iccid->valuestring,device_status.iccid);
- rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
- ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid)); //Iccid
- }
- else {
- rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
- rt_kprintf("ICCID not change:%s-%s",iccid->valuestring,device_status.iccid);
- }
- rt_kprintf("DTU时间:%s",datetime->valuestring);
- char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
- strncpy(year_s, datetime->valuestring, 4);
- strncpy(month_s,datetime->valuestring+4,2);
- strncpy(day_s,datetime->valuestring+6,2);
- strncpy(hour_s,datetime->valuestring+8,2);
- strncpy(min_s,datetime->valuestring+10,2);
- strncpy(sec_s,datetime->valuestring+12,2);
- 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)
- {
- rt_kprintf("设置时间:%d年%d月%d日\n",atoi(year_s),atoi(month_s),atoi(day_s));
- if(atoi(net->valuestring)==0)
- {
- if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
- {
- }
- else {
- rt_hw_rtc_init();
- }
- }
- else {
- rt_kprintf("网络连接失败,同步时间失败!!!\n");
- }
- }
- 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)
- {
- rt_kprintf("设置时间:%d时%d分%d秒\n",atoi(hour_s),atoi(min_s),atoi(sec_s));
- if(atoi(net->valuestring)==0)
- {
- set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
- }
- else {
- rt_kprintf("网络连接失败,同步时间失败!!!\n");
- }
- }
- rt_kprintf("参数同步:\nlat:%s\nlng:%s\n",lat->valuestring,lng->valuestring);
- //CSQ
- if(csq!=NULL&&atoi(csq->valuestring)>=0&&atoi(csq->valuestring)<=31)
- {
- rt_sprintf(device_status.csq,"%s",csq->valuestring);
- }
- else {
- rt_sprintf(device_status.csq,"0");
- }
- //更新网线接入状态
- if(link_sta!=NULL&&atoi(link_sta->valuestring)>=0&&atoi(link_sta->valuestring)<=1)
- {
- device_status.link_sta = atoi(link_sta->valuestring);
- rt_kprintf("\n>>>>>>>>>>>>>有线接入状态>>>>>>>>>>>>>>:%d\n",device_status.link_sta);
- }
- //Volume
- if(volume!=NULL&&atoi(volume->valuestring)>=0&&atoi(volume->valuestring)<=31)
- {
- device_status.vol = atoi(volume->valuestring);
- rt_kprintf("设置音量:%d\n",device_status.vol);
- }
- else {
- device_status.vol = 7;
- }
- //NET
- if (net!=NULL&&atoi(net->valuestring)>=0&&atoi(net->valuestring)<=1)
- {
- rt_kprintf("网络状态:%s\n",net->valuestring);
- rt_sprintf(device_status.net,"%s",net->valuestring);
- }
- else {
- rt_sprintf(device_status.net,"1");
- }
- //SIM
- if (sim!=NULL&&atoi(sim->valuestring)>=0&&atoi(sim->valuestring)<=1)
- {
- rt_sprintf(device_status.sim,"%s",sim->valuestring);
- }
- else {
- rt_sprintf(device_status.sim,"1");
- }
- //GPS
- if (gps!=NULL&&atoi(gps->valuestring)>=0&&atoi(gps->valuestring)<=3)
- {
- rt_sprintf(device_status.gps,"%s",gps->valuestring);
- if(atoi(gps->valuestring)!=0)
- {
- if(atoi(lat->valuestring)==-1||atoi(lat->valuestring)==1)
- {
- sprintf(device_status.lat,"%s","34.8088073");
- sprintf(device_status.lng,"%s","113.6624298");
- }
- else {
- if(lat->valuestring!=NULL||lng->valuestring!=NULL){
- sprintf(device_status.lat,"%s",lat->valuestring);
- sprintf(device_status.lng,"%s",lng->valuestring);
- }
- else {
- sprintf(device_status.lat,"%s","34.8088073");
- sprintf(device_status.lng,"%s","113.6624298");
- }
- if(gps_sta==0)
- {
- gps_sta = 1;
- ef_set_env_blob("lat", &device_status.lat, sizeof(device_status.lat)); //Iccid
- ef_set_env_blob("lng", &device_status.lng, sizeof(device_status.lng)); //Iccid
- }
- }
- }else if(atoi(gps->valuestring)==0)
- {
- sprintf(device_status.lat,"%s",lat->valuestring);
- sprintf(device_status.lng,"%s",lng->valuestring);
- }
- }
- else {
- rt_sprintf(device_status.gps,"0");
- if(sizeof(device_status.lat)<3)
- {
- sprintf(device_status.lat,"%s","34.8088073");
- sprintf(device_status.lng,"%s","113.6624298");
- }
- }
- sprintf(device_status.driver_4g,"%s",driver_4g->valuestring);
- sprintf(device_status.project,"%s",project->valuestring);
- // sprintf(device_status.mqttcfg1,"%s",mqttcfg1->valuestring);
- // sprintf(device_status.mqttcfg2,"%s",mqttcfg2->valuestring);
- if( device_status.num_updata==0)
- {
- Beep_Tip(100);
- }
- else if(device_status.num_updata!=0&&device_status.hmi_fresh==2)
- {
- if(strstr(device_status.project,"A7_DTU_V3")!=RT_NULL)
- {
- UpLoad_Data();
- }
- else
- {
- rt_thread_delay(2000);
- UpLoad_Data();
- }
- }
- device_status.hmi_fresh = 0;
- }
- if(item_ext!=RT_NULL&&strstr(item_cmd->valuestring,"paramconf")!=RT_NULL)
- {
- hst = cJSON_GetObjectItem(item_ext,"hst");
- et = cJSON_GetObjectItem(item_ext,"et");
- collt = cJSON_GetObjectItem(item_ext,"collt");
- datt = cJSON_GetObjectItem(item_ext,"datt");
- shake_sec = cJSON_GetObjectItem(item_ext,"shake_sec");
- tpl = cJSON_GetObjectItem(item_ext,"tpl");
- shake = cJSON_GetObjectItem(item_ext,"shake");
- tph = cJSON_GetObjectItem(item_ext,"tph");
- ts = cJSON_GetObjectItem(item_ext,"ts");
- st = cJSON_GetObjectItem(item_ext,"st");
- tt = cJSON_GetObjectItem(item_ext,"tt");
- htim = cJSON_GetObjectItem(item_ext,"htim");
- ds = cJSON_GetObjectItem(item_ext,"ds");
- vt = cJSON_GetObjectItem(item_ext,"vt");
- if(hst!=NULL)
- {
- if(hst->type==cJSON_Number)
- {
- if(hst->valueint>=10&&hst->valueint<=150)
- {
- device_status.heatvalue = hst->valueint;
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); //开始时间
- rt_kprintf("加热温度hst:%d\n",hst->valueint);
- }
- else {
- rt_kprintf("加热温度hst设置失败参数错误!!!\n");
- }
- }
- else {
- if(atoi(hst->valuestring)>10&&atoi(hst->valuestring)<150)
- {
- device_status.heatvalue=atoi(hst->valuestring);
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); //加热温度
- rt_kprintf("加热温度hst:%d\n",atoi(hst->valuestring));
- }
- else {
- rt_kprintf("加热温度hst设置失败参数错误!!!\n");
- }
- }
- }
- if (st!=NULL)
- {
- if(st->type==cJSON_Number)
- {
- if(st->valueint>=0&&st->valueint<=23)
- {
- device_status.startBar = st->valueint;
- ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); //开始时间
- rt_kprintf("开始时间st:%d\n",st->valueint);
- }
- else {
- rt_kprintf("开始时间st设置失败参数错误!!!\n");
- }
- }
- else {
- if(atoi(st->valuestring)>=0&&atoi(st->valuestring)<=23)
- {
- device_status.startBar = atoi(st->valuestring);
- ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); //开始时间
- rt_kprintf("开始时间st:%d\n",atoi(st->valuestring));
- }
- else {
- rt_kprintf("开始时间st设置失败参数错误!!!\n");
- }
- }
- }
- if(et!=NULL)
- {
- if(et->type==cJSON_Number)
- {
- if(et->valueint>=0&&et->valueint<=23)
- {
- device_status.stopBar = et->valueint;
- ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); //结束时间
- rt_kprintf("结束时间et:%d\n",et->valueint);
- }
- else {
- rt_kprintf("结束时间et设置失败参数错误!!!\n");
- }
- }
- else if(et->type==cJSON_String){
- if(atoi(et->valuestring)>=0&&atoi(et->valuestring)<=23)
- {
- device_status.stopBar = atoi(et->valuestring);
- ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); //结束时间
- rt_kprintf("结束时间et:%d\n",atoi(et->valuestring));
- }
- else {
- rt_kprintf("结束时间et设置失败参数错误!!!\n");
- }
- }
- }
- if(collt!=NULL)
- {
- if(collt->type==cJSON_Number)
- {
- if(collt->valueint>=1&&collt->valueint<=60)
- {
- device_status.clt_time = collt->valueint;
- ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); //收集时间
- rt_kprintf("收集时间collt:%d\n",collt->valueint);
- }
- else {
- rt_kprintf("收集时间collt设置失败参数错误!!!\n");
- }
- }
- else if (collt->type==cJSON_String) {
- if(atoi(collt->valuestring)>=1&&atoi(collt->valuestring)<=60)
- {
- device_status.clt_time = atoi(collt->valuestring);
- ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); //收集时间
- rt_kprintf("收集时间collt:%d\n",atoi(collt->valuestring));
- }
- else {
- rt_kprintf("收集时间collt设置失败参数错误!!!\n");
- }
- }
- }
- if(datt!=NULL)
- {
- if(datt->type==cJSON_Number)
- {
- if(datt->valueint>=1&&datt->valueint<=120)
- {
- device_status.dattim = datt->valueint;
- ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim)); //数据上传间隔
- rt_kprintf("数据上传间隔datt:%d\n",datt->valueint);
- }
- else {
- rt_kprintf("数据上传间隔datt设置失败参数错误!!!\n");
- }
- }
- else if (datt->type==cJSON_String) {
- if(atoi(datt->valuestring)>=1&&atoi(datt->valuestring)<=120)
- {
- device_status.dattim = atoi(datt->valuestring);
- ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim)); //数据上传间隔
- rt_kprintf("数据上传间隔datt:%d\n",atoi(datt->valuestring));
- }
- else {
- rt_kprintf("数据上传间隔datt设置失败参数错误!!!\n");
- }
- }
- }
- if(shake_sec!=NULL)
- {
- if(shake_sec->type==cJSON_Number)
- {
- if(shake_sec->valueint>=1&&shake_sec->valueint<=1000)
- {
- device_status.shake_sec = shake_sec->valueint;
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
- rt_kprintf("震动时间shake_sec:%d\n",shake_sec->valueint);
- }
- else {
- rt_kprintf("震动时间shake_sec设置失败参数错误!!!\n");
- }
- }
- else if (shake_sec->type==cJSON_String) {
- if(atoi(shake_sec->valuestring)>=1&&atoi(shake_sec->valuestring)<=1000)
- {
- device_status.shake_sec = atoi(shake_sec->valuestring);
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
- rt_kprintf("震动时间shake_sec:%d\n",atoi(shake_sec->valuestring));
- }
- else {
- rt_kprintf("震动时间shake_sec设置失败参数错误!!!\n");
- }
- }
- }
- if(shake!=NULL)
- {
- if(shake->type==cJSON_Number)
- {
- if(shake->valueint==0||shake->valueint==1)
- {
- device_status.shake_ws = shake->valueint;
- ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
- rt_kprintf("震动开关shake:%d\n",shake->valueint);
- }
- else {
- rt_kprintf("震动开关shake设置失败参数错误!!!\n");
- }
- }
- else if (shake->type==cJSON_String) {
- if(atoi(shake->valuestring)==0||atoi(shake->valuestring)==1)
- {
- device_status.shake_ws = atoi(shake->valuestring);
- ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
- rt_kprintf("震动开关shake:%d\n",atoi(shake->valuestring));
- }
- else {
- rt_kprintf("震动开关shake设置失败参数错误!!!\n");
- }
- }
- }
- if(tt!=NULL)
- {
- if(tt->type==cJSON_Number)
- {
- if(tt->valueint>=1&&tt->valueint<=10)
- {
- #if LAMP_EN == 0
- device_status.sunBar = tt->valueint;
- ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); //光控定时
- rt_kprintf("光控定时tt:%d\n",tt->valueint);
- #else
- rt_kprintf("三光源模式,禁止设置定时时长!!!\n");
- #endif
- }
- else {
- rt_kprintf("光控定时tt设置失败参数错误!!!\n");
- }
- }
- else if (tt->type==cJSON_String) {
- if(atoi(tt->valuestring)>=1&&atoi(tt->valuestring)<=10)
- {
- #if LAMP_EN == 0
- device_status.sunBar = atoi(tt->valuestring);
- ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); //光控定时
- rt_kprintf("光控定时tt:%d\n",atoi(tt->valuestring));
- #else
- rt_kprintf("三光源模式,禁止设置定时时长!!!\n");
- #endif
- }
- else {
- rt_kprintf("光控定时tt设置失败参数错误!!!\n");
- }
- }
- }
- if(htim!=NULL)
- {
- if(htim->type==cJSON_Number)
- {
- if(htim->valueint>=1&&htim->valueint<=30)
- {
- device_status.heattime = htim->valueint;
- ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); //加热时间
- rt_kprintf("加热时长htim:%d\n",htim->valueint);
- }
- else {
- rt_kprintf("加热时长htim设置失败参数错误!!!\n");
- }
- }
- else if (htim->type==cJSON_String) {
- if(atoi(htim->valuestring)>=1&&atoi(htim->valuestring)<=30)
- {
- device_status.heattime = atoi(htim->valuestring);
- ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); //加热时间
- rt_kprintf("加热时长htim:%d\n",atoi(htim->valuestring));
- }
- else {
- rt_kprintf("加热时长htim设置失败参数错误!!!\n");
- }
- }
- }
- if(ts!=NULL)
- {
- if(ts->type==cJSON_Number)
- {
- if(ts->valueint==0||ts->valueint==1)
- {
- #if LAMP_EN == 0
- device_status.timeSwitch = ts->valueint;
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); //工作模式
- // time_t now;
- // time(&now);
- // static struct tm nowstamp;
- // nowstamp =*localtime(&now);
- // nowstamp.tm_hour = nowstamp.tm_hour - 8;
- // static rt_uint32_t nowstamp_num = 0;
- // nowstamp_num = mktime(&nowstamp);
- // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
- rt_kprintf("定时模式ts:%d\n",ts->valueint);
- #else
- rt_kprintf("三光源模式,禁止设置定时!!!\n");
- #endif
- }
- else {
- rt_kprintf("定时模式ts设置失败参数错误!!!\n");
- }
- }
- else if (ts->type==cJSON_String) {
- if(atoi(ts->valuestring)==0||atoi(ts->valuestring)==1)
- {
- #if LAMP_EN == 0
- device_status.timeSwitch = atoi(ts->valuestring);
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); //工作模式
- // time_t now;
- // time(&now);
- // static struct tm nowstamp;
- // nowstamp =*localtime(&now);
- // nowstamp.tm_hour = nowstamp.tm_hour - 8;
- // static rt_uint32_t nowstamp_num = 0;
- // nowstamp_num = mktime(&nowstamp);
- // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
- rt_kprintf("定时模式ts:%d\n",atoi(ts->valuestring));
- #else
- rt_kprintf("三光源模式,禁止设置定时!!!\n");
- #endif
- }
- else {
- rt_kprintf("定时模式ts设置失败参数错误!!!\n");
- }
- }
- }
- if(tpl!=NULL)
- {
- if(tpl->type==cJSON_Number)
- {
- if(tpl->valueint>=-50&&tpl->valueint<=50)
- {
- device_status.tpl = tpl->valueint;
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); //低温保护
- rt_kprintf("低温保护tpl:%d\n",tpl->valueint);
- }
- else {
- rt_kprintf("低温保护tpl设置失败参数错误!!!\n");
- }
- }
- else if (tpl->type==cJSON_String) {
- if(atoi(tpl->valuestring)>=-50&&atoi(tpl->valuestring)<=50)
- {
- device_status.tpl = atoi(tpl->valuestring);
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); //低温保护
- rt_kprintf("低温保护tpl:%d\n",atoi(tpl->valuestring));
- }
- else {
- rt_kprintf("低温保护tpl设置失败参数错误!!!\n");
- }
- }
- }
- if(tph!=NULL)
- {
- if(tph->type==cJSON_Number)
- {
- if(tph->valueint>=50&&tph->valueint<=80)
- {
- device_status.tph = tph->valueint;
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); //高温保护
- rt_kprintf("高温保护tph:%d\n",tph->valueint);
- }
- else {
- rt_kprintf("高温保护tph设置失败参数错误!!!\n");
- }
- }
- else if (tph->type==cJSON_String) {
- if(atoi(tph->valuestring)>=50&&atoi(tph->valuestring)<=80)
- {
- device_status.tph = atoi(tph->valuestring);
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); //高温保护
- rt_kprintf("高温保护tph:%d\n",atoi(tph->valuestring));
- }
- else {
- rt_kprintf("高温保护tph设置失败参数错误!!!\n");
- }
- }
- }
- if(ds!=NULL)
- {
- if(ds->type==cJSON_Number)
- {
- if(ds->valueint>=0&&ds->valueint <=1)
- {
- device_status.ds = ds->valueint;
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- rt_kprintf("开关机状态:%d\n",ds->valueint);
- }
- else {
- rt_kprintf("开关机状态设置失败参数错误!!!\n");
- }
- }
- else if (ds->type==cJSON_String) {
- if(atoi(ds->valuestring)>=0&&atoi(ds->valuestring) <=1)
- {
- device_status.ds = atoi(ds->valuestring);
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- rt_kprintf("开关机状态:%d\n",atoi(ds->valuestring));
- }
- else {
- rt_kprintf("开关机状态设置失败参数错误!!!\n");
- }
- }
- }
- if(vt!=NULL)
- {
- if(atoi(vt->valuestring)>=0&&atoi(vt->valuestring)<=100)
- {
- device_status.version_type = atoi(vt->valuestring);
- ef_set_env_blob("version_type", &device_status.version_type, sizeof(device_status.version_type));
- rt_kprintf("版本类型vt:%d\n",atoi(vt->valuestring));
- }
- else {
- rt_kprintf("版本类型设置失败参数错误!!!\n");
- }
- }
- MA_save();
- rt_thread_delay(2000);
- UpLoad_Data();
- }
- }
- cJSON_Delete(root);
- }
- return;
- }
- void UpLoad_Data(void)
- {
- char *updata = makeJson();
- rt_kprintf("upload data:%d\n%s\n",rt_strlen(updata),updata);
- DTU_Write("%s",updata);
- rt_free(updata);
- list_mem();
- //播放数据上传中语音
- }
|