#include "gydcg_UBCH2.h"


int ccu_DCGround_init(DCGround_t* dev, const char* devname)
{
    if (dev)
    {
        dev->salve = 0x01;
        com_ttySx_init(&dev->tty, devname, 9600, 8, 'N', 1);
        com_ttySx_rs485_enable(&dev->tty, 1);
        modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+0, 0x0102, 0x0011);
        modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+1, 0x0102, 0x0012);
        pthread_mutex_init(&dev->mutex, NULL);
        return 0;
    }
    return -1;
}

void ccu_DCGround_exit(DCGround_t* dev)
{
    if(dev)
    {
        modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+0, 0x0102, 0x0000);
        modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+1, 0x0102, 0x0000);
        com_ttySx_rs485_enable(&dev->tty, 0);
        com_ttySx_exit(&dev->tty);
        pthread_mutex_destroy(&dev->mutex);
    }
}

void ccu_DCGroundA_set(DCGround_t* dev, int on)
{
    if (dev)
    {
        //pthread_mutex_lock(&dev->mutex);
        if (on)
        {
            modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+0, 0x0102, 0x0011);
        }else{
            modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+0, 0x0102, 0x0000);
        }
        //pthread_mutex_unlock(&dev->mutex);
    }
}

void ccu_DCGroundB_set(DCGround_t* dev, int on)
{
    if (dev)
    {
        //pthread_mutex_lock(&dev->mutex);
        if (on)
        {
            modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+1, 0x0102, 0x0011);
        }else{
            modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+1, 0x0102, 0x0000);
        }
        //pthread_mutex_unlock(&dev->mutex);
    }
}

/*****************************************************************************************
 * 功能描述: 获取绝缘检测板A端口母线电压
 *****************************************************************************************/
float ccu_DCGroundA_readV(DCGround_t* dev)
{
    if (dev) 
    {
        uint16_t value[3];
        //pthread_mutex_lock(&dev->mutex);
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x0010, 1, value) == 0) 
        {
            dev->Vol[0] = (float)(value[0])/10;
        }
        // printd("dev->Vol[0]=%f\n", dev->Vol[0]);
        //pthread_mutex_unlock(&dev->mutex);
    }
    return dev->Vol[0];
}

/*****************************************************************************************
 * 功能描述: 获取绝缘检测板A端口DC+上电阻值
 *****************************************************************************************/
float ccu_DCGroundA_readR_P(DCGround_t* dev)
{
    if (dev) 
    {
        uint16_t value[2];
        //pthread_mutex_lock(&dev->mutex);
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x0010, 1, value) == 0)
        {
            dev->Vol[0] = (float)(value[0])/10;
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x001B, 1, value) == 0)
        {
            dev->state[0] = value[0];
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x0012, 1, value) == 0) 
        {
            // if (dev->Vol[0]>100 && (dev->state[0]&0x2))
            {
                dev->R_P[0] = (float)(value[0])*1000;
            }
        }
        //pthread_mutex_unlock(&dev->mutex);
    }
    return dev->R_P[0];
}

/*****************************************************************************************
 * 功能描述: 获取绝缘检测板A端口DC-上电阻值
 *****************************************************************************************/
float ccu_DCGroundA_readR_N(DCGround_t* dev)
{
    if (dev) 
    {
        uint16_t value[2];
        //pthread_mutex_lock(&dev->mutex);
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x0010, 1, value) == 0)
        {
            dev->Vol[0] = (float)(value[0])/10;
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x001B, 1, value) == 0)
        {
            dev->state[0] = value[0];
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x0013, 1, value) == 0) 
        {
            // if (dev->Vol[0]>100 && (dev->state[0]&0x2))
            {
                dev->R_N[0] = (float)(value[0])*1000;
            }
        }
        //pthread_mutex_unlock(&dev->mutex);
    }
    return dev->R_N[0];
}


/*****************************************************************************************
 * 功能描述: 获取绝缘检测板B端口母线电压
 *****************************************************************************************/
float ccu_DCGroundB_readV(DCGround_t* dev)
{
    if (dev) 
    {
        uint16_t value[3];
        //pthread_mutex_lock(&dev->mutex);
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x0010, 1, value) == 0) 
        {
            dev->Vol[1] = (float)(value[0])/10;
        }
        // printd("dev->Vol[1]=%f\n", dev->Vol[1]);
        //pthread_mutex_unlock(&dev->mutex);
    }
    return dev->Vol[1];
}

