yc_protocol.c 118 KB

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