#ifndef _APP_LEAKAGE_H_
#define _APP_LEAKAGE_H_

#include "main.h"
#include "bsp_W5500.h"

#define ENABLE  (1)
#define DISABLE (0)

#define BLOCKED    (1)      /*屏蔽*/
#define UNBLOCKED  (0)

#define APP_LEAKAGE_SUB_DEVICE_STATE_LEAKAGE    (0x0001)        /*漏液状态*/
#define APP_LEAKAGE_SUB_DEVICE_STATE_OPEN       (0x0002)        /*断带状态*/
#define APP_LEAKAGE_SUB_DEVICE_STATE_TIME_OUT   (0x8000)        /*通讯超时*/

#define APP_LEAKAGE_STRING_NANE_LEN (10)
#define APP_LEAKAGE_SUB_DEVICE_NUM (32)
#define APP_LEAKAGE_SUB_DEVICE_CH_NUM (4)

#define APP_LEAKAGE_SUB_DEVICE_USE_CH_NUM (2)

void app_leakage_task(void);
void app_leakage_region_classify(void);
void history_add_alarm_record(u8 region_idx, u8 device_idx, u8 channel, u16 alarm_type, u16 leak_distance);


#define NETWORK_PORT_NUM BSP_W5500_PORT_NUM

/*子设备存储的参数*/
typedef struct
{
    u8 state;                                           /*状态 使能 非使能*/
    u8 com;                                             /*端口*/
    u8 baudrate;                                        /*波特率*/   
    u8 modbus_id;                                       /*modbus id*/
    u8 device_name[APP_LEAKAGE_STRING_NANE_LEN];                /*设备名*/
    u8 region_name[APP_LEAKAGE_STRING_NANE_LEN];                /*区域名*/
}app_leakage_sub_device_flash_data_t;

/*子设备信息*/
typedef struct
{
    app_leakage_sub_device_flash_data_t flash_data;     /*flash存储数据*/
    struct
    {
        u16 state;                                      /*状态*/
        u16 distance;                                   /*漏液距离*/
    }ch_data[APP_LEAKAGE_SUB_DEVICE_CH_NUM];            /*通道数据*/
    u8 heartbeat;                                       /*心跳包，0-59循环*/
    u8 test_mode;                                       /*测试模式，0=正常，1-4=测试对应通道*/
    u8 shield;                                          /*设备屏蔽状态*/ 
}app_leakage_sub_device_class_t;            
       

/*区域信息*/
typedef struct
{
    u8 leakage_num;                                     /*漏液数量*/
    u8 open_num;                                        /*断带数量*/
    u8 time_out_num;                                    /*通讯超时数量*/
    u8 sub_device_num;                                  /*设备总数量*/
    u8 name[APP_LEAKAGE_STRING_NANE_LEN];               /*区域名称*/
    u8 sub_device_index[APP_LEAKAGE_SUB_DEVICE_NUM];    /*设备的索引*/
}app_leakage_region_data_class_t;      

/* 网络配置结构体 */
typedef struct {
    u8 ip[4];
    u8 mask[4];
    u8 gateway[4];
    u16 port[NETWORK_PORT_NUM];
}network_config_t;

/* 历史报警记录结构 */
typedef struct __attribute__((packed))
{
    u8 region_name[APP_LEAKAGE_STRING_NANE_LEN];  /* 区域名 */
    u8 device_id;                                  /* 设备ID */
    u8 device_name[APP_LEAKAGE_STRING_NANE_LEN];   /* 设备名称 */
    u16 alarm_type;                                 /* 报警类型 */
    u8 start_time[7];                              /* 开始时间: 年(2字节)月日时分 */
    u16 leak_distance;                             /* 漏液距离 (0表示非漏液报警) */
    u8 channel;                                    /* 通道号 (0-3) */
} app_leakage_history_alarm_t;

/* 历史报警管理结构 */
typedef struct
{
    u32 total_records;                             /* 总记录数 */
    u32 write_index;                               /* 写指针 */
    u32 read_index;                                /* 读指针 */
    u32 max_records;                               /* 最大记录数 */
} app_leakage_history_metadata_t;

typedef struct
{
    u8 region_num;
    u8 sub_device_num;
    u16 alarm_state;                                 /*系统报警状态*/
    
    /*区域*/
    app_leakage_region_data_class_t region_data[APP_LEAKAGE_SUB_DEVICE_NUM];
    /*子设备*/
    app_leakage_sub_device_class_t sub_device_data[APP_LEAKAGE_SUB_DEVICE_NUM];
    /*历史报警*/
    app_leakage_history_metadata_t history_metadata;
    
    void (*init)(void);                                 /*初始化*/
    void (*task)(void);                                 /*执行任务*/
    void (*class_update)(void);                      
}app_leakage_t;

typedef struct
{
    u8 (*read_history)(u32, app_leakage_history_alarm_t *);
    void (*clean_history)(void);
    void (*init_history)(void);
}app_hitory_t;

extern app_leakage_t leakage;
extern app_hitory_t history;
#endif
