#include "../adapter/pe_adapter.h"
#include <string.h>
#include "../common.h"
// PCS
//
#define YINGBO_POWER_SET           "ActPowSet"
#define YINGBO_POWER_READ          "ActivePower"
#define YINGBO_REPOWER_SET         "ReactPowSet"
#define YINGBO_REPOWER_READ        "ReactivePower"

#define YINGBO_A_POWER_SET         "ASplitPhaseActPowSet"
#define YINGBO_B_POWER_SET         "BSplitPhaseActPowSet"
#define YINGBO_C_POWER_SET         "CSplitPhaseActPowSet"
#define YINGBO_A_POWER_READ        "AphaseActPower"
#define YINGBO_B_POWER_READ        "BphaseActPower"
#define YINGBO_C_POWER_READ        "CphaseActPower"
#define YINGBO_A_REPOWER_SET       "ASplitPhaseReactPowSet"
#define YINGBO_B_REPOWER_SET       "BSplitPhaseReactPowSet"
#define YINGBO_C_REPOWER_SET       "CSplitPhaseReactPowSet"
#define YINGBO_A_REPOWER_READ      "AphaseReactivePower"
#define YINGBO_B_REPOWER_READ      "BphaseReactivePower"
#define YINGBO_C_REPOWER_READ      "CphaseReactivePower"

#define YINGBO_MODE_SET            "WorkMode"
#define YINGBO_MODE_READ           "RunStatu"
#define YINGBO_GRID_CONNECT_SET    "SetConnect"
#define YINGBO_GRID_CONNECT        "GridOnOff"
#define YINGBO_UP_SET              "PCSStart"
#define YINGBO_DOWN_SET            "PCSStop"
#define YINGBO_ONOFF_READ          "Stoppage"    //停机

enum grid_onff
{
    GRID_OFF = 0,
    GRID_ON,
};

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

int yingbo_pcs_set_power(pe_class *this_p, double power)
{

    dev_set_dev_tag_float(this_p->no, YINGBO_A_POWER_SET, power/3);
    dev_set_dev_tag_float(this_p->no, YINGBO_B_POWER_SET, power/3);
    dev_set_dev_tag_float(this_p->no, YINGBO_C_POWER_SET, power/3);
    return 0;
}

int yingbo_pcs_set_repower(pe_class *this_p, double repower)
{

    dev_set_dev_tag_float(this_p->no, YINGBO_A_REPOWER_SET, repower/3);
    dev_set_dev_tag_float(this_p->no, YINGBO_B_REPOWER_SET, repower/3);
    dev_set_dev_tag_float(this_p->no, YINGBO_C_REPOWER_SET, repower/3);
    return 0;
}

int yingbo_pcs_set_mode(pe_class *this_p, int mode)
{
    dev_set_dev_tag_int(this_p->no, YINGBO_MODE_SET, mode);
    return 0;
}

int yingbo_pcs_set_onoff(pe_class *this_p, int onoff)
{
    switch (onoff)
    {
        case PE_ADP_OFF:
        dev_set_dev_tag_int(this_p->no, YINGBO_DOWN_SET, true);
        break;
        case PE_ADP_ON:
        dev_set_dev_tag_int(this_p->no, YINGBO_UP_SET, true);
        break;
    }
    return 0;
}
int yingbo_pcs_set_connect(pe_class *this_p, int connect)
{
    if (PE_ADP_CONNECT_GRID == connect)
        dev_set_dev_tag_int(this_p->no, YINGBO_GRID_CONNECT_SET, GRID_ON);
    if (PE_ADP_DISCONNECT_GRID == connect)
        dev_set_dev_tag_int(this_p->no, YINGBO_GRID_CONNECT_SET, GRID_OFF);
    return 0;
}

int yingbo_pcs_set_fault_rst(pe_class *this_p, int rst)
{
    if(rst == 1)
    {
        dev_set_dev_tag_int(this_p->no, YINGBO_DOWN_SET, true);
        dev_set_dev_tag_int(this_p->no, "FaultRst", rst);
    }
    return 0;
}

int yingbo_pcs_get_power(pe_class *this_p, double *power)
{
    *power = dev_get_dev_tag_int(this_p->no, YINGBO_POWER_READ);
    return 0;
}

int yingbo_pcs_get_repower(pe_class *this_p, double *repower)
{
    *repower = dev_get_dev_tag_int(this_p->no, YINGBO_REPOWER_READ);
    return 0;
}

int yingbo_pcs_get_mode(pe_class *this_p, int *mode)
{
    *mode = dev_get_dev_tag_int(this_p->no,YINGBO_MODE_READ);
    return 0;
}

int yingbo_pcs_get_onoff(pe_class *this_p, int *onoff)
{
    if (dev_get_dev_tag_int(this_p->no,YINGBO_ONOFF_READ))
    {
        *onoff = 0;
    }
    else
    {
        *onoff = 1;
    }
    return 0;
}

int yingbo_pcs_get_connect(pe_class *this_p, int *connect)
{
    if (dev_get_dev_tag_int(this_p->no, YINGBO_GRID_CONNECT))
    {
        *connect = PE_ADP_CONNECT_GRID;
    }
    else
    {
        *connect = PE_ADP_DISCONNECT_GRID;
    }
    return 0;
}

int yingbo_pcs_set_value(pe_class* this_p, const char* function, double value)
{
    if (strcmp(function, FUNCTION_SET_POWER) == 0)
    {
        return yingbo_pcs_set_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_REPOWER) == 0)
    {
        return yingbo_pcs_set_repower(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_MODE) == 0)
    {
        return yingbo_pcs_set_mode(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_ONOFF) == 0)
    {
        return yingbo_pcs_set_onoff(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_CONNECT) == 0)
    {
        return yingbo_pcs_set_connect(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_FAULT_RST) == 0)
    {
        return yingbo_pcs_set_fault_rst(this_p, (int)value);
    }
    else
    {
        return adapter_function_set(this_p, function, value);
    }
    return -1;
}

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

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

static char *yingbo_pcs_compatible_arr[] = {"yingbo",NULL};

pe_adapter yingbo_pcs_adp __at_pe_adp_section = {
    .compatible = yingbo_pcs_compatible_arr, // 属性
    //
    .init = yingbo_pcs_init,
    //
    .set_power = yingbo_pcs_set_power,
    .set_repower = yingbo_pcs_set_repower,
    .set_mode = yingbo_pcs_set_mode,
    .set_onoff = yingbo_pcs_set_onoff,
    .set_connect = yingbo_pcs_set_connect,
    .set_fault_rst = yingbo_pcs_set_fault_rst,
    //
    .get_power = yingbo_pcs_get_power,
    .get_repower = yingbo_pcs_get_repower,
    .get_mode = yingbo_pcs_get_mode,
    .get_onoff = yingbo_pcs_get_onoff,
    .get_connect = yingbo_pcs_get_connect,

    .set_value        = yingbo_pcs_set_value,
    .get_int_value    = yingbo_pcs_get_int_value,
    .get_double_value = yingbo_pcs_get_double_value,
};
