#include "pcutc.h"
#include "modbusTCP_TC.h"
#include "modbusTCP_contex.h"
#include "interface.h"

static modbusTCP_TC_t modbusTCP_TC;

static void pcutc_GUNA_power_request(void* self, pcu_request_mode_t mode, float voltage, float current)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    modbusTCP_TC_cmd_0x83(dev, 0x1, mode, voltage, current);
    printd("mode:%d, V:%f, I:%f", mode, voltage, current);
}

static void pcutc_GUNA_Update_error_state(void* self, uint32_t GUN_Error, uint32_t GUN_State)
{

}

static void pcutc_GUNA_Update_GUN_type(void *self, gun_type_t GUN_type)
{

}

static void pcutc_GUNA_Update_K1K2_state(void *self, int K1, int K2)
{

}

static void pcutc_GUNA_Update_ELock_state(void *self, int GUN_State)
{

}

static void pcutc_GUNA_Update_MLock_state(void *self, int GUN_State)
{

}

static void pcutc_GUNA_Update_K1K2_voltate(void *self, float voltage)
{

}

static void pcutc_GUNA_Get_PCU_maxVoltateCurrent(void *self, float *maxV, float *maxI)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[0];

    if (modbusTCP_TC_cmd_0x81(dev, 0x1) == 0)
    {
        maxV[0] = ((float)slot->supportedMaxVoltage/10);
        maxI[0] = ((float)slot->supportedMaxCurrent/10);
    }
}

static void pcutc_GUNA_Get_PCU_minVoltageCurrent(void *self, float *minV, float *minI)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[0];

    if (modbusTCP_TC_cmd_0x81(dev, 0x1) == 0)
    {
        minV[0] = ((float)slot->supportedMinVoltage/10);
        minI[0] = ((float)slot->supportedMinCurrent/10);
    }
}

static void pcutc_GUNA_Get_PCU_maxPermittedPower(void *self, float *power)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[0];
    if (modbusTCP_TC_cmd_0x81(dev, 0x1) == 0)
    {
        power[0] = ((float)slot->supportedMaxPowerKW/10);
    }
}


static void pcutc_GUNA_Update_MeterEnergyPower(void *self, float energy, float power)
{

}

static void pcutc_GUNA_Update_MeterVoltageCurrent(void *self, float voltage, float current)
{

}

static void pcutc_GUNA_Update_EV_BatteryVoltage(void *self, float voltage)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[0];
    slot->batteryVoltage = (uint16_t)(voltage*10 + 0.5);
    if (modbusTCP_TC_cmd_0x82(dev, 0x1) == 0)
    {
        printv("GUNB.Update_EV_BatteryVoltage OK\n");
    }
}

static void pcutc_GUNA_Update_maxPermittedCurrent(void *self, float maxI)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[0];
    slot->permittedMaxCurrent = (uint16_t)(maxI*10 + 0.5);
    if (modbusTCP_TC_cmd_0x82(dev, 0x1) == 0)
    {
        printv("GUNA.Update_maxPermittedCurrent OK\n");
    }
}


pcu_port_t pcutcA = 
{
    .priv = &modbusTCP_TC,
    
    .update_GUN_type = pcutc_GUNA_Update_GUN_type,
    .update_GUN_error_state = pcutc_GUNA_Update_error_state,
    .update_K1K2_state = pcutc_GUNA_Update_K1K2_state,
    .update_ElecLock_state = pcutc_GUNA_Update_ELock_state,
    .update_K1K2_voltate = pcutc_GUNA_Update_K1K2_voltate,
    .update_Meter_EnergyPower = pcutc_GUNA_Update_MeterEnergyPower,
    .update_Meter_VoltageCurrent = pcutc_GUNA_Update_MeterVoltageCurrent,
    .update_EV_BatteryPower = pcutc_GUNA_Update_EV_BatteryVoltage,

    .set_GUNLine_PermittedVoltageCurrent = pcutc_GUNA_Update_maxPermittedCurrent,
    .get_PCU_maxVoltageCurrent = pcutc_GUNA_Get_PCU_maxVoltateCurrent,
    .get_PCU_minVoltageCurrent = pcutc_GUNA_Get_PCU_minVoltageCurrent,
    .get_PCU_maxPermittedPower = pcutc_GUNA_Get_PCU_maxPermittedPower,

    .request_Power = pcutc_GUNA_power_request,
};


static void pcutc_GUNB_power_request(void* self, pcu_request_mode_t mode, float voltage, float current)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    modbusTCP_TC_cmd_0x83(dev, 0x2, mode, voltage, current);
    printd("mode:%d, V:%f, I:%f\n", mode, voltage, current);
}

