//
// Created by DELL on 2024/10/16.
//
#include "main.h"
#include "ffx_master.h"
#include "modbus.h"
#include "usart.h"
#include "cmsis_os2.h"
#include "mini_gateway_master.h"
#include "modbus_slave.h"

static nmbs_t nmbs;
uint8_t slaves[SLAVE_ADDRESS_END] = {0}; // 保存已经存在的从站号
static uint8_t index_2 = 0;
static osMutexId_t ffx_mutex;
extern uint8_t error_count;
uint8_t buffer_reg[10]; // 保存读取到的寄存器值
void search_ffx_slave();
uint8_t is_slave_exist = 0;

static int32_t uart_read(uint8_t *buf, uint16_t count, int32_t byte_timeout_ms,
                         void *arg) {
    HAL_StatusTypeDef status = HAL_UART_Receive(&huart2, 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_1, GPIO_PIN_SET);
    HAL_UART_Transmit(&huart2, buf, count, byte_timeout_ms);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
    return count;
}

// task
_Noreturn void ffx_poll_task(void *pv) {
    for (;;) {
        if (osMutexAcquire(ffx_mutex, osWaitForever) == osOK) {
            search_ffx_slave();
            osMutexRelease(ffx_mutex);
        }
        osDelay(1000 * 60 * 10);
    }
}

bool get_ffx_status(ffx_status_t *ffxStatus) {
    uint16_t ffx_ststus[3] = {0};
    nmbs_error err = nmbs_read_holding_registers(&nmbs, POWER_FFX_ADDRESS, 2, ffx_ststus);
    if (err == NMBS_ERROR_NONE) {
        ffxStatus->power = ffx_ststus[0];
        ffxStatus->mode = ffx_ststus[1];
    } else {
        return false;
    }
    uint16_t data;
    err = nmbs_read_holding_registers(&nmbs, INNER_NUM_FFX_ADDRESS, 1, &data);
    if (err == NMBS_ERROR_NONE) {
        ffxStatus->inner_num = data;
    }
    err = nmbs_read_holding_registers(&nmbs, SET_TEMP_FFX_ADDRESS, 1, &data);
    if (err == NMBS_ERROR_NONE) {
        ffxStatus->set_temp = data;
    }
    err = nmbs_read_holding_registers(&nmbs, FAN_SPEED_FFX_ADDRESS, 1, &data);
    if (err == NMBS_ERROR_NONE) {
        ffxStatus->fan_speed = data;
    }
    err = nmbs_read_holding_registers(&nmbs, TEMP_ON_FFX_ADDRESS, 1, &data);
    if (err == NMBS_ERROR_NONE) {
        ffxStatus->humidity_on = data;
    }
    err = nmbs_read_holding_registers(&nmbs, TEMP_OFF_FFX_ADDRESS, 1, &data);
    if (err == NMBS_ERROR_NONE) {
        ffxStatus->humidity_off = data;
    }
    return true;
}


_Noreturn void ffx_mater_task(void *pv) {
    uint8_t error_count = 0;
    uint8_t is_error_ffx[8] = {0};
    for (;;) {
        if (osMutexAcquire(ffx_mutex, osWaitForever) == osOK) {
            ffx_status_t ffxStatus;
            for (int i = 0; i < sizeof(slaves) + 1; i++) {
                if (slaves[i] != 0) {
                    nmbs_set_destination_rtu_address(&nmbs, slaves[i]);
                    if (get_ffx_status(&ffxStatus)) {
                        set_reg_value(TEMP_ON_FFX_ADDRESS, ffxStatus.humidity_on);
                        set_reg_value(TEMP_OFF_FFX_ADDRESS, ffxStatus.humidity_off);
                        set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
                        osDelay(300);
                        sync_ac_status(&ffxStatus);
                        is_error_ffx[i] = 0;
                        error_count = 0;
                    } else{
                        error_count++;
                        if(error_count >= 10){
                            set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
                        }
                    }
                    osDelay(2000);
                }
            }
            osMutexRelease(ffx_mutex);
        }
        osDelay(1000);
    }
}

// 检测从机是否存在
bool check_slave_exist(uint8_t slave_addr) {
    nmbs_set_destination_rtu_address(&nmbs, slave_addr);
    uint16_t result;
    osDelay(100);
    nmbs_error err = nmbs_read_holding_registers(&nmbs, 0x01, 1, &result);
    return err == NMBS_ERROR_NONE;
}

// 轮询从站
void search_ffx_slave() {
    for (int i = SLAVE_ADDRESS_START; i <= SLAVE_ADDRESS_END; i++) {
        bool exist = check_slave_exist(i);
        if (exist) {
            slaves[i] = i;
            set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
        }
        osDelay(200);
        if (exist == NMBS_ERROR_NONE) {
            exist = check_slave_exist(i);
            if (exist != NMBS_ERROR_NONE) {
                slaves[i] = i;
                set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
            }else{
                set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
            }
        }
        osDelay(200);
    }
}

void ffx_master_init() {
    ffx_mutex = osMutexNew(NULL);
    nmbs_platform_conf platformConf;// 配置uart
    nmbs_platform_conf_create(&platformConf);
    platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
    platformConf.read = &uart_read;     // 读写函数
    platformConf.write = &uart_write;   // 读写函数
    nmbs_client_create(&nmbs, &platformConf);// 创建客户端
    nmbs_set_read_timeout(&nmbs, 400);
    nmbs_set_byte_timeout(&nmbs, 400);
    // 开启轮询任务
    osThreadNew(ffx_poll_task, NULL, NULL);
    osThreadNew(ffx_mater_task, NULL, NULL);
}

