/*
 * @Author: mbw
 * @Date: 2024-10-09 08:42:14
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2024-10-29 16:03:50
 * @FilePath: \JT-DT-YD4N02A_RTT_MRS-4G\bsp\src\bsp_relay.c
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
#include "bsp_relay.h"
#include "rtthread.h"
#include "pin.h"
#include "bsp_flash.h"

//用到了atoi
#include <stdlib.h>

#include "agile_led.h"
agile_led_t *relay = RT_NULL;

#define LOG_TAG     "bsp.relay"
#define LOG_LVL     LOG_LVL_DBG
#include <ulog.h>

#define RELAY_LINKED       0x02
#define RELAY_FUNCTION_ON  0x04
#define RELAY_STATUS_OPEN  0x07
#define RELAY_STATUS_CLOSE 0x06
#define RELAY_NOT_CONNECTED 0x04
#define RELAY_FUNCTION_OFF  0x00


volatile rt_uint8_t relay_state_flag = 0;// 0:关闭，1：打开
struct rt_timer relay_timer;

// 获取继电器连接状态
static int Get_Relay_Link_Status(void)
{
    // TODO: 需要有检测功能
    return RELAY_LINKED;
    // return RELAY_NOT_CONNECTED;
}

// 获取继电器状态
int Get_Relay_Status(void)
{
    rt_uint8_t relay_state = (rt_uint8_t)Flash_Get_SysCfg(kRelaySwitchId) & 0x07; // 获取开关阀功能开关状态
    if (relay_state & RELAY_FUNCTION_ON) // 继电器功能开启了
    {
        LOG_D("具备继电器功能");
        if (Get_Relay_Link_Status() & RELAY_LINKED) 
        {
            LOG_D("继电器已经连接");

            if (rt_pin_read(RELAY_PIN)) // 高电平
            {
                return RELAY_STATUS_OPEN;
            }
            else // 低电平
            {
                return RELAY_STATUS_CLOSE;
            }
        }
        else
        {
            LOG_D("继电器未连接");
            return RELAY_NOT_CONNECTED; // 0x04为未连接
        }
    }
    return RELAY_FUNCTION_OFF; // 0继电器功能未开启
}

/*
 * @brief  设置继电器状态
 * 0x07 为打开，0x06为关闭
*/
int BSP_Set_Relay_Status(rt_bool_t status)
{
    rt_uint8_t ret = Get_Relay_Status();
    if (ret == RELAY_FUNCTION_OFF || ret == RELAY_NOT_CONNECTED) // 功能未开启或未连接
    {
        return (ret | 0xB0);
    }
    else
    {
        if (status == 0)
        {
            if (ret == RELAY_STATUS_CLOSE) // 继电器已经关闭
            {
                return RELAY_STATUS_CLOSE;
            }
            else
            {
                relay_state_flag = 0;
                return RELAY_STATUS_CLOSE;
            }
        }
        else
        {
            if (ret == RELAY_STATUS_OPEN) // 继电器已经打开
            {
                return RELAY_STATUS_OPEN;
            }
            else
            {
                relay_state_flag = 1;
                return RELAY_STATUS_OPEN;
            }
        }
    }
}

// 定时器回调函数
void BSP_Relay_Timer_Callback(void *parameter)
{
    static rt_uint8_t last_pos = 0;
    rt_uint8_t pos = 0;

    pos = relay_state_flag;

    // 执行动作逻辑
    if (last_pos != pos)
    {
        if (pos == 1)
        {
            LOG_D("RELAY_ON");
            // TODO: 闭合继电器动作
            RELAY_ON;
        }
        else if (pos == 0)
        {
            LOG_D("RELAY_OFF");
            // TODO: 断开继电器动作
            RELAY_OFF;
        }
        last_pos = pos;
    }
}

int BSP_RELAY_Init(void)
{
    rt_pin_mode(RELAY_PIN, PIN_MODE_OUTPUT);
    RELAY_OFF;

    LOG_D("BSP_RELAY_Init");
    rt_timer_init(&relay_timer, "relay_timer", BSP_Relay_Timer_Callback, RT_NULL, 50, RT_TIMER_FLAG_PERIODIC);
    rt_timer_start(&relay_timer); // 启动定时器

    return RT_EOK;
}
// INIT_DEVICE_EXPORT(BSP_RELAY_Init);

#ifdef TEST_ENABLE
static void TEST_RELAY(int argc, char **argv)
{
    if (argc == 2)
    {
        int flag = atoi(argv[1]);
        if (flag)
        {
           relay_state_flag = 1; 
        }
        else
        {
            relay_state_flag = 0;
        }
    }
}
MSH_CMD_EXPORT(TEST_RELAY, "TEST_RELAY");
#endif