//
// Created by DELL on 2024/10/16.
//

#include <stdint.h>
#include "mini_gateway_master.h"
#include "usart.h"
#include "modbus.h"
#include "cmsis_os2.h"
#include "modbus_slave.h"

static nmbs_t nmbs;

static int32_t uart_read(uint8_t *buf, uint16_t count, int32_t byte_timeout_ms,
                         void *arg)
{
    HAL_StatusTypeDef status = HAL_UART_Receive(&huart4, buf, count, byte_timeout_ms);
    if (status == HAL_OK)
    {
        return count;
    } else
    {
        return 0;
    }
}

static int32_t uart_write(const uint8_t *buf, uint16_t count, int32_t byte_timeout_ms,
                          void *arg)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    HAL_UART_Transmit(&huart4, buf, count, byte_timeout_ms);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
    return count;
}

void sync_ac_status(ffx_status_t *status)
{
    uint16_t power;
    uint16_t mode;
    uint16_t fan_speed;
    uint16_t set_temp;
    if (status->power == 0x01)
    {
        power = 0xAA;
        fan_speed = status->fan_speed + 1;
        set_temp = status->set_temp;
    } else
        power = 0x55;
    mode = status->mode + 1;
    switch (status->mode)
    {
        case 0://制冷模式
            mode = status->mode + 1;
            break;
        case 1://制热模式
            mode = status->mode + 3;
            break;
        case 2://除湿模式
            mode = status->mode;
            break;
        case 3://送风模式
            mode = status->mode;
            break;
        case 4://加湿模式、送风
            mode = 3;
            break;
        default:
            break;
    }
    uint16_t regs[5] = {power, mode, set_temp, fan_speed, 0};
//    nmbs_write_multiple_registers(&nmbs, 102 + 25 * (status->inner_num - 1), 5, regs);
}

_Noreturn void mini_mater_task(void *pv)
{
    for (;;)
    {
//        for (uint8_t i = 0; i < 10; i++)
//        {
//            bool regs[1] = {0};
//            nmbs_error err = nmbs_read_coils(&nmbs, 120 + i, 1, &regs[0]);
//            osDelay(300);
//            if (err == NMBS_ERROR_NONE)
//            {
//                set_reg_value(INNER_POWER_REG_ADDRESS + i -1, regs[0]);
//            }
//        }
        osDelay(300);
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
        osDelay(1);
    }
}

void mini_gateway_master_init(void)
{
    nmbs_platform_conf platformConf;// 配置uart
    platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
    platformConf.read = &uart_read;     // 读写函数
    platformConf.write = &uart_write;   // 读写函数
    nmbs_client_create(&nmbs, &platformConf);// 创建客户端
    nmbs_set_read_timeout(&nmbs, 1000);//
    nmbs_set_byte_timeout(&nmbs, 1000);
    nmbs_set_destination_rtu_address(&nmbs, MINI_GATEWAY_SLAVE_ADDRESS);
    for (uint8_t i = 0; i < 2; i++)
    {
        bool regs[2] = {0};
        nmbs_error err = nmbs_read_coils(&nmbs, 120, 8, &regs[0]);
        osDelay(300);
        nmbs_error err_2 = nmbs_read_coils(&nmbs, 120, 8, &regs[0]);
        osDelay(300);
        if ((err_2 == NMBS_ERROR_NONE) && (err == NMBS_ERROR_NONE))
        {
            set_reg_value(INNER_GROUP1_REG_ADDRESS, regs[0]);
            set_reg_value(INNER_GROUP2_REG_ADDRESS, regs[1]);
        } else
        {
            i = 0;
        }
    }

    osThreadNew(mini_mater_task, NULL, NULL);
}
