yc_protocol.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603
  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. RTC_NOINIT_ATTR 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. ESP_LOGE(LOG_TAG, "%d %d %d %d %d ",is_into_sleep,is_left,is_change_power,is_dont_dis,ms);
  134. screen_dis_info.is_into_sleep = is_into_sleep;
  135. screen_dis_info.is_left = is_left;
  136. screen_dis_info.is_change_power = is_change_power;
  137. screen_dis_info.is_dont_dis = is_dont_dis;
  138. screen_dis_info.sleep_ms = ms;
  139. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  140. {
  141. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  142. }
  143. }
  144. void terminal_send_data(void)
  145. {
  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 = 4; // 刷两个屏最好不同时慢刷
  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_LOGD(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_LOGD(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_LOGD(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_LOGD(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_LOGD(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. ESP_LOG_BUFFER_HEX("send ack to gateway", strlorabuf, 15);
  964. lora_send_data((char *)strlorabuf, 15);
  965. uart_wait_tx_idle_polling(UART_NUM_1);
  966. }
  967. return 15;
  968. }
  969. /*硬件配网回复*/ // 参数cid cid长度 返回result 结果及长度
  970. int reply_gwpair_ack_func(uint8_t *cid, uint8_t cid_len, uint8_t *result)
  971. {
  972. uint8_t strlorabuf[50] = {0};
  973. uint8_t mac[6] = {0};
  974. strlorabuf[0] = 0x5A;
  975. strlorabuf[1] = 0x51;
  976. strlorabuf[2] = 0x00; // crc
  977. // int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
  978. for (int i = 0; i < 6; i++)
  979. {
  980. strlorabuf[3 + i] = Machine_info.gateway_mac[i]; // 网关的mac地址
  981. }
  982. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  983. strlorabuf[10] = 0x80; // 配对命令
  984. strlorabuf[11] = 0x01; // 总包
  985. strlorabuf[12] = 0x01; // 当前包
  986. strlorabuf[13] = 0x00; // 当前长度
  987. strlorabuf[14] = 0x01; // 请求成功
  988. // 设备cid长度
  989. memcpy(&strlorabuf[15], cid, cid_len); // 拷贝cid
  990. strlorabuf[13] = cid_len + 1; // 重新设置当前当前长度
  991. int crc = 0; // 计算crc
  992. for (int i = 0; i < sizeof(strlorabuf); i++)
  993. {
  994. crc += strlorabuf[i];
  995. }
  996. strlorabuf[2] = crc;
  997. memcpy(result, strlorabuf, 14 + cid_len + 1);
  998. return 14 + cid_len + 1;
  999. }
  1000. /*硬件配网*/
  1001. static void hardware_gwpair_func(const LORA_DATA_T *buf)
  1002. {
  1003. uint8_t child_mac[6] = {0}; // 子设备的mac地址
  1004. uint8_t gateway_mac[6] = {0}; // 网关的组内编号
  1005. int child_num = 0;
  1006. int gateway_num = 0;
  1007. #if 0
  1008. for(int i = 0;i<buf->data_len;i++)
  1009. {
  1010. printf("%02X",buf->data_buf[i]);
  1011. }
  1012. #endif
  1013. uint64_t time = 0x000000000000LL;
  1014. // printf("1\r\n");
  1015. memcpy(child_mac, &buf->data_buf[3], 6);
  1016. // printf("2\r\n");
  1017. gateway_num = buf->data_buf[9];
  1018. // printf("3\r\n");
  1019. memcpy(gateway_mac, &buf->data_buf[14], 6);
  1020. // printf("4\r\n");
  1021. child_num = buf->data_buf[20];
  1022. // printf("5\r\n");
  1023. // printf("\r\n");
  1024. // for(int i = 21;i<27;i++)
  1025. // {
  1026. // printf("%02X",buf->data_buf[i]);
  1027. // }
  1028. #if 0
  1029. #include <inttypes.h>
  1030. time|=(buf->data_buf[26]<<0);
  1031. //printf("time = %" PRIX64 "\r\n",time);
  1032. time|=(buf->data_buf[25]<<8);
  1033. //printf("time = %" PRIX64 "\r\n",time);
  1034. time|=(buf->data_buf[24]<<16);
  1035. //printf("time = %" PRIX64 "\r\n",time);
  1036. time|=(buf->data_buf[23]<<24);
  1037. //printf("time = %" PRIX64 "\r\n",time);
  1038. #if 0
  1039. uint64_t temp = 0;
  1040. temp |=(buf->data_buf[22]<<32);
  1041. temp |=(buf->data_buf[21]<<40);
  1042. printf("temp = %" PRIX64 "\r\n",temp);
  1043. #endif
  1044. #if 1
  1045. int64_t temp = 0;
  1046. temp = buf->data_buf[22];
  1047. time|=(temp<<32);//(buf->data_buf[22]<<32);
  1048. #else
  1049. time|=temp;
  1050. #endif
  1051. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1052. #if 0
  1053. time|=(buf->data_buf[21]<<40);
  1054. #else
  1055. temp = 0;
  1056. temp = buf->data_buf[21];
  1057. time|=(temp<<40);//(buf->data_buf[22]<<32);
  1058. //time|=(buf->data_buf[21]<<40);
  1059. #endif
  1060. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1061. //uint64_t value = 0x018B1E3D24AC;
  1062. //printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
  1063. //printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
  1064. #if 0
  1065. time=(buf->data_buf[21]<<40)+
  1066. (buf->data_buf[22]<<32)+
  1067. (buf->data_buf[23]<<24)+
  1068. (buf->data_buf[24]<<16)+
  1069. (buf->data_buf[25]<<8) +
  1070. (buf->data_buf[26]<<0);
  1071. #endif
  1072. #else
  1073. Machine_info.lora_new_channel = buf->data_buf[21];
  1074. printf("Machine_info.lora_new_channel =%d\r\n", Machine_info.lora_new_channel);
  1075. memcpy(Machine_info.timestamp, &buf->data_buf[22], 13);
  1076. printf("Machine_info.timestamp =%s\r\n", Machine_info.timestamp);
  1077. int timezone_offset = 8; // 中国时区偏移为UTC+8,你可以根据需要设置不同的时区偏移
  1078. Machine_info.time_offset = timezone_offset;
  1079. timestamp_to_local_time(Machine_info.timestamp,
  1080. Machine_info.time_offset,
  1081. &Machine_info.year,
  1082. &Machine_info.month,
  1083. &Machine_info.day,
  1084. &Machine_info.hour,
  1085. &Machine_info.min,
  1086. &Machine_info.sec);
  1087. setRtcTime(Machine_info.year, Machine_info.month, Machine_info.day,
  1088. Machine_info.hour, Machine_info.min, Machine_info.sec);
  1089. // 更新当前状态时间
  1090. Machine_info.current_button.Year = Machine_info.year;
  1091. Machine_info.current_button.Month = Machine_info.month;
  1092. Machine_info.current_button.Day = Machine_info.day;
  1093. Machine_info.current_button.Hour = Machine_info.hour;
  1094. Machine_info.current_button.Minute = Machine_info.min;
  1095. Machine_info.current_button.Second = Machine_info.sec;
  1096. Machine_info.last_button.Year = Machine_info.year;
  1097. Machine_info.last_button.Month = Machine_info.month;
  1098. Machine_info.last_button.Day = Machine_info.day;
  1099. Machine_info.last_button.Hour = Machine_info.hour;
  1100. Machine_info.last_button.Minute = Machine_info.min;
  1101. Machine_info.last_button.Second = Machine_info.sec;
  1102. #endif
  1103. printf("gateway mac = ");
  1104. for (int i = 0; i < 6; i++)
  1105. printf("%02X", gateway_mac[i]);
  1106. printf("\r\n");
  1107. printf("gateway num = %d\r\n", gateway_num);
  1108. printf("child mac = ");
  1109. for (int i = 0; i < 6; i++)
  1110. printf("%02X", child_mac[i]);
  1111. printf("\r\n");
  1112. printf("child_num num = %d\r\n", child_num);
  1113. printf("time = %llu\r\n", time);
  1114. printf("time = %llx\r\n", time);
  1115. //(3000 - (480 - 1*100))
  1116. #include "user_sleep.h"
  1117. #include "esp_sleep.h"
  1118. // 配网完成设置休眠唤醒时间
  1119. if (Machine_info.eflagID == 0xff)
  1120. {
  1121. #if 0
  1122. printf("sleep time = %d ms\r\n",(TIMER_WAKEUP_TIME_US*child_num)/1000);
  1123. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*child_num); //配置当前休眠的唤醒时间
  1124. #else
  1125. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US); // 配置当前休眠的唤醒时间
  1126. #endif
  1127. }
  1128. Machine_info.eflagID = child_num; // 保存当前的子设备组内编号
  1129. memcpy(Machine_info.gateway_mac, gateway_mac, 6);
  1130. // 配网成功 回复 ACK 设置新的信道
  1131. Machine_info.paired = 1; // 已配对
  1132. #if 0
  1133. send_pair_ack();
  1134. #else
  1135. int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除
  1136. printf("msg_id =%X\r\n", msg_id);
  1137. reply_ack_func(PROTOCOL_HARDWARE_GWPAIRED, msg_id, true, NULL); // 回复ack
  1138. #endif
  1139. // vTaskDelay(200 / portTICK_PERIOD_MS);
  1140. // //修改信道前修改设备ID 及 设备ID
  1141. // dymatic_change_device_id(child_num+1);
  1142. // dymatic_change_dst_device_id(0x00000001);
  1143. // //切换信道
  1144. // dymatic_change_chanel(Machine_info.lora_new_channel); //切换信道
  1145. // lora_get_rssi();
  1146. vTaskDelay(200 / portTICK_PERIOD_MS);
  1147. Machine_info.rssi = set_lora(Machine_info.lora_new_channel, Machine_info.eflagID);
  1148. f_send_version();
  1149. f_send_reply_status(0x01); // 回复在线状态
  1150. f_send_update_status();
  1151. f_send_battary_vaule(Machine_info.batt_precent);
  1152. #if 0
  1153. f_send_get_chart_data();
  1154. #endif
  1155. // xQueueReset(screen_queue);
  1156. // 重置快刷
  1157. Machine_info.left_current_Quick_refresh_time = 0;
  1158. Machine_info.right_current_Quick_refresh_time = 0;
  1159. set_screen_dis_info_and_send_queue(false, true, false, false, 100);
  1160. set_screen_dis_info_and_send_queue(true, false, false, false, 100);
  1161. // printf("start timer id = %d,%d\r\n",Machine_info.eflagID,Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  1162. // Already_send_timer_start(Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  1163. }
  1164. /*硬件取消配网*/
  1165. static void hardware_ungwpair_func(const LORA_DATA_T *buf)
  1166. {
  1167. uint8_t child_mac[6] = {0}; // 子设备的mac地址
  1168. uint8_t gateway_mac[6] = {0}; // 网关的组内编号
  1169. int child_num = 0;
  1170. int gateway_num = 0;
  1171. uint64_t time = 0x000000000000LL;
  1172. memcpy(child_mac, &buf->data_buf[3], 6);
  1173. gateway_num = buf->data_buf[9];
  1174. memcpy(gateway_mac, &buf->data_buf[14], 6);
  1175. child_num = buf->data_buf[20];
  1176. #include <inttypes.h>
  1177. time |= (buf->data_buf[26] << 0);
  1178. time |= (buf->data_buf[25] << 8);
  1179. time |= (buf->data_buf[24] << 16);
  1180. time |= (buf->data_buf[23] << 24);
  1181. #if 1
  1182. int64_t temp = 0;
  1183. temp = buf->data_buf[22];
  1184. time |= (temp << 32); //(buf->data_buf[22]<<32);
  1185. #else
  1186. time |= temp;
  1187. #endif
  1188. // printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1189. #if 0
  1190. time|=(buf->data_buf[21]<<40);
  1191. #else
  1192. temp = 0;
  1193. temp = buf->data_buf[21];
  1194. time |= (temp << 40); //(buf->data_buf[22]<<32);
  1195. // time|=(buf->data_buf[21]<<40);
  1196. #endif
  1197. // printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1198. // uint64_t value = 0x018B1E3D24AC;
  1199. // printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
  1200. // printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
  1201. printf("gateway mac = ");
  1202. for (int i = 0; i < 6; i++)
  1203. printf("%02X", gateway_mac[i]);
  1204. printf("\r\n");
  1205. printf("gateway num = %d\r\n", gateway_num);
  1206. printf("child mac = ");
  1207. for (int i = 0; i < 6; i++)
  1208. printf("%02X", child_mac[i]);
  1209. printf("\r\n");
  1210. printf("child_num num = %d\r\n", child_num);
  1211. printf("time = %llu\r\n", time);
  1212. printf("time = %llx\r\n", time);
  1213. //(3000 - (480 - 1*100))
  1214. #include "user_sleep.h"
  1215. #include "esp_sleep.h"
  1216. memcpy(Machine_info.gateway_mac, gateway_mac, 6);
  1217. // 取消配网成功 回复 ACK 设置新的信道
  1218. #if 0
  1219. send_pair_ack();
  1220. #else
  1221. int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除
  1222. // printf("msg_id =%X\r\n",msg_id);
  1223. reply_ack_func(PROTOCOL_HARDWARE_UNGWPAIRED, msg_id, true, NULL); // 回复ack
  1224. #endif
  1225. // #include "user_sleep.h"
  1226. // #include "esp_sleep.h"
  1227. extern void sleep_timer_stop();
  1228. extern void Already_send_timer_stop();
  1229. sleep_timer_stop();
  1230. Already_send_timer_stop();
  1231. // 修改信道前修改设备ID 及 设备ID
  1232. dymatic_change_device_id(0x00000001);
  1233. dymatic_change_dst_device_id(0xFFFFFFFF);
  1234. // 切换信道
  1235. reset_default(true, 1);
  1236. vTaskDelay(300 / portTICK_PERIOD_MS);
  1237. Machine_info.lora_new_channel = LORA_CHANENL;
  1238. // dymatic_change_chanel(Machine_info.lora_factory_channel); //切换信道
  1239. reset_lora(LORA_CHANENL);
  1240. }
  1241. /*网关收到终端发送的数据 ACK 终端收到用于删除当前的msgid的数据*/
  1242. static void hardware_ack_delete_msg_id(const LORA_DATA_T *buf)
  1243. {
  1244. int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除当前链表的数据
  1245. ESP_LOG_BUFFER_HEX("ACK", buf->data_buf, buf->data_len);
  1246. // int current = countNodes(Send_list); //获取当前节点数据
  1247. Node *find = findNode(Send_list, msg_id);
  1248. if (find != NULL)
  1249. {
  1250. ESP_LOGE(LOG_TAG, "find msg_id : %d", msg_id);
  1251. // printList(Send_list);
  1252. for (int i = 0; i < LORA_INFO_END; i++)
  1253. {
  1254. if (lora_pool[i].num == msg_id)
  1255. {
  1256. lora_pool[i].flag = false;
  1257. Send_list = deleteNode(Send_list, msg_id);
  1258. ESP_LOGE(LOG_TAG, "delete msg_id %d", msg_id);
  1259. }
  1260. }
  1261. // printf("delete list node msg_id = %d\r\n",msg_id);
  1262. // printf("delete list node after = %d\r\n",msg_id);
  1263. // printList(Send_list);
  1264. }
  1265. else
  1266. {
  1267. ESP_LOGE(LOG_TAG, "delete fail");
  1268. printList(Send_list);
  1269. }
  1270. // display_flag = 0;
  1271. }
  1272. void business_logic_func(const LORA_DATA_T *buf, uint8_t cmd_index, int msg)
  1273. {
  1274. // int cmd = 0; // 当前的命令
  1275. int msg_id = 0; // 消息的id
  1276. // bool is_rsp_ack = false;
  1277. #if 0
  1278. printf("cmd = %02X\r\n",buf->data_buf[cmd_index]);
  1279. #endif
  1280. // cmd = buf->data_buf[cmd_index];
  1281. switch (buf->data_buf[cmd_index])
  1282. {
  1283. case PROTOCOL_TIME_SYNCHRONIZATION:
  1284. msg_id = time_synchronization_func(buf);
  1285. // is_rsp_ack = true;
  1286. break;
  1287. case PROTOCOL_TERMINAL_NAME:
  1288. msg_id = terminal_name_func(buf);
  1289. // is_rsp_ack = true;
  1290. break;
  1291. case PROTOCOL_TERMINAL_NUMBER:
  1292. msg_id = terminal_number_func(buf);
  1293. // is_rsp_ack = true;
  1294. break;
  1295. case PROTOCOL_ELECTRIC_QUANTITY:
  1296. msg_id = electric_quantity_func(buf);
  1297. // is_rsp_ack = true;
  1298. break;
  1299. case PROTOCOL_SIGNAL_STRENGTH:
  1300. msg_id = signal_strength_func(buf);
  1301. // is_rsp_ack = true;
  1302. break;
  1303. case PROTOCOL_SCREEN_REFRESH_CYCLE:
  1304. msg_id = screen_refresh_cycle_func(buf);
  1305. // is_rsp_ack = true;
  1306. break;
  1307. case PROTOCOL_SCREEN_LEFT:
  1308. msg_id = screen_left_func(buf);
  1309. // is_rsp_ack = true;
  1310. break;
  1311. case PROTOCOL_SCREEN_RIGHT:
  1312. msg_id = screen_right_func(buf);
  1313. // is_rsp_ack = true;
  1314. break;
  1315. case PROTOCOL_RSPONSIBLE_PERSON_CHANGE:
  1316. msg_id = rsponsible_person_change_func(buf);
  1317. // is_rsp_ack = true;
  1318. break;
  1319. case PROTOCOL_RSPONSIBLE_PERSON_DELETE:
  1320. msg_id = rsponsible_person_delete_func(buf);
  1321. // is_rsp_ack = true;
  1322. break;
  1323. case PROTOCOL_PRODUCTION_PUNCH_CARD:
  1324. msg_id = production_punch_func(buf);
  1325. // is_rsp_ack = true;
  1326. break;
  1327. case PROTOCOL_PERSONNEL_CHECK_IN:
  1328. msg_id = personnel_check_in_func(buf);
  1329. // is_rsp_ack = true;
  1330. break;
  1331. case PROTOCOL_STATUS_SETTING:
  1332. msg_id = status_setting_func(buf);
  1333. // is_rsp_ack = true;
  1334. break;
  1335. case PROTOCOL_STATUS_DURATION:
  1336. msg_id = status_duration_func(buf);
  1337. // is_rsp_ack = true;
  1338. break;
  1339. case PROTOCOL_UPDATE_AND_DURATION:
  1340. msg_id = update_and_duration_func(buf);
  1341. // is_rsp_ack = true;
  1342. break;
  1343. case PROTOCOL_ANNOUNCEMENT:
  1344. msg_id = announcement_func(buf);
  1345. // is_rsp_ack = true;
  1346. break;
  1347. case PROTOCOL_CAPACITY_STATISTICS:
  1348. msg_id = capacity_statistics_func(buf);
  1349. // is_rsp_ack = true;
  1350. break;
  1351. case PROTOCOL_RESPONSIBLE_PERSON_SYNCHRONIZE_INFO:
  1352. msg_id = responsible_person_synchronize_info_func(buf);
  1353. // is_rsp_ack = true;
  1354. break;
  1355. case PROTOCOL_STATUS_LED:
  1356. msg_id = status_led_func(buf);
  1357. // is_rsp_ack = true;
  1358. break;
  1359. case PROTOCOL_LOG_LED:
  1360. msg_id = logo_led_func(buf);
  1361. // is_rsp_ack = true;
  1362. break;
  1363. case PROTOCOL_BATTERY_TEMPERATURE:
  1364. msg_id = battery_temperature_func(buf);
  1365. // is_rsp_ack = true;
  1366. break;
  1367. case PROTOCOL_STATION_NAME:
  1368. msg_id = station_name_func(buf);
  1369. // is_rsp_ack = true;
  1370. break;
  1371. case PROTOCOL_STATION_NUMBER:
  1372. msg_id = station_number_func(buf);
  1373. // is_rsp_ack = true;
  1374. break;
  1375. case PROTOCOL_RESPONSIBLE_PERSON_SETUP:
  1376. msg_id = responsible_person_setup_func(buf);
  1377. // is_rsp_ack = true;
  1378. break;
  1379. case PROTOCOL_SYSTEM_BULLETIN:
  1380. msg_id = system_bulletin_func(buf);
  1381. // is_rsp_ack = true;
  1382. break;
  1383. case PROTOCOL_HARDWARE_UPDATE:
  1384. msg_id = hardware_update_func(buf);
  1385. // is_rsp_ack = true;
  1386. break;
  1387. case PROTOCOL_HARDWARE_GWPAIRED: // 配网 暂时单独发送ack
  1388. reset_default(false, 1);
  1389. hardware_gwpair_func(buf);
  1390. // Paint_leftScreen_main_quick(&Machine_info);
  1391. // Paint_rightScreen_main_quick(&Machine_info);
  1392. break;
  1393. case PROTOCOL_HARDWARE_UNGWPAIRED: // 取消配网
  1394. printf("pair ungwpaired\r\n");
  1395. hardware_ungwpair_func(buf);
  1396. break;
  1397. case PROTOCOL_RSPONSIBLE_ACK:
  1398. // printf("receive gateway ack delete data in list\r\n");
  1399. hardware_ack_delete_msg_id(buf);
  1400. break;
  1401. default:
  1402. ESP_LOGE(LOG_TAG, "ERR:CMD = 0x%02x", buf->data_buf[cmd_index]);
  1403. break;
  1404. }
  1405. #if USER_NOT_SLEEP_ENABLE
  1406. send_can_I_receive(); // 发送可以接受的命令
  1407. #endif
  1408. }
  1409. void uincode2gbk(char *str, uint16_t len, char *out_buffer)
  1410. {
  1411. char strtmp[1024];
  1412. char str1[3];
  1413. unsigned int iunicode, i;
  1414. unsigned int igbk;
  1415. memset(strtmp, 0, 1024);
  1416. int index = 0;
  1417. i = 0;
  1418. int length = len;
  1419. while (length)
  1420. {
  1421. if ((str[i] == 0) && (str[i + 1]) < 0x80) // ASCII 码 *text是ASCII码
  1422. {
  1423. str1[0] = (str[i + 1]) & 0xff;
  1424. memcpy(&strtmp[index], str1, 1);
  1425. index += 1;
  1426. }
  1427. else
  1428. {
  1429. iunicode = (str[i] << 8) + str[i + 1];
  1430. igbk = U2G(iunicode);
  1431. str1[0] = (igbk >> 8) & 0xff;
  1432. str1[1] = igbk & 0xff;
  1433. memcpy(&strtmp[index], str1, 2);
  1434. index += 2;
  1435. }
  1436. length -= 2;
  1437. i += 2;
  1438. }
  1439. // ESP_LOGW(LOG_TAG,"index:%d",index);
  1440. memcpy(out_buffer, strtmp, index);
  1441. }
  1442. // 计算数组元素的总和
  1443. uint32_t sumArray(uint8_t array[], int size)
  1444. {
  1445. uint32_t sum = 0;
  1446. for (int i = 0; i < size; i++)
  1447. {
  1448. sum += array[i];
  1449. }
  1450. return sum;
  1451. }
  1452. void mac_to_hex(char *out_string, char *in_string)
  1453. {
  1454. sprintf(out_string, "%02X:%02X:%02X:%02X:%02X:%02X",
  1455. in_string[0],
  1456. in_string[1],
  1457. in_string[2],
  1458. in_string[3],
  1459. in_string[4],
  1460. in_string[5]);
  1461. // ESP_LOG_BUFFER_HEX("--->",out_string,17);
  1462. }
  1463. void qrcode_protocol_create(char *result, Machine_info_t info)
  1464. {
  1465. // char str_https[] = "https://www.yeechart.com/antnest_download.php?data=";
  1466. char str_https[] = "https://www.yeechart.com/yeechart_download.php?data=";
  1467. Qrcode_protocol_t Qcode;
  1468. char qrcode_protocol[1024] = {0};
  1469. Qcode.head[0] = 0x5a;
  1470. Qcode.head[1] = 0x51;
  1471. Qcode.CRC = 0x00;
  1472. Qcode.mac.cmd = 0x03;
  1473. Qcode.mac.len = 0x11;
  1474. esp_read_mac((uint8_t *)mac_addr, ESP_MAC_WIFI_STA);
  1475. mac_to_hex((char *)Qcode.mac.mac_addr, (char *)mac_addr);
  1476. // strcpy((char *)Qcode.mac.mac_addr,(char*)mac_addr);
  1477. Qcode.client.cmd = 0X04;
  1478. #if 1 // 硬件设备id (qx 设置)
  1479. strcpy((char *)Qcode.client.client_id, (char *)info.cid);
  1480. Qcode.client.len = strlen((char *)Qcode.client.client_id);
  1481. #else // 假硬件设备id
  1482. strcpy((char *)Qcode.client.client_id, "whc_test");
  1483. Qcode.client.len = strlen((char *)Qcode.client.client_id);
  1484. #endif
  1485. Qcode.product.cmd = 0x05;
  1486. strcpy((char *)Qcode.product.product, "020101");
  1487. Qcode.product.len = strlen((char *)Qcode.product.product);
  1488. Qcode.time.cmd = 0x61;
  1489. Qcode.time.len = 0x0E;
  1490. #if 1
  1491. Qcode.time.Year = info.year;
  1492. Qcode.time.Month = info.month;
  1493. Qcode.time.Day = info.day;
  1494. Qcode.time.Hour = info.hour;
  1495. Qcode.time.Minute = info.min;
  1496. Qcode.time.Second = info.sec;
  1497. #else
  1498. Qcode.time.Year = 2023;
  1499. Qcode.time.Month = 9;
  1500. Qcode.time.Day = 19;
  1501. Qcode.time.Hour = 19;
  1502. Qcode.time.Minute = 30;
  1503. Qcode.time.Second = 20;
  1504. #endif
  1505. int time_index = 0;
  1506. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X%02X", Qcode.time.Year >> 8, Qcode.time.Year & 0xff);
  1507. time_index += 2;
  1508. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Month);
  1509. time_index += 1;
  1510. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Day);
  1511. time_index += 1;
  1512. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Hour);
  1513. time_index += 1;
  1514. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Minute);
  1515. time_index += 1;
  1516. sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Second);
  1517. uint32_t crc = 0;
  1518. crc += Qcode.head[0];
  1519. crc += Qcode.head[1];
  1520. crc += sumArray((uint8_t *)&Qcode.mac, sizeof(Qcode.mac));
  1521. crc += Qcode.client.cmd;
  1522. crc += Qcode.client.len;
  1523. int len = 0, i = 0;
  1524. len = Qcode.client.len;
  1525. while (len)
  1526. {
  1527. crc += Qcode.client.client_id[i];
  1528. len--;
  1529. i++;
  1530. }
  1531. crc += Qcode.product.cmd;
  1532. crc += Qcode.product.len;
  1533. i = 0;
  1534. len = Qcode.product.len;
  1535. while (len)
  1536. {
  1537. if (Qcode.product.product[i] != '0')
  1538. {
  1539. crc += Qcode.product.product[i];
  1540. }
  1541. else
  1542. {
  1543. break;
  1544. }
  1545. len--;
  1546. i++;
  1547. }
  1548. i = 0;
  1549. len = Qcode.time.len;
  1550. while (len)
  1551. {
  1552. if (Qcode.time.time[i] != '0')
  1553. {
  1554. crc += Qcode.time.time[i];
  1555. }
  1556. else
  1557. {
  1558. break;
  1559. }
  1560. len--;
  1561. i++;
  1562. }
  1563. Qcode.CRC = crc & 0xff;
  1564. int index = 0;
  1565. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.head[0], Qcode.head[1]);
  1566. index += 2;
  1567. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.CRC);
  1568. index += 1;
  1569. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.mac.cmd, Qcode.mac.len);
  1570. index += 2;
  1571. // 转换16进制数组为ASCII字符串 mac 地址
  1572. for (int i = 0; i < Qcode.mac.len; i++)
  1573. {
  1574. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.mac.mac_addr[i]);
  1575. index += 1;
  1576. }
  1577. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.client.cmd, Qcode.client.len);
  1578. index += 2;
  1579. // 转换16进制数组为ASCII字符串 client id
  1580. for (int i = 0; i < Qcode.client.len; i++)
  1581. {
  1582. if (Qcode.client.client_id[i] != 0)
  1583. {
  1584. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.client.client_id[i]);
  1585. index += 1;
  1586. }
  1587. }
  1588. // 转换16进制数组为ASCII字符串 product
  1589. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.product.cmd, Qcode.product.len);
  1590. index += 2;
  1591. for (int i = 0; i < Qcode.product.len; i++)
  1592. {
  1593. // if(Qcode.product.product[i] != '0')
  1594. { // 这里产品id为0也需要
  1595. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.product.product[i]);
  1596. index += 1;
  1597. }
  1598. }
  1599. // 转换16进制数组为ASCII字符串 time
  1600. sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.time.cmd, Qcode.time.len);
  1601. index += 2;
  1602. for (int i = 0; i < Qcode.time.len; i++)
  1603. {
  1604. // if(Qcode.time.time[i] != '0')
  1605. { // 这里时间为0也需要
  1606. sprintf(qrcode_protocol + index * 2, "%02X", Qcode.time.time[i]);
  1607. index += 1;
  1608. }
  1609. }
  1610. strcpy(result, str_https);
  1611. strcat(result, qrcode_protocol);
  1612. // ESP_LOGW(LOG_TAG,"二维码信息:%s",result);
  1613. }
  1614. // 函数用于查找字节序列在数组中出现的次数,并返回出现位置的索引
  1615. int findByteSequence(const u8 *array, int arraySize, const u8 *sequence, int sequenceSize, int **positions)
  1616. {
  1617. int count = 0;
  1618. int *indices = NULL;
  1619. for (int i = 0; i <= arraySize - sequenceSize; i++)
  1620. {
  1621. // 检查当前位置开始的字节序列是否与目标序列匹配
  1622. int j;
  1623. for (j = 0; j < sequenceSize; j++)
  1624. {
  1625. if (array[i + j] != sequence[j])
  1626. {
  1627. break; // 不匹配,跳出循环
  1628. }
  1629. }
  1630. if (j == sequenceSize)
  1631. {
  1632. count++; // 找到匹配的序列
  1633. // 记录匹配位置的索引
  1634. int *temp = (int *)realloc(indices, count * sizeof(int));
  1635. if (temp == NULL)
  1636. {
  1637. // 内存分配失败,处理错误
  1638. if (indices != NULL)
  1639. {
  1640. free(indices);
  1641. }
  1642. return -1;
  1643. }
  1644. indices = temp;
  1645. indices[count - 1] = i;
  1646. }
  1647. }
  1648. *positions = indices; // 传递匹配位置的索引数组
  1649. return count;
  1650. }
  1651. // 匹配mac地址是否为本机
  1652. bool mac_is_true(uint8_t *mac)
  1653. {
  1654. if (
  1655. (mac[0] == Machine_info.mac_addr[0]) &&
  1656. (mac[1] == Machine_info.mac_addr[1]) &&
  1657. (mac[2] == Machine_info.mac_addr[2]) &&
  1658. (mac[3] == Machine_info.mac_addr[3]) &&
  1659. (mac[4] == Machine_info.mac_addr[4]) &&
  1660. (mac[5] == Machine_info.mac_addr[5]))
  1661. {
  1662. return true;
  1663. }
  1664. return false;
  1665. }
  1666. bool subcontract(YC_DATA_T *data)
  1667. {
  1668. int rsp_msgid = 0;
  1669. int index = 0;
  1670. int all_len = data->len;
  1671. uint8_t *buf = data->data;
  1672. USE_DATA_T use_data;
  1673. bool refresh_flag = false;
  1674. u8 targetSequence[] = {0x5A, 0x51};
  1675. int sequenceSize = sizeof(targetSequence);
  1676. int *positions = NULL;
  1677. int result = findByteSequence(buf, all_len, targetSequence, sequenceSize, &positions);
  1678. if (result == 1)
  1679. {
  1680. printf("cmd 5A 51 find times %d,data len = %d\r\n", result, data->len);
  1681. for (int index = 0; index < result; index++)
  1682. {
  1683. if ((buf[0] == 0x5A) && (buf[1] == 0x51) && (result == 1))
  1684. {
  1685. // 单条命令处理逻辑
  1686. use_data.data_len = all_len; //(((buf[positions[index] - 2])<<8) | ((buf[positions[index] - 1])))+2;
  1687. memcpy(use_data.data_buf, data->data, use_data.data_len);
  1688. #if 0
  1689. printf("single cmd len =%d\r\n",use_data.data_len);
  1690. for(int i = 0;i<use_data.data_len;i++)
  1691. {
  1692. printf("%02X",use_data.data_buf[i]);
  1693. }
  1694. printf("\r\n");
  1695. #endif
  1696. bool is = mac_is_true(&use_data.data_buf[3]);
  1697. if (is)
  1698. {
  1699. business_logic_func(&use_data, USE_CMD_LEN_INDEX - 2, 0);
  1700. refresh_flag = true;
  1701. }
  1702. else
  1703. {
  1704. printf("mac is fail not deal\r\n");
  1705. }
  1706. }
  1707. else
  1708. {
  1709. rsp_msgid = (buf[13] << 8) | buf[14]; // 获取当前消息id 用于删除
  1710. printf("rsp_msgid %02x,%02x,%d\r\n", buf[13], buf[14], rsp_msgid);
  1711. for (int index = 0; index < result; index++)
  1712. {
  1713. // 多条指令处理逻辑
  1714. bool is = mac_is_true(&buf[positions[index] + 3]);
  1715. if (is) // 验证为本机,处理数据 回复ack
  1716. {
  1717. use_data.data_len = (((buf[positions[index] - 2]) << 8) | ((buf[positions[index] - 1]))) + 2;
  1718. memcpy(use_data.data_buf, data->data + (positions[index] - 2), use_data.data_len);
  1719. // print_yc_data(&use_data);
  1720. business_logic_func(&use_data, USE_CMD_LEN_INDEX, rsp_msgid);
  1721. refresh_flag = true;
  1722. reply_ack_func(0x00, rsp_msgid, true, NULL); // 回复ack
  1723. spiffs_write(&Machine_info);
  1724. ESP_LOGE(LOG_TAG, "1times cmd = %02X msg = %X start reply ack\r\n", 0x00, rsp_msgid);
  1725. }
  1726. else
  1727. {
  1728. printf("mac is fail not deal\r\n");
  1729. }
  1730. }
  1731. }
  1732. }
  1733. }
  1734. if (result > 1)
  1735. {
  1736. int data_len = 0;
  1737. data_len = (buf[0] << 8) | (buf[1]);
  1738. printf("cmd 5A 51 find times %d,all data len = %d\r\n", result, data_len);
  1739. printf("not single cmd len =%d\r\n", use_data.data_len);
  1740. rsp_msgid = (buf[13] << 8) | buf[14]; // 获取当前消息id 用于删除;
  1741. ESP_LOGW(LOG_TAG, "--rsp_msgid %02x,%02x,%d\r\n", buf[13], buf[14], rsp_msgid);
  1742. for (int index = 0; index < result; index++)
  1743. {
  1744. // 多条指令处理逻辑
  1745. bool is = mac_is_true(&buf[positions[index] + 3]);
  1746. if (is)
  1747. {
  1748. use_data.data_len = 15 + 2 + (((buf[positions[index] + 13]) << 8) | ((buf[positions[index] + 14])));
  1749. use_data.data_buf[0] = 0x00;
  1750. use_data.data_buf[1] = 0x00;
  1751. memcpy(&use_data.data_buf[2], data->data + (positions[index]), use_data.data_len + 15);
  1752. // printf("================last result============== len =%d\r\n",use_data.data_len);
  1753. // for(int i = 0;i<use_data.data_len;i++)
  1754. // {
  1755. // printf("%02x",use_data.data_buf[i]);
  1756. // }
  1757. // printf("================last result==============\r\n");
  1758. // print_yc_data(&use_data);
  1759. business_logic_func(&use_data, USE_CMD_LEN_INDEX, rsp_msgid);
  1760. refresh_flag = true;
  1761. reply_ack_func(0x00, rsp_msgid, true, NULL); // 回复ack
  1762. spiffs_write(&Machine_info);
  1763. ESP_LOGE(LOG_TAG, "cmd = %02X msg = %X start reply ack\r\n", 0x00, rsp_msgid);
  1764. }
  1765. else
  1766. {
  1767. printf("mac is fail not deal\r\n");
  1768. }
  1769. }
  1770. }
  1771. free(positions);
  1772. return refresh_flag;
  1773. }
  1774. // // len:有效数据长度,不是总长//
  1775. // static void protocol_package(char cmd, const uint8_t *info, int len)
  1776. // {
  1777. // #define __PACKAGE 15
  1778. // uint8_t *__str = (uint8_t *)malloc(sizeof(uint8_t) * (__PACKAGE + len) * 2);
  1779. // __str[0] = 0x5a;
  1780. // __str[1] = 0x51;
  1781. // __str[2] = 0x00;
  1782. // __str[3] = mac_addr[0];
  1783. // __str[4] = mac_addr[1];
  1784. // __str[5] = mac_addr[2];
  1785. // __str[6] = mac_addr[3];
  1786. // __str[7] = mac_addr[4];
  1787. // __str[8] = mac_addr[5];
  1788. // __str[9] = Machine_info.eflagID;
  1789. // __str[10] = cmd;
  1790. // #if 0
  1791. // __str[11] = 0x01;//当前只为一包
  1792. // __str[12] = 0x01;//当前只为一包
  1793. // #else
  1794. // // 随机数
  1795. // srand((unsigned int)time(NULL));
  1796. // int time_rand = rand() % 65535 + 1; // 生成1~100的随机数
  1797. // __str[11] = (time_rand >> 8) & 0xFF;
  1798. // __str[12] = time_rand & 0xFF;
  1799. // #endif
  1800. // // 分包
  1801. // __str[13] = (len >> 8) & 0xFF;
  1802. // __str[14] = len & 0xFF;
  1803. // memcpy(__str + __PACKAGE, info, len);
  1804. // int i = 0, sum = 0;
  1805. // for (i = 0; i < len + __PACKAGE; i++)
  1806. // {
  1807. // sum += __str[i];
  1808. // }
  1809. // __str[2] = sum & 0xFF;
  1810. // printf("crc=%02x ", __str[2]);
  1811. // printf("-->package:");
  1812. // for (i = 0; i < len + __PACKAGE; i++)
  1813. // {
  1814. // printf("%02x ", __str[i]);
  1815. // }
  1816. // printf("\n");
  1817. // changebintotxt(__str, len + __PACKAGE);
  1818. // #if 0
  1819. // Send_list = postInsert(Send_list, time_rand,cmd,(char *)__str,(int)(len+__PACKAGE)*2);//len*2
  1820. // #else
  1821. // lora_send_data((char *)__str, len + __PACKAGE);
  1822. // #endif
  1823. // free(__str);
  1824. // }
  1825. int txttobin(uint8_t *strlorabuf)
  1826. {
  1827. int len = 0, i = 0;
  1828. uint8_t strbuf[1024];
  1829. char ch = 0, chhi = 0, chlo = 0;
  1830. len = strlen((char *)strlorabuf);
  1831. strcpy((char *)strbuf, (char *)strlorabuf);
  1832. memset(strlorabuf, 0, len);
  1833. // Serial.print("jiexishuju:");Serial.println((char *)strbuf);
  1834. for (i = 0; i < len; i++)
  1835. {
  1836. ch = strbuf[i * 2];
  1837. if ((ch >= 0x30 && ch <= 0x39) ||
  1838. (ch >= 0x41 && ch <= 0x5a) ||
  1839. (ch >= 0x61 && ch <= 0x7a))
  1840. {
  1841. {
  1842. if (ch >= 0x30 && ch <= 0x39)
  1843. ch = ch - 0x30;
  1844. else if (ch >= 0x41 && ch <= 0x5a)
  1845. ch = ch - 0x37;
  1846. else
  1847. ch = ch - 0x57;
  1848. ch &= 0x000f;
  1849. chhi = (ch << 4);
  1850. }
  1851. ch = strbuf[i * 2 + 1];
  1852. if ((ch >= 0x30 && ch <= 0x39) ||
  1853. (ch >= 0x41 && ch <= 0x5a) ||
  1854. (ch >= 0x61 && ch <= 0x7a))
  1855. {
  1856. if (ch >= 0x30 && ch <= 0x39)
  1857. ch = ch - 0x30;
  1858. else if (ch >= 0x41 && ch <= 0x5a)
  1859. ch = ch - 0x37;
  1860. else
  1861. ch = ch - 0x57;
  1862. ch &= 0x000f;
  1863. ch = chhi + ch;
  1864. }
  1865. strlorabuf[i] = ch;
  1866. }
  1867. }
  1868. // Serial.write(strlorabuf,len);
  1869. return i;
  1870. }
  1871. void changebintotxt(uint8_t *strb, int len)
  1872. {
  1873. int i = 0;
  1874. uint8_t strtxt[512];
  1875. char ch, chhi, chlo;
  1876. memset(strtxt, 0, 512);
  1877. for (i = 0; i < len; i++)
  1878. {
  1879. ch = strb[i];
  1880. chhi = ch;
  1881. chhi >>= 4;
  1882. chhi &= 0x000f;
  1883. if (chhi <= 9)
  1884. chhi += 0x30;
  1885. else
  1886. chhi += 0x37;
  1887. strtxt[i * 2] = chhi;
  1888. chlo = ch;
  1889. chlo &= 0x000f;
  1890. if (chlo <= 9)
  1891. chlo += 0x30;
  1892. else
  1893. chlo += 0x37;
  1894. strtxt[i * 2 + 1] = chlo;
  1895. }
  1896. strcpy((char *)strb, (char *)strtxt);
  1897. }
  1898. /******************************
  1899.  //获取网关时间
  1900. ******************************/
  1901. void f_send_get_time(void)
  1902. {
  1903. if (lora_pool[LORA_INFO_GET_TIME].flag == true)
  1904. {
  1905. ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
  1906. return;
  1907. }
  1908. uint8_t strlorabuf[200];
  1909. uint8_t strnum[20];
  1910. static int rssiold = 0;
  1911. int crc16 = 0, i = 0, len = 0;
  1912. strlorabuf[0] = 0x5A;
  1913. strlorabuf[1] = 0x51; // 包头
  1914. strlorabuf[2] = 0x00; // 校验和
  1915. strlorabuf[3] = mac_addr[0];
  1916. strlorabuf[4] = mac_addr[1];
  1917. strlorabuf[5] = mac_addr[2];
  1918. strlorabuf[6] = mac_addr[3];
  1919. strlorabuf[7] = mac_addr[4];
  1920. strlorabuf[8] = mac_addr[5];
  1921. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  1922. strlorabuf[10] = 0x01; // cmd命令字
  1923. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  1924. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  1925. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  1926. strlorabuf[13] = 0x00;
  1927. strlorabuf[14] = 0x00; // 长度
  1928. strlorabuf[15] = 0;
  1929. crc16 = strlorabuf[0] + strlorabuf[1];
  1930. for (i = 3; i < 15; i++)
  1931. crc16 = crc16 + strlorabuf[i];
  1932. strlorabuf[2] = crc16 & 0xff;
  1933. changebintotxt(strlorabuf, 15); // Serial.println((char *)strlorabuf);
  1934. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  1935. len = strlen((char *)strlorabuf);
  1936. // lora_send_data((char *)strlorabuf,len);
  1937. lora_pool[LORA_INFO_GET_TIME].num = Machine_info.msg_id;
  1938. lora_pool[LORA_INFO_GET_TIME].flag = true;
  1939. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_GET_TIME, (char *)strlorabuf, len); // 插入数据
  1940. }
  1941. void f_send_get_chart_data(void)
  1942. {
  1943. if (lora_pool[LORA_INFO_GET_CHART].flag == true)
  1944. {
  1945. ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
  1946. return;
  1947. }
  1948. ESP_LOGW(LOG_TAG, "f_send_get_chart_data");
  1949. uint8_t strlorabuf[200];
  1950. int crc16 = 0, i = 0, len = 0;
  1951. strlorabuf[0] = 0x5A;
  1952. strlorabuf[1] = 0x51; // 包头
  1953. strlorabuf[2] = 0x00; // 校验和
  1954. strlorabuf[3] = mac_addr[0];
  1955. strlorabuf[4] = mac_addr[1];
  1956. strlorabuf[5] = mac_addr[2];
  1957. strlorabuf[6] = mac_addr[3];
  1958. strlorabuf[7] = mac_addr[4];
  1959. strlorabuf[8] = mac_addr[5];
  1960. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  1961. strlorabuf[10] = 0x11; // cmd命令字
  1962. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  1963. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  1964. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  1965. strlorabuf[13] = 0x00;
  1966. strlorabuf[14] = 0x00; // 长度
  1967. strlorabuf[15] = 0;
  1968. crc16 = strlorabuf[0] + strlorabuf[1];
  1969. for (i = 3; i < 15; i++)
  1970. crc16 = crc16 + strlorabuf[i];
  1971. strlorabuf[2] = crc16 & 0xff;
  1972. changebintotxt(strlorabuf, 15);
  1973. // Serial.println((char *)strlorabuf);
  1974. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  1975. len = strlen((char *)strlorabuf);
  1976. // lora_send_data((char *)strlorabuf,len);
  1977. lora_pool[LORA_INFO_GET_CHART].num = Machine_info.msg_id;
  1978. lora_pool[LORA_INFO_GET_CHART].flag = true;
  1979. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_GET_CHART, (char *)strlorabuf, len); // 插入数据
  1980. }
  1981. void f_send_version(void)
  1982. {
  1983. if (lora_pool[LORA_INFO_VERSION].flag == true)
  1984. {
  1985. ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
  1986. return;
  1987. }
  1988. ESP_LOGW(LOG_TAG, "f_send_version");
  1989. uint8_t strlorabuf[200];
  1990. int crc16 = 0, i = 0, len = 0;
  1991. strlorabuf[0] = 0x5A;
  1992. strlorabuf[1] = 0x51; // 包头
  1993. strlorabuf[2] = 0x00; // 校验和
  1994. strlorabuf[3] = mac_addr[0];
  1995. strlorabuf[4] = mac_addr[1];
  1996. strlorabuf[5] = mac_addr[2];
  1997. strlorabuf[6] = mac_addr[3];
  1998. strlorabuf[7] = mac_addr[4];
  1999. strlorabuf[8] = mac_addr[5];
  2000. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2001. strlorabuf[10] = 0x91; // cmd命令字
  2002. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2003. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2004. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2005. strlorabuf[13] = 0x00;
  2006. strlorabuf[14] = 0x03; // 长度
  2007. strlorabuf[15] = (VERSION_X) & 0xff;
  2008. strlorabuf[16] = (VERSION_Y) & 0xff;
  2009. strlorabuf[17] = (VERSION_Z) & 0xff;
  2010. strlorabuf[18] = 0;
  2011. crc16 = strlorabuf[0] + strlorabuf[1];
  2012. for (i = 3; i < 18; i++)
  2013. crc16 = crc16 + strlorabuf[i];
  2014. strlorabuf[2] = crc16 & 0xff;
  2015. changebintotxt(strlorabuf, 18);
  2016. // Serial.println((char *)strlorabuf);
  2017. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2018. len = strlen((char *)strlorabuf);
  2019. lora_pool[LORA_INFO_VERSION].num = Machine_info.msg_id;
  2020. lora_pool[LORA_INFO_VERSION].flag = true;
  2021. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_VERSION, (char *)strlorabuf, len); // 插入数据
  2022. }
  2023. void f_send_lora_rssi(int rssi)
  2024. {
  2025. if (lora_pool[LORA_INFO_RSSI].flag == true)
  2026. {
  2027. ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
  2028. return;
  2029. }
  2030. // ESP_LOGW(LOG_TAG,"f_send_lora_rssi:rssi = %d",rssi);
  2031. uint8_t strlorabuf[200];
  2032. int crc16 = 0, i = 0, len = 0;
  2033. strlorabuf[0] = 0x5A;
  2034. strlorabuf[1] = 0x51; // 包头
  2035. strlorabuf[2] = 0x00; // 校验和
  2036. strlorabuf[3] = mac_addr[0];
  2037. strlorabuf[4] = mac_addr[1];
  2038. strlorabuf[5] = mac_addr[2];
  2039. strlorabuf[6] = mac_addr[3];
  2040. strlorabuf[7] = mac_addr[4];
  2041. strlorabuf[8] = mac_addr[5];
  2042. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2043. strlorabuf[10] = 0x05; // cmd命令字
  2044. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2045. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2046. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2047. strlorabuf[13] = 0x00;
  2048. strlorabuf[14] = 0x01; // 长度
  2049. strlorabuf[15] = (rssi) & 0xff;
  2050. strlorabuf[16] = 0;
  2051. crc16 = strlorabuf[0] + strlorabuf[1];
  2052. for (i = 3; i < 16; i++)
  2053. crc16 = crc16 + strlorabuf[i];
  2054. strlorabuf[2] = crc16 & 0xff;
  2055. changebintotxt(strlorabuf, 16);
  2056. // Serial.println((char *)strlorabuf);
  2057. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2058. len = strlen((char *)strlorabuf);
  2059. lora_pool[LORA_INFO_RSSI].num = Machine_info.msg_id;
  2060. lora_pool[LORA_INFO_RSSI].flag = true;
  2061. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_RSSI, (char *)strlorabuf, len); // 插入数据
  2062. }
  2063. void f_send_test(void)
  2064. {
  2065. // ESP_LOGW(LOG_TAG,"f_send_lora_rssi:rssi = %d",rssi);
  2066. uint8_t strlorabuf[200];
  2067. int crc16 = 0, i = 0, len = 0;
  2068. strlorabuf[0] = 0x5A;
  2069. strlorabuf[1] = 0x51; // 包头
  2070. strlorabuf[2] = 0x00; // 校验和
  2071. strlorabuf[3] = mac_addr[0];
  2072. strlorabuf[4] = mac_addr[1];
  2073. strlorabuf[5] = mac_addr[2];
  2074. strlorabuf[6] = mac_addr[3];
  2075. strlorabuf[7] = mac_addr[4];
  2076. strlorabuf[8] = mac_addr[5];
  2077. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2078. strlorabuf[10] = 0x05; // cmd命令字
  2079. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2080. Machine_info.msg_id = 0x1234;
  2081. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2082. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2083. strlorabuf[13] = 0x00;
  2084. strlorabuf[14] = 0x01; // 长度
  2085. strlorabuf[15] = 0xab;
  2086. strlorabuf[16] = 0;
  2087. crc16 = strlorabuf[0] + strlorabuf[1];
  2088. for (i = 3; i < 16; i++)
  2089. crc16 = crc16 + strlorabuf[i];
  2090. strlorabuf[2] = crc16 & 0xff;
  2091. changebintotxt(strlorabuf, 16);
  2092. // Serial.println((char *)strlorabuf);
  2093. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2094. len = strlen((char *)strlorabuf);
  2095. // send_lora_data(strlorabuf, len);
  2096. // lora_send_data((char *)strlorabuf, len);
  2097. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_BATT, (char *)strlorabuf, len); // 插入数据
  2098. }
  2099. void f_send_battary_vaule(int battry)
  2100. {
  2101. if (lora_pool[LORA_INFO_BATT].flag == true)
  2102. {
  2103. ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
  2104. return;
  2105. }
  2106. uint8_t strlorabuf[200];
  2107. int crc16 = 0, i = 0, len = 0;
  2108. strlorabuf[0] = 0x5A;
  2109. strlorabuf[1] = 0x51; // 包头
  2110. strlorabuf[2] = 0x00; // 校验和
  2111. strlorabuf[3] = mac_addr[0];
  2112. strlorabuf[4] = mac_addr[1];
  2113. strlorabuf[5] = mac_addr[2];
  2114. strlorabuf[6] = mac_addr[3];
  2115. strlorabuf[7] = mac_addr[4];
  2116. strlorabuf[8] = mac_addr[5];
  2117. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2118. strlorabuf[10] = 0x04; // cmd命令字
  2119. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2120. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2121. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2122. strlorabuf[13] = 0x00;
  2123. strlorabuf[14] = 0x01; // 长度
  2124. strlorabuf[15] = (battry) & 0xff;
  2125. strlorabuf[16] = 0;
  2126. crc16 = strlorabuf[0] + strlorabuf[1];
  2127. for (i = 3; i < 16; i++)
  2128. crc16 = crc16 + strlorabuf[i];
  2129. strlorabuf[2] = crc16 & 0xff;
  2130. changebintotxt(strlorabuf, 16);
  2131. // Serial.println((char *)strlorabuf);
  2132. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2133. len = strlen((char *)strlorabuf);
  2134. lora_pool[LORA_INFO_BATT].num = Machine_info.msg_id;
  2135. lora_pool[LORA_INFO_BATT].flag = true;
  2136. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_BATT, (char *)strlorabuf, len); // 插入数据
  2137. }
  2138. void f_send_reply_status(int status)
  2139. {
  2140. if (lora_pool[LORA_INFO_BATT].flag == true)
  2141. {
  2142. ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
  2143. return;
  2144. }
  2145. uint8_t strlorabuf[200];
  2146. int crc16 = 0, i = 0, len = 0;
  2147. strlorabuf[0] = 0x5A;
  2148. strlorabuf[1] = 0x51; // 包头
  2149. strlorabuf[2] = 0x00; // 校验和
  2150. strlorabuf[3] = mac_addr[0];
  2151. strlorabuf[4] = mac_addr[1];
  2152. strlorabuf[5] = mac_addr[2];
  2153. strlorabuf[6] = mac_addr[3];
  2154. strlorabuf[7] = mac_addr[4];
  2155. strlorabuf[8] = mac_addr[5];
  2156. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2157. strlorabuf[10] = 0x06; // cmd命令字
  2158. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2159. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2160. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2161. strlorabuf[13] = 0x00;
  2162. strlorabuf[14] = 0x01; // 长度
  2163. strlorabuf[15] = (status) & 0xff;
  2164. strlorabuf[16] = 0;
  2165. crc16 = strlorabuf[0] + strlorabuf[1];
  2166. for (i = 3; i < 16; i++)
  2167. crc16 = crc16 + strlorabuf[i];
  2168. strlorabuf[2] = crc16 & 0xff;
  2169. changebintotxt(strlorabuf, 16);
  2170. // Serial.println((char *)strlorabuf);
  2171. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2172. len = strlen((char *)strlorabuf);
  2173. lora_pool[LORA_INFO_REPLY].num = Machine_info.msg_id;
  2174. lora_pool[LORA_INFO_REPLY].flag = true;
  2175. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_REPLY, (char *)strlorabuf, len); // 插入数据
  2176. }
  2177. // 唤醒时发送是否可以接收数据命令
  2178. void send_can_I_receive()
  2179. {
  2180. uint8_t strlorabuf[200];
  2181. strlorabuf[0] = 0x5A;
  2182. strlorabuf[1] = 0x51;
  2183. strlorabuf[2] = 0x51;
  2184. strlorabuf[3] = 0x5A;
  2185. strlorabuf[4] = 0x00;
  2186. #if 0
  2187. strlorabuf[5] = 0x01;
  2188. #else
  2189. strlorabuf[5] = Machine_info.eflagID;
  2190. if (Send_list != NULL)
  2191. {
  2192. strlorabuf[6] = 0x01;
  2193. }
  2194. else
  2195. {
  2196. strlorabuf[6] = 0x00;
  2197. }
  2198. strlorabuf[7] = Machine_info.mac_addr[0];
  2199. strlorabuf[8] = Machine_info.mac_addr[1];
  2200. strlorabuf[9] = Machine_info.mac_addr[2];
  2201. strlorabuf[10] = Machine_info.mac_addr[3];
  2202. strlorabuf[11] = Machine_info.mac_addr[4];
  2203. strlorabuf[12] = Machine_info.mac_addr[5];
  2204. int crc = 0;
  2205. for (int i = 0; i < 13; i++)
  2206. {
  2207. crc += strlorabuf[i];
  2208. }
  2209. strlorabuf[13] = crc & 0xff;
  2210. #endif
  2211. // send_lora_data(strlorabuf,len); //不直接发送
  2212. // printf("->can i\n");
  2213. ESP_LOG_BUFFER_HEX("->can i", strlorabuf, 14);
  2214. lora_send_data((char *)strlorabuf, 14);
  2215. }
  2216. // 发送是否可以接收数据命令
  2217. // void send_pair_ack()
  2218. // {
  2219. // uint8_t strlorabuf[200];
  2220. // strlorabuf[0] = 0x5A;
  2221. // strlorabuf[1] = 0x51;
  2222. // strlorabuf[2] = 0x51;
  2223. // strlorabuf[3] = 0x5A;
  2224. // strlorabuf[4] = 0x00;
  2225. // #if 0
  2226. // strlorabuf[5] = 0x01;
  2227. // #else
  2228. // strlorabuf[5] = Machine_info.eflagID;
  2229. // strlorabuf[6] = lora_channel + 1;
  2230. // #endif
  2231. // //send_lora_data(strlorabuf,len); //不直接发送
  2232. // lora_send_data((char *)strlorabuf,7);
  2233. // }
  2234. // 4bytes、32bit数据大小端转化
  2235. #define L2B32(Little) (Little = ((Little & 0xff) << 24) | (((Little) & 0xff00) << 8) | (((Little) & 0xff0000) >> 8) | ((Little >> 24) & 0xff))
  2236. uint8_t change_btn_reversal(uint8_t reversal_btn) // lora收到按键,转化为本地状态
  2237. {
  2238. uint8_t btn_tmp = 0;
  2239. switch (reversal_btn)
  2240. {
  2241. case 0:
  2242. btn_tmp = 1;
  2243. break;
  2244. case 1:
  2245. btn_tmp = 6;
  2246. break;
  2247. case 2:
  2248. btn_tmp = 4;
  2249. break;
  2250. case 3:
  2251. btn_tmp = 3;
  2252. break;
  2253. case 4:
  2254. btn_tmp = 5;
  2255. break;
  2256. case 5:
  2257. btn_tmp = 2;
  2258. break;
  2259. break;
  2260. default:
  2261. btn_tmp = 1;
  2262. break;
  2263. }
  2264. return btn_tmp;
  2265. }
  2266. uint8_t change_btn(uint8_t btn)
  2267. {
  2268. uint8_t btn_tmp = 0;
  2269. switch (btn)
  2270. {
  2271. case 1:
  2272. btn_tmp = 0;
  2273. break;
  2274. case 2:
  2275. btn_tmp = 5;
  2276. break;
  2277. case 3:
  2278. btn_tmp = 3;
  2279. break;
  2280. case 4:
  2281. btn_tmp = 2;
  2282. break;
  2283. case 5:
  2284. btn_tmp = 4;
  2285. break;
  2286. case 6:
  2287. btn_tmp = 1;
  2288. break;
  2289. default:
  2290. btn_tmp = 0;
  2291. break;
  2292. }
  2293. // ESP_LOGW(LOG_TAG,"btn = [%d] ,btn_tmp[%d]",btn,btn_tmp);
  2294. return btn_tmp;
  2295. }
  2296. /*发送状态持续的时间 状态持续时长(0x0E)*/
  2297. void f_send_status_duration(void)
  2298. {
  2299. if (lora_pool[LORA_INFO_DUR].flag == true)
  2300. {
  2301. return;
  2302. }
  2303. uint8_t strlorabuf[200];
  2304. int crc16 = 0, i = 0, len = 0;
  2305. // 帧头
  2306. strlorabuf[0] = 0x5A; // 包头
  2307. strlorabuf[1] = 0x51; // 包头
  2308. strlorabuf[2] = 0x00; // 校验和
  2309. strlorabuf[3] = mac_addr[0];
  2310. strlorabuf[4] = mac_addr[1];
  2311. strlorabuf[5] = mac_addr[2];
  2312. strlorabuf[6] = mac_addr[3];
  2313. strlorabuf[7] = mac_addr[4];
  2314. strlorabuf[8] = mac_addr[5];
  2315. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2316. strlorabuf[10] = 0x0E; // cmd命令字
  2317. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2318. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2319. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2320. strlorabuf[13] = 0x00;
  2321. strlorabuf[14] = 0x0C; // 长度
  2322. uint8_t tmp = change_btn(Machine_info.last_button.button_info);
  2323. strlorabuf[15] = tmp; // 上一次的状态
  2324. if ((Machine_info.last_button.Year == 0x00) && (Machine_info.last_button.Month == 0x00) && (Machine_info.last_button.Day == 0x00))
  2325. {
  2326. strlorabuf[16] = 0x00;
  2327. strlorabuf[17] = 0x00;
  2328. strlorabuf[18] = 0x00;
  2329. strlorabuf[19] = 0x00;
  2330. strlorabuf[20] = 0x00;
  2331. strlorabuf[21] = 0x00;
  2332. strlorabuf[22] = 0x00;
  2333. printf("time is not sync\r\n");
  2334. }
  2335. else
  2336. {
  2337. strlorabuf[16] = (Machine_info.last_button.Year >> 8) & 0xff;
  2338. strlorabuf[17] = Machine_info.last_button.Year & 0xff;
  2339. strlorabuf[18] = Machine_info.last_button.Month;
  2340. strlorabuf[19] = Machine_info.last_button.Day;
  2341. strlorabuf[20] = Machine_info.last_button.Hour;
  2342. strlorabuf[21] = Machine_info.last_button.Minute;
  2343. strlorabuf[22] = Machine_info.last_button.Second;
  2344. }
  2345. strlorabuf[23] = (Machine_info.Duration_time >> 24) & 0xff; // 分钟计算
  2346. strlorabuf[24] = (Machine_info.Duration_time >> 16) & 0xff;
  2347. strlorabuf[25] = (Machine_info.Duration_time >> 8) & 0xff;
  2348. strlorabuf[26] = (Machine_info.Duration_time) & 0xff;
  2349. strlorabuf[27] = 0;
  2350. crc16 = strlorabuf[0] + strlorabuf[1];
  2351. for (i = 3; i < 27; i++)
  2352. crc16 = crc16 + strlorabuf[i];
  2353. strlorabuf[2] = crc16 & 0xff;
  2354. 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]);
  2355. changebintotxt(strlorabuf, 27);
  2356. // printf("Machine_info cid %s\r\n",Machine_info.cid);
  2357. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2358. len = strlen((char *)strlorabuf);
  2359. #if 0
  2360. lora_send_data((char *)strlorabuf,len);/直接发送
  2361. #else
  2362. // printList(Send_list);
  2363. lora_pool[LORA_INFO_DUR].flag = true;
  2364. lora_pool[LORA_INFO_DUR].num = Machine_info.msg_id;
  2365. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_DUR, (char *)strlorabuf, len); // 插入数据
  2366. #endif
  2367. // lora_send_data((char *)strlorabuf,len);
  2368. last_button_info.time_min = 0;
  2369. }
  2370. /*当前最新的状态*/
  2371. void f_send_update_status(void)
  2372. {
  2373. if (lora_pool[LORA_INFO_STATUS].flag == true)
  2374. {
  2375. return;
  2376. }
  2377. // memset(&Machine_info.current_button,0,sizeof(Button_Time_t));
  2378. Machine_info.current_button.Year = Machine_info.year;
  2379. Machine_info.current_button.Month = Machine_info.month;
  2380. Machine_info.current_button.Day = Machine_info.day;
  2381. Machine_info.current_button.Hour = Machine_info.hour;
  2382. Machine_info.current_button.Minute = Machine_info.min;
  2383. Machine_info.current_button.Second = Machine_info.sec;
  2384. Machine_info.current_button.time_min = 0;
  2385. uint8_t strlorabuf[200];
  2386. int crc16 = 0, i = 0, len = 0;
  2387. // 帧头 目标地址 源地址(id) 长度 模式 具体内容 校验
  2388. strlorabuf[0] = 0x5A;
  2389. strlorabuf[1] = 0x51; // 包头
  2390. strlorabuf[2] = 0x00; // 校验和
  2391. strlorabuf[3] = mac_addr[0];
  2392. strlorabuf[4] = mac_addr[1];
  2393. strlorabuf[5] = mac_addr[2];
  2394. strlorabuf[6] = mac_addr[3];
  2395. strlorabuf[7] = mac_addr[4];
  2396. strlorabuf[8] = mac_addr[5];
  2397. strlorabuf[9] = Machine_info.eflagID; // 组内编号
  2398. strlorabuf[10] = 0x0F; // cmd命令字
  2399. Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
  2400. strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
  2401. strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
  2402. strlorabuf[13] = 0x00;
  2403. strlorabuf[14] = 0x0C; // 长度
  2404. uint8_t tmp = change_btn(Machine_info.current_button.button_info);
  2405. strlorabuf[15] = tmp; // 当前的状态
  2406. if ((Machine_info.current_button.Year == 0x00) && (Machine_info.current_button.Month == 0x00) && (Machine_info.current_button.Day == 0x00))
  2407. {
  2408. strlorabuf[16] = 0x00;
  2409. strlorabuf[17] = 0x00;
  2410. strlorabuf[18] = 0x00;
  2411. strlorabuf[19] = 0x00;
  2412. strlorabuf[20] = 0x00;
  2413. strlorabuf[21] = 0x00;
  2414. strlorabuf[22] = 0x00;
  2415. printf("time is not sync\r\n");
  2416. }
  2417. else
  2418. {
  2419. strlorabuf[16] = (Machine_info.current_button.Year >> 8) & 0xff;
  2420. strlorabuf[17] = Machine_info.current_button.Year & 0xff;
  2421. strlorabuf[18] = Machine_info.current_button.Month;
  2422. strlorabuf[19] = Machine_info.current_button.Day;
  2423. strlorabuf[20] = Machine_info.current_button.Hour;
  2424. strlorabuf[21] = Machine_info.current_button.Minute;
  2425. strlorabuf[22] = Machine_info.current_button.Second;
  2426. }
  2427. strlorabuf[23] = (Machine_info.current_button.time_min >> 24) & 0xff; // 分钟计算
  2428. strlorabuf[24] = (Machine_info.current_button.time_min >> 16) & 0xff;
  2429. strlorabuf[25] = (Machine_info.current_button.time_min >> 8) & 0xff;
  2430. strlorabuf[26] = Machine_info.current_button.time_min & 0xff;
  2431. strlorabuf[27] = 0;
  2432. 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]);
  2433. crc16 = strlorabuf[0] + strlorabuf[1];
  2434. for (i = 3; i < 27; i++)
  2435. crc16 = crc16 + strlorabuf[i];
  2436. strlorabuf[2] = crc16 & 0xff;
  2437. changebintotxt(strlorabuf, 27);
  2438. // printf("Machine_info cid %s\r\n",Machine_info.cid);
  2439. strcat((char *)strlorabuf, (char *)Machine_info.cid);
  2440. len = strlen((char *)strlorabuf);
  2441. #if 0
  2442. lora_send_data((char *)strlorabuf,len);
  2443. #else
  2444. lora_pool[LORA_INFO_STATUS].num = Machine_info.msg_id;
  2445. lora_pool[LORA_INFO_STATUS].flag = true;
  2446. Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_STATUS, (char *)strlorabuf, len); // 插入数据
  2447. #endif
  2448. Machine_info.last_button.time_min = 0;
  2449. }
  2450. // void send_ACK(char cmd, bool flag)
  2451. // {
  2452. // uint8_t ack = 0;
  2453. // if (flag)
  2454. // ack = 0x01;
  2455. // else
  2456. // ack = 0x00;
  2457. // protocol_package(cmd, &ack, 1);
  2458. // }
  2459. void adc1_init(void)
  2460. {
  2461. //-------------ADC Init---------------//
  2462. adc_oneshot_unit_init_cfg_t init_config = {
  2463. .unit_id = ADC_UNIT_1,
  2464. .ulp_mode = ADC_ULP_MODE_DISABLE,
  2465. };
  2466. ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc1_handle));
  2467. }
  2468. // void set_status_heights(void)
  2469. // {
  2470. // extern uint32_t arr_heights[10];
  2471. // memcpy(&_0703_button_info,&last_button_info,sizeof(Button_Time_t));
  2472. // switch (_0703_button_info.button_info)
  2473. // {
  2474. // case 0:
  2475. // case 1://yunxing
  2476. // arr_heights[0]+=1;
  2477. // break;
  2478. // case 2://tingji
  2479. // arr_heights[1]+=1;
  2480. // break;
  2481. // case 3://guzhang
  2482. // arr_heights[2]+=1;
  2483. // break;
  2484. // case 4://fengcun
  2485. // arr_heights[3]+=1;
  2486. // break;
  2487. // case 5://baoyang
  2488. // arr_heights[4]+=1;
  2489. // break;
  2490. // case 6://dailiao
  2491. // arr_heights[5]+=1;
  2492. // break;
  2493. // default:
  2494. // break;
  2495. // }
  2496. // // ESP_LOGE(LOG_TAG,"button_info = %d ,time_min = %ld ---%ld - %ld - %ld - %ld - %ld - %ld",
  2497. // // _0703_button_info.button_info,_0703_button_info.time_min,
  2498. // // arr_heights[0],arr_heights[1],arr_heights[2],
  2499. // // arr_heights[3],arr_heights[4],arr_heights[5]);
  2500. // }
  2501. void set_status_heights(void)
  2502. {
  2503. // STATE_OPERATION = 1, //运行
  2504. // STATE_SHUT_DOWN = 2, //停机
  2505. // STATE_BREAKDOWN = 3, //故障
  2506. // STATE_SAFEKEEP = 4, //封存
  2507. // STATE_UPKEEP = 5, //保养
  2508. // STATE_WAIT_MATERIALS = 6, //待料
  2509. extern uint32_t arr_heights[10];
  2510. switch (Machine_info.last_button.button_info)
  2511. {
  2512. case 0:
  2513. case 1: // yunxing
  2514. arr_heights[0] += Machine_info.Duration_time;
  2515. break;
  2516. case 2: // tingji
  2517. arr_heights[1] += Machine_info.Duration_time;
  2518. break;
  2519. case 3: // guzhang
  2520. arr_heights[2] += Machine_info.Duration_time;
  2521. break;
  2522. case 4: // fengcun
  2523. arr_heights[3] += Machine_info.Duration_time;
  2524. break;
  2525. case 5: // baoyang
  2526. arr_heights[4] += Machine_info.Duration_time;
  2527. break;
  2528. case 6: // dailiao
  2529. arr_heights[5] += Machine_info.Duration_time;
  2530. break;
  2531. default:
  2532. break;
  2533. }
  2534. // ESP_LOGE(LOG_TAG,"button_info = %d ,time_min = %ld ---%ld - %ld - %ld - %ld - %ld - %ld",
  2535. // _0703_button_info.button_info,_0703_button_info.time_min,
  2536. // arr_heights[0],arr_heights[1],arr_heights[2],
  2537. // arr_heights[3],arr_heights[4],arr_heights[5]);
  2538. }
  2539. // void periodic_timer_callback(void* arg)
  2540. // {
  2541. // // if(++Machine_info.sec == 60)
  2542. // // {
  2543. // // last_button_info.time_min+=1;
  2544. // // set_status_heights();
  2545. // // printf("time min ++\r\n");
  2546. // // printf("YEAR = %d,MONTH =%d,DAY=%d,HOUR=%d,MIN=%d,SEC =%d\r\n",
  2547. // // Machine_info.year,
  2548. // // Machine_info.month,
  2549. // // Machine_info.day,
  2550. // // Machine_info.hour,
  2551. // // Machine_info.min,
  2552. // // Machine_info.sec);
  2553. // // Machine_info.sec=0;
  2554. // // if(++Machine_info.min == 60)
  2555. // // {
  2556. // // Machine_info.min=0;
  2557. // // if(++Machine_info.hour == 24)
  2558. // // {
  2559. // // Machine_info.hour=0;
  2560. // // Machine_info.day++;
  2561. // // if(IS_FEB_MONTH && IS_LEAP_YEAR && IS_SMALL_MONTH && IS_BIG_MONTH)
  2562. // // {
  2563. // // Machine_info.day=0;
  2564. // // if(++Machine_info.month == 13)
  2565. // // {
  2566. // // Machine_info.month=1;
  2567. // // Machine_info.year++;
  2568. // // }
  2569. // // }
  2570. // // }
  2571. // // }
  2572. // // }
  2573. // }
  2574. void lora_timer_callback(void *arg)
  2575. {
  2576. ESP_LOGI(LOG_TAG, " lora_timer_callback");
  2577. if (xQueueSend(yc_data_queue, &yc_data, 0) != true)
  2578. {
  2579. ESP_LOGE(LOG_TAG, "yc_data_queue send is fail");
  2580. }
  2581. yc_data.len = 0;
  2582. yc_data.index = 0;
  2583. }
  2584. void timer_init(void)
  2585. {
  2586. // const esp_timer_create_args_t periodic_timer_args = {
  2587. // .callback = &periodic_timer_callback,
  2588. // /* name is optional, but may help identify the timer when debugging */
  2589. // .name = "periodic"
  2590. // };
  2591. // ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  2592. // /* The timer has been created but is not running yet */
  2593. // ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 1*1000*1000));
  2594. // lora timer
  2595. const esp_timer_create_args_t lora_timer_args = {
  2596. .callback = &lora_timer_callback,
  2597. /* argument specified here will be passed to timer callback function */
  2598. .name = "one-shot"};
  2599. ESP_ERROR_CHECK(esp_timer_create(&lora_timer_args, &lora_timer));
  2600. }
  2601. // void yc_timer_restart(void)
  2602. // {
  2603. // ESP_LOGI(LOG_TAG,"timer restart");
  2604. // ESP_ERROR_CHECK(esp_timer_stop(periodic_timer));
  2605. // ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 1*1000*1000));
  2606. // }
  2607. void lora_timer_start(void)
  2608. {
  2609. ESP_ERROR_CHECK(esp_timer_start_once(lora_timer, 500 * 1000)); // 500ms
  2610. }
  2611. void lora_tiemr_stop(void)
  2612. {
  2613. ESP_ERROR_CHECK(esp_timer_stop(lora_timer));
  2614. }
  2615. void lora_timer_restart(void)
  2616. {
  2617. ESP_ERROR_CHECK(esp_timer_stop(lora_timer));
  2618. ESP_ERROR_CHECK(esp_timer_start_once(lora_timer, 500 * 1000)); // 500ms
  2619. }
  2620. #if 1
  2621. /*********************************************************************************
  2622. * function : analysis_protocol
  2623. * Description : lora 接受数据解析函数
  2624. * Input :
  2625. * Output :
  2626. * Author : 祁鑫 Data : 2023 9.18
  2627. **********************************************************************************/
  2628. void analysis_protocol(char *result, int len)
  2629. {
  2630. int index = 0;
  2631. int length = 0;
  2632. int count = len;
  2633. int num = 0;
  2634. while (count)
  2635. {
  2636. if (((result[index + 2]) == 0x5A) && ((result[index + 3]) == 0x51))
  2637. {
  2638. length = ((result[index]) << 8) | ((result[index + 1]));
  2639. num++;
  2640. printf("num =%d,length=%d\r\n", num, length);
  2641. #if 1
  2642. for (int i = 0; i < length; i++)
  2643. {
  2644. printf("%02x", result[index + 2 + i]);
  2645. }
  2646. #endif
  2647. printf("\r\n");
  2648. // 匹配mac地址 //计算校验和
  2649. if (
  2650. (Machine_info.mac_addr[0] == (result[index + 5])) &&
  2651. (Machine_info.mac_addr[1] == (result[index + 6])) &&
  2652. (Machine_info.mac_addr[2] == (result[index + 7])) &&
  2653. (Machine_info.mac_addr[3] == (result[index + 8])) &&
  2654. (Machine_info.mac_addr[4] == (result[index + 9])) &&
  2655. (Machine_info.mac_addr[5] == (result[index + 10])))
  2656. {
  2657. // mac地址匹配
  2658. printf("mac check ok\r\n");
  2659. // packetparsing((unsigned char *)&result[index+2]); //16进制解析数据
  2660. }
  2661. else
  2662. {
  2663. printf("mac = %02x%02x%02x%02x%02x%02x\r\n",
  2664. result[index + 5],
  2665. result[index + 6],
  2666. result[index + 7],
  2667. result[index + 8],
  2668. result[index + 9],
  2669. result[index + 10]);
  2670. printf("device mac = %02x%02x%02x%02x%02x%02x\r\n",
  2671. Machine_info.mac_addr[0],
  2672. Machine_info.mac_addr[1],
  2673. Machine_info.mac_addr[2],
  2674. Machine_info.mac_addr[3],
  2675. Machine_info.mac_addr[4],
  2676. Machine_info.mac_addr[5]);
  2677. // mac地址匹配
  2678. printf("mac check fail\r\n");
  2679. }
  2680. }
  2681. {
  2682. index++;
  2683. }
  2684. count--;
  2685. }
  2686. // printf("num =%d\r\n",num);
  2687. #if 0
  2688. if(left_display_refresh == true)
  2689. {
  2690. left_display_refresh = false;
  2691. PIC_display((unsigned char *)dspbuf_left);
  2692. }
  2693. reflashdfpr(dspbuf);
  2694. #endif
  2695. }
  2696. /*********************************************************************************
  2697. * function : selectionSort
  2698. * Description : 进行数据排序并输出排序后的数据 从小到大的索引
  2699. * Input :
  2700. * Output :
  2701. * Author : 祁鑫 Data : 2023 8.12
  2702. **********************************************************************************/
  2703. void selectionSort(int arr[], int n, int indices[])
  2704. {
  2705. for (int i = 0; i < n - 1; i++)
  2706. {
  2707. int minIndex = i;
  2708. for (int j = i + 1; j < n; j++)
  2709. {
  2710. if (arr[j] < arr[minIndex])
  2711. {
  2712. minIndex = j;
  2713. }
  2714. }
  2715. // 交换元素
  2716. int temp = arr[i];
  2717. arr[i] = arr[minIndex];
  2718. arr[minIndex] = temp;
  2719. // 更新索引
  2720. int tempIndex = indices[i];
  2721. indices[i] = indices[minIndex];
  2722. indices[minIndex] = tempIndex;
  2723. }
  2724. }
  2725. /*********************************************************************************
  2726. * function : person_get_num_is_exist
  2727. * Description : 获取当前类型序号是否存在
  2728. * Input :
  2729. * Output :
  2730. * Author : 祁鑫 Data : 2023 8.12
  2731. **********************************************************************************/
  2732. int person_get_num_is_exist(uint8_t type, uint16_t num)
  2733. {
  2734. // uint8_t index = 0;
  2735. // printf("person_get_num_is_exist %d\r\n",num);
  2736. if (type == person[type].person_type)
  2737. {
  2738. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2739. {
  2740. if (person[type].person_name[i].person_num == num)
  2741. {
  2742. // printf("person_get_num is exist\r\n");
  2743. return 1;
  2744. }
  2745. }
  2746. }
  2747. // printf("person_get_num not exist\r\n");
  2748. return 0;
  2749. }
  2750. /*********************************************************************************
  2751. * function : person_get_num
  2752. * Description : 获取当前类型人员数量
  2753. * Input :
  2754. * Output :
  2755. * Author : 祁鑫 Data : 2023 8.12
  2756. **********************************************************************************/
  2757. int person_get_num(uint8_t type)
  2758. {
  2759. uint8_t index = 0;
  2760. // printf("person_get_num\r\n");
  2761. if (type == person[type].person_type) // 管理员
  2762. {
  2763. // 查找当前不为空的字符串数组
  2764. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2765. {
  2766. if (strlen((char *)&person[type].person_name[i].person_name) != 0)
  2767. {
  2768. index++;
  2769. }
  2770. }
  2771. }
  2772. return index;
  2773. }
  2774. /*********************************************************************************
  2775. * function : person_add
  2776. * Description : 人员添加/更新
  2777. * Input :
  2778. * Output :
  2779. * Author : 祁鑫 Data : 2023 8.12
  2780. **********************************************************************************/
  2781. void person_add(uint8_t type, uint8_t num, char *in, char *out)
  2782. {
  2783. // printf("person_add\r\n");
  2784. if (type == person[type].person_type) // 管理员
  2785. {
  2786. uint8_t index = 0;
  2787. // 查找当前不为空的字符串数组
  2788. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2789. {
  2790. // Serial.println(person[type].person_name[i].person_num);
  2791. // 如果序号存在更新
  2792. if (person[type].person_name[i].person_num == num)
  2793. {
  2794. // Serial.print("person_num exist = ");
  2795. // Serial.println(person[type].person_name[i].person_num);
  2796. index = i;
  2797. break;
  2798. }
  2799. // 如果序号不存在添加
  2800. if (strlen((char *)&person[type].person_name[i].person_name) == 0)
  2801. {
  2802. // Serial.print("person_num not exist = ");
  2803. index = i;
  2804. person[type].person_name[i].person_num = num;
  2805. // Serial.println(person[type].person_name[i].person_num);
  2806. break;
  2807. }
  2808. }
  2809. if (index > PERSON_MAX_NAME)
  2810. {
  2811. }
  2812. else
  2813. {
  2814. if (person[type].person_name[index].person_num == num)
  2815. {
  2816. memset(&person[type].person_name[index].person_name, 0x00, PERSON_NAME_MAX_LEN);
  2817. }
  2818. strcpy((char *)&person[type].person_name[index].person_name, in);
  2819. }
  2820. // Serial.println("show font");
  2821. // 开始显示数据
  2822. int sort_buffer[PERSON_MAX_NAME] = {0}; // 获取当前序号值
  2823. int indices[PERSON_MAX_NAME]; // 排序后的索引值
  2824. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2825. {
  2826. indices[i] = i;
  2827. }
  2828. // 拷贝序号数据
  2829. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2830. {
  2831. sort_buffer[i] = person[type].person_name[i].person_num;
  2832. }
  2833. selectionSort(sort_buffer, PERSON_MAX_NAME, indices);
  2834. #if 0
  2835. //Serial.print("Sorted array: ");
  2836. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2837. //Serial.print(sort_buffer[i]);
  2838. //Serial.print(",");
  2839. }
  2840. //Serial.println("");
  2841. //Serial.print("\nOriginal indices: ");
  2842. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2843. //Serial.print(indices[i]);
  2844. //Serial.print(",");
  2845. }
  2846. //Serial.println("");
  2847. #endif
  2848. #if 0
  2849. //进行序号排序
  2850. bubbleSort(sort_buffer, PERSON_MAX_NAME);
  2851. #endif
  2852. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2853. {
  2854. int display_index = 0;
  2855. display_index = indices[i]; // 获取当前显示的索引
  2856. // Serial.println(person[type].person_name[display_index].person_num);
  2857. if (strlen((char *)&person[type].person_name[display_index].person_name) != 0)
  2858. {
  2859. strcat(out, (char *)&person[type].person_name[display_index].person_name);
  2860. strcat(out, " ");
  2861. }
  2862. }
  2863. }
  2864. }
  2865. /*********************************************************************************
  2866. * function : person_del
  2867. * Description : 人员删除
  2868. * Input :
  2869. * Output :
  2870. * Author : 祁鑫 Data : 2023 8.12
  2871. **********************************************************************************/
  2872. void person_del(uint8_t type, uint8_t num, char *in, char *out)
  2873. {
  2874. // printf("person_del\r\n");
  2875. // Serial.print("delete num ");
  2876. // Serial.println(num);
  2877. if (type == person[type].person_type)
  2878. {
  2879. uint8_t index = 0;
  2880. // 查找当前不为空的字符串数组
  2881. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2882. {
  2883. // 如果序号存在
  2884. if (person[type].person_name[i].person_num == num)
  2885. {
  2886. printf("person_num exist \r\n");
  2887. // Serial.println(person[type].person_name[i].person_num);
  2888. index = i;
  2889. break;
  2890. }
  2891. // return;
  2892. }
  2893. {
  2894. if ((person[type].person_name[index].person_num == num))
  2895. {
  2896. // 清空显示
  2897. // Serial.println("delete index data");
  2898. printf("delete index data \r\n");
  2899. person[type].person_name[index].person_num = 0;
  2900. memset(&person[type].person_name[index].person_name, 0x00, PERSON_NAME_MAX_LEN);
  2901. }
  2902. // strcpy((char *)&person[type].person_name[index].person_name,in);
  2903. }
  2904. // Serial.println("show font");
  2905. #if 0
  2906. for(int i = 0;i<PERSON_MAX_NAME;i++)
  2907. {
  2908. Serial.println(person[type].person_name[i].person_num);
  2909. if(strlen((char *)&person[type].person_name[i].person_name)!=0)
  2910. {
  2911. strcat(out,(char *)&person[type].person_name[i].person_name);
  2912. strcat(out," ");
  2913. }
  2914. }
  2915. #else
  2916. // 开始显示数据
  2917. int sort_buffer[PERSON_MAX_NAME] = {0}; // 获取当前序号值
  2918. int indices[PERSON_MAX_NAME]; // 排序后的索引值
  2919. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2920. {
  2921. indices[i] = i;
  2922. }
  2923. // 拷贝序号数据
  2924. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2925. {
  2926. sort_buffer[i] = person[type].person_name[i].person_num;
  2927. }
  2928. selectionSort(sort_buffer, PERSON_MAX_NAME, indices);
  2929. #if 0
  2930. Serial.print("Sorted array: ");
  2931. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2932. Serial.print(sort_buffer[i]);
  2933. Serial.print(",");
  2934. }
  2935. Serial.println("");
  2936. Serial.print("\nOriginal indices: ");
  2937. for (int i = 0; i < PERSON_MAX_NAME; i++) {
  2938. Serial.print(indices[i]);
  2939. Serial.print(",");
  2940. }
  2941. Serial.println("");
  2942. #endif
  2943. for (int i = 0; i < PERSON_MAX_NAME; i++)
  2944. {
  2945. int display_index = 0;
  2946. display_index = indices[i]; // 获取当前显示的索引
  2947. // Serial.println(person[type].person_name[display_index].person_num);
  2948. if (strlen((char *)&person[type].person_name[display_index].person_name) != 0)
  2949. {
  2950. strcat(out, (char *)&person[type].person_name[display_index].person_name);
  2951. strcat(out, " ");
  2952. printf("out = %s\r\n", out);
  2953. }
  2954. }
  2955. #endif
  2956. }
  2957. }
  2958. #endif
  2959. void loro_gwpair_ack(int destaddr, int sourceaddr, int cmd, int st, char *strd)
  2960. {
  2961. uint8_t strlorabuf[50];
  2962. int crc16 = 0, len, i;
  2963. memset(strlorabuf, 0, 50);
  2964. // 源地址 是指网关地址
  2965. // 帧头 目标地址 源地址(id) 长度 模式 具体内容 校验
  2966. strlorabuf[0] = 0x55;
  2967. strlorabuf[1] = 0xaa;
  2968. strlorabuf[2] = destaddr & 0xff;
  2969. strlorabuf[3] = sourceaddr & 0xff;
  2970. strlorabuf[4] = 0x02;
  2971. strlorabuf[5] = cmd;
  2972. strlorabuf[6] = st;
  2973. strcpy((char *)(strlorabuf + 7), strd); // mac+c_id
  2974. crc16 = strlorabuf[0] + strlorabuf[1] + strlorabuf[2] + strlorabuf[3] + strlorabuf[4] + strlorabuf[5] + strlorabuf[6];
  2975. len = strlen(strd);
  2976. strlorabuf[4] = 4 + len;
  2977. for (i = 0; i < len; i++)
  2978. crc16 = crc16 + strlorabuf[7 + len];
  2979. strlorabuf[7 + len] = (crc16 >> 8) & 0xff;
  2980. strlorabuf[8 + len] = crc16 & 0xff;
  2981. strlorabuf[9 + len] = 0;
  2982. #if 0
  2983. send_lora_data(strlorabuf,9+len);
  2984. #else
  2985. // loraSerial.write((char *)strlorabuf,len);
  2986. lora_send_data((char *)strlorabuf, len);
  2987. #endif
  2988. }
  2989. void lora_rev_cmd_send_ack()
  2990. {
  2991. }
  2992. void right_screen_send()
  2993. {
  2994. set_screen_dis_info_and_send_queue(true, false, false, false, 100);
  2995. }
  2996. void user_compare(int last, int now) // 这个函数用来比较rssi和 电量变化 刷右屏//
  2997. {
  2998. if ((last == now) || (Machine_info.power_status == 0))
  2999. {
  3000. // ESP_LOGW(LOG_TAG,"last[%d]-now[%d]",last,now);
  3001. return;
  3002. }
  3003. // printf("user_compare last[%d] now[%d] \n",last,now);
  3004. bool is_send_right_screen = false;
  3005. switch (last)
  3006. {
  3007. case 100:
  3008. if (now != 100)
  3009. {
  3010. is_send_right_screen = true;
  3011. }
  3012. break;
  3013. case 75 ... 99:
  3014. if ((now == 100) || (now < 75))
  3015. {
  3016. is_send_right_screen = true;
  3017. }
  3018. break;
  3019. case 50 ... 74:
  3020. if ((now > 74) || (now < 50))
  3021. {
  3022. is_send_right_screen = true;
  3023. }
  3024. break;
  3025. case 25 ... 49:
  3026. if ((now > 49) || (now < 25))
  3027. {
  3028. is_send_right_screen = true;
  3029. }
  3030. break;
  3031. case 0 ... 24:
  3032. if ((now == 0) || (now > 24))
  3033. {
  3034. is_send_right_screen = true;
  3035. }
  3036. break;
  3037. default:
  3038. ESP_LOGE(LOG_TAG, "err:not 0~100");
  3039. break;
  3040. }
  3041. if (is_send_right_screen)
  3042. right_screen_send();
  3043. }
  3044. void user_compare_power_off(int last, int now) // 这个函数用来比较关机状态时电量变化 刷右屏//
  3045. {
  3046. if ((last == now))
  3047. {
  3048. // ESP_LOGW(LOG_TAG,"last[%d]-now[%d]",last,now);
  3049. return;
  3050. }
  3051. bool is_dis = false;
  3052. switch (last)
  3053. {
  3054. case 100:
  3055. if (now != 100)
  3056. {
  3057. gpio_hold_dis(PIN_R_CS);
  3058. is_dis = true;
  3059. }
  3060. break;
  3061. case 75 ... 99:
  3062. if ((now == 100) || (now < 75))
  3063. {
  3064. gpio_hold_dis(PIN_R_CS);
  3065. is_dis = true;
  3066. }
  3067. break;
  3068. case 50 ... 74:
  3069. if ((now > 74) || (now < 50))
  3070. {
  3071. gpio_hold_dis(PIN_R_CS);
  3072. is_dis = true;
  3073. }
  3074. break;
  3075. case 25 ... 49:
  3076. if ((now > 49) || (now < 25))
  3077. {
  3078. gpio_hold_dis(PIN_R_CS);
  3079. is_dis = true;
  3080. }
  3081. break;
  3082. case 0 ... 24:
  3083. if ((now == 0) || (now > 24))
  3084. {
  3085. gpio_hold_dis(PIN_R_CS);
  3086. is_dis = true;
  3087. }
  3088. break;
  3089. default:
  3090. ESP_LOGE(LOG_TAG, "err:not 0~100");
  3091. break;
  3092. }
  3093. if (is_dis)
  3094. {
  3095. set_screen_dis_info_and_send_queue(true, false, false, false, 100);
  3096. }
  3097. }