yc_protocol.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649
  1. #include "yc_protocol.h"
  2. #include "esp_log.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/queue.h"
  6. #include "esp_mac.h"
  7. #include "stdbool.h"
  8. #include "esp_timer.h"
  9. #include "list.h"
  10. #include "user_time.h"
  11. #include "freertos/event_groups.h"
  12. #include "list.h"
  13. #include "wait_list.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include "driver/uart.h"
  18. #include "freertos/timers.h"
  19. #include "SPIFFS.h"
  20. // #include "user_config.h"
  21. Screen_dis_t screen_dis_info;
  22. // 声明一个TimerHandle_t类型的变量,用于存储定时器句柄
  23. extern EventGroupHandle_t screen_event;
  24. extern int retry_times;
  25. static const char *LOG_TAG = "yc_protocol";
  26. // esp_timer_handle_t periodic_timer;
  27. esp_timer_handle_t lora_timer;
  28. TERMINAL_INFO_T terminal_info; // 保存收到的lora数据
  29. Button_Time_t last_button_info;
  30. Button_Time_t new_button_info;
  31. Button_Time_t _0703_button_info;
  32. void changebintotxt(uint8_t *strb, int len);
  33. Node *clockIn_list = NULL; // 打卡 n:crc(ID) cmd:打卡类型 data:ID
  34. extern char str_operation[6]; // 运行
  35. extern char str_upKeep[6]; // 保养
  36. extern char str_shutDown[6]; // 停机
  37. extern char str_safeKeep[6]; // 封存
  38. extern char str_waitMaterials[6]; // 待料
  39. extern char str_breakDown[6]; // 故障
  40. extern QueueHandle_t screen_queue;
  41. uint8_t mac_addr[6];
  42. extern QueueHandle_t yc_data_queue;
  43. extern YC_DATA_T yc_data;
  44. // extern ListNode *list_head;
  45. #define LEFT_FLAG 1
  46. #define RIGHT_FLAG 2
  47. static uint8_t display_flag = 0; // 1:left 2:right
  48. #if 1
  49. #include "esp_attr.h"
  50. RTC_FAST_ATTR Person_t person[5] =
  51. {
  52. {
  53. .person_type = Administrator,
  54. },
  55. {
  56. .person_type = product_person,
  57. },
  58. {
  59. .person_type = repair_person,
  60. },
  61. {
  62. .person_type = Maintenance_person,
  63. },
  64. {
  65. .person_type = check_person,
  66. },
  67. };
  68. #endif
  69. void reset_default(bool is_dis, uint8_t power_status)
  70. {
  71. extern Machine_info_t default_info;
  72. Machine_info_t tmp;
  73. memcpy(&tmp, &default_info, sizeof(Machine_info_t));
  74. tmp.power_status = power_status;
  75. esp_read_mac((uint8_t *)tmp.mac_addr, ESP_MAC_WIFI_STA);
  76. memcpy(&tmp.cid, &Machine_info.cid, 20);
  77. tmp.batt_precent = Machine_info.batt_precent;
  78. tmp.last_batt_precent = Machine_info.last_batt_precent;
  79. tmp.rssi = Machine_info.rssi;
  80. memcpy(&Machine_info, &tmp, sizeof(Machine_info_t)); // 恢复默认
  81. spiffs_write(&Machine_info); // 保存Machine_info
  82. if (!is_dis)
  83. {
  84. return;
  85. }
  86. // Paint_leftScreen_main_quick(&Machine_info);
  87. // Paint_rightScreen_main_quick(&Machine_info);
  88. screen_dis_info.is_left = true;
  89. screen_dis_info.is_into_sleep = false;
  90. // 重置快刷
  91. Machine_info.left_current_Quick_refresh_time = 0;
  92. Machine_info.right_current_Quick_refresh_time = 0;
  93. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  94. {
  95. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  96. }
  97. screen_dis_info.is_left = false;
  98. screen_dis_info.is_into_sleep = false;
  99. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  100. {
  101. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  102. }
  103. }
  104. /*
  105. * 使用 vTaskList() 前需使能:
  106. * Enable FreeRTOS trace facility
  107. * Enable FreeRTOS stats formatting functions
  108. */
  109. void print_systenInfo(void)
  110. {
  111. printf("=================================\r\n");
  112. printf("=======%s======\r\n", "2024.1.4 [11时] burn"); // 打印烧录时间
  113. printf("=============%s===========\r\n", __TIME__);
  114. printf("=================================\r\n");
  115. printf("========version V %d.%d.%d==========\r\n", VERSION_X, VERSION_Y, VERSION_Z);
  116. esp_efuse_mac_get_default((uint8_t *)mac_addr);
  117. esp_efuse_mac_get_default((uint8_t *)Machine_info.mac_addr);
  118. #if 1 // 获取mac地址
  119. uint8_t macAddr[6];
  120. esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
  121. // 保留以前的mac地址段
  122. // mac_addr[0]+=0x02;
  123. {
  124. printf("mac addr : %02x:%02x:%02x:%02x:%02x:%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
  125. }
  126. memcpy((uint8_t *)Machine_info.mac_addr, macAddr, 6);
  127. // Machine_info.mac_addr[0]= Machine_info.mac_addr[0]+0x02;
  128. printf("Machine_info mac addr : %02x:%02x:%02x:%02x:%02x:%02x\n", Machine_info.mac_addr[0], Machine_info.mac_addr[1], Machine_info.mac_addr[2], Machine_info.mac_addr[3], Machine_info.mac_addr[4], Machine_info.mac_addr[5]);
  129. #endif
  130. printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n",
  131. esp_get_free_heap_size(), esp_get_free_internal_heap_size(), esp_get_minimum_free_heap_size());
  132. #if 0
  133. ESP_LOGW(LOG_TAG,"mac:[%02x-%02x-%02x-%02x-%02x-%02x]",\
  134. mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[05]);
  135. static char InfoBuffer[512] = {0};
  136. vTaskList((char*)&InfoBuffer);
  137. ESP_LOGW(LOG_TAG,"free_heap_size:%ld \nfree_internal_heap_size:%ld \nminimum_free_heap_size:%ld",\
  138. esp_get_free_heap_size(),esp_get_free_internal_heap_size(),esp_get_minimum_free_heap_size());
  139. printf("任务名 任务状态 优先级 剩余栈 任务序号\r\n");
  140. printf("\r\n%s\r\n", InfoBuffer);
  141. #endif
  142. }
  143. void send_update_status(void);
  144. void send_status_duration(void);
  145. void terminal_send_data(void)
  146. {
  147. // send_rssi();
  148. // vTaskDelay(100 / portTICK_PERIOD_MS);
  149. send_status_duration();
  150. // vTaskDelay(50 / portTICK_PERIOD_MS);
  151. send_update_status();
  152. // vTaskDelay(100 / portTICK_PERIOD_MS);
  153. }
  154. void update_last_button_info(uint8_t btn)
  155. {
  156. #if 0
  157. memset(&last_button_info,0,sizeof(Button_Time_t));
  158. last_button_info.button_info = btn;
  159. last_button_info.Year = Machine_info.year;
  160. last_button_info.Month = Machine_info.month;
  161. last_button_info.Day = Machine_info.day;
  162. last_button_info.Hour = Machine_info.hour;
  163. last_button_info.Minute = Machine_info.min;
  164. last_button_info.Second = Machine_info.sec;
  165. ESP_LOGW(LOG_TAG,"btn = %d [%d-%d-%d]%d:%d:%d", btn, Machine_info.year,Machine_info.month,Machine_info.day,\
  166. Machine_info.hour, Machine_info.min,Machine_info.sec);
  167. //last_button_info.time_min = 0;
  168. #else
  169. // 更新上次的状态
  170. // 获取当前时间
  171. getRtcTime(&Machine_info);
  172. Machine_info.last_button.button_info = btn;
  173. Machine_info.last_button.Year = Machine_info.year;
  174. Machine_info.last_button.Month = Machine_info.month;
  175. Machine_info.last_button.Day = Machine_info.day;
  176. Machine_info.last_button.Hour = Machine_info.hour;
  177. Machine_info.last_button.Minute = Machine_info.min;
  178. Machine_info.last_button.Second = Machine_info.sec;
  179. Machine_info.last_button.time_min = 0x00000000;
  180. #endif
  181. }
  182. void screen_display(void)
  183. {
  184. font_exit_sleep();
  185. EventBits_t bits = xEventGroupWaitBits(screen_event,
  186. LEFT_SCREEN_BIT | RIGHT_SCREEN_BIT,
  187. pdTRUE,
  188. pdFALSE,
  189. 0);
  190. ESP_LOGW(LOG_TAG, "xEventGroupWaitBits bits = %ld", bits);
  191. /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  192. * happened. */
  193. if (bits == 3)
  194. {
  195. screen_dis_info.is_left = true;
  196. screen_dis_info.is_into_sleep = false;
  197. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  198. {
  199. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  200. }
  201. screen_dis_info.is_left = false;
  202. screen_dis_info.is_into_sleep = true;
  203. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  204. {
  205. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  206. }
  207. }
  208. else if (bits & RIGHT_SCREEN_BIT)
  209. {
  210. ESP_LOGE(LOG_TAG, "RIGHT_SCREEN_BIT");
  211. screen_dis_info.is_left = false;
  212. screen_dis_info.is_into_sleep = true;
  213. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  214. {
  215. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  216. }
  217. }
  218. else if (bits & LEFT_SCREEN_BIT)
  219. {
  220. ESP_LOGE(LOG_TAG, "LEFT_SCREEN_BIT");
  221. screen_dis_info.is_left = true;
  222. screen_dis_info.is_into_sleep = true;
  223. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  224. {
  225. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  226. }
  227. }
  228. else
  229. {
  230. ESP_LOGI(LOG_TAG, "UNEXPECTED EVENT");
  231. }
  232. }
  233. void print_yc_data(const LORA_DATA_T *buf)
  234. {
  235. int len = (buf->data_buf[13 + 2]) << 8 | (buf->data_buf[14 + 2]);
  236. ESP_LOGI(LOG_TAG, "\n总长:0x%02x,crc:0x%02x,\
  237. \nmac:[%02x-%02x-%02x-%02x-%02x-%02x], \
  238. \n组内编号:%02x,\
  239. cmd:0x%02x,\
  240. 总包数:%02x, 当前包:%02x,\
  241. \n有效数据 len:0x%d",
  242. buf->data_len, buf->data_buf[4],
  243. buf->data_buf[5], buf->data_buf[6], buf->data_buf[7],
  244. buf->data_buf[8], buf->data_buf[9], buf->data_buf[10],
  245. buf->data_buf[11],
  246. buf->data_buf[12],
  247. buf->data_buf[13], buf->data_buf[14],
  248. len);
  249. for (int i = 0; i < buf->data_len; i++)
  250. {
  251. printf("%02x ", buf->data_buf[i]);
  252. if (i == (USE_DATA_LEN_INDEX + 1))
  253. {
  254. printf("data--->\n");
  255. }
  256. }
  257. printf("\n");
  258. }
  259. bool filtered_data(const LORA_DATA_T *buf)
  260. {
  261. if (buf->data_buf[11] != Machine_info.eflagID)
  262. {
  263. if (Machine_info.eflagID == 0)
  264. {
  265. ESP_LOGE(LOG_TAG, "err:未分配ID");
  266. }
  267. ESP_LOGE(LOG_TAG, "err:eflagID:%02x ,is not %02x",
  268. Machine_info.eflagID, buf->data_buf[11]);
  269. return false;
  270. }
  271. if ((buf->data_buf[5] != Machine_info.mac_addr[0]) ||
  272. (buf->data_buf[6] != Machine_info.mac_addr[1]) ||
  273. (buf->data_buf[7] != Machine_info.mac_addr[2]) ||
  274. (buf->data_buf[8] != Machine_info.mac_addr[3]) ||
  275. (buf->data_buf[9] != Machine_info.mac_addr[4]) ||
  276. (buf->data_buf[10] != Machine_info.mac_addr[5]))
  277. {
  278. ESP_LOGE(LOG_TAG, "err:mac addr:[%02x-%02x-%02x-%02x-%02x-%02x],\
  279. is not:[%02x-%02x-%02x-%02x-%02x-%02x]",
  280. Machine_info.mac_addr[0], Machine_info.mac_addr[1], Machine_info.mac_addr[2],
  281. Machine_info.mac_addr[3], Machine_info.mac_addr[4], Machine_info.mac_addr[5],
  282. buf->data_buf[5], buf->data_buf[6], buf->data_buf[7],
  283. buf->data_buf[8], buf->data_buf[9], buf->data_buf[10]);
  284. return false;
  285. }
  286. return true;
  287. }
  288. static int time_synchronization_func(const LORA_DATA_T *buf)
  289. {
  290. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  291. memcpy(terminal_info.time_synchronization, buf->data_buf + DATA_START_LEN, len);
  292. // yc_timer_restart();
  293. // Machine_info
  294. Machine_info.year = (terminal_info.time_synchronization[0] << 8) | terminal_info.time_synchronization[1];
  295. Machine_info.month = terminal_info.time_synchronization[2];
  296. Machine_info.day = terminal_info.time_synchronization[3];
  297. Machine_info.hour = terminal_info.time_synchronization[4];
  298. Machine_info.min = terminal_info.time_synchronization[5];
  299. Machine_info.sec = terminal_info.time_synchronization[6];
  300. display_flag = 0;
  301. setRtcTime(Machine_info.year, Machine_info.month, Machine_info.day,
  302. Machine_info.hour, Machine_info.min, Machine_info.sec);
  303. printf("TIMER SYNC YEAR = %d,MONTH =%d,DAY=%d,HOUR=%d,MIN=%d,SEC =%d\r\n",
  304. Machine_info.year,
  305. Machine_info.month,
  306. Machine_info.day,
  307. Machine_info.hour,
  308. Machine_info.min,
  309. Machine_info.sec);
  310. getRtcTime(&Machine_info);
  311. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  312. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  313. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  314. // printf("msg_id =%X\r\n",msg_id);
  315. return msg_id;
  316. }
  317. static int terminal_name_func(const LORA_DATA_T *buf)
  318. {
  319. #define FONT_MAX_LEN 8
  320. uint8_t font_num = 0; // 获取当前的文字个数
  321. int i = 0;
  322. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  323. memset(terminal_info.terminal_name, 0, 32);
  324. memcpy(terminal_info.terminal_name, buf->data_buf + DATA_START_LEN, len);
  325. // memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
  326. memset(Machine_info.terminal_name, 0x00, sizeof(Machine_info.terminal_name)); // 清除终端名称信息
  327. font_num = len / 2;
  328. if (font_num <= FONT_MAX_LEN)
  329. {
  330. uincode2gbk((char *)terminal_info.terminal_name, len, (char *)Machine_info.terminal_name);
  331. }
  332. else
  333. {
  334. uincode2gbk((char *)terminal_info.terminal_name, FONT_MAX_LEN * 2, (char *)Machine_info.terminal_name);
  335. strcat((char *)Machine_info.terminal_name, "...");
  336. }
  337. // printf("unicode:");
  338. // for(i=0;i<len;i++)
  339. // {
  340. // printf("%02x ",terminal_info.terminal_name[i]);
  341. // }
  342. // printf("\n");
  343. // printf("gbk:");
  344. // for(i=0;i<len;i++)
  345. // {
  346. // printf("%02x ",Machine_info.terminal_name[i]);
  347. // }
  348. // printf("setting name %s",Machine_info.terminal_name);
  349. // printf("\n");
  350. // display_flag = RIGHT_FLAG;
  351. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  352. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  353. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  354. // printf("msg_id =%X\r\n",msg_id);
  355. return msg_id;
  356. }
  357. static int terminal_number_func(const LORA_DATA_T *buf)
  358. {
  359. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  360. memcpy(terminal_info.terminal_number, buf->data_buf + DATA_START_LEN, len);
  361. // memcpy(&Machine_info.terminal_number,&terminal_info.terminal_number,len);
  362. memset(Machine_info.terminal_number, 0x00, sizeof(Machine_info.terminal_number)); // 清除终端名称信息
  363. uincode2gbk((char *)terminal_info.terminal_number, len, (char *)Machine_info.terminal_number);
  364. // printf("unicode:");
  365. // for(int i=0;i<len;i++)
  366. // {
  367. // printf("%02x ",terminal_info.terminal_number[i]);
  368. // }
  369. // printf("\n");
  370. // printf("gbk:");
  371. // for(int i=0;i<len;i++)
  372. // {
  373. // printf("%02x ",Machine_info.terminal_number[i]);
  374. // }
  375. printf("setting num %s", Machine_info.terminal_number);
  376. printf("\n");
  377. // display_flag = RIGHT_FLAG;
  378. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  379. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  380. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  381. // printf("msg_id =%X\r\n",msg_id);
  382. return msg_id;
  383. }
  384. static int electric_quantity_func(const LORA_DATA_T *buf)
  385. {
  386. terminal_info.batt_percent = buf->data_buf[DATA_START_LEN];
  387. ESP_LOGI(LOG_TAG, "batt_percent : %d", terminal_info.batt_percent);
  388. // display_flag = RIGHT_FLAG;
  389. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  390. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  391. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  392. // printf("msg_id =%X\r\n",msg_id);
  393. return msg_id;
  394. }
  395. static int signal_strength_func(const LORA_DATA_T *buf)
  396. {
  397. // terminal_info.rssi = buf->data_buf[DATA_START_LEN];
  398. // ESP_LOGI(LOG_TAG,"rssi : %d",terminal_info.rssi);
  399. // Machine_info.rssi = terminal_info.rssi;
  400. // display_flag = RIGHT_FLAG;
  401. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  402. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  403. // printf("msg_id =%X\r\n",msg_id);
  404. return msg_id;
  405. }
  406. static int screen_refresh_cycle_func(const LORA_DATA_T *buf)
  407. {
  408. terminal_info.refresh_cycle = buf->data_buf[DATA_START_LEN];
  409. ESP_LOGI(LOG_TAG, "refresh_cycle : %d", terminal_info.refresh_cycle);
  410. Machine_info.refresh_cycle = terminal_info.refresh_cycle;
  411. // display_flag = RIGHT_FLAG;
  412. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  413. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  414. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  415. // printf("msg_id =%X\r\n",msg_id);
  416. return msg_id;
  417. }
  418. static int screen_left_func(const LORA_DATA_T *buf)
  419. {
  420. terminal_info.left_epd = buf->data_buf[DATA_START_LEN];
  421. ESP_LOGI(LOG_TAG, "left_epd : %d", terminal_info.left_epd);
  422. Machine_info.left_display_mode = terminal_info.left_epd;
  423. // display_flag = LEFT_FLAG;
  424. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  425. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  426. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  427. // printf("msg_id =%X\r\n",msg_id);
  428. return msg_id;
  429. }
  430. static int screen_right_func(const LORA_DATA_T *buf)
  431. {
  432. terminal_info.right_epd = buf->data_buf[DATA_START_LEN];
  433. Machine_info.right_display_mode = terminal_info.right_epd;
  434. ESP_LOGW(LOG_TAG, "-----right_epd : %d", terminal_info.right_epd);
  435. // display_flag = RIGHT_FLAG;
  436. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  437. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  438. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  439. // printf("msg_id =%X\r\n",msg_id);
  440. return msg_id;
  441. }
  442. static int rsponsible_person_change_func(const LORA_DATA_T *buf)
  443. {
  444. int len = ((buf->data_buf[USE_DATA_LEN_INDEX]) << 8) | ((buf->data_buf[USE_DATA_LEN_INDEX + 1]));
  445. // for(int i = 0;i<buf->data_len;i++)
  446. // {
  447. // printf("%02X",buf->data_buf[i]);
  448. // }
  449. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  450. ESP_LOGW(LOG_TAG, "tmp name len = %d", len);
  451. memcpy(terminal_info.rsponsible_person_change, buf->data_buf + DATA_START_LEN, len);
  452. // ESP_LOGI(LOG_TAG,"mode:[%02x] len = %d",terminal_info.rsponsible_person_change[2],len);
  453. uint16_t tmp_number;
  454. uint8_t tmp_type;
  455. if (len == 3)
  456. {
  457. return msg_id;
  458. }
  459. else
  460. {
  461. char *tmp_name;
  462. tmp_name = (char *)malloc((len - 3 + 1));
  463. memset(tmp_name, 0x00, (len - 3 + 1));
  464. uincode2gbk((char *)terminal_info.rsponsible_person_change + 3, (len - 3), tmp_name);
  465. // printf("tmp_name = %s\r\n",tmp_name);
  466. tmp_name[len - 2] = '\0';
  467. // memcpy(tmp_name,(char*)terminal_info.rsponsible_person_change+3,len-3);
  468. #if 0
  469. for(int i=0;i<strlen(tmp_name);i++)
  470. {
  471. printf("%02x ",tmp_name[i]);
  472. }
  473. printf("\n%d\n",strlen(tmp_name));
  474. #endif
  475. tmp_number = (terminal_info.rsponsible_person_change[0] << 8) | (terminal_info.rsponsible_person_change[1] & 0xFF);
  476. tmp_type = terminal_info.rsponsible_person_change[2];
  477. #if 0
  478. ListNode *p = newListNode(tmp_number,tmp_type,tmp_name);
  479. insertNode_head(list_head,p);
  480. #else
  481. memset(Machine_info.person[tmp_type].string_name, 0x00, sizeof(Machine_info.person[tmp_type].string_name));
  482. person_add(tmp_type, tmp_number, tmp_name, Machine_info.person[tmp_type].string_name);
  483. ESP_LOGE(LOG_TAG, "add name = %s\r\n", Machine_info.person[tmp_type].string_name);
  484. #endif
  485. // display_flag = RIGHT_FLAG;
  486. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  487. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  488. // printf("msg_id =%X\r\n",msg_id);
  489. free(tmp_name);
  490. tmp_name = NULL;
  491. }
  492. return msg_id;
  493. }
  494. static int rsponsible_person_delete_func(const LORA_DATA_T *buf)
  495. {
  496. uint16_t tmp_number;
  497. uint8_t tmp_type;
  498. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  499. memcpy(terminal_info.rsponsible_person_delete, buf->data_buf + DATA_START_LEN, len);
  500. tmp_number = (terminal_info.rsponsible_person_delete[0] << 8) | (terminal_info.rsponsible_person_delete[1] & 0xFF);
  501. tmp_type = terminal_info.rsponsible_person_delete[2];
  502. #if 1
  503. int num = person_get_num(tmp_type); // 获取当前类型人员数量
  504. int is_exist = person_get_num_is_exist(tmp_type, tmp_number);
  505. if (is_exist)
  506. {
  507. memset(Machine_info.person[tmp_type].string_name, 0x00, sizeof(Machine_info.person[tmp_type].string_name));
  508. person_del(tmp_type, tmp_number, NULL, Machine_info.person[tmp_type].string_name);
  509. }
  510. #endif
  511. // display_flag = RIGHT_FLAG;
  512. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  513. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  514. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  515. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  516. // printf("msg_id =%X\r\n",msg_id);
  517. return msg_id;
  518. }
  519. // #define SET_BIT(flag,_bit) (flag |= (0x01 << _bit))
  520. // #define CLE_BIT(flag,_bit) (flag &= ~(0x01 << _bit))
  521. static int production_punch_func(const LORA_DATA_T *buf)
  522. {
  523. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  524. memcpy(terminal_info.production_punch, buf->data_buf + DATA_START_LEN, len);
  525. ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.production_punch, len);
  526. if (terminal_info.production_punch[1] == 0)
  527. {
  528. Machine_info.checkIn_set[terminal_info.production_punch[0]].checkIn_close = true; // 关闭该类型打卡
  529. }
  530. else
  531. {
  532. Machine_info.checkIn_set[terminal_info.production_punch[0]].checkIn_close = false; // 开启该类型打卡
  533. }
  534. ESP_LOGW(LOG_TAG, "打卡设置: 类型[%d] %s", terminal_info.production_punch[0], terminal_info.production_punch[1] ? "开启" : "关闭");
  535. if (len > 2) // 数据长度大于2 ,说明设置了别名
  536. {
  537. memset(Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name, 0, 10);
  538. // memcpy(Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name,terminal_info.production_punch+2,len-2);
  539. uincode2gbk((char *)terminal_info.production_punch + 2, len - 2, (char *)Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name);
  540. }
  541. // display_flag = RIGHT_FLAG;
  542. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  543. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  544. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  545. return msg_id;
  546. }
  547. static int personnel_check_in_func(const LORA_DATA_T *buf)
  548. {
  549. // ESP_LOGI(LOG_TAG,"check in : %s,person type : 0x%02x",buf->data_buf[DATA_START_LEN+1] ? "OUT":"IN",buf->data_buf[DATA_START_LEN]);
  550. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  551. memcpy(terminal_info.personnel_check_in, buf->data_buf + DATA_START_LEN, len);
  552. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  553. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  554. // printf("msg_id =%X\r\n",msg_id);
  555. // display_flag = RIGHT_FLAG;
  556. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  557. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  558. ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.personnel_check_in, len);
  559. int ID_crc = crc8(&terminal_info.personnel_check_in[2], len - 2, 0x5a, 0x51); // lora_crc8( , ,0x5a,0x51);暂定对人员id求crc
  560. ESP_LOGW(LOG_TAG, "打卡类型[%d] %s ,ID_crc = %d", terminal_info.personnel_check_in[0], Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].checkIn_close ? "关闭" : "开启", ID_crc);
  561. if (!(Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].checkIn_close)) // 开启打卡
  562. {
  563. if (terminal_info.personnel_check_in[1] == 0x00) // 打卡签入
  564. {
  565. // Machine_info.personnel_check_in[terminal_info.personnel_check_in[0]]++;
  566. // Node *find = findNode(Send_list,msg_id);
  567. Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].number++;
  568. Node *find = findNode(clockIn_list, ID_crc);
  569. // printList(clockIn_list);
  570. if (find == NULL)
  571. {
  572. Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].real_number++;
  573. clockIn_list = postInsert(clockIn_list, ID_crc, terminal_info.personnel_check_in[0], (&terminal_info.personnel_check_in) + 2, len - 2);
  574. }
  575. else
  576. {
  577. ESP_LOGE(LOG_TAG, "该人员已存在");
  578. }
  579. }
  580. else if (terminal_info.personnel_check_in[1] == 0x01) // check out
  581. {
  582. Node *find = findNode(clockIn_list, ID_crc);
  583. if (find != NULL)
  584. {
  585. clockIn_list = deleteNode(clockIn_list, ID_crc);
  586. Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].real_number--;
  587. }
  588. }
  589. else
  590. {
  591. ESP_LOGE(LOG_TAG, "err:not in or out");
  592. }
  593. }
  594. // printList(clockIn_list);
  595. return msg_id;
  596. }
  597. uint8_t change_btn_reversal(uint8_t reversal_btn);
  598. static void set_button_anotherName(void) // cmd 0x0d操作
  599. {
  600. uint8_t tmp = change_btn_reversal(terminal_info.status_setting[0]);
  601. if ((terminal_info.status_setting[0] == 0) && (terminal_info.status_setting[1] == 0))
  602. { //[0]:状态 [1]:开关 ————>运行不能关闭,这两位都为0,说明没有收到信息
  603. // ESP_LOGW(LOG_TAG,"默认按键");
  604. return; // 没有cmd[0x0d]的lora信息
  605. }
  606. if (terminal_info.status_setting[1] == 0x00) // close
  607. {
  608. Machine_info.btn_dis_flag[tmp - 1] = false;
  609. ESP_LOGE(LOG_TAG, "按键状态关闭,清空按键文字 状态%d", tmp);
  610. switch (terminal_info.status_setting[0]) // 按键状态关闭,清空按键文字
  611. {
  612. case 0x00:
  613. memset(Machine_info.btn_operation, 0, 6);
  614. break;
  615. case 0x01:
  616. memset(Machine_info.btn_waitMaterials_info, 0, 6);
  617. break;
  618. case 0x02:
  619. memset(Machine_info.btn_safeKeep_info, 0, 6);
  620. break;
  621. case 0x03:
  622. memset(Machine_info.btn_breakDown_info, 0, 6);
  623. break;
  624. case 0x04:
  625. memset(Machine_info.btn_upKeep_info, 0, 6);
  626. break;
  627. case 0x05:
  628. memset(Machine_info.btn_shutDown_info, 0, 6);
  629. break;
  630. default:
  631. ESP_LOGE(LOG_TAG, "err:按键状态%d", __LINE__);
  632. break;
  633. }
  634. }
  635. else if (terminal_info.status_setting[1] == 0x01) // open
  636. {
  637. Machine_info.btn_dis_flag[tmp - 1] = true;
  638. // ESP_LOGW(LOG_TAG,"别名:[%02x %02x %02x %02x]",terminal_info.status_setting[2],terminal_info.status_setting[3],terminal_info.status_setting[4],terminal_info.status_setting[5]);
  639. if ((terminal_info.status_setting[2] == 0x00) && (terminal_info.status_setting[3] == 0x00))
  640. { // 别名为两个字,这判断第一个字为空,为空就赋值初始化的按键信息
  641. ESP_LOGE(LOG_TAG, "button is open[other name is NULL] %d", terminal_info.status_setting[0]);
  642. switch (terminal_info.status_setting[0])
  643. {
  644. case 0x00: // 运行
  645. printf("clear btn_operation display\r\n");
  646. memcpy(Machine_info.btn_operation, str_operation, 6);
  647. break;
  648. case 0x01: // 待料
  649. printf("clear btn_waitMaterials_info display\r\n");
  650. memcpy(Machine_info.btn_waitMaterials_info, str_waitMaterials, 6);
  651. break;
  652. case 0x02: // 封存
  653. printf("clear btn_safeKeep_info display\r\n");
  654. memcpy(Machine_info.btn_safeKeep_info, str_safeKeep, 6);
  655. break;
  656. case 0x03: // 故障
  657. printf("clear btn_breakDown_info display\r\n");
  658. memcpy(Machine_info.btn_breakDown_info, str_breakDown, 6);
  659. break;
  660. case 0x04: // 保养
  661. printf("clear btn_upKeep_info display\r\n");
  662. memcpy(Machine_info.btn_upKeep_info, str_upKeep, 6);
  663. break;
  664. case 0x05: // 停机
  665. printf("clear btn_shutDown_info display\r\n");
  666. memcpy(Machine_info.btn_shutDown_info, str_shutDown, 6);
  667. break;
  668. default:
  669. printf("other btn_operation display\r\n");
  670. ESP_LOGE(LOG_TAG, "err:按键状态%d", __LINE__);
  671. break;
  672. }
  673. }
  674. else
  675. {
  676. // ESP_LOGW(LOG_TAG,"button is open[other name] %d",terminal_info.status_setting[0]);
  677. switch (terminal_info.status_setting[0])
  678. {
  679. case 0x00:
  680. memset(Machine_info.btn_operation, 0, 6);
  681. uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_operation);
  682. // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_operation,6);
  683. break;
  684. case 0x01:
  685. memset(Machine_info.btn_waitMaterials_info, 0, 6);
  686. uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_waitMaterials_info);
  687. // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_waitMaterials_info,6);
  688. break;
  689. case 0x02:
  690. memset(Machine_info.btn_safeKeep_info, 0, 6);
  691. uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_safeKeep_info);
  692. // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_safeKeep_info,6);
  693. break;
  694. case 0x03:
  695. memset(Machine_info.btn_breakDown_info, 0, 6);
  696. uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_breakDown_info);
  697. // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_breakDown_info,6);
  698. break;
  699. case 0x04:
  700. memset(Machine_info.btn_upKeep_info, 0, 6);
  701. uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_upKeep_info);
  702. // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_upKeep_info,6);
  703. break;
  704. case 0x05:
  705. memset(Machine_info.btn_shutDown_info, 0, 6);
  706. uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_shutDown_info);
  707. // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_shutDown_info,6);
  708. break;
  709. default:
  710. ESP_LOGE(LOG_TAG, "err:按键状态%d", __LINE__);
  711. break;
  712. }
  713. }
  714. }
  715. else
  716. {
  717. ESP_LOGE(LOG_TAG, "not open or close");
  718. }
  719. }
  720. uint8_t change_btn(uint8_t btn);
  721. static int status_setting_func(const LORA_DATA_T *buf)
  722. {
  723. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  724. memset(terminal_info.status_setting, 0x00, sizeof(terminal_info.status_setting));
  725. memcpy(terminal_info.status_setting, buf->data_buf + DATA_START_LEN, len); // 别名
  726. // ESP_LOGE(LOG_TAG,"len = %d ",len);
  727. ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.status_setting, 10);
  728. set_button_anotherName();
  729. // 当前按键状态关闭了
  730. if ((change_btn(Machine_info.left_state) == terminal_info.status_setting[0]) && (Machine_info.btn_dis_flag[Machine_info.left_state - 1] == false))
  731. {
  732. ESP_LOGE(LOG_TAG, "按键发送");
  733. send_button_key_queue(STATE_OPERATION);
  734. }
  735. else
  736. {
  737. // display_flag = LEFT_FLAG;
  738. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  739. }
  740. ESP_LOGW(LOG_TAG, "按键 :%s %s %s %s %s %s",
  741. Machine_info.btn_dis_flag[0] ? "开启" : "关闭",
  742. Machine_info.btn_dis_flag[1] ? "开启" : "关闭",
  743. Machine_info.btn_dis_flag[2] ? "开启" : "关闭",
  744. Machine_info.btn_dis_flag[3] ? "开启" : "关闭",
  745. Machine_info.btn_dis_flag[4] ? "开启" : "关闭",
  746. Machine_info.btn_dis_flag[5] ? "开启" : "关闭");
  747. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  748. return msg_id;
  749. }
  750. static int status_duration_func(const LORA_DATA_T *buf)
  751. {
  752. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  753. memcpy(terminal_info.status_duration, buf->data_buf + DATA_START_LEN, len);
  754. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  755. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  756. // printf("msg_id =%X\r\n",msg_id);
  757. return msg_id;
  758. }
  759. static int update_and_duration_func(const LORA_DATA_T *buf)
  760. {
  761. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  762. memcpy(terminal_info.update_and_duration, buf->data_buf + DATA_START_LEN, len);
  763. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  764. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  765. // printf("msg_id =%X\r\n",msg_id);
  766. return msg_id;
  767. }
  768. static int announcement_func(const LORA_DATA_T *buf)
  769. {
  770. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  771. memcpy(terminal_info.announcement, buf->data_buf + DATA_START_LEN, len); // 拷贝公告
  772. ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.announcement, len);
  773. ESP_LOGW(LOG_TAG, "设置公告");
  774. // memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
  775. memset(Machine_info.announcement, 0x00, sizeof(Machine_info.announcement)); // 清除终端公告信息
  776. uincode2gbk((char *)terminal_info.announcement, len, (char *)Machine_info.announcement);
  777. // printf("unicode:");
  778. // for(i=0;i<len;i++)
  779. // {
  780. // printf("%02x ",terminal_info.terminal_name[i]);
  781. // }
  782. // printf("\n");
  783. // printf("gbk:");
  784. // for(i=0;i<len;i++)
  785. // {
  786. // printf("%02x ",Machine_info.terminal_name[i]);
  787. // }
  788. // printf("setting gong gao %s",Machine_info.announcement);
  789. // printf("\n");
  790. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  791. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  792. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  793. // printf("msg_id =%X\r\n",msg_id);
  794. return msg_id;
  795. }
  796. void set_statisticsData(uint8_t *data)
  797. {
  798. // memset(&Machine_info.num_goodProducts,0,32);
  799. // memset(&Machine_info.num_badProducts,0,32);
  800. #define __DATA_SIZE 24
  801. for (int i = 0; i < 8; i++)
  802. {
  803. Machine_info.num_goodProducts[i] = (data[0 + i * __DATA_SIZE] << 24) | (data[1 + i * __DATA_SIZE] << 16) | (data[2 + i * __DATA_SIZE] << 8) | (data[3 + i * __DATA_SIZE]);
  804. Machine_info.num_badProducts[i] = (data[4 + i * __DATA_SIZE] << 24) | (data[5 + i * __DATA_SIZE] << 16) | (data[6 + i * __DATA_SIZE] << 8) | (data[7 + i * __DATA_SIZE]);
  805. Machine_info.scale_UR_int[i] = (data[8 + i * __DATA_SIZE]);
  806. Machine_info.scale_UR_dec[i] = (data[9 + i * __DATA_SIZE]);
  807. Machine_info.scale_YR_int[i] = (data[10 + i * __DATA_SIZE]);
  808. Machine_info.scale_YR_dec[i] = (data[11 + i * __DATA_SIZE]);
  809. Machine_info.num_manHour[i] = (data[12 + i * __DATA_SIZE] << 8) | (data[13 + i * __DATA_SIZE]);
  810. Machine_info.num_people[i] = (data[14 + i * __DATA_SIZE] << 8) | (data[15 + i * __DATA_SIZE]);
  811. Machine_info.num_production[i] = (data[16 + i * __DATA_SIZE] << 8) | (data[17 + i * __DATA_SIZE]);
  812. Machine_info.num_repair[i] = (data[18 + i * __DATA_SIZE] << 8) | (data[19 + i * __DATA_SIZE]);
  813. Machine_info.num_inspection[i] = (data[20 + i * __DATA_SIZE] << 8) | (data[21 + i * __DATA_SIZE]);
  814. Machine_info.num_upkeep[i] = (data[22 + i * __DATA_SIZE] << 8) | (data[23 + i * __DATA_SIZE]);
  815. // ESP_LOGW(LOG_TAG,"good:[%ld] bad[%ld]",Machine_info.num_goodProducts[i],Machine_info.num_badProducts[i]);
  816. }
  817. }
  818. static int capacity_statistics_func(const LORA_DATA_T *buf)
  819. {
  820. #if 1
  821. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  822. memcpy(terminal_info.capacity_statistics, buf->data_buf + DATA_START_LEN, len);
  823. #endif
  824. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  825. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  826. // printf("msg_id =%X\r\n",msg_id);
  827. set_statisticsData(terminal_info.capacity_statistics);
  828. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  829. return msg_id;
  830. }
  831. static int responsible_person_synchronize_info_func(const LORA_DATA_T *buf)
  832. {
  833. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  834. memcpy(terminal_info.responsible_person_synchronize_info, buf->data_buf + DATA_START_LEN, len);
  835. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  836. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  837. // printf("msg_id =%X\r\n",msg_id);
  838. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  839. return msg_id;
  840. }
  841. static int status_led_func(const LORA_DATA_T *buf)
  842. {
  843. terminal_info.states_led = buf->data_buf[DATA_START_LEN];
  844. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  845. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  846. // printf("msg_id =%X\r\n",msg_id);
  847. return msg_id;
  848. }
  849. static int logo_led_func(const LORA_DATA_T *buf)
  850. {
  851. terminal_info.logo_led = buf->data_buf[DATA_START_LEN];
  852. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  853. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  854. // printf("msg_id =%X\r\n",msg_id);
  855. return msg_id;
  856. }
  857. static int battery_temperature_func(const LORA_DATA_T *buf)
  858. {
  859. terminal_info.batt_temperature[0] = buf->data_buf[DATA_START_LEN];
  860. terminal_info.batt_temperature[1] = buf->data_buf[DATA_START_LEN + 1];
  861. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  862. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  863. // printf("msg_id =%X\r\n",msg_id);
  864. return msg_id;
  865. }
  866. static int station_name_func(const LORA_DATA_T *buf)
  867. {
  868. #define FONT_MAX_LEN 8
  869. uint8_t font_num = 0; // 获取当前的文字个数
  870. int i = 0;
  871. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  872. memset(terminal_info.station_name, 0, 32);
  873. memcpy(terminal_info.station_name, buf->data_buf + DATA_START_LEN, len);
  874. // memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
  875. memset(Machine_info.station_name, 0x00, sizeof(Machine_info.station_name)); // 清除终端名称信息
  876. font_num = len / 2;
  877. if (font_num <= FONT_MAX_LEN)
  878. {
  879. uincode2gbk((char *)terminal_info.station_name, len, (char *)Machine_info.station_name);
  880. }
  881. else
  882. {
  883. uincode2gbk((char *)terminal_info.station_name, FONT_MAX_LEN * 2, (char *)Machine_info.station_name);
  884. strcat((char *)Machine_info.station_name, "...");
  885. }
  886. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  887. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  888. return msg_id;
  889. }
  890. static int station_number_func(const LORA_DATA_T *buf)
  891. {
  892. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  893. memcpy(terminal_info.station_number, buf->data_buf + DATA_START_LEN, len);
  894. memset(Machine_info.station_number, 0x00, sizeof(Machine_info.station_number));
  895. uincode2gbk((char *)terminal_info.station_number, len, (char *)Machine_info.station_number);
  896. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  897. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  898. return msg_id;
  899. }
  900. static int responsible_person_setup_func(const LORA_DATA_T *buf)
  901. {
  902. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  903. ESP_LOG_BUFFER_HEX(LOG_TAG, buf->data_buf + DATA_START_LEN, len);
  904. ESP_LOGE(LOG_TAG, "设置责任人 类型[%d]%s", buf->data_buf[DATA_START_LEN], buf->data_buf[DATA_START_LEN + 1] ? "开启" : "关闭");
  905. if (buf->data_buf[DATA_START_LEN + 1] == 0x01) // 开
  906. {
  907. Machine_info.person[buf->data_buf[DATA_START_LEN]].Charge_close = false;
  908. memset(Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name, 0, 16);
  909. uincode2gbk(buf->data_buf + DATA_START_LEN + 2, len - 2, (char *)Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name);
  910. // ESP_LOG_BUFFER_HEX(LOG_TAG,Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name,len);
  911. }
  912. else if (buf->data_buf[DATA_START_LEN + 1] == 0x00) // 关
  913. {
  914. Machine_info.person[buf->data_buf[DATA_START_LEN]].Charge_close = true;
  915. }
  916. ESP_LOGE(LOG_TAG, "Charge_close %d %d %d %d %d", Machine_info.person[0].Charge_close, Machine_info.person[1].Charge_close, Machine_info.person[2].Charge_close,
  917. Machine_info.person[3].Charge_close, Machine_info.person[4].Charge_close);
  918. // display_flag = RIGHT_FLAG;
  919. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  920. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  921. return msg_id;
  922. }
  923. static int system_bulletin_func(const LORA_DATA_T *buf)
  924. {
  925. ESP_LOGE(LOG_TAG, "system_bulletin_func");
  926. int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
  927. memcpy(terminal_info.systemMessage, buf->data_buf + DATA_START_LEN, len);
  928. memset(Machine_info.systemMessage, 0x00, sizeof(Machine_info.systemMessage));
  929. uincode2gbk((char *)terminal_info.systemMessage, len, (char *)Machine_info.systemMessage);
  930. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  931. return msg_id;
  932. }
  933. static int hardware_update_func(const LORA_DATA_T *buf)
  934. {
  935. // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  936. int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
  937. // printf("msg_id =%X\r\n",msg_id);
  938. return msg_id;
  939. }
  940. /*硬件回复命令ACK*/ /*长度 返回result结果及长度*/
  941. /*********************************************************************************
  942. * function : reply_ack_func
  943. * Description : 回复ACK cmd 指定命令
  944. * Input : cmd 命令 msg_id 当前的ID号 用于网关删除自身的已经接受到ACK 的数据 result 返回当前的数据
  945. * Output :
  946. * Author : 祁鑫 Data : 2023 8.12
  947. **********************************************************************************/
  948. int reply_ack_func(int cmd, int msg_id, bool is_send, uint8_t *result)
  949. {
  950. uint8_t strlorabuf[50] = {0};
  951. uint8_t mac[6] = {0};
  952. strlorabuf[0] = 0x5A;
  953. strlorabuf[1] = 0x51;
  954. strlorabuf[2] = 0x00; // crc
  955. // int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
  956. for (int i = 0; i < 6; i++)
  957. {
  958. strlorabuf[3 + i] = Machine_info.gateway_mac[i]; // 网关的mac地址
  959. }
  960. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  961. strlorabuf[10] = cmd; // 命令
  962. strlorabuf[11] = (msg_id >> 8) & 0xff; // 总包
  963. strlorabuf[12] = msg_id & 0xff; // 当前包
  964. strlorabuf[13] = 0x00; // 当前长度
  965. strlorabuf[14] = PROTOCOL_RSPONSIBLE_ACK; // ACK命令字段
  966. #if 0
  967. //设备cid长度
  968. memcpy(&strlorabuf[15],cid,cid_len); //拷贝cid
  969. strlorabuf[13] = cid_len+1; //重新设置当前当前长度
  970. #endif
  971. int crc = 0; // 计算crc
  972. for (int i = 0; i < sizeof(strlorabuf); i++)
  973. {
  974. crc += strlorabuf[i];
  975. }
  976. strlorabuf[2] = crc;
  977. if (result != NULL)
  978. {
  979. memcpy(result, strlorabuf, 15);
  980. }
  981. if (is_send)
  982. {
  983. lora_send_data((char *)strlorabuf, 15);
  984. uart_wait_tx_idle_polling(UART_NUM_1);
  985. }
  986. return 15;
  987. }
  988. /*硬件配网回复*/ // 参数cid cid长度 返回result 结果及长度
  989. int reply_gwpair_ack_func(uint8_t *cid, uint8_t cid_len, uint8_t *result)
  990. {
  991. uint8_t strlorabuf[50] = {0};
  992. uint8_t mac[6] = {0};
  993. strlorabuf[0] = 0x5A;
  994. strlorabuf[1] = 0x51;
  995. strlorabuf[2] = 0x00; // crc
  996. // int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
  997. for (int i = 0; i < 6; i++)
  998. {
  999. strlorabuf[3 + i] = Machine_info.gateway_mac[i]; // 网关的mac地址
  1000. }
  1001. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  1002. strlorabuf[10] = 0x80; // 配对命令
  1003. strlorabuf[11] = 0x01; // 总包
  1004. strlorabuf[12] = 0x01; // 当前包
  1005. strlorabuf[13] = 0x00; // 当前长度
  1006. strlorabuf[14] = 0x01; // 请求成功
  1007. // 设备cid长度
  1008. memcpy(&strlorabuf[15], cid, cid_len); // 拷贝cid
  1009. strlorabuf[13] = cid_len + 1; // 重新设置当前当前长度
  1010. int crc = 0; // 计算crc
  1011. for (int i = 0; i < sizeof(strlorabuf); i++)
  1012. {
  1013. crc += strlorabuf[i];
  1014. }
  1015. strlorabuf[2] = crc;
  1016. memcpy(result, strlorabuf, 14 + cid_len + 1);
  1017. return 14 + cid_len + 1;
  1018. }
  1019. /*硬件配网*/
  1020. static void hardware_gwpair_func(const LORA_DATA_T *buf)
  1021. {
  1022. uint8_t child_mac[6] = {0}; // 子设备的mac地址
  1023. uint8_t gateway_mac[6] = {0}; // 网关的组内编号
  1024. int child_num = 0;
  1025. int gateway_num = 0;
  1026. #if 0
  1027. for(int i = 0;i<buf->data_len;i++)
  1028. {
  1029. printf("%02X",buf->data_buf[i]);
  1030. }
  1031. #endif
  1032. uint64_t time = 0x000000000000LL;
  1033. // printf("1\r\n");
  1034. memcpy(child_mac, &buf->data_buf[3], 6);
  1035. // printf("2\r\n");
  1036. gateway_num = buf->data_buf[9];
  1037. // printf("3\r\n");
  1038. memcpy(gateway_mac, &buf->data_buf[14], 6);
  1039. // printf("4\r\n");
  1040. child_num = buf->data_buf[20];
  1041. // printf("5\r\n");
  1042. // printf("\r\n");
  1043. // for(int i = 21;i<27;i++)
  1044. // {
  1045. // printf("%02X",buf->data_buf[i]);
  1046. // }
  1047. #if 0
  1048. #include <inttypes.h>
  1049. time|=(buf->data_buf[26]<<0);
  1050. //printf("time = %" PRIX64 "\r\n",time);
  1051. time|=(buf->data_buf[25]<<8);
  1052. //printf("time = %" PRIX64 "\r\n",time);
  1053. time|=(buf->data_buf[24]<<16);
  1054. //printf("time = %" PRIX64 "\r\n",time);
  1055. time|=(buf->data_buf[23]<<24);
  1056. //printf("time = %" PRIX64 "\r\n",time);
  1057. #if 0
  1058. uint64_t temp = 0;
  1059. temp |=(buf->data_buf[22]<<32);
  1060. temp |=(buf->data_buf[21]<<40);
  1061. printf("temp = %" PRIX64 "\r\n",temp);
  1062. #endif
  1063. #if 1
  1064. int64_t temp = 0;
  1065. temp = buf->data_buf[22];
  1066. time|=(temp<<32);//(buf->data_buf[22]<<32);
  1067. #else
  1068. time|=temp;
  1069. #endif
  1070. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1071. #if 0
  1072. time|=(buf->data_buf[21]<<40);
  1073. #else
  1074. temp = 0;
  1075. temp = buf->data_buf[21];
  1076. time|=(temp<<40);//(buf->data_buf[22]<<32);
  1077. //time|=(buf->data_buf[21]<<40);
  1078. #endif
  1079. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1080. //uint64_t value = 0x018B1E3D24AC;
  1081. //printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
  1082. //printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
  1083. #if 0
  1084. time=(buf->data_buf[21]<<40)+
  1085. (buf->data_buf[22]<<32)+
  1086. (buf->data_buf[23]<<24)+
  1087. (buf->data_buf[24]<<16)+
  1088. (buf->data_buf[25]<<8) +
  1089. (buf->data_buf[26]<<0);
  1090. #endif
  1091. #else
  1092. Machine_info.lora_new_channel = buf->data_buf[21];
  1093. printf("Machine_info.lora_new_channel =%d\r\n", Machine_info.lora_new_channel);
  1094. memcpy(Machine_info.timestamp, &buf->data_buf[22], 13);
  1095. printf("Machine_info.timestamp =%s\r\n", Machine_info.timestamp);
  1096. int timezone_offset = 8; // 中国时区偏移为UTC+8,你可以根据需要设置不同的时区偏移
  1097. Machine_info.time_offset = timezone_offset;
  1098. timestamp_to_local_time(Machine_info.timestamp,
  1099. Machine_info.time_offset,
  1100. &Machine_info.year,
  1101. &Machine_info.month,
  1102. &Machine_info.day,
  1103. &Machine_info.hour,
  1104. &Machine_info.min,
  1105. &Machine_info.sec);
  1106. setRtcTime(Machine_info.year, Machine_info.month, Machine_info.day,
  1107. Machine_info.hour, Machine_info.min, Machine_info.sec);
  1108. // 更新当前状态时间
  1109. Machine_info.current_button.Year = Machine_info.year;
  1110. Machine_info.current_button.Month = Machine_info.month;
  1111. Machine_info.current_button.Day = Machine_info.day;
  1112. Machine_info.current_button.Hour = Machine_info.hour;
  1113. Machine_info.current_button.Minute = Machine_info.min;
  1114. Machine_info.current_button.Second = Machine_info.sec;
  1115. Machine_info.last_button.Year = Machine_info.year;
  1116. Machine_info.last_button.Month = Machine_info.month;
  1117. Machine_info.last_button.Day = Machine_info.day;
  1118. Machine_info.last_button.Hour = Machine_info.hour;
  1119. Machine_info.last_button.Minute = Machine_info.min;
  1120. Machine_info.last_button.Second = Machine_info.sec;
  1121. #endif
  1122. printf("gateway mac = ");
  1123. for (int i = 0; i < 6; i++)
  1124. printf("%02X", gateway_mac[i]);
  1125. printf("\r\n");
  1126. printf("gateway num = %d\r\n", gateway_num);
  1127. printf("child mac = ");
  1128. for (int i = 0; i < 6; i++)
  1129. printf("%02X", child_mac[i]);
  1130. printf("\r\n");
  1131. printf("child_num num = %d\r\n", child_num);
  1132. printf("time = %llu\r\n", time);
  1133. printf("time = %llx\r\n", time);
  1134. //(3000 - (480 - 1*100))
  1135. #include "user_sleep.h"
  1136. #include "esp_sleep.h"
  1137. // 配网完成设置休眠唤醒时间
  1138. if (Machine_info.eflagID == 0xff)
  1139. {
  1140. #if 0
  1141. printf("sleep time = %d ms\r\n",(TIMER_WAKEUP_TIME_US*child_num)/1000);
  1142. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*child_num); //配置当前休眠的唤醒时间
  1143. #else
  1144. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US); // 配置当前休眠的唤醒时间
  1145. #endif
  1146. }
  1147. Machine_info.eflagID = child_num; // 保存当前的子设备组内编号
  1148. memcpy(Machine_info.gateway_mac, gateway_mac, 6);
  1149. // 配网成功 回复 ACK 设置新的信道
  1150. Machine_info.paired = 1; // 已配对
  1151. #if 0
  1152. send_pair_ack();
  1153. #else
  1154. int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除
  1155. printf("msg_id =%X\r\n", msg_id);
  1156. reply_ack_func(PROTOCOL_HARDWARE_GWPAIRED, msg_id, true, NULL); // 回复ack
  1157. #endif
  1158. // vTaskDelay(200 / portTICK_PERIOD_MS);
  1159. // //修改信道前修改设备ID 及 设备ID
  1160. // dymatic_change_device_id(child_num+1);
  1161. // dymatic_change_dst_device_id(0x00000001);
  1162. // //切换信道
  1163. // dymatic_change_chanel(Machine_info.lora_new_channel); //切换信道
  1164. // lora_get_rssi();
  1165. vTaskDelay(200 / portTICK_PERIOD_MS);
  1166. Machine_info.rssi = set_lora(Machine_info.lora_new_channel, Machine_info.eflagID);
  1167. f_send_version();
  1168. f_send_reply_status(0x01); // 回复在线状态
  1169. send_update_status();
  1170. // f_send_lora_rssi(Machine_info.rssi);
  1171. f_send_lora_rssi(100); // 刚配完直接发100
  1172. f_send_battary_vaule(Machine_info.batt_precent);
  1173. #if USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  1174. sleep_timer_start(2000); // 进入睡眠
  1175. #endif
  1176. // printf("start timer id = %d,%d\r\n",Machine_info.eflagID,Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  1177. // Already_send_timer_start(Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  1178. }
  1179. /*硬件取消配网*/
  1180. static void hardware_ungwpair_func(const LORA_DATA_T *buf)
  1181. {
  1182. uint8_t child_mac[6] = {0}; // 子设备的mac地址
  1183. uint8_t gateway_mac[6] = {0}; // 网关的组内编号
  1184. int child_num = 0;
  1185. int gateway_num = 0;
  1186. uint64_t time = 0x000000000000LL;
  1187. memcpy(child_mac, &buf->data_buf[3], 6);
  1188. gateway_num = buf->data_buf[9];
  1189. memcpy(gateway_mac, &buf->data_buf[14], 6);
  1190. child_num = buf->data_buf[20];
  1191. #include <inttypes.h>
  1192. time |= (buf->data_buf[26] << 0);
  1193. time |= (buf->data_buf[25] << 8);
  1194. time |= (buf->data_buf[24] << 16);
  1195. time |= (buf->data_buf[23] << 24);
  1196. #if 1
  1197. int64_t temp = 0;
  1198. temp = buf->data_buf[22];
  1199. time |= (temp << 32); //(buf->data_buf[22]<<32);
  1200. #else
  1201. time |= temp;
  1202. #endif
  1203. // printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1204. #if 0
  1205. time|=(buf->data_buf[21]<<40);
  1206. #else
  1207. temp = 0;
  1208. temp = buf->data_buf[21];
  1209. time |= (temp << 40); //(buf->data_buf[22]<<32);
  1210. // time|=(buf->data_buf[21]<<40);
  1211. #endif
  1212. // printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1213. // uint64_t value = 0x018B1E3D24AC;
  1214. // printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
  1215. // printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
  1216. printf("gateway mac = ");
  1217. for (int i = 0; i < 6; i++)
  1218. printf("%02X", gateway_mac[i]);
  1219. printf("\r\n");
  1220. printf("gateway num = %d\r\n", gateway_num);
  1221. printf("child mac = ");
  1222. for (int i = 0; i < 6; i++)
  1223. printf("%02X", child_mac[i]);
  1224. printf("\r\n");
  1225. printf("child_num num = %d\r\n", child_num);
  1226. printf("time = %llu\r\n", time);
  1227. printf("time = %llx\r\n", time);
  1228. //(3000 - (480 - 1*100))
  1229. #include "user_sleep.h"
  1230. #include "esp_sleep.h"
  1231. memcpy(Machine_info.gateway_mac, gateway_mac, 6);
  1232. // 取消配网成功 回复 ACK 设置新的信道
  1233. #if 0
  1234. send_pair_ack();
  1235. #else
  1236. int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除
  1237. // printf("msg_id =%X\r\n",msg_id);
  1238. reply_ack_func(PROTOCOL_HARDWARE_UNGWPAIRED, msg_id, true, NULL); // 回复ack
  1239. #endif
  1240. // #include "user_sleep.h"
  1241. // #include "esp_sleep.h"
  1242. extern void sleep_timer_stop();
  1243. extern void Already_send_timer_stop();
  1244. sleep_timer_stop();
  1245. Already_send_timer_stop();
  1246. // 修改信道前修改设备ID 及 设备ID
  1247. dymatic_change_device_id(0x00000001);
  1248. dymatic_change_dst_device_id(0xFFFFFFFF);
  1249. // 切换信道
  1250. reset_default(true, 1);
  1251. vTaskDelay(300 / portTICK_PERIOD_MS);
  1252. Machine_info.lora_new_channel = LORA_CHANENL;
  1253. // dymatic_change_chanel(Machine_info.lora_factory_channel); //切换信道
  1254. reset_lora(LORA_CHANENL);
  1255. }
  1256. /*网关收到终端发送的数据 ACK 终端收到用于删除当前的msgid的数据*/
  1257. static void hardware_ack_delete_msg_id(const LORA_DATA_T *buf)
  1258. {
  1259. int msg_id = (buf->data_buf[11] >> 8) | buf->data_buf[12]; // 获取当前消息id 用于删除当前链表的数据
  1260. ESP_LOG_BUFFER_HEX("ACK", buf->data_buf, buf->data_len);
  1261. // int current = countNodes(Send_list); //获取当前节点数据
  1262. Node *find = findNode(Send_list, msg_id);
  1263. printf("delete list node bofore = %d\r\n", msg_id);
  1264. if (find != NULL)
  1265. {
  1266. // printf("delete list node bofore = %d\r\n",msg_id);
  1267. // printList(Send_list);
  1268. Send_list = deleteNode(Send_list, msg_id);
  1269. // printf("delete list node msg_id = %d\r\n",msg_id);
  1270. // printf("delete list node after = %d\r\n",msg_id);
  1271. // printList(Send_list);
  1272. }
  1273. else
  1274. {
  1275. ESP_LOGE(LOG_TAG, "delete fail");
  1276. }
  1277. display_flag = 0;
  1278. }
  1279. void business_logic_func(const LORA_DATA_T *buf, uint8_t cmd_index, int msg)
  1280. {
  1281. int cmd = 0; // 当前的命令
  1282. int msg_id = 0; // 消息的id
  1283. // bool is_rsp_ack = false;
  1284. #if 0
  1285. printf("cmd = %02X\r\n",buf->data_buf[cmd_index]);
  1286. #endif
  1287. cmd = buf->data_buf[cmd_index];
  1288. switch (buf->data_buf[cmd_index])
  1289. {
  1290. case PROTOCOL_TIME_SYNCHRONIZATION:
  1291. msg_id = time_synchronization_func(buf);
  1292. // is_rsp_ack = true;
  1293. break;
  1294. case PROTOCOL_TERMINAL_NAME:
  1295. msg_id = terminal_name_func(buf);
  1296. // is_rsp_ack = true;
  1297. break;
  1298. case PROTOCOL_TERMINAL_NUMBER:
  1299. msg_id = terminal_number_func(buf);
  1300. // is_rsp_ack = true;
  1301. break;
  1302. case PROTOCOL_ELECTRIC_QUANTITY:
  1303. msg_id = electric_quantity_func(buf);
  1304. // is_rsp_ack = true;
  1305. break;
  1306. case PROTOCOL_SIGNAL_STRENGTH:
  1307. msg_id = signal_strength_func(buf);
  1308. // is_rsp_ack = true;
  1309. break;
  1310. case PROTOCOL_SCREEN_REFRESH_CYCLE:
  1311. msg_id = screen_refresh_cycle_func(buf);
  1312. // is_rsp_ack = true;
  1313. break;
  1314. case PROTOCOL_SCREEN_LEFT:
  1315. msg_id = screen_left_func(buf);
  1316. // is_rsp_ack = true;
  1317. break;
  1318. case PROTOCOL_SCREEN_RIGHT:
  1319. msg_id = screen_right_func(buf);
  1320. // is_rsp_ack = true;
  1321. break;
  1322. case PROTOCOL_RSPONSIBLE_PERSON_CHANGE:
  1323. msg_id = rsponsible_person_change_func(buf);
  1324. // is_rsp_ack = true;
  1325. break;
  1326. case PROTOCOL_RSPONSIBLE_PERSON_DELETE:
  1327. msg_id = rsponsible_person_delete_func(buf);
  1328. // is_rsp_ack = true;
  1329. break;
  1330. case PROTOCOL_PRODUCTION_PUNCH_CARD:
  1331. msg_id = production_punch_func(buf);
  1332. // is_rsp_ack = true;
  1333. break;
  1334. case PROTOCOL_PERSONNEL_CHECK_IN:
  1335. msg_id = personnel_check_in_func(buf);
  1336. // is_rsp_ack = true;
  1337. break;
  1338. case PROTOCOL_STATUS_SETTING:
  1339. msg_id = status_setting_func(buf);
  1340. // is_rsp_ack = true;
  1341. break;
  1342. case PROTOCOL_STATUS_DURATION:
  1343. msg_id = status_duration_func(buf);
  1344. // is_rsp_ack = true;
  1345. break;
  1346. case PROTOCOL_UPDATE_AND_DURATION:
  1347. msg_id = update_and_duration_func(buf);
  1348. // is_rsp_ack = true;
  1349. break;
  1350. case PROTOCOL_ANNOUNCEMENT:
  1351. msg_id = announcement_func(buf);
  1352. // is_rsp_ack = true;
  1353. break;
  1354. case PROTOCOL_CAPACITY_STATISTICS:
  1355. msg_id = capacity_statistics_func(buf);
  1356. // is_rsp_ack = true;
  1357. break;
  1358. case PROTOCOL_RESPONSIBLE_PERSON_SYNCHRONIZE_INFO:
  1359. msg_id = responsible_person_synchronize_info_func(buf);
  1360. // is_rsp_ack = true;
  1361. break;
  1362. case PROTOCOL_STATUS_LED:
  1363. msg_id = status_led_func(buf);
  1364. // is_rsp_ack = true;
  1365. break;
  1366. case PROTOCOL_LOG_LED:
  1367. msg_id = logo_led_func(buf);
  1368. // is_rsp_ack = true;
  1369. break;
  1370. case PROTOCOL_BATTERY_TEMPERATURE:
  1371. msg_id = battery_temperature_func(buf);
  1372. // is_rsp_ack = true;
  1373. break;
  1374. case PROTOCOL_STATION_NAME:
  1375. msg_id = station_name_func(buf);
  1376. // is_rsp_ack = true;
  1377. break;
  1378. case PROTOCOL_STATION_NUMBER:
  1379. msg_id = station_number_func(buf);
  1380. // is_rsp_ack = true;
  1381. break;
  1382. case PROTOCOL_RESPONSIBLE_PERSON_SETUP:
  1383. msg_id = responsible_person_setup_func(buf);
  1384. // is_rsp_ack = true;
  1385. break;
  1386. case PROTOCOL_SYSTEM_BULLETIN:
  1387. msg_id = system_bulletin_func(buf);
  1388. // is_rsp_ack = true;
  1389. break;
  1390. case PROTOCOL_HARDWARE_UPDATE:
  1391. msg_id = hardware_update_func(buf);
  1392. // is_rsp_ack = true;
  1393. break;
  1394. case PROTOCOL_HARDWARE_GWPAIRED: // 配网 暂时单独发送ack
  1395. reset_default(false, 1);
  1396. hardware_gwpair_func(buf);
  1397. // Paint_leftScreen_main_quick(&Machine_info);
  1398. // Paint_rightScreen_main_quick(&Machine_info);
  1399. screen_dis_info.is_left = true;
  1400. screen_dis_info.is_into_sleep = false;
  1401. // 重置快刷
  1402. Machine_info.left_current_Quick_refresh_time = 0;
  1403. Machine_info.right_current_Quick_refresh_time = 0;
  1404. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  1405. {
  1406. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  1407. }
  1408. screen_dis_info.is_left = false;
  1409. screen_dis_info.is_into_sleep = false;
  1410. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  1411. {
  1412. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  1413. }
  1414. break;
  1415. case PROTOCOL_HARDWARE_UNGWPAIRED: // 取消配网
  1416. printf("pair ungwpaired\r\n");
  1417. hardware_ungwpair_func(buf);
  1418. break;
  1419. case PROTOCOL_RSPONSIBLE_ACK:
  1420. printf("receive gateway ack delete data in list\r\n");
  1421. hardware_ack_delete_msg_id(buf);
  1422. break;
  1423. default:
  1424. ESP_LOGE(LOG_TAG, "ERR:CMD = 0x%02x", buf->data_buf[cmd_index]);
  1425. break;
  1426. }
  1427. #if USER_NOT_SLEEP_ENABLE
  1428. send_can_I_receive(); // 发送可以接受的命令
  1429. #endif
  1430. }
  1431. void uincode2gbk(char *str, uint16_t len, char *out_buffer)
  1432. {
  1433. char strtmp[1024];
  1434. char str1[3];
  1435. unsigned int iunicode, i;
  1436. unsigned int igbk;
  1437. memset(strtmp, 0, 1024);
  1438. int index = 0;
  1439. i = 0;
  1440. int length = len;
  1441. while (length)
  1442. {
  1443. if ((str[i] == 0) && (str[i + 1]) < 0x80) // ASCII 码 *text是ASCII码
  1444. {
  1445. str1[0] = (str[i + 1]) & 0xff;
  1446. memcpy(&strtmp[index], str1, 1);
  1447. index += 1;
  1448. }
  1449. else
  1450. {
  1451. iunicode = (str[i] << 8) + str[i + 1];
  1452. igbk = U2G(iunicode);
  1453. str1[0] = (igbk >> 8) & 0xff;
  1454. str1[1] = igbk & 0xff;
  1455. memcpy(&strtmp[index], str1, 2);
  1456. index += 2;
  1457. }
  1458. length -= 2;
  1459. i += 2;
  1460. }
  1461. // ESP_LOGW(LOG_TAG,"index:%d",index);
  1462. memcpy(out_buffer, strtmp, index);
  1463. }
  1464. // 计算数组元素的总和
  1465. uint32_t sumArray(uint8_t array[], int size)
  1466. {
  1467. uint32_t sum = 0;
  1468. for (int i = 0; i < size; i++)
  1469. {
  1470. sum += array[i];
  1471. }
  1472. return sum;
  1473. }
  1474. void mac_to_hex(char *out_string, char *in_string)
  1475. {
  1476. sprintf(out_string, "%02X:%02X:%02X:%02X:%02X:%02X",
  1477. in_string[0],
  1478. in_string[1],
  1479. in_string[2],
  1480. in_string[3],
  1481. in_string[4],
  1482. in_string[5]);
  1483. // ESP_LOG_BUFFER_HEX("--->",out_string,17);
  1484. }
  1485. void qrcode_protocol_create(char *result, Machine_info_t info)
  1486. {
  1487. // char str_https[] = "https://www.yeechart.com/antnest_download.php?data=";
  1488. char str_https[] = "https://www.yeechart.com/yeechart_download.php?data=";
  1489. Qrcode_protocol_t Qcode;
  1490. char qrcode_protocol[1024] = {0};
  1491. Qcode.head[0] = 0x5a;
  1492. Qcode.head[1] = 0x51;
  1493. Qcode.CRC = 0x00;
  1494. Qcode.mac.cmd = 0x03;
  1495. Qcode.mac.len = 0x11;
  1496. esp_read_mac((uint8_t *)mac_addr, ESP_MAC_WIFI_STA);
  1497. mac_to_hex((char *)Qcode.mac.mac_addr, (char *)mac_addr);
  1498. // strcpy((char *)Qcode.mac.mac_addr,(char*)mac_addr);
  1499. Qcode.client.cmd = 0X04;
  1500. #if 1 // 硬件设备id (qx 设置)
  1501. strcpy((char *)Qcode.client.client_id, (char *)info.cid);
  1502. Qcode.client.len = strlen((char *)Qcode.client.client_id);
  1503. #else // 假硬件设备id
  1504. strcpy((char *)Qcode.client.client_id, "whc_test");
  1505. Qcode.client.len = strlen((char *)Qcode.client.client_id);
  1506. #endif
  1507. Qcode.product.cmd = 0x05;
  1508. strcpy((char *)Qcode.product.product, "020101");
  1509. Qcode.product.len = strlen((char *)Qcode.product.product);
  1510. Qcode.time.cmd = 0x61;
  1511. Qcode.time.len = 0x0E;
  1512. #if 1
  1513. Qcode.time.Year = info.year;
  1514. Qcode.time.Month = info.month;
  1515. Qcode.time.Day = info.day;
  1516. Qcode.time.Hour = info.hour;
  1517. Qcode.time.Minute = info.min;
  1518. Qcode.time.Second = info.sec;
  1519. #else
  1520. Qcode.time.Year = 2023;
  1521. Qcode.time.Month = 9;
  1522. Qcode.time.Day = 19;
  1523. Qcode.time.Hour = 19;
  1524. Qcode.time.Minute = 30;
  1525. Qcode.time.Second = 20;
  1526. #endif
  1527. int time_index = 0;
  1528. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X%02X", Qcode.time.Year >> 8, Qcode.time.Year & 0xff);
  1529. time_index += 2;
  1530. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Month);
  1531. time_index += 1;
  1532. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Day);
  1533. time_index += 1;
  1534. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Hour);
  1535. time_index += 1;
  1536. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Minute);
  1537. time_index += 1;
  1538. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Second);
  1539. uint32_t crc = 0;
  1540. crc += Qcode.head[0];
  1541. crc += Qcode.head[1];
  1542. crc += sumArray((uint8_t *)&Qcode.mac, sizeof(Qcode.mac));
  1543. crc += Qcode.client.cmd;
  1544. crc += Qcode.client.len;
  1545. int len = 0, i = 0;
  1546. len = Qcode.client.len;
  1547. while (len)
  1548. {
  1549. crc += Qcode.client.client_id[i];
  1550. len--;
  1551. i++;
  1552. }
  1553. crc += Qcode.product.cmd;
  1554. crc += Qcode.product.len;
  1555. i = 0;
  1556. len = Qcode.product.len;
  1557. while (len)
  1558. {
  1559. if (Qcode.product.product[i] != '0')
  1560. {
  1561. crc += Qcode.product.product[i];
  1562. }
  1563. else
  1564. {
  1565. break;
  1566. }
  1567. len--;
  1568. i++;
  1569. }
  1570. i = 0;
  1571. len = Qcode.time.len;
  1572. while (len)
  1573. {
  1574. if (Qcode.time.time[i] != '0')
  1575. {
  1576. crc += Qcode.time.time[i];
  1577. }
  1578. else
  1579. {
  1580. break;
  1581. }
  1582. len--;
  1583. i++;
  1584. }
  1585. Qcode.CRC = crc & 0xff;
  1586. int index = 0;
  1587. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.head[0], Qcode.head[1]);
  1588. index += 2;
  1589. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.CRC);
  1590. index += 1;
  1591. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.mac.cmd, Qcode.mac.len);
  1592. index += 2;
  1593. // 转换16进制数组为ASCII字符串 mac 地址
  1594. for (int i = 0; i < Qcode.mac.len; i++)
  1595. {
  1596. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.mac.mac_addr[i]);
  1597. index += 1;
  1598. }
  1599. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.client.cmd, Qcode.client.len);
  1600. index += 2;
  1601. // 转换16进制数组为ASCII字符串 client id
  1602. for (int i = 0; i < Qcode.client.len; i++)
  1603. {
  1604. if (Qcode.client.client_id[i] != 0)
  1605. {
  1606. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.client.client_id[i]);
  1607. index += 1;
  1608. }
  1609. }
  1610. // 转换16进制数组为ASCII字符串 product
  1611. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.product.cmd, Qcode.product.len);
  1612. index += 2;
  1613. for (int i = 0; i < Qcode.product.len; i++)
  1614. {
  1615. // if(Qcode.product.product[i] != '0')
  1616. { // 这里产品id为0也需要
  1617. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.product.product[i]);
  1618. index += 1;
  1619. }
  1620. }
  1621. // 转换16进制数组为ASCII字符串 time
  1622. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.time.cmd, Qcode.time.len);
  1623. index += 2;
  1624. for (int i = 0; i < Qcode.time.len; i++)
  1625. {
  1626. // if(Qcode.time.time[i] != '0')
  1627. { // 这里时间为0也需要
  1628. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.time.time[i]);
  1629. index += 1;
  1630. }
  1631. }
  1632. strcpy(result, str_https);
  1633. strcat(result, qrcode_protocol);
  1634. // ESP_LOGW(LOG_TAG,"二维码信息:%s",result);
  1635. }
  1636. // 函数用于查找字节序列在数组中出现的次数,并返回出现位置的索引
  1637. int findByteSequence(const u8 *array, int arraySize, const u8 *sequence, int sequenceSize, int **positions)
  1638. {
  1639. int count = 0;
  1640. int *indices = NULL;
  1641. for (int i = 0; i <= arraySize - sequenceSize; i++)
  1642. {
  1643. // 检查当前位置开始的字节序列是否与目标序列匹配
  1644. int j;
  1645. for (j = 0; j < sequenceSize; j++)
  1646. {
  1647. if (array[i + j] != sequence[j])
  1648. {
  1649. break; // 不匹配,跳出循环
  1650. }
  1651. }
  1652. if (j == sequenceSize)
  1653. {
  1654. count++; // 找到匹配的序列
  1655. // 记录匹配位置的索引
  1656. int *temp = (int *)realloc(indices, count * sizeof(int));
  1657. if (temp == NULL)
  1658. {
  1659. // 内存分配失败,处理错误
  1660. if (indices != NULL)
  1661. {
  1662. free(indices);
  1663. }
  1664. return -1;
  1665. }
  1666. indices = temp;
  1667. indices[count - 1] = i;
  1668. }
  1669. }
  1670. *positions = indices; // 传递匹配位置的索引数组
  1671. return count;
  1672. }
  1673. // 匹配mac地址是否为本机
  1674. bool mac_is_true(uint8_t *mac)
  1675. {
  1676. if (
  1677. (mac[0] == Machine_info.mac_addr[0]) &&
  1678. (mac[1] == Machine_info.mac_addr[1]) &&
  1679. (mac[2] == Machine_info.mac_addr[2]) &&
  1680. (mac[3] == Machine_info.mac_addr[3]) &&
  1681. (mac[4] == Machine_info.mac_addr[4]) &&
  1682. (mac[5] == Machine_info.mac_addr[5]))
  1683. {
  1684. return true;
  1685. }
  1686. return false;
  1687. }
  1688. bool subcontract(YC_DATA_T *data)
  1689. {
  1690. int rsp_msgid = 0;
  1691. int index = 0;
  1692. int all_len = data->len;
  1693. uint8_t *buf = data->data;
  1694. USE_DATA_T use_data;
  1695. bool refresh_flag = false;
  1696. #if 1
  1697. u8 targetSequence[] = {0x5A, 0x51};
  1698. int sequenceSize = sizeof(targetSequence);
  1699. int *positions = NULL;
  1700. int result = findByteSequence(buf, all_len, targetSequence, sequenceSize, &positions);
  1701. if (result > 1)
  1702. {
  1703. int data_len = 0;
  1704. data_len = (buf[0] << 8) | (buf[1]);
  1705. printf("cmd 5A 51 find times %d,all data len = %d\r\n", result, data_len);
  1706. }
  1707. if (result == 1)
  1708. {
  1709. printf("cmd 5A 51 find times %d,data len = %d\r\n", result, data->len);
  1710. for (int index = 0; index < result; index++)
  1711. {
  1712. if ((buf[0] == 0x5A) && (buf[1] == 0x51) && (result == 1))
  1713. {
  1714. // 单条命令处理逻辑
  1715. use_data.data_len = all_len; //(((buf[positions[index] - 2])<<8) | ((buf[positions[index] - 1])))+2;
  1716. memcpy(use_data.data_buf, data->data, use_data.data_len);
  1717. #if 0
  1718. printf("single cmd len =%d\r\n",use_data.data_len);
  1719. for(int i = 0;i<use_data.data_len;i++)
  1720. {
  1721. printf("%02X",use_data.data_buf[i]);
  1722. }
  1723. printf("\r\n");
  1724. #endif
  1725. bool is = mac_is_true(&use_data.data_buf[3]);
  1726. if (is)
  1727. {
  1728. business_logic_func(&use_data, USE_CMD_LEN_INDEX - 2, 0);
  1729. refresh_flag = true;
  1730. }
  1731. else
  1732. {
  1733. printf("mac is fail not deal\r\n");
  1734. }
  1735. }
  1736. else
  1737. {
  1738. rsp_msgid = (buf[13] << 8) | buf[14]; // 获取当前消息id 用于删除
  1739. printf("rsp_msgid %02x,%02x,%d\r\n", buf[13], buf[14], rsp_msgid);
  1740. for (int index = 0; index < result; index++)
  1741. {
  1742. // 多条指令处理逻辑
  1743. bool is = mac_is_true(&buf[positions[index] + 3]);
  1744. if (is)
  1745. {
  1746. use_data.data_len = (((buf[positions[index] - 2]) << 8) | ((buf[positions[index] - 1]))) + 2;
  1747. memcpy(use_data.data_buf, data->data + (positions[index] - 2), use_data.data_len);
  1748. // print_yc_data(&use_data);
  1749. business_logic_func(&use_data, USE_CMD_LEN_INDEX, rsp_msgid);
  1750. refresh_flag = true;
  1751. }
  1752. else
  1753. {
  1754. printf("mac is fail not deal\r\n");
  1755. }
  1756. }
  1757. // if(is_rsp_ack)
  1758. {
  1759. reply_ack_func(0x00, rsp_msgid, true, NULL); // 回复ack
  1760. spiffs_write(&Machine_info);
  1761. ESP_LOGE(LOG_TAG, "1times cmd = %02X msg = %X start reply ack\r\n", 0x00, rsp_msgid);
  1762. }
  1763. }
  1764. }
  1765. }
  1766. if (result > 1)
  1767. {
  1768. printf("not single cmd len =%d\r\n", use_data.data_len);
  1769. rsp_msgid = (buf[13] << 8) | buf[14]; // 获取当前消息id 用于删除;
  1770. ESP_LOGW(LOG_TAG, "--rsp_msgid %02x,%02x,%d\r\n", buf[13], buf[14], rsp_msgid);
  1771. for (int index = 0; index < result; index++)
  1772. {
  1773. // 多条指令处理逻辑
  1774. bool is = mac_is_true(&buf[positions[index] + 3]);
  1775. if (is)
  1776. {
  1777. use_data.data_len = 15 + 2 + (((buf[positions[index] + 13]) << 8) | ((buf[positions[index] + 14])));
  1778. use_data.data_buf[0] = 0x00;
  1779. use_data.data_buf[1] = 0x00;
  1780. memcpy(&use_data.data_buf[2], data->data + (positions[index]), use_data.data_len + 15);
  1781. // printf("================last result============== len =%d\r\n",use_data.data_len);
  1782. // for(int i = 0;i<use_data.data_len;i++)
  1783. // {
  1784. // printf("%02x",use_data.data_buf[i]);
  1785. // }
  1786. // printf("================last result==============\r\n");
  1787. // print_yc_data(&use_data);
  1788. business_logic_func(&use_data, USE_CMD_LEN_INDEX, rsp_msgid);
  1789. refresh_flag = true;
  1790. }
  1791. else
  1792. {
  1793. printf("mac is fail not deal\r\n");
  1794. }
  1795. }
  1796. // if(is_rsp_ack)
  1797. {
  1798. reply_ack_func(0x00, rsp_msgid, true, NULL); // 回复ack
  1799. spiffs_write(&Machine_info);
  1800. ESP_LOGE(LOG_TAG, "cmd = %02X msg = %X start reply ack\r\n", 0x00, rsp_msgid);
  1801. }
  1802. }
  1803. #if 0
  1804. u8 targetSequence1[] = {0x55, 0x51};
  1805. int sequenceSize1 = sizeof(targetSequence);
  1806. int* positions1 = NULL;
  1807. result = findByteSequence(buf, all_len, targetSequence1, sequenceSize1, &positions1);
  1808. if(result!=0)
  1809. {
  1810. printf("ack 55 51 find time %d\r\n", result);
  1811. }
  1812. for(int index = 0;index<result;index++)
  1813. {
  1814. //use_data.data_len =(((buf[positions[index] - 2])<<8) | ((buf[positions[index] - 1])))+2;
  1815. //memcpy(use_data.data_buf,data->data + (positions[index] -2) ,use_data.data_len);
  1816. //print_yc_data(&use_data);
  1817. use_data.data_len = all_len;
  1818. memcpy(use_data.data_buf,data->data + (positions1[index]) ,use_data.data_len);
  1819. business_ack_func(&use_data);
  1820. //refresh_flag =true;
  1821. }
  1822. #endif
  1823. // #if 1
  1824. // for (int i = 0; i < all_len; i++)
  1825. // {
  1826. // printf("%02x",buf[i]);
  1827. // }
  1828. // #endif
  1829. free(positions);
  1830. // free(positions1);
  1831. #if 0
  1832. printf("index:");
  1833. if (result > 0) {
  1834. //printf("出现的位置索引:");
  1835. for (int i = 0; i < result; i++) {
  1836. printf("%d,", positions[i]);
  1837. }
  1838. printf("\n");
  1839. }
  1840. #endif
  1841. #else
  1842. while (all_len > 0)
  1843. {
  1844. // ESP_LOGW(LOG_TAG,"-->[%d]",all_len);
  1845. // for(int i=0;i<all_len;i++)
  1846. // {
  1847. // printf("%02x ",data->data[i]);
  1848. // }
  1849. // printf("\n");
  1850. if (((buf[index + 2]) == 0x5A) && ((buf[index + 3]) == 0x51))
  1851. {
  1852. use_data.data_len = (((buf[index]) << 8) | ((buf[index + 1]))) + 2;
  1853. memcpy(use_data.data_buf, data->data + index, use_data.data_len);
  1854. all_len -= (use_data.data_len);
  1855. index += (use_data.data_len);
  1856. // print_yc_data(&use_data);
  1857. business_logic_func(&use_data);
  1858. refresh_flag = true;
  1859. }
  1860. else
  1861. {
  1862. all_len--;
  1863. index++;
  1864. // ESP_LOGE(LOG_TAG,"err:data");
  1865. // break;
  1866. }
  1867. }
  1868. #endif
  1869. return refresh_flag;
  1870. }
  1871. // len:有效数据长度,不是总长//
  1872. static void protocol_package(char cmd, const uint8_t *info, int len)
  1873. {
  1874. #define __PACKAGE 15
  1875. uint8_t *__str = (uint8_t *)malloc(sizeof(uint8_t) * (__PACKAGE + len) * 2);
  1876. __str[0] = 0x5a;
  1877. __str[1] = 0x51;
  1878. __str[2] = 0x00;
  1879. __str[3] = mac_addr[0];
  1880. __str[4] = mac_addr[1];
  1881. __str[5] = mac_addr[2];
  1882. __str[6] = mac_addr[3];
  1883. __str[7] = mac_addr[4];
  1884. __str[8] = mac_addr[5];
  1885. __str[9] = Machine_info.eflagID;
  1886. __str[10] = cmd;
  1887. #if 0
  1888. __str[11] = 0x01;//当前只为一包
  1889. __str[12] = 0x01;//当前只为一包
  1890. #else
  1891. // 随机数
  1892. srand((unsigned int)time(NULL));
  1893. int time_rand = rand() % 65535 + 1; // 生成1~100的随机数
  1894. __str[11] = (time_rand >> 8) & 0xFF;
  1895. __str[12] = time_rand & 0xFF;
  1896. #endif
  1897. // 分包
  1898. __str[13] = (len >> 8) & 0xFF;
  1899. __str[14] = len & 0xFF;
  1900. memcpy(__str + __PACKAGE, info, len);
  1901. int i = 0, sum = 0;
  1902. for (i = 0; i < len + __PACKAGE; i++)
  1903. {
  1904. sum += __str[i];
  1905. }
  1906. __str[2] = sum & 0xFF;
  1907. printf("crc=%02x ", __str[2]);
  1908. printf("-->package:");
  1909. for (i = 0; i < len + __PACKAGE; i++)
  1910. {
  1911. printf("%02x ", __str[i]);
  1912. }
  1913. printf("\n");
  1914. changebintotxt(__str, len + __PACKAGE);
  1915. #if 0
  1916. Send_list = postInsert(Send_list, time_rand,cmd,(char *)__str,(int)(len+__PACKAGE)*2);//len*2
  1917. #else
  1918. lora_send_data((char *)__str, len + __PACKAGE);
  1919. #endif
  1920. free(__str);
  1921. }
  1922. int txttobin(uint8_t *strlorabuf)
  1923. {
  1924. int len = 0, i = 0;
  1925. uint8_t strbuf[1024];
  1926. char ch = 0, chhi = 0, chlo = 0;
  1927. len = strlen((char *)strlorabuf);
  1928. strcpy((char *)strbuf, (char *)strlorabuf);
  1929. memset(strlorabuf, 0, len);
  1930. // Serial.print("jiexishuju:");Serial.println((char *)strbuf);
  1931. for (i = 0; i < len; i++)
  1932. {
  1933. ch = strbuf[i * 2];
  1934. if ((ch >= 0x30 && ch <= 0x39) ||
  1935. (ch >= 0x41 && ch <= 0x5a) ||
  1936. (ch >= 0x61 && ch <= 0x7a))
  1937. {
  1938. {
  1939. if (ch >= 0x30 && ch <= 0x39)
  1940. ch = ch - 0x30;
  1941. else if (ch >= 0x41 && ch <= 0x5a)
  1942. ch = ch - 0x37;
  1943. else
  1944. ch = ch - 0x57;
  1945. ch &= 0x000f;
  1946. chhi = (ch << 4);
  1947. }
  1948. ch = strbuf[i * 2 + 1];
  1949. if ((ch >= 0x30 && ch <= 0x39) ||
  1950. (ch >= 0x41 && ch <= 0x5a) ||
  1951. (ch >= 0x61 && ch <= 0x7a))
  1952. {
  1953. if (ch >= 0x30 && ch <= 0x39)
  1954. ch = ch - 0x30;
  1955. else if (ch >= 0x41 && ch <= 0x5a)
  1956. ch = ch - 0x37;
  1957. else
  1958. ch = ch - 0x57;
  1959. ch &= 0x000f;
  1960. ch = chhi + ch;
  1961. }
  1962. strlorabuf[i] = ch;
  1963. }
  1964. }
  1965. // Serial.write(strlorabuf,len);
  1966. return i;
  1967. }
  1968. void changebintotxt(uint8_t *strb, int len)
  1969. {
  1970. int i = 0;
  1971. uint8_t strtxt[512];
  1972. char ch, chhi, chlo;
  1973. memset(strtxt, 0, 512);
  1974. for (i = 0; i < len; i++)
  1975. {
  1976. ch = strb[i];
  1977. chhi = ch;
  1978. chhi >>= 4;
  1979. chhi &= 0x000f;
  1980. if (chhi <= 9)
  1981. chhi += 0x30;
  1982. else
  1983. chhi += 0x37;
  1984. strtxt[i * 2] = chhi;
  1985. chlo = ch;
  1986. chlo &= 0x000f;
  1987. if (chlo <= 9)
  1988. chlo += 0x30;
  1989. else
  1990. chlo += 0x37;
  1991. strtxt[i * 2 + 1] = chlo;
  1992. }
  1993. strcpy((char *)strb, (char *)strtxt);
  1994. }
  1995. void f_send_get_chart_data(void)
  1996. {
  1997. uint8_t strlorabuf[200];
  1998. int crc16 = 0, i = 0, len = 0;
  1999. strlorabuf[0] = 0x5A;
  2000. strlorabuf[1] = 0x51; // 包头
  2001. strlorabuf[2] = 0x00; // 校验和
  2002. strlorabuf[3] = mac_addr[0];
  2003. strlorabuf[4] = mac_addr[1];
  2004. strlorabuf[5] = mac_addr[2];
  2005. strlorabuf[6] = mac_addr[3];
  2006. strlorabuf[7] = mac_addr[4];
  2007. strlorabuf[8] = mac_addr[5];
  2008. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2009. strlorabuf[10] = 0x11; // cmd命令字
  2010. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2011. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2012. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2013. strlorabuf[13] = 0x00;
  2014. strlorabuf[14] = 0x00; // 长度
  2015. strlorabuf[15] = 0;
  2016. crc16 = strlorabuf[0] + strlorabuf[1];
  2017. for (i = 3; i < 15; i++)
  2018. crc16 = crc16 + strlorabuf[i];
  2019. strlorabuf[2] = crc16 & 0xff;
  2020. changebintotxt(strlorabuf, 15);
  2021. // Serial.println((char *)strlorabuf);
  2022. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2023. len = strlen((char *)strlorabuf);
  2024. // lora_send_data((char *)strlorabuf,len);
  2025. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2026. }
  2027. void f_send_version(void)
  2028. {
  2029. ESP_LOGW(LOG_TAG, "f_send_version");
  2030. uint8_t strlorabuf[200];
  2031. int crc16 = 0, i = 0, len = 0;
  2032. strlorabuf[0] = 0x5A;
  2033. strlorabuf[1] = 0x51; // 包头
  2034. strlorabuf[2] = 0x00; // 校验和
  2035. strlorabuf[3] = mac_addr[0];
  2036. strlorabuf[4] = mac_addr[1];
  2037. strlorabuf[5] = mac_addr[2];
  2038. strlorabuf[6] = mac_addr[3];
  2039. strlorabuf[7] = mac_addr[4];
  2040. strlorabuf[8] = mac_addr[5];
  2041. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2042. strlorabuf[10] = 0x91; // cmd命令字
  2043. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2044. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2045. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2046. strlorabuf[13] = 0x00;
  2047. strlorabuf[14] = 0x03; // 长度
  2048. strlorabuf[15] = (VERSION_X) & 0xff;
  2049. strlorabuf[16] = (VERSION_Y) & 0xff;
  2050. strlorabuf[17] = (VERSION_Z) & 0xff;
  2051. strlorabuf[18] = 0;
  2052. crc16 = strlorabuf[0] + strlorabuf[1];
  2053. for (i = 3; i < 18; i++)
  2054. crc16 = crc16 + strlorabuf[i];
  2055. strlorabuf[2] = crc16 & 0xff;
  2056. changebintotxt(strlorabuf, 18);
  2057. // Serial.println((char *)strlorabuf);
  2058. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2059. len = strlen((char *)strlorabuf);
  2060. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2061. }
  2062. void f_send_lora_rssi(int rssi)
  2063. {
  2064. // ESP_LOGW(LOG_TAG,"f_send_lora_rssi:rssi = %d",rssi);
  2065. uint8_t strlorabuf[200];
  2066. int crc16 = 0, i = 0, len = 0;
  2067. strlorabuf[0] = 0x5A;
  2068. strlorabuf[1] = 0x51; // 包头
  2069. strlorabuf[2] = 0x00; // 校验和
  2070. strlorabuf[3] = mac_addr[0];
  2071. strlorabuf[4] = mac_addr[1];
  2072. strlorabuf[5] = mac_addr[2];
  2073. strlorabuf[6] = mac_addr[3];
  2074. strlorabuf[7] = mac_addr[4];
  2075. strlorabuf[8] = mac_addr[5];
  2076. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2077. strlorabuf[10] = 0x05; // cmd命令字
  2078. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2079. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2080. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2081. strlorabuf[13] = 0x00;
  2082. strlorabuf[14] = 0x01; // 长度
  2083. strlorabuf[15] = (rssi) & 0xff;
  2084. strlorabuf[16] = 0;
  2085. crc16 = strlorabuf[0] + strlorabuf[1];
  2086. for (i = 3; i < 16; i++)
  2087. crc16 = crc16 + strlorabuf[i];
  2088. strlorabuf[2] = crc16 & 0xff;
  2089. changebintotxt(strlorabuf, 16);
  2090. // Serial.println((char *)strlorabuf);
  2091. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2092. len = strlen((char *)strlorabuf);
  2093. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2094. }
  2095. void f_send_battary_vaule(int battry)
  2096. {
  2097. uint8_t strlorabuf[200];
  2098. int crc16 = 0, i = 0, len = 0;
  2099. strlorabuf[0] = 0x5A;
  2100. strlorabuf[1] = 0x51; // 包头
  2101. strlorabuf[2] = 0x00; // 校验和
  2102. strlorabuf[3] = mac_addr[0];
  2103. strlorabuf[4] = mac_addr[1];
  2104. strlorabuf[5] = mac_addr[2];
  2105. strlorabuf[6] = mac_addr[3];
  2106. strlorabuf[7] = mac_addr[4];
  2107. strlorabuf[8] = mac_addr[5];
  2108. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2109. strlorabuf[10] = 0x04; // cmd命令字
  2110. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2111. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2112. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2113. strlorabuf[13] = 0x00;
  2114. strlorabuf[14] = 0x01; // 长度
  2115. strlorabuf[15] = (battry) & 0xff;
  2116. strlorabuf[16] = 0;
  2117. crc16 = strlorabuf[0] + strlorabuf[1];
  2118. for (i = 3; i < 16; i++)
  2119. crc16 = crc16 + strlorabuf[i];
  2120. strlorabuf[2] = crc16 & 0xff;
  2121. changebintotxt(strlorabuf, 16);
  2122. // Serial.println((char *)strlorabuf);
  2123. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2124. len = strlen((char *)strlorabuf);
  2125. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2126. }
  2127. void f_send_reply_status(int status)
  2128. {
  2129. uint8_t strlorabuf[200];
  2130. int crc16 = 0, i = 0, len = 0;
  2131. strlorabuf[0] = 0x5A;
  2132. strlorabuf[1] = 0x51; // 包头
  2133. strlorabuf[2] = 0x00; // 校验和
  2134. strlorabuf[3] = mac_addr[0];
  2135. strlorabuf[4] = mac_addr[1];
  2136. strlorabuf[5] = mac_addr[2];
  2137. strlorabuf[6] = mac_addr[3];
  2138. strlorabuf[7] = mac_addr[4];
  2139. strlorabuf[8] = mac_addr[5];
  2140. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2141. strlorabuf[10] = 0x06; // cmd命令字
  2142. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2143. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2144. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2145. strlorabuf[13] = 0x00;
  2146. strlorabuf[14] = 0x01; // 长度
  2147. strlorabuf[15] = (status) & 0xff;
  2148. strlorabuf[16] = 0;
  2149. crc16 = strlorabuf[0] + strlorabuf[1];
  2150. for (i = 3; i < 16; i++)
  2151. crc16 = crc16 + strlorabuf[i];
  2152. strlorabuf[2] = crc16 & 0xff;
  2153. changebintotxt(strlorabuf, 16);
  2154. // Serial.println((char *)strlorabuf);
  2155. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2156. len = strlen((char *)strlorabuf);
  2157. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2158. }
  2159. // 唤醒时发送是否可以接收数据命令
  2160. void send_can_I_receive()
  2161. {
  2162. printf("->can i\n");
  2163. uint8_t strlorabuf[200];
  2164. strlorabuf[0] = 0x5A;
  2165. strlorabuf[1] = 0x51;
  2166. strlorabuf[2] = 0x51;
  2167. strlorabuf[3] = 0x5A;
  2168. strlorabuf[4] = 0x00;
  2169. #if 0
  2170. strlorabuf[5] = 0x01;
  2171. #else
  2172. strlorabuf[5] = Machine_info.eflagID;
  2173. if (Send_list != NULL)
  2174. {
  2175. strlorabuf[6] = 0x01;
  2176. }
  2177. else
  2178. {
  2179. strlorabuf[6] = 0x00;
  2180. }
  2181. strlorabuf[7] = Machine_info.mac_addr[0];
  2182. strlorabuf[8] = Machine_info.mac_addr[1];
  2183. strlorabuf[9] = Machine_info.mac_addr[2];
  2184. strlorabuf[10] = Machine_info.mac_addr[3];
  2185. strlorabuf[11] = Machine_info.mac_addr[4];
  2186. strlorabuf[12] = Machine_info.mac_addr[5];
  2187. int crc = 0;
  2188. for (int i = 0; i < 13; i++)
  2189. {
  2190. crc += strlorabuf[i];
  2191. }
  2192. strlorabuf[13] = crc & 0xff;
  2193. #endif
  2194. // send_lora_data(strlorabuf,len); //不直接发送
  2195. lora_send_data((char *)strlorabuf, 14);
  2196. }
  2197. // 发送是否可以接收数据命令
  2198. // void send_pair_ack()
  2199. // {
  2200. // uint8_t strlorabuf[200];
  2201. // strlorabuf[0] = 0x5A;
  2202. // strlorabuf[1] = 0x51;
  2203. // strlorabuf[2] = 0x51;
  2204. // strlorabuf[3] = 0x5A;
  2205. // strlorabuf[4] = 0x00;
  2206. // #if 0
  2207. // strlorabuf[5] = 0x01;
  2208. // #else
  2209. // strlorabuf[5] = Machine_info.eflagID;
  2210. // strlorabuf[6] = lora_channel + 1;
  2211. // #endif
  2212. // //send_lora_data(strlorabuf,len); //不直接发送
  2213. // lora_send_data((char *)strlorabuf,7);
  2214. // }
  2215. void send_rssi(void)
  2216. {
  2217. char rssi = Machine_info.rssi;
  2218. #if 0
  2219. protocol_package(0x05,&rssi,1);
  2220. #else
  2221. f_send_lora_rssi(rssi);
  2222. #endif
  2223. }
  2224. // 4bytes、32bit数据大小端转化
  2225. #define L2B32(Little) (Little = ((Little & 0xff) << 24) | (((Little) & 0xff00) << 8) | (((Little) & 0xff0000) >> 8) | ((Little >> 24) & 0xff))
  2226. uint8_t change_btn_reversal(uint8_t reversal_btn) // lora收到按键,转化为本地状态
  2227. {
  2228. uint8_t btn_tmp = 0;
  2229. switch (reversal_btn)
  2230. {
  2231. case 0:
  2232. btn_tmp = 1;
  2233. break;
  2234. case 1:
  2235. btn_tmp = 6;
  2236. break;
  2237. case 2:
  2238. btn_tmp = 4;
  2239. break;
  2240. case 3:
  2241. btn_tmp = 3;
  2242. break;
  2243. case 4:
  2244. btn_tmp = 5;
  2245. break;
  2246. case 5:
  2247. btn_tmp = 2;
  2248. break;
  2249. break;
  2250. default:
  2251. btn_tmp = 1;
  2252. break;
  2253. }
  2254. return btn_tmp;
  2255. }
  2256. uint8_t change_btn(uint8_t btn)
  2257. {
  2258. uint8_t btn_tmp = 0;
  2259. switch (btn)
  2260. {
  2261. case 1:
  2262. btn_tmp = 0;
  2263. break;
  2264. case 2:
  2265. btn_tmp = 5;
  2266. break;
  2267. case 3:
  2268. btn_tmp = 3;
  2269. break;
  2270. case 4:
  2271. btn_tmp = 2;
  2272. break;
  2273. case 5:
  2274. btn_tmp = 4;
  2275. break;
  2276. case 6:
  2277. btn_tmp = 1;
  2278. break;
  2279. default:
  2280. btn_tmp = 0;
  2281. break;
  2282. }
  2283. // ESP_LOGW(LOG_TAG,"btn = [%d] ,btn_tmp[%d]",btn,btn_tmp);
  2284. return btn_tmp;
  2285. }
  2286. /*发送状态持续的时间 状态持续时长(0x0E)*/
  2287. void send_status_duration(void)
  2288. {
  2289. #if 0
  2290. uint8_t* dur = (uint8_t*)malloc(sizeof(uint8_t)*(12));
  2291. memcpy(dur,&last_button_info.button_info,1);
  2292. memcpy(dur+1,&last_button_info.Year,2);
  2293. memcpy(dur+3,&last_button_info.Month,1);
  2294. memcpy(dur+4,&last_button_info.Day,1);
  2295. memcpy(dur+5,&last_button_info.Hour,1);
  2296. memcpy(dur+6,&last_button_info.Minute,1);
  2297. memcpy(dur+7,&last_button_info.Second,1);
  2298. uint32_t tmp = last_button_info.time_min;
  2299. L2B32(tmp);
  2300. memcpy(dur+8,&tmp,4);
  2301. ESP_LOGW(LOG_TAG,"log -->btn = %d [%d-%d-%d]%d:%d:%d <%04ld>",\
  2302. last_button_info.button_info, last_button_info.Year,\
  2303. last_button_info.Month,last_button_info.Day,\
  2304. last_button_info.Hour, last_button_info.Minute,\
  2305. last_button_info.Second,last_button_info.time_min);
  2306. protocol_package(0x0E,dur,12);
  2307. last_button_info.time_min = 0;
  2308. free(dur);
  2309. #else
  2310. uint8_t strlorabuf[200];
  2311. int crc16 = 0, i = 0, len = 0;
  2312. // 帧头
  2313. strlorabuf[0] = 0x5A; // 包头
  2314. strlorabuf[1] = 0x51; // 包头
  2315. strlorabuf[2] = 0x00; // 校验和
  2316. strlorabuf[3] = mac_addr[0];
  2317. strlorabuf[4] = mac_addr[1];
  2318. strlorabuf[5] = mac_addr[2];
  2319. strlorabuf[6] = mac_addr[3];
  2320. strlorabuf[7] = mac_addr[4];
  2321. strlorabuf[8] = mac_addr[5];
  2322. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2323. strlorabuf[10] = 0x0E; // cmd命令字
  2324. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2325. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2326. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2327. strlorabuf[13] = 0x00;
  2328. strlorabuf[14] = 0x0C; // 长度
  2329. uint8_t tmp = change_btn(Machine_info.last_button.button_info);
  2330. strlorabuf[15] = tmp; // 上一次的状态
  2331. if ((Machine_info.last_button.Year == 0x00) && (Machine_info.last_button.Month == 0x00) && (Machine_info.last_button.Day == 0x00))
  2332. {
  2333. strlorabuf[16] = 0x00;
  2334. strlorabuf[17] = 0x00;
  2335. strlorabuf[18] = 0x00;
  2336. strlorabuf[19] = 0x00;
  2337. strlorabuf[20] = 0x00;
  2338. strlorabuf[21] = 0x00;
  2339. strlorabuf[22] = 0x00;
  2340. printf("time is not sync\r\n");
  2341. }
  2342. else
  2343. {
  2344. strlorabuf[16] = (Machine_info.last_button.Year >> 8) & 0xff;
  2345. strlorabuf[17] = Machine_info.last_button.Year & 0xff;
  2346. strlorabuf[18] = Machine_info.last_button.Month;
  2347. strlorabuf[19] = Machine_info.last_button.Day;
  2348. strlorabuf[20] = Machine_info.last_button.Hour;
  2349. strlorabuf[21] = Machine_info.last_button.Minute;
  2350. strlorabuf[22] = Machine_info.last_button.Second;
  2351. }
  2352. strlorabuf[23] = (Machine_info.Duration_time >> 24) & 0xff; // 分钟计算
  2353. strlorabuf[24] = (Machine_info.Duration_time >> 16) & 0xff;
  2354. strlorabuf[25] = (Machine_info.Duration_time >> 8) & 0xff;
  2355. strlorabuf[26] = (Machine_info.Duration_time) & 0xff;
  2356. strlorabuf[27] = 0;
  2357. crc16 = strlorabuf[0] + strlorabuf[1];
  2358. for (i = 3; i < 27; i++)
  2359. crc16 = crc16 + strlorabuf[i];
  2360. strlorabuf[2] = crc16 & 0xff;
  2361. // ESP_LOGE(LOG_TAG,"{cmd:0e}send_status_duration btn -->[%d] -->Duration_time = %ld -->time :%d %d %d %d %d %d %d",strlorabuf[15],Machine_info.Duration_time,strlorabuf[16],strlorabuf[17],strlorabuf[18],strlorabuf[19],strlorabuf[20],strlorabuf[21],strlorabuf[22]);
  2362. changebintotxt(strlorabuf, 27);
  2363. // printf("Machine_info cid %s\r\n",Machine_info.cid);
  2364. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2365. len = strlen((char *)strlorabuf);
  2366. #if 0
  2367. lora_send_data((char *)strlorabuf,len);
  2368. #else
  2369. // printList(Send_list);
  2370. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2371. #endif
  2372. // lora_send_data((char *)strlorabuf,len);
  2373. last_button_info.time_min = 0;
  2374. #endif
  2375. }
  2376. /*当前最新的状态*/
  2377. void send_update_status(void)
  2378. {
  2379. #if 0
  2380. uint8_t* update = (uint8_t*)malloc(sizeof(uint8_t)*(12));
  2381. memset(&new_button_info,0,sizeof(Button_Time_t));
  2382. new_button_info.Year = Machine_info.year;
  2383. new_button_info.Month = Machine_info.month;
  2384. new_button_info.Day = Machine_info.day;
  2385. new_button_info.Hour = Machine_info.hour;
  2386. new_button_info.Minute = Machine_info.min;
  2387. new_button_info.Second = Machine_info.sec;
  2388. new_button_info.time_min = 0;
  2389. memcpy(update,&last_button_info.button_info,1);
  2390. memcpy(update+1,&new_button_info.Year,2);
  2391. memcpy(update+3,&new_button_info.Month,1);
  2392. memcpy(update+4,&new_button_info.Day,1);
  2393. memcpy(update+5,&new_button_info.Hour,1);
  2394. memcpy(update+6,&new_button_info.Minute,1);
  2395. memcpy(update+7,&new_button_info.Second,1);
  2396. memcpy(update+8,&new_button_info.time_min,4);
  2397. protocol_package(0x0F,update,12);
  2398. free(update);
  2399. #else
  2400. // memset(&Machine_info.current_button,0,sizeof(Button_Time_t));
  2401. Machine_info.current_button.Year = Machine_info.year;
  2402. Machine_info.current_button.Month = Machine_info.month;
  2403. Machine_info.current_button.Day = Machine_info.day;
  2404. Machine_info.current_button.Hour = Machine_info.hour;
  2405. Machine_info.current_button.Minute = Machine_info.min;
  2406. Machine_info.current_button.Second = Machine_info.sec;
  2407. Machine_info.current_button.time_min = 0;
  2408. uint8_t strlorabuf[200];
  2409. int crc16 = 0, i = 0, len = 0;
  2410. // 帧头 目标地址 源地址(id) 长度 模式 具体内容 校验
  2411. strlorabuf[0] = 0x5A;
  2412. strlorabuf[1] = 0x51; // 包头
  2413. strlorabuf[2] = 0x00; // 校验和
  2414. strlorabuf[3] = mac_addr[0];
  2415. strlorabuf[4] = mac_addr[1];
  2416. strlorabuf[5] = mac_addr[2];
  2417. strlorabuf[6] = mac_addr[3];
  2418. strlorabuf[7] = mac_addr[4];
  2419. strlorabuf[8] = mac_addr[5];
  2420. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2421. strlorabuf[10] = 0x0F; // cmd命令字
  2422. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2423. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2424. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2425. strlorabuf[13] = 0x00;
  2426. strlorabuf[14] = 0x0C; // 长度
  2427. uint8_t tmp = change_btn(Machine_info.current_button.button_info);
  2428. strlorabuf[15] = tmp; // 当前的状态
  2429. if ((Machine_info.current_button.Year == 0x00) && (Machine_info.current_button.Month == 0x00) && (Machine_info.current_button.Day == 0x00))
  2430. {
  2431. strlorabuf[16] = 0x00;
  2432. strlorabuf[17] = 0x00;
  2433. strlorabuf[18] = 0x00;
  2434. strlorabuf[19] = 0x00;
  2435. strlorabuf[20] = 0x00;
  2436. strlorabuf[21] = 0x00;
  2437. strlorabuf[22] = 0x00;
  2438. printf("time is not sync\r\n");
  2439. }
  2440. else
  2441. {
  2442. strlorabuf[16] = (Machine_info.current_button.Year >> 8) & 0xff;
  2443. strlorabuf[17] = Machine_info.current_button.Year & 0xff;
  2444. strlorabuf[18] = Machine_info.current_button.Month;
  2445. strlorabuf[19] = Machine_info.current_button.Day;
  2446. strlorabuf[20] = Machine_info.current_button.Hour;
  2447. strlorabuf[21] = Machine_info.current_button.Minute;
  2448. strlorabuf[22] = Machine_info.current_button.Second;
  2449. }
  2450. strlorabuf[23] = (Machine_info.current_button.time_min >> 24) & 0xff; // 分钟计算
  2451. strlorabuf[24] = (Machine_info.current_button.time_min >> 16) & 0xff;
  2452. strlorabuf[25] = (Machine_info.current_button.time_min >> 8) & 0xff;
  2453. strlorabuf[26] = Machine_info.current_button.time_min & 0xff;
  2454. strlorabuf[27] = 0;
  2455. // ESP_LOGE(LOG_TAG,"@@send_update_status -->btn = [%d] -->time :%d %d %d %d %d %d %d",strlorabuf[15],strlorabuf[16],strlorabuf[17],strlorabuf[18],strlorabuf[19],strlorabuf[20],strlorabuf[21],strlorabuf[22]);
  2456. crc16 = strlorabuf[0] + strlorabuf[1];
  2457. for (i = 3; i < 27; i++)
  2458. crc16 = crc16 + strlorabuf[i];
  2459. strlorabuf[2] = crc16 & 0xff;
  2460. changebintotxt(strlorabuf, 27);
  2461. // printf("Machine_info cid %s\r\n",Machine_info.cid);
  2462. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2463. len = strlen((char *)strlorabuf);
  2464. #if 0
  2465. lora_send_data((char *)strlorabuf,len);
  2466. #else
  2467. Send_list = postInsert(Send_list, Machine_info.msg_id, Machine_info.msg_id, (char *)strlorabuf, len); // 插入数据
  2468. #endif
  2469. Machine_info.last_button.time_min = 0;
  2470. #endif
  2471. }
  2472. void send_ACK(char cmd, bool flag)
  2473. {
  2474. uint8_t ack = 0;
  2475. if (flag)
  2476. ack = 0x01;
  2477. else
  2478. ack = 0x00;
  2479. protocol_package(cmd, &ack, 1);
  2480. }
  2481. void adc1_init(void)
  2482. {
  2483. //-------------ADC Init---------------//
  2484. adc_oneshot_unit_init_cfg_t init_config = {
  2485. .unit_id = ADC_UNIT_1,
  2486. .ulp_mode = ADC_ULP_MODE_DISABLE,
  2487. };
  2488. ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc1_handle));
  2489. }
  2490. // void set_status_heights(void)
  2491. // {
  2492. // extern uint32_t arr_heights[10];
  2493. // memcpy(&_0703_button_info,&last_button_info,sizeof(Button_Time_t));
  2494. // switch (_0703_button_info.button_info)
  2495. // {
  2496. // case 0:
  2497. // case 1://yunxing
  2498. // arr_heights[0]+=1;
  2499. // break;
  2500. // case 2://tingji
  2501. // arr_heights[1]+=1;
  2502. // break;
  2503. // case 3://guzhang
  2504. // arr_heights[2]+=1;
  2505. // break;
  2506. // case 4://fengcun
  2507. // arr_heights[3]+=1;
  2508. // break;
  2509. // case 5://baoyang
  2510. // arr_heights[4]+=1;
  2511. // break;
  2512. // case 6://dailiao
  2513. // arr_heights[5]+=1;
  2514. // break;
  2515. // default:
  2516. // break;
  2517. // }
  2518. // // ESP_LOGE(LOG_TAG,"button_info = %d ,time_min = %ld ---%ld - %ld - %ld - %ld - %ld - %ld",
  2519. // // _0703_button_info.button_info,_0703_button_info.time_min,
  2520. // // arr_heights[0],arr_heights[1],arr_heights[2],
  2521. // // arr_heights[3],arr_heights[4],arr_heights[5]);
  2522. // }
  2523. void set_status_heights(void)
  2524. {
  2525. // STATE_OPERATION = 1, //运行
  2526. // STATE_SHUT_DOWN = 2, //停机
  2527. // STATE_BREAKDOWN = 3, //故障
  2528. // STATE_SAFEKEEP = 4, //封存
  2529. // STATE_UPKEEP = 5, //保养
  2530. // STATE_WAIT_MATERIALS = 6, //待料
  2531. extern uint32_t arr_heights[10];
  2532. switch (Machine_info.last_button.button_info)
  2533. {
  2534. case 0:
  2535. case 1: // yunxing
  2536. arr_heights[0] += Machine_info.Duration_time;
  2537. break;
  2538. case 2: // tingji
  2539. arr_heights[1] += Machine_info.Duration_time;
  2540. break;
  2541. case 3: // guzhang
  2542. arr_heights[2] += Machine_info.Duration_time;
  2543. break;
  2544. case 4: // fengcun
  2545. arr_heights[3] += Machine_info.Duration_time;
  2546. break;
  2547. case 5: // baoyang
  2548. arr_heights[4] += Machine_info.Duration_time;
  2549. break;
  2550. case 6: // dailiao
  2551. arr_heights[5] += Machine_info.Duration_time;
  2552. break;
  2553. default:
  2554. break;
  2555. }
  2556. // ESP_LOGE(LOG_TAG,"button_info = %d ,time_min = %ld ---%ld - %ld - %ld - %ld - %ld - %ld",
  2557. // _0703_button_info.button_info,_0703_button_info.time_min,
  2558. // arr_heights[0],arr_heights[1],arr_heights[2],
  2559. // arr_heights[3],arr_heights[4],arr_heights[5]);
  2560. }
  2561. // void periodic_timer_callback(void* arg)
  2562. // {
  2563. // // if(++Machine_info.sec == 60)
  2564. // // {
  2565. // // last_button_info.time_min+=1;
  2566. // // set_status_heights();
  2567. // // printf("time min ++\r\n");
  2568. // // printf("YEAR = %d,MONTH =%d,DAY=%d,HOUR=%d,MIN=%d,SEC =%d\r\n",
  2569. // // Machine_info.year,
  2570. // // Machine_info.month,
  2571. // // Machine_info.day,
  2572. // // Machine_info.hour,
  2573. // // Machine_info.min,
  2574. // // Machine_info.sec);
  2575. // // Machine_info.sec=0;
  2576. // // if(++Machine_info.min == 60)
  2577. // // {
  2578. // // Machine_info.min=0;
  2579. // // if(++Machine_info.hour == 24)
  2580. // // {
  2581. // // Machine_info.hour=0;
  2582. // // Machine_info.day++;
  2583. // // if(IS_FEB_MONTH && IS_LEAP_YEAR && IS_SMALL_MONTH && IS_BIG_MONTH)
  2584. // // {
  2585. // // Machine_info.day=0;
  2586. // // if(++Machine_info.month == 13)
  2587. // // {
  2588. // // Machine_info.month=1;
  2589. // // Machine_info.year++;
  2590. // // }
  2591. // // }
  2592. // // }
  2593. // // }
  2594. // // }
  2595. // }
  2596. void lora_timer_callback(void *arg)
  2597. {
  2598. ESP_LOGI(LOG_TAG, " lora_timer_callback");
  2599. if (xQueueSend(yc_data_queue, &yc_data, 0) != true)
  2600. {
  2601. ESP_LOGE(LOG_TAG, "yc_data_queue send is fail");
  2602. }
  2603. yc_data.len = 0;
  2604. yc_data.index = 0;
  2605. }
  2606. void timer_init(void)
  2607. {
  2608. // const esp_timer_create_args_t periodic_timer_args = {
  2609. // .callback = &periodic_timer_callback,
  2610. // /* name is optional, but may help identify the timer when debugging */
  2611. // .name = "periodic"
  2612. // };
  2613. // ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  2614. // /* The timer has been created but is not running yet */
  2615. // ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 1*1000*1000));
  2616. // lora timer
  2617. const esp_timer_create_args_t lora_timer_args = {
  2618. .callback = &lora_timer_callback,
  2619. /* argument specified here will be passed to timer callback function */
  2620. .name = "one-shot"};
  2621. ESP_ERROR_CHECK(esp_timer_create(&lora_timer_args, &lora_timer));
  2622. }
  2623. // void yc_timer_restart(void)
  2624. // {
  2625. // ESP_LOGI(LOG_TAG,"timer restart");
  2626. // ESP_ERROR_CHECK(esp_timer_stop(periodic_timer));
  2627. // ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 1*1000*1000));
  2628. // }
  2629. void lora_timer_start(void)
  2630. {
  2631. ESP_ERROR_CHECK(esp_timer_start_once(lora_timer, 500 * 1000)); // 500ms
  2632. }
  2633. void lora_tiemr_stop(void)
  2634. {
  2635. ESP_ERROR_CHECK(esp_timer_stop(lora_timer));
  2636. }
  2637. void lora_timer_restart(void)
  2638. {
  2639. ESP_ERROR_CHECK(esp_timer_stop(lora_timer));
  2640. ESP_ERROR_CHECK(esp_timer_start_once(lora_timer, 500 * 1000)); // 500ms
  2641. }
  2642. #if 1
  2643. /*********************************************************************************
  2644. * function : analysis_protocol
  2645. * Description : lora 接受数据解析函数
  2646. * Input :
  2647. * Output :
  2648. * Author : 祁鑫 Data : 2023 9.18
  2649. **********************************************************************************/
  2650. void analysis_protocol(char *result, int len)
  2651. {
  2652. int index = 0;
  2653. int length = 0;
  2654. int count = len;
  2655. int num = 0;
  2656. while (count)
  2657. {
  2658. if (((result[index + 2]) == 0x5A) && ((result[index + 3]) == 0x51))
  2659. {
  2660. length = ((result[index]) << 8) | ((result[index + 1]));
  2661. num++;
  2662. printf("num =%d,length=%d\r\n", num, length);
  2663. #if 1
  2664. for (int i = 0; i < length; i++)
  2665. {
  2666. printf("%02x", result[index + 2 + i]);
  2667. }
  2668. #endif
  2669. printf("\r\n");
  2670. // 匹配mac地址 //计算校验和
  2671. if (
  2672. (Machine_info.mac_addr[0] == (result[index + 5])) &&
  2673. (Machine_info.mac_addr[1] == (result[index + 6])) &&
  2674. (Machine_info.mac_addr[2] == (result[index + 7])) &&
  2675. (Machine_info.mac_addr[3] == (result[index + 8])) &&
  2676. (Machine_info.mac_addr[4] == (result[index + 9])) &&
  2677. (Machine_info.mac_addr[5] == (result[index + 10])))
  2678. {
  2679. // mac地址匹配
  2680. printf("mac check ok\r\n");
  2681. // packetparsing((unsigned char *)&result[index+2]); //16进制解析数据
  2682. }
  2683. else
  2684. {
  2685. printf("mac = %02x%02x%02x%02x%02x%02x\r\n",
  2686. result[index + 5],
  2687. result[index + 6],
  2688. result[index + 7],
  2689. result[index + 8],
  2690. result[index + 9],
  2691. result[index + 10]);
  2692. printf("device mac = %02x%02x%02x%02x%02x%02x\r\n",
  2693. Machine_info.mac_addr[0],
  2694. Machine_info.mac_addr[1],
  2695. Machine_info.mac_addr[2],
  2696. Machine_info.mac_addr[3],
  2697. Machine_info.mac_addr[4],
  2698. Machine_info.mac_addr[5]);
  2699. // mac地址匹配
  2700. printf("mac check fail\r\n");
  2701. }
  2702. }
  2703. {
  2704. index++;
  2705. }
  2706. count--;
  2707. }
  2708. // printf("num =%d\r\n",num);
  2709. #if 0
  2710. if(left_display_refresh == true)
  2711. {
  2712. left_display_refresh = false;
  2713. PIC_display((unsigned char *)dspbuf_left);
  2714. }
  2715. reflashdfpr(dspbuf);
  2716. #endif
  2717. }
  2718. /*********************************************************************************
  2719. * function : selectionSort
  2720. * Description : 进行数据排序并输出排序后的数据 从小到大的索引
  2721. * Input :
  2722. * Output :
  2723. * Author : 祁鑫 Data : 2023 8.12
  2724. **********************************************************************************/
  2725. void selectionSort(int arr[], int n, int indices[])
  2726. {
  2727. for (int i = 0; i < n - 1; i++)
  2728. {
  2729. int minIndex = i;
  2730. for (int j = i + 1; j < n; j++)
  2731. {
  2732. if (arr[j] < arr[minIndex])
  2733. {
  2734. minIndex = j;
  2735. }
  2736. }
  2737. // 交换元素
  2738. int temp = arr[i];
  2739. arr[i] = arr[minIndex];
  2740. arr[minIndex] = temp;
  2741. // 更新索引
  2742. int tempIndex = indices[i];
  2743. indices[i] = indices[minIndex];
  2744. indices[minIndex] = tempIndex;
  2745. }
  2746. }
  2747. /*********************************************************************************
  2748. * function : person_get_num_is_exist
  2749. * Description : 获取当前类型序号是否存在
  2750. * Input :
  2751. * Output :
  2752. * Author : 祁鑫 Data : 2023 8.12
  2753. **********************************************************************************/
  2754. int person_get_num_is_exist(uint8_t type, uint16_t num)
  2755. {
  2756. // uint8_t index = 0;
  2757. // printf("person_get_num_is_exist %d\r\n",num);
  2758. if (type == person[type].person_type)
  2759. {
  2760. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2761. {
  2762. if (person[type].person_name[i].person_num == num)
  2763. {
  2764. // printf("person_get_num is exist\r\n");
  2765. return 1;
  2766. }
  2767. }
  2768. }
  2769. // printf("person_get_num not exist\r\n");
  2770. return 0;
  2771. }
  2772. /*********************************************************************************
  2773. * function : person_get_num
  2774. * Description : 获取当前类型人员数量
  2775. * Input :
  2776. * Output :
  2777. * Author : 祁鑫 Data : 2023 8.12
  2778. **********************************************************************************/
  2779. int person_get_num(uint8_t type)
  2780. {
  2781. uint8_t index = 0;
  2782. // printf("person_get_num\r\n");
  2783. if (type == person[type].person_type) // 管理员
  2784. {
  2785. // 查找当前不为空的字符串数组
  2786. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2787. {
  2788. if (strlen((char *)&person[type].person_name[i].person_name) != 0)
  2789. {
  2790. index++;
  2791. }
  2792. }
  2793. }
  2794. return index;
  2795. }
  2796. /*********************************************************************************
  2797. * function : person_add
  2798. * Description : 人员添加/更新
  2799. * Input :
  2800. * Output :
  2801. * Author : 祁鑫 Data : 2023 8.12
  2802. **********************************************************************************/
  2803. void person_add(uint8_t type, uint8_t num, char *in, char *out)
  2804. {
  2805. // printf("person_add\r\n");
  2806. if (type == person[type].person_type) // 管理员
  2807. {
  2808. uint8_t index = 0;
  2809. // 查找当前不为空的字符串数组
  2810. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2811. {
  2812. // Serial.println(person[type].person_name[i].person_num);
  2813. // 如果序号存在更新
  2814. if (person[type].person_name[i].person_num == num)
  2815. {
  2816. // Serial.print("person_num exist = ");
  2817. // Serial.println(person[type].person_name[i].person_num);
  2818. index = i;
  2819. break;
  2820. }
  2821. // 如果序号不存在添加
  2822. if (strlen((char *)&person[type].person_name[i].person_name) == 0)
  2823. {
  2824. // Serial.print("person_num not exist = ");
  2825. index = i;
  2826. person[type].person_name[i].person_num = num;
  2827. // Serial.println(person[type].person_name[i].person_num);
  2828. break;
  2829. }
  2830. }
  2831. if (index > PERSON_MAX_NAME)
  2832. {
  2833. }
  2834. else
  2835. {
  2836. if (person[type].person_name[index].person_num == num)
  2837. {
  2838. memset(&person[type].person_name[index].person_name, 0x00, PERSON_NAME_MAX_LEN);
  2839. }
  2840. strcpy((char *)&person[type].person_name[index].person_name, in);
  2841. }
  2842. // Serial.println("show font");
  2843. // 开始显示数据
  2844. int sort_buffer[PERSON_MAX_NAME] = {0}; // 获取当前序号值
  2845. int indices[PERSON_MAX_NAME]; // 排序后的索引值
  2846. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2847. {
  2848. indices[i] = i;
  2849. }
  2850. // 拷贝序号数据
  2851. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2852. {
  2853. sort_buffer[i] = person[type].person_name[i].person_num;
  2854. }
  2855. selectionSort(sort_buffer, PERSON_MAX_NAME, indices);
  2856. #if 0
  2857. //Serial.print("Sorted array: ");
  2858. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2859. //Serial.print(sort_buffer[i]);
  2860. //Serial.print(",");
  2861. }
  2862. //Serial.println("");
  2863. //Serial.print("\nOriginal indices: ");
  2864. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2865. //Serial.print(indices[i]);
  2866. //Serial.print(",");
  2867. }
  2868. //Serial.println("");
  2869. #endif
  2870. #if 0
  2871. //进行序号排序
  2872. bubbleSort(sort_buffer, PERSON_MAX_NAME);
  2873. #endif
  2874. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2875. {
  2876. int display_index = 0;
  2877. display_index = indices[i]; // 获取当前显示的索引
  2878. // Serial.println(person[type].person_name[display_index].person_num);
  2879. if (strlen((char *)&person[type].person_name[display_index].person_name) != 0)
  2880. {
  2881. strcat(out, (char *)&person[type].person_name[display_index].person_name);
  2882. strcat(out, " ");
  2883. }
  2884. }
  2885. }
  2886. }
  2887. /*********************************************************************************
  2888. * function : person_del
  2889. * Description : 人员删除
  2890. * Input :
  2891. * Output :
  2892. * Author : 祁鑫 Data : 2023 8.12
  2893. **********************************************************************************/
  2894. void person_del(uint8_t type, uint8_t num, char *in, char *out)
  2895. {
  2896. // printf("person_del\r\n");
  2897. // Serial.print("delete num ");
  2898. // Serial.println(num);
  2899. if (type == person[type].person_type)
  2900. {
  2901. uint8_t index = 0;
  2902. // 查找当前不为空的字符串数组
  2903. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2904. {
  2905. // 如果序号存在
  2906. if (person[type].person_name[i].person_num == num)
  2907. {
  2908. printf("person_num exist \r\n");
  2909. // Serial.println(person[type].person_name[i].person_num);
  2910. index = i;
  2911. break;
  2912. }
  2913. // return;
  2914. }
  2915. {
  2916. if ((person[type].person_name[index].person_num == num))
  2917. {
  2918. // 清空显示
  2919. // Serial.println("delete index data");
  2920. printf("delete index data \r\n");
  2921. person[type].person_name[index].person_num = 0;
  2922. memset(&person[type].person_name[index].person_name, 0x00, PERSON_NAME_MAX_LEN);
  2923. }
  2924. // strcpy((char *)&person[type].person_name[index].person_name,in);
  2925. }
  2926. // Serial.println("show font");
  2927. #if 0
  2928. for(int i = 0;i<PERSON_MAX_NAME;i++)
  2929. {
  2930. Serial.println(person[type].person_name[i].person_num);
  2931. if(strlen((char *)&person[type].person_name[i].person_name)!=0)
  2932. {
  2933. strcat(out,(char *)&person[type].person_name[i].person_name);
  2934. strcat(out," ");
  2935. }
  2936. }
  2937. #else
  2938. // 开始显示数据
  2939. int sort_buffer[PERSON_MAX_NAME] = {0}; // 获取当前序号值
  2940. int indices[PERSON_MAX_NAME]; // 排序后的索引值
  2941. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2942. {
  2943. indices[i] = i;
  2944. }
  2945. // 拷贝序号数据
  2946. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2947. {
  2948. sort_buffer[i] = person[type].person_name[i].person_num;
  2949. }
  2950. selectionSort(sort_buffer, PERSON_MAX_NAME, indices);
  2951. #if 0
  2952. Serial.print("Sorted array: ");
  2953. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2954. Serial.print(sort_buffer[i]);
  2955. Serial.print(",");
  2956. }
  2957. Serial.println("");
  2958. Serial.print("\nOriginal indices: ");
  2959. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2960. Serial.print(indices[i]);
  2961. Serial.print(",");
  2962. }
  2963. Serial.println("");
  2964. #endif
  2965. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2966. {
  2967. int display_index = 0;
  2968. display_index = indices[i]; // 获取当前显示的索引
  2969. // Serial.println(person[type].person_name[display_index].person_num);
  2970. if (strlen((char *)&person[type].person_name[display_index].person_name) != 0)
  2971. {
  2972. strcat(out, (char *)&person[type].person_name[display_index].person_name);
  2973. strcat(out, " ");
  2974. printf("out = %s\r\n", out);
  2975. }
  2976. }
  2977. #endif
  2978. }
  2979. }
  2980. #endif
  2981. void loro_gwpair_ack(int destaddr, int sourceaddr, int cmd, int st, char *strd)
  2982. {
  2983. uint8_t strlorabuf[50];
  2984. int crc16 = 0, len, i;
  2985. memset(strlorabuf, 0, 50);
  2986. // 源地址 是指网关地址
  2987. // 帧头 目标地址 源地址(id) 长度 模式 具体内容 校验
  2988. strlorabuf[0] = 0x55;
  2989. strlorabuf[1] = 0xaa;
  2990. strlorabuf[2] = destaddr & 0xff;
  2991. strlorabuf[3] = sourceaddr & 0xff;
  2992. strlorabuf[4] = 0x02;
  2993. strlorabuf[5] = cmd;
  2994. strlorabuf[6] = st;
  2995. strcpy((char *)(strlorabuf + 7), strd); // mac+c_id
  2996. crc16 = strlorabuf[0] + strlorabuf[1] + strlorabuf[2] + strlorabuf[3] + strlorabuf[4] + strlorabuf[5] + strlorabuf[6];
  2997. len = strlen(strd);
  2998. strlorabuf[4] = 4 + len;
  2999. for (i = 0; i < len; i++)
  3000. crc16 = crc16 + strlorabuf[7 + len];
  3001. strlorabuf[7 + len] = (crc16 >> 8) & 0xff;
  3002. strlorabuf[8 + len] = crc16 & 0xff;
  3003. strlorabuf[9 + len] = 0;
  3004. #if 0
  3005. send_lora_data(strlorabuf,9+len);
  3006. #else
  3007. // loraSerial.write((char *)strlorabuf,len);
  3008. lora_send_data((char *)strlorabuf, len);
  3009. #endif
  3010. }
  3011. void lora_rev_cmd_send_ack()
  3012. {
  3013. }
  3014. void right_screen_send()
  3015. {
  3016. screen_dis_info.is_left = false;
  3017. screen_dis_info.is_into_sleep = true;
  3018. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  3019. {
  3020. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  3021. }
  3022. }
  3023. void user_compare(int last, int now) // 这个函数用来比较rssi和 电量变化 刷右屏//
  3024. {
  3025. if ((last == now) || (Machine_info.power_status == 0))
  3026. {
  3027. // ESP_LOGW(LOG_TAG,"last[%d]-now[%d]",last,now);
  3028. return;
  3029. }
  3030. // printf("user_compare last[%d] now[%d] \n",last,now);
  3031. bool is_send_right_screen = false;
  3032. switch (last)
  3033. {
  3034. case 100:
  3035. if (now != 100)
  3036. {
  3037. is_send_right_screen = true;
  3038. }
  3039. break;
  3040. case 75 ... 99:
  3041. if ((now == 100) || (now < 75))
  3042. {
  3043. is_send_right_screen = true;
  3044. }
  3045. break;
  3046. case 50 ... 74:
  3047. if ((now > 74) || (now < 50))
  3048. {
  3049. is_send_right_screen = true;
  3050. }
  3051. break;
  3052. case 25 ... 49:
  3053. if ((now > 49) || (now < 25))
  3054. {
  3055. is_send_right_screen = true;
  3056. }
  3057. break;
  3058. case 0 ... 24:
  3059. if ((now == 0) || (now > 24))
  3060. {
  3061. is_send_right_screen = true;
  3062. }
  3063. break;
  3064. default:
  3065. ESP_LOGE(LOG_TAG, "err:not 0~100");
  3066. break;
  3067. }
  3068. if (is_send_right_screen)
  3069. right_screen_send();
  3070. }
  3071. void user_compare_power_off(int last, int now) // 这个函数用来比较关机状态时电量变化 刷右屏//
  3072. {
  3073. if ((last == now))
  3074. {
  3075. // ESP_LOGW(LOG_TAG,"last[%d]-now[%d]",last,now);
  3076. return;
  3077. }
  3078. bool is_dis = false;
  3079. switch (last)
  3080. {
  3081. case 100:
  3082. if (now != 100)
  3083. {
  3084. gpio_hold_dis(PIN_R_CS);
  3085. is_dis = true;
  3086. }
  3087. break;
  3088. case 75 ... 99:
  3089. if ((now == 100) || (now < 75))
  3090. {
  3091. gpio_hold_dis(PIN_R_CS);
  3092. is_dis = true;
  3093. }
  3094. break;
  3095. case 50 ... 74:
  3096. if ((now > 74) || (now < 50))
  3097. {
  3098. gpio_hold_dis(PIN_R_CS);
  3099. is_dis = true;
  3100. }
  3101. break;
  3102. case 25 ... 49:
  3103. if ((now > 49) || (now < 25))
  3104. {
  3105. gpio_hold_dis(PIN_R_CS);
  3106. is_dis = true;
  3107. }
  3108. break;
  3109. case 0 ... 24:
  3110. if ((now == 0) || (now > 24))
  3111. {
  3112. gpio_hold_dis(PIN_R_CS);
  3113. is_dis = true;
  3114. }
  3115. break;
  3116. default:
  3117. ESP_LOGE(LOG_TAG, "err:not 0~100");
  3118. break;
  3119. }
  3120. if (is_dis)
  3121. {
  3122. screen_dis_info.is_left = false;
  3123. screen_dis_info.is_into_sleep = true;
  3124. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  3125. {
  3126. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  3127. }
  3128. }
  3129. }