yc_protocol.c 119 KB

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