hmi.c 91 KB

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