yc_protocol.c 120 KB

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