yc_protocol.c 119 KB

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