yc_protocol.c 121 KB

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