yc_protocol.c 120 KB

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