#ifndef _YC_PROTOCOL_H_
#define _YC_PROTOCOL_H_


#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "user_config.h"

#include "LORA.h"
#include "SPIFFS.h"
#include "GUI_Paint.h"
#include "../components/USER_SPIFFS/include/SPIFFS.h"

extern LORA_DATA_T lora_data;

//睡眠时间
#define SLEEP_TIMES    3000  //3s
//工作时间
#define WORK_TIMES     100   //100ms

typedef unsigned char u8;


#define DEVOP_TEST 0

#define VERSION_X    3
#define VERSION_Y   2
#define VERSION_Z   1





// //------right char 3---------
// extern uint32_t num_goodProducts[8];
// extern uint32_t num_badProducts[8];

// extern uint8_t scale_UR_int[8];
// extern uint8_t scale_UR_dec[8];
// extern uint8_t scale_YR_int[8];
// extern uint8_t scale_YR_dec[8];

// extern uint16_t num_manHour[8];
// extern uint16_t num_people[8];

// extern uint16_t num_production[8];
// extern uint16_t num_repair[8];
// extern uint16_t num_inspection[8];
// extern uint16_t num_upkeep[8];
// //-----------------------------

typedef enum
{
   KEY_CURRENT_STATUS_CMD  = 0, 
   KEY_NEW_STATUS_CMD      = 1,
};




typedef enum 
{
    PROTOCOL_NONE = 0x00,

    PROTOCOL_TIME_SYNCHRONIZATION = 0x01,
    PROTOCOL_TERMINAL_NAME = 0x02,
    PROTOCOL_TERMINAL_NUMBER = 0x03,
    PROTOCOL_ELECTRIC_QUANTITY = 0x04,
    PROTOCOL_SIGNAL_STRENGTH = 0x05,
    PROTOCOL_SCREEN_REFRESH_CYCLE = 0x06,
    PROTOCOL_SCREEN_LEFT = 0x07,
    PROTOCOL_SCREEN_RIGHT = 0x08,
    PROTOCOL_RSPONSIBLE_PERSON_CHANGE = 0x09,
    PROTOCOL_RSPONSIBLE_PERSON_DELETE = 0x0A,
    PROTOCOL_PERSONNEL_CHECK_IN = 0x0C,
    PROTOCOL_PRODUCTION_PUNCH_CARD = 0x0B,
    PROTOCOL_STATUS_SETTING = 0x0D,
    PROTOCOL_STATUS_DURATION = 0x0E,
    PROTOCOL_UPDATE_AND_DURATION = 0x0F,
    PROTOCOL_ANNOUNCEMENT = 0x10,
    PROTOCOL_CAPACITY_STATISTICS = 0x11,
    PROTOCOL_RESPONSIBLE_PERSON_SYNCHRONIZE_INFO = 0x12,
    PROTOCOL_STATUS_LED = 0x13,
    PROTOCOL_LOG_LED =0x14,
    PROTOCOL_BATTERY_TEMPERATURE = 0x15,

    PROTOCOL_STATION_NAME = 0X16,
    PROTOCOL_STATION_NUMBER = 0X17,
    PROTOCOL_RESPONSIBLE_PERSON_SETUP= 0X18,
    PROTOCOL_SYSTEM_BULLETIN = 0X19,
    PROTOCOL_HARDWARE_UPDATE = 0x27,

    PROTOCOL_HARDWARE_GWPAIRED = 0x80,  //硬件配网

    PROTOCOL_HARDWARE_UNGWPAIRED = 0x81,  //硬件取消配网

    PROTOCOL_RSPONSIBLE_ACK = 0x99,      //ACK

    PROTOCOL_END,
}COMMUNICATION_PROTOCOL_T;


typedef struct {
    uint8_t time_synchronization[8];
    uint8_t terminal_name[32];//unicode
    uint8_t terminal_number[16];
    uint8_t station_name[32];
    uint8_t station_number[16];
    uint8_t batt_percent;
    uint8_t rssi;
    uint8_t refresh_cycle;
    uint8_t left_epd;
    uint8_t right_epd;
    uint8_t rsponsible_person_change[32];
    uint8_t rsponsible_person_delete[32];
    uint8_t production_punch[16];
    uint8_t personnel_check_in[32];
    uint8_t status_setting[10];//别名最多2个汉字
    uint8_t status_duration[32];
    uint8_t update_and_duration[32];
    uint8_t announcement[120];
    uint8_t systemMessage[120];
    uint8_t capacity_statistics[200];
    uint8_t responsible_person_synchronize_info[32];
    uint8_t states_led;
    uint8_t logo_led;
    uint8_t batt_temperature[2];
}TERMINAL_INFO_T;

typedef enum
{
    CHARGE_administrator                =0,                //
    CHARGE_product                    = 1,               //生产责任人
    CHARGE_repair                     = 2,               //维修责任人
    CHARGE_maintenance                  = 3,               //保养责任人
    CHARGE_check                     = 4,               //巡检责任人
    CHARGE_END,
}PERSON_CHARGE_TYPE_T;