/*****************************************************************************************
 * 功能描述: 获取绝缘检测板B端口DC+上电阻值
 *****************************************************************************************/
float ccu_DCGroundB_readR_P(DCGround_t* dev)
{
    if (dev) 
    {
        uint16_t value[2];
        //pthread_mutex_lock(&dev->mutex);
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x0010, 1, value) == 0)
        {
            dev->Vol[1] = (float)(value[0])/10;
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x001B, 1, value) == 0)
        {
            dev->state[1] = value[0];
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x0012, 1, value) == 0) 
        {
            // if (dev->Vol[1]>100 && (dev->state[1]&0x2))
            {
                dev->R_P[1] = (float)(value[0])*1000;
            }
        }
        //pthread_mutex_unlock(&dev->mutex);
    }
    return dev->R_P[1];
}

/*****************************************************************************************
 * 功能描述: 获取绝缘检测板B端口DC-上电阻值
 *****************************************************************************************/
float ccu_DCGroundB_readR_N(DCGround_t* dev)
{
    if (dev) 
    {
        uint16_t value[2];
        //pthread_mutex_lock(&dev->mutex);
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x0010, 1, value) == 0)
        {
            dev->Vol[1] = (float)(value[0])/10;
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x001B, 1, value) == 0)
        {
            dev->state[1] = value[0];
        }
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x0013, 1, value) == 0) 
        {
            // if (dev->Vol[1]>100 && (dev->state[1]&0x2))
            {
                dev->R_N[1] = (float)(value[0])*1000;
            }
        }
        //pthread_mutex_unlock(&dev->mutex);
    }
    return dev->R_N[1];
}

void ccu_DCGroundA_check(DCGround_t* dev, float* VGround, float* RGround)
{
    if (dev)
    {
        // if (dev->onoff[0] == 1 && dev->state[0] != 1)
        // {
        //     modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+0, 0x0102, 0x0011);
        //     dev->state[0] = 1;
        // }
        // if (dev->onoff[0] == 0 && dev->state[0] != 0)
        // {
        //     modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+0, 0x0102, 0x0000);
        //     dev->state[0] = 0;
        // }
        uint16_t value[16];
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 0, 0x0010, 12, value) == 0)
        {
            dev->tty.debug = 0;
            dev->Vol[0] = (float)(value[0])/10;
            dev->R_P[0] = (float)(value[2])*1000;
            dev->R_N[0] = (float)(value[3])*1000;
        }else{
            dev->tty.debug = 1;
        }
        if (dev->tty.debug) printd("Vol:%f, R_P:%f, R_N:%f\n", dev->Vol[0], dev->R_P[0], dev->R_N[0]);

        if (VGround) *VGround = dev->Vol[0];

        if (RGround && (dev->onoff[0] == 1) && (value[0xB]&0x2))
        {
            *RGround = dev->R_P[0] + dev->R_N[0];
        }else{
            *RGround = 0.0f;
        }
    }
}

void ccu_DCGroundB_check(DCGround_t* dev, float* VGround, float* RGround)
{
    if (dev)
    {
        // if (dev->onoff[1] == 1 && dev->state[1] != 1)
        // {
        //     modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+1, 0x0102, 0x0012);
        //     dev->state[1] = 1;
        // }
        // if (dev->onoff[1] == 0 && dev->state[1] != 0)
        // {
        //     modbus_RTU_write_GYDCG_register(&dev->tty, dev->salve+1, 0x0102, 0x0000);
        //     dev->state[1] = 0;
        // }
        uint16_t value[16];
        if (modbus_RTU_read_holding_registers(&dev->tty, dev->salve + 1, 0x0010, 12, value) == 0)
        {
            dev->Vol[1] = (float)(value[0])/10;
            dev->R_P[1] = (float)(value[2])*1000;
            dev->R_N[1] = (float)(value[3])*1000;
        }else{
            dev->tty.debug = 1;
        }
        if (dev->tty.debug) printd("Vol:%f, R_P:%f, R_N:%f\n", dev->Vol[1], dev->R_P[1], dev->R_N[1]);

        if (VGround) *VGround = dev->Vol[1];

        if (RGround && (dev->onoff[1] == 1) && (value[0xB]&0x2))
        {
            *RGround = dev->R_P[1] + dev->R_N[1];
        }else{
            *RGround = 0.0f;
        }
    }
}
