#ifndef __SECC_PORT_H__
#define __SECC_PORT_H__
#include <stdio.h>
#include <stdint.h>
#include "ccu_define.h"
#include "ccu_port.h"
#include "ccu_config.h"

#define MAX_PERMITTED_KWH         1000000

typedef struct SECC
{
    void *priv;
    void *pcu;
    void *ccu;

    GUN_config_t *config;
    void* offline_recorder;

    float CC1_voltage;
    float CP_voltage;
    float PP_voltage;

    uint32_t CP_State;
    uint32_t GUN_State;
    uint32_t GUN_Error;

    bool manul_stop;

    float GUNTemp1;
    float GUNTemp2;
    int GUNLine_state;

    float started_KWh;
    uint32_t started_UTC;

    float request_KWh;
    uint32_t request_SOC;
    uint32_t request_Minutes;

    float request_voltage;
    float request_current;
    float request_powerKW;

    float limit_DefaultPowerKW;//默认情况，限功率值
    float limit_DefaultCurrent;//默认情况，限电流值

    float limit_TxPowerKW;//针对当前交易，限功率值
    float limit_TxCurrent;//针对当前交易，限电流值

    float EVCC_maxV;
    float EVCC_maxI;
    float EVCC_maxP;

    uint8_t emaid[64];
    uint8_t evccid[64];
    
    uint32_t SOC;                   //EV端统计的已经充电百分比
    uint32_t EV_Estimate_Minutes;   //EV端统计的已经充电时间
    float EV_Charging_voltage;
    float EV_Charging_current;
    float VMeters;
    float IMeters;
    float EMeters;
    float PMeters;
    float VGround;
    float RGround;

    float SECC_maxV;
    float SECC_maxI;
    float SECC_minV;
    float SECC_minI;
    float SECC_maxP;

    int transactionId;
    
    time_t ClockAlignedDataTime;
    time_t MeterValueSampleTime;

    int (*set_K1K2_state)(void *self, int on, int timeout);
    int (*set_ELock_state)(void *self, int on, int timeout);
    int (*set_Aux12V_state)(void *self, int on);
    void (*set_IMD_state)(void *self, int on);
    void (*set_PWM_ratio)(void *self, int ratio);

    int reserve_on;
    char reserve_emaid[64];
    time_t reserve_expiryDate;
    int reservationId;

    int fp[2];
}secc_port_t;

uint32_t secc_check_CP_State(float voltage, int pwm_state);

int secc_reserveNow(void* self, char* emaid, time_t expiryDate, int reservationId);
int secc_start_Transaction(void *self);
int secc_stop_Transaction(void* self);
int secc_report_MeterValues(void* self);

int secc_init_can(void* self);
void secc_exit_can(void* self);

int secc_init_v2g(void* self);
void secc_exit_v2g(void* self);

int secc_eim_startup(secc_port_t* secc, const char* emaid, float request_KWh, uint32_t request_Minutes, uint32_t request_SOC);
int secc_eim_shutdown(secc_port_t* secc);

int secc_set_error(secc_port_t* dev, int ERRNO);
int secc_set_state(secc_port_t* dev, int STATE);
uint32_t secc_recv_error_state(secc_port_t *dev, int timeout);

int secc_should_stop(secc_port_t* dev);
int secc_request_power(void* self, int mode, float voltage, float current);

void secc_portA_bind(ccu_port_t* ccu);
void secc_portB_bind(ccu_port_t* ccu);

#endif//__SECC_PORT_H__
