yc_protocol.c 122 KB

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