yc_protocol.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001
  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()
  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 = 1;
  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. Paint_leftScreen_main_quick(&Machine_info);
  72. Paint_rightScreen_main_quick(&Machine_info);
  73. }
  74. #if 0
  75. void insertNode_head(ListNode *head, ListNode *P);
  76. void removeNode_byNumber(ListNode *head,uint16_t number);
  77. ListNode *getListNode_byNumber(ListNode *head,uint16_t number);
  78. ListNode *newListNode(uint16_t number,uint8_t type,char* name);
  79. #endif
  80. /*
  81. * 使用 vTaskList() 前需使能:
  82. * Enable FreeRTOS trace facility
  83. * Enable FreeRTOS stats formatting functions
  84. */
  85. void print_systenInfo(void)
  86. {
  87. printf("=================================\r\n");
  88. printf("=================================\r\n");
  89. printf("=================================\r\n");
  90. printf("=================================\r\n");
  91. printf("=================================\r\n");
  92. printf("========version V %d.%d.%d==========\r\n",VERSION_X,VERSION_Y,VERSION_Z);
  93. esp_efuse_mac_get_default((uint8_t*)mac_addr);
  94. esp_efuse_mac_get_default((uint8_t*)Machine_info.mac_addr);
  95. #if 1 //获取mac地址
  96. uint8_t macAddr[6];
  97. esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
  98. //保留以前的mac地址段
  99. //mac_addr[0]+=0x02;
  100. {
  101. printf("mac addr : %02x:%02x:%02x:%02x:%02x:%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
  102. }
  103. memcpy((uint8_t*)Machine_info.mac_addr,macAddr,6);
  104. //Machine_info.mac_addr[0]= Machine_info.mac_addr[0]+0x02;
  105. 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]);
  106. #endif
  107. printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n",\
  108. esp_get_free_heap_size(),esp_get_free_internal_heap_size(),esp_get_minimum_free_heap_size());
  109. #if 0
  110. ESP_LOGW(LOG_TAG,"mac:[%02x-%02x-%02x-%02x-%02x-%02x]",\
  111. mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[05]);
  112. static char InfoBuffer[512] = {0};
  113. vTaskList((char*)&InfoBuffer);
  114. ESP_LOGW(LOG_TAG,"free_heap_size:%ld \nfree_internal_heap_size:%ld \nminimum_free_heap_size:%ld",\
  115. esp_get_free_heap_size(),esp_get_free_internal_heap_size(),esp_get_minimum_free_heap_size());
  116. printf("任务名 任务状态 优先级 剩余栈 任务序号\r\n");
  117. printf("\r\n%s\r\n", InfoBuffer);
  118. #endif
  119. }
  120. void send_update_status(void);
  121. void send_status_duration(void);
  122. void terminal_send_data(void)
  123. {
  124. //send_rssi();
  125. //vTaskDelay(100 / portTICK_PERIOD_MS);
  126. send_status_duration();
  127. //vTaskDelay(50 / portTICK_PERIOD_MS);
  128. send_update_status();
  129. //vTaskDelay(100 / portTICK_PERIOD_MS);
  130. }
  131. void update_last_button_info(uint8_t btn)
  132. {
  133. #if 0
  134. memset(&last_button_info,0,sizeof(Button_Time_t));
  135. last_button_info.button_info = btn;
  136. last_button_info.Year = Machine_info.year;
  137. last_button_info.Month = Machine_info.month;
  138. last_button_info.Day = Machine_info.day;
  139. last_button_info.Hour = Machine_info.hour;
  140. last_button_info.Minute = Machine_info.min;
  141. last_button_info.Second = Machine_info.sec;
  142. ESP_LOGW(LOG_TAG,"btn = %d [%d-%d-%d]%d:%d:%d", btn, Machine_info.year,Machine_info.month,Machine_info.day,\
  143. Machine_info.hour, Machine_info.min,Machine_info.sec);
  144. //last_button_info.time_min = 0;
  145. #else
  146. //更新上次的状态
  147. //获取当前时间
  148. getRtcTime(&Machine_info);
  149. Machine_info.last_button.button_info = btn;
  150. Machine_info.last_button.Year = Machine_info.year;
  151. Machine_info.last_button.Month = Machine_info.month;
  152. Machine_info.last_button.Day = Machine_info.day;
  153. Machine_info.last_button.Hour = Machine_info.hour;
  154. Machine_info.last_button.Minute = Machine_info.min;
  155. Machine_info.last_button.Second = Machine_info.sec;
  156. Machine_info.last_button.time_min = 0x00000000;
  157. #endif
  158. }
  159. void screen_display(void)
  160. {
  161. font_exit_sleep();
  162. // ESP_LOGW(LOG_TAG,"display_flag =%d\r\n",display_flag);
  163. // if(display_flag == 1)
  164. // {
  165. // if(xQueueSend(left_screen_queue,&Machine_info,portMAX_DELAY) != true)
  166. // {
  167. // ESP_LOGE(LOG_TAG,"queue:left_screen_queue");
  168. // }
  169. // }
  170. // //vTaskDelay(1000 / portTICK_PERIOD_MS);
  171. // else if(display_flag == 2)
  172. // {
  173. // if(xQueueSend(right_screen_queue,&Machine_info,portMAX_DELAY) != true)
  174. // {
  175. // ESP_LOGE(LOG_TAG,"queue:right_screen_queue");
  176. // }
  177. // }
  178. EventBits_t bits = xEventGroupWaitBits(screen_event,
  179. LEFT_SCREEN_BIT | RIGHT_SCREEN_BIT,
  180. pdTRUE,
  181. pdFALSE,
  182. 0);
  183. ESP_LOGE(LOG_TAG,"xEventGroupWaitBits bits = %ld",bits);
  184. /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  185. * happened. */
  186. if(bits == 3){
  187. printf("--------------------left right all screen---------------\r\n");
  188. while(1)
  189. {
  190. if( (right_refresh_timer_isActive() == false) )
  191. {
  192. printf("right refresh complete %s\r\n",right_refresh_timer_isActive()?"true":"false");
  193. break;
  194. }
  195. vTaskDelay(100/ portTICK_PERIOD_MS);
  196. }
  197. while(1)
  198. {
  199. if( (left_refresh_timer_isActive() == false) )
  200. {
  201. printf("2left refresh complete %s\r\n",left_refresh_timer_isActive()?"true":"false");
  202. break;
  203. }
  204. vTaskDelay(100/ portTICK_PERIOD_MS);
  205. }
  206. //Paint_leftScreen_main_quick(&Machine_info);
  207. if(xQueueSend(left_screen_queue,&Machine_info,portMAX_DELAY) != true)
  208. {
  209. ESP_LOGE(LOG_TAG,"queue:right_screen_queue");
  210. }
  211. vTaskDelay(2000/ portTICK_PERIOD_MS);
  212. // while(1)
  213. // {
  214. // if( (left_refresh_timer_isActive() == false) )
  215. // {
  216. // printf("left refresh complete %s\r\n",left_refresh_timer_isActive()?"true":"false");
  217. // break;
  218. // }
  219. // vTaskDelay(100/ portTICK_PERIOD_MS);
  220. // }
  221. while(1)
  222. {
  223. if( (right_refresh_timer_isActive() == false) )
  224. {
  225. printf("right refresh complete %s\r\n",right_refresh_timer_isActive()?"true":"false");
  226. break;
  227. }
  228. vTaskDelay(100/ portTICK_PERIOD_MS);
  229. }
  230. //Paint_rightScreen_main_quick(&Machine_info);
  231. if(xQueueSend(right_screen_queue,&Machine_info,portMAX_DELAY) != true)
  232. {
  233. ESP_LOGE(LOG_TAG,"queue:right_screen_queue");
  234. }
  235. // while(1)
  236. // {
  237. // if( (right_refresh_timer_isActive() == false) )
  238. // {
  239. // printf("right refresh complete %s\r\n",right_refresh_timer_isActive()?"true":"false");
  240. // break;
  241. // }
  242. // vTaskDelay(100/ portTICK_PERIOD_MS);
  243. // }
  244. }
  245. else if (bits & RIGHT_SCREEN_BIT) {
  246. ESP_LOGE(LOG_TAG,"RIGHT_SCREEN_BIT");
  247. while(1)
  248. {
  249. if( (left_refresh_timer_isActive() == false) )
  250. {
  251. printf("3left refresh complete %s\r\n",left_refresh_timer_isActive()?"true":"false");
  252. break;
  253. }
  254. vTaskDelay(100/ portTICK_PERIOD_MS);
  255. }
  256. while(1)
  257. {
  258. if( (right_refresh_timer_isActive() == false) )
  259. {
  260. printf("4left refresh complete %s\r\n",right_refresh_timer_isActive()?"true":"false");
  261. break;
  262. }
  263. vTaskDelay(100/ portTICK_PERIOD_MS);
  264. }
  265. if(xQueueSend(right_screen_queue,&Machine_info,portMAX_DELAY) != true)
  266. {
  267. ESP_LOGE(LOG_TAG,"queue:right_screen_queue");
  268. }
  269. }
  270. else if (bits & LEFT_SCREEN_BIT) {
  271. while(1)
  272. {
  273. if( (left_refresh_timer_isActive() == false) )
  274. {
  275. printf("5left refresh complete %s\r\n",left_refresh_timer_isActive()?"true":"false");
  276. break;
  277. }
  278. vTaskDelay(100/ portTICK_PERIOD_MS);
  279. }
  280. while(1)
  281. {
  282. if( (right_refresh_timer_isActive() == false) )
  283. {
  284. printf("6left refresh complete %s\r\n",right_refresh_timer_isActive()?"true":"false");
  285. break;
  286. }
  287. vTaskDelay(100/ portTICK_PERIOD_MS);
  288. }
  289. ESP_LOGE(LOG_TAG,"LEFT_SCREEN_BIT");
  290. if(xQueueSend(left_screen_queue,&Machine_info,portMAX_DELAY) != true)
  291. {
  292. ESP_LOGE(LOG_TAG,"queue:left_screen_queue");
  293. }
  294. } else {
  295. ESP_LOGE(LOG_TAG, "UNEXPECTED EVENT");
  296. }
  297. }
  298. void print_yc_data(const LORA_DATA_T* buf)
  299. {
  300. int len = (buf->data_buf[13+2])<<8 | (buf->data_buf[14+2]);
  301. ESP_LOGI(LOG_TAG,"\n总长:0x%02x,crc:0x%02x,\
  302. \nmac:[%02x-%02x-%02x-%02x-%02x-%02x], \
  303. \n组内编号:%02x,\
  304. cmd:0x%02x,\
  305. 总包数:%02x, 当前包:%02x,\
  306. \n有效数据 len:0x%d",\
  307. buf->data_len,buf->data_buf[4],\
  308. buf->data_buf[5],buf->data_buf[6],buf->data_buf[7],\
  309. buf->data_buf[8],buf->data_buf[9],buf->data_buf[10],\
  310. buf->data_buf[11],\
  311. buf->data_buf[12],\
  312. buf->data_buf[13],buf->data_buf[14],\
  313. len);
  314. for(int i=0;i < buf->data_len;i++)
  315. {
  316. printf("%02x ",buf->data_buf[i]);
  317. if(i == (USE_DATA_LEN_INDEX+1))
  318. {
  319. printf("data--->\n");
  320. }
  321. }
  322. printf("\n");
  323. }
  324. bool filtered_data(const LORA_DATA_T* buf)
  325. {
  326. if(buf->data_buf[11] != Machine_info.eflagID)
  327. {
  328. if(Machine_info.eflagID == 0)
  329. {
  330. ESP_LOGE(LOG_TAG,"err:未分配ID");
  331. }
  332. ESP_LOGE(LOG_TAG,"err:eflagID:%02x ,is not %02x",\
  333. Machine_info.eflagID,buf->data_buf[11]);
  334. return false;
  335. }
  336. if((buf->data_buf[5] != Machine_info.mac_addr[0])||\
  337. (buf->data_buf[6] != Machine_info.mac_addr[1])||\
  338. (buf->data_buf[7] != Machine_info.mac_addr[2])||\
  339. (buf->data_buf[8] != Machine_info.mac_addr[3])||\
  340. (buf->data_buf[9] != Machine_info.mac_addr[4])||\
  341. (buf->data_buf[10] != Machine_info.mac_addr[5]))
  342. {
  343. ESP_LOGE(LOG_TAG,"err:mac addr:[%02x-%02x-%02x-%02x-%02x-%02x],\
  344. is not:[%02x-%02x-%02x-%02x-%02x-%02x]",\
  345. Machine_info.mac_addr[0],Machine_info.mac_addr[1],Machine_info.mac_addr[2],\
  346. Machine_info.mac_addr[3],Machine_info.mac_addr[4],Machine_info.mac_addr[5],\
  347. buf->data_buf[5],buf->data_buf[6],buf->data_buf[7],\
  348. buf->data_buf[8],buf->data_buf[9],buf->data_buf[10]);
  349. return false;
  350. }
  351. return true;
  352. }
  353. static int time_synchronization_func(const LORA_DATA_T* buf)
  354. {
  355. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  356. memcpy(terminal_info.time_synchronization,buf->data_buf+DATA_START_LEN,len);
  357. //yc_timer_restart();
  358. //Machine_info
  359. Machine_info.year = (terminal_info.time_synchronization[0] << 8) | terminal_info.time_synchronization[1];
  360. Machine_info.month = terminal_info.time_synchronization[2];
  361. Machine_info.day = terminal_info.time_synchronization[3];
  362. Machine_info.hour = terminal_info.time_synchronization[4];
  363. Machine_info.min = terminal_info.time_synchronization[5];
  364. Machine_info.sec = terminal_info.time_synchronization[6];
  365. display_flag = 0;
  366. setRtcTime(Machine_info.year, Machine_info.month,Machine_info.day,
  367. Machine_info.hour,Machine_info.min,Machine_info.sec);
  368. printf("TIMER SYNC YEAR = %d,MONTH =%d,DAY=%d,HOUR=%d,MIN=%d,SEC =%d\r\n",
  369. Machine_info.year,
  370. Machine_info.month,
  371. Machine_info.day,
  372. Machine_info.hour,
  373. Machine_info.min,
  374. Machine_info.sec);
  375. getRtcTime(&Machine_info);
  376. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  377. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  378. //printf("msg_id =%X\r\n",msg_id);
  379. return msg_id;
  380. }
  381. static int terminal_name_func(const LORA_DATA_T* buf)
  382. {
  383. #define FONT_MAX_LEN 8
  384. uint8_t font_num = 0; //获取当前的文字个数
  385. int i=0;
  386. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  387. memset(terminal_info.terminal_name,0,32);
  388. memcpy(terminal_info.terminal_name,buf->data_buf+DATA_START_LEN,len);
  389. //memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
  390. memset(Machine_info.terminal_name,0x00,sizeof(Machine_info.terminal_name)); //清除终端名称信息
  391. font_num = len/2;
  392. if(font_num<=FONT_MAX_LEN)
  393. {
  394. uincode2gbk((char*)terminal_info.terminal_name,len,(char*)Machine_info.terminal_name);
  395. }
  396. else
  397. {
  398. uincode2gbk((char*)terminal_info.terminal_name,FONT_MAX_LEN*2,(char*)Machine_info.terminal_name);
  399. strcat((char *)Machine_info.terminal_name,"...");
  400. }
  401. // printf("unicode:");
  402. // for(i=0;i<len;i++)
  403. // {
  404. // printf("%02x ",terminal_info.terminal_name[i]);
  405. // }
  406. // printf("\n");
  407. // printf("gbk:");
  408. // for(i=0;i<len;i++)
  409. // {
  410. // printf("%02x ",Machine_info.terminal_name[i]);
  411. // }
  412. // printf("setting name %s",Machine_info.terminal_name);
  413. // printf("\n");
  414. // display_flag = RIGHT_FLAG;
  415. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  416. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  417. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  418. //printf("msg_id =%X\r\n",msg_id);
  419. return msg_id;
  420. }
  421. static int terminal_number_func(const LORA_DATA_T* buf)
  422. {
  423. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  424. memcpy(terminal_info.terminal_number,buf->data_buf+DATA_START_LEN,len);
  425. //memcpy(&Machine_info.terminal_number,&terminal_info.terminal_number,len);
  426. memset(Machine_info.terminal_number,0x00,sizeof(Machine_info.terminal_number)); //清除终端名称信息
  427. uincode2gbk((char*)terminal_info.terminal_number,len,(char*)Machine_info.terminal_number);
  428. // printf("unicode:");
  429. // for(int i=0;i<len;i++)
  430. // {
  431. // printf("%02x ",terminal_info.terminal_number[i]);
  432. // }
  433. // printf("\n");
  434. // printf("gbk:");
  435. // for(int i=0;i<len;i++)
  436. // {
  437. // printf("%02x ",Machine_info.terminal_number[i]);
  438. // }
  439. printf("setting num %s",Machine_info.terminal_number);
  440. printf("\n");
  441. // display_flag = RIGHT_FLAG;
  442. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  443. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  444. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  445. //printf("msg_id =%X\r\n",msg_id);
  446. return msg_id;
  447. }
  448. static int electric_quantity_func(const LORA_DATA_T* buf)
  449. {
  450. terminal_info.batt_percent = buf->data_buf[DATA_START_LEN];
  451. ESP_LOGI(LOG_TAG,"batt_percent : %d",terminal_info.batt_percent);
  452. // display_flag = RIGHT_FLAG;
  453. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  454. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  455. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  456. //printf("msg_id =%X\r\n",msg_id);
  457. return msg_id;
  458. }
  459. static int signal_strength_func(const LORA_DATA_T* buf)
  460. {
  461. // terminal_info.rssi = buf->data_buf[DATA_START_LEN];
  462. // ESP_LOGI(LOG_TAG,"rssi : %d",terminal_info.rssi);
  463. // Machine_info.rssi = terminal_info.rssi;
  464. // display_flag = RIGHT_FLAG;
  465. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  466. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  467. //printf("msg_id =%X\r\n",msg_id);
  468. return msg_id;
  469. }
  470. static int screen_refresh_cycle_func(const LORA_DATA_T* buf)
  471. {
  472. terminal_info.refresh_cycle = buf->data_buf[DATA_START_LEN];
  473. ESP_LOGI(LOG_TAG,"refresh_cycle : %d",terminal_info.refresh_cycle);
  474. Machine_info.refresh_cycle = terminal_info.refresh_cycle;
  475. // display_flag = RIGHT_FLAG;
  476. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  477. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  478. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  479. //printf("msg_id =%X\r\n",msg_id);
  480. return msg_id;
  481. }
  482. static int screen_left_func(const LORA_DATA_T* buf)
  483. {
  484. terminal_info.left_epd = buf->data_buf[DATA_START_LEN];
  485. ESP_LOGI(LOG_TAG,"left_epd : %d",terminal_info.left_epd);
  486. Machine_info.left_display_mode = terminal_info.left_epd;
  487. // display_flag = LEFT_FLAG;
  488. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  489. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  490. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  491. //printf("msg_id =%X\r\n",msg_id);
  492. return msg_id;
  493. }
  494. static int screen_right_func(const LORA_DATA_T* buf)
  495. {
  496. terminal_info.right_epd = buf->data_buf[DATA_START_LEN];
  497. Machine_info.right_display_mode = terminal_info.right_epd;
  498. ESP_LOGW(LOG_TAG,"-----right_epd : %d",terminal_info.right_epd);
  499. // display_flag = RIGHT_FLAG;
  500. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  501. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  502. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  503. //printf("msg_id =%X\r\n",msg_id);
  504. return msg_id;
  505. }
  506. static int rsponsible_person_change_func(const LORA_DATA_T* buf)
  507. {
  508. int len = ((buf->data_buf[USE_DATA_LEN_INDEX])<<8) | ((buf->data_buf[USE_DATA_LEN_INDEX+1]));
  509. // for(int i = 0;i<buf->data_len;i++)
  510. // {
  511. // printf("%02X",buf->data_buf[i]);
  512. // }
  513. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  514. ESP_LOGW(LOG_TAG,"tmp name len = %d",len);
  515. memcpy(terminal_info.rsponsible_person_change,buf->data_buf+DATA_START_LEN,len);
  516. //ESP_LOGI(LOG_TAG,"mode:[%02x] len = %d",terminal_info.rsponsible_person_change[2],len);
  517. uint16_t tmp_number;
  518. uint8_t tmp_type;
  519. if(len == 3)
  520. {
  521. return msg_id;
  522. }else{
  523. char* tmp_name;
  524. tmp_name = (char*)malloc((len-3+1));
  525. memset(tmp_name,0x00,(len-3+1));
  526. uincode2gbk((char*)terminal_info.rsponsible_person_change+3,(len-3),tmp_name);
  527. // printf("tmp_name = %s\r\n",tmp_name);
  528. tmp_name[len-2] = '\0';
  529. //memcpy(tmp_name,(char*)terminal_info.rsponsible_person_change+3,len-3);
  530. #if 0
  531. for(int i=0;i<strlen(tmp_name);i++)
  532. {
  533. printf("%02x ",tmp_name[i]);
  534. }
  535. printf("\n%d\n",strlen(tmp_name));
  536. #endif
  537. tmp_number = (terminal_info.rsponsible_person_change[0]<<8)|(terminal_info.rsponsible_person_change[1]&0xFF);
  538. tmp_type = terminal_info.rsponsible_person_change[2];
  539. #if 0
  540. ListNode *p = newListNode(tmp_number,tmp_type,tmp_name);
  541. insertNode_head(list_head,p);
  542. #else
  543. memset(Machine_info.person[tmp_type].string_name,0x00,sizeof(Machine_info.person[tmp_type].string_name));
  544. person_add(tmp_type ,tmp_number,tmp_name,Machine_info.person[tmp_type].string_name);
  545. ESP_LOGE(LOG_TAG,"add name = %s\r\n",Machine_info.person[tmp_type].string_name);
  546. #endif
  547. // display_flag = RIGHT_FLAG;
  548. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  549. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  550. //printf("msg_id =%X\r\n",msg_id);
  551. free(tmp_name);
  552. tmp_name = NULL;
  553. }
  554. return msg_id;
  555. }
  556. static int rsponsible_person_delete_func(const LORA_DATA_T* buf)
  557. {
  558. uint16_t tmp_number;
  559. uint8_t tmp_type;
  560. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  561. memcpy(terminal_info.rsponsible_person_delete,buf->data_buf+DATA_START_LEN,len);
  562. tmp_number = (terminal_info.rsponsible_person_delete[0]<<8)|(terminal_info.rsponsible_person_delete[1]&0xFF);
  563. tmp_type = terminal_info.rsponsible_person_delete[2];
  564. #if 1
  565. int num = person_get_num(tmp_type); //获取当前类型人员数量
  566. int is_exist = person_get_num_is_exist(tmp_type,tmp_number);
  567. if(is_exist)
  568. {
  569. memset(Machine_info.person[tmp_type].string_name,0x00,sizeof(Machine_info.person[tmp_type].string_name));
  570. person_del(tmp_type ,tmp_number,NULL,Machine_info.person[tmp_type].string_name);
  571. }
  572. #endif
  573. // display_flag = RIGHT_FLAG;
  574. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  575. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  576. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  577. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  578. //printf("msg_id =%X\r\n",msg_id);
  579. return msg_id;
  580. }
  581. // #define SET_BIT(flag,_bit) (flag |= (0x01 << _bit))
  582. // #define CLE_BIT(flag,_bit) (flag &= ~(0x01 << _bit))
  583. static int production_punch_func(const LORA_DATA_T* buf)
  584. {
  585. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  586. memcpy(terminal_info.production_punch,buf->data_buf+DATA_START_LEN,len);
  587. ESP_LOG_BUFFER_HEX(LOG_TAG,terminal_info.production_punch,len);
  588. if(terminal_info.production_punch[1] == 0)
  589. {
  590. Machine_info.checkIn_set[terminal_info.production_punch[0]].checkIn_close = true;//关闭该类型打卡
  591. }
  592. else
  593. {
  594. Machine_info.checkIn_set[terminal_info.production_punch[0]].checkIn_close = false;//开启该类型打卡
  595. }
  596. ESP_LOGW(LOG_TAG,"打卡设置: 类型[%d] %s",terminal_info.production_punch[0],terminal_info.production_punch[1]?"开启":"关闭");
  597. if(len > 2)//数据长度大于2 ,说明设置了别名
  598. {
  599. memset(Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name,0,10);
  600. // memcpy(Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name,terminal_info.production_punch+2,len-2);
  601. uincode2gbk((char*)terminal_info.production_punch+2,len-2,(char*)Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name);
  602. }
  603. // display_flag = RIGHT_FLAG;
  604. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  605. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  606. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  607. return msg_id;
  608. }
  609. static int personnel_check_in_func(const LORA_DATA_T* buf)
  610. {
  611. //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]);
  612. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  613. memcpy(terminal_info.personnel_check_in,buf->data_buf+DATA_START_LEN,len);
  614. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  615. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  616. //printf("msg_id =%X\r\n",msg_id);
  617. // display_flag = RIGHT_FLAG;
  618. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  619. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  620. ESP_LOG_BUFFER_HEX(LOG_TAG,terminal_info.personnel_check_in,len);
  621. int ID_crc = crc8(&terminal_info.personnel_check_in[2],len-2,0x5a,0x51);//lora_crc8( , ,0x5a,0x51);暂定对人员id求crc
  622. 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);
  623. if(!(Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].checkIn_close))//开启打卡
  624. {
  625. if(terminal_info.personnel_check_in[1] == 0x00)//打卡签入
  626. {
  627. // Machine_info.personnel_check_in[terminal_info.personnel_check_in[0]]++;
  628. // Node *find = findNode(Send_list,msg_id);
  629. Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].number++;
  630. Node *find = findNode(clockIn_list,ID_crc);
  631. // printList(clockIn_list);
  632. if(find==NULL)
  633. {
  634. Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].real_number++;
  635. clockIn_list = postInsert(clockIn_list,ID_crc,terminal_info.personnel_check_in[0],(&terminal_info.personnel_check_in)+2,len-2);
  636. }
  637. else
  638. {
  639. ESP_LOGE(LOG_TAG,"该人员已存在");
  640. }
  641. }
  642. else if(terminal_info.personnel_check_in[1] == 0x01)//check out
  643. {
  644. Node *find = findNode(clockIn_list,ID_crc);
  645. if(find!=NULL)
  646. {
  647. clockIn_list = deleteNode(clockIn_list,ID_crc);
  648. Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].real_number--;
  649. }
  650. }
  651. else
  652. {
  653. ESP_LOGE(LOG_TAG,"err:not in or out");
  654. }
  655. }
  656. //printList(clockIn_list);
  657. return msg_id;
  658. }
  659. uint8_t change_btn_reversal(uint8_t reversal_btn);
  660. static void set_button_anotherName(void)//cmd 0x0d操作
  661. {
  662. uint8_t tmp = change_btn_reversal(terminal_info.status_setting[0]);
  663. if((terminal_info.status_setting[0] == 0)&&(terminal_info.status_setting[1] == 0))
  664. {//[0]:状态 [1]:开关 ————>运行不能关闭,这两位都为0,说明没有收到信息
  665. // ESP_LOGW(LOG_TAG,"默认按键");
  666. return ;//没有cmd[0x0d]的lora信息
  667. }
  668. if(terminal_info.status_setting[1] == 0x00)//close
  669. {
  670. Machine_info.btn_dis_flag[tmp-1] = false;
  671. ESP_LOGE(LOG_TAG,"按键状态关闭,清空按键文字 状态%d",tmp);
  672. switch (terminal_info.status_setting[0])//按键状态关闭,清空按键文字
  673. {
  674. case 0x00:
  675. memset(Machine_info.btn_operation,0,6);
  676. break;
  677. case 0x01:
  678. memset(Machine_info.btn_waitMaterials_info,0,6);
  679. break;
  680. case 0x02:
  681. memset(Machine_info.btn_safeKeep_info,0,6);
  682. break;
  683. case 0x03:
  684. memset(Machine_info.btn_breakDown_info,0,6);
  685. break;
  686. case 0x04:
  687. memset(Machine_info.btn_upKeep_info,0,6);
  688. break;
  689. case 0x05:
  690. memset(Machine_info.btn_shutDown_info,0,6);
  691. break;
  692. default:
  693. ESP_LOGE(LOG_TAG,"err:按键状态%d",__LINE__);
  694. break;
  695. }
  696. }
  697. else if(terminal_info.status_setting[1] == 0x01)//open
  698. {
  699. Machine_info.btn_dis_flag[tmp-1] = true;
  700. //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]);
  701. if((terminal_info.status_setting[2] == 0x00) && (terminal_info.status_setting[3] == 0x00))
  702. {//别名为两个字,这判断第一个字为空,为空就赋值初始化的按键信息
  703. ESP_LOGE(LOG_TAG,"button is open[other name is NULL] %d",terminal_info.status_setting[0]);
  704. switch (terminal_info.status_setting[0])
  705. {
  706. case 0x00://运行
  707. printf("clear btn_operation display\r\n");
  708. memcpy(Machine_info.btn_operation,str_operation,6);
  709. break;
  710. case 0x01://待料
  711. printf("clear btn_waitMaterials_info display\r\n");
  712. memcpy(Machine_info.btn_waitMaterials_info,str_waitMaterials,6);
  713. break;
  714. case 0x02://封存
  715. printf("clear btn_safeKeep_info display\r\n");
  716. memcpy(Machine_info.btn_safeKeep_info,str_safeKeep,6);
  717. break;
  718. case 0x03://故障
  719. printf("clear btn_breakDown_info display\r\n");
  720. memcpy(Machine_info.btn_breakDown_info,str_breakDown,6);
  721. break;
  722. case 0x04://保养
  723. printf("clear btn_upKeep_info display\r\n");
  724. memcpy(Machine_info.btn_upKeep_info,str_upKeep,6);
  725. break;
  726. case 0x05://停机
  727. printf("clear btn_shutDown_info display\r\n");
  728. memcpy(Machine_info.btn_shutDown_info,str_shutDown,6);
  729. break;
  730. default:
  731. printf("other btn_operation display\r\n");
  732. ESP_LOGE(LOG_TAG,"err:按键状态%d",__LINE__);
  733. break;
  734. }
  735. }
  736. else
  737. {
  738. // ESP_LOGW(LOG_TAG,"button is open[other name] %d",terminal_info.status_setting[0]);
  739. switch (terminal_info.status_setting[0])
  740. {
  741. case 0x00:
  742. memset(Machine_info.btn_operation,0,6);
  743. uincode2gbk((char*)terminal_info.status_setting + 2,4,(char*)Machine_info.btn_operation);
  744. //ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_operation,6);
  745. break;
  746. case 0x01:
  747. memset(Machine_info.btn_waitMaterials_info,0,6);
  748. uincode2gbk((char*)terminal_info.status_setting + 2,4,(char*)Machine_info.btn_waitMaterials_info);
  749. //ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_waitMaterials_info,6);
  750. break;
  751. case 0x02:
  752. memset(Machine_info.btn_safeKeep_info,0,6);
  753. uincode2gbk((char*)terminal_info.status_setting + 2,4,(char*)Machine_info.btn_safeKeep_info);
  754. //ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_safeKeep_info,6);
  755. break;
  756. case 0x03:
  757. memset(Machine_info.btn_breakDown_info,0,6);
  758. uincode2gbk((char*)terminal_info.status_setting + 2,4,(char*)Machine_info.btn_breakDown_info);
  759. //ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_breakDown_info,6);
  760. break;
  761. case 0x04:
  762. memset(Machine_info.btn_upKeep_info,0,6);
  763. uincode2gbk((char*)terminal_info.status_setting + 2,4,(char*)Machine_info.btn_upKeep_info);
  764. //ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_upKeep_info,6);
  765. break;
  766. case 0x05:
  767. memset(Machine_info.btn_shutDown_info,0,6);
  768. uincode2gbk((char*)terminal_info.status_setting + 2,4,(char*)Machine_info.btn_shutDown_info);
  769. //ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_shutDown_info,6);
  770. break;
  771. default:
  772. ESP_LOGE(LOG_TAG,"err:按键状态%d",__LINE__);
  773. break;
  774. }
  775. }
  776. }
  777. else
  778. {
  779. ESP_LOGE(LOG_TAG,"not open or close");
  780. }
  781. }
  782. uint8_t change_btn(uint8_t btn);
  783. static int status_setting_func(const LORA_DATA_T* buf)
  784. {
  785. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  786. memset(terminal_info.status_setting,0x00,sizeof(terminal_info.status_setting));
  787. memcpy(terminal_info.status_setting,buf->data_buf+DATA_START_LEN,len); //别名
  788. ESP_LOGE(LOG_TAG,"len = %d ",len);
  789. ESP_LOG_BUFFER_HEX(LOG_TAG,terminal_info.status_setting,10);
  790. set_button_anotherName();
  791. //当前按键状态关闭了
  792. if((change_btn(Machine_info.left_state) == terminal_info.status_setting[0]) && (Machine_info.btn_dis_flag[Machine_info.left_state-1] == false))
  793. {
  794. ESP_LOGE(LOG_TAG,"按键发送");
  795. send_button_key_queue(STATE_OPERATION);
  796. }
  797. else
  798. {
  799. // display_flag = LEFT_FLAG;
  800. xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
  801. }
  802. ESP_LOGW(LOG_TAG,"按键 :%s %s %s %s %s %s",\
  803. Machine_info.btn_dis_flag[0]?"开启":"关闭",\
  804. Machine_info.btn_dis_flag[1]?"开启":"关闭",\
  805. Machine_info.btn_dis_flag[2]?"开启":"关闭",\
  806. Machine_info.btn_dis_flag[3]?"开启":"关闭",\
  807. Machine_info.btn_dis_flag[4]?"开启":"关闭",\
  808. Machine_info.btn_dis_flag[5]?"开启":"关闭");
  809. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  810. return msg_id;
  811. }
  812. static int status_duration_func(const LORA_DATA_T* buf)
  813. {
  814. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  815. memcpy(terminal_info.status_duration,buf->data_buf+DATA_START_LEN,len);
  816. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  817. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  818. //printf("msg_id =%X\r\n",msg_id);
  819. return msg_id;
  820. }
  821. static int update_and_duration_func(const LORA_DATA_T* buf)
  822. {
  823. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  824. memcpy(terminal_info.update_and_duration,buf->data_buf+DATA_START_LEN,len);
  825. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  826. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  827. //printf("msg_id =%X\r\n",msg_id);
  828. return msg_id;
  829. }
  830. static int announcement_func(const LORA_DATA_T* buf)
  831. {
  832. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  833. memcpy(terminal_info.announcement,buf->data_buf+DATA_START_LEN,len); //拷贝公告
  834. ESP_LOG_BUFFER_HEX(LOG_TAG,terminal_info.announcement,len);
  835. ESP_LOGW(LOG_TAG,"设置公告");
  836. //memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
  837. memset(Machine_info.announcement,0x00,sizeof(Machine_info.announcement)); //清除终端公告信息
  838. uincode2gbk((char*)terminal_info.announcement,len,(char*)Machine_info.announcement);
  839. // printf("unicode:");
  840. // for(i=0;i<len;i++)
  841. // {
  842. // printf("%02x ",terminal_info.terminal_name[i]);
  843. // }
  844. // printf("\n");
  845. // printf("gbk:");
  846. // for(i=0;i<len;i++)
  847. // {
  848. // printf("%02x ",Machine_info.terminal_name[i]);
  849. // }
  850. // printf("setting gong gao %s",Machine_info.announcement);
  851. // printf("\n");
  852. // display_flag = RIGHT_FLAG;
  853. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  854. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  855. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  856. //printf("msg_id =%X\r\n",msg_id);
  857. return msg_id;
  858. }
  859. void set_statisticsData(uint8_t* data)
  860. {
  861. // memset(&Machine_info.num_goodProducts,0,32);
  862. // memset(&Machine_info.num_badProducts,0,32);
  863. #define __DATA_SIZE 24
  864. for(int i=0;i<8;i++)
  865. {
  866. 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]);
  867. 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]);
  868. Machine_info.scale_UR_int[i] = (data[8+i*__DATA_SIZE]);
  869. Machine_info.scale_UR_dec[i] = (data[9+i*__DATA_SIZE]);
  870. Machine_info.scale_YR_int[i] = (data[10+i*__DATA_SIZE]);
  871. Machine_info.scale_YR_dec[i] = (data[11+i*__DATA_SIZE]);
  872. Machine_info.num_manHour[i] = (data[12+i*__DATA_SIZE]<<8)|(data[13+i*__DATA_SIZE]);
  873. Machine_info.num_people[i] = (data[14+i*__DATA_SIZE]<<8)|(data[15+i*__DATA_SIZE]);
  874. Machine_info.num_production[i] = (data[16+i*__DATA_SIZE]<<8)|(data[17+i*__DATA_SIZE]);
  875. Machine_info.num_repair[i] = (data[18+i*__DATA_SIZE]<<8)|(data[19+i*__DATA_SIZE]);
  876. Machine_info.num_inspection[i] = (data[20+i*__DATA_SIZE]<<8)|(data[21+i*__DATA_SIZE]);
  877. Machine_info.num_upkeep[i] = (data[22+i*__DATA_SIZE]<<8)|(data[23+i*__DATA_SIZE]);
  878. //ESP_LOGW(LOG_TAG,"good:[%ld] bad[%ld]",Machine_info.num_goodProducts[i],Machine_info.num_badProducts[i]);
  879. }
  880. }
  881. static int capacity_statistics_func(const LORA_DATA_T* buf)
  882. {
  883. #if 1
  884. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  885. memcpy(terminal_info.capacity_statistics,buf->data_buf+DATA_START_LEN,len);
  886. #endif
  887. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  888. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  889. //printf("msg_id =%X\r\n",msg_id);
  890. set_statisticsData(terminal_info.capacity_statistics);
  891. // display_flag = RIGHT_FLAG;
  892. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  893. return msg_id;
  894. }
  895. static int responsible_person_synchronize_info_func(const LORA_DATA_T* buf)
  896. {
  897. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  898. memcpy(terminal_info.responsible_person_synchronize_info,buf->data_buf+DATA_START_LEN,len);
  899. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  900. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  901. //printf("msg_id =%X\r\n",msg_id);
  902. // display_flag = RIGHT_FLAG;
  903. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  904. return msg_id;
  905. }
  906. static int status_led_func(const LORA_DATA_T* buf)
  907. {
  908. terminal_info.states_led = buf->data_buf[DATA_START_LEN];
  909. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  910. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  911. //printf("msg_id =%X\r\n",msg_id);
  912. return msg_id;
  913. }
  914. static int logo_led_func(const LORA_DATA_T* buf)
  915. {
  916. terminal_info.logo_led = buf->data_buf[DATA_START_LEN];
  917. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  918. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  919. //printf("msg_id =%X\r\n",msg_id);
  920. return msg_id;
  921. }
  922. static int battery_temperature_func(const LORA_DATA_T* buf)
  923. {
  924. terminal_info.batt_temperature[0] = buf->data_buf[DATA_START_LEN];
  925. terminal_info.batt_temperature[1] = buf->data_buf[DATA_START_LEN+1];
  926. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  927. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  928. //printf("msg_id =%X\r\n",msg_id);
  929. return msg_id;
  930. }
  931. static int station_name_func(const LORA_DATA_T* buf)
  932. {
  933. #define FONT_MAX_LEN 8
  934. uint8_t font_num = 0; //获取当前的文字个数
  935. int i=0;
  936. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  937. memset(terminal_info.station_name,0,32);
  938. memcpy(terminal_info.station_name,buf->data_buf+DATA_START_LEN,len);
  939. //memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
  940. memset(Machine_info.station_name,0x00,sizeof(Machine_info.station_name)); //清除终端名称信息
  941. font_num = len/2;
  942. if(font_num<=FONT_MAX_LEN)
  943. {
  944. uincode2gbk((char*)terminal_info.station_name,len,(char*)Machine_info.station_name);
  945. }
  946. else
  947. {
  948. uincode2gbk((char*)terminal_info.station_name,FONT_MAX_LEN*2,(char*)Machine_info.station_name);
  949. strcat((char *)Machine_info.station_name,"...");
  950. }
  951. // display_flag = RIGHT_FLAG;
  952. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  953. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  954. return msg_id;
  955. }
  956. static int station_number_func(const LORA_DATA_T* buf)
  957. {
  958. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  959. memcpy(terminal_info.station_number,buf->data_buf+DATA_START_LEN,len);
  960. memset(Machine_info.station_number,0x00,sizeof(Machine_info.station_number));
  961. uincode2gbk((char*)terminal_info.station_number,len,(char*)Machine_info.station_number);
  962. // display_flag = RIGHT_FLAG;
  963. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  964. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  965. return msg_id;
  966. }
  967. static int responsible_person_setup_func(const LORA_DATA_T* buf)
  968. {
  969. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  970. ESP_LOG_BUFFER_HEX(LOG_TAG,buf->data_buf + DATA_START_LEN,len);
  971. ESP_LOGE(LOG_TAG,"设置责任人 类型[%d]%s",buf->data_buf[DATA_START_LEN],buf->data_buf[DATA_START_LEN+1]?"开启":"关闭");
  972. if(buf->data_buf[DATA_START_LEN+1] == 0x01)//开
  973. {
  974. Machine_info.person[buf->data_buf[DATA_START_LEN]].Charge_close = false;
  975. memset(Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name,0,16);
  976. uincode2gbk(buf->data_buf+DATA_START_LEN +2,len-2,(char*)Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name);
  977. // ESP_LOG_BUFFER_HEX(LOG_TAG,Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name,len);
  978. }
  979. else if(buf->data_buf[DATA_START_LEN+1] == 0x00)//关
  980. {
  981. Machine_info.person[buf->data_buf[DATA_START_LEN]].Charge_close = true;
  982. }
  983. 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,\
  984. Machine_info.person[3].Charge_close,Machine_info.person[4].Charge_close);
  985. // display_flag = RIGHT_FLAG;
  986. xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
  987. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  988. return msg_id;
  989. }
  990. static int system_bulletin_func(const LORA_DATA_T* buf)
  991. {
  992. ESP_LOGE(LOG_TAG,"system_bulletin_func");
  993. int len = (buf->data_buf[USE_DATA_LEN_INDEX])<<8 | (buf->data_buf[USE_DATA_LEN_INDEX+1]);
  994. memcpy(terminal_info.systemMessage,buf->data_buf+DATA_START_LEN,len);
  995. memset(Machine_info.systemMessage,0x00,sizeof(Machine_info.systemMessage));
  996. uincode2gbk((char*)terminal_info.systemMessage,len,(char*)Machine_info.systemMessage);
  997. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  998. return msg_id;
  999. }
  1000. static int hardware_update_func(const LORA_DATA_T* buf)
  1001. {
  1002. //printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
  1003. int msg_id = ( buf->data_buf[13]>>8)| buf->data_buf[14]; //获取当前消息id 用于删除
  1004. //printf("msg_id =%X\r\n",msg_id);
  1005. return msg_id;
  1006. }
  1007. /*硬件回复命令ACK*/ /*长度 返回result结果及长度*/
  1008. /*********************************************************************************
  1009. * function : reply_ack_func
  1010. * Description : 回复ACK cmd 指定命令
  1011. * Input : cmd 命令 msg_id 当前的ID号 用于网关删除自身的已经接受到ACK 的数据 result 返回当前的数据
  1012. * Output :
  1013. * Author : 祁鑫 Data : 2023 8.12
  1014. **********************************************************************************/
  1015. int reply_ack_func(int cmd,int msg_id,bool is_send,uint8_t *result)
  1016. {
  1017. uint8_t strlorabuf[50]={0};
  1018. uint8_t mac[6] = {0};
  1019. strlorabuf[0]=0x5A;
  1020. strlorabuf[1]=0x51;
  1021. strlorabuf[2]=0x00; //crc
  1022. //int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
  1023. for(int i = 0;i<6;i++)
  1024. {
  1025. strlorabuf[3+i]=Machine_info.gateway_mac[i]; //网关的mac地址
  1026. }
  1027. strlorabuf[9] = Machine_info.eflagID; //组内编号
  1028. strlorabuf[10] = cmd; //命令
  1029. strlorabuf[11] = (msg_id>>8)&0xff; //总包
  1030. strlorabuf[12] = msg_id&0xff; //当前包
  1031. strlorabuf[13] = 0x00; //当前长度
  1032. strlorabuf[14] = PROTOCOL_RSPONSIBLE_ACK; //ACK命令字段
  1033. #if 0
  1034. //设备cid长度
  1035. memcpy(&strlorabuf[15],cid,cid_len); //拷贝cid
  1036. strlorabuf[13] = cid_len+1; //重新设置当前当前长度
  1037. #endif
  1038. int crc = 0; //计算crc
  1039. for(int i = 0;i<sizeof(strlorabuf);i++)
  1040. {
  1041. crc +=strlorabuf[i];
  1042. }
  1043. strlorabuf[2] = crc;
  1044. if(result!=NULL)
  1045. {
  1046. memcpy(result,strlorabuf,15);
  1047. }
  1048. if(is_send)
  1049. {
  1050. lora_send_data((char *)strlorabuf,15);
  1051. uart_wait_tx_idle_polling(UART_NUM_1);
  1052. }
  1053. return 15;
  1054. }
  1055. /*硬件配网回复*/ //参数cid cid长度 返回result 结果及长度
  1056. int reply_gwpair_ack_func(uint8_t *cid,uint8_t cid_len,uint8_t *result)
  1057. {
  1058. uint8_t strlorabuf[50]={0};
  1059. uint8_t mac[6] = {0};
  1060. strlorabuf[0]=0x5A;
  1061. strlorabuf[1]=0x51;
  1062. strlorabuf[2]=0x00; //crc
  1063. //int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
  1064. for(int i = 0;i<6;i++)
  1065. {
  1066. strlorabuf[3+i]=Machine_info.gateway_mac[i]; //网关的mac地址
  1067. }
  1068. strlorabuf[9] = Machine_info.eflagID; //组内编号
  1069. strlorabuf[10] = 0x80; //配对命令
  1070. strlorabuf[11] = 0x01; //总包
  1071. strlorabuf[12] = 0x01; //当前包
  1072. strlorabuf[13] = 0x00; //当前长度
  1073. strlorabuf[14] = 0x01; //请求成功
  1074. //设备cid长度
  1075. memcpy(&strlorabuf[15],cid,cid_len); //拷贝cid
  1076. strlorabuf[13] = cid_len+1; //重新设置当前当前长度
  1077. int crc = 0; //计算crc
  1078. for(int i = 0;i<sizeof(strlorabuf);i++)
  1079. {
  1080. crc +=strlorabuf[i];
  1081. }
  1082. strlorabuf[2] = crc;
  1083. memcpy(result,strlorabuf,14+cid_len+1);
  1084. return 14+cid_len+1;
  1085. }
  1086. /*硬件配网*/
  1087. static void hardware_gwpair_func(const LORA_DATA_T* buf)
  1088. {
  1089. uint8_t child_mac[6] = {0}; //子设备的mac地址
  1090. uint8_t gateway_mac[6] = {0}; //网关的组内编号
  1091. int child_num = 0;
  1092. int gateway_num = 0;
  1093. #if 0
  1094. for(int i = 0;i<buf->data_len;i++)
  1095. {
  1096. printf("%02X",buf->data_buf[i]);
  1097. }
  1098. #endif
  1099. uint64_t time=0x000000000000LL;
  1100. //printf("1\r\n");
  1101. memcpy(child_mac,&buf->data_buf[3],6);
  1102. //printf("2\r\n");
  1103. gateway_num = buf->data_buf[9];
  1104. //printf("3\r\n");
  1105. memcpy(gateway_mac,&buf->data_buf[14],6);
  1106. //printf("4\r\n");
  1107. child_num = buf->data_buf[20];
  1108. //printf("5\r\n");
  1109. //printf("\r\n");
  1110. // for(int i = 21;i<27;i++)
  1111. // {
  1112. // printf("%02X",buf->data_buf[i]);
  1113. // }
  1114. #if 0
  1115. #include <inttypes.h>
  1116. time|=(buf->data_buf[26]<<0);
  1117. //printf("time = %" PRIX64 "\r\n",time);
  1118. time|=(buf->data_buf[25]<<8);
  1119. //printf("time = %" PRIX64 "\r\n",time);
  1120. time|=(buf->data_buf[24]<<16);
  1121. //printf("time = %" PRIX64 "\r\n",time);
  1122. time|=(buf->data_buf[23]<<24);
  1123. //printf("time = %" PRIX64 "\r\n",time);
  1124. #if 0
  1125. uint64_t temp = 0;
  1126. temp |=(buf->data_buf[22]<<32);
  1127. temp |=(buf->data_buf[21]<<40);
  1128. printf("temp = %" PRIX64 "\r\n",temp);
  1129. #endif
  1130. #if 1
  1131. int64_t temp = 0;
  1132. temp = buf->data_buf[22];
  1133. time|=(temp<<32);//(buf->data_buf[22]<<32);
  1134. #else
  1135. time|=temp;
  1136. #endif
  1137. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1138. #if 0
  1139. time|=(buf->data_buf[21]<<40);
  1140. #else
  1141. temp = 0;
  1142. temp = buf->data_buf[21];
  1143. time|=(temp<<40);//(buf->data_buf[22]<<32);
  1144. //time|=(buf->data_buf[21]<<40);
  1145. #endif
  1146. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1147. //uint64_t value = 0x018B1E3D24AC;
  1148. //printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
  1149. //printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
  1150. #if 0
  1151. time=(buf->data_buf[21]<<40)+
  1152. (buf->data_buf[22]<<32)+
  1153. (buf->data_buf[23]<<24)+
  1154. (buf->data_buf[24]<<16)+
  1155. (buf->data_buf[25]<<8) +
  1156. (buf->data_buf[26]<<0);
  1157. #endif
  1158. #else
  1159. Machine_info.lora_new_channel = buf->data_buf[21];
  1160. printf("Machine_info.lora_new_channel =%d\r\n",Machine_info.lora_new_channel);
  1161. memcpy(Machine_info.timestamp,&buf->data_buf[22],13);
  1162. printf("Machine_info.timestamp =%s\r\n",Machine_info.timestamp);
  1163. int timezone_offset = 8; // 中国时区偏移为UTC+8,你可以根据需要设置不同的时区偏移
  1164. Machine_info.time_offset = timezone_offset;
  1165. timestamp_to_local_time(Machine_info.timestamp,
  1166. Machine_info.time_offset,
  1167. &Machine_info.year,
  1168. &Machine_info.month,
  1169. &Machine_info.day,
  1170. &Machine_info.hour,
  1171. &Machine_info.min,
  1172. &Machine_info.sec);
  1173. setRtcTime(Machine_info.year, Machine_info.month,Machine_info.day,
  1174. Machine_info.hour,Machine_info.min,Machine_info.sec);
  1175. //更新当前状态时间
  1176. Machine_info.current_button.Year = Machine_info.year;
  1177. Machine_info.current_button.Month = Machine_info.month;
  1178. Machine_info.current_button.Day = Machine_info.day;
  1179. Machine_info.current_button.Hour = Machine_info.hour;
  1180. Machine_info.current_button.Minute = Machine_info.min;
  1181. Machine_info.current_button.Second = Machine_info.sec;
  1182. Machine_info.last_button.Year = Machine_info.year;
  1183. Machine_info.last_button.Month = Machine_info.month;
  1184. Machine_info.last_button.Day = Machine_info.day;
  1185. Machine_info.last_button.Hour = Machine_info.hour;
  1186. Machine_info.last_button.Minute = Machine_info.min;
  1187. Machine_info.last_button.Second = Machine_info.sec;
  1188. #endif
  1189. printf("gateway mac = ");
  1190. for(int i = 0;i<6;i++)
  1191. printf("%02X",gateway_mac[i]);
  1192. printf("\r\n");
  1193. printf("gateway num = %d\r\n",gateway_num);
  1194. printf("child mac = ");
  1195. for(int i = 0;i<6;i++)
  1196. printf("%02X",child_mac[i]);
  1197. printf("\r\n");
  1198. printf("child_num num = %d\r\n",child_num);
  1199. printf("time = %llu\r\n",time);
  1200. printf("time = %llx\r\n",time);
  1201. //(3000 - (480 - 1*100))
  1202. #include "user_sleep.h"
  1203. #include "esp_sleep.h"
  1204. //配网完成设置休眠唤醒时间
  1205. if(Machine_info.eflagID == 0xff)
  1206. {
  1207. #if 0
  1208. printf("sleep time = %d ms\r\n",(TIMER_WAKEUP_TIME_US*child_num)/1000);
  1209. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*child_num); //配置当前休眠的唤醒时间
  1210. #else
  1211. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US); //配置当前休眠的唤醒时间
  1212. #endif
  1213. }
  1214. Machine_info.eflagID = child_num; //保存当前的子设备组内编号
  1215. memcpy(Machine_info.gateway_mac,gateway_mac,6);
  1216. //配网成功 回复 ACK 设置新的信道
  1217. Machine_info.paired = 1; //已配对
  1218. #if 0
  1219. send_pair_ack();
  1220. #else
  1221. int msg_id = ( buf->data_buf[11]<<8)| buf->data_buf[12]; //获取当前消息id 用于删除
  1222. printf("msg_id =%X\r\n",msg_id);
  1223. reply_ack_func(PROTOCOL_HARDWARE_GWPAIRED,msg_id,true,NULL); //回复ack
  1224. #endif
  1225. // vTaskDelay(200 / portTICK_PERIOD_MS);
  1226. // //修改信道前修改设备ID 及 设备ID
  1227. // dymatic_change_device_id(child_num+1);
  1228. // dymatic_change_dst_device_id(0x00000001);
  1229. // //切换信道
  1230. // dymatic_change_chanel(Machine_info.lora_new_channel); //切换信道
  1231. // lora_get_rssi();
  1232. vTaskDelay(200 / portTICK_PERIOD_MS);
  1233. Machine_info.rssi = set_lora(Machine_info.lora_new_channel,Machine_info.eflagID);
  1234. f_send_version();
  1235. f_send_reply_status(0x01); //回复在线状态
  1236. send_update_status();
  1237. // f_send_lora_rssi(Machine_info.rssi);
  1238. f_send_lora_rssi(100);//刚配完直接发100
  1239. f_send_battary_vaule(Machine_info.batt_precent);
  1240. #if USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  1241. sleep_timer_start(2000); //进入睡眠
  1242. #endif
  1243. //printf("start timer id = %d,%d\r\n",Machine_info.eflagID,Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  1244. //Already_send_timer_start(Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  1245. }
  1246. /*硬件取消配网*/
  1247. static void hardware_ungwpair_func(const LORA_DATA_T* buf)
  1248. {
  1249. uint8_t child_mac[6] = {0}; //子设备的mac地址
  1250. uint8_t gateway_mac[6] = {0}; //网关的组内编号
  1251. int child_num = 0;
  1252. int gateway_num = 0;
  1253. uint64_t time=0x000000000000LL;
  1254. memcpy(child_mac,&buf->data_buf[3],6);
  1255. gateway_num = buf->data_buf[9];
  1256. memcpy(gateway_mac,&buf->data_buf[14],6);
  1257. child_num = buf->data_buf[20];
  1258. #include <inttypes.h>
  1259. time|=(buf->data_buf[26]<<0);
  1260. time|=(buf->data_buf[25]<<8);
  1261. time|=(buf->data_buf[24]<<16);
  1262. time|=(buf->data_buf[23]<<24);
  1263. #if 1
  1264. int64_t temp = 0;
  1265. temp = buf->data_buf[22];
  1266. time|=(temp<<32);//(buf->data_buf[22]<<32);
  1267. #else
  1268. time|=temp;
  1269. #endif
  1270. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1271. #if 0
  1272. time|=(buf->data_buf[21]<<40);
  1273. #else
  1274. temp = 0;
  1275. temp = buf->data_buf[21];
  1276. time|=(temp<<40);//(buf->data_buf[22]<<32);
  1277. //time|=(buf->data_buf[21]<<40);
  1278. #endif
  1279. //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
  1280. //uint64_t value = 0x018B1E3D24AC;
  1281. //printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
  1282. //printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
  1283. printf("gateway mac = ");
  1284. for(int i = 0;i<6;i++)
  1285. printf("%02X",gateway_mac[i]);
  1286. printf("\r\n");
  1287. printf("gateway num = %d\r\n",gateway_num);
  1288. printf("child mac = ");
  1289. for(int i = 0;i<6;i++)
  1290. printf("%02X",child_mac[i]);
  1291. printf("\r\n");
  1292. printf("child_num num = %d\r\n",child_num);
  1293. printf("time = %llu\r\n",time);
  1294. printf("time = %llx\r\n",time);
  1295. //(3000 - (480 - 1*100))
  1296. #include "user_sleep.h"
  1297. #include "esp_sleep.h"
  1298. memcpy(Machine_info.gateway_mac,gateway_mac,6);
  1299. //取消配网成功 回复 ACK 设置新的信道
  1300. #if 0
  1301. send_pair_ack();
  1302. #else
  1303. int msg_id = ( buf->data_buf[11]<<8)| buf->data_buf[12]; //获取当前消息id 用于删除
  1304. //printf("msg_id =%X\r\n",msg_id);
  1305. reply_ack_func(PROTOCOL_HARDWARE_UNGWPAIRED,msg_id,true,NULL); //回复ack
  1306. #endif
  1307. //#include "user_sleep.h"
  1308. //#include "esp_sleep.h"
  1309. extern void sleep_timer_stop();
  1310. extern void Already_send_timer_stop();
  1311. sleep_timer_stop();
  1312. Already_send_timer_stop();
  1313. //修改信道前修改设备ID 及 设备ID
  1314. dymatic_change_device_id(0x00000001);
  1315. dymatic_change_dst_device_id(0xFFFFFFFF);
  1316. //切换信道
  1317. reset_default();
  1318. vTaskDelay(300 / portTICK_PERIOD_MS);
  1319. Machine_info.lora_new_channel = LORA_CHANENL;
  1320. // dymatic_change_chanel(Machine_info.lora_factory_channel); //切换信道
  1321. reset_lora();
  1322. }
  1323. /*网关收到终端发送的数据 ACK 终端收到用于删除当前的msgid的数据*/
  1324. static void hardware_ack_delete_msg_id(const LORA_DATA_T* buf)
  1325. {
  1326. int msg_id = ( buf->data_buf[11]>>8)| buf->data_buf[12]; //获取当前消息id 用于删除当前链表的数据
  1327. int current = countNodes(Send_list); //获取当前节点数据
  1328. Node *find = findNode(Send_list,msg_id);
  1329. if(find != NULL)
  1330. {
  1331. // printf("delete list node bofore = %d\r\n",msg_id);
  1332. // printList(Send_list);
  1333. Send_list = deleteNode(Send_list,msg_id);
  1334. // printf("delete list node msg_id = %d\r\n",msg_id);
  1335. // printf("delete list node after = %d\r\n",msg_id);
  1336. // printList(Send_list);
  1337. }
  1338. else
  1339. {
  1340. ESP_LOGE(LOG_TAG,"delete fail");
  1341. }
  1342. display_flag = 0;
  1343. }
  1344. void business_logic_func(const LORA_DATA_T* buf,uint8_t cmd_index,int msg)
  1345. {
  1346. int cmd = 0; //当前的命令
  1347. int msg_id = 0; //消息的id
  1348. bool is_rsp_ack = false;
  1349. #if 0
  1350. printf("cmd = %02X\r\n",buf->data_buf[cmd_index]);
  1351. #endif
  1352. cmd = buf->data_buf[cmd_index];
  1353. switch (buf->data_buf[cmd_index])
  1354. {
  1355. case PROTOCOL_TIME_SYNCHRONIZATION:
  1356. msg_id = time_synchronization_func(buf);
  1357. //is_rsp_ack = true;
  1358. break;
  1359. case PROTOCOL_TERMINAL_NAME:
  1360. msg_id = terminal_name_func(buf);
  1361. //is_rsp_ack = true;
  1362. break;
  1363. case PROTOCOL_TERMINAL_NUMBER:
  1364. msg_id = terminal_number_func(buf);
  1365. //is_rsp_ack = true;
  1366. break;
  1367. case PROTOCOL_ELECTRIC_QUANTITY:
  1368. msg_id = electric_quantity_func(buf);
  1369. //is_rsp_ack = true;
  1370. break;
  1371. case PROTOCOL_SIGNAL_STRENGTH:
  1372. msg_id = signal_strength_func(buf);
  1373. //is_rsp_ack = true;
  1374. break;
  1375. case PROTOCOL_SCREEN_REFRESH_CYCLE:
  1376. msg_id = screen_refresh_cycle_func(buf);
  1377. //is_rsp_ack = true;
  1378. break;
  1379. case PROTOCOL_SCREEN_LEFT:
  1380. msg_id = screen_left_func(buf);
  1381. //is_rsp_ack = true;
  1382. break;
  1383. case PROTOCOL_SCREEN_RIGHT:
  1384. msg_id = screen_right_func(buf);
  1385. //is_rsp_ack = true;
  1386. break;
  1387. case PROTOCOL_RSPONSIBLE_PERSON_CHANGE:
  1388. msg_id = rsponsible_person_change_func(buf);
  1389. //is_rsp_ack = true;
  1390. break;
  1391. case PROTOCOL_RSPONSIBLE_PERSON_DELETE:
  1392. msg_id = rsponsible_person_delete_func(buf);
  1393. //is_rsp_ack = true;
  1394. break;
  1395. case PROTOCOL_PRODUCTION_PUNCH_CARD:
  1396. msg_id = production_punch_func(buf);
  1397. // is_rsp_ack = true;
  1398. break;
  1399. case PROTOCOL_PERSONNEL_CHECK_IN:
  1400. msg_id = personnel_check_in_func(buf);
  1401. //is_rsp_ack = true;
  1402. break;
  1403. case PROTOCOL_STATUS_SETTING:
  1404. msg_id = status_setting_func(buf);
  1405. //is_rsp_ack = true;
  1406. break;
  1407. case PROTOCOL_STATUS_DURATION:
  1408. msg_id = status_duration_func(buf);
  1409. //is_rsp_ack = true;
  1410. break;
  1411. case PROTOCOL_UPDATE_AND_DURATION:
  1412. msg_id = update_and_duration_func(buf);
  1413. // is_rsp_ack = true;
  1414. break;
  1415. case PROTOCOL_ANNOUNCEMENT:
  1416. msg_id = announcement_func(buf);
  1417. // is_rsp_ack = true;
  1418. break;
  1419. case PROTOCOL_CAPACITY_STATISTICS:
  1420. msg_id = capacity_statistics_func(buf);
  1421. // is_rsp_ack = true;
  1422. break;
  1423. case PROTOCOL_RESPONSIBLE_PERSON_SYNCHRONIZE_INFO:
  1424. msg_id = responsible_person_synchronize_info_func(buf);
  1425. // is_rsp_ack = true;
  1426. break;
  1427. case PROTOCOL_STATUS_LED:
  1428. msg_id = status_led_func(buf);
  1429. // is_rsp_ack = true;
  1430. break;
  1431. case PROTOCOL_LOG_LED:
  1432. msg_id = logo_led_func(buf);
  1433. // is_rsp_ack = true;
  1434. break;
  1435. case PROTOCOL_BATTERY_TEMPERATURE:
  1436. msg_id = battery_temperature_func(buf);
  1437. // is_rsp_ack = true;
  1438. break;
  1439. case PROTOCOL_STATION_NAME:
  1440. msg_id = station_name_func(buf);
  1441. // is_rsp_ack = true;
  1442. break;
  1443. case PROTOCOL_STATION_NUMBER:
  1444. msg_id = station_number_func(buf);
  1445. // is_rsp_ack = true;
  1446. break;
  1447. case PROTOCOL_RESPONSIBLE_PERSON_SETUP:
  1448. msg_id = responsible_person_setup_func(buf);
  1449. // is_rsp_ack = true;
  1450. break;
  1451. case PROTOCOL_SYSTEM_BULLETIN:
  1452. msg_id = system_bulletin_func(buf);
  1453. // is_rsp_ack = true;
  1454. break;
  1455. case PROTOCOL_HARDWARE_UPDATE:
  1456. msg_id = hardware_update_func(buf);
  1457. // is_rsp_ack = true;
  1458. break;
  1459. case PROTOCOL_HARDWARE_GWPAIRED: //配网 暂时单独发送ack
  1460. reset_default();
  1461. hardware_gwpair_func(buf);
  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_LOGD(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. }