yc_protocol.c 119 KB

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