yc_protocol.c 121 KB

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