hmi.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  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-26 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include "hmi.h"
  12. #include <board.h>
  13. #include <control.h>
  14. #include <rtdevice.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "stdarg.h"
  19. #include "string.h"
  20. #include "sht30_collect.h"
  21. #include "beep.h"
  22. #include "easyflash.h"
  23. #include "sound.h"
  24. #include "ymodem.h"
  25. #include <shell.h>
  26. #include "dtu_uart2.h"
  27. #include <fal.h>
  28. #include "selfcheck.h"
  29. #include <user_mb_app.h>
  30. #include "sys_monitor.h"
  31. #define DBG_ENABLE
  32. #define DBG_COLOR
  33. #define DBG_TAG "hmi"
  34. #define DBG_LEVEL DBG_LOG
  35. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  36. #define HMI_THREAD_PRIORITY 5 //线程优先级
  37. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  38. #define HMI_THREAD_TIMESLICE 10 //时间片
  39. #define CONTROL_MOTOR_PRIORITY 18
  40. #define CONTROL_MOTOR_STACK_SIZE 2048
  41. #define CONTROL_MOTOR_TIMESLICE 5
  42. static char msg_pool[512];
  43. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  44. static USHORT sound122[] = {0x736f,0x756e,0x6431,0x3232};
  45. void list_mem(void);
  46. int mb_master_samlpe();
  47. /**/
  48. Device_Status_typedef device_status;
  49. System_stats_t system_status;
  50. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  51. time_t time(time_t *t);
  52. /* 串口接收消息结构*/
  53. struct rx_msg
  54. {
  55. rt_device_t dev;
  56. rt_size_t size;
  57. };
  58. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  59. ////
  60. static rt_thread_t hmi1 = RT_NULL;
  61. static rt_thread_t ctr_motor = RT_NULL;
  62. rt_device_t serial; /*串口设备句柄*/
  63. rt_timer_t timer_uart2 = RT_NULL;
  64. static upgrade_state_t current_state = STATE_IDLE;
  65. uint32_t total_file_size = 0;
  66. static uint32_t bytes_received = 0;
  67. static uint8_t recv_buffer[RECV_BUFFER_SIZE];
  68. static uint32_t recv_write_pos = 0;
  69. /* 消息队列控制块 */
  70. static struct rt_messagequeue rx_mq;//接收数据消息队列
  71. static void HMI_Decode(void);
  72. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  73. static rt_bool_t uart2_timeout_flag = RT_FALSE;
  74. #ifdef RT_USING_SERIAL_V2
  75. static int HMI_uart4_init(void)
  76. {
  77. serial = rt_device_find(UART_NAME);
  78. /*修改串口配置参数*/
  79. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  80. config.data_bits = DATA_BITS_8; //数据位8
  81. config.stop_bits = STOP_BITS_1; //停止位1
  82. #ifdef RT_USING_SERIAL_V2
  83. config.rx_bufsz = 512;
  84. config.tx_bufsz = 1024;
  85. #else
  86. config.bufsz = 512; //缓冲区128字节
  87. #endif
  88. config.parity = PARITY_NONE; //无奇偶校验
  89. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  90. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  91. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  92. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  93. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  94. #ifdef RT_USING_SERIAL_V2
  95. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  96. #else
  97. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  98. #endif
  99. if(uart_res!=RT_EOK)
  100. {
  101. LOG_E("%s打开失败!",UART_NAME);
  102. }else{
  103. LOG_I("%s Open Success!",UART_NAME);
  104. }
  105. return RT_EOK;
  106. }
  107. #endif
  108. /*串口屏写数据*/
  109. void HMI_WRITE(char *fmt, ...)
  110. {
  111. va_list ap;
  112. //末尾需要拼接的3个ff
  113. static const char hmi_hex[3]={0xff,0xff,0xff};
  114. static char str[MAX_SIZE];
  115. //memset(str,'\0',sizeof(str));
  116. rt_size_t str_length;
  117. va_start(ap,fmt);
  118. // 预留3字节空间给 hmi_hex
  119. str_length = rt_vsnprintf(str, sizeof(str) - 3, fmt, ap);
  120. va_end(ap);
  121. if (str_length > MAX_SIZE - 3)
  122. str_length = MAX_SIZE - 3;
  123. // 追加三个 0xff
  124. memcpy(str + str_length, hmi_hex, sizeof(hmi_hex));
  125. rt_device_write(serial, 0, str, str_length + sizeof(hmi_hex));
  126. rt_thread_mdelay(1); //2025-0308
  127. system_status.uart4_tx_bytes+=str_length + sizeof(hmi_hex);
  128. }
  129. /* 接收数据回调函数 */
  130. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  131. {
  132. struct rx_msg msg;
  133. rt_err_t result;
  134. msg.dev = dev;
  135. msg.size = size;
  136. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  137. if ( result == -RT_EFULL)
  138. {
  139. /* 消息队列满 */
  140. LOG_E("hmi message queue full!\n");
  141. rt_mq_control(&rx_mq, RT_IPC_CMD_RESET, RT_NULL); // 清空队列
  142. }
  143. return result;
  144. }
  145. // 预计算转换表
  146. static const int8_t hex_to_byte[256] = {
  147. [0 ... 255] = -1,
  148. ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
  149. ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9,
  150. ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
  151. ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15
  152. };
  153. /**
  154. * @brief 快速HEX字符串转二进制
  155. * @param hex HEX字符串
  156. * @param bin 输出缓冲区
  157. * @param max_len 最大输出长度
  158. * @return 转换的字节数,<0表示错误
  159. */
  160. static inline int fast_hex_to_bin(const char* hex, uint8_t* bin, int max_len)
  161. {
  162. if (!hex || !bin || max_len <= 0) {
  163. return -1;
  164. }
  165. int hex_len = strlen(hex);
  166. if (hex_len % 2 != 0) {
  167. return -1; // HEX长度必须是偶数
  168. }
  169. int bin_len = hex_len / 2;
  170. if (bin_len > max_len) {
  171. return -1; // 输出缓冲区太小
  172. }
  173. for (int i = 0; i < bin_len; i++) {
  174. int high = hex_to_byte[(unsigned char)hex[i * 2]];
  175. int low = hex_to_byte[(unsigned char)hex[i * 2 + 1]];
  176. if (high < 0 || low < 0) {
  177. return -1; // 无效HEX字符
  178. }
  179. bin[i] = (high << 4) | low;
  180. }
  181. return bin_len;
  182. }
  183. // 发送原始数据到HMI
  184. static int send_raw_to_hmi(const uint8_t* data, uint32_t size)
  185. {
  186. if (!serial || !data || size == 0) return -1;
  187. rt_size_t sent = rt_device_write(serial, 0, data, size);
  188. return (sent == size) ? 0 : -1;
  189. }
  190. // 最简洁版本 - 每4096字节直接清空
  191. void process_hex_payload(const char* hex_str, int req_id) {
  192. if (!hex_str) return;
  193. current_state = STATE_RECEIVING;
  194. // 转换HEX
  195. uint8_t temp[JSON_CHUNK_SIZE];
  196. int data_len = fast_hex_to_bin(hex_str, temp, JSON_CHUNK_SIZE);
  197. if (data_len <= 0) return;
  198. // 添加到缓冲区
  199. if (recv_write_pos + data_len > sizeof(recv_buffer)) {
  200. LOG_E("缓冲区溢出");
  201. return;
  202. }
  203. memcpy(recv_buffer + recv_write_pos, temp, data_len);
  204. recv_write_pos += data_len;
  205. bytes_received += data_len;
  206. LOG_I("收到 %d 字节,缓冲: %u/%u",
  207. data_len, recv_write_pos, sizeof(recv_buffer));
  208. // 每4096字节发送一次
  209. if (recv_write_pos >= 4096) {
  210. LOG_I("发送 4096 字节到HMI...");
  211. if (send_raw_to_hmi(recv_buffer, 4096) == 0) {
  212. LOG_I("发送成功");
  213. // ✅ 直接清空缓冲区
  214. recv_write_pos = 0;
  215. } else {
  216. LOG_E("发送失败");
  217. }
  218. return;
  219. }
  220. // 检查完成
  221. if (bytes_received >= total_file_size && total_file_size > 0) {
  222. if (recv_write_pos > 0) {
  223. LOG_I("发送最后 %u 字节", recv_write_pos);
  224. send_raw_to_hmi(recv_buffer, recv_write_pos);
  225. recv_write_pos = 0; // 清空
  226. }
  227. current_state = STATE_COMPLETED;
  228. LOG_I("升级完成!");
  229. return;
  230. }
  231. // 发送进度
  232. int progress = total_file_size > 0 ?
  233. (bytes_received * 100 / total_file_size) : 0;
  234. char progress_json[256];
  235. rt_snprintf(progress_json, sizeof(progress_json),
  236. "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
  237. "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
  238. bytes_received, total_file_size, progress, req_id);
  239. DTU_Write(progress_json);
  240. }
  241. /*入口函数*/
  242. static void Hmi_thread_entry(void *parameter)
  243. {
  244. struct rx_msg msg;
  245. rt_err_t result;
  246. rt_uint32_t rx_length;
  247. if(device_status.dtype==3){
  248. device_status.collt_box_sta = 0;
  249. device_status.run_sta = 0;
  250. }
  251. device_status.upan_sta = 1;
  252. device_status.camsta = 1;
  253. while(1)
  254. {
  255. rt_memset(&msg, 0, sizeof(msg));
  256. /* 从消息队列中读取消息*/
  257. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  258. if (result == RT_EOK)
  259. {
  260. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  261. system_status.uart4_rx_bytes+=rx_length;
  262. rx_buffer[rx_length] = '\0';
  263. LOG_I("%s",rx_buffer);
  264. //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
  265. if(rx_length==1&&rx_buffer[rx_length-1]==0x05){
  266. LOG_W("屏幕响应信号!");
  267. if(current_state==STATE_COMPLETED) return;
  268. //RTU可以发送升级文件了
  269. char* upgrade_hmi = makeJsonRpc2_0("upgradehmi",NULL,1);
  270. if(upgrade_hmi){
  271. DTU_Write(upgrade_hmi);
  272. free(upgrade_hmi);
  273. }
  274. }else {
  275. HMI_Decode();
  276. }
  277. }
  278. /*********************************************************************************************************************/
  279. //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  280. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  281. }
  282. }
  283. /*******************************************************************************************/
  284. void Start_timer_Uart2(void)
  285. {
  286. device_status.mock_uart2_timer_flag = 1;
  287. }
  288. void Stop_timer_Uart2(void)
  289. {
  290. device_status.mock_uart2_timer_flag = 0;
  291. }
  292. static void refresh_hmi_Component(struct tm tm)
  293. {
  294. char *readinfo = makeJson_ReadInfo();
  295. if(device_status.refresh_num==5)
  296. {
  297. device_status.refresh_num = 15;
  298. DTU_Write("%s",readinfo);
  299. //list_mem();
  300. device_status.hmi_fresh = 1;
  301. rt_thread_delay(100);
  302. }
  303. //在解析getinfo中清除超时
  304. if(device_status.refresh_timeout==30)
  305. {
  306. rt_sprintf(device_status.sim,"1");
  307. rt_sprintf(device_status.net,"1");
  308. rt_sprintf(device_status.csq,"0");
  309. device_status.upan_sta = 1;
  310. device_status.camsta = 1;
  311. device_status.link_sta=0;
  312. device_status.refresh_timeout=0;
  313. }
  314. rt_free(readinfo);
  315. device_status.refresh_num--;
  316. device_status.refresh_timeout++;
  317. if(device_status.version_type!=6)
  318. {
  319. HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid);
  320. if(strstr(device_status.mqttcfg1,"8.136.98.49")!=NULL)
  321. {
  322. HMI_WRITE("uploadSet.plt.val=%d",RESET);
  323. }
  324. else if (strstr(device_status.mqttcfg1,"120.27.222.26")!=NULL) {
  325. HMI_WRITE("uploadSet.plt.val=%d",SET);
  326. }
  327. //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1);
  328. HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg1);
  329. //更新固件版本号
  330. HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver);
  331. //更新固件编译日期
  332. HMI_WRITE("debug.t4.txt=\"%s\"",device_status.driver_compile);
  333. //
  334. HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl);
  335. HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile);
  336. //更新dtu/rtu版本
  337. HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g);
  338. //更新时间
  339. HMI_WRITE("menu.rtc.txt=\"%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);
  340. HMI_WRITE("workSta.rtc.txt=\"%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);
  341. HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour);
  342. HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min);
  343. HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec);
  344. //更新电压
  345. HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
  346. //更新电流
  347. HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
  348. //更新温湿度数据
  349. HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity));
  350. HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture));
  351. //更新电池状态
  352. HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status);
  353. //更新上仓门状态
  354. HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status);
  355. //更新下仓门状态
  356. HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status);
  357. //更新工作状态
  358. HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status);
  359. //更新温控状态
  360. HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status);
  361. //更新雨控状态
  362. HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status);
  363. //更新光控状态
  364. HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status);
  365. //更新收集时间
  366. HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time);
  367. //更新加热时间
  368. HMI_WRITE("workSta.heatTime.val=%d",device_status.heattime);
  369. HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime);
  370. //
  371. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  372. //更新加热时间和温度
  373. HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem);
  374. HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue);
  375. //更新加热状态
  376. HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
  377. //更新密码
  378. HMI_WRITE("inputPasswd.pwdData.txt=\"8888\"");
  379. //更新工作进度
  380. //HMI_WRITE("menu.workProgress.val=%d",);
  381. //更新定时模式
  382. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  383. //更新光控定时时长
  384. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  385. //更新时控开始
  386. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  387. //更新时控结束
  388. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  389. //SIM卡状态
  390. HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim));
  391. //ID
  392. HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
  393. HMI_WRITE("menu.deviceId.txt=\"%s\"",device_status.deviceId);
  394. //HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
  395. //数据上传间隔
  396. HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim);
  397. //语音音量
  398. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  399. //更新熄屏时间
  400. if(device_status.thsp<60) device_status.thsp = device_status.thsp * 60;
  401. HMI_WRITE("thsp=%d",device_status.thsp);
  402. if(strstr(device_status.link_ip,".")!=RT_NULL){
  403. HMI_WRITE("menu.t0.txt=\"%s\"",device_status.link_ip);
  404. }
  405. #if MATCH_DEVICE == 1
  406. HMI_WRITE("menu.mode.val=%d",device_status.work_mode);
  407. HMI_WRITE("menu.upan.val=%d",device_status.upan_sta);
  408. HMI_WRITE("menu.alarm_tg.txt=\"%s\"",device_status.alarm_tg);
  409. //更新有线连接状态
  410. HMI_WRITE("menu.va_net.val=%d",0);
  411. #else
  412. HMI_WRITE("menu.va_net.val=%d",device_status.link_sta);
  413. #endif
  414. }
  415. HMI_WRITE("menu.cam_sta.val=%d",device_status.camsta);
  416. if(atoi(device_status.gps)==1){
  417. HMI_WRITE("menu.gps_state.txt=\"定位成功:(%s)%d/%d\"",device_status.gps_state,device_status.gps_inuse,device_status.gps_view);
  418. }else if(atoi(device_status.gps)==2){
  419. HMI_WRITE("menu.gps_state.txt=\"定位成功:(基站)\"");
  420. }
  421. //更新联网状态
  422. if(atoi(device_status.net)==1)
  423. {
  424. HMI_WRITE("menu.net.val=1");
  425. HMI_WRITE("workSta.net.val=1");
  426. }
  427. else {
  428. #if MATCH_DEVICE == 0
  429. HMI_WRITE("workSta.net.val=0");
  430. HMI_WRITE("menu.net.val=0");
  431. //更新CSQ
  432. HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
  433. HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
  434. HMI_WRITE("workSta.csq_value.txt=\"%s\"",device_status.csq);
  435. #else
  436. HMI_WRITE("workSta.net.val=1");
  437. HMI_WRITE("menu.net.val=1");
  438. HMI_WRITE("menu.va_csq.val=0");
  439. #endif
  440. }
  441. if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
  442. {
  443. HMI_WRITE("menu.net.val=1");
  444. HMI_WRITE("workSta.net.val=2");
  445. }
  446. /**********************************************************************/
  447. }
  448. /***************************** 解析从屏幕收到的数据 begin **********************************/
  449. static void HMI_Decode(void)
  450. {
  451. time_t now = time(RT_NULL);
  452. static struct tm tm;
  453. gmtime_r(&now, &tm);
  454. static char *json_temp;
  455. static char *var;
  456. if(device_status.refresh_num==0) device_status.refresh_num = 10;
  457. if(strstr(rx_buffer,"getFullData") != NULL){
  458. refresh_hmi_Component(tm);
  459. }
  460. else if (strstr(rx_buffer,"getClockData")!=NULL) {
  461. //更新定时模式
  462. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  463. //更新光控定时时长
  464. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  465. //更新时控开始
  466. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  467. //更新时控结束
  468. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  469. }
  470. else if (strstr(rx_buffer,"button")!=NULL) {
  471. LOG_D("button!");
  472. DTU_Write("button");
  473. }
  474. //拍照
  475. else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) {
  476. #if MATCH_DEVICE == 1
  477. ctr_led(ON);
  478. //发送拍照指令
  479. char *takephotojson = makeJson_takephoto();
  480. DTU_Write("%s",takephotojson);
  481. rt_free(takephotojson);
  482. HMI_WRITE("sysTest.switchPhoto.val=0");
  483. #else
  484. device_status.photo_ws = 1;
  485. #endif
  486. }
  487. else if (strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
  488. #if MATCH_DEVICE == 1
  489. ctr_led(ON);
  490. //发送拍照指令
  491. char *takephotojson = makeJson_takephoto();
  492. DTU_Write("%s",takephotojson);
  493. rt_free(takephotojson);
  494. HMI_WRITE("sysTest.switchPhoto.val=0");
  495. #else
  496. device_status.photo_ws = 1;
  497. #endif
  498. }
  499. //打开补光
  500. else if (strstr(rx_buffer,"putTest.switchLed=1")!=NULL) {
  501. LOG_I("打开补光!!");
  502. ctr_led(ON);
  503. }
  504. //关闭补光
  505. else if (strstr(rx_buffer,"putTest.switchLed=0")!=NULL) {
  506. LOG_I("关闭补光!!!");
  507. ctr_led(OFF);
  508. }
  509. //关闭加热
  510. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  511. device_status.heat_status = 0;
  512. ctr_Heat(OFF);
  513. LOG_I("Close Heat!");
  514. }
  515. //打开加热
  516. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  517. device_status.heat_status = 1;
  518. ctr_Heat(ON);
  519. LOG_I("Open Heat!");
  520. }
  521. //打开制冷
  522. else if(strstr(rx_buffer,"putTest.switchCold=1")!=NULL) {
  523. ctr_Cold(ON);
  524. LOG_I("Open Cold!");
  525. }
  526. //关闭制冷
  527. else if(strstr(rx_buffer,"putTest.switchCold=0")!=NULL) {
  528. ctr_Cold(OFF);
  529. LOG_I("Close Cold!");
  530. }
  531. //打开采集
  532. else if(strstr(rx_buffer,"putTest.switchCollect=1")!=NULL){
  533. ctr_Collect_Fan(ON);
  534. LOG_I("采集风机已打开!");
  535. }
  536. //关闭采集
  537. else if(strstr(rx_buffer,"putTest.switchCollect=0")!=NULL){
  538. ctr_Collect_Fan(OFF);
  539. LOG_I("采集风机已关闭!");
  540. }
  541. //打开散热
  542. else if(strstr(rx_buffer,"putTest.switchCool=1")!=NULL){
  543. ctr_Cool_Fan(ON);
  544. LOG_I("散热风机已打开!");
  545. }
  546. //关闭散热
  547. else if(strstr(rx_buffer,"putTest.switchCool=0")!=NULL){
  548. ctr_Cool_Fan(OFF);
  549. LOG_I("散热风机已关闭!");
  550. }
  551. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  552. {
  553. LOG_I("开始一键测试!!!");
  554. device_status.auto_test_sw=1;
  555. }
  556. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  557. {
  558. LOG_I("停止一键测试!!!");
  559. device_status.auto_test_sw=0;
  560. }
  561. else if (strstr(rx_buffer,"putTest.switchCc=1")!=NULL)
  562. {
  563. LOG_I("推杆打开测试!!!");
  564. ctr_Cold(ON);
  565. }
  566. else if (strstr(rx_buffer,"putTest.switchCc=0")!=NULL)
  567. {
  568. LOG_I("推杆关闭测试!!!");
  569. ctr_Cold(OFF);
  570. }
  571. else if (strstr(rx_buffer,"putTest.shakeCc=1")!=NULL)
  572. {
  573. LOG_I("震动电机打开测试!!!");
  574. ctr_Collect_Fan(ON);
  575. }
  576. else if (strstr(rx_buffer,"putTest.shakeCc=0")!=NULL)
  577. {
  578. LOG_I("震动电机关闭测试!!!");
  579. ctr_Collect_Fan(OFF);
  580. }
  581. else if (strstr(rx_buffer,"turnzp")!=NULL) {
  582. LOG_W("转仓!");
  583. device_status.clear_status=2;
  584. }
  585. //清扫
  586. else if (strstr(rx_buffer,"clear")!=NULL) {
  587. LOG_W("清扫!");
  588. device_status.clear_status=1;
  589. }
  590. //震动
  591. else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
  592. LOG_I("震动!");
  593. if(device_status.version_type==6)
  594. {
  595. Motor_shake(ON, device_status.shake_sec);
  596. }
  597. else{
  598. ctr_shake(ON,device_status.shake_sec);
  599. ctr_router(ON);
  600. }
  601. }
  602. //打开补光
  603. else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
  604. rt_kprintf("\n打开补光!!!\n");
  605. ctr_led(ON);
  606. }
  607. //关闭补光
  608. else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
  609. rt_kprintf("\n关闭补光!!!\n");
  610. ctr_led(OFF);
  611. }
  612. //打开灯管
  613. else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
  614. {
  615. device_status.test_flag=1;
  616. rt_kprintf("Open Lamp!\n");
  617. ctr_lamp(ON,1);
  618. #if LAMP_EN == 1
  619. ctr_lamp(ON,2);
  620. ctr_lamp(ON,3);
  621. #endif
  622. device_status.rain_status = 0;
  623. }
  624. //关闭灯管
  625. else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
  626. {
  627. device_status.test_flag=0;
  628. rt_kprintf("Close Lamp1!\n");
  629. ctr_lamp(OFF,1);
  630. #if LAMP_EN == 1
  631. ctr_lamp(OFF,2);
  632. ctr_lamp(OFF,3);
  633. #endif
  634. }
  635. //关闭加热
  636. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  637. device_status.heat_status = 0;
  638. ctr_Heat(OFF);
  639. rt_kprintf("Close Heat!\n");
  640. }
  641. //打开加热
  642. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  643. device_status.heat_status = 1;
  644. ctr_Heat(ON);
  645. rt_kprintf("Open Heat!\n");
  646. }
  647. //打开上仓门
  648. else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
  649. if(device_status.dtype==7)
  650. {
  651. ctr_lamp(ON, 1);
  652. ctr_Motor_Driver(1, 0,1000000,500);
  653. LOG_I("开始步进上行!");
  654. }
  655. else
  656. {
  657. rt_kprintf("Open UP!\n");
  658. ctr_cangmen_up(OFF);
  659. device_status.ctr_up_motor = 1;
  660. }
  661. }
  662. //关闭上仓门
  663. else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
  664. if(device_status.dtype==7)
  665. {
  666. LOG_I("停止步进上行!");
  667. ctr_lamp(OFF, 1);
  668. ctr_Motor_Driver(0, 0,0,0);
  669. }
  670. else
  671. {
  672. rt_kprintf("CLOSE UP!\n");
  673. ctr_cangmen_up(OFF);
  674. device_status.ctr_up_motor = 2;
  675. }
  676. }
  677. //打开下仓门
  678. else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
  679. if(device_status.dtype==7)
  680. {
  681. LOG_I("开始步进下行!");
  682. ctr_lamp(ON, 1);
  683. ctr_Motor_Driver(1, 1,1000000,500);
  684. }
  685. else
  686. {
  687. ctr_cangmen_down(OFF);
  688. device_status.ctr_down_motor = 1;
  689. rt_kprintf("Open down!\n");
  690. }
  691. }
  692. //关闭下仓门
  693. else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
  694. if(device_status.dtype==7)
  695. {
  696. ctr_lamp(OFF, 1);
  697. ctr_Motor_Driver(0, 0,0,0);
  698. rt_kprintf("停止步进下行!\n");
  699. }
  700. else
  701. {
  702. ctr_cangmen_down(OFF);
  703. device_status.ctr_down_motor = 2;
  704. rt_kprintf("CLOSE down!\n");
  705. }
  706. }
  707. else if(strstr(rx_buffer,"putTest.Testpaichong")!=NULL){
  708. if(device_status.collt_box_sta==0){
  709. device_status.collt_box_sta = 111;
  710. }else{
  711. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  712. }
  713. LOG_I("排虫测试!");
  714. }
  715. //排虫
  716. else if(strstr(rx_buffer,"putTest.paichong")!=NULL){
  717. if(device_status.run_sta == 0){
  718. device_status.run_sta = 111;
  719. }else{
  720. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  721. }
  722. LOG_I("开始排虫!");
  723. device_status.handle_bugger = 0;
  724. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  725. }
  726. else if(strstr(rx_buffer,"putTest.shouchong")!=NULL){
  727. if(device_status.run_sta == 0){
  728. device_status.run_sta = 112;
  729. }else{
  730. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  731. }
  732. LOG_I("开始收虫!");
  733. device_status.handle_bugger = 1;
  734. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  735. }
  736. //设置设备时间
  737. else if (strstr(rx_buffer,"stamp=")!=NULL)
  738. {
  739. var = strstr(rx_buffer,"=");
  740. var ++;
  741. LOG_I("设置时间:%s",var);
  742. static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
  743. strncpy(year_s, var, 4);
  744. strncpy(month_s,var+4,2);
  745. strncpy(day_s,var+6,2);
  746. strncpy(hour_s,var+8,2);
  747. strncpy(min_s,var+10,2);
  748. strncpy(sec_s,var+12,2);
  749. if(atoi(year_s)>=2024&&atoi(year_s)<3000&&atoi(month_s)>0&&atoi(month_s)<13&&atoi(day_s)>0&&atoi(day_s)<32)
  750. {
  751. if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
  752. {
  753. LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
  754. if(atoi(hour_s)>0&&atoi(hour_s)<=24&&atoi(min_s)>=0&&atoi(min_s)<=59&&atoi(sec_s)>=0&&atoi(sec_s)<=59)
  755. {
  756. LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
  757. set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
  758. MA_save();
  759. }
  760. //上传最新数据
  761. device_status.num_updata = 2; //2秒后上报数据更新RTU的时间
  762. }
  763. else {
  764. LOG_E("set_date failed!");
  765. }
  766. }
  767. }
  768. //设置闹钟时间
  769. else if (strstr(rx_buffer,"ALARM=")!=NULL)
  770. {
  771. char *var = strstr(rx_buffer,"=");
  772. if (var == NULL) {
  773. return;
  774. }
  775. var ++;
  776. // ===================== 修复:去掉末尾的 \r 或 换行 =====================
  777. // 遍历字符串,把 \r \n 全部替换成结束符
  778. for (int i = 0; var[i] != '\0'; i++) {
  779. if (var[i] == '\r' || var[i] == '\n') {
  780. var[i] = '\0';
  781. break;
  782. }
  783. }
  784. if(var[6] != '\r') {
  785. var[6] = '\0';
  786. }
  787. // 现在 var 只剩下干净的 6 位数字了
  788. int len = strlen(var);
  789. int is_valid = 1;
  790. // 1. 必须是 6 个字符(已经去掉\r)
  791. if (len != 6) {
  792. is_valid = 0;
  793. }
  794. // 2. 必须全部是数字 0-9
  795. else {
  796. for (int i = 0; i < 6; i++) {
  797. if (!(var[i] >= '0' && var[i] <= '9')) {
  798. is_valid = 0;
  799. break;
  800. }
  801. }
  802. }
  803. // 校验不通过:打印报错,不赋值
  804. if (!is_valid) {
  805. LOG_E("闹钟格式错误!必须是 6 位数字时间(如 150000),当前值:%s", var);
  806. return;
  807. }
  808. // ======================================================================
  809. // 格式正确才赋值
  810. LOG_I("设置闹钟时间:%s",var);
  811. rt_sprintf(device_status.alarm_tg, "%s", var);
  812. ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
  813. }
  814. //设备编码设置
  815. else if (strstr(rx_buffer,"deviceId=")!=NULL)
  816. {
  817. char value_str[20];
  818. memset(value_str,0,sizeof(value_str));
  819. LOG_I("设备编码 长度:%d",strlen(rx_buffer));
  820. if(strlen(rx_buffer)!=19){
  821. LOG_E("编码长度错误!");
  822. return;
  823. }
  824. var = strstr(rx_buffer,"=");
  825. var ++;
  826. strncpy(value_str,var,8); //设备编码为16位
  827. if(strstr(value_str,"00000000")!=RT_NULL){
  828. char *config_reset = makeJson_Config_Reset();
  829. //发送json到RTU
  830. DTU_Write("%s",config_reset);
  831. rt_free(config_reset);
  832. //恢复RTU的默认配置参数
  833. }else{
  834. rt_sprintf(device_status.deviceId,"10300101%s",value_str); //设备大类编码103-虫情 设备小类编码 001 生产商代码01
  835. ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
  836. LOG_I("解析到设备编码:%s",device_status.deviceId);
  837. char *device_id = makeJson_Config_BDH(device_status.deviceId);
  838. //发送json到RTU
  839. DTU_Write("%s",device_id);
  840. rt_free(device_id);
  841. }
  842. }
  843. //定时设置
  844. else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
  845. char value_str[10];
  846. memset(value_str,0,sizeof(value_str));
  847. if(device_status.dtype==7)
  848. {
  849. var = strstr(rx_buffer,"=");
  850. var ++;
  851. strncpy(value_str,var,2);
  852. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  853. {
  854. device_status.collecting_sh1 = atoi(value_str);
  855. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  856. }
  857. else {
  858. LOG_E("Set collecting_sh1 Failed!");
  859. }
  860. memset(value_str,0,sizeof(value_str));
  861. strncpy(value_str,var+2,2);
  862. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  863. {
  864. device_status.collecting_eh1 = atoi(value_str);
  865. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  866. }
  867. else {
  868. LOG_E("Set collecting_eh1 Failed!");
  869. }
  870. memset(value_str,0,sizeof(value_str));
  871. strncpy(value_str,var+4,2);
  872. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  873. {
  874. device_status.collecting_sh2 = atoi(value_str);
  875. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  876. }
  877. else {
  878. LOG_E("Set collecting_sh2 Failed!");
  879. }
  880. memset(value_str,0,sizeof(value_str));
  881. strncpy(value_str,var+6,2);
  882. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  883. {
  884. device_status.collecting_eh2 = atoi(value_str);
  885. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  886. }
  887. else {
  888. LOG_E("Set collecting_eh2 Failed!");
  889. }
  890. memset(value_str,0,sizeof(value_str));
  891. strncpy(value_str,var+8,2);
  892. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  893. {
  894. device_status.shake_sec = atoi(value_str);
  895. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  896. }
  897. else {
  898. LOG_E("Set shake_sec Failed!");
  899. }
  900. memset(value_str,0,sizeof(value_str));
  901. strncpy(value_str,var+10,2);
  902. if(atoi(value_str)>0&&atoi(value_str)<=24)
  903. {
  904. device_status.cul_time = atoi(value_str);
  905. ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time));
  906. }
  907. else {
  908. LOG_E("Set cul_time Failed!");
  909. }
  910. MA_save();
  911. LOG_I("参数已保存:%d-%d-%d-%d-%d-%d",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2,device_status.shake_sec,device_status.cul_time);
  912. return;
  913. }
  914. LOG_I("时间设置!");
  915. var=strstr(rx_buffer,"=");
  916. var++;
  917. strncpy(value_str,var,2);
  918. if(atoi(value_str) > 0 && atoi(value_str) <60)
  919. {
  920. if(device_status.version_type!=6)
  921. {
  922. device_status.sunBar=atoi(value_str);
  923. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  924. }
  925. else {
  926. device_status.shake_sec = atoi(value_str);
  927. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  928. }
  929. }
  930. else {
  931. LOG_E("参数设置异常(光控时长):%d!!!",atoi(value_str));
  932. }
  933. memset(value_str,0,sizeof(value_str));
  934. //时控开始时间
  935. strncpy(value_str,var+2,2);
  936. if(atoi(value_str) >= 0 && atoi(value_str) < 24)
  937. {
  938. #if 1
  939. device_status.startBar=atoi(value_str);
  940. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  941. #endif
  942. }
  943. else {
  944. LOG_E("参数设置异常(开始):%d!!!",atoi(value_str));
  945. }
  946. memset(value_str,0,sizeof(value_str));
  947. //
  948. strncpy(value_str,var+4,2);
  949. if(atoi(value_str) >= 0&& atoi(value_str) < 24)
  950. {
  951. #if 1
  952. device_status.stopBar=atoi(value_str);
  953. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  954. #endif
  955. }
  956. else {
  957. LOG_E("参数设置异常(结束):%d!!!",atoi(value_str));
  958. }
  959. memset(value_str,0,sizeof(value_str));
  960. //
  961. strncpy(value_str,var+6,2);
  962. if(atoi(value_str) >= 0 && atoi(value_str) <= 1)
  963. {
  964. device_status.timeSwitch=atoi(value_str);
  965. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  966. }
  967. else {
  968. if(device_status.version_type==6)
  969. {
  970. device_status.timeSwitch=1;
  971. device_status.dattim=atoi(value_str);
  972. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  973. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  974. }
  975. else {
  976. LOG_E("参数设置异常(模式):%d!!!",atoi(value_str));
  977. }
  978. }
  979. //2022/06/23,add:
  980. rt_sprintf(device_status.json_w_clock,"屏幕操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)操作时间:%s",device_status.startBar,device_status.stopBar,device_status.timeSwitch,device_status.stamp);
  981. json_temp = makeJson_Warn(device_status.json_w_clock, "修改工作时间");
  982. ef_set_env_blob("json_w_clock", &device_status.json_w_clock, sizeof(device_status.json_w_clock)); //将修改屏幕参数操作记录保存到Flash中
  983. if(atoi(device_status.net)==0)
  984. DTU_Write("%s",json_temp);
  985. rt_free(json_temp);
  986. rt_thread_delay(2000);
  987. MA_save();
  988. rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
  989. }
  990. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  991. static char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
  992. static char value_temp[5];
  993. LOG_I("系统设置!");
  994. if(device_status.dtype==7)
  995. {
  996. LOG_I("系统设置,孢子仪!");
  997. var = strstr(rx_buffer,"=");
  998. var ++;
  999. memset(value_temp,0,sizeof(value_temp));
  1000. strncpy(value_temp,var,2);
  1001. if(atoi(value_temp)==0 || atoi(value_temp) == 1)
  1002. {
  1003. device_status.cold_sw = atoi(value_temp); //制冷开关
  1004. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  1005. }
  1006. else {
  1007. LOG_E("制冷开关参数设置错误!");
  1008. }
  1009. memset(value_temp,0,sizeof(value_temp));
  1010. strncpy(value_temp,var+2,2);
  1011. if(atoi(value_temp)>=0 && atoi(value_temp) <= 7)
  1012. {
  1013. device_status.vol = atoi(value_temp); //音量
  1014. }
  1015. else {
  1016. LOG_E("音量参数设置错误!");
  1017. }
  1018. memset(value_temp,0,sizeof(value_temp));
  1019. strncpy(value_temp,var+4,2);
  1020. if(atoi(value_temp)>=10 && atoi(value_temp) <= 50)
  1021. {
  1022. device_status.heatvalue = atoi(value_temp); //设定的培养温度
  1023. ef_set_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1024. }
  1025. else {
  1026. LOG_E("培养温度参数设置失败!");
  1027. }
  1028. memset(value_temp,0,sizeof(value_temp));
  1029. strncpy(value_temp,var+6,2);
  1030. if(atoi(value_temp)>=1 && atoi(value_temp) <= 60)
  1031. {
  1032. device_status.dattim = atoi(value_temp); //设定的数据上传间隔,单位:分钟
  1033. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  1034. }
  1035. else {
  1036. LOG_E("数据上传间隔参数设置失败!");
  1037. }
  1038. memset(value_temp,0,sizeof(value_temp));
  1039. strncpy(value_temp,var+8,2);
  1040. if(atoi(value_temp)>=1 && atoi(value_temp) <= 30)
  1041. {
  1042. device_status.thsp = atoi(value_temp); //设定的息屏间隔,单位:分钟
  1043. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1044. }
  1045. else {
  1046. LOG_E("数据息屏间隔参数设置失败!");
  1047. }
  1048. LOG_I("制冷开关:%d",device_status.cold_sw);
  1049. LOG_I("系统设置已保存:\n自动熄屏-%d\n音量-%d\n培养温度-%d\n数据上传间隔-%d",device_status.thsp,device_status.vol,device_status.heatvalue,device_status.dattim);
  1050. MA_save();
  1051. return;
  1052. }
  1053. var = strstr(rx_buffer,"=");
  1054. var ++;
  1055. strncpy(clt_time_str,var,2);
  1056. if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
  1057. {
  1058. device_status.clt_time = atoi(clt_time_str); //收集时间
  1059. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1060. }
  1061. strncpy(heattime_str,var+2,2);
  1062. if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
  1063. {
  1064. device_status.heattime = atoi(heattime_str); //加热时间
  1065. LOG_I("设置加热时间:%d",atoi(heattime_str));
  1066. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1067. }
  1068. strncpy(heatvale_str,var+4,3);
  1069. if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
  1070. {
  1071. device_status.heatvalue=atoi(heatvale_str);
  1072. LOG_I("设置加热温度:%d",device_status.heatvalue);
  1073. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1074. }
  1075. strncpy(vol_str,var+7,3);
  1076. if(atoi(vol_str)>0&&atoi(vol_str)<8)
  1077. {
  1078. device_status.vol = atoi(vol_str); //音量
  1079. LOG_I("设置音量:%d",device_status.vol);
  1080. //DTU_Write("volume=");
  1081. }
  1082. strncpy(thsp_str,var+10,4);
  1083. if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
  1084. {
  1085. device_status.thsp = atoi(thsp_str); //熄屏时间
  1086. LOG_I("设置熄屏时间:%d",device_status.thsp);
  1087. }
  1088. else {
  1089. LOG_E("参数设置失败!!!");
  1090. }
  1091. rt_thread_delay(1000);
  1092. MA_save();
  1093. //rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
  1094. }
  1095. else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
  1096. char rtc_h[2];
  1097. var = strstr(rx_buffer,"=");
  1098. var++;
  1099. strncpy(rtc_h,var,2);
  1100. if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24)
  1101. {
  1102. set_time(atoi(rtc_h), 99, 99);
  1103. }
  1104. }
  1105. else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
  1106. char rtc_m[2];
  1107. var = strstr(rx_buffer,"=");
  1108. var++;
  1109. strncpy(rtc_m,var,2);
  1110. if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24)
  1111. {
  1112. set_time(99, atoi(rtc_m), 99);
  1113. }
  1114. }
  1115. else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
  1116. char rtc_s[2];
  1117. var = strstr(rx_buffer,"=");
  1118. var++;
  1119. strncpy(rtc_s,var,2);
  1120. if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24)
  1121. {
  1122. set_time(99, 99, atoi(rtc_s));
  1123. }
  1124. }
  1125. else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
  1126. {
  1127. static char dattim_str[3];
  1128. rt_kprintf("上传间隔设置!\n");
  1129. var = strstr(rx_buffer,"=");
  1130. var ++;
  1131. strncpy(dattim_str,var,2);
  1132. if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
  1133. {
  1134. device_status.dattim =atoi(dattim_str); //数据上传间隔
  1135. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  1136. MA_save();
  1137. }
  1138. else {
  1139. rt_kprintf("设置失败!!!\n");
  1140. }
  1141. rt_kprintf("数据上传设置:%d\n",device_status.dattim);
  1142. }
  1143. //光控测试
  1144. else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
  1145. {
  1146. rt_kprintf("光控测试!!!");
  1147. ctr_lamp(ON,1);
  1148. device_status.sun_test = 1;
  1149. }
  1150. else if (strstr(rx_buffer,"setshake_sec=")!=NULL)
  1151. {
  1152. static char shakesec_str[4];
  1153. LOG_I("震动时间设置!");
  1154. var = strstr(rx_buffer,"=");
  1155. var ++;
  1156. strncpy(shakesec_str,var,2);
  1157. if(atoi(shakesec_str)>=0&&atoi(shakesec_str)<=10)
  1158. {
  1159. device_status.shake_sec =atoi(shakesec_str); //2026-0608:取消*10
  1160. }
  1161. else {
  1162. LOG_E("设置失败!!!");
  1163. }
  1164. LOG_D("震动时间设置:%d",device_status.shake_sec);
  1165. }
  1166. //恢复出厂设置电池
  1167. else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
  1168. {
  1169. ResetParamDefault_DC();
  1170. rt_thread_delay(1000);
  1171. //2022/06/23,add:
  1172. rt_sprintf(device_status.json_w_reset,"屏幕操作:系统重置:电池供电,操作时间:%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);
  1173. json_temp = makeJson_Warn(device_status.json_w_reset, "系统重置");
  1174. ef_set_env_blob("json_w_reset", &device_status.json_w_reset, sizeof(device_status.json_w_reset)); //将修改屏幕参数操作记录保存到Flash中
  1175. if(atoi(device_status.net)==0)
  1176. DTU_Write("%s",json_temp);
  1177. rt_free(json_temp);
  1178. }
  1179. //恢复出厂设置-交流
  1180. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  1181. {
  1182. ResetParamDefault();
  1183. rt_thread_delay(1000);
  1184. //2022/06/23,add:
  1185. rt_sprintf(device_status.json_w_reset,"屏幕操作:系统重置:交流供电,操作时间:%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);
  1186. json_temp = makeJson_Warn(device_status.json_w_reset, "系统重置");
  1187. ef_set_env_blob("json_w_reset", &device_status.json_w_reset, sizeof(device_status.json_w_reset)); //将修改屏幕参数操作记录保存到Flash中
  1188. if(atoi(device_status.net)==0)
  1189. DTU_Write("%s",json_temp);
  1190. rt_free(json_temp);
  1191. }
  1192. //获取SIM卡信息
  1193. else if(strstr(rx_buffer,"getSim")!=NULL)
  1194. {
  1195. //http://sim.openluat.com/api/wechat/openapidoc
  1196. DTU_Write("getsiminfo");
  1197. }
  1198. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  1199. Debug_log_ON();
  1200. }
  1201. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  1202. Debug_log_OFF();
  1203. }
  1204. //高空
  1205. else if (strstr(rx_buffer,"SwversionV11")!=NULL)
  1206. {
  1207. device_status.dtype = 3;
  1208. if(device_status.version_type != 11)
  1209. {
  1210. device_status.version_type = 11;
  1211. sprintf(device_status.driver, "%s",VERSION_GK);
  1212. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1213. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1214. MA_save();
  1215. }
  1216. else {
  1217. }
  1218. return;
  1219. }
  1220. //1.0
  1221. else if (strstr(rx_buffer,"SwversionV1")!=NULL)
  1222. {
  1223. if(device_status.version_type != 1)
  1224. {
  1225. device_status.dtype = 3;
  1226. device_status.version_type = 1;
  1227. sprintf(device_status.driver, "%s",VERSION_ZP);
  1228. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1229. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1230. MA_save();
  1231. }
  1232. else {
  1233. }
  1234. return;
  1235. }
  1236. //中端款
  1237. else if (strstr(rx_buffer,"SwversionV43")!=NULL) {
  1238. device_status.dtype = 3;
  1239. if(device_status.version_type != 43)
  1240. {
  1241. device_status.dtype = 3;
  1242. device_status.version_type = 43;
  1243. sprintf(device_status.driver, "%s",VERSION_HK);
  1244. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1245. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1246. MA_save();
  1247. }
  1248. else {
  1249. }
  1250. return;
  1251. }
  1252. //低端款
  1253. else if (strstr(rx_buffer,"SwversionV42")!=NULL) {
  1254. device_status.dtype = 3;
  1255. if(device_status.version_type != 42)
  1256. {
  1257. device_status.dtype = 3;
  1258. device_status.version_type = 42;
  1259. sprintf(device_status.driver, "%s",VERSION_HK);
  1260. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1261. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1262. MA_save();
  1263. }
  1264. else {
  1265. }
  1266. return;
  1267. }
  1268. //4.0
  1269. else if (strstr(rx_buffer,"SwversionV41")!=NULL) {
  1270. device_status.dtype = 3;
  1271. if(device_status.version_type != 41)
  1272. {
  1273. device_status.dtype = 3;
  1274. device_status.version_type = 41;
  1275. sprintf(device_status.driver, "%s",VERSION_HK);
  1276. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1277. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1278. MA_save();
  1279. }
  1280. else {
  1281. }
  1282. return;
  1283. }
  1284. //水稻
  1285. else if (strstr(rx_buffer,"SwversionV4")!=NULL) {
  1286. device_status.dtype = 3;
  1287. if(device_status.version_type != 4)
  1288. {
  1289. device_status.version_type = 4;
  1290. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1291. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1292. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1293. MA_save();
  1294. }
  1295. else {
  1296. }
  1297. return;
  1298. }
  1299. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  1300. {
  1301. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  1302. HMI_WRITE("sysTest.heat_txt.txt=\"%s\"",device_status.heat_power);
  1303. // HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
  1304. }
  1305. //立即上传
  1306. else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
  1307. {
  1308. UpLoad_Data();
  1309. }
  1310. else if (strstr(rx_buffer,"checkupdate")!=NULL)
  1311. {
  1312. time_t now = time(RT_NULL);
  1313. static struct tm tm;
  1314. gmtime_r(&now, &tm);
  1315. HMI_WRITE("hardcheck.tip_rtc.txt=\"%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);
  1316. HMI_WRITE("hardcheck.p0.pic=%d",70);
  1317. //RTC时间判断,如果当前年份小于2020,提示需要设置时间
  1318. if(tm.tm_year+1900<2020)
  1319. {
  1320. rt_kprintf("时间未设置:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1321. HMI_WRITE("hardcheck.tip_rtc.txt=%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);
  1322. }
  1323. }
  1324. //进入bootloader模式
  1325. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  1326. device_status.stop_in_bootloader = 1;
  1327. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  1328. rt_hw_cpu_reset();
  1329. }
  1330. //软件重启
  1331. else if (strstr(rx_buffer,"reboot")!=NULL) {
  1332. rt_hw_cpu_reset();
  1333. }
  1334. else if (strstr(rx_buffer,"dtu_reboot")!=NULL) {
  1335. DTU_Write("{\"cmd\": \"dtu_reboot\"}");
  1336. }
  1337. else if (strstr(rx_buffer,"saveLog_usb")!=NULL) {
  1338. DTU_Write("{\"cmd\": \"saveLog_usb\"}");
  1339. }
  1340. //屏幕上触发远程升级
  1341. else if (strstr(rx_buffer,"hmi_update")!=NULL) {
  1342. DTU_Write("hmi_update");
  1343. device_status.update_flag = 1;
  1344. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1345. rt_thread_delay(2000);
  1346. rt_hw_cpu_reset();
  1347. }else if (strstr(rx_buffer,"SaveRecordParam")!=NULL){
  1348. //"SaveRecordParam:10000-200"
  1349. int param1, param2;
  1350. // 从冒号后开始扫描
  1351. if (sscanf(rx_buffer + strlen("SaveRecordParam:"), "%d-%d", &param1, &param2) == 2) {
  1352. printf("Param1: %d\n", param1);
  1353. printf("Param2: %d\n", param2);
  1354. char temp_val_mqtt[10],temp_val_pic[10];
  1355. itoa(param1, temp_val_pic, 10);
  1356. itoa(param2, temp_val_mqtt, 10);
  1357. char *write_record_param = makeJson_record_write(temp_val_mqtt,temp_val_pic);
  1358. DTU_Write(write_record_param);
  1359. free(write_record_param);
  1360. } else {
  1361. LOG_E("Parse failed.\n");
  1362. }
  1363. }
  1364. else if (strstr(rx_buffer,"ReadRecordParam")!=NULL){
  1365. LOG_I("读离线存储参数");
  1366. char* read_record =makeJson_record_read("data");
  1367. DTU_Write(read_record);
  1368. free(read_record);
  1369. }
  1370. else if (strstr(rx_buffer,"checkcamera")!=NULL){
  1371. DTU_Write("{\"ctr_cam\":\"checkcamera\"}");
  1372. //校准屏幕的RTC
  1373. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  1374. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  1375. HMI_WRITE("rtc2=%d",tm.tm_mday);
  1376. HMI_WRITE("rtc3=%d",tm.tm_hour);
  1377. HMI_WRITE("rtc4=%d",tm.tm_min);
  1378. HMI_WRITE("rtc5=%d",tm.tm_sec);
  1379. //开启超时定时器
  1380. Start_timer_Uart2();
  1381. }else if (strstr(rx_buffer,"ReadCamParam")!=NULL){
  1382. DTU_Write("{\"ctr_cam\":\"ReadParam\"}");
  1383. //开启超时定时器
  1384. Start_timer_Uart2();
  1385. }//SaveCamParam:2070.00000-0.7-144-30-0-0-5472*5472
  1386. else if (strstr(rx_buffer,"SaveCamParam")!=NULL){
  1387. const char *prefix ="SaveCamParam:";
  1388. size_t prefix_len = strlen(prefix);
  1389. const char *start_pos = strstr(rx_buffer, prefix);
  1390. if(start_pos!=NULL)
  1391. {
  1392. // 移动到前缀之后的位置
  1393. start_pos += prefix_len;
  1394. DTU_Write("%s",makeJson_Cam("SaveParam",start_pos));
  1395. }
  1396. //开启超时定时器
  1397. Start_timer_Uart2();
  1398. }else if (strstr(rx_buffer,"GrabOn")!=NULL){
  1399. DTU_Write("{\"ctr_cam\":\"GrabOn\"}");
  1400. //开启超时定时器
  1401. Start_timer_Uart2();
  1402. }else if (strstr(rx_buffer,"GrabOff")!=NULL){
  1403. DTU_Write("{\"ctr_cam\":\"GrabOff\"}");
  1404. //开启超时定时器
  1405. Start_timer_Uart2();
  1406. }
  1407. else if(strstr(rx_buffer,"SelfCheck")!=NULL)
  1408. {
  1409. //校准屏幕的RTC
  1410. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  1411. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  1412. HMI_WRITE("rtc2=%d",tm.tm_mday);
  1413. HMI_WRITE("rtc3=%d",tm.tm_hour);
  1414. HMI_WRITE("rtc4=%d",tm.tm_min);
  1415. HMI_WRITE("rtc5=%d",tm.tm_sec);
  1416. if(device_status.selfcheck_ws==2||device_status.selfcheck_ws==3)
  1417. {
  1418. LOG_I("自检进行中!");
  1419. }else {
  1420. device_status.selfcheck_ws = 2;
  1421. LOG_W("开始自检:%d",device_status.selfcheck_ws);
  1422. }
  1423. }else if(strstr(rx_buffer,"SwitchMode")!=NULL)
  1424. {
  1425. device_status.work_mode = !device_status.work_mode;
  1426. ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
  1427. LOG_W("修改工作模式:%d",device_status.work_mode);
  1428. }else if(strstr(rx_buffer,"DeleteUimage")!=NULL){
  1429. char* temp = makeJsonRpc2_0("upan_delete_image", NULL, 1);
  1430. if(temp){
  1431. DTU_Write(temp);
  1432. free(temp);
  1433. }
  1434. }else if(strstr(rx_buffer,"MOVE_IMAGE")!=NULL){
  1435. char* temp = makeJsonRpc2_0("MOVE_IMAGE", NULL, 1);
  1436. if(temp){
  1437. DTU_Write(temp);
  1438. free(temp);
  1439. }
  1440. }else if(strstr(rx_buffer,"COPY_IMAGE")!=NULL){
  1441. cJSON *params = cJSON_CreateObject(); // params 子对象
  1442. cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
  1443. cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
  1444. char* temp = makeJsonRpc2_0("COPY_IMAGE", params, 1);
  1445. if(temp){
  1446. DTU_Write(temp);
  1447. free(temp);
  1448. }
  1449. }else if(strstr(rx_buffer,"UMOUNT_UPAN")!=NULL){
  1450. char* temp = makeJsonRpc2_0("UMOUNT_UPAN", NULL, 1);
  1451. if(temp){
  1452. DTU_Write(temp);
  1453. free(temp);
  1454. }
  1455. }
  1456. }
  1457. //入口函数-电机控制及
  1458. static void Ctr_Mortor_entry(void* parameter)
  1459. {
  1460. rt_uint16_t num_up_open = 200; //20s 上仓门打开时间
  1461. rt_uint16_t num_up_close = 200; // 上仓门关闭时间
  1462. rt_uint16_t num_down_open = 200; // 下仓门打开时间
  1463. rt_uint16_t num_down_close = 200; // 下仓门关闭时间
  1464. rt_int16_t photo_led_sec = 900; //55S
  1465. static rt_uint16_t mock_uart2_timer_cnt = 0;
  1466. static rt_uint16_t route_off_t = 0;
  1467. device_status.route_off_t = 0;
  1468. rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
  1469. while(1)
  1470. {
  1471. #if 1
  1472. time_t time_reset12V;
  1473. time(&time_reset12V);
  1474. static struct tm alarm_reset12V;
  1475. alarm_reset12V =*localtime(&time_reset12V);
  1476. if((alarm_reset12V.tm_hour)==8&&(alarm_reset12V.tm_min==18)&&(alarm_reset12V.tm_sec==00))
  1477. {
  1478. char *rep = makeJsonRpc2_0("reboot",NULL, 1);
  1479. if(rep){
  1480. DTU_Write(rep);
  1481. free(rep);
  1482. }
  1483. rt_thread_delay(1000);
  1484. }
  1485. if(device_status.mock_uart2_timer_flag==1){
  1486. mock_uart2_timer_cnt++;
  1487. if(mock_uart2_timer_cnt>=200){
  1488. mock_uart2_timer_cnt = 0;
  1489. device_status.mock_uart2_timer_flag = 0;
  1490. }
  1491. }
  1492. #endif
  1493. if(device_status.ctr_up_motor==1)
  1494. {
  1495. ctr_cangmen_up(OPEN);
  1496. num_up_open--;
  1497. if (num_up_open == 0)
  1498. {
  1499. ctr_cangmen_up(OFF);
  1500. num_up_open = 200;
  1501. system_status.upper_door_open_count +=1;
  1502. }
  1503. num_up_close = 200;
  1504. }
  1505. else if(device_status.ctr_up_motor==2)
  1506. {
  1507. ctr_cangmen_up(CLOSE);
  1508. num_up_close --;
  1509. if (num_up_close == 0)
  1510. {
  1511. ctr_cangmen_up(OFF);
  1512. num_up_close= 200;
  1513. system_status.upper_door_close_count +=1;
  1514. }
  1515. num_up_open = 200;
  1516. }
  1517. if (device_status.ctr_down_motor == 1)
  1518. {
  1519. ctr_cangmen_down(OPEN);
  1520. num_down_open--;
  1521. #if TG_MODULE ==1
  1522. if(rt_pin_read(CTR_UPC_LIMIT)==1){
  1523. ctr_cangmen_down(OFF);
  1524. Beep_Tip(2000);
  1525. ctr_cangmen_down(OPEN);
  1526. Beep_Tip(2000);
  1527. num_down_open = 200;
  1528. device_status.ctr_down_motor = 1;
  1529. }
  1530. #endif
  1531. if (num_down_open == 0) //2025-07-07,增加限位检测
  1532. {
  1533. ctr_cangmen_down(OFF);
  1534. num_down_open = 200;
  1535. system_status.lower_door_open_count +=1;
  1536. }
  1537. num_down_close = 200;
  1538. }
  1539. else if (device_status.ctr_down_motor == 2)
  1540. {
  1541. ctr_cangmen_down(CLOSE);
  1542. num_down_close--;
  1543. if (num_down_close == 0)
  1544. {
  1545. ctr_cangmen_down(OFF);
  1546. num_down_close = 200;
  1547. system_status.lower_door_close_count += 1;
  1548. }
  1549. num_down_open = 200;
  1550. }
  1551. if(device_status.sun_test==1)
  1552. {
  1553. SunTest();
  1554. }
  1555. //拍照
  1556. if(device_status.photo_ws==1)
  1557. {
  1558. struct timeval tv;
  1559. gettimeofday(&tv, NULL);
  1560. device_status.photo_stamp = tv.tv_sec-8*3600;
  1561. if(photo_led_sec==900)
  1562. ctr_led(ON);
  1563. //打开补光灯
  1564. if(photo_led_sec==800)
  1565. {
  1566. system_status.photo_taken_count+=1;
  1567. LOG_I("测试拍照!");
  1568. #if 1
  1569. char *takephotojson = makeJson_takephoto();
  1570. DTU_Write("%s",takephotojson);
  1571. rt_free(takephotojson);
  1572. #endif
  1573. rt_thread_delay(5000); //延时
  1574. }
  1575. //15S后执行拍照
  1576. else if (photo_led_sec==750)
  1577. {
  1578. //DTU_Write("takephoto.mp3");
  1579. //延时
  1580. rt_thread_delay(1000);
  1581. LOG_I("测试拍照!");
  1582. Hk_Take_io();
  1583. }
  1584. photo_led_sec--;
  1585. if(photo_led_sec==300) //
  1586. {
  1587. ctr_led(OFF);
  1588. device_status.photo_ws = 0;
  1589. photo_led_sec = 900;
  1590. device_status.clear_status = 1;
  1591. HMI_WRITE("sysTest.switchPhoto.val=0");
  1592. HMI_WRITE("sysTest.switchFill.val=0");
  1593. }
  1594. }else{
  1595. photo_led_sec = 900;
  1596. }
  1597. if(device_status.selfcheck_ws==2)
  1598. {
  1599. selfcheck(1);
  1600. }else if (device_status.selfcheck_ws==3) {
  1601. selfcheck(0);
  1602. }
  1603. if(device_status.route_off_t!=0)
  1604. {
  1605. ctr_router(OFF);
  1606. route_off_t = device_status.route_off_t * 60 * 10;
  1607. device_status.route_off_t = 0;
  1608. }
  1609. if(route_off_t>0)
  1610. {
  1611. route_off_t--;
  1612. if(route_off_t==1)
  1613. {
  1614. ctr_router(ON);
  1615. route_off_t = 0;
  1616. }
  1617. }
  1618. rt_thread_delay(100);
  1619. }
  1620. }
  1621. /*屏幕写数据*/
  1622. int Hmi_Fresh(void)
  1623. {
  1624. //初始化串口4
  1625. //HMI_uart4_init();
  1626. rt_err_t ret = RT_EOK;
  1627. /* 初始化消息队列 */
  1628. rt_mq_init(&rx_mq, "rx_mq",
  1629. msg_pool, /* 存放消息的缓冲区 */
  1630. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1631. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1632. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1633. /* 设置接收回调函数 */
  1634. rt_device_set_rx_indicate(serial, uart_input);
  1635. /* 创建线程*/
  1636. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1637. Hmi_thread_entry,
  1638. RT_NULL,
  1639. HMI_THREAD_STACK_SIZE,
  1640. HMI_THREAD_PRIORITY,
  1641. HMI_THREAD_TIMESLICE);
  1642. /*部分控制的线程*/
  1643. ctr_motor = rt_thread_create("Ctr motor",
  1644. Ctr_Mortor_entry,
  1645. RT_NULL,
  1646. CONTROL_MOTOR_STACK_SIZE,
  1647. CONTROL_MOTOR_PRIORITY,
  1648. CONTROL_MOTOR_TIMESLICE);
  1649. if(ctr_motor != RT_NULL)
  1650. {
  1651. rt_thread_startup(ctr_motor);
  1652. }
  1653. /* 如果获得线程控制块,启动这个线程 */
  1654. if (hmi1 != RT_NULL)
  1655. {
  1656. rt_thread_startup(hmi1);
  1657. }
  1658. else
  1659. {
  1660. ret = RT_ERROR;
  1661. }
  1662. return ret;
  1663. }
  1664. void Check_ParamConfig(void)
  1665. {
  1666. time_t now = time(RT_NULL);
  1667. static struct tm tm;
  1668. gmtime_r(&now, &tm);
  1669. if(easyflash_init()==EF_NO_ERR) //初始化成功
  1670. {
  1671. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  1672. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  1673. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  1674. device_status.rebootcnt++;
  1675. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  1676. LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  1677. }
  1678. else {
  1679. LOG_E("easyflash init ERRPR!!!");
  1680. }
  1681. //保存默认配置
  1682. if(device_status.iSave!=FLASH_FLAG)
  1683. {
  1684. device_status.iSave=FLASH_FLAG;
  1685. device_status.iFlag = 1;
  1686. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  1687. device_status.dtype = 3;
  1688. device_status.version_type = 4; //4.0
  1689. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  1690. device_status.clt_time = 10; //收集时间
  1691. device_status.heattime = 10; //加热时间
  1692. device_status.heatvalue=100;//加热温度
  1693. device_status.thsp = 300; //默认熄屏时间5分钟
  1694. device_status.sunBar = 9; //光控定时时长
  1695. device_status.startBar = 20; //定时开始时间
  1696. device_status.stopBar = 0; //定时结束时间
  1697. device_status.tph = 70; //高温保护阈值
  1698. device_status.tpl = 5; //低温保护阈值
  1699. device_status.shake_ws = 1; //震动开关,默认开
  1700. #if TG_MODULE == 0
  1701. device_status.shake_sec = 1; //震动时间,s
  1702. #else
  1703. device_status.shake_sec = 2; //震动时间,s
  1704. #endif
  1705. device_status.vol = 7; //音量
  1706. device_status.timeSwitch = 0; //工作模式:光控
  1707. device_status.passWord = 8888; //屏幕密码
  1708. device_status.dattim = 10; //数据上传间隔
  1709. device_status.ds = 1; //设备状态
  1710. device_status.update_flag = 0;
  1711. /*孢子仪*/
  1712. device_status.collecting_sh1 = 9;
  1713. device_status.collecting_eh1 = 10;
  1714. device_status.collecting_sh2 = 14;
  1715. device_status.collecting_eh2 = 16;
  1716. device_status.cold_sw = 0;
  1717. device_status.heat_sw = 0;
  1718. device_status.cul_time = 1;
  1719. device_status.run_sta = 0;
  1720. device_status.handle_bugger = 0; //默认排虫
  1721. device_status.work_mode = 0;
  1722. sprintf(device_status.driver, "%s",VERSION_HK);
  1723. sprintf(device_status.deviceId,"0");
  1724. rt_sprintf(device_status.tempture, "-15");
  1725. rt_sprintf(device_status.humidity, "31");
  1726. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  1727. rt_sprintf(device_status.alarm_tg,"220000");
  1728. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  1729. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1730. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1731. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1732. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1733. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1734. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1735. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1736. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1737. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1738. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1739. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1740. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1741. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1742. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1743. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1744. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1745. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  1746. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  1747. ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
  1748. ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  1749. ef_set_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw)); //制冷开关
  1750. ef_set_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //加热开关
  1751. ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
  1752. ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
  1753. ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
  1754. ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
  1755. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
  1756. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1757. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  1758. ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
  1759. ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
  1760. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  1761. LOG_I("Default paramconf is:");
  1762. LOG_I("stime:%d",device_status.iSave);
  1763. LOG_I("stime:%d",device_status.clt_time);
  1764. LOG_I("startBar:%d",device_status.startBar);
  1765. LOG_I("stopBar:%d",device_status.stopBar);
  1766. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  1767. LOG_I("sunBar:%d",device_status.sunBar);
  1768. LOG_I("jtime:%d",device_status.heattime);
  1769. LOG_I("jheat:%d",device_status.heatvalue);
  1770. LOG_I("thsp:%d", device_status.thsp);
  1771. LOG_I("passwd:%d",device_status.passWord);
  1772. LOG_I("alarm_tg:%s",device_status.alarm_tg);
  1773. }//从easyflash读取参数配置
  1774. else if (device_status.iSave==FLASH_FLAG) {
  1775. device_status.iFlag = 0;
  1776. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  1777. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  1778. ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
  1779. ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
  1780. ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
  1781. ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
  1782. ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
  1783. ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
  1784. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
  1785. ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
  1786. ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
  1787. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  1788. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  1789. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  1790. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  1791. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  1792. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  1793. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  1794. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  1795. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  1796. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  1797. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  1798. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  1799. ef_get_env_blob("json_w_clock",&device_status.json_w_clock,sizeof(device_status.json_w_clock),NULL); //时间
  1800. ef_get_env_blob("json_w_reset",&device_status.json_w_reset,sizeof(device_status.json_w_reset),NULL); //重置
  1801. ef_get_env_blob("json_w_platform",&device_status.json_w_platform,sizeof(device_status.json_w_platform),NULL);//平台
  1802. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  1803. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  1804. ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //平台地址
  1805. /*孢子仪*/
  1806. ef_get_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time),NULL); //培养时间
  1807. ef_get_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw),NULL); //制冷开关
  1808. ef_get_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw),NULL); //加热开关
  1809. ef_get_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1),NULL); //采集开始1
  1810. ef_get_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1),NULL); //采集结束1
  1811. ef_get_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2),NULL); //采集开始2
  1812. ef_get_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2),NULL); //采集结束2
  1813. ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
  1814. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  1815. ef_get_env_blob("deviceId",&device_status.deviceId,sizeof(device_status.deviceId),NULL); //设备编码
  1816. ef_get_env_blob("handle_bugger",&device_status.handle_bugger,sizeof(device_status.handle_bugger),NULL); //排虫
  1817. ef_get_env_blob("work_mode",&device_status.work_mode,sizeof(device_status.work_mode),NULL); //工作模式
  1818. ef_get_env_blob("alarm_tg",&device_status.alarm_tg,sizeof(device_status.alarm_tg),NULL); //闹钟
  1819. device_status.light_over_tim = device_status.sunBar * 3600; //光控计数
  1820. device_status.work_done = 0;
  1821. //工作完成标志位
  1822. if(device_status.dattim==0)
  1823. {
  1824. device_status.dattim = 10;
  1825. }
  1826. if(device_status.thsp==0) device_status.thsp = 300;
  1827. if(device_status.clt_time==0) device_status.clt_time = 10;
  1828. if(device_status.heattime==0) device_status.heattime = 10;
  1829. if(device_status.tph==0&&device_status.tpl==0){ //参数丢失
  1830. ResetParamDefault_DC();
  1831. }
  1832. if(device_status.heatvalue==0) device_status.heatvalue = 100;
  1833. if(device_status.work_mode>1) device_status.work_mode = 0;
  1834. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  1835. if(device_status.dtype!=7)
  1836. {
  1837. device_status.dtype = 3;
  1838. }
  1839. else {
  1840. device_status.version_type = 7;
  1841. }
  1842. if(device_status.version_type==0)
  1843. {
  1844. app_partion_backup(); //固件备份至df_area
  1845. device_status.version_type = 41;
  1846. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  1847. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
  1848. }
  1849. else if(device_status.version_type==1)
  1850. {
  1851. rt_sprintf(device_status.driver, "%s",VERSION_ZP);
  1852. }
  1853. else if (device_status.version_type==11)
  1854. {
  1855. rt_sprintf(device_status.driver, "%s",VERSION_GK);
  1856. }
  1857. else if (device_status.version_type==4||device_status.version_type==42||device_status.version_type==43)
  1858. {
  1859. rt_sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1860. }
  1861. else if (device_status.version_type==41) {
  1862. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  1863. }
  1864. /*定制三色光源*/
  1865. #if LAMP_EN == 1
  1866. if(device_status.sunBar%3!=0)
  1867. {
  1868. device_status.sunBar = 3;
  1869. }
  1870. // device_status.timeSwitch = 1;
  1871. // device_status.startBar = 20;
  1872. // device_status.stopBar = 23;
  1873. #endif
  1874. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  1875. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  1876. LOG_I("开机次数:%d",device_status.rebootcnt);
  1877. LOG_I("收集时间:%d",device_status.clt_time);
  1878. LOG_I("定时开始时间:%d",device_status.startBar);
  1879. LOG_I("定时结束时间:%d",device_status.stopBar);
  1880. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  1881. LOG_I("光控定时时长:%d",device_status.sunBar);
  1882. LOG_I("加热开关:%d",device_status.heat_sw);
  1883. LOG_I("加热时间:%d",device_status.heattime);
  1884. LOG_I("加热温度:%d",device_status.heatvalue);
  1885. LOG_I("震动开关:%d",device_status.shake_ws);
  1886. LOG_I("震动时间:%d",device_status.shake_sec);
  1887. LOG_I("高温保护:%d",device_status.tph);
  1888. LOG_I("低温保护:%d",device_status.tpl);
  1889. LOG_I("thsp:%d", device_status.thsp);
  1890. LOG_I("密码:%d",device_status.passWord);
  1891. LOG_I("数据上传间隔:%d",device_status.dattim);
  1892. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  1893. LOG_I("上传平台:%s",device_status.mqttcfg1);
  1894. LOG_I("孢子仪运行状态:%d",device_status.run_sta);
  1895. LOG_I("孢子仪采集时间:[%d-%d,%d-%d]",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2);
  1896. LOG_I("PowerType:%d",device_status.Power_Type);
  1897. LOG_I("deviceId:%s",device_status.deviceId);
  1898. LOG_I("handle_bugger:%d",device_status.handle_bugger);
  1899. LOG_I("alarm_tg:%s",device_status.alarm_tg);
  1900. }
  1901. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  1902. if(device_status.update_flag==1)
  1903. {
  1904. device_status.update_flag = 0;
  1905. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1906. ymodem_ota(1,0);
  1907. }
  1908. else {
  1909. device_status.update_flag = 0;
  1910. //初始化联网状态
  1911. rt_sprintf(device_status.net,"1");
  1912. rt_sprintf(device_status.csq,"0");
  1913. rt_sprintf(device_status.sim,"1");
  1914. Sys_Motor_Task();
  1915. ctr_router(ON);
  1916. Hmi_Fresh();
  1917. Control_clear();
  1918. Control_Check(); //自检
  1919. dtu_data_play();
  1920. sht30_collect();
  1921. #if SENSOR_EN==1
  1922. mb_master_samlpe();
  1923. #endif
  1924. uint16_t connect_num=0;
  1925. //等待联网,
  1926. while(atoi(device_status.net)==1)
  1927. {
  1928. char *readinfo = makeJson_ReadInfo();
  1929. DTU_Write("%s",readinfo);
  1930. rt_free(readinfo);
  1931. rt_thread_delay(1000);
  1932. connect_num++;
  1933. LOG_I("联网中......%d",connect_num);
  1934. //超时时间为3分钟
  1935. #if MATCH_DEVICE == 1
  1936. if(connect_num==60) //180
  1937. #else
  1938. if(connect_num==180) //180
  1939. #endif
  1940. {
  1941. device_status.timeSwitch = 0;
  1942. LOG_W("Time not set:%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);
  1943. #if 0
  1944. set_date(2020, 1, 1);
  1945. set_time(0, 0, 0);
  1946. #endif
  1947. break;
  1948. }
  1949. }
  1950. connect_num = 0;
  1951. while(atoi(device_status.net)==0)
  1952. {
  1953. char *readinfo = makeJson_ReadInfo();
  1954. DTU_Write("%s",readinfo);
  1955. rt_free(readinfo);
  1956. rt_thread_delay(2000);
  1957. connect_num++;
  1958. if(connect_num>5) break;
  1959. }
  1960. #if MATCH_DEVICE == 1
  1961. device_status.timeSwitch = 1;
  1962. #endif
  1963. DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
  1964. rt_thread_delay(1000);
  1965. dtu_send_data_start();
  1966. Workstatus_check();
  1967. Work_Loop_Task();
  1968. }
  1969. }
  1970. void ResetParamDefault(void)
  1971. {
  1972. #if MATCH_DEVICE == 1
  1973. rt_sprintf(device_status.tempture,"%d",30);
  1974. #else
  1975. rt_sprintf(device_status.tempture,"%d",-15);
  1976. #endif
  1977. device_status.ds = 1; //设备状态开机
  1978. device_status.thsp = 300;
  1979. device_status.passWord = 8888;
  1980. device_status.dattim = 10;
  1981. device_status.collecting_sh1 = 9;
  1982. device_status.collecting_eh1 = 10;
  1983. device_status.collecting_sh2 = 14;
  1984. device_status.collecting_eh2 = 16;
  1985. device_status.Power_Type = 0;
  1986. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1987. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1988. if(device_status.dtype==3)
  1989. {
  1990. device_status.clt_time = 10; //收集时间
  1991. #if LAMP_EN == 0
  1992. device_status.startBar = 20; //定时开始时间
  1993. device_status.stopBar = 5; //定时结束时间
  1994. #endif
  1995. device_status.heattime = 10; //加热时间
  1996. device_status.shake_ws = 1; //针对开关
  1997. #if TG_MODULE == 0
  1998. device_status.shake_sec = 1; //震动时间,s
  1999. #else
  2000. device_status.shake_sec = 2; //震动时间,s
  2001. #endif
  2002. #if LAMP_EN == 1
  2003. device_status.sunBar = 3; //光控定时时长
  2004. #else
  2005. device_status.sunBar = 8;
  2006. #endif
  2007. device_status.tph = 70; //高温保护阈值
  2008. device_status.tpl = -5; //低温保护阈值
  2009. device_status.timeSwitch = 0; //定时模式:光控
  2010. device_status.heatvalue=100; //加热温度
  2011. device_status.heat_sw = 1;
  2012. ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
  2013. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  2014. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  2015. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  2016. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2017. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  2018. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  2019. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2020. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  2021. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  2022. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  2023. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  2024. device_status.handle_bugger = 0;
  2025. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2026. }
  2027. else if(device_status.dtype==7)
  2028. {
  2029. device_status.run_sta = 0;
  2030. device_status.heatvalue = 25; //培养温度
  2031. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  2032. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  2033. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  2034. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  2035. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  2036. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2037. }
  2038. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  2039. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  2040. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  2041. HMI_WRITE("play %d,%d,%d",1,5,0);
  2042. DTU_Write("reset.mp3");
  2043. rt_thread_delay(1000);
  2044. }
  2045. void ResetParamDefault_DC(void)
  2046. {
  2047. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  2048. rt_sprintf(device_status.tempture,"%d",-15);
  2049. device_status.clt_time = 10; //收集时间
  2050. device_status.dattim = 10; //数据上传间隔
  2051. #if LAMP_EN == 0
  2052. device_status.startBar = 20; //定时开始时间
  2053. device_status.stopBar = 0; //定时结束时间
  2054. #endif
  2055. device_status.heattime = 10; //加热时间
  2056. device_status.shake_ws = 1; //针对开关
  2057. #if TG_MODULE == 0
  2058. device_status.shake_sec = 1; //震动时间,s
  2059. #else
  2060. device_status.shake_sec = 2; //震动时间,s
  2061. #endif
  2062. #if LAMP_EN == 1
  2063. device_status.sunBar = 3; //光控定时时长
  2064. #else
  2065. device_status.sunBar = 4;
  2066. #endif
  2067. device_status.tph = 70; //高温保护阈值
  2068. device_status.tpl = -5; //低温保护阈值
  2069. device_status.timeSwitch = 0; //定时模式
  2070. device_status.heatvalue=100;
  2071. device_status.ds = 1; //设备状态开机
  2072. device_status.thsp = 300;
  2073. device_status.passWord = 8888;
  2074. device_status.timeSwitch = 0;
  2075. device_status.Power_Type = 1;
  2076. device_status.handle_bugger = 0;
  2077. device_status.heat_sw = 0;
  2078. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  2079. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2080. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  2081. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  2082. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  2083. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  2084. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  2085. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2086. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  2087. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  2088. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2089. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  2090. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  2091. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  2092. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  2093. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  2094. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  2095. ef_set_env_blob("heat_sw", &device_status.heat_sw,sizeof(device_status.heat_sw));
  2096. DTU_Write("reset.mp3");
  2097. rt_thread_delay(1000);
  2098. }
  2099. //固件备份
  2100. void app_partion_backup(void)
  2101. {
  2102. rt_kprintf("固件恢复!!!\n");
  2103. const char *dst_part_name = "df_area";
  2104. const char *src_part_name = "app";
  2105. const struct fal_partition *dst_part;
  2106. const struct fal_partition *src_part;
  2107. dst_part = fal_partition_find(dst_part_name);
  2108. src_part = fal_partition_find(src_part_name);
  2109. if (dst_part == RT_NULL || src_part == RT_NULL)
  2110. {
  2111. if (dst_part == RT_NULL)
  2112. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  2113. if (src_part == RT_NULL)
  2114. rt_kprintf("%s partition is not exist!\n", src_part_name);
  2115. }
  2116. else
  2117. {
  2118. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  2119. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  2120. {
  2121. int clone_pos = 0;
  2122. int clone_len = 0, clone_tol_len;
  2123. rt_uint8_t *buf = rt_malloc(4096);
  2124. if (dst_part->len < src_part->len)
  2125. clone_tol_len = dst_part->len;
  2126. else
  2127. clone_tol_len = src_part->len;
  2128. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  2129. {
  2130. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  2131. if (clone_len < 0)
  2132. {
  2133. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  2134. break;
  2135. }
  2136. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  2137. {
  2138. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  2139. break;
  2140. }
  2141. HMI_WRITE("#");
  2142. rt_kprintf("#");
  2143. clone_pos += clone_len;
  2144. }
  2145. if (clone_pos >= clone_tol_len)
  2146. {
  2147. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  2148. }
  2149. else
  2150. {
  2151. rt_kprintf("Clone partition failed!");
  2152. }
  2153. if (buf)
  2154. rt_free(buf);
  2155. }
  2156. }
  2157. }
  2158. void Debug_log_ON(void)
  2159. {
  2160. rt_console_set_device("uart4");
  2161. finsh_set_device("uart4");
  2162. }
  2163. void Debug_log_OFF(void)
  2164. {
  2165. rt_console_set_device("uart1");
  2166. finsh_set_device("uart1");
  2167. rt_mq_init(&rx_mq, "rx_mq",
  2168. msg_pool, /* 存放消息的缓冲区 */
  2169. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  2170. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  2171. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  2172. rt_device_set_rx_indicate(serial, uart_input);
  2173. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  2174. Hmi_thread_entry,
  2175. RT_NULL,
  2176. HMI_THREAD_STACK_SIZE,
  2177. HMI_THREAD_PRIORITY,
  2178. HMI_THREAD_TIMESLICE);
  2179. if (hmi1 != RT_NULL)
  2180. {
  2181. rt_thread_startup(hmi1);
  2182. }
  2183. }
  2184. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  2185. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  2186. //
  2187. INIT_BOARD_EXPORT(HMI_uart4_init);