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


int sinexcel_pcs_set_onoff(pe_class *this_p, int onoff);
int sinexcel_pcs_get_onoff(pe_class *this_p, int *onoff);

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

int sinexcel_pcs_set_power(pe_class *this_p, double power)
{
    //用开机指令消除关机指令
    dev_set_dev_tag_float(this_p->no, "ActPowSet", power);
    return 0;
}

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

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

int sinexcel_pcs_set_onoff(pe_class *this_p, int onoff)
{
    if (PE_ADP_OFF == onoff)
    {
        dev_set_dev_tag_int(this_p->no,"TotalStop",1);
    }
    else if (PE_ADP_ON == onoff)
    {
        dev_set_dev_tag_int(this_p->no,"TotalStart",1);
    }
    else
    {
        return -1;
    }
    return 0;
}
int sinexcel_pcs_set_connect(pe_class *this_p, int connect)
{

    return 0;
}

int sinexcel_pcs_get_power(pe_class *this_p, double *power)
{
    *power = dev_get_dev_tag_int(this_p->no, "ActPowSet");
    return 0;
}

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

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

int sinexcel_pcs_get_onoff(pe_class *this_p, int *onoff)
{

    if(dev_get_dev_tag_int(this_p->no,"RunStatu") == 1)
    {
        *onoff = PE_ADP_ON;
    }
    else
    {
        *onoff = PE_ADP_OFF;
    }
    
    return 0;
}

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

int sinexcel_pcs_set_value(pe_class* this_p, const char* function, double value)
{
    if (strcmp(function, FUNCTION_SET_POWER) == 0)
    {
        return sinexcel_pcs_set_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_REPOWER) == 0)
    {
        return sinexcel_pcs_set_repower(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_MODE) == 0)
    {
        return sinexcel_pcs_set_mode(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_ONOFF) == 0)
    {
        return sinexcel_pcs_set_onoff(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_CONNECT) == 0)
    {
        return sinexcel_pcs_set_connect(this_p, (int)value);
    }
    else
    {
        return adapter_function_set(this_p, function, value);
    }
    return -1;
}

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

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

static char *sinexcel_pcs_compatible_arr[] = {"sinexcel", NULL};

pe_adapter sinexcel_pcs_adp __at_pe_adp_section = {
    .compatible = sinexcel_pcs_compatible_arr, // 属性
    //
    .init = sinexcel_pcs_init,
    //
    .set_power = sinexcel_pcs_set_power,
    .set_repower = sinexcel_pcs_set_repower,
    .set_mode = sinexcel_pcs_set_mode,
    .set_onoff = sinexcel_pcs_set_onoff,
    .set_connect = sinexcel_pcs_set_connect,
    //
    .get_power = sinexcel_pcs_get_power,
    .get_repower = sinexcel_pcs_get_repower,
    .get_mode = sinexcel_pcs_get_mode,
    .get_onoff = sinexcel_pcs_get_onoff,
    .get_connect = sinexcel_pcs_get_connect,

    .set_value        = sinexcel_pcs_set_value,
    .get_int_value    = sinexcel_pcs_get_int_value,
    .get_double_value = sinexcel_pcs_get_double_value,
};
