yc_protocol.c 122 KB

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