static void pcutc_GUNB_Update_error_state(void* self, uint32_t GUN_Error, uint32_t GUN_State)
{

}

static void pcutc_GUNB_Update_GUN_type(void *self, gun_type_t GUN_type)
{

}

static void pcutc_GUNB_Update_K1K2_state(void *self, int K1, int K2)
{

}

static void pcutc_GUNB_Update_ELock_state(void *self, int GUN_State)
{

}

static void pcutc_GUNB_Update_MLock_state(void *self, int GUN_State)
{

}

static void pcutc_GUNB_Update_K1K2_voltate(void *self, float voltage)
{

}

static void pcutc_GUNB_Get_PCU_maxVoltateCurrent(void *self, float *maxV, float *maxI)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[1];
    if (modbusTCP_TC_cmd_0x81(dev, 0x2) == 0)
    {
        maxV[0] = ((float)slot->supportedMaxVoltage)/10;
        maxI[0] = ((float)slot->supportedMaxCurrent)/10;
    }
}

static void pcutc_GUNB_Get_PCU_minVoltageCurrent(void *self, float *minV, float *minI)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[1];
    uint16_t addr0 = ((uint64_t)&slot->supportedMinVoltage) - ((uint64_t)slot);
    if (modbusTCP_TC_cmd_0x81(dev, 0x2) == 0)
    {
        minV[0] = ((float)slot->supportedMinVoltage)/10;
        minI[0] = ((float)slot->supportedMinCurrent)/10;
    }
}

static void pcutc_GUNB_Get_PCU_maxPermittedPower(void *self, float *power)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[1];
    if (modbusTCP_TC_cmd_0x81(dev, 0x2) == 0)
    {
        power[0] = ((float)slot->supportedMaxPowerKW)/10;
    }
}


static void pcutc_GUNB_Update_MeterEnergyPower(void *self, float energy, float power)
{

}

static void pcutc_GUNB_Update_MeterVoltageCurrent(void *self, float voltage, float current)
{

}

static void pcutc_GUNB_Update_EV_BatteryVoltage(void *self, float voltage)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[1];
    slot->batteryVoltage = (uint16_t)(voltage*10 + 0.5);
    if (modbusTCP_TC_cmd_0x82(dev, 0x2) == 0)
    {
        printv("GUNB.Update_EV_BatteryVoltage OK\n");
    }
}

static void pcutc_GUNB_Update_maxPermittedCurrent(void *self, float maxI)
{
    pcu_port_t *stub = (pcu_port_t*)self;
    modbusTCP_TC_t* dev = (modbusTCP_TC_t*)stub->priv;
    GUNSlot_t* slot = (GUNSlot_t*)&dev->gunSlot[1];
    slot->permittedMaxCurrent = (uint16_t)(maxI*10 + 0.5);
    if (modbusTCP_TC_cmd_0x82(dev, 0x2) == 0)
    {
        printv("GUNB.Update_maxPermittedCurrent OK\n");
    }
}

pcu_port_t pcutcB = 
{
    .priv = &modbusTCP_TC,
    
    .update_GUN_type = pcutc_GUNB_Update_GUN_type,
    .update_GUN_error_state = pcutc_GUNB_Update_error_state,
    .update_K1K2_state = pcutc_GUNB_Update_K1K2_state,
    .update_ElecLock_state = pcutc_GUNB_Update_ELock_state,
    .update_K1K2_voltate = pcutc_GUNB_Update_K1K2_voltate,
    .update_Meter_EnergyPower = pcutc_GUNB_Update_MeterEnergyPower,
    .update_Meter_VoltageCurrent = pcutc_GUNB_Update_MeterVoltageCurrent,
    .update_EV_BatteryPower = pcutc_GUNB_Update_EV_BatteryVoltage,

    .get_PCU_maxPermittedPower = pcutc_GUNB_Get_PCU_maxPermittedPower,
    .set_GUNLine_PermittedVoltageCurrent = pcutc_GUNB_Update_maxPermittedCurrent,
    .get_PCU_maxVoltageCurrent = pcutc_GUNB_Get_PCU_maxVoltateCurrent,
    .get_PCU_minVoltageCurrent = pcutc_GUNB_Get_PCU_minVoltageCurrent,
    

    .request_Power = pcutc_GUNB_power_request,
};


void pcutc_Stub_init(void)
{
    modbusTCP_TC_init(&modbusTCP_TC);
}

void pcutc_Stub_exit(void)
{
    modbusTCP_TC_exit(&modbusTCP_TC);
}