#include "../adapter/pe_adapter.h"
#include <string.h>
#include "../common.h"
// PCS
//

int enjoy_pcs_init(pe_class *this_p, void *par)
{
    return 0;
}

int enjoy_pcs_set_power(pe_class *this_p, double power)
{
    dev_set_dev_tag_float(this_p->no, "ActPowSet", power);
    return 0;
}

int enjoy_pcs_set_repower(pe_class *this_p, double repower)
{
    dev_set_dev_tag_float(this_p->no, "ReactPowSet", repower);
    return 0;
}

int enjoy_pcs_set_mode(pe_class *this_p, int mode)
{
    dev_set_dev_tag_int(this_p->no, "WorkMode", mode);
    return 0;
}

int enjoy_pcs_set_onoff(pe_class *this_p, int onoff)
{
    
    int re = 0;
    if ((dev_get_dev_tag_int(this_p->no, "Online") != 0) && (dev_get_dev_tag_int_with_result(this_p->no, "SetGridModel", &re) == 0) && (re == 0))
    {
        switch (onoff)
        {
        case PE_ADP_OFF:
            dev_set_dev_tag_int(this_p->no, "StartStopCtr", 0);
            break;

        case PE_ADP_ON:
            dev_set_dev_tag_int(this_p->no, "StartStopCtr", 1);
            break;

        default:
            re = -1;
            break;
        }
    }
    else
    {
        ems_syslog(LOG_ERR, "PCS[no:%s] is off grid,so can not power on", this_p->no);
        re = -9999;
    }
    return re;
}
int enjoy_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 enjoy_pcs_get_power(pe_class *this_p, double *power)
{
    *power = dev_get_dev_tag_int(this_p->no, "ActPowSet");
    return 0;
}

int enjoy_pcs_get_repower(pe_class *this_p, double *repower)
{
    *repower = dev_get_dev_tag_int(this_p->no, "ReactPowSet");
    return 0;
}

int enjoy_pcs_get_mode(pe_class *this_p, int *mode)
{
    *mode = dev_get_dev_tag_int(this_p->no, "WorkMode");
    return 0;
}

int enjoy_pcs_get_onoff(pe_class *this_p, int *onoff)
{
    int re = 0;
    int val = dev_get_dev_tag_int(this_p->no, "StartStopCtr");
    switch (val)
    {
    case 0:
        *onoff = PE_ADP_OFF;
        break;

    case 1:
        *onoff = PE_ADP_ON;
        break;

    default:
        re = -1;
        break;
    }
    return re;
}

int enjoy_pcs_get_connect(pe_class *this_p, int *connect)
{
    *connect = dev_get_dev_tag_int(this_p->no, "setConnect");
    return 0;
}

int enjoy_pcs_set_value(pe_class* this_p, const char* function, double value)
{
    if (strcmp(function, FUNCTION_SET_POWER) == 0)
    {
        return enjoy_pcs_set_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_REPOWER) == 0)
    {
        return enjoy_pcs_set_repower(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_MODE) == 0)
    {
        return enjoy_pcs_set_mode(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_ONOFF) == 0)
    {
        return enjoy_pcs_set_onoff(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_CONNECT) == 0)
    {
        return enjoy_pcs_set_connect(this_p, (int)value);
    }
    else
    {
        return adapter_function_set(this_p, function, value);
    }
    return -1;
}

int enjoy_pcs_get_int_value(pe_class* this_p, const char* function, int* value)
{
    if (strcmp(function, FUNCTION_GET_ONOFF) == 0)
    {
        return enjoy_pcs_get_onoff(this_p, value);
    }
    else if (strcmp(function, FUNCTION_GET_MODE) == 0)
    {
        return enjoy_pcs_get_onoff(this_p, value);
    }
    else if (strcmp(function, FUNCTION_GET_CONNECT) == 0)
    {
        return enjoy_pcs_get_onoff(this_p, value);
    }
    else
    {
        return adapter_function_get_int(this_p, function, value);
    }
    return -1;
}

int enjoy_pcs_get_double_value(pe_class* this_p, const char* function, double* value)
{
    if (strcmp(function, FUNCTION_GET_POWER) == 0)
    {
        return enjoy_pcs_get_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_GET_REPOWER) == 0)
    {
        return enjoy_pcs_get_repower(this_p, value);
    }
    else
    {
        return adapter_function_get_double(this_p, function, value);
    }
    return -1;
}

static char *enjoy_pcs_compatible_arr[] = {"enjoy", NULL};

pe_adapter enjoy_pcs_adp __at_pe_adp_section = {
    .compatible = enjoy_pcs_compatible_arr, // 属性
    //
    .init = enjoy_pcs_init,
    //
    .set_power   = enjoy_pcs_set_power,
    .set_repower = enjoy_pcs_set_repower,
    .set_mode    = enjoy_pcs_set_mode,
    .set_onoff   = enjoy_pcs_set_onoff,
    .set_connect = enjoy_pcs_set_connect,
    //
    .get_power   = enjoy_pcs_get_power,
    .get_repower = enjoy_pcs_get_repower,
    .get_mode    = enjoy_pcs_get_mode,
    .get_onoff   = enjoy_pcs_get_onoff,
    .get_connect = enjoy_pcs_get_connect,

    .set_value        = enjoy_pcs_set_value,
    .get_int_value    = enjoy_pcs_get_int_value,
    .get_double_value = enjoy_pcs_get_double_value,
};
