1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568 |
- #include "yc_protocol.h"
- #include "esp_log.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/queue.h"
- #include "esp_mac.h"
- #include "stdbool.h"
- #include "esp_timer.h"
- #include "list.h"
- #include "user_time.h"
- #include "freertos/event_groups.h"
- #include "list.h"
- #include "wait_list.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "driver/uart.h"
- #include "freertos/timers.h"
- #include "SPIFFS.h"
- // #include "user_config.h"
- RTC_NOINIT_ATTR Screen_dis_t screen_dis_info;
- // 声明一个TimerHandle_t类型的变量,用于存储定时器句柄
- extern EventGroupHandle_t screen_event;
- extern int retry_times;
- static const char *LOG_TAG = "yc_protocol";
- // esp_timer_handle_t periodic_timer;
- esp_timer_handle_t lora_timer;
- TERMINAL_INFO_T terminal_info; // 保存收到的lora数据
- Button_Time_t last_button_info;
- Button_Time_t new_button_info;
- Button_Time_t _0703_button_info;
- void changebintotxt(uint8_t *strb, int len);
- Node *clockIn_list = NULL; // 打卡 n:crc(ID) cmd:打卡类型 data:ID
- extern char str_operation[6]; // 运行
- extern char str_upKeep[6]; // 保养
- extern char str_shutDown[6]; // 停机
- extern char str_safeKeep[6]; // 封存
- extern char str_waitMaterials[6]; // 待料
- extern char str_breakDown[6]; // 故障
- extern QueueHandle_t screen_queue;
- uint8_t mac_addr[6];
- extern QueueHandle_t yc_data_queue;
- extern YC_DATA_T yc_data;
- // extern ListNode *list_head;
- #define LEFT_FLAG 1
- #define RIGHT_FLAG 2
- static uint8_t display_flag = 0; // 1:left 2:right
- #if 1
- #include "esp_attr.h"
- RTC_FAST_ATTR Person_t person[5] =
- {
- {
- .person_type = Administrator,
- },
- {
- .person_type = product_person,
- },
- {
- .person_type = repair_person,
- },
- {
- .person_type = Maintenance_person,
- },
- {
- .person_type = check_person,
- },
- };
- #endif
- void reset_default(bool is_dis, uint8_t power_status)
- {
- extern Machine_info_t default_info;
- Machine_info_t tmp;
- memcpy(&tmp, &default_info, sizeof(Machine_info_t));
- tmp.power_status = power_status;
- esp_read_mac((uint8_t *)tmp.mac_addr, ESP_MAC_WIFI_STA);
- memcpy(&tmp.cid, &Machine_info.cid, 20);
- tmp.batt_precent = Machine_info.batt_precent;
- tmp.last_batt_precent = Machine_info.last_batt_precent;
- tmp.rssi = Machine_info.rssi;
- memcpy(&Machine_info, &tmp, sizeof(Machine_info_t)); // 恢复默认
- spiffs_write(&Machine_info); // 保存Machine_info
- if (!is_dis)
- {
- return;
- }
- // xQueueReset(screen_queue);
- set_screen_dis_info_and_send_queue(false, true, false, false, 100);
- set_screen_dis_info_and_send_queue(true, false, false, false, 100);
- }
- /*
- * 使用 vTaskList() 前需使能:
- * Enable FreeRTOS trace facility
- * Enable FreeRTOS stats formatting functions
- */
- void print_systenInfo(void)
- {
- printf("=================================\r\n");
- printf("=======%s======\r\n", "2024.1.9 [18时] burn"); // 打印烧录时间
- printf("=============%s===========\r\n", __TIME__);
- printf("=================================\r\n");
- printf("========version V %d.%d.%d==========\r\n", VERSION_X, VERSION_Y, VERSION_Z);
- esp_efuse_mac_get_default((uint8_t *)mac_addr);
- esp_efuse_mac_get_default((uint8_t *)Machine_info.mac_addr);
- #if 1 // 获取mac地址
- uint8_t macAddr[6];
- esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
- // 保留以前的mac地址段
- // mac_addr[0]+=0x02;
- {
- printf("mac addr : %02x:%02x:%02x:%02x:%02x:%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
- }
- memcpy((uint8_t *)Machine_info.mac_addr, macAddr, 6);
- // Machine_info.mac_addr[0]= Machine_info.mac_addr[0]+0x02;
- 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]);
- #endif
- printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n",
- esp_get_free_heap_size(), esp_get_free_internal_heap_size(), esp_get_minimum_free_heap_size());
- #if 0
- ESP_LOGW(LOG_TAG,"mac:[%02x-%02x-%02x-%02x-%02x-%02x]",\
- mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[05]);
- static char InfoBuffer[512] = {0};
- vTaskList((char*)&InfoBuffer);
- ESP_LOGW(LOG_TAG,"free_heap_size:%ld \nfree_internal_heap_size:%ld \nminimum_free_heap_size:%ld",\
- esp_get_free_heap_size(),esp_get_free_internal_heap_size(),esp_get_minimum_free_heap_size());
- printf("任务名 任务状态 优先级 剩余栈 任务序号\r\n");
- printf("\r\n%s\r\n", InfoBuffer);
- #endif
- }
- void f_send_update_status(void);
- void f_send_status_duration(void);
- 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)
- {
- 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);
- screen_dis_info.is_into_sleep = is_into_sleep;
- screen_dis_info.is_left = is_left;
- screen_dis_info.is_change_power = is_change_power;
- screen_dis_info.is_dont_dis = is_dont_dis;
- screen_dis_info.sleep_ms = ms;
- if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
- {
- ESP_LOGE(LOG_TAG, "err:screen queue send fail");
- }
- }
- void terminal_send_data(void)
- {
- // vTaskDelay(100 / portTICK_PERIOD_MS);
- f_send_status_duration();
- // vTaskDelay(50 / portTICK_PERIOD_MS);
- f_send_update_status();
- // vTaskDelay(100 / portTICK_PERIOD_MS);
- }
- void update_last_button_info(uint8_t btn)
- {
- #if 0
- memset(&last_button_info,0,sizeof(Button_Time_t));
- last_button_info.button_info = btn;
-
- last_button_info.Year = Machine_info.year;
- last_button_info.Month = Machine_info.month;
- last_button_info.Day = Machine_info.day;
- last_button_info.Hour = Machine_info.hour;
- last_button_info.Minute = Machine_info.min;
- last_button_info.Second = Machine_info.sec;
- ESP_LOGW(LOG_TAG,"btn = %d [%d-%d-%d]%d:%d:%d", btn, Machine_info.year,Machine_info.month,Machine_info.day,\
- Machine_info.hour, Machine_info.min,Machine_info.sec);
- //last_button_info.time_min = 0;
- #else
- // 更新上次的状态
- // 获取当前时间
- getRtcTime(&Machine_info);
- Machine_info.last_button.button_info = btn;
- Machine_info.last_button.Year = Machine_info.year;
- Machine_info.last_button.Month = Machine_info.month;
- Machine_info.last_button.Day = Machine_info.day;
- Machine_info.last_button.Hour = Machine_info.hour;
- Machine_info.last_button.Minute = Machine_info.min;
- Machine_info.last_button.Second = Machine_info.sec;
- Machine_info.last_button.time_min = 0x00000000;
- #endif
- }
- void screen_display(void)
- {
- // font_exit_sleep();
- EventBits_t bits = xEventGroupWaitBits(screen_event,
- LEFT_SCREEN_BIT | RIGHT_SCREEN_BIT,
- pdTRUE,
- pdFALSE,
- 0);
- ESP_LOGW(LOG_TAG, "xEventGroupWaitBits bits = %ld", bits);
- /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
- * happened. */
- if (bits == 3)
- {
- // xQueueReset(screen_queue);
- Machine_info.left_current_Quick_refresh_time = 4; // 刷两个屏最好不同时慢刷
- set_screen_dis_info_and_send_queue(false, true, false, false, 100);
- set_screen_dis_info_and_send_queue(true, false, false, false, 100);
- }
- else if (bits & RIGHT_SCREEN_BIT)
- {
- ESP_LOGE(LOG_TAG, "RIGHT_SCREEN_BIT");
- set_screen_dis_info_and_send_queue(true, false, false, false, 100);
- }
- else if (bits & LEFT_SCREEN_BIT)
- {
- ESP_LOGE(LOG_TAG, "LEFT_SCREEN_BIT");
- set_screen_dis_info_and_send_queue(true, true, false, false, 100);
- }
- else
- {
- ESP_LOGI(LOG_TAG, "UNEXPECTED EVENT");
- }
- }
- void print_yc_data(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[13 + 2]) << 8 | (buf->data_buf[14 + 2]);
- ESP_LOGI(LOG_TAG, "\n总长:0x%02x,crc:0x%02x,\
- \nmac:[%02x-%02x-%02x-%02x-%02x-%02x], \
- \n组内编号:%02x,\
- cmd:0x%02x,\
- 总包数:%02x, 当前包:%02x,\
- \n有效数据 len:0x%d",
- buf->data_len, buf->data_buf[4],
- buf->data_buf[5], buf->data_buf[6], buf->data_buf[7],
- buf->data_buf[8], buf->data_buf[9], buf->data_buf[10],
- buf->data_buf[11],
- buf->data_buf[12],
- buf->data_buf[13], buf->data_buf[14],
- len);
- for (int i = 0; i < buf->data_len; i++)
- {
- printf("%02x ", buf->data_buf[i]);
- if (i == (USE_DATA_LEN_INDEX + 1))
- {
- printf("data--->\n");
- }
- }
- printf("\n");
- }
- bool filtered_data(const LORA_DATA_T *buf)
- {
- if (buf->data_buf[11] != Machine_info.eflagID)
- {
- if (Machine_info.eflagID == 0)
- {
- ESP_LOGE(LOG_TAG, "err:未分配ID");
- }
- ESP_LOGE(LOG_TAG, "err:eflagID:%02x ,is not %02x",
- Machine_info.eflagID, buf->data_buf[11]);
- return false;
- }
- if ((buf->data_buf[5] != Machine_info.mac_addr[0]) ||
- (buf->data_buf[6] != Machine_info.mac_addr[1]) ||
- (buf->data_buf[7] != Machine_info.mac_addr[2]) ||
- (buf->data_buf[8] != Machine_info.mac_addr[3]) ||
- (buf->data_buf[9] != Machine_info.mac_addr[4]) ||
- (buf->data_buf[10] != Machine_info.mac_addr[5]))
- {
- ESP_LOGE(LOG_TAG, "err:mac addr:[%02x-%02x-%02x-%02x-%02x-%02x],\
- is not:[%02x-%02x-%02x-%02x-%02x-%02x]",
- 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],
- buf->data_buf[5], buf->data_buf[6], buf->data_buf[7],
- buf->data_buf[8], buf->data_buf[9], buf->data_buf[10]);
- return false;
- }
- return true;
- }
- static int time_synchronization_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.time_synchronization, buf->data_buf + DATA_START_LEN, len);
- // yc_timer_restart();
- // Machine_info
- Machine_info.year = (terminal_info.time_synchronization[0] << 8) | terminal_info.time_synchronization[1];
- Machine_info.month = terminal_info.time_synchronization[2];
- Machine_info.day = terminal_info.time_synchronization[3];
- Machine_info.hour = terminal_info.time_synchronization[4];
- Machine_info.min = terminal_info.time_synchronization[5];
- Machine_info.sec = terminal_info.time_synchronization[6];
- display_flag = 0;
- setRtcTime(Machine_info.year, Machine_info.month, Machine_info.day,
- Machine_info.hour, Machine_info.min, Machine_info.sec);
- printf("TIMER SYNC YEAR = %d,MONTH =%d,DAY=%d,HOUR=%d,MIN=%d,SEC =%d\r\n",
- Machine_info.year,
- Machine_info.month,
- Machine_info.day,
- Machine_info.hour,
- Machine_info.min,
- Machine_info.sec);
- getRtcTime(&Machine_info);
- // xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int terminal_name_func(const LORA_DATA_T *buf)
- {
- #define FONT_MAX_LEN 8
- uint8_t font_num = 0; // 获取当前的文字个数
- int i = 0;
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memset(terminal_info.terminal_name, 0, 32);
- memcpy(terminal_info.terminal_name, buf->data_buf + DATA_START_LEN, len);
- // memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
- memset(Machine_info.terminal_name, 0x00, sizeof(Machine_info.terminal_name)); // 清除终端名称信息
- font_num = len / 2;
- if (font_num <= FONT_MAX_LEN)
- {
- uincode2gbk((char *)terminal_info.terminal_name, len, (char *)Machine_info.terminal_name);
- }
- else
- {
- uincode2gbk((char *)terminal_info.terminal_name, FONT_MAX_LEN * 2, (char *)Machine_info.terminal_name);
- strcat((char *)Machine_info.terminal_name, "...");
- }
- // printf("unicode:");
- // for(i=0;i<len;i++)
- // {
- // printf("%02x ",terminal_info.terminal_name[i]);
- // }
- // printf("\n");
- // printf("gbk:");
- // for(i=0;i<len;i++)
- // {
- // printf("%02x ",Machine_info.terminal_name[i]);
- // }
- // printf("setting name %s",Machine_info.terminal_name);
- // printf("\n");
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int terminal_number_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.terminal_number, buf->data_buf + DATA_START_LEN, len);
- // memcpy(&Machine_info.terminal_number,&terminal_info.terminal_number,len);
- memset(Machine_info.terminal_number, 0x00, sizeof(Machine_info.terminal_number)); // 清除终端名称信息
- uincode2gbk((char *)terminal_info.terminal_number, len, (char *)Machine_info.terminal_number);
- // printf("unicode:");
- // for(int i=0;i<len;i++)
- // {
- // printf("%02x ",terminal_info.terminal_number[i]);
- // }
- // printf("\n");
- // printf("gbk:");
- // for(int i=0;i<len;i++)
- // {
- // printf("%02x ",Machine_info.terminal_number[i]);
- // }
- // printf("setting num %s", Machine_info.terminal_number);
- // printf("\n");
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int electric_quantity_func(const LORA_DATA_T *buf)
- {
- terminal_info.batt_percent = buf->data_buf[DATA_START_LEN];
- ESP_LOGI(LOG_TAG, "batt_percent : %d", terminal_info.batt_percent);
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int signal_strength_func(const LORA_DATA_T *buf)
- {
- // terminal_info.rssi = buf->data_buf[DATA_START_LEN];
- // ESP_LOGI(LOG_TAG,"rssi : %d",terminal_info.rssi);
- // Machine_info.rssi = terminal_info.rssi;
- // display_flag = RIGHT_FLAG;
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int screen_refresh_cycle_func(const LORA_DATA_T *buf)
- {
- terminal_info.refresh_cycle = buf->data_buf[DATA_START_LEN];
- ESP_LOGI(LOG_TAG, "refresh_cycle : %d", terminal_info.refresh_cycle);
- Machine_info.refresh_cycle = terminal_info.refresh_cycle;
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int screen_left_func(const LORA_DATA_T *buf)
- {
- terminal_info.left_epd = buf->data_buf[DATA_START_LEN];
- ESP_LOGI(LOG_TAG, "left_epd : %d", terminal_info.left_epd);
- Machine_info.left_display_mode = terminal_info.left_epd;
- // display_flag = LEFT_FLAG;
- xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int screen_right_func(const LORA_DATA_T *buf)
- {
- terminal_info.right_epd = buf->data_buf[DATA_START_LEN];
- Machine_info.right_display_mode = terminal_info.right_epd;
- ESP_LOGW(LOG_TAG, "-----right_epd : %d", terminal_info.right_epd);
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int rsponsible_person_change_func(const LORA_DATA_T *buf)
- {
- int len = ((buf->data_buf[USE_DATA_LEN_INDEX]) << 8) | ((buf->data_buf[USE_DATA_LEN_INDEX + 1]));
- // for(int i = 0;i<buf->data_len;i++)
- // {
- // printf("%02X",buf->data_buf[i]);
- // }
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- ESP_LOGW(LOG_TAG, "tmp name len = %d", len);
- memcpy(terminal_info.rsponsible_person_change, buf->data_buf + DATA_START_LEN, len);
- // ESP_LOGI(LOG_TAG,"mode:[%02x] len = %d",terminal_info.rsponsible_person_change[2],len);
- uint16_t tmp_number;
- uint8_t tmp_type;
- if (len == 3)
- {
- return msg_id;
- }
- else
- {
- char *tmp_name;
- tmp_name = (char *)malloc((len - 3 + 1));
- memset(tmp_name, 0x00, (len - 3 + 1));
- uincode2gbk((char *)terminal_info.rsponsible_person_change + 3, (len - 3), tmp_name);
- // printf("tmp_name = %s\r\n",tmp_name);
- tmp_name[len - 2] = '\0';
- // memcpy(tmp_name,(char*)terminal_info.rsponsible_person_change+3,len-3);
- #if 0
- for(int i=0;i<strlen(tmp_name);i++)
- {
- printf("%02x ",tmp_name[i]);
- }
- printf("\n%d\n",strlen(tmp_name));
- #endif
- tmp_number = (terminal_info.rsponsible_person_change[0] << 8) | (terminal_info.rsponsible_person_change[1] & 0xFF);
- tmp_type = terminal_info.rsponsible_person_change[2];
- #if 0
- ListNode *p = newListNode(tmp_number,tmp_type,tmp_name);
- insertNode_head(list_head,p);
- #else
- memset(Machine_info.person[tmp_type].string_name, 0x00, sizeof(Machine_info.person[tmp_type].string_name));
- person_add(tmp_type, tmp_number, tmp_name, Machine_info.person[tmp_type].string_name);
- ESP_LOGE(LOG_TAG, "add name = %s\r\n", Machine_info.person[tmp_type].string_name);
- #endif
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- // printf("msg_id =%X\r\n",msg_id);
- free(tmp_name);
- tmp_name = NULL;
- }
- return msg_id;
- }
- static int rsponsible_person_delete_func(const LORA_DATA_T *buf)
- {
- uint16_t tmp_number;
- uint8_t tmp_type;
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.rsponsible_person_delete, buf->data_buf + DATA_START_LEN, len);
- tmp_number = (terminal_info.rsponsible_person_delete[0] << 8) | (terminal_info.rsponsible_person_delete[1] & 0xFF);
- tmp_type = terminal_info.rsponsible_person_delete[2];
- #if 1
- int num = person_get_num(tmp_type); // 获取当前类型人员数量
- int is_exist = person_get_num_is_exist(tmp_type, tmp_number);
- if (is_exist)
- {
- memset(Machine_info.person[tmp_type].string_name, 0x00, sizeof(Machine_info.person[tmp_type].string_name));
- person_del(tmp_type, tmp_number, NULL, Machine_info.person[tmp_type].string_name);
- }
- #endif
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- // #define SET_BIT(flag,_bit) (flag |= (0x01 << _bit))
- // #define CLE_BIT(flag,_bit) (flag &= ~(0x01 << _bit))
- static int production_punch_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.production_punch, buf->data_buf + DATA_START_LEN, len);
- ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.production_punch, len);
- if (terminal_info.production_punch[1] == 0)
- {
- Machine_info.checkIn_set[terminal_info.production_punch[0]].checkIn_close = true; // 关闭该类型打卡
- }
- else
- {
- Machine_info.checkIn_set[terminal_info.production_punch[0]].checkIn_close = false; // 开启该类型打卡
- }
- ESP_LOGD(LOG_TAG, "打卡设置: 类型[%d] %s", terminal_info.production_punch[0], terminal_info.production_punch[1] ? "开启" : "关闭");
- if (len > 2) // 数据长度大于2 ,说明设置了别名
- {
- memset(Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name, 0, 10);
- // memcpy(Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name,terminal_info.production_punch+2,len-2);
- uincode2gbk((char *)terminal_info.production_punch + 2, len - 2, (char *)Machine_info.checkIn_set[terminal_info.production_punch[0]].other_name);
- }
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- return msg_id;
- }
- static int personnel_check_in_func(const LORA_DATA_T *buf)
- {
- // 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]);
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.personnel_check_in, buf->data_buf + DATA_START_LEN, len);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
- ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.personnel_check_in, len);
- int ID_crc = crc8(&terminal_info.personnel_check_in[2], len - 2, 0x5a, 0x51); // lora_crc8( , ,0x5a,0x51);暂定对人员id求crc
- 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);
- if (!(Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].checkIn_close)) // 开启打卡
- {
- if (terminal_info.personnel_check_in[1] == 0x00) // 打卡签入
- {
- // Machine_info.personnel_check_in[terminal_info.personnel_check_in[0]]++;
- // Node *find = findNode(Send_list,msg_id);
- Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].number++;
- Node *find = findNode(clockIn_list, ID_crc);
- // printList(clockIn_list);
- if (find == NULL)
- {
- Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].real_number++;
- clockIn_list = postInsert(clockIn_list, ID_crc, terminal_info.personnel_check_in[0], (&terminal_info.personnel_check_in) + 2, len - 2);
- }
- else
- {
- ESP_LOGE(LOG_TAG, "该人员已存在");
- }
- }
- else if (terminal_info.personnel_check_in[1] == 0x01) // check out
- {
- Node *find = findNode(clockIn_list, ID_crc);
- if (find != NULL)
- {
- clockIn_list = deleteNode(clockIn_list, ID_crc);
- Machine_info.checkIn_set[terminal_info.personnel_check_in[0]].real_number--;
- }
- }
- else
- {
- ESP_LOGE(LOG_TAG, "err:not in or out");
- }
- }
- // printList(clockIn_list);
- return msg_id;
- }
- uint8_t change_btn_reversal(uint8_t reversal_btn);
- static void set_button_anotherName(void) // cmd 0x0d操作
- {
- uint8_t tmp = change_btn_reversal(terminal_info.status_setting[0]);
- if ((terminal_info.status_setting[0] == 0) && (terminal_info.status_setting[1] == 0))
- { //[0]:状态 [1]:开关 ————>运行不能关闭,这两位都为0,说明没有收到信息
- // ESP_LOGW(LOG_TAG,"默认按键");
- return; // 没有cmd[0x0d]的lora信息
- }
- if (terminal_info.status_setting[1] == 0x00) // close
- {
- Machine_info.btn_dis_flag[tmp - 1] = false;
- ESP_LOGD(LOG_TAG, "按键状态关闭,清空按键文字 状态%d", tmp);
- switch (terminal_info.status_setting[0]) // 按键状态关闭,清空按键文字
- {
- case 0x00:
- memset(Machine_info.btn_operation, 0, 6);
- break;
- case 0x01:
- memset(Machine_info.btn_waitMaterials_info, 0, 6);
- break;
- case 0x02:
- memset(Machine_info.btn_safeKeep_info, 0, 6);
- break;
- case 0x03:
- memset(Machine_info.btn_breakDown_info, 0, 6);
- break;
- case 0x04:
- memset(Machine_info.btn_upKeep_info, 0, 6);
- break;
- case 0x05:
- memset(Machine_info.btn_shutDown_info, 0, 6);
- break;
- default:
- ESP_LOGD(LOG_TAG, "err:按键状态%d", __LINE__);
- break;
- }
- }
- else if (terminal_info.status_setting[1] == 0x01) // open
- {
- Machine_info.btn_dis_flag[tmp - 1] = true;
- // 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]);
- if ((terminal_info.status_setting[2] == 0x00) && (terminal_info.status_setting[3] == 0x00))
- { // 别名为两个字,这判断第一个字为空,为空就赋值初始化的按键信息
- ESP_LOGD(LOG_TAG, "button is open[other name is NULL] %d", terminal_info.status_setting[0]);
- switch (terminal_info.status_setting[0])
- {
- case 0x00: // 运行
- printf("clear btn_operation display\r\n");
- memcpy(Machine_info.btn_operation, str_operation, 6);
- break;
- case 0x01: // 待料
- printf("clear btn_waitMaterials_info display\r\n");
- memcpy(Machine_info.btn_waitMaterials_info, str_waitMaterials, 6);
- break;
- case 0x02: // 封存
- printf("clear btn_safeKeep_info display\r\n");
- memcpy(Machine_info.btn_safeKeep_info, str_safeKeep, 6);
- break;
- case 0x03: // 故障
- printf("clear btn_breakDown_info display\r\n");
- memcpy(Machine_info.btn_breakDown_info, str_breakDown, 6);
- break;
- case 0x04: // 保养
- printf("clear btn_upKeep_info display\r\n");
- memcpy(Machine_info.btn_upKeep_info, str_upKeep, 6);
- break;
- case 0x05: // 停机
- printf("clear btn_shutDown_info display\r\n");
- memcpy(Machine_info.btn_shutDown_info, str_shutDown, 6);
- break;
- default:
- printf("other btn_operation display\r\n");
- ESP_LOGE(LOG_TAG, "err:按键状态%d", __LINE__);
- break;
- }
- }
- else
- {
- // ESP_LOGW(LOG_TAG,"button is open[other name] %d",terminal_info.status_setting[0]);
- switch (terminal_info.status_setting[0])
- {
- case 0x00:
- memset(Machine_info.btn_operation, 0, 6);
- uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_operation);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_operation,6);
- break;
- case 0x01:
- memset(Machine_info.btn_waitMaterials_info, 0, 6);
- uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_waitMaterials_info);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_waitMaterials_info,6);
- break;
- case 0x02:
- memset(Machine_info.btn_safeKeep_info, 0, 6);
- uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_safeKeep_info);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_safeKeep_info,6);
- break;
- case 0x03:
- memset(Machine_info.btn_breakDown_info, 0, 6);
- uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_breakDown_info);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_breakDown_info,6);
- break;
- case 0x04:
- memset(Machine_info.btn_upKeep_info, 0, 6);
- uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_upKeep_info);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_upKeep_info,6);
- break;
- case 0x05:
- memset(Machine_info.btn_shutDown_info, 0, 6);
- uincode2gbk((char *)terminal_info.status_setting + 2, 4, (char *)Machine_info.btn_shutDown_info);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,&Machine_info.btn_shutDown_info,6);
- break;
- default:
- ESP_LOGE(LOG_TAG, "err:按键状态%d", __LINE__);
- break;
- }
- }
- }
- else
- {
- ESP_LOGE(LOG_TAG, "not open or close");
- }
- }
- uint8_t change_btn(uint8_t btn);
- static int status_setting_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memset(terminal_info.status_setting, 0x00, sizeof(terminal_info.status_setting));
- memcpy(terminal_info.status_setting, buf->data_buf + DATA_START_LEN, len); // 别名
- // ESP_LOGE(LOG_TAG,"len = %d ",len);
- ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.status_setting, 10);
- set_button_anotherName();
- // 当前按键状态关闭了
- if ((change_btn(Machine_info.left_state) == terminal_info.status_setting[0]) && (Machine_info.btn_dis_flag[Machine_info.left_state - 1] == false))
- {
- ESP_LOGE(LOG_TAG, "按键发送");
- send_button_key_queue(STATE_OPERATION);
- }
- else
- {
- // display_flag = LEFT_FLAG;
- xEventGroupSetBits(screen_event, LEFT_SCREEN_BIT);
- }
- ESP_LOGW(LOG_TAG, "按键 :%s %s %s %s %s %s",
- Machine_info.btn_dis_flag[0] ? "开启" : "关闭",
- Machine_info.btn_dis_flag[1] ? "开启" : "关闭",
- Machine_info.btn_dis_flag[2] ? "开启" : "关闭",
- Machine_info.btn_dis_flag[3] ? "开启" : "关闭",
- Machine_info.btn_dis_flag[4] ? "开启" : "关闭",
- Machine_info.btn_dis_flag[5] ? "开启" : "关闭");
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- return msg_id;
- }
- static int status_duration_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.status_duration, buf->data_buf + DATA_START_LEN, len);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int update_and_duration_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.update_and_duration, buf->data_buf + DATA_START_LEN, len);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int announcement_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.announcement, buf->data_buf + DATA_START_LEN, len); // 拷贝公告
- ESP_LOG_BUFFER_HEX(LOG_TAG, terminal_info.announcement, len);
- ESP_LOGW(LOG_TAG, "设置公告");
- // memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
- memset(Machine_info.announcement, 0x00, sizeof(Machine_info.announcement)); // 清除终端公告信息
- uincode2gbk((char *)terminal_info.announcement, len, (char *)Machine_info.announcement);
- // printf("unicode:");
- // for(i=0;i<len;i++)
- // {
- // printf("%02x ",terminal_info.terminal_name[i]);
- // }
- // printf("\n");
- // printf("gbk:");
- // for(i=0;i<len;i++)
- // {
- // printf("%02x ",Machine_info.terminal_name[i]);
- // }
- // printf("setting gong gao %s",Machine_info.announcement);
- // printf("\n");
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- void set_statisticsData(uint8_t *data)
- {
- // memset(&Machine_info.num_goodProducts,0,32);
- // memset(&Machine_info.num_badProducts,0,32);
- #define __DATA_SIZE 24
- for (int i = 0; i < 8; i++)
- {
- 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]);
- 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]);
- Machine_info.scale_UR_int[i] = (data[8 + i * __DATA_SIZE]);
- Machine_info.scale_UR_dec[i] = (data[9 + i * __DATA_SIZE]);
- Machine_info.scale_YR_int[i] = (data[10 + i * __DATA_SIZE]);
- Machine_info.scale_YR_dec[i] = (data[11 + i * __DATA_SIZE]);
- Machine_info.num_manHour[i] = (data[12 + i * __DATA_SIZE] << 8) | (data[13 + i * __DATA_SIZE]);
- Machine_info.num_people[i] = (data[14 + i * __DATA_SIZE] << 8) | (data[15 + i * __DATA_SIZE]);
- Machine_info.num_production[i] = (data[16 + i * __DATA_SIZE] << 8) | (data[17 + i * __DATA_SIZE]);
- Machine_info.num_repair[i] = (data[18 + i * __DATA_SIZE] << 8) | (data[19 + i * __DATA_SIZE]);
- Machine_info.num_inspection[i] = (data[20 + i * __DATA_SIZE] << 8) | (data[21 + i * __DATA_SIZE]);
- Machine_info.num_upkeep[i] = (data[22 + i * __DATA_SIZE] << 8) | (data[23 + i * __DATA_SIZE]);
- // ESP_LOGW(LOG_TAG,"good:[%ld] bad[%ld]",Machine_info.num_goodProducts[i],Machine_info.num_badProducts[i]);
- }
- }
- static int capacity_statistics_func(const LORA_DATA_T *buf)
- {
- #if 1
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.capacity_statistics, buf->data_buf + DATA_START_LEN, len);
- #endif
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- set_statisticsData(terminal_info.capacity_statistics);
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- return msg_id;
- }
- static int responsible_person_synchronize_info_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.responsible_person_synchronize_info, buf->data_buf + DATA_START_LEN, len);
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- return msg_id;
- }
- static int status_led_func(const LORA_DATA_T *buf)
- {
- terminal_info.states_led = buf->data_buf[DATA_START_LEN];
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int logo_led_func(const LORA_DATA_T *buf)
- {
- terminal_info.logo_led = buf->data_buf[DATA_START_LEN];
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int battery_temperature_func(const LORA_DATA_T *buf)
- {
- terminal_info.batt_temperature[0] = buf->data_buf[DATA_START_LEN];
- terminal_info.batt_temperature[1] = buf->data_buf[DATA_START_LEN + 1];
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- static int station_name_func(const LORA_DATA_T *buf)
- {
- #define FONT_MAX_LEN 8
- uint8_t font_num = 0; // 获取当前的文字个数
- int i = 0;
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memset(terminal_info.station_name, 0, 32);
- memcpy(terminal_info.station_name, buf->data_buf + DATA_START_LEN, len);
- // memcpy(&Machine_info.terminal_name,&terminal_info.terminal_name,len);
- memset(Machine_info.station_name, 0x00, sizeof(Machine_info.station_name)); // 清除终端名称信息
- font_num = len / 2;
- if (font_num <= FONT_MAX_LEN)
- {
- uincode2gbk((char *)terminal_info.station_name, len, (char *)Machine_info.station_name);
- }
- else
- {
- uincode2gbk((char *)terminal_info.station_name, FONT_MAX_LEN * 2, (char *)Machine_info.station_name);
- strcat((char *)Machine_info.station_name, "...");
- }
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- return msg_id;
- }
- static int station_number_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.station_number, buf->data_buf + DATA_START_LEN, len);
- memset(Machine_info.station_number, 0x00, sizeof(Machine_info.station_number));
- uincode2gbk((char *)terminal_info.station_number, len, (char *)Machine_info.station_number);
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- return msg_id;
- }
- static int responsible_person_setup_func(const LORA_DATA_T *buf)
- {
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- ESP_LOG_BUFFER_HEX(LOG_TAG, buf->data_buf + DATA_START_LEN, len);
- ESP_LOGE(LOG_TAG, "设置责任人 类型[%d]%s", buf->data_buf[DATA_START_LEN], buf->data_buf[DATA_START_LEN + 1] ? "开启" : "关闭");
- if (buf->data_buf[DATA_START_LEN + 1] == 0x01) // 开
- {
- Machine_info.person[buf->data_buf[DATA_START_LEN]].Charge_close = false;
- memset(Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name, 0, 16);
- uincode2gbk(buf->data_buf + DATA_START_LEN + 2, len - 2, (char *)Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name);
- // ESP_LOG_BUFFER_HEX(LOG_TAG,Machine_info.person[buf->data_buf[DATA_START_LEN]].other_name,len);
- }
- else if (buf->data_buf[DATA_START_LEN + 1] == 0x00) // 关
- {
- Machine_info.person[buf->data_buf[DATA_START_LEN]].Charge_close = true;
- }
- 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,
- Machine_info.person[3].Charge_close, Machine_info.person[4].Charge_close);
- // display_flag = RIGHT_FLAG;
- xEventGroupSetBits(screen_event, RIGHT_SCREEN_BIT);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- return msg_id;
- }
- static int system_bulletin_func(const LORA_DATA_T *buf)
- {
- ESP_LOGE(LOG_TAG, "system_bulletin_func");
- int len = (buf->data_buf[USE_DATA_LEN_INDEX]) << 8 | (buf->data_buf[USE_DATA_LEN_INDEX + 1]);
- memcpy(terminal_info.systemMessage, buf->data_buf + DATA_START_LEN, len);
- memset(Machine_info.systemMessage, 0x00, sizeof(Machine_info.systemMessage));
- uincode2gbk((char *)terminal_info.systemMessage, len, (char *)Machine_info.systemMessage);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- return msg_id;
- }
- static int hardware_update_func(const LORA_DATA_T *buf)
- {
- // printf("msg h = %02X,msg l = %02X\r\n",buf->data_buf[13],buf->data_buf[14]);
- int msg_id = (buf->data_buf[13] >> 8) | buf->data_buf[14]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- return msg_id;
- }
- /*硬件回复命令ACK*/ /*长度 返回result结果及长度*/
- /*********************************************************************************
- * function : reply_ack_func
- * Description : 回复ACK cmd 指定命令
- * Input : cmd 命令 msg_id 当前的ID号 用于网关删除自身的已经接受到ACK 的数据 result 返回当前的数据
- * Output :
- * Author : 祁鑫 Data : 2023 8.12
- **********************************************************************************/
- int reply_ack_func(int cmd, int msg_id, bool is_send, uint8_t *result)
- {
- uint8_t strlorabuf[50] = {0};
- uint8_t mac[6] = {0};
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51;
- strlorabuf[2] = 0x00; // crc
- // int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
- for (int i = 0; i < 6; i++)
- {
- strlorabuf[3 + i] = Machine_info.gateway_mac[i]; // 网关的mac地址
- }
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = cmd; // 命令
- strlorabuf[11] = (msg_id >> 8) & 0xff; // 总包
- strlorabuf[12] = msg_id & 0xff; // 当前包
- strlorabuf[13] = 0x00; // 当前长度
- strlorabuf[14] = PROTOCOL_RSPONSIBLE_ACK; // ACK命令字段
- #if 0
- //设备cid长度
- memcpy(&strlorabuf[15],cid,cid_len); //拷贝cid
- strlorabuf[13] = cid_len+1; //重新设置当前当前长度
- #endif
- int crc = 0; // 计算crc
- for (int i = 0; i < sizeof(strlorabuf); i++)
- {
- crc += strlorabuf[i];
- }
- strlorabuf[2] = crc;
- if (result != NULL)
- {
- memcpy(result, strlorabuf, 15);
- }
- if (is_send)
- {
- ESP_LOG_BUFFER_HEX("send ack to gateway", strlorabuf, 15);
- lora_send_data((char *)strlorabuf, 15);
- uart_wait_tx_idle_polling(UART_NUM_1);
- }
- return 15;
- }
- /*硬件配网回复*/ // 参数cid cid长度 返回result 结果及长度
- int reply_gwpair_ack_func(uint8_t *cid, uint8_t cid_len, uint8_t *result)
- {
- uint8_t strlorabuf[50] = {0};
- uint8_t mac[6] = {0};
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51;
- strlorabuf[2] = 0x00; // crc
- // int byteArrayLength = hexStringToByteArray((char *)C_MAC, (unsigned char *)mac); //HEX字符串转HEX数组
- for (int i = 0; i < 6; i++)
- {
- strlorabuf[3 + i] = Machine_info.gateway_mac[i]; // 网关的mac地址
- }
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x80; // 配对命令
- strlorabuf[11] = 0x01; // 总包
- strlorabuf[12] = 0x01; // 当前包
- strlorabuf[13] = 0x00; // 当前长度
- strlorabuf[14] = 0x01; // 请求成功
- // 设备cid长度
- memcpy(&strlorabuf[15], cid, cid_len); // 拷贝cid
- strlorabuf[13] = cid_len + 1; // 重新设置当前当前长度
- int crc = 0; // 计算crc
- for (int i = 0; i < sizeof(strlorabuf); i++)
- {
- crc += strlorabuf[i];
- }
- strlorabuf[2] = crc;
- memcpy(result, strlorabuf, 14 + cid_len + 1);
- return 14 + cid_len + 1;
- }
- /*硬件配网*/
- static void hardware_gwpair_func(const LORA_DATA_T *buf)
- {
- if (buf->data_len != 35)
- {
- ESP_LOGE(LOG_TAG, "err:hardware_gwpair_func");
- return;
- }
- uint8_t child_mac[6] = {0}; // 子设备的mac地址
- uint8_t gateway_mac[6] = {0}; // 网关的组内编号
- int child_num = 0;
- int gateway_num = 0;
- #if 0
- for(int i = 0;i<buf->data_len;i++)
- {
- printf("%02X",buf->data_buf[i]);
- }
- #endif
- uint64_t time = 0x000000000000LL;
- // printf("1\r\n");
- memcpy(child_mac, &buf->data_buf[3], 6);
- // printf("2\r\n");
- gateway_num = buf->data_buf[9];
- // printf("3\r\n");
- memcpy(gateway_mac, &buf->data_buf[14], 6);
- // printf("4\r\n");
- child_num = buf->data_buf[20];
- // printf("5\r\n");
- // printf("\r\n");
- // for(int i = 21;i<27;i++)
- // {
- // printf("%02X",buf->data_buf[i]);
- // }
- #if 0
- #include <inttypes.h>
- time|=(buf->data_buf[26]<<0);
- //printf("time = %" PRIX64 "\r\n",time);
- time|=(buf->data_buf[25]<<8);
- //printf("time = %" PRIX64 "\r\n",time);
- time|=(buf->data_buf[24]<<16);
- //printf("time = %" PRIX64 "\r\n",time);
- time|=(buf->data_buf[23]<<24);
- //printf("time = %" PRIX64 "\r\n",time);
- #if 0
- uint64_t temp = 0;
- temp |=(buf->data_buf[22]<<32);
- temp |=(buf->data_buf[21]<<40);
- printf("temp = %" PRIX64 "\r\n",temp);
- #endif
- #if 1
- int64_t temp = 0;
- temp = buf->data_buf[22];
- time|=(temp<<32);//(buf->data_buf[22]<<32);
- #else
- time|=temp;
- #endif
- //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
- #if 0
- time|=(buf->data_buf[21]<<40);
- #else
- temp = 0;
- temp = buf->data_buf[21];
- time|=(temp<<40);//(buf->data_buf[22]<<32);
- //time|=(buf->data_buf[21]<<40);
- #endif
- //printf("time = %" PRIX64 "\r\n",(uint64_t)time);
- //uint64_t value = 0x018B1E3D24AC;
- //printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
- //printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
- #if 0
- time=(buf->data_buf[21]<<40)+
- (buf->data_buf[22]<<32)+
- (buf->data_buf[23]<<24)+
- (buf->data_buf[24]<<16)+
- (buf->data_buf[25]<<8) +
- (buf->data_buf[26]<<0);
- #endif
- #else
- Machine_info.lora_new_channel = buf->data_buf[21];
- printf("Machine_info.lora_new_channel =%d\r\n", Machine_info.lora_new_channel);
- memcpy(Machine_info.timestamp, &buf->data_buf[22], 13);
- printf("Machine_info.timestamp =%s\r\n", Machine_info.timestamp);
- int timezone_offset = 8; // 中国时区偏移为UTC+8,你可以根据需要设置不同的时区偏移
- Machine_info.time_offset = timezone_offset;
- timestamp_to_local_time(Machine_info.timestamp,
- Machine_info.time_offset,
- &Machine_info.year,
- &Machine_info.month,
- &Machine_info.day,
- &Machine_info.hour,
- &Machine_info.min,
- &Machine_info.sec);
- setRtcTime(Machine_info.year, Machine_info.month, Machine_info.day,
- Machine_info.hour, Machine_info.min, Machine_info.sec);
- // 更新当前状态时间
- Machine_info.current_button.Year = Machine_info.year;
- Machine_info.current_button.Month = Machine_info.month;
- Machine_info.current_button.Day = Machine_info.day;
- Machine_info.current_button.Hour = Machine_info.hour;
- Machine_info.current_button.Minute = Machine_info.min;
- Machine_info.current_button.Second = Machine_info.sec;
- Machine_info.last_button.Year = Machine_info.year;
- Machine_info.last_button.Month = Machine_info.month;
- Machine_info.last_button.Day = Machine_info.day;
- Machine_info.last_button.Hour = Machine_info.hour;
- Machine_info.last_button.Minute = Machine_info.min;
- Machine_info.last_button.Second = Machine_info.sec;
- #endif
- printf("gateway mac = ");
- for (int i = 0; i < 6; i++)
- printf("%02X", gateway_mac[i]);
- printf("\r\n");
- printf("gateway num = %d\r\n", gateway_num);
- printf("child mac = ");
- for (int i = 0; i < 6; i++)
- printf("%02X", child_mac[i]);
- printf("\r\n");
- printf("child_num num = %d\r\n", child_num);
- printf("time = %llu\r\n", time);
- printf("time = %llx\r\n", time);
- //(3000 - (480 - 1*100))
- #include "user_sleep.h"
- #include "esp_sleep.h"
- // 配网完成设置休眠唤醒时间
- if (Machine_info.eflagID == 0xff)
- {
- #if 0
- printf("sleep time = %d ms\r\n",(TIMER_WAKEUP_TIME_US*child_num)/1000);
- esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*child_num); //配置当前休眠的唤醒时间
- #else
- esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US); // 配置当前休眠的唤醒时间
- #endif
- }
- Machine_info.eflagID = child_num; // 保存当前的子设备组内编号
- memcpy(Machine_info.gateway_mac, gateway_mac, 6);
- // 配网成功 回复 ACK 设置新的信道
- Machine_info.paired = 1; // 已配对
- int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除
- printf("msg_id =%X\r\n", msg_id);
- reply_ack_func(PROTOCOL_HARDWARE_GWPAIRED, msg_id, true, NULL); // 回复ack
- // vTaskDelay(200 / portTICK_PERIOD_MS);
- // //修改信道前修改设备ID 及 设备ID
- // dymatic_change_device_id(child_num+1);
- // dymatic_change_dst_device_id(0x00000001);
- // //切换信道
- // dymatic_change_chanel(Machine_info.lora_new_channel); //切换信道
- // lora_get_rssi();
- vTaskDelay(200 / portTICK_PERIOD_MS);
- Machine_info.rssi = set_lora(Machine_info.lora_new_channel, Machine_info.eflagID);
- // f_send_get_time();
- f_send_version();
- f_send_reply_status(0x01); // 回复在线状态
- f_send_update_status();
- f_send_battary_vaule(Machine_info.batt_precent);
- #if 0
- f_send_get_chart_data();
- #endif
- // xQueueReset(screen_queue);
- // 重置快刷
- extern bool is_paired_sleep;
- is_paired_sleep = true;
- ESP_LOGW(LOG_TAG, "3is_paired_sleep = true");
- Machine_info.left_current_Quick_refresh_time = 0;
- Machine_info.right_current_Quick_refresh_time = 0;
- set_screen_dis_info_and_send_queue(false, true, false, false, 100);
- set_screen_dis_info_and_send_queue(true, false, false, false, 100);
- // printf("start timer id = %d,%d\r\n",Machine_info.eflagID,Machine_info.eflagID * TIMER_CAN_SEND_TIME);
- // Already_send_timer_start(Machine_info.eflagID * TIMER_CAN_SEND_TIME);
- }
- /*硬件取消配网*/
- static void hardware_ungwpair_func(const LORA_DATA_T *buf)
- {
- uint8_t child_mac[6] = {0}; // 子设备的mac地址
- uint8_t gateway_mac[6] = {0}; // 网关的组内编号
- int child_num = 0;
- int gateway_num = 0;
- uint64_t time = 0x000000000000LL;
- memcpy(child_mac, &buf->data_buf[3], 6);
- gateway_num = buf->data_buf[9];
- memcpy(gateway_mac, &buf->data_buf[14], 6);
- child_num = buf->data_buf[20];
- #include <inttypes.h>
- time |= (buf->data_buf[26] << 0);
- time |= (buf->data_buf[25] << 8);
- time |= (buf->data_buf[24] << 16);
- time |= (buf->data_buf[23] << 24);
- #if 1
- int64_t temp = 0;
- temp = buf->data_buf[22];
- time |= (temp << 32); //(buf->data_buf[22]<<32);
- #else
- time |= temp;
- #endif
- // printf("time = %" PRIX64 "\r\n",(uint64_t)time);
- #if 0
- time|=(buf->data_buf[21]<<40);
- #else
- temp = 0;
- temp = buf->data_buf[21];
- time |= (temp << 40); //(buf->data_buf[22]<<32);
- // time|=(buf->data_buf[21]<<40);
- #endif
- // printf("time = %" PRIX64 "\r\n",(uint64_t)time);
- // uint64_t value = 0x018B1E3D24AC;
- // printf("Value as a 64-bit integer: %" PRIu64 "\n", value);
- // printf("Value as a 64-bit integer: %" PRIX64 "\n", value);
- printf("gateway mac = ");
- for (int i = 0; i < 6; i++)
- printf("%02X", gateway_mac[i]);
- printf("\r\n");
- printf("gateway num = %d\r\n", gateway_num);
- printf("child mac = ");
- for (int i = 0; i < 6; i++)
- printf("%02X", child_mac[i]);
- printf("\r\n");
- printf("child_num num = %d\r\n", child_num);
- printf("time = %llu\r\n", time);
- printf("time = %llx\r\n", time);
- //(3000 - (480 - 1*100))
- #include "user_sleep.h"
- #include "esp_sleep.h"
- memcpy(Machine_info.gateway_mac, gateway_mac, 6);
- // 取消配网成功 回复 ACK 设置新的信道
- int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除
- // printf("msg_id =%X\r\n",msg_id);
- reply_ack_func(PROTOCOL_HARDWARE_UNGWPAIRED, msg_id, true, NULL); // 回复ack
- // #include "user_sleep.h"
- // #include "esp_sleep.h"
- extern void sleep_timer_stop();
- extern void Already_send_timer_stop();
- sleep_timer_stop();
- Already_send_timer_stop();
- // 修改信道前修改设备ID 及 设备ID
- dymatic_change_device_id(0x00000001);
- dymatic_change_dst_device_id(0xFFFFFFFF);
- // 切换信道
- reset_default(true, 1);
- vTaskDelay(300 / portTICK_PERIOD_MS);
- Machine_info.lora_new_channel = LORA_CHANENL;
- // dymatic_change_chanel(Machine_info.lora_factory_channel); //切换信道
- reset_lora(LORA_CHANENL);
- Machine_info.paired = 0; // 解除配对
- extern void power_sleep_timer_start(void);
- power_sleep_timer_start();
- }
- /*网关收到终端发送的数据 ACK 终端收到用于删除当前的msgid的数据*/
- static void hardware_ack_delete_msg_id(const LORA_DATA_T *buf)
- {
- int msg_id = (buf->data_buf[11] << 8) | buf->data_buf[12]; // 获取当前消息id 用于删除当前链表的数据
- ESP_LOG_BUFFER_HEX("ACK", buf->data_buf, buf->data_len);
- // int current = countNodes(Send_list); //获取当前节点数据
- Node *find = findNode(Send_list, msg_id);
- if (find != NULL)
- {
- ESP_LOGE(LOG_TAG, "find msg_id : %d", msg_id);
- // printList(Send_list);
- for (int i = 0; i < LORA_INFO_END; i++)
- {
- if (lora_pool[i].num == msg_id)
- {
- lora_pool[i].flag = false;
- Send_list = deleteNode(Send_list, msg_id);
- ESP_LOGE(LOG_TAG, "delete msg_id %d", msg_id);
- }
- }
- // printf("delete list node msg_id = %d\r\n",msg_id);
- // printf("delete list node after = %d\r\n",msg_id);
- // printList(Send_list);
- }
- else
- {
- ESP_LOGE(LOG_TAG, "delete fail");
- printList(Send_list);
- }
- // display_flag = 0;
- }
- void business_logic_func(const LORA_DATA_T *buf, uint8_t cmd_index, int msg)
- {
- // int cmd = 0; // 当前的命令
- int msg_id = 0; // 消息的id
- // bool is_rsp_ack = false;
- #if 0
- printf("cmd = %02X\r\n",buf->data_buf[cmd_index]);
- #endif
- // cmd = buf->data_buf[cmd_index];
- switch (buf->data_buf[cmd_index])
- {
- case PROTOCOL_TIME_SYNCHRONIZATION:
- msg_id = time_synchronization_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_TERMINAL_NAME:
- msg_id = terminal_name_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_TERMINAL_NUMBER:
- msg_id = terminal_number_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_ELECTRIC_QUANTITY:
- msg_id = electric_quantity_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_SIGNAL_STRENGTH:
- msg_id = signal_strength_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_SCREEN_REFRESH_CYCLE:
- msg_id = screen_refresh_cycle_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_SCREEN_LEFT:
- msg_id = screen_left_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_SCREEN_RIGHT:
- msg_id = screen_right_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_RSPONSIBLE_PERSON_CHANGE:
- msg_id = rsponsible_person_change_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_RSPONSIBLE_PERSON_DELETE:
- msg_id = rsponsible_person_delete_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_PRODUCTION_PUNCH_CARD:
- msg_id = production_punch_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_PERSONNEL_CHECK_IN:
- msg_id = personnel_check_in_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_STATUS_SETTING:
- msg_id = status_setting_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_STATUS_DURATION:
- msg_id = status_duration_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_UPDATE_AND_DURATION:
- msg_id = update_and_duration_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_ANNOUNCEMENT:
- msg_id = announcement_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_CAPACITY_STATISTICS:
- msg_id = capacity_statistics_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_RESPONSIBLE_PERSON_SYNCHRONIZE_INFO:
- msg_id = responsible_person_synchronize_info_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_STATUS_LED:
- msg_id = status_led_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_LOG_LED:
- msg_id = logo_led_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_BATTERY_TEMPERATURE:
- msg_id = battery_temperature_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_STATION_NAME:
- msg_id = station_name_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_STATION_NUMBER:
- msg_id = station_number_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_RESPONSIBLE_PERSON_SETUP:
- msg_id = responsible_person_setup_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_SYSTEM_BULLETIN:
- msg_id = system_bulletin_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_HARDWARE_UPDATE:
- msg_id = hardware_update_func(buf);
- // is_rsp_ack = true;
- break;
- case PROTOCOL_HARDWARE_GWPAIRED: // 配网 暂时单独发送ack
- reset_default(false, 1);
- hardware_gwpair_func(buf);
- // Paint_leftScreen_main_quick(&Machine_info);
- // Paint_rightScreen_main_quick(&Machine_info);
- break;
- case PROTOCOL_HARDWARE_UNGWPAIRED: // 取消配网
- printf("pair ungwpaired\r\n");
- hardware_ungwpair_func(buf);
- break;
- case PROTOCOL_RSPONSIBLE_ACK:
- // printf("receive gateway ack delete data in list\r\n");
- hardware_ack_delete_msg_id(buf);
- break;
- default:
- ESP_LOGE(LOG_TAG, "ERR:CMD = 0x%02x", buf->data_buf[cmd_index]);
- break;
- }
- #if USER_NOT_SLEEP_ENABLE
- send_can_I_receive(); // 发送可以接受的命令
- #endif
- }
- void uincode2gbk(char *str, uint16_t len, char *out_buffer)
- {
- char strtmp[1024];
- char str1[3];
- unsigned int iunicode, i;
- unsigned int igbk;
- memset(strtmp, 0, 1024);
- int index = 0;
- i = 0;
- int length = len;
- while (length)
- {
- if ((str[i] == 0) && (str[i + 1]) < 0x80) // ASCII 码 *text是ASCII码
- {
- str1[0] = (str[i + 1]) & 0xff;
- memcpy(&strtmp[index], str1, 1);
- index += 1;
- }
- else
- {
- iunicode = (str[i] << 8) + str[i + 1];
- igbk = U2G(iunicode);
- str1[0] = (igbk >> 8) & 0xff;
- str1[1] = igbk & 0xff;
- memcpy(&strtmp[index], str1, 2);
- index += 2;
- }
- length -= 2;
- i += 2;
- }
- // ESP_LOGW(LOG_TAG,"index:%d",index);
- memcpy(out_buffer, strtmp, index);
- }
- // 计算数组元素的总和
- uint32_t sumArray(uint8_t array[], int size)
- {
- uint32_t sum = 0;
- for (int i = 0; i < size; i++)
- {
- sum += array[i];
- }
- return sum;
- }
- void mac_to_hex(char *out_string, char *in_string)
- {
- sprintf(out_string, "%02X:%02X:%02X:%02X:%02X:%02X",
- in_string[0],
- in_string[1],
- in_string[2],
- in_string[3],
- in_string[4],
- in_string[5]);
- // ESP_LOG_BUFFER_HEX("--->",out_string,17);
- }
- void qrcode_protocol_create(char *result, Machine_info_t info)
- {
- // char str_https[] = "https://www.yeechart.com/antnest_download.php?data=";
- char str_https[] = "https://www.yeechart.com/yeechart_download.php?data=";
- Qrcode_protocol_t Qcode;
- char qrcode_protocol[1024] = {0};
- Qcode.head[0] = 0x5a;
- Qcode.head[1] = 0x51;
- Qcode.CRC = 0x00;
- Qcode.mac.cmd = 0x03;
- Qcode.mac.len = 0x11;
- esp_read_mac((uint8_t *)mac_addr, ESP_MAC_WIFI_STA);
- mac_to_hex((char *)Qcode.mac.mac_addr, (char *)mac_addr);
- // strcpy((char *)Qcode.mac.mac_addr,(char*)mac_addr);
- Qcode.client.cmd = 0X04;
- #if 1 // 硬件设备id (qx 设置)
- strcpy((char *)Qcode.client.client_id, (char *)info.cid);
- Qcode.client.len = strlen((char *)Qcode.client.client_id);
- #else // 假硬件设备id
- strcpy((char *)Qcode.client.client_id, "whc_test");
- Qcode.client.len = strlen((char *)Qcode.client.client_id);
- #endif
- Qcode.product.cmd = 0x05;
- strcpy((char *)Qcode.product.product, "020101");
- Qcode.product.len = strlen((char *)Qcode.product.product);
- Qcode.time.cmd = 0x61;
- Qcode.time.len = 0x0E;
- #if 1
- Qcode.time.Year = info.year;
- Qcode.time.Month = info.month;
- Qcode.time.Day = info.day;
- Qcode.time.Hour = info.hour;
- Qcode.time.Minute = info.min;
- Qcode.time.Second = info.sec;
- #else
- Qcode.time.Year = 2023;
- Qcode.time.Month = 9;
- Qcode.time.Day = 19;
- Qcode.time.Hour = 19;
- Qcode.time.Minute = 30;
- Qcode.time.Second = 20;
- #endif
- int time_index = 0;
- sprintf((char *)(Qcode.time.time + time_index * 2), "%02X%02X", Qcode.time.Year >> 8, Qcode.time.Year & 0xff);
- time_index += 2;
- sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Month);
- time_index += 1;
- sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Day);
- time_index += 1;
- sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Hour);
- time_index += 1;
- sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Minute);
- time_index += 1;
- sprintf((char *)(Qcode.time.time + time_index * 2), "%02X", Qcode.time.Second);
- uint32_t crc = 0;
- crc += Qcode.head[0];
- crc += Qcode.head[1];
- crc += sumArray((uint8_t *)&Qcode.mac, sizeof(Qcode.mac));
- crc += Qcode.client.cmd;
- crc += Qcode.client.len;
- int len = 0, i = 0;
- len = Qcode.client.len;
- while (len)
- {
- crc += Qcode.client.client_id[i];
- len--;
- i++;
- }
- crc += Qcode.product.cmd;
- crc += Qcode.product.len;
- i = 0;
- len = Qcode.product.len;
- while (len)
- {
- if (Qcode.product.product[i] != '0')
- {
- crc += Qcode.product.product[i];
- }
- else
- {
- break;
- }
- len--;
- i++;
- }
- i = 0;
- len = Qcode.time.len;
- while (len)
- {
- if (Qcode.time.time[i] != '0')
- {
- crc += Qcode.time.time[i];
- }
- else
- {
- break;
- }
- len--;
- i++;
- }
- Qcode.CRC = crc & 0xff;
- int index = 0;
- sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.head[0], Qcode.head[1]);
- index += 2;
- sprintf(qrcode_protocol + index * 2, "%02X", Qcode.CRC);
- index += 1;
- sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.mac.cmd, Qcode.mac.len);
- index += 2;
- // 转换16进制数组为ASCII字符串 mac 地址
- for (int i = 0; i < Qcode.mac.len; i++)
- {
- sprintf(qrcode_protocol + index * 2, "%02X", Qcode.mac.mac_addr[i]);
- index += 1;
- }
- sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.client.cmd, Qcode.client.len);
- index += 2;
- // 转换16进制数组为ASCII字符串 client id
- for (int i = 0; i < Qcode.client.len; i++)
- {
- if (Qcode.client.client_id[i] != 0)
- {
- sprintf(qrcode_protocol + index * 2, "%02X", Qcode.client.client_id[i]);
- index += 1;
- }
- }
- // 转换16进制数组为ASCII字符串 product
- sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.product.cmd, Qcode.product.len);
- index += 2;
- for (int i = 0; i < Qcode.product.len; i++)
- {
- // if(Qcode.product.product[i] != '0')
- { // 这里产品id为0也需要
- sprintf(qrcode_protocol + index * 2, "%02X", Qcode.product.product[i]);
- index += 1;
- }
- }
- // 转换16进制数组为ASCII字符串 time
- sprintf(qrcode_protocol + index * 2, "%02X%02X", Qcode.time.cmd, Qcode.time.len);
- index += 2;
- for (int i = 0; i < Qcode.time.len; i++)
- {
- // if(Qcode.time.time[i] != '0')
- { // 这里时间为0也需要
- sprintf(qrcode_protocol + index * 2, "%02X", Qcode.time.time[i]);
- index += 1;
- }
- }
- strcpy(result, str_https);
- strcat(result, qrcode_protocol);
- // ESP_LOGW(LOG_TAG,"二维码信息:%s",result);
- }
- // 函数用于查找字节序列在数组中出现的次数,并返回出现位置的索引
- int findByteSequence(const u8 *array, int arraySize, const u8 *sequence, int sequenceSize, int **positions)
- {
- int count = 0;
- int *indices = NULL;
- for (int i = 0; i <= arraySize - sequenceSize; i++)
- {
- // 检查当前位置开始的字节序列是否与目标序列匹配
- int j;
- for (j = 0; j < sequenceSize; j++)
- {
- if (array[i + j] != sequence[j])
- {
- break; // 不匹配,跳出循环
- }
- }
- if (j == sequenceSize)
- {
- count++; // 找到匹配的序列
- // 记录匹配位置的索引
- int *temp = (int *)realloc(indices, count * sizeof(int));
- if (temp == NULL)
- {
- // 内存分配失败,处理错误
- if (indices != NULL)
- {
- free(indices);
- }
- return -1;
- }
- indices = temp;
- indices[count - 1] = i;
- }
- }
- *positions = indices; // 传递匹配位置的索引数组
- return count;
- }
- // 匹配mac地址是否为本机
- bool mac_is_true(uint8_t *mac)
- {
- if (
- (mac[0] == Machine_info.mac_addr[0]) &&
- (mac[1] == Machine_info.mac_addr[1]) &&
- (mac[2] == Machine_info.mac_addr[2]) &&
- (mac[3] == Machine_info.mac_addr[3]) &&
- (mac[4] == Machine_info.mac_addr[4]) &&
- (mac[5] == Machine_info.mac_addr[5]))
- {
- return true;
- }
- return false;
- }
- bool subcontract(YC_DATA_T *data)
- {
- int rsp_msgid = 0;
- int index = 0;
- int all_len = data->len;
- uint8_t *buf = data->data;
- USE_DATA_T use_data;
- volatile bool refresh_flag = false;
- u8 targetSequence[] = {0x5A, 0x51};
- int sequenceSize = sizeof(targetSequence);
- int *positions = NULL;
- int result = findByteSequence(buf, all_len, targetSequence, sequenceSize, &positions);
- if (result == 1)
- {
- printf("cmd 5A 51 find times %d,data len = %d\r\n", result, data->len);
- for (int index = 0; index < result; index++)
- {
- if ((buf[0] == 0x5A) && (buf[1] == 0x51) && (result == 1))
- {
- // 单条命令处理逻辑
- printf("deal one data \r\n");
- use_data.data_len = all_len; //(((buf[positions[index] - 2])<<8) | ((buf[positions[index] - 1])))+2;
- memcpy(use_data.data_buf, data->data, use_data.data_len);
- #if 0
- printf("single cmd len =%d\r\n",use_data.data_len);
- for(int i = 0;i<use_data.data_len;i++)
- {
- printf("%02X",use_data.data_buf[i]);
- }
- printf("\r\n");
- #endif
- bool is = mac_is_true(&use_data.data_buf[3]);
- if (is)
- {
- business_logic_func(&use_data, USE_CMD_LEN_INDEX - 2, 0);
- refresh_flag = true;
- }
- else
- {
- printf("mac is fail not deal\r\n");
- goto err;
- }
- }
- else
- {
- rsp_msgid = (buf[13] << 8) | buf[14]; // 获取当前消息id 用于删除
- printf("rsp_msgid %02x,%02x,%d\r\n", buf[13], buf[14], rsp_msgid);
- for (int index = 0; index < result; index++)
- {
- // 多条指令处理逻辑
- bool is = mac_is_true(&buf[positions[index] + 3]);
- if (is) // 验证为本机,处理数据 回复ack
- {
- use_data.data_len = (((buf[positions[index] - 2]) << 8) | ((buf[positions[index] - 1]))) + 2;
- memcpy(use_data.data_buf, data->data + (positions[index] - 2), use_data.data_len);
- // print_yc_data(&use_data);
- business_logic_func(&use_data, USE_CMD_LEN_INDEX, rsp_msgid);
- refresh_flag = true;
- reply_ack_func(0x00, rsp_msgid, true, NULL); // 回复ack
- spiffs_write(&Machine_info);
- ESP_LOGE(LOG_TAG, "1times cmd = %02X msg = %X start reply ack\r\n", 0x00, rsp_msgid);
- }
- else
- {
- printf("mac is fail not deal\r\n");
- goto err;
- }
- }
- }
- }
- }
- if (result > 1)
- {
- if ((buf[0] == 0x5a) && (buf[1] == 0x51))
- {
- printf("result = 1,but not real\r\n");
- goto err;
- }
- int data_len = 0;
- data_len = (buf[0] << 8) | (buf[1]);
- printf("cmd 5A 51 find times %d,all data len = %d\r\n", result, data_len);
- printf("not single cmd len =%d\r\n", use_data.data_len);
- rsp_msgid = (buf[13] << 8) | buf[14]; // 获取当前消息id 用于删除;
- ESP_LOGW(LOG_TAG, "--rsp_msgid %02x,%02x,%d\r\n", buf[13], buf[14], rsp_msgid);
- for (int index = 0; index < result; index++)
- {
- // 多条指令处理逻辑
- bool is = mac_is_true(&buf[positions[index] + 3]);
- if (is)
- {
- use_data.data_len = 15 + 2 + (((buf[positions[index] + 13]) << 8) | ((buf[positions[index] + 14])));
- use_data.data_buf[0] = 0x00;
- use_data.data_buf[1] = 0x00;
- memcpy(&use_data.data_buf[2], data->data + (positions[index]), use_data.data_len + 15);
- // printf("================last result============== len =%d\r\n",use_data.data_len);
- // for(int i = 0;i<use_data.data_len;i++)
- // {
- // printf("%02x",use_data.data_buf[i]);
- // }
- // printf("================last result==============\r\n");
- // print_yc_data(&use_data);
- business_logic_func(&use_data, USE_CMD_LEN_INDEX, rsp_msgid);
- refresh_flag = true;
- reply_ack_func(0x00, rsp_msgid, true, NULL); // 回复ack
- spiffs_write(&Machine_info);
- ESP_LOGE(LOG_TAG, "cmd = %02X msg = %X start reply ack\r\n", 0x00, rsp_msgid);
- }
- else
- {
- printf("mac is fail not deal\r\n");
- goto err;
- }
- }
- }
- free(positions);
- return refresh_flag;
- err:
- refresh_flag = false;
- free(positions);
- return refresh_flag;
- }
- // // len:有效数据长度,不是总长//
- // static void protocol_package(char cmd, const uint8_t *info, int len)
- // {
- // #define __PACKAGE 15
- // uint8_t *__str = (uint8_t *)malloc(sizeof(uint8_t) * (__PACKAGE + len) * 2);
- // __str[0] = 0x5a;
- // __str[1] = 0x51;
- // __str[2] = 0x00;
- // __str[3] = mac_addr[0];
- // __str[4] = mac_addr[1];
- // __str[5] = mac_addr[2];
- // __str[6] = mac_addr[3];
- // __str[7] = mac_addr[4];
- // __str[8] = mac_addr[5];
- // __str[9] = Machine_info.eflagID;
- // __str[10] = cmd;
- // #if 0
- // __str[11] = 0x01;//当前只为一包
- // __str[12] = 0x01;//当前只为一包
- // #else
- // // 随机数
- // srand((unsigned int)time(NULL));
- // int time_rand = rand() % 65535 + 1; // 生成1~100的随机数
- // __str[11] = (time_rand >> 8) & 0xFF;
- // __str[12] = time_rand & 0xFF;
- // #endif
- // // 分包
- // __str[13] = (len >> 8) & 0xFF;
- // __str[14] = len & 0xFF;
- // memcpy(__str + __PACKAGE, info, len);
- // int i = 0, sum = 0;
- // for (i = 0; i < len + __PACKAGE; i++)
- // {
- // sum += __str[i];
- // }
- // __str[2] = sum & 0xFF;
- // printf("crc=%02x ", __str[2]);
- // printf("-->package:");
- // for (i = 0; i < len + __PACKAGE; i++)
- // {
- // printf("%02x ", __str[i]);
- // }
- // printf("\n");
- // changebintotxt(__str, len + __PACKAGE);
- // #if 0
- // Send_list = postInsert(Send_list, time_rand,cmd,(char *)__str,(int)(len+__PACKAGE)*2);//len*2
- // #else
- // lora_send_data((char *)__str, len + __PACKAGE);
- // #endif
- // free(__str);
- // }
- int txttobin(uint8_t *strlorabuf)
- {
- int len = 0, i = 0;
- uint8_t strbuf[1024];
- char ch = 0, chhi = 0, chlo = 0;
- len = strlen((char *)strlorabuf);
- strcpy((char *)strbuf, (char *)strlorabuf);
- memset(strlorabuf, 0, len);
- // Serial.print("jiexishuju:");Serial.println((char *)strbuf);
- for (i = 0; i < len; i++)
- {
- ch = strbuf[i * 2];
- if ((ch >= 0x30 && ch <= 0x39) ||
- (ch >= 0x41 && ch <= 0x5a) ||
- (ch >= 0x61 && ch <= 0x7a))
- {
- {
- if (ch >= 0x30 && ch <= 0x39)
- ch = ch - 0x30;
- else if (ch >= 0x41 && ch <= 0x5a)
- ch = ch - 0x37;
- else
- ch = ch - 0x57;
- ch &= 0x000f;
- chhi = (ch << 4);
- }
- ch = strbuf[i * 2 + 1];
- if ((ch >= 0x30 && ch <= 0x39) ||
- (ch >= 0x41 && ch <= 0x5a) ||
- (ch >= 0x61 && ch <= 0x7a))
- {
- if (ch >= 0x30 && ch <= 0x39)
- ch = ch - 0x30;
- else if (ch >= 0x41 && ch <= 0x5a)
- ch = ch - 0x37;
- else
- ch = ch - 0x57;
- ch &= 0x000f;
- ch = chhi + ch;
- }
- strlorabuf[i] = ch;
- }
- }
- // Serial.write(strlorabuf,len);
- return i;
- }
- void changebintotxt(uint8_t *strb, int len)
- {
- int i = 0;
- uint8_t strtxt[512];
- char ch, chhi, chlo;
- memset(strtxt, 0, 512);
- for (i = 0; i < len; i++)
- {
- ch = strb[i];
- chhi = ch;
- chhi >>= 4;
- chhi &= 0x000f;
- if (chhi <= 9)
- chhi += 0x30;
- else
- chhi += 0x37;
- strtxt[i * 2] = chhi;
- chlo = ch;
- chlo &= 0x000f;
- if (chlo <= 9)
- chlo += 0x30;
- else
- chlo += 0x37;
- strtxt[i * 2 + 1] = chlo;
- }
- strcpy((char *)strb, (char *)strtxt);
- }
- /******************************
- //获取网关时间
- ******************************/
- void f_send_get_time(void)
- {
- if (lora_pool[LORA_INFO_GET_TIME].flag == true)
- {
- ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
- return;
- }
- uint8_t strlorabuf[200];
- uint8_t strnum[20];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x01; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- if (Machine_info.msg_id > 1000)
- {
- ESP_LOGE(LOG_TAG, "time Machine_info.msg_id>1000");
- return;
- }
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x00; // 长度
- strlorabuf[15] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 15; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 15); // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- // lora_send_data((char *)strlorabuf,len);
- lora_pool[LORA_INFO_GET_TIME].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_GET_TIME].flag = true;
- Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_GET_TIME, (char *)strlorabuf, len); // 插入数据
- }
- void f_send_get_chart_data(void)
- {
- if (lora_pool[LORA_INFO_GET_CHART].flag == true)
- {
- ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
- return;
- }
- ESP_LOGW(LOG_TAG, "f_send_get_chart_data");
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x11; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- if (Machine_info.msg_id > 1000)
- {
- ESP_LOGE(LOG_TAG, "chart Machine_info.msg_id>1000");
- return;
- }
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x00; // 长度
- strlorabuf[15] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 15; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 15);
- // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- // lora_send_data((char *)strlorabuf,len);
- lora_pool[LORA_INFO_GET_CHART].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_GET_CHART].flag = true;
- Send_list = postInsert(Send_list, (int)Machine_info.msg_id, LORA_INFO_GET_CHART, (char *)strlorabuf, len); // 插入数据
- }
- void f_send_version(void)
- {
- if (lora_pool[LORA_INFO_VERSION].flag == true)
- {
- ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
- return;
- }
- // ESP_LOGW(LOG_TAG, "f_send_version");
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x91; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- if (Machine_info.msg_id > 1000)
- {
- ESP_LOGE(LOG_TAG, "version Machine_info.msg_id>1000");
- return;
- }
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x03; // 长度
- strlorabuf[15] = (VERSION_X) & 0xff;
- strlorabuf[16] = (VERSION_Y) & 0xff;
- strlorabuf[17] = (VERSION_Z) & 0xff;
- strlorabuf[18] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 18; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 18);
- // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- lora_pool[LORA_INFO_VERSION].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_VERSION].flag = true;
- Send_list = postInsert(Send_list, (int)Machine_info.msg_id, LORA_INFO_VERSION, (char *)strlorabuf, len); // 插入数据
- }
- void f_send_lora_rssi(int rssi)
- {
- if (lora_pool[LORA_INFO_RSSI].flag == true)
- {
- ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
- return;
- }
- // ESP_LOGW(LOG_TAG,"f_send_lora_rssi:rssi = %d",rssi);
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x05; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- if (Machine_info.msg_id > 1000)
- {
- ESP_LOGE(LOG_TAG, "rssi Machine_info.msg_id>1000");
- return;
- }
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x01; // 长度
- strlorabuf[15] = (rssi) & 0xff;
- strlorabuf[16] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 16; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 16);
- // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- lora_pool[LORA_INFO_RSSI].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_RSSI].flag = true;
- Send_list = postInsert(Send_list, (int)Machine_info.msg_id, LORA_INFO_RSSI, (char *)strlorabuf, len); // 插入数据
- }
- void f_send_test(void)
- {
- // ESP_LOGW(LOG_TAG,"f_send_lora_rssi:rssi = %d",rssi);
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x05; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- Machine_info.msg_id = 0x1234;
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x01; // 长度
- strlorabuf[15] = 0xab;
- strlorabuf[16] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 16; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 16);
- // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- // send_lora_data(strlorabuf, len);
- // lora_send_data((char *)strlorabuf, len);
- Send_list = postInsert(Send_list, (int)Machine_info.msg_id, LORA_INFO_BATT, (char *)strlorabuf, len); // 插入数据
- }
- void f_send_battary_vaule(int battry)
- {
- if (lora_pool[LORA_INFO_BATT].flag == true)
- {
- ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
- return;
- }
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x04; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- if (Machine_info.msg_id > 1000)
- {
- ESP_LOGE(LOG_TAG, "batt Machine_info.msg_id>1000");
- return;
- }
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x01; // 长度
- strlorabuf[15] = (battry) & 0xff;
- strlorabuf[16] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 16; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 16);
- // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- lora_pool[LORA_INFO_BATT].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_BATT].flag = true;
- Send_list = postInsert(Send_list, (int)Machine_info.msg_id, LORA_INFO_BATT, (char *)strlorabuf, len); // 插入数据
- }
- void f_send_reply_status(int status)
- {
- if (lora_pool[LORA_INFO_BATT].flag == true)
- {
- ESP_LOGE(LOG_TAG, "%s-->return", __FUNCTION__);
- return;
- }
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x06; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x01; // 长度
- strlorabuf[15] = (status) & 0xff;
- strlorabuf[16] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 16; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 16);
- // Serial.println((char *)strlorabuf);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- lora_pool[LORA_INFO_REPLY].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_REPLY].flag = true;
- Send_list = postInsert(Send_list,(int)Machine_info.msg_id, LORA_INFO_REPLY, (char *)strlorabuf, len); // 插入数据
- }
- // 唤醒时发送是否可以接收数据命令
- void send_can_I_receive()
- {
- uint8_t strlorabuf[200];
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51;
- strlorabuf[2] = 0x51;
- strlorabuf[3] = 0x5A;
- strlorabuf[4] = 0x00;
- #if 0
- strlorabuf[5] = 0x01;
- #else
- strlorabuf[5] = Machine_info.eflagID;
- if (Send_list != NULL)
- {
- strlorabuf[6] = 0x01;
- }
- else
- {
- strlorabuf[6] = 0x00;
- }
- strlorabuf[7] = Machine_info.mac_addr[0];
- strlorabuf[8] = Machine_info.mac_addr[1];
- strlorabuf[9] = Machine_info.mac_addr[2];
- strlorabuf[10] = Machine_info.mac_addr[3];
- strlorabuf[11] = Machine_info.mac_addr[4];
- strlorabuf[12] = Machine_info.mac_addr[5];
- int crc = 0;
- for (int i = 0; i < 13; i++)
- {
- crc += strlorabuf[i];
- }
- strlorabuf[13] = crc & 0xff;
- #endif
- // send_lora_data(strlorabuf,len); //不直接发送
- // printf("->can i\n");
- ESP_LOG_BUFFER_HEX("->can i", strlorabuf, 14);
- lora_send_data((char *)strlorabuf, 14);
- }
- // 发送是否可以接收数据命令
- // void send_pair_ack()
- // {
- // uint8_t strlorabuf[200];
- // strlorabuf[0] = 0x5A;
- // strlorabuf[1] = 0x51;
- // strlorabuf[2] = 0x51;
- // strlorabuf[3] = 0x5A;
- // strlorabuf[4] = 0x00;
- // #if 0
- // strlorabuf[5] = 0x01;
- // #else
- // strlorabuf[5] = Machine_info.eflagID;
- // strlorabuf[6] = lora_channel + 1;
- // #endif
- // //send_lora_data(strlorabuf,len); //不直接发送
- // lora_send_data((char *)strlorabuf,7);
- // }
- // 4bytes、32bit数据大小端转化
- #define L2B32(Little) (Little = ((Little & 0xff) << 24) | (((Little) & 0xff00) << 8) | (((Little) & 0xff0000) >> 8) | ((Little >> 24) & 0xff))
- uint8_t change_btn_reversal(uint8_t reversal_btn) // lora收到按键,转化为本地状态
- {
- uint8_t btn_tmp = 0;
- switch (reversal_btn)
- {
- case 0:
- btn_tmp = 1;
- break;
- case 1:
- btn_tmp = 6;
- break;
- case 2:
- btn_tmp = 4;
- break;
- case 3:
- btn_tmp = 3;
- break;
- case 4:
- btn_tmp = 5;
- break;
- case 5:
- btn_tmp = 2;
- break;
- break;
- default:
- btn_tmp = 1;
- break;
- }
- return btn_tmp;
- }
- uint8_t change_btn(uint8_t btn)
- {
- uint8_t btn_tmp = 0;
- switch (btn)
- {
- case 1:
- btn_tmp = 0;
- break;
- case 2:
- btn_tmp = 5;
- break;
- case 3:
- btn_tmp = 3;
- break;
- case 4:
- btn_tmp = 2;
- break;
- case 5:
- btn_tmp = 4;
- break;
- case 6:
- btn_tmp = 1;
- break;
- default:
- btn_tmp = 0;
- break;
- }
- // ESP_LOGW(LOG_TAG,"btn = [%d] ,btn_tmp[%d]",btn,btn_tmp);
- return btn_tmp;
- }
- /*发送状态持续的时间 状态持续时长(0x0E)*/
- void f_send_status_duration(void)
- {
- if (lora_pool[LORA_INFO_DUR].flag == true)
- {
- return;
- }
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- // 帧头
- strlorabuf[0] = 0x5A; // 包头
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x0E; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x0C; // 长度
- uint8_t tmp = change_btn(Machine_info.last_button.button_info);
- strlorabuf[15] = tmp; // 上一次的状态
- if ((Machine_info.last_button.Year == 0x00) && (Machine_info.last_button.Month == 0x00) && (Machine_info.last_button.Day == 0x00))
- {
- strlorabuf[16] = 0x00;
- strlorabuf[17] = 0x00;
- strlorabuf[18] = 0x00;
- strlorabuf[19] = 0x00;
- strlorabuf[20] = 0x00;
- strlorabuf[21] = 0x00;
- strlorabuf[22] = 0x00;
- printf("time is not sync\r\n");
- }
- else
- {
- strlorabuf[16] = (Machine_info.last_button.Year >> 8) & 0xff;
- strlorabuf[17] = Machine_info.last_button.Year & 0xff;
- strlorabuf[18] = Machine_info.last_button.Month;
- strlorabuf[19] = Machine_info.last_button.Day;
- strlorabuf[20] = Machine_info.last_button.Hour;
- strlorabuf[21] = Machine_info.last_button.Minute;
- strlorabuf[22] = Machine_info.last_button.Second;
- }
- strlorabuf[23] = (Machine_info.Duration_time >> 24) & 0xff; // 分钟计算
- strlorabuf[24] = (Machine_info.Duration_time >> 16) & 0xff;
- strlorabuf[25] = (Machine_info.Duration_time >> 8) & 0xff;
- strlorabuf[26] = (Machine_info.Duration_time) & 0xff;
- strlorabuf[27] = 0;
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 27; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- 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]);
- Machine_info.Duration_time = 0;
- changebintotxt(strlorabuf, 27);
- // printf("Machine_info cid %s\r\n",Machine_info.cid);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- #if 0
- lora_send_data((char *)strlorabuf,len);/直接发送
- #else
- // printList(Send_list);
- lora_pool[LORA_INFO_DUR].flag = true;
- lora_pool[LORA_INFO_DUR].num = Machine_info.msg_id;
- Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_DUR, (char *)strlorabuf, len); // 插入数据
- #endif
- // lora_send_data((char *)strlorabuf,len);
- last_button_info.time_min = 0;
- }
- /*当前最新的状态*/
- void f_send_update_status(void)
- {
- if (lora_pool[LORA_INFO_STATUS].flag == true)
- {
- return;
- }
- // memset(&Machine_info.current_button,0,sizeof(Button_Time_t));
- Machine_info.current_button.Year = Machine_info.year;
- Machine_info.current_button.Month = Machine_info.month;
- Machine_info.current_button.Day = Machine_info.day;
- Machine_info.current_button.Hour = Machine_info.hour;
- Machine_info.current_button.Minute = Machine_info.min;
- Machine_info.current_button.Second = Machine_info.sec;
- Machine_info.current_button.time_min = 0;
- uint8_t strlorabuf[200];
- int crc16 = 0, i = 0, len = 0;
- // 帧头 目标地址 源地址(id) 长度 模式 具体内容 校验
- strlorabuf[0] = 0x5A;
- strlorabuf[1] = 0x51; // 包头
- strlorabuf[2] = 0x00; // 校验和
- strlorabuf[3] = mac_addr[0];
- strlorabuf[4] = mac_addr[1];
- strlorabuf[5] = mac_addr[2];
- strlorabuf[6] = mac_addr[3];
- strlorabuf[7] = mac_addr[4];
- strlorabuf[8] = mac_addr[5];
- strlorabuf[9] = Machine_info.eflagID; // 组内编号
- strlorabuf[10] = 0x0F; // cmd命令字
- Machine_info.msg_id = allocateMsgIdNum(); // 分配ID
- strlorabuf[11] = ((Machine_info.msg_id) >> 8) & 0XFF; //
- strlorabuf[12] = ((Machine_info.msg_id) >> 0) & 0XFF; // 总包数,当前包数
- strlorabuf[13] = 0x00;
- strlorabuf[14] = 0x0C; // 长度
- uint8_t tmp = change_btn(Machine_info.current_button.button_info);
- strlorabuf[15] = tmp; // 当前的状态
- if ((Machine_info.current_button.Year == 0x00) && (Machine_info.current_button.Month == 0x00) && (Machine_info.current_button.Day == 0x00))
- {
- strlorabuf[16] = 0x00;
- strlorabuf[17] = 0x00;
- strlorabuf[18] = 0x00;
- strlorabuf[19] = 0x00;
- strlorabuf[20] = 0x00;
- strlorabuf[21] = 0x00;
- strlorabuf[22] = 0x00;
- printf("time is not sync\r\n");
- }
- else
- {
- strlorabuf[16] = (Machine_info.current_button.Year >> 8) & 0xff;
- strlorabuf[17] = Machine_info.current_button.Year & 0xff;
- strlorabuf[18] = Machine_info.current_button.Month;
- strlorabuf[19] = Machine_info.current_button.Day;
- strlorabuf[20] = Machine_info.current_button.Hour;
- strlorabuf[21] = Machine_info.current_button.Minute;
- strlorabuf[22] = Machine_info.current_button.Second;
- }
- strlorabuf[23] = (Machine_info.current_button.time_min >> 24) & 0xff; // 分钟计算
- strlorabuf[24] = (Machine_info.current_button.time_min >> 16) & 0xff;
- strlorabuf[25] = (Machine_info.current_button.time_min >> 8) & 0xff;
- strlorabuf[26] = Machine_info.current_button.time_min & 0xff;
- strlorabuf[27] = 0;
- 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]);
- crc16 = strlorabuf[0] + strlorabuf[1];
- for (i = 3; i < 27; i++)
- crc16 = crc16 + strlorabuf[i];
- strlorabuf[2] = crc16 & 0xff;
- changebintotxt(strlorabuf, 27);
- // printf("Machine_info cid %s\r\n",Machine_info.cid);
- strcat((char *)strlorabuf, (char *)Machine_info.cid);
- len = strlen((char *)strlorabuf);
- #if 0
- lora_send_data((char *)strlorabuf,len);
- #else
- lora_pool[LORA_INFO_STATUS].num = Machine_info.msg_id;
- lora_pool[LORA_INFO_STATUS].flag = true;
- Send_list = postInsert(Send_list, Machine_info.msg_id, LORA_INFO_STATUS, (char *)strlorabuf, len); // 插入数据
- #endif
- Machine_info.last_button.time_min = 0;
- }
- // void send_ACK(char cmd, bool flag)
- // {
- // uint8_t ack = 0;
- // if (flag)
- // ack = 0x01;
- // else
- // ack = 0x00;
- // protocol_package(cmd, &ack, 1);
- // }
- void adc1_init(void)
- {
- //-------------ADC Init---------------//
- adc_oneshot_unit_init_cfg_t init_config = {
- .unit_id = ADC_UNIT_1,
- .ulp_mode = ADC_ULP_MODE_DISABLE,
- };
- ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc1_handle));
- }
- // void set_status_heights(void)
- // {
- // extern uint32_t arr_heights[10];
- // memcpy(&_0703_button_info,&last_button_info,sizeof(Button_Time_t));
- // switch (_0703_button_info.button_info)
- // {
- // case 0:
- // case 1://yunxing
- // arr_heights[0]+=1;
- // break;
- // case 2://tingji
- // arr_heights[1]+=1;
- // break;
- // case 3://guzhang
- // arr_heights[2]+=1;
- // break;
- // case 4://fengcun
- // arr_heights[3]+=1;
- // break;
- // case 5://baoyang
- // arr_heights[4]+=1;
- // break;
- // case 6://dailiao
- // arr_heights[5]+=1;
- // break;
- // default:
- // break;
- // }
- // // ESP_LOGE(LOG_TAG,"button_info = %d ,time_min = %ld ---%ld - %ld - %ld - %ld - %ld - %ld",
- // // _0703_button_info.button_info,_0703_button_info.time_min,
- // // arr_heights[0],arr_heights[1],arr_heights[2],
- // // arr_heights[3],arr_heights[4],arr_heights[5]);
- // }
- void set_status_heights(void)
- {
- // STATE_OPERATION = 1, //运行
- // STATE_SHUT_DOWN = 2, //停机
- // STATE_BREAKDOWN = 3, //故障
- // STATE_SAFEKEEP = 4, //封存
- // STATE_UPKEEP = 5, //保养
- // STATE_WAIT_MATERIALS = 6, //待料
- extern uint32_t arr_heights[10];
- switch (Machine_info.last_button.button_info)
- {
- case 0:
- case 1: // yunxing
- arr_heights[0] += Machine_info.Duration_time;
- break;
- case 2: // tingji
- arr_heights[1] += Machine_info.Duration_time;
- break;
- case 3: // guzhang
- arr_heights[2] += Machine_info.Duration_time;
- break;
- case 4: // fengcun
- arr_heights[3] += Machine_info.Duration_time;
- break;
- case 5: // baoyang
- arr_heights[4] += Machine_info.Duration_time;
- break;
- case 6: // dailiao
- arr_heights[5] += Machine_info.Duration_time;
- break;
- default:
- break;
- }
- // ESP_LOGE(LOG_TAG,"button_info = %d ,time_min = %ld ---%ld - %ld - %ld - %ld - %ld - %ld",
- // _0703_button_info.button_info,_0703_button_info.time_min,
- // arr_heights[0],arr_heights[1],arr_heights[2],
- // arr_heights[3],arr_heights[4],arr_heights[5]);
- }
- void lora_timer_start(void)
- {
- ESP_ERROR_CHECK(esp_timer_start_once(lora_timer, 500 * 1000)); // 500ms
- }
- void lora_tiemr_stop(void)
- {
- ESP_ERROR_CHECK(esp_timer_stop(lora_timer));
- }
- void lora_timer_restart(void)
- {
- ESP_ERROR_CHECK(esp_timer_stop(lora_timer));
- ESP_ERROR_CHECK(esp_timer_start_once(lora_timer, 500 * 1000)); // 500ms
- }
- #if 1
- // /*********************************************************************************
- // * function : analysis_protocol
- // * Description : lora 接受数据解析函数
- // * Input :
- // * Output :
- // * Author : 祁鑫 Data : 2023 9.18
- // **********************************************************************************/
- // void analysis_protocol(char *result, int len)
- // {
- // int index = 0;
- // int length = 0;
- // int count = len;
- // int num = 0;
- // while (count)
- // {
- // if (((result[index + 2]) == 0x5A) && ((result[index + 3]) == 0x51))
- // {
- // length = ((result[index]) << 8) | ((result[index + 1]));
- // num++;
- // printf("num =%d,length=%d\r\n", num, length);
- // #if 1
- // for (int i = 0; i < length; i++)
- // {
- // printf("%02x", result[index + 2 + i]);
- // }
- // #endif
- // printf("\r\n");
- // // 匹配mac地址 //计算校验和
- // if (
- // (Machine_info.mac_addr[0] == (result[index + 5])) &&
- // (Machine_info.mac_addr[1] == (result[index + 6])) &&
- // (Machine_info.mac_addr[2] == (result[index + 7])) &&
- // (Machine_info.mac_addr[3] == (result[index + 8])) &&
- // (Machine_info.mac_addr[4] == (result[index + 9])) &&
- // (Machine_info.mac_addr[5] == (result[index + 10])))
- // {
- // // mac地址匹配
- // printf("mac check ok\r\n");
- // // packetparsing((unsigned char *)&result[index+2]); //16进制解析数据
- // }
- // else
- // {
- // printf("mac = %02x%02x%02x%02x%02x%02x\r\n",
- // result[index + 5],
- // result[index + 6],
- // result[index + 7],
- // result[index + 8],
- // result[index + 9],
- // result[index + 10]);
- // printf("device mac = %02x%02x%02x%02x%02x%02x\r\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]);
- // // mac地址匹配
- // printf("mac check fail\r\n");
- // }
- // }
- // {
- // index++;
- // }
- // count--;
- // }
- // // printf("num =%d\r\n",num);
- // #if 0
- // if(left_display_refresh == true)
- // {
- // left_display_refresh = false;
- // PIC_display((unsigned char *)dspbuf_left);
- // }
- // reflashdfpr(dspbuf);
- // #endif
- // }
- /*********************************************************************************
- * function : selectionSort
- * Description : 进行数据排序并输出排序后的数据 从小到大的索引
- * Input :
- * Output :
- * Author : 祁鑫 Data : 2023 8.12
- **********************************************************************************/
- void selectionSort(int arr[], int n, int indices[])
- {
- for (int i = 0; i < n - 1; i++)
- {
- int minIndex = i;
- for (int j = i + 1; j < n; j++)
- {
- if (arr[j] < arr[minIndex])
- {
- minIndex = j;
- }
- }
- // 交换元素
- int temp = arr[i];
- arr[i] = arr[minIndex];
- arr[minIndex] = temp;
- // 更新索引
- int tempIndex = indices[i];
- indices[i] = indices[minIndex];
- indices[minIndex] = tempIndex;
- }
- }
- /*********************************************************************************
- * function : person_get_num_is_exist
- * Description : 获取当前类型序号是否存在
- * Input :
- * Output :
- * Author : 祁鑫 Data : 2023 8.12
- **********************************************************************************/
- int person_get_num_is_exist(uint8_t type, uint16_t num)
- {
- // uint8_t index = 0;
- // printf("person_get_num_is_exist %d\r\n",num);
- if (type == person[type].person_type)
- {
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- if (person[type].person_name[i].person_num == num)
- {
- // printf("person_get_num is exist\r\n");
- return 1;
- }
- }
- }
- // printf("person_get_num not exist\r\n");
- return 0;
- }
- /*********************************************************************************
- * function : person_get_num
- * Description : 获取当前类型人员数量
- * Input :
- * Output :
- * Author : 祁鑫 Data : 2023 8.12
- **********************************************************************************/
- int person_get_num(uint8_t type)
- {
- uint8_t index = 0;
- // printf("person_get_num\r\n");
- if (type == person[type].person_type) // 管理员
- {
- // 查找当前不为空的字符串数组
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- if (strlen((char *)&person[type].person_name[i].person_name) != 0)
- {
- index++;
- }
- }
- }
- return index;
- }
- /*********************************************************************************
- * function : person_add
- * Description : 人员添加/更新
- * Input :
- * Output :
- * Author : 祁鑫 Data : 2023 8.12
- **********************************************************************************/
- void person_add(uint8_t type, uint8_t num, char *in, char *out)
- {
- // printf("person_add\r\n");
- if (type == person[type].person_type) // 管理员
- {
- uint8_t index = 0;
- // 查找当前不为空的字符串数组
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- // Serial.println(person[type].person_name[i].person_num);
- // 如果序号存在更新
- if (person[type].person_name[i].person_num == num)
- {
- // Serial.print("person_num exist = ");
- // Serial.println(person[type].person_name[i].person_num);
- index = i;
- break;
- }
- // 如果序号不存在添加
- if (strlen((char *)&person[type].person_name[i].person_name) == 0)
- {
- // Serial.print("person_num not exist = ");
- index = i;
- person[type].person_name[i].person_num = num;
- // Serial.println(person[type].person_name[i].person_num);
- break;
- }
- }
- if (index > PERSON_MAX_NAME)
- {
- }
- else
- {
- if (person[type].person_name[index].person_num == num)
- {
- memset(&person[type].person_name[index].person_name, 0x00, PERSON_NAME_MAX_LEN);
- }
- strcpy((char *)&person[type].person_name[index].person_name, in);
- }
- // Serial.println("show font");
- // 开始显示数据
- int sort_buffer[PERSON_MAX_NAME] = {0}; // 获取当前序号值
- int indices[PERSON_MAX_NAME]; // 排序后的索引值
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- indices[i] = i;
- }
- // 拷贝序号数据
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- sort_buffer[i] = person[type].person_name[i].person_num;
- }
- selectionSort(sort_buffer, PERSON_MAX_NAME, indices);
- #if 0
- //Serial.print("Sorted array: ");
- for (int i = 0; i < PERSON_MAX_NAME; i++) {
- //Serial.print(sort_buffer[i]);
- //Serial.print(",");
- }
- //Serial.println("");
- //Serial.print("\nOriginal indices: ");
- for (int i = 0; i < PERSON_MAX_NAME; i++) {
- //Serial.print(indices[i]);
- //Serial.print(",");
- }
- //Serial.println("");
- #endif
- #if 0
- //进行序号排序
- bubbleSort(sort_buffer, PERSON_MAX_NAME);
- #endif
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- int display_index = 0;
- display_index = indices[i]; // 获取当前显示的索引
- // Serial.println(person[type].person_name[display_index].person_num);
- if (strlen((char *)&person[type].person_name[display_index].person_name) != 0)
- {
- strcat(out, (char *)&person[type].person_name[display_index].person_name);
- strcat(out, " ");
- }
- }
- }
- }
- /*********************************************************************************
- * function : person_del
- * Description : 人员删除
- * Input :
- * Output :
- * Author : 祁鑫 Data : 2023 8.12
- **********************************************************************************/
- void person_del(uint8_t type, uint8_t num, char *in, char *out)
- {
- // printf("person_del\r\n");
- // Serial.print("delete num ");
- // Serial.println(num);
- if (type == person[type].person_type)
- {
- uint8_t index = 0;
- // 查找当前不为空的字符串数组
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- // 如果序号存在
- if (person[type].person_name[i].person_num == num)
- {
- printf("person_num exist \r\n");
- // Serial.println(person[type].person_name[i].person_num);
- index = i;
- break;
- }
- // return;
- }
- {
- if ((person[type].person_name[index].person_num == num))
- {
- // 清空显示
- // Serial.println("delete index data");
- printf("delete index data \r\n");
- person[type].person_name[index].person_num = 0;
- memset(&person[type].person_name[index].person_name, 0x00, PERSON_NAME_MAX_LEN);
- }
- // strcpy((char *)&person[type].person_name[index].person_name,in);
- }
- // Serial.println("show font");
- #if 0
- for(int i = 0;i<PERSON_MAX_NAME;i++)
- {
- Serial.println(person[type].person_name[i].person_num);
- if(strlen((char *)&person[type].person_name[i].person_name)!=0)
- {
- strcat(out,(char *)&person[type].person_name[i].person_name);
- strcat(out," ");
- }
- }
- #else
- // 开始显示数据
- int sort_buffer[PERSON_MAX_NAME] = {0}; // 获取当前序号值
- int indices[PERSON_MAX_NAME]; // 排序后的索引值
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- indices[i] = i;
- }
- // 拷贝序号数据
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- sort_buffer[i] = person[type].person_name[i].person_num;
- }
- selectionSort(sort_buffer, PERSON_MAX_NAME, indices);
- #if 0
- Serial.print("Sorted array: ");
- for (int i = 0; i < PERSON_MAX_NAME; i++) {
- Serial.print(sort_buffer[i]);
- Serial.print(",");
- }
- Serial.println("");
- Serial.print("\nOriginal indices: ");
- for (int i = 0; i < PERSON_MAX_NAME; i++) {
- Serial.print(indices[i]);
- Serial.print(",");
- }
- Serial.println("");
- #endif
- for (int i = 0; i < PERSON_MAX_NAME; i++)
- {
- int display_index = 0;
- display_index = indices[i]; // 获取当前显示的索引
- // Serial.println(person[type].person_name[display_index].person_num);
- if (strlen((char *)&person[type].person_name[display_index].person_name) != 0)
- {
- strcat(out, (char *)&person[type].person_name[display_index].person_name);
- strcat(out, " ");
- printf("out = %s\r\n", out);
- }
- }
- #endif
- }
- }
- #endif
- void loro_gwpair_ack(int destaddr, int sourceaddr, int cmd, int st, char *strd)
- {
- uint8_t strlorabuf[50];
- int crc16 = 0, len, i;
- memset(strlorabuf, 0, 50);
- // 源地址 是指网关地址
- // 帧头 目标地址 源地址(id) 长度 模式 具体内容 校验
- strlorabuf[0] = 0x55;
- strlorabuf[1] = 0xaa;
- strlorabuf[2] = destaddr & 0xff;
- strlorabuf[3] = sourceaddr & 0xff;
- strlorabuf[4] = 0x02;
- strlorabuf[5] = cmd;
- strlorabuf[6] = st;
- strcpy((char *)(strlorabuf + 7), strd); // mac+c_id
- crc16 = strlorabuf[0] + strlorabuf[1] + strlorabuf[2] + strlorabuf[3] + strlorabuf[4] + strlorabuf[5] + strlorabuf[6];
- len = strlen(strd);
- strlorabuf[4] = 4 + len;
- for (i = 0; i < len; i++)
- crc16 = crc16 + strlorabuf[7 + len];
- strlorabuf[7 + len] = (crc16 >> 8) & 0xff;
- strlorabuf[8 + len] = crc16 & 0xff;
- strlorabuf[9 + len] = 0;
- #if 0
- send_lora_data(strlorabuf,9+len);
- #else
- // loraSerial.write((char *)strlorabuf,len);
- lora_send_data((char *)strlorabuf, len);
- #endif
- }
- void lora_rev_cmd_send_ack()
- {
- }
- void right_screen_send()
- {
- set_screen_dis_info_and_send_queue(true, false, false, false, 100);
- }
- void user_compare(int last, int now) // 这个函数用来比较rssi和 电量变化 刷右屏//
- {
- if ((last == now) || (Machine_info.power_status == 0))
- {
- // ESP_LOGW(LOG_TAG,"last[%d]-now[%d]",last,now);
- return;
- }
- // printf("user_compare last[%d] now[%d] \n",last,now);
- bool is_send_right_screen = false;
- switch (last)
- {
- case 100:
- if (now != 100)
- {
- is_send_right_screen = true;
- }
- break;
- case 75 ... 99:
- if ((now == 100) || (now < 75))
- {
- is_send_right_screen = true;
- }
- break;
- case 50 ... 74:
- if ((now > 74) || (now < 50))
- {
- is_send_right_screen = true;
- }
- break;
- case 25 ... 49:
- if ((now > 49) || (now < 25))
- {
- is_send_right_screen = true;
- }
- break;
- case 0 ... 24:
- if ((now == 0) || (now > 24))
- {
- is_send_right_screen = true;
- }
- break;
- default:
- ESP_LOGE(LOG_TAG, "err:not 0~100");
- break;
- }
- if (is_send_right_screen)
- right_screen_send();
- }
- void user_compare_power_off(int last, int now) // 这个函数用来比较关机状态时电量变化 刷右屏//
- {
- if ((last == now))
- {
- // ESP_LOGW(LOG_TAG,"last[%d]-now[%d]",last,now);
- return;
- }
- bool is_dis = false;
- switch (last)
- {
- case 100:
- if (now != 100)
- {
- gpio_hold_dis(PIN_R_CS);
- is_dis = true;
- }
- break;
- case 75 ... 99:
- if ((now == 100) || (now < 75))
- {
- gpio_hold_dis(PIN_R_CS);
- is_dis = true;
- }
- break;
- case 50 ... 74:
- if ((now > 74) || (now < 50))
- {
- gpio_hold_dis(PIN_R_CS);
- is_dis = true;
- }
- break;
- case 25 ... 49:
- if ((now > 49) || (now < 25))
- {
- gpio_hold_dis(PIN_R_CS);
- is_dis = true;
- }
- break;
- case 0 ... 24:
- if ((now == 0) || (now > 24))
- {
- gpio_hold_dis(PIN_R_CS);
- is_dis = true;
- }
- break;
- default:
- ESP_LOGE(LOG_TAG, "err:not 0~100");
- break;
- }
- if (is_dis)
- {
- set_screen_dis_info_and_send_queue(true, false, false, false, 100);
- }
- }
|