#include "../adapter/pe_adapter.h"
#include <string.h>
#include "../common.h"
// PCS
#define HUAZHI_PCS_ACT_POWER "ActPowSet"
#define HUAZHI_PCS_REACT_POWER "ReactPowSet"
#define HUAZHI_PCS_STATE "StartStopCtr"
#define HUAZHI_PCS_WORK_MODE "WorkMode"
#define HUAZHI_PCS_CONNECT "setConnect"
#define HUAZHI_PCS_FAULT "DevFault"

//
#define HUAZHI_PCS_RUNSTA_OFF 0x00ff
#define HUAZHI_PCS_RUNSTA_ON 0xff00

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

int huazhi_pcs_set_power(pe_class *this_p, double power)
{
    dev_set_dev_tag_float(this_p->no, HUAZHI_PCS_ACT_POWER, power);
    return 0;
}

int huazhi_pcs_set_repower(pe_class *this_p, double repower)
{
    dev_set_dev_tag_float(this_p->no, HUAZHI_PCS_REACT_POWER, repower);
    return 0;
}

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

int huazhi_pcs_set_onoff(pe_class *this_p, int onoff)
{
    int re = 0;
    switch (onoff)
    {
    case PE_ADP_OFF:
        dev_set_dev_tag_int(this_p->no, "OnOff", 21845);
        break;

    case PE_ADP_ON:
        dev_set_dev_tag_int(this_p->no, "OnOff", 43690);
        break;

    default:
        re = -1;
        break;
    }
    return re;
}
int huazhi_pcs_set_connect(pe_class *this_p, int connect)
{
    dev_set_dev_tag_int(this_p->no, HUAZHI_PCS_CONNECT, connect);
    return 0;
}

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

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

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

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

    case 6:
    case 5:
    case 4:
    case 3:
    case 2:
        *onoff = PE_ADP_ON;
        break;

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

int huazhi_pcs_get_connect(pe_class *this_p, int *connect)
{
    *connect = dev_get_dev_tag_int(this_p->no, HUAZHI_PCS_CONNECT);
    return 0;
}

int huazhi_pcs_set_value(pe_class* this_p, const char* function, double value)
{
    if (strcmp(function, FUNCTION_SET_POWER) == 0)
    {
        return huazhi_pcs_set_power(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_REPOWER) == 0)
    {
        return huazhi_pcs_set_repower(this_p, value);
    }
    else if (strcmp(function, FUNCTION_SET_MODE) == 0)
    {
        return huazhi_pcs_set_mode(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_ONOFF) == 0)
    {
        return huazhi_pcs_set_onoff(this_p, (int)value);
    }
    else if (strcmp(function, FUNCTION_SET_CONNECT) == 0)
    {
        return huazhi_pcs_set_connect(this_p, (int)value);
    }
    else
    {
        return adapter_function_set(this_p, function, value);
    }
    return -1;
}

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

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

static char *huazhi_pcs_compatible_arr[] = {"RTC", NULL};

pe_adapter huazhi_pcs_adp __at_pe_adp_section = {
    .compatible = huazhi_pcs_compatible_arr, // 属性
    //
    .init = huazhi_pcs_init,
    //
    .set_power = huazhi_pcs_set_power,
    .set_repower = huazhi_pcs_set_repower,
    .set_mode = huazhi_pcs_set_mode,
    .set_onoff = huazhi_pcs_set_onoff,
    .set_connect = huazhi_pcs_set_connect,
    //
    .get_power = huazhi_pcs_get_power,
    .get_repower = huazhi_pcs_get_repower,
    .get_mode = huazhi_pcs_get_mode,
    .get_onoff = huazhi_pcs_get_onoff,
    .get_connect = huazhi_pcs_get_connect,

    .set_value        = huazhi_pcs_set_value,
    .get_int_value    = huazhi_pcs_get_int_value,
    .get_double_value = huazhi_pcs_get_double_value,
};
