yc_protocol.c 119 KB

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