yc_protocol.c 122 KB

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