yc_protocol.c 120 KB

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