#include "../pe_adapter.h"
#include <string.h>

int coepo_pcs_init(pe_class* this_p, void* par)
{
    return 0;
}

int coepo_pcs_set_power(pe_class *this_p, double power)
{
    dev_set_dev_tag_float(this_p->no, "OutPowerSet", power);
    return 0;
}

int coepo_pcs_set_repower(pe_class *this_p, double repower)
{
    dev_set_dev_tag_float(this_p->no, "OutReactivePower", repower);
    return 0;
}

int coepo_pcs_set_mode(pe_class *this_p, int mode)
{
    dev_set_dev_tag_int(this_p->no, "WorkMode", mode);
    return 0;
}

int coepo_pcs_set_onoff(pe_class *this_p, int onoff)
{
    dev_set_dev_tag_int(this_p->no, "StopCtr", onoff);
    return 0;
}
int coepo_pcs_set_connect(pe_class *this_p, int connect)
{
    dev_set_dev_tag_int(this_p->no, "setConnect", (connect == PE_ADP_CONNECT_GRID) ? 0 : 1);
    return 0;
}

int coepo_pcs_get_power(pe_class *this_p, double *power)
{
    *power = dev_get_dev_tag_int(this_p->no, "OutPowerSet");
    return 0;
}

int coepo_pcs_get_repower(pe_class *this_p, double *repower)
{
    *repower = dev_get_dev_tag_int(this_p->no, "OutReactivePower");
    return 0;
}

int coepo_pcs_get_mode(pe_class *this_p, int *mode)
{
    *mode = dev_get_dev_tag_int(this_p->no, "WorkMode");
    return 0;
}

int coepo_pcs_get_onoff(pe_class *this_p, int *onoff)
{
    int re = 0;
    int val = dev_get_dev_tag_int(this_p->no, "StopCtr");
    switch (val)
    {
    case 0:
        *onoff = PE_ADP_OFF;
        break;

    case 1:
        *onoff = PE_ADP_ON;
        break;

    default:
        re = -1;
        break;
    }
    return re;
}

int coepo_pcs_get_connect(pe_class *this_p, int *connect)
{
    *connect = dev_get_dev_tag_int(this_p->no, "setConnect");
    return 0;
}


int coepo_pcs_set_value(pe_class* this_p, const char* function ,double value)
{
    if (strcmp(function, FUNCTION_SET_POWER) == 0)
    {
        return coepo_pcs_set_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_REPOWER) == 0)
    {
        return coepo_pcs_set_repower(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_MODE) == 0)
    {
        return coepo_pcs_set_mode(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_ONOFF) == 0)
    {
        return coepo_pcs_set_onoff(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_CONNECT) == 0)
    {
        return coepo_pcs_set_connect(this_p, (int)value);
    }
    else
    {
        return adapter_function_set(this_p, function, value);
    }
    return -1;
}

int coepo_pcs_get_int_value(pe_class* this_p, const char* function, int* value)
{
    if (strcmp(function, FUNCTION_GET_ONOFF) == 0)
    {
        return coepo_pcs_get_onoff(this_p, value);
    }
    else if (strcmp(function, FUNCTION_GET_MODE) == 0)
    {
        return coepo_pcs_get_onoff(this_p, value);
    }
    else if (strcmp(function, FUNCTION_GET_CONNECT) == 0)
    {
        return coepo_pcs_get_onoff(this_p, value);
    }
    else
    {
        return adapter_function_get_int(this_p, function, value);
    }
    return -1;
}

int coepo_pcs_get_double_value(pe_class* this_p, const char* function, double* value)
{
    if (strcmp(function, FUNCTION_GET_POWER) == 0)
    {
        return coepo_pcs_get_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_GET_REPOWER) == 0)
    {
        return coepo_pcs_get_repower(this_p, value);
    }
    else
    {
        return adapter_function_get_double(this_p, function, value);
    }
    return -1;
}

static char* coepo_pcs_compatible_arr[] = {"coepo", NULL};

pe_adapter coepo_pcs_adp __at_pe_adp_section = {
    .compatible = coepo_pcs_compatible_arr, // 属性
    .init = coepo_pcs_init,
    
    .set_power = coepo_pcs_set_power,
    .set_repower = coepo_pcs_set_repower,
    .set_mode = coepo_pcs_set_mode,
    .set_onoff = coepo_pcs_set_onoff,
    .set_connect = coepo_pcs_set_connect,
    //
    .get_power = coepo_pcs_get_power,
    .get_repower = coepo_pcs_get_repower,
    .get_mode = coepo_pcs_get_mode,
    .get_onoff = coepo_pcs_get_onoff,
    .get_connect = coepo_pcs_get_connect,

    .set_value   = coepo_pcs_set_value,
    .get_int_value = coepo_pcs_get_int_value,
    .get_double_value = coepo_pcs_get_double_value,
};
