yc_protocol.c 123 KB

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