hmi.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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. else if (strstr(rx_buffer,"turnzp")!=NULL) {
  522. LOG_W("转仓!");
  523. device_status.clear_status=2;
  524. }
  525. //清扫
  526. else if (strstr(rx_buffer,"clear")!=NULL) {
  527. LOG_W("清扫!");
  528. device_status.clear_status=1;
  529. }
  530. //震动
  531. else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
  532. LOG_I("震动!");
  533. if(device_status.version_type==6)
  534. {
  535. Motor_shake(ON, device_status.shake_sec);
  536. }
  537. else{
  538. ctr_shake(ON,device_status.shake_sec);
  539. ctr_router(ON);
  540. }
  541. }
  542. //打开补光
  543. else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
  544. rt_kprintf("\n打开补光!!!\n");
  545. ctr_led(ON);
  546. }
  547. //关闭补光
  548. else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
  549. rt_kprintf("\n关闭补光!!!\n");
  550. ctr_led(OFF);
  551. }
  552. //打开灯管
  553. else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
  554. {
  555. device_status.test_flag=1;
  556. rt_kprintf("Open Lamp!\n");
  557. ctr_lamp(ON,1);
  558. #if LAMP_EN == 1
  559. ctr_lamp(ON,2);
  560. ctr_lamp(ON,3);
  561. #endif
  562. device_status.rain_status = 0;
  563. }
  564. //关闭灯管
  565. else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
  566. {
  567. device_status.test_flag=0;
  568. rt_kprintf("Close Lamp1!\n");
  569. ctr_lamp(OFF,1);
  570. #if LAMP_EN == 1
  571. ctr_lamp(OFF,2);
  572. ctr_lamp(OFF,3);
  573. #endif
  574. }
  575. //关闭加热
  576. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  577. device_status.heat_status = 0;
  578. ctr_Heat(OFF);
  579. rt_kprintf("Close Heat!\n");
  580. }
  581. //打开加热
  582. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  583. device_status.heat_status = 1;
  584. ctr_Heat(ON);
  585. rt_kprintf("Open Heat!\n");
  586. }
  587. //打开上仓门
  588. else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
  589. if(device_status.dtype==7)
  590. {
  591. ctr_lamp(ON, 1);
  592. ctr_Motor_Driver(1, 0,1000000,500);
  593. LOG_I("开始步进上行!");
  594. }
  595. else
  596. {
  597. rt_kprintf("Open UP!\n");
  598. ctr_cangmen_up(OFF);
  599. device_status.ctr_up_motor = 1;
  600. }
  601. }
  602. //关闭上仓门
  603. else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
  604. if(device_status.dtype==7)
  605. {
  606. LOG_I("停止步进上行!");
  607. ctr_lamp(OFF, 1);
  608. ctr_Motor_Driver(0, 0,0,0);
  609. }
  610. else
  611. {
  612. rt_kprintf("CLOSE UP!\n");
  613. ctr_cangmen_up(OFF);
  614. device_status.ctr_up_motor = 2;
  615. }
  616. }
  617. //打开下仓门
  618. else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
  619. if(device_status.dtype==7)
  620. {
  621. LOG_I("开始步进下行!");
  622. ctr_lamp(ON, 1);
  623. ctr_Motor_Driver(1, 1,1000000,500);
  624. }
  625. else
  626. {
  627. ctr_cangmen_down(OFF);
  628. device_status.ctr_down_motor = 1;
  629. rt_kprintf("Open down!\n");
  630. }
  631. }
  632. //关闭下仓门
  633. else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
  634. if(device_status.dtype==7)
  635. {
  636. ctr_lamp(OFF, 1);
  637. ctr_Motor_Driver(0, 0,0,0);
  638. rt_kprintf("停止步进下行!\n");
  639. }
  640. else
  641. {
  642. ctr_cangmen_down(OFF);
  643. device_status.ctr_down_motor = 2;
  644. rt_kprintf("CLOSE down!\n");
  645. }
  646. }
  647. else if(strstr(rx_buffer,"putTest.Testpaichong")!=NULL){
  648. if(device_status.collt_box_sta==0){
  649. device_status.collt_box_sta = 111;
  650. }else{
  651. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  652. }
  653. LOG_I("排虫测试!");
  654. }
  655. //排虫
  656. else if(strstr(rx_buffer,"putTest.paichong")!=NULL){
  657. if(device_status.run_sta == 0){
  658. device_status.run_sta = 111;
  659. }else{
  660. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  661. }
  662. LOG_I("开始排虫!");
  663. device_status.handle_bugger = 0;
  664. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  665. }
  666. else if(strstr(rx_buffer,"putTest.shouchong")!=NULL){
  667. if(device_status.run_sta == 0){
  668. device_status.run_sta = 112;
  669. }else{
  670. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  671. }
  672. LOG_I("开始收虫!");
  673. device_status.handle_bugger = 1;
  674. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  675. }
  676. //设置设备时间
  677. else if (strstr(rx_buffer,"stamp=")!=NULL)
  678. {
  679. var = strstr(rx_buffer,"=");
  680. var ++;
  681. LOG_I("设置时间:%s",var);
  682. static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
  683. strncpy(year_s, var, 4);
  684. strncpy(month_s,var+4,2);
  685. strncpy(day_s,var+6,2);
  686. strncpy(hour_s,var+8,2);
  687. strncpy(min_s,var+10,2);
  688. strncpy(sec_s,var+12,2);
  689. 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)
  690. {
  691. if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
  692. {
  693. LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
  694. 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)
  695. {
  696. LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
  697. set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
  698. MA_save();
  699. }
  700. //上传最新数据
  701. device_status.num_updata = 2; //2秒后上报数据更新RTU的时间
  702. }
  703. else {
  704. LOG_E("set_date failed!");
  705. }
  706. }
  707. }
  708. //设置闹钟时间
  709. else if (strstr(rx_buffer,"ALARM=")!=NULL)
  710. {
  711. char *var = strstr(rx_buffer,"=");
  712. if (var == NULL) {
  713. return;
  714. }
  715. var ++;
  716. // ===================== 修复:去掉末尾的 \r 或 换行 =====================
  717. // 遍历字符串,把 \r \n 全部替换成结束符
  718. for (int i = 0; var[i] != '\0'; i++) {
  719. if (var[i] == '\r' || var[i] == '\n') {
  720. var[i] = '\0';
  721. break;
  722. }
  723. }
  724. if(var[6] != '\r') {
  725. var[6] = '\0';
  726. }
  727. // 现在 var 只剩下干净的 6 位数字了
  728. int len = strlen(var);
  729. int is_valid = 1;
  730. // 1. 必须是 6 个字符(已经去掉\r)
  731. if (len != 6) {
  732. is_valid = 0;
  733. }
  734. // 2. 必须全部是数字 0-9
  735. else {
  736. for (int i = 0; i < 6; i++) {
  737. if (!(var[i] >= '0' && var[i] <= '9')) {
  738. is_valid = 0;
  739. break;
  740. }
  741. }
  742. }
  743. // 校验不通过:打印报错,不赋值
  744. if (!is_valid) {
  745. LOG_E("闹钟格式错误!必须是 6 位数字时间(如 150000),当前值:%s", var);
  746. return;
  747. }
  748. // ======================================================================
  749. // 格式正确才赋值
  750. LOG_I("设置闹钟时间:%s",var);
  751. rt_sprintf(device_status.alarm_tg, "%s", var);
  752. ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
  753. }
  754. //设备编码设置
  755. else if (strstr(rx_buffer,"deviceId=")!=NULL)
  756. {
  757. char value_str[20];
  758. memset(value_str,0,sizeof(value_str));
  759. LOG_I("设备编码 长度:%d",strlen(rx_buffer));
  760. if(strlen(rx_buffer)!=19){
  761. LOG_E("编码长度错误!");
  762. return;
  763. }
  764. var = strstr(rx_buffer,"=");
  765. var ++;
  766. strncpy(value_str,var,8); //设备编码为16位
  767. if(strstr(value_str,"00000000")!=RT_NULL){
  768. char *config_reset = makeJson_Config_Reset();
  769. //发送json到RTU
  770. DTU_Write("%s",config_reset);
  771. rt_free(config_reset);
  772. //恢复RTU的默认配置参数
  773. }else{
  774. rt_sprintf(device_status.deviceId,"10300101%s",value_str); //设备大类编码103-虫情 设备小类编码 001 生产商代码01
  775. ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
  776. LOG_I("解析到设备编码:%s",device_status.deviceId);
  777. char *device_id = makeJson_Config_BDH(device_status.deviceId);
  778. //发送json到RTU
  779. DTU_Write("%s",device_id);
  780. rt_free(device_id);
  781. }
  782. }
  783. //定时设置
  784. else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
  785. char value_str[10];
  786. memset(value_str,0,sizeof(value_str));
  787. if(device_status.dtype==7)
  788. {
  789. var = strstr(rx_buffer,"=");
  790. var ++;
  791. strncpy(value_str,var,2);
  792. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  793. {
  794. device_status.collecting_sh1 = atoi(value_str);
  795. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  796. }
  797. else {
  798. LOG_E("Set collecting_sh1 Failed!");
  799. }
  800. memset(value_str,0,sizeof(value_str));
  801. strncpy(value_str,var+2,2);
  802. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  803. {
  804. device_status.collecting_eh1 = atoi(value_str);
  805. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  806. }
  807. else {
  808. LOG_E("Set collecting_eh1 Failed!");
  809. }
  810. memset(value_str,0,sizeof(value_str));
  811. strncpy(value_str,var+4,2);
  812. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  813. {
  814. device_status.collecting_sh2 = atoi(value_str);
  815. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  816. }
  817. else {
  818. LOG_E("Set collecting_sh2 Failed!");
  819. }
  820. memset(value_str,0,sizeof(value_str));
  821. strncpy(value_str,var+6,2);
  822. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  823. {
  824. device_status.collecting_eh2 = atoi(value_str);
  825. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  826. }
  827. else {
  828. LOG_E("Set collecting_eh2 Failed!");
  829. }
  830. memset(value_str,0,sizeof(value_str));
  831. strncpy(value_str,var+8,2);
  832. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  833. {
  834. device_status.shake_sec = atoi(value_str);
  835. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  836. }
  837. else {
  838. LOG_E("Set shake_sec Failed!");
  839. }
  840. memset(value_str,0,sizeof(value_str));
  841. strncpy(value_str,var+10,2);
  842. if(atoi(value_str)>0&&atoi(value_str)<=24)
  843. {
  844. device_status.cul_time = atoi(value_str);
  845. ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time));
  846. }
  847. else {
  848. LOG_E("Set cul_time Failed!");
  849. }
  850. MA_save();
  851. 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);
  852. return;
  853. }
  854. LOG_I("时间设置!");
  855. var=strstr(rx_buffer,"=");
  856. var++;
  857. strncpy(value_str,var,2);
  858. if(atoi(value_str) > 0 && atoi(value_str) <60)
  859. {
  860. if(device_status.version_type!=6)
  861. {
  862. device_status.sunBar=atoi(value_str);
  863. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  864. }
  865. else {
  866. device_status.shake_sec = atoi(value_str);
  867. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  868. }
  869. }
  870. else {
  871. LOG_E("参数设置异常(光控时长):%d!!!",atoi(value_str));
  872. }
  873. memset(value_str,0,sizeof(value_str));
  874. //时控开始时间
  875. strncpy(value_str,var+2,2);
  876. if(atoi(value_str) >= 0 && atoi(value_str) < 24)
  877. {
  878. #if 1
  879. device_status.startBar=atoi(value_str);
  880. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  881. #endif
  882. }
  883. else {
  884. LOG_E("参数设置异常(开始):%d!!!",atoi(value_str));
  885. }
  886. memset(value_str,0,sizeof(value_str));
  887. //
  888. strncpy(value_str,var+4,2);
  889. if(atoi(value_str) >= 0&& atoi(value_str) < 24)
  890. {
  891. #if 1
  892. device_status.stopBar=atoi(value_str);
  893. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  894. #endif
  895. }
  896. else {
  897. LOG_E("参数设置异常(结束):%d!!!",atoi(value_str));
  898. }
  899. memset(value_str,0,sizeof(value_str));
  900. //
  901. strncpy(value_str,var+6,2);
  902. if(atoi(value_str) >= 0 && atoi(value_str) <= 1)
  903. {
  904. device_status.timeSwitch=atoi(value_str);
  905. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  906. }
  907. else {
  908. if(device_status.version_type==6)
  909. {
  910. device_status.timeSwitch=1;
  911. device_status.dattim=atoi(value_str);
  912. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  913. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  914. }
  915. else {
  916. LOG_E("参数设置异常(模式):%d!!!",atoi(value_str));
  917. }
  918. }
  919. //2022/06/23,add:
  920. 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);
  921. json_temp = makeJson_Warn(device_status.json_w_clock, "修改工作时间");
  922. ef_set_env_blob("json_w_clock", &device_status.json_w_clock, sizeof(device_status.json_w_clock)); //将修改屏幕参数操作记录保存到Flash中
  923. if(atoi(device_status.net)==0)
  924. DTU_Write("%s",json_temp);
  925. rt_free(json_temp);
  926. rt_thread_delay(2000);
  927. MA_save();
  928. rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
  929. }
  930. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  931. static char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
  932. static char value_temp[5];
  933. LOG_I("系统设置!");
  934. if(device_status.dtype==7)
  935. {
  936. LOG_I("系统设置,孢子仪!");
  937. var = strstr(rx_buffer,"=");
  938. var ++;
  939. memset(value_temp,0,sizeof(value_temp));
  940. strncpy(value_temp,var,2);
  941. if(atoi(value_temp)==0 || atoi(value_temp) == 1)
  942. {
  943. device_status.cold_sw = atoi(value_temp); //制冷开关
  944. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  945. }
  946. else {
  947. LOG_E("制冷开关参数设置错误!");
  948. }
  949. memset(value_temp,0,sizeof(value_temp));
  950. strncpy(value_temp,var+2,2);
  951. if(atoi(value_temp)>=0 && atoi(value_temp) <= 7)
  952. {
  953. device_status.vol = atoi(value_temp); //音量
  954. }
  955. else {
  956. LOG_E("音量参数设置错误!");
  957. }
  958. memset(value_temp,0,sizeof(value_temp));
  959. strncpy(value_temp,var+4,2);
  960. if(atoi(value_temp)>=10 && atoi(value_temp) <= 50)
  961. {
  962. device_status.heatvalue = atoi(value_temp); //设定的培养温度
  963. ef_set_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue));
  964. }
  965. else {
  966. LOG_E("培养温度参数设置失败!");
  967. }
  968. memset(value_temp,0,sizeof(value_temp));
  969. strncpy(value_temp,var+6,2);
  970. if(atoi(value_temp)>=1 && atoi(value_temp) <= 60)
  971. {
  972. device_status.dattim = atoi(value_temp); //设定的数据上传间隔,单位:分钟
  973. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  974. }
  975. else {
  976. LOG_E("数据上传间隔参数设置失败!");
  977. }
  978. memset(value_temp,0,sizeof(value_temp));
  979. strncpy(value_temp,var+8,2);
  980. if(atoi(value_temp)>=1 && atoi(value_temp) <= 30)
  981. {
  982. device_status.thsp = atoi(value_temp); //设定的息屏间隔,单位:分钟
  983. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  984. }
  985. else {
  986. LOG_E("数据息屏间隔参数设置失败!");
  987. }
  988. LOG_I("制冷开关:%d",device_status.cold_sw);
  989. LOG_I("系统设置已保存:\n自动熄屏-%d\n音量-%d\n培养温度-%d\n数据上传间隔-%d",device_status.thsp,device_status.vol,device_status.heatvalue,device_status.dattim);
  990. MA_save();
  991. return;
  992. }
  993. var = strstr(rx_buffer,"=");
  994. var ++;
  995. strncpy(clt_time_str,var,2);
  996. if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
  997. {
  998. device_status.clt_time = atoi(clt_time_str); //收集时间
  999. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1000. }
  1001. strncpy(heattime_str,var+2,2);
  1002. if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
  1003. {
  1004. device_status.heattime = atoi(heattime_str); //加热时间
  1005. LOG_I("设置加热时间:%d",atoi(heattime_str));
  1006. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1007. }
  1008. strncpy(heatvale_str,var+4,3);
  1009. if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
  1010. {
  1011. device_status.heatvalue=atoi(heatvale_str);
  1012. LOG_I("设置加热温度:%d",device_status.heatvalue);
  1013. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1014. }
  1015. strncpy(vol_str,var+7,3);
  1016. if(atoi(vol_str)>0&&atoi(vol_str)<8)
  1017. {
  1018. device_status.vol = atoi(vol_str); //音量
  1019. LOG_I("设置音量:%d",device_status.vol);
  1020. //DTU_Write("volume=");
  1021. }
  1022. strncpy(thsp_str,var+10,4);
  1023. if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
  1024. {
  1025. device_status.thsp = atoi(thsp_str); //熄屏时间
  1026. LOG_I("设置熄屏时间:%d",device_status.thsp);
  1027. }
  1028. else {
  1029. LOG_E("参数设置失败!!!");
  1030. }
  1031. rt_thread_delay(1000);
  1032. MA_save();
  1033. //rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
  1034. }
  1035. else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
  1036. char rtc_h[2];
  1037. var = strstr(rx_buffer,"=");
  1038. var++;
  1039. strncpy(rtc_h,var,2);
  1040. if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24)
  1041. {
  1042. set_time(atoi(rtc_h), 99, 99);
  1043. }
  1044. }
  1045. else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
  1046. char rtc_m[2];
  1047. var = strstr(rx_buffer,"=");
  1048. var++;
  1049. strncpy(rtc_m,var,2);
  1050. if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24)
  1051. {
  1052. set_time(99, atoi(rtc_m), 99);
  1053. }
  1054. }
  1055. else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
  1056. char rtc_s[2];
  1057. var = strstr(rx_buffer,"=");
  1058. var++;
  1059. strncpy(rtc_s,var,2);
  1060. if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24)
  1061. {
  1062. set_time(99, 99, atoi(rtc_s));
  1063. }
  1064. }
  1065. else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
  1066. {
  1067. static char dattim_str[3];
  1068. rt_kprintf("上传间隔设置!\n");
  1069. var = strstr(rx_buffer,"=");
  1070. var ++;
  1071. strncpy(dattim_str,var,2);
  1072. if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
  1073. {
  1074. device_status.dattim =atoi(dattim_str); //数据上传间隔
  1075. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  1076. MA_save();
  1077. }
  1078. else {
  1079. rt_kprintf("设置失败!!!\n");
  1080. }
  1081. rt_kprintf("数据上传设置:%d\n",device_status.dattim);
  1082. }
  1083. //光控测试
  1084. else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
  1085. {
  1086. rt_kprintf("光控测试!!!");
  1087. ctr_lamp(ON,1);
  1088. device_status.sun_test = 1;
  1089. }
  1090. else if (strstr(rx_buffer,"setshake_sec=")!=NULL)
  1091. {
  1092. static char shakesec_str[4];
  1093. LOG_I("震动时间设置!");
  1094. var = strstr(rx_buffer,"=");
  1095. var ++;
  1096. strncpy(shakesec_str,var,2);
  1097. if(atoi(shakesec_str)>=0&&atoi(shakesec_str)<=10)
  1098. {
  1099. device_status.shake_sec =atoi(shakesec_str); //2026-0608:取消*10
  1100. }
  1101. else {
  1102. LOG_E("设置失败!!!");
  1103. }
  1104. LOG_D("震动时间设置:%d",device_status.shake_sec);
  1105. }
  1106. //恢复出厂设置电池
  1107. else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
  1108. {
  1109. ResetParamDefault_DC();
  1110. rt_thread_delay(1000);
  1111. //2022/06/23,add:
  1112. 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);
  1113. json_temp = makeJson_Warn(device_status.json_w_reset, "系统重置");
  1114. ef_set_env_blob("json_w_reset", &device_status.json_w_reset, sizeof(device_status.json_w_reset)); //将修改屏幕参数操作记录保存到Flash中
  1115. if(atoi(device_status.net)==0)
  1116. DTU_Write("%s",json_temp);
  1117. rt_free(json_temp);
  1118. }
  1119. //恢复出厂设置-交流
  1120. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  1121. {
  1122. ResetParamDefault();
  1123. rt_thread_delay(1000);
  1124. //2022/06/23,add:
  1125. 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);
  1126. json_temp = makeJson_Warn(device_status.json_w_reset, "系统重置");
  1127. ef_set_env_blob("json_w_reset", &device_status.json_w_reset, sizeof(device_status.json_w_reset)); //将修改屏幕参数操作记录保存到Flash中
  1128. if(atoi(device_status.net)==0)
  1129. DTU_Write("%s",json_temp);
  1130. rt_free(json_temp);
  1131. }
  1132. //获取SIM卡信息
  1133. else if(strstr(rx_buffer,"getSim")!=NULL)
  1134. {
  1135. //http://sim.openluat.com/api/wechat/openapidoc
  1136. DTU_Write("getsiminfo");
  1137. }
  1138. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  1139. Debug_log_ON();
  1140. }
  1141. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  1142. Debug_log_OFF();
  1143. }
  1144. //高空
  1145. else if (strstr(rx_buffer,"SwversionV11")!=NULL)
  1146. {
  1147. device_status.dtype = 3;
  1148. if(device_status.version_type != 11)
  1149. {
  1150. device_status.version_type = 11;
  1151. sprintf(device_status.driver, "%s",VERSION_GK);
  1152. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1153. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1154. MA_save();
  1155. }
  1156. else {
  1157. }
  1158. return;
  1159. }
  1160. //1.0
  1161. else if (strstr(rx_buffer,"SwversionV1")!=NULL)
  1162. {
  1163. if(device_status.version_type != 1)
  1164. {
  1165. device_status.dtype = 3;
  1166. device_status.version_type = 1;
  1167. sprintf(device_status.driver, "%s",VERSION_ZP);
  1168. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1169. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1170. MA_save();
  1171. }
  1172. else {
  1173. }
  1174. return;
  1175. }
  1176. //中端款
  1177. else if (strstr(rx_buffer,"SwversionV43")!=NULL) {
  1178. device_status.dtype = 3;
  1179. if(device_status.version_type != 43)
  1180. {
  1181. device_status.dtype = 3;
  1182. device_status.version_type = 43;
  1183. sprintf(device_status.driver, "%s",VERSION_HK);
  1184. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1185. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1186. MA_save();
  1187. }
  1188. else {
  1189. }
  1190. return;
  1191. }
  1192. //低端款
  1193. else if (strstr(rx_buffer,"SwversionV42")!=NULL) {
  1194. device_status.dtype = 3;
  1195. if(device_status.version_type != 42)
  1196. {
  1197. device_status.dtype = 3;
  1198. device_status.version_type = 42;
  1199. sprintf(device_status.driver, "%s",VERSION_HK);
  1200. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1201. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1202. MA_save();
  1203. }
  1204. else {
  1205. }
  1206. return;
  1207. }
  1208. //4.0
  1209. else if (strstr(rx_buffer,"SwversionV41")!=NULL) {
  1210. device_status.dtype = 3;
  1211. if(device_status.version_type != 41)
  1212. {
  1213. device_status.dtype = 3;
  1214. device_status.version_type = 41;
  1215. sprintf(device_status.driver, "%s",VERSION_HK);
  1216. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1217. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1218. MA_save();
  1219. }
  1220. else {
  1221. }
  1222. return;
  1223. }
  1224. //水稻
  1225. else if (strstr(rx_buffer,"SwversionV4")!=NULL) {
  1226. device_status.dtype = 3;
  1227. if(device_status.version_type != 4)
  1228. {
  1229. device_status.version_type = 4;
  1230. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1231. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1232. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1233. MA_save();
  1234. }
  1235. else {
  1236. }
  1237. return;
  1238. }
  1239. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  1240. {
  1241. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  1242. HMI_WRITE("sysTest.heat_txt.txt=\"%s\"",device_status.heat_power);
  1243. // HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
  1244. }
  1245. //立即上传
  1246. else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
  1247. {
  1248. UpLoad_Data();
  1249. }
  1250. else if (strstr(rx_buffer,"checkupdate")!=NULL)
  1251. {
  1252. time_t now = time(RT_NULL);
  1253. static struct tm tm;
  1254. gmtime_r(&now, &tm);
  1255. 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);
  1256. HMI_WRITE("hardcheck.p0.pic=%d",70);
  1257. //RTC时间判断,如果当前年份小于2020,提示需要设置时间
  1258. if(tm.tm_year+1900<2020)
  1259. {
  1260. 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);
  1261. 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);
  1262. }
  1263. }
  1264. //进入bootloader模式
  1265. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  1266. device_status.stop_in_bootloader = 1;
  1267. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  1268. rt_hw_cpu_reset();
  1269. }
  1270. //软件重启
  1271. else if (strstr(rx_buffer,"reboot")!=NULL) {
  1272. rt_hw_cpu_reset();
  1273. }
  1274. else if (strstr(rx_buffer,"dtu_reboot")!=NULL) {
  1275. DTU_Write("{\"cmd\": \"dtu_reboot\"}");
  1276. }
  1277. else if (strstr(rx_buffer,"saveLog_usb")!=NULL) {
  1278. DTU_Write("{\"cmd\": \"saveLog_usb\"}");
  1279. }
  1280. //屏幕上触发远程升级
  1281. else if (strstr(rx_buffer,"hmi_update")!=NULL) {
  1282. DTU_Write("hmi_update");
  1283. device_status.update_flag = 1;
  1284. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1285. rt_thread_delay(2000);
  1286. rt_hw_cpu_reset();
  1287. }else if (strstr(rx_buffer,"SaveRecordParam")!=NULL){
  1288. //"SaveRecordParam:10000-200"
  1289. int param1, param2;
  1290. // 从冒号后开始扫描
  1291. if (sscanf(rx_buffer + strlen("SaveRecordParam:"), "%d-%d", &param1, &param2) == 2) {
  1292. printf("Param1: %d\n", param1);
  1293. printf("Param2: %d\n", param2);
  1294. char temp_val_mqtt[10],temp_val_pic[10];
  1295. itoa(param1, temp_val_pic, 10);
  1296. itoa(param2, temp_val_mqtt, 10);
  1297. char *write_record_param = makeJson_record_write(temp_val_mqtt,temp_val_pic);
  1298. DTU_Write(write_record_param);
  1299. free(write_record_param);
  1300. } else {
  1301. LOG_E("Parse failed.\n");
  1302. }
  1303. }
  1304. else if (strstr(rx_buffer,"ReadRecordParam")!=NULL){
  1305. LOG_I("读离线存储参数");
  1306. char* read_record =makeJson_record_read("data");
  1307. DTU_Write(read_record);
  1308. free(read_record);
  1309. }
  1310. else if (strstr(rx_buffer,"checkcamera")!=NULL){
  1311. DTU_Write("{\"ctr_cam\":\"checkcamera\"}");
  1312. //校准屏幕的RTC
  1313. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  1314. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  1315. HMI_WRITE("rtc2=%d",tm.tm_mday);
  1316. HMI_WRITE("rtc3=%d",tm.tm_hour);
  1317. HMI_WRITE("rtc4=%d",tm.tm_min);
  1318. HMI_WRITE("rtc5=%d",tm.tm_sec);
  1319. //开启超时定时器
  1320. Start_timer_Uart2();
  1321. }else if (strstr(rx_buffer,"ReadCamParam")!=NULL){
  1322. DTU_Write("{\"ctr_cam\":\"ReadParam\"}");
  1323. //开启超时定时器
  1324. Start_timer_Uart2();
  1325. }//SaveCamParam:2070.00000-0.7-144-30-0-0-5472*5472
  1326. else if (strstr(rx_buffer,"SaveCamParam")!=NULL){
  1327. const char *prefix ="SaveCamParam:";
  1328. size_t prefix_len = strlen(prefix);
  1329. const char *start_pos = strstr(rx_buffer, prefix);
  1330. if(start_pos!=NULL)
  1331. {
  1332. // 移动到前缀之后的位置
  1333. start_pos += prefix_len;
  1334. DTU_Write("%s",makeJson_Cam("SaveParam",start_pos));
  1335. }
  1336. //开启超时定时器
  1337. Start_timer_Uart2();
  1338. }else if (strstr(rx_buffer,"GrabOn")!=NULL){
  1339. DTU_Write("{\"ctr_cam\":\"GrabOn\"}");
  1340. //开启超时定时器
  1341. Start_timer_Uart2();
  1342. }else if (strstr(rx_buffer,"GrabOff")!=NULL){
  1343. DTU_Write("{\"ctr_cam\":\"GrabOff\"}");
  1344. //开启超时定时器
  1345. Start_timer_Uart2();
  1346. }
  1347. else if(strstr(rx_buffer,"SelfCheck")!=NULL)
  1348. {
  1349. //校准屏幕的RTC
  1350. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  1351. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  1352. HMI_WRITE("rtc2=%d",tm.tm_mday);
  1353. HMI_WRITE("rtc3=%d",tm.tm_hour);
  1354. HMI_WRITE("rtc4=%d",tm.tm_min);
  1355. HMI_WRITE("rtc5=%d",tm.tm_sec);
  1356. if(device_status.selfcheck_ws==2||device_status.selfcheck_ws==3)
  1357. {
  1358. LOG_I("自检进行中!");
  1359. }else {
  1360. device_status.selfcheck_ws = 2;
  1361. LOG_W("开始自检:%d",device_status.selfcheck_ws);
  1362. }
  1363. }else if(strstr(rx_buffer,"SwitchMode")!=NULL)
  1364. {
  1365. device_status.work_mode = !device_status.work_mode;
  1366. ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
  1367. LOG_W("修改工作模式:%d",device_status.work_mode);
  1368. }else if(strstr(rx_buffer,"DeleteUimage")!=NULL){
  1369. char* temp = makeJsonRpc2_0("upan_delete_image", NULL, 1);
  1370. if(temp){
  1371. DTU_Write(temp);
  1372. free(temp);
  1373. }
  1374. }else if(strstr(rx_buffer,"MOVE_IMAGE")!=NULL){
  1375. char* temp = makeJsonRpc2_0("MOVE_IMAGE", NULL, 1);
  1376. if(temp){
  1377. DTU_Write(temp);
  1378. free(temp);
  1379. }
  1380. }else if(strstr(rx_buffer,"COPY_IMAGE")!=NULL){
  1381. cJSON *params = cJSON_CreateObject(); // params 子对象
  1382. cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
  1383. cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
  1384. char* temp = makeJsonRpc2_0("COPY_IMAGE", params, 1);
  1385. if(temp){
  1386. DTU_Write(temp);
  1387. free(temp);
  1388. }
  1389. }else if(strstr(rx_buffer,"UMOUNT_UPAN")!=NULL){
  1390. char* temp = makeJsonRpc2_0("UMOUNT_UPAN", NULL, 1);
  1391. if(temp){
  1392. DTU_Write(temp);
  1393. free(temp);
  1394. }
  1395. }
  1396. }
  1397. //入口函数-电机控制及
  1398. static void Ctr_Mortor_entry(void* parameter)
  1399. {
  1400. rt_uint16_t num_up_open = 200; //20s 上仓门打开时间
  1401. rt_uint16_t num_up_close = 200; // 上仓门关闭时间
  1402. rt_uint16_t num_down_open = 200; // 下仓门打开时间
  1403. rt_uint16_t num_down_close = 200; // 下仓门关闭时间
  1404. rt_int16_t photo_led_sec = 900; //55S
  1405. static rt_uint16_t mock_uart2_timer_cnt = 0;
  1406. static rt_uint16_t route_off_t = 0;
  1407. device_status.route_off_t = 0;
  1408. static uint8_t is_rain = 0;
  1409. static uint8_t isnot_rain = 0;
  1410. rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
  1411. while(1)
  1412. {
  1413. #if 1
  1414. time_t time_reset12V;
  1415. time(&time_reset12V);
  1416. static struct tm alarm_reset12V;
  1417. alarm_reset12V =*localtime(&time_reset12V);
  1418. if((alarm_reset12V.tm_hour)==8&&(alarm_reset12V.tm_min==18)&&(alarm_reset12V.tm_sec==00))
  1419. {
  1420. char *rep = makeJsonRpc2_0("reboot",NULL, 1);
  1421. if(rep){
  1422. DTU_Write(rep);
  1423. free(rep);
  1424. }
  1425. rt_thread_delay(1000);
  1426. }
  1427. if(device_status.mock_uart2_timer_flag==1){
  1428. mock_uart2_timer_cnt++;
  1429. if(mock_uart2_timer_cnt>=200){
  1430. mock_uart2_timer_cnt = 0;
  1431. device_status.mock_uart2_timer_flag = 0;
  1432. }
  1433. }
  1434. #endif
  1435. if(device_status.ctr_up_motor==1)
  1436. {
  1437. ctr_cangmen_up(OPEN);
  1438. num_up_open--;
  1439. if (num_up_open == 0)
  1440. {
  1441. ctr_cangmen_up(OFF);
  1442. num_up_open = 200;
  1443. system_status.upper_door_open_count +=1;
  1444. }
  1445. num_up_close = 200;
  1446. }
  1447. else if(device_status.ctr_up_motor==2)
  1448. {
  1449. ctr_cangmen_up(CLOSE);
  1450. num_up_close --;
  1451. if (num_up_close == 0)
  1452. {
  1453. ctr_cangmen_up(OFF);
  1454. num_up_close= 200;
  1455. system_status.upper_door_close_count +=1;
  1456. }
  1457. num_up_open = 200;
  1458. }
  1459. if (device_status.ctr_down_motor == 1)
  1460. {
  1461. ctr_cangmen_down(OPEN);
  1462. num_down_open--;
  1463. #if TG_MODULE ==1
  1464. if(rt_pin_read(CTR_UPC_LIMIT)==1){
  1465. ctr_cangmen_down(OFF);
  1466. Beep_Tip(2000);
  1467. ctr_cangmen_down(OPEN);
  1468. Beep_Tip(2000);
  1469. num_down_open = 200;
  1470. device_status.ctr_down_motor = 1;
  1471. }
  1472. #endif
  1473. if (num_down_open == 0) //2025-07-07,增加限位检测
  1474. {
  1475. ctr_cangmen_down(OFF);
  1476. num_down_open = 200;
  1477. system_status.lower_door_open_count +=1;
  1478. }
  1479. num_down_close = 200;
  1480. }
  1481. else if (device_status.ctr_down_motor == 2)
  1482. {
  1483. ctr_cangmen_down(CLOSE);
  1484. num_down_close--;
  1485. if (num_down_close == 0)
  1486. {
  1487. ctr_cangmen_down(OFF);
  1488. num_down_close = 200;
  1489. system_status.lower_door_close_count += 1;
  1490. }
  1491. num_down_open = 200;
  1492. }
  1493. if(device_status.sun_test==1)
  1494. {
  1495. SunTest();
  1496. }
  1497. //拍照
  1498. if(device_status.photo_ws==1)
  1499. {
  1500. struct timeval tv;
  1501. gettimeofday(&tv, NULL);
  1502. device_status.photo_stamp = tv.tv_sec-8*3600;
  1503. if(photo_led_sec==900)
  1504. ctr_led(ON);
  1505. //打开补光灯
  1506. if(photo_led_sec==800)
  1507. {
  1508. system_status.photo_taken_count+=1;
  1509. LOG_I("测试拍照!");
  1510. #if 1
  1511. char *takephotojson = makeJson_takephoto();
  1512. DTU_Write("%s",takephotojson);
  1513. rt_free(takephotojson);
  1514. #endif
  1515. rt_thread_delay(5000); //延时
  1516. }
  1517. //15S后执行拍照
  1518. else if (photo_led_sec==750)
  1519. {
  1520. //DTU_Write("takephoto.mp3");
  1521. //延时
  1522. rt_thread_delay(1000);
  1523. LOG_I("测试拍照!");
  1524. Hk_Take_io();
  1525. }
  1526. photo_led_sec--;
  1527. if(photo_led_sec==300) //
  1528. {
  1529. ctr_led(OFF);
  1530. device_status.photo_ws = 0;
  1531. photo_led_sec = 900;
  1532. device_status.clear_status = 1;
  1533. HMI_WRITE("sysTest.switchPhoto.val=0");
  1534. HMI_WRITE("sysTest.switchFill.val=0");
  1535. }
  1536. }else{
  1537. photo_led_sec = 900;
  1538. }
  1539. if(device_status.selfcheck_ws==2)
  1540. {
  1541. selfcheck(1);
  1542. }else if (device_status.selfcheck_ws==3) {
  1543. selfcheck(0);
  1544. }
  1545. if(device_status.route_off_t!=0)
  1546. {
  1547. ctr_router(OFF);
  1548. route_off_t = device_status.route_off_t * 60 * 10;
  1549. device_status.route_off_t = 0;
  1550. }
  1551. if(route_off_t>0)
  1552. {
  1553. route_off_t--;
  1554. if(route_off_t==1)
  1555. {
  1556. ctr_router(ON);
  1557. route_off_t = 0;
  1558. }
  1559. }
  1560. if(device_status.rain_status==1){
  1561. isnot_rain = 0;
  1562. if(is_rain==0){
  1563. ctr_rain_motor(CLOSE);
  1564. is_rain = 1;
  1565. }
  1566. }else{
  1567. is_rain = 0;
  1568. if(isnot_rain == 0){
  1569. ctr_rain_motor(OPEN);
  1570. isnot_rain = 1;
  1571. }
  1572. }
  1573. rt_thread_delay(100);
  1574. }
  1575. }
  1576. /*屏幕写数据*/
  1577. int Hmi_Fresh(void)
  1578. {
  1579. //初始化串口4
  1580. //HMI_uart4_init();
  1581. rt_err_t ret = RT_EOK;
  1582. /* 初始化消息队列 */
  1583. rt_mq_init(&rx_mq, "rx_mq",
  1584. msg_pool, /* 存放消息的缓冲区 */
  1585. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1586. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1587. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1588. /* 设置接收回调函数 */
  1589. rt_device_set_rx_indicate(serial, uart_input);
  1590. /* 创建线程*/
  1591. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1592. Hmi_thread_entry,
  1593. RT_NULL,
  1594. HMI_THREAD_STACK_SIZE,
  1595. HMI_THREAD_PRIORITY,
  1596. HMI_THREAD_TIMESLICE);
  1597. /*部分控制的线程*/
  1598. ctr_motor = rt_thread_create("Ctr motor",
  1599. Ctr_Mortor_entry,
  1600. RT_NULL,
  1601. CONTROL_MOTOR_STACK_SIZE,
  1602. CONTROL_MOTOR_PRIORITY,
  1603. CONTROL_MOTOR_TIMESLICE);
  1604. if(ctr_motor != RT_NULL)
  1605. {
  1606. rt_thread_startup(ctr_motor);
  1607. }
  1608. /* 如果获得线程控制块,启动这个线程 */
  1609. if (hmi1 != RT_NULL)
  1610. {
  1611. rt_thread_startup(hmi1);
  1612. }
  1613. else
  1614. {
  1615. ret = RT_ERROR;
  1616. }
  1617. return ret;
  1618. }
  1619. void Check_ParamConfig(void)
  1620. {
  1621. time_t now = time(RT_NULL);
  1622. static struct tm tm;
  1623. gmtime_r(&now, &tm);
  1624. if(easyflash_init()==EF_NO_ERR) //初始化成功
  1625. {
  1626. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  1627. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  1628. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  1629. device_status.rebootcnt++;
  1630. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  1631. LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  1632. }
  1633. else {
  1634. LOG_E("easyflash init ERRPR!!!");
  1635. }
  1636. //保存默认配置
  1637. if(device_status.iSave!=FLASH_FLAG)
  1638. {
  1639. device_status.iSave=FLASH_FLAG;
  1640. device_status.iFlag = 1;
  1641. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  1642. device_status.dtype = 3;
  1643. device_status.version_type = 4; //4.0
  1644. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  1645. device_status.clt_time = 6; //收集时间
  1646. device_status.heattime = 8; //加热时间
  1647. device_status.heatvalue=100; //加热温度
  1648. device_status.thsp = 300; //默认熄屏时间5分钟
  1649. device_status.sunBar = 9; //光控定时时长
  1650. device_status.startBar = 20; //定时开始时间
  1651. device_status.stopBar = 0; //定时结束时间
  1652. device_status.tph = 70; //高温保护阈值
  1653. device_status.tpl = 5; //低温保护阈值
  1654. device_status.shake_ws = 1; //震动开关,默认开
  1655. #if TG_MODULE == 0
  1656. device_status.shake_sec = 1; //震动时间,s
  1657. #else
  1658. device_status.shake_sec = 2; //震动时间,s
  1659. #endif
  1660. device_status.vol = 7; //音量
  1661. device_status.timeSwitch = 0; //工作模式:光控
  1662. device_status.passWord = 8888; //屏幕密码
  1663. device_status.dattim = 10; //数据上传间隔
  1664. device_status.ds = 1; //设备状态
  1665. device_status.update_flag = 0;
  1666. /*孢子仪*/
  1667. device_status.collecting_sh1 = 9;
  1668. device_status.collecting_eh1 = 10;
  1669. device_status.collecting_sh2 = 14;
  1670. device_status.collecting_eh2 = 16;
  1671. device_status.motor_active = 0;
  1672. device_status.heat_sw = 0;
  1673. device_status.cul_time = 1;
  1674. device_status.run_sta = 0;
  1675. device_status.handle_bugger = 0; //默认排虫
  1676. device_status.work_mode = 0;
  1677. sprintf(device_status.driver, "%s",VERSION_HK);
  1678. sprintf(device_status.deviceId,"0");
  1679. rt_sprintf(device_status.tempture, "-15");
  1680. rt_sprintf(device_status.humidity, "31");
  1681. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  1682. rt_sprintf(device_status.alarm_tg,"220000");
  1683. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  1684. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1685. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1686. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1687. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1688. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1689. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1690. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1691. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1692. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1693. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1694. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1695. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1696. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1697. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1698. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1699. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1700. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  1701. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  1702. ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
  1703. ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  1704. ef_set_env_blob("motor_active",&device_status.motor_active,sizeof(device_status.motor_active)); //制冷开关
  1705. ef_set_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //加热开关
  1706. ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
  1707. ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
  1708. ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
  1709. ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
  1710. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
  1711. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1712. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  1713. ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
  1714. ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
  1715. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  1716. LOG_I("Default paramconf is:");
  1717. LOG_I("stime:%d",device_status.iSave);
  1718. LOG_I("stime:%d",device_status.clt_time);
  1719. LOG_I("startBar:%d",device_status.startBar);
  1720. LOG_I("stopBar:%d",device_status.stopBar);
  1721. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  1722. LOG_I("sunBar:%d",device_status.sunBar);
  1723. LOG_I("jtime:%d",device_status.heattime);
  1724. LOG_I("jheat:%d",device_status.heatvalue);
  1725. LOG_I("thsp:%d", device_status.thsp);
  1726. LOG_I("passwd:%d",device_status.passWord);
  1727. LOG_I("alarm_tg:%s",device_status.alarm_tg);
  1728. }//从easyflash读取参数配置
  1729. else if (device_status.iSave==FLASH_FLAG) {
  1730. device_status.iFlag = 0;
  1731. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  1732. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  1733. ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
  1734. ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
  1735. ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
  1736. ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
  1737. ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
  1738. ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
  1739. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
  1740. ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
  1741. ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
  1742. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  1743. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  1744. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  1745. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  1746. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  1747. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  1748. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  1749. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  1750. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  1751. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  1752. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  1753. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  1754. ef_get_env_blob("json_w_clock",&device_status.json_w_clock,sizeof(device_status.json_w_clock),NULL); //时间
  1755. ef_get_env_blob("json_w_reset",&device_status.json_w_reset,sizeof(device_status.json_w_reset),NULL); //重置
  1756. ef_get_env_blob("json_w_platform",&device_status.json_w_platform,sizeof(device_status.json_w_platform),NULL);//平台
  1757. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  1758. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  1759. ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //平台地址
  1760. /*孢子仪*/
  1761. ef_get_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time),NULL); //培养时间
  1762. ef_get_env_blob("motor_active",&device_status.motor_active,sizeof(device_status.motor_active),NULL); //电机
  1763. ef_get_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw),NULL); //加热开关
  1764. ef_get_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1),NULL); //采集开始1
  1765. ef_get_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1),NULL); //采集结束1
  1766. ef_get_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2),NULL); //采集开始2
  1767. ef_get_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2),NULL); //采集结束2
  1768. ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
  1769. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  1770. ef_get_env_blob("deviceId",&device_status.deviceId,sizeof(device_status.deviceId),NULL); //设备编码
  1771. ef_get_env_blob("handle_bugger",&device_status.handle_bugger,sizeof(device_status.handle_bugger),NULL); //排虫
  1772. ef_get_env_blob("work_mode",&device_status.work_mode,sizeof(device_status.work_mode),NULL); //工作模式
  1773. ef_get_env_blob("alarm_tg",&device_status.alarm_tg,sizeof(device_status.alarm_tg),NULL); //闹钟
  1774. device_status.light_over_tim = device_status.sunBar * 3600; //光控计数
  1775. device_status.work_done = 0;
  1776. //工作完成标志位
  1777. if(device_status.dattim==0)
  1778. {
  1779. device_status.dattim = 10;
  1780. }
  1781. if(device_status.thsp==0) device_status.thsp = 300;
  1782. if(device_status.clt_time==0) device_status.clt_time = 10;
  1783. if(device_status.heattime==0) device_status.heattime = 10;
  1784. if(device_status.tph==0&&device_status.tpl==0){ //参数丢失
  1785. ResetParamDefault_DC();
  1786. }
  1787. if(device_status.heatvalue==0) device_status.heatvalue = 100;
  1788. if(device_status.work_mode>1) device_status.work_mode = 0;
  1789. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  1790. if(device_status.dtype!=7)
  1791. {
  1792. device_status.dtype = 3;
  1793. }
  1794. else {
  1795. device_status.version_type = 7;
  1796. }
  1797. if(device_status.version_type==0)
  1798. {
  1799. app_partion_backup(); //固件备份至df_area
  1800. device_status.version_type = 41;
  1801. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  1802. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
  1803. }
  1804. else if(device_status.version_type==1)
  1805. {
  1806. rt_sprintf(device_status.driver, "%s",VERSION_ZP);
  1807. }
  1808. else if (device_status.version_type==11)
  1809. {
  1810. rt_sprintf(device_status.driver, "%s",VERSION_GK);
  1811. }
  1812. else if (device_status.version_type==4||device_status.version_type==42||device_status.version_type==43)
  1813. {
  1814. rt_sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1815. }
  1816. else if (device_status.version_type==41) {
  1817. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  1818. }
  1819. /*定制三色光源*/
  1820. #if LAMP_EN == 1
  1821. if(device_status.sunBar%3!=0)
  1822. {
  1823. device_status.sunBar = 3;
  1824. }
  1825. // device_status.timeSwitch = 1;
  1826. // device_status.startBar = 20;
  1827. // device_status.stopBar = 23;
  1828. #endif
  1829. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  1830. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  1831. LOG_I("开机次数:%d",device_status.rebootcnt);
  1832. LOG_I("收集时间:%d",device_status.clt_time);
  1833. LOG_I("定时开始时间:%d",device_status.startBar);
  1834. LOG_I("定时结束时间:%d",device_status.stopBar);
  1835. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  1836. LOG_I("光控定时时长:%d",device_status.sunBar);
  1837. LOG_I("加热开关:%d",device_status.heat_sw);
  1838. LOG_I("加热时间:%d",device_status.heattime);
  1839. LOG_I("加热温度:%d",device_status.heatvalue);
  1840. LOG_I("震动开关:%d",device_status.shake_ws);
  1841. LOG_I("震动时间:%d",device_status.shake_sec);
  1842. LOG_I("高温保护:%d",device_status.tph);
  1843. LOG_I("低温保护:%d",device_status.tpl);
  1844. LOG_I("thsp:%d", device_status.thsp);
  1845. LOG_I("密码:%d",device_status.passWord);
  1846. LOG_I("数据上传间隔:%d",device_status.dattim);
  1847. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  1848. LOG_I("上传平台:%s",device_status.mqttcfg1);
  1849. LOG_I("孢子仪运行状态:%d",device_status.run_sta);
  1850. LOG_I("孢子仪采集时间:[%d-%d,%d-%d]",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2);
  1851. LOG_I("PowerType:%d",device_status.Power_Type);
  1852. LOG_I("deviceId:%s",device_status.deviceId);
  1853. LOG_I("handle_bugger:%d",device_status.handle_bugger);
  1854. LOG_I("alarm_tg:%s",device_status.alarm_tg);
  1855. LOG_I("motor_active:%d",device_status.motor_active);
  1856. }
  1857. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  1858. if(device_status.update_flag==1)
  1859. {
  1860. device_status.update_flag = 0;
  1861. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1862. ymodem_ota(1,0);
  1863. }
  1864. else {
  1865. device_status.update_flag = 0;
  1866. //初始化联网状态
  1867. rt_sprintf(device_status.net,"1");
  1868. rt_sprintf(device_status.csq,"0");
  1869. rt_sprintf(device_status.sim,"1");
  1870. Sys_Motor_Task();
  1871. ctr_router(ON);
  1872. ctr_Cold(ON);
  1873. Hmi_Fresh();
  1874. Control_clear();
  1875. Control_Check(); //自检
  1876. dtu_data_play();
  1877. sht30_collect();
  1878. #if SENSOR_EN==1
  1879. mb_master_samlpe();
  1880. #endif
  1881. uint16_t connect_num=0;
  1882. //等待联网,
  1883. while(atoi(device_status.net)==1)
  1884. {
  1885. char *readinfo = makeJson_ReadInfo();
  1886. DTU_Write("%s",readinfo);
  1887. rt_free(readinfo);
  1888. rt_thread_delay(1000);
  1889. connect_num++;
  1890. LOG_I("联网中......%d",connect_num);
  1891. //超时时间为3分钟
  1892. #if MATCH_DEVICE == 1
  1893. if(connect_num==60) //180
  1894. #else
  1895. if(connect_num==180) //180
  1896. #endif
  1897. {
  1898. device_status.timeSwitch = 0;
  1899. 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);
  1900. #if 0
  1901. set_date(2020, 1, 1);
  1902. set_time(0, 0, 0);
  1903. #endif
  1904. break;
  1905. }
  1906. }
  1907. connect_num = 0;
  1908. while(atoi(device_status.net)==0)
  1909. {
  1910. char *readinfo = makeJson_ReadInfo();
  1911. DTU_Write("%s",readinfo);
  1912. rt_free(readinfo);
  1913. rt_thread_delay(2000);
  1914. connect_num++;
  1915. if(connect_num>5) break;
  1916. }
  1917. #if MATCH_DEVICE == 1
  1918. device_status.timeSwitch = 1;
  1919. #endif
  1920. DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
  1921. rt_thread_delay(1000);
  1922. dtu_send_data_start();
  1923. Workstatus_check();
  1924. Work_Loop_Task();
  1925. }
  1926. }
  1927. void ResetParamDefault(void)
  1928. {
  1929. #if MATCH_DEVICE == 1
  1930. rt_sprintf(device_status.tempture,"%d",30);
  1931. #else
  1932. rt_sprintf(device_status.tempture,"%d",-15);
  1933. #endif
  1934. device_status.ds = 1; //设备状态开机
  1935. device_status.thsp = 300;
  1936. device_status.passWord = 8888;
  1937. device_status.dattim = 10;
  1938. device_status.collecting_sh1 = 9;
  1939. device_status.collecting_eh1 = 10;
  1940. device_status.collecting_sh2 = 14;
  1941. device_status.collecting_eh2 = 16;
  1942. device_status.Power_Type = 0;
  1943. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1944. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1945. if(device_status.dtype==3)
  1946. {
  1947. device_status.clt_time = 6; //收集时间
  1948. #if LAMP_EN == 0
  1949. device_status.startBar = 20; //定时开始时间
  1950. device_status.stopBar = 5; //定时结束时间
  1951. #endif
  1952. device_status.heattime = 8; //加热时间
  1953. device_status.shake_ws = 1; //针对开关
  1954. #if TG_MODULE == 0
  1955. device_status.shake_sec = 1; //震动时间,s
  1956. #else
  1957. device_status.shake_sec = 2; //震动时间,s
  1958. #endif
  1959. #if LAMP_EN == 1
  1960. device_status.sunBar = 3; //光控定时时长
  1961. #else
  1962. device_status.sunBar = 8;
  1963. #endif
  1964. device_status.tph = 70; //高温保护阈值
  1965. device_status.tpl = -5; //低温保护阈值
  1966. device_status.timeSwitch = 0; //定时模式:光控
  1967. device_status.heatvalue=100; //加热温度
  1968. device_status.heat_sw = 1;
  1969. ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
  1970. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1971. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1972. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1973. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1974. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1975. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1976. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1977. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1978. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1979. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1980. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1981. device_status.handle_bugger = 0;
  1982. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  1983. }
  1984. else if(device_status.dtype==7)
  1985. {
  1986. device_status.run_sta = 0;
  1987. device_status.heatvalue = 25; //培养温度
  1988. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  1989. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  1990. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  1991. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  1992. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  1993. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1994. }
  1995. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1996. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1997. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1998. HMI_WRITE("play %d,%d,%d",1,5,0);
  1999. DTU_Write("reset.mp3");
  2000. rt_thread_delay(1000);
  2001. }
  2002. void ResetParamDefault_DC(void)
  2003. {
  2004. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  2005. rt_sprintf(device_status.tempture,"%d",-15);
  2006. device_status.clt_time = 6; //收集时间
  2007. device_status.dattim = 10; //数据上传间隔
  2008. #if LAMP_EN == 0
  2009. device_status.startBar = 20; //定时开始时间
  2010. device_status.stopBar = 0; //定时结束时间
  2011. #endif
  2012. device_status.heattime = 8; //加热时间
  2013. device_status.shake_ws = 1; //针对开关
  2014. #if TG_MODULE == 0
  2015. device_status.shake_sec = 1; //震动时间,s
  2016. #else
  2017. device_status.shake_sec = 2; //震动时间,s
  2018. #endif
  2019. #if LAMP_EN == 1
  2020. device_status.sunBar = 3; //光控定时时长
  2021. #else
  2022. device_status.sunBar = 4;
  2023. #endif
  2024. device_status.tph = 70; //高温保护阈值
  2025. device_status.tpl = -5; //低温保护阈值
  2026. device_status.timeSwitch = 0; //定时模式
  2027. device_status.heatvalue=100;
  2028. device_status.ds = 1; //设备状态开机
  2029. device_status.thsp = 300;
  2030. device_status.passWord = 8888;
  2031. device_status.timeSwitch = 0;
  2032. device_status.Power_Type = 1;
  2033. device_status.handle_bugger = 0;
  2034. device_status.heat_sw = 0;
  2035. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  2036. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2037. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  2038. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  2039. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  2040. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  2041. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  2042. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2043. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  2044. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  2045. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2046. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  2047. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  2048. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  2049. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  2050. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  2051. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  2052. ef_set_env_blob("heat_sw", &device_status.heat_sw,sizeof(device_status.heat_sw));
  2053. DTU_Write("reset.mp3");
  2054. rt_thread_delay(1000);
  2055. }
  2056. //固件备份
  2057. void app_partion_backup(void)
  2058. {
  2059. rt_kprintf("固件恢复!!!\n");
  2060. const char *dst_part_name = "df_area";
  2061. const char *src_part_name = "app";
  2062. const struct fal_partition *dst_part;
  2063. const struct fal_partition *src_part;
  2064. dst_part = fal_partition_find(dst_part_name);
  2065. src_part = fal_partition_find(src_part_name);
  2066. if (dst_part == RT_NULL || src_part == RT_NULL)
  2067. {
  2068. if (dst_part == RT_NULL)
  2069. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  2070. if (src_part == RT_NULL)
  2071. rt_kprintf("%s partition is not exist!\n", src_part_name);
  2072. }
  2073. else
  2074. {
  2075. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  2076. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  2077. {
  2078. int clone_pos = 0;
  2079. int clone_len = 0, clone_tol_len;
  2080. rt_uint8_t *buf = rt_malloc(4096);
  2081. if (dst_part->len < src_part->len)
  2082. clone_tol_len = dst_part->len;
  2083. else
  2084. clone_tol_len = src_part->len;
  2085. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  2086. {
  2087. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  2088. if (clone_len < 0)
  2089. {
  2090. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  2091. break;
  2092. }
  2093. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  2094. {
  2095. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  2096. break;
  2097. }
  2098. HMI_WRITE("#");
  2099. rt_kprintf("#");
  2100. clone_pos += clone_len;
  2101. }
  2102. if (clone_pos >= clone_tol_len)
  2103. {
  2104. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  2105. }
  2106. else
  2107. {
  2108. rt_kprintf("Clone partition failed!");
  2109. }
  2110. if (buf)
  2111. rt_free(buf);
  2112. }
  2113. }
  2114. }
  2115. void Debug_log_ON(void)
  2116. {
  2117. rt_console_set_device("uart4");
  2118. finsh_set_device("uart4");
  2119. }
  2120. void Debug_log_OFF(void)
  2121. {
  2122. rt_console_set_device("uart1");
  2123. finsh_set_device("uart1");
  2124. rt_mq_init(&rx_mq, "rx_mq",
  2125. msg_pool, /* 存放消息的缓冲区 */
  2126. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  2127. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  2128. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  2129. rt_device_set_rx_indicate(serial, uart_input);
  2130. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  2131. Hmi_thread_entry,
  2132. RT_NULL,
  2133. HMI_THREAD_STACK_SIZE,
  2134. HMI_THREAD_PRIORITY,
  2135. HMI_THREAD_TIMESLICE);
  2136. if (hmi1 != RT_NULL)
  2137. {
  2138. rt_thread_startup(hmi1);
  2139. }
  2140. }
  2141. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  2142. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  2143. //
  2144. INIT_BOARD_EXPORT(HMI_uart4_init);