#if 0
typedef struct
{
    PERSON_CHARGE_TYPE_T  person_type;  //人员类型
    uint8_t             person_serial_number;
    uint32_t            person_total;
    uint8_t             person_name[20];
}Person_t;
#endif
typedef enum
{
    checkIn_product                    = 0,               //生产
    checkIn_repair                     = 1,               //维修
    checkIn_maintenance                = 2,               //保养
    checkIn_check                      = 3,               //巡检
}PERSON_CHECK_IN_TYPE_T;

typedef struct
{
    PERSON_CHARGE_TYPE_T        person_type;  //人员类型
    uint8_t                     person_serial_number[17];
    PAINT_TIME                  cur_time;
}CHECK_IN_T;


#if 1

// #define PERSON_MAX_NAME      5
// #define PERSON_NAME_MAX_LEN  20
// typedef enum
// {
//       Administrator                     = 0,               //管理员
//       product_person                    = 1,               //生产责任人
//       repair_person                     = 2,               //维修责任人
//       Maintenance_person                = 3,               //保养责任人
//       check_person                      = 4,               //巡检责任人

// }Person_type_t;

// typedef struct
// {
//     uint16_t   person_num;  //序号
//     uint8_t person_name[PERSON_NAME_MAX_LEN]; //人员名称
// }Person_Name_t;
// ///////////////////////////////////////////
// typedef struct
// {
//     uint8_t    person_type;  //人员类型
//     Person_Name_t  person_name[PERSON_MAX_NAME];
//     char string_name[100];
// }Person_t;

// // extern Person_t;
// // extern Person_Name_t;
// // extern Person_type_t;




void selectionSort(int arr[], int n, int indices[]);
int  person_get_num_is_exist(uint8_t type,uint16_t num);
int  person_get_num(uint8_t type);
void person_add(uint8_t type,uint8_t num,char* in,char *out);
void person_del(uint8_t type,uint8_t num,char* in,char *out);





#endif

typedef struct
{
    uint8_t cmd;
    uint8_t len;

    uint16_t Year;
    uint8_t Month;
    uint8_t Day;
    uint8_t Hour;
    uint8_t Minute;
    uint8_t Second;

    uint8_t time[20];

}Current_Time_t;
typedef struct
{
    uint8_t cmd;
    uint8_t len;
    uint8_t mac_addr[20];   //16进制mac地址 中间: 分割  0x3A

}Mac_t;


typedef struct
{
    uint8_t cmd;
    uint8_t len;
    uint8_t client_id[20];  //设备id

}Client_t;

typedef struct
{
    uint8_t cmd;
    uint8_t len;
    uint8_t product[20];  //产品型号设备

}Product_Device_t;
typedef struct
{
    uint8_t head[2];
    uint8_t CRC;
    Mac_t  mac ;                //0x03
    Client_t client;            //0x04
    Product_Device_t  product;  //0x05

    Current_Time_t time;        //0x61

}Qrcode_protocol_t;


typedef struct
{
    bool is_complete;
    uint8_t *data;
    // uint8_t data[1024];
    int len;
    int index;
}YC_DATA_T;


#define USE_DATA_LEN_INDEX              15
#define USE_CMD_LEN_INDEX               12
#define USELESS_DATA_LEN                17
#define DATA_START_LEN              (USELESS_DATA_LEN)



void timer_init(void);
void lora_timer_start(void);
void lora_timer_restart(void);
void lora_tiemr_stop(void);
void adc1_init(void);
void print_systenInfo(void);
void yc_timer_restart(void);
// void periodic_timer_callback(void* arg);
void print_yc_data(const LORA_DATA_T* buf);
void business_logic_func(const LORA_DATA_T* buf,uint8_t cmd_index,int msg);
void screen_display(void);
bool filtered_data(const LORA_DATA_T* buf);

void uincode2gbk(char *str,uint16_t len,char *out_buffer);
void qrcode_protocol_create(char *result,Machine_info_t info);
bool subcontract(YC_DATA_T* data);

void send_rssi(void);
void send_status(void);
void send_ACK(char cmd,bool flag);


void loro_gwpair_ack(int destaddr,int sourceaddr,int cmd,int st,char *strd);


//typedef unsigned char u8;
int findByteSequence(const u8* array, int arraySize, const u8* sequence, int sequenceSize, int** positions);
void  analysis_protocol(char *result,int len);


int reply_ack_func(int cmd,int msg_id,bool is_send,uint8_t *result);


void send_can_I_receive();

void send_pair_ack();


void terminal_send_data(void);
void update_last_button_info(uint8_t btn);
void f_send_version(void);
void f_send_get_chart_data(void);
void f_send_lora_rssi(int rssi);
void f_send_battary_vaule(int battry);
void f_send_reply_status(int status);


void set_status_heights(void);
void user_compare(int last,int now);

void user_compare_power_off(int last,int now);//这个函数用来比较关机状态时电量变化 刷右屏//

void right_screen_send();

// /* 链表节点结构体 */
// struct ListNode {
//     uint16_t number;               // 节点值
//     uint8_t type;
//     char* name;
    
//     struct ListNode *next; // 指向下一节点的引用
// };
// typedef struct ListNode ListNode;
// ListNode *newListHead(void);
// void printList(ListNode *head);
#endif/*_YC_PROTOCOL_H_*/
