#pragma once

#include "myBase.h"
#include "iec.h"
#include "lib60870_common.h"

#include<vector>

#define  SOE_LEN          300  //SOE记录缓存区大小
#define  DISTURB_YC_LEN   200  //扰动遥测缓存区大小
#define  FAULT_EVENT_LEN  200  // 故障事件缓冲大小
#define  FAULT_YC_NUM_MAIN 8   // 故障遥测数量
#define  Dd_LEN           50   // 电能量缓冲大小

/*
* 遥信配置表   内存对齐后增加1个字节  共4个字节
*/
typedef struct
{
    uint16_t usInfoAddr;    //信息体地址
    uint8_t  ucAttr;        //属性
    uint16_t usShake;       //遥信防抖时间 单位ms
}stYX_TABLE;


/*
* 遥测配置表  内存对齐后增加3个字节 共16个字节
*/
typedef struct
{
    uint16_t usInfoAddr;     /*信息体地址*/
    float    fScale;         /*系数*/
    uint8_t  ucAttr;         /*属性*/
    uint16_t usDeadVal;      /*死区值*/
    float    fLimitVal;      /*上限值*/
}stYC_TABLE;

/**  
* @brief  单条YC记录结构
*/
typedef struct
{
    uint16_t usIndex;  // 序号
    float fVal;
}stYC_RECORD;


/**
* @brief  单条缓冲区出指针与通道规约关联信息
*/
typedef struct
{

    uint8_t  ucPrtocol;     /*规约类型*/
    uint16_t usOutNum;
    uint32_t uiChannelType; /*通信类型*/
}stINFOR_OUT;

typedef struct
{
    bool             isInit;
    sem_t            sem;
    uint16_t         usInptr; /*入指针*/
    std::vector<stINFOR_OUT> astOutptr;
    stYC_RECORD astYc[DISTURB_YC_LEN];
}stDISTURB_YC_AVR;


typedef struct
{
    uint8_t  ucState;  //遥信状态
    uint8_t  ucType;   //单点双点
    // uint16_t usChannel;//遥信点表
    uint16_t usYxPot;  //遥信总表中的下标值
    uint16_t usMs;     //SOE时标 毫秒
    uint32_t uiSeconds;//SOE时标 秒
    // float    fValueI;  //故障电流值
    // float    fValueU;  //故障电压值
}stYX_CHANGE_INFO;

typedef struct
{
    bool             isInit;
    sem_t            sem;
    uint16_t              usInptr;                                 //入指针
    std::vector<stINFOR_OUT> astOutptr;
    stYX_CHANGE_INFO astSoeInfo[SOE_LEN];                 //soe信息
}stSOE_INFORM;


typedef struct 
{
    uint8_t ucLMs;        /*毫秒低位*/
    uint8_t ucHMs;        /*毫秒高位*/
    uint8_t ucMin;        /*分钟*/
    uint8_t ucHour;       /*小时*/    
    uint8_t ucWeek;       /*week: 0-不用,n-星期n */
    uint8_t ucMonth;      /*月*/
    uint8_t ucYear;       /*年*/
} stCP56Time2a;

/**
 * @brief SOE 记录信息
 */
typedef struct 
{
    uint32_t           uiInfoAddr;  /*信息体地址*/
    uint8_t            ucStatus;    /*状态位*/
    uint8_t            ucStateType; /*单点双点*/
    stCP56Time2a  stCTime;     /*时间*/
    uint16_t           usMs;        /*SOE时标 毫秒*/
    uint32_t           uiSeconds;   /*SOE时标 秒*/
}stSOEInfo;

/**
 * @brief Yc信息
 */
typedef struct 
{
    uint32_t    uiInfoAddr;     /*信息体地址*/
    float    fVal;           /*遥测值*/
}stYcInfo;

/**
* @brief  单条故障事件结构体(通信板)
*/
typedef struct
{
    uint8_t        ucYxNum;               // YX数量
    stSOEInfo stSoe;

    uint8_t        ucYcNum;               // YC数量
    stYcInfo  astYc[FAULT_YC_NUM_MAIN];
}stFAULT_EVENT_MAIN;

/**
* @brief  故障事件缓存区(通信板)
*/
typedef struct
{
    bool             isInit;
    sem_t            sem;
    uint16_t                usInptr; /*入指针*/
    std::vector<stINFOR_OUT> astOutptr;
    stFAULT_EVENT_MAIN astFault[FAULT_EVENT_LEN];
}stFAULT_INFORM_MAIN;



int GetTimeItfs(__CP56Time2a *pTime);
int SetTimeItfs(__CP56Time2a *pTime);

uint8_t Cs10xReadFileResetCOI(void);
void Cs10xWriteFileResetCOI(uint8_t ucCoi);

int32_t GetYcCountByGroup(uint16_t usGroupID, void *pvParam, uint32_t *puiStartInf, uint32_t *puiEndInf);
int32_t GetYcValueByGroup(uint16_t usGroupID, uint16_t usIdx, Yc_Info *pstInfo, void *pvParam);

int32_t GetDiturbYc(Yc_Info *pstInfo, void *pvParam, uint32_t uiPos);
int32_t GetDiturbYcSendNum(void *pvArg);
int32_t UpdateDiturbYcPtrOut(void *pvParam, uint32_t uiPos);

int32_t GetYxCountByGroup(uint16_t usGroupID, void *pvParam, uint32_t *puiStartInf, uint32_t *puiEndInf);
int32_t GetYxStatusByGroup(uint16_t usGroupID, uint16_t usIdx, Yx_Info *pstInfo, void *pvParam);

int32_t GetSOE(SOE_Info *pstInfo, void *pvParam, uint32_t uiPos);
int32_t GetSOESendNum(void *pvArg);
int32_t UpdateSoePtrOut(void *pvParam, uint32_t uiPos);

int32_t GetDdCountByGroup(uint16_t usGroupID, void *pvParam, uint32_t *puiStartInf, uint32_t *puiEndInf, uint8_t ucTimeEn);
int32_t GetDdValueByGroup(uint16_t usGroupID, uint16_t usIdx, TimeDd_Info *pstInfo, void *pvParam, uint8_t ucTimeEn);


int32_t GetEventSendNum(void *pvArg);
int32_t GetFaultEvent(FAULT_EVENT_Info *pstInfo, void *pvParam, uint32_t uiPos);
int32_t UpdateFaultEventPtrOut(void *pvParam, uint32_t uiPos);

