yc_protocol.c 119 KB

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