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

enum inovance_onoff 
{
    stopped,
    waited,
    dischg,
    chg,
    zero_run

};

int inovance_pcs_get_onoff(pe_class *this_p, int *onoff);
//启动流程猜测，启动，待机
void check_open_status(pe_class *this_p,int onoff)
{
    inovance_pcs_get_onoff(this_p,&onoff);
    if(stopped == onoff)
    {
        dev_set_dev_tag_int(this_p->no,"StartCtr",1);
        dev_set_dev_tag_int(this_p->no,"StandbyCtr",1);
    }
}

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

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

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

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

int inovance_pcs_set_onoff(pe_class *this_p, int onoff)
{
    dev_set_dev_tag_int(this_p->no,"StartCtr",onoff);
    return 0;
}

//设置并离网
int inovance_pcs_set_connect(pe_class *this_p, int connect)
{

    return 0;
}

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

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

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


int inovance_pcs_get_onoff(pe_class *this_p, int *onoff)
{
    *onoff = dev_get_dev_tag_int(this_p->no,"RunStatu");
    return 0;
}

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

int inovance_pcs_set_value(pe_class* this_p, const char* function, double value)
{
    if (strcmp(function, FUNCTION_SET_POWER) == 0)
    {
        return inovance_pcs_set_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_REPOWER) == 0)
    {
        return inovance_pcs_set_repower(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_MODE) == 0)
    {
        return inovance_pcs_set_mode(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_ONOFF) == 0)
    {
        return inovance_pcs_set_onoff(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_CONNECT) == 0)
    {
        return inovance_pcs_set_connect(this_p, (int)value);
    }
    else
    {
        return adapter_function_set(this_p, function, value);
    }
    return -1;
}

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

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

static char *inovance_pcs_compatible_arr[] = {"inovance", NULL};

pe_adapter inovance_pcs_adp __at_pe_adp_section = {
    .compatible = inovance_pcs_compatible_arr, // 属性
    //
    .init = inovance_pcs_init,
    //
    .set_power = inovance_pcs_set_power,
    .set_repower = inovance_pcs_set_repower,
    .set_mode = inovance_pcs_set_mode,
    .set_onoff = inovance_pcs_set_onoff,
    .set_connect = inovance_pcs_set_connect,
    //
    .get_power = inovance_pcs_get_power,
    .get_repower = inovance_pcs_get_repower,
    .get_mode = inovance_pcs_get_mode,
    .get_onoff = inovance_pcs_get_onoff,
    .get_connect = inovance_pcs_get_connect,

    .set_value        = inovance_pcs_set_value,
    .get_int_value    = inovance_pcs_get_int_value,
    .get_double_value = inovance_pcs_get_double_value,
};
