// 同步定时模块
// 杭州秒寻科技有限公司
// 周涛
// 2025-3-3
#include "includes.h"

// 定义全局变量保存设置参数
static SYNC_TIMER_T g_stSyncTimerParam;

// 模块初始化
void oam_sync_timer_init()
{
    oam_sync_timer_set_param_with_default_data();
}

// 向外暴露参数，方便其他模块使用参数
SYNC_TIMER_T *oam_sync_timer_query_param()
{
    return (SYNC_TIMER_T *)(&g_stSyncTimerParam);
}

// 初始化默认参数
void oam_sync_timer_set_param_with_default_data()
{
    g_stSyncTimerParam.byTimerSwitch = 0; // 关闭
    g_stSyncTimerParam.byTimerNum = 1;

    g_stSyncTimerParam.wTimer1Start = 0x0f10;
    g_stSyncTimerParam.wTimer1Stop = 0x0f10;
    g_stSyncTimerParam.wTimer2Start = 0x0f10;
    g_stSyncTimerParam.wTimer2Stop = 0x0f10;
}

// 基于flash中的数据配置设备参数
void oam_sync_timer_set_current_datetime(uint8 *pbyData)
{
    SYS_MEMCPY((void *)&g_stSyncTimerParam, (void *)pbyData, sizeof(SYNC_TIMER_T));

    // 配置当前时间
    uint8 year = g_stSyncTimerParam.abyCurrentTime[5];
    uint8 month = g_stSyncTimerParam.abyCurrentTime[4];
    uint8 date = g_stSyncTimerParam.abyCurrentTime[3];
    uint8 hour = g_stSyncTimerParam.abyCurrentTime[2];
    uint8 minute = g_stSyncTimerParam.abyCurrentTime[1];
    uint8 second = g_stSyncTimerParam.abyCurrentTime[0];
    driverapp_rtc_set_current_datetime(year, month, date, hour, minute, second);
}

// 查询同步定时参数
// pbyData:当前监控对象
void oam_sync_timer_query(uint8 *pbyData)
{
    LTV_T *pstLtv = (LTV_T *)pbyData;
    uint8 byLen = pstLtv->wLen - 4; // 净荷长度
    uint16 wCommand = pstLtv->wTag;

    switch (wCommand)
    {
    case ORDER_SYNC_TIMER_CURRENT_TIME: // 当前时间
        uint8 year, month, day, hour, minute, second;
        uint8 byFlag = driverapp_rtc_get_current_datetime(&year, &month, &day, &hour, &minute, &second);
        if (!byFlag)
        {
            pstLtv->abyData[5] = 23;
            pstLtv->abyData[4] = 6;
            pstLtv->abyData[3] = 14;
            pstLtv->abyData[2] = 15;
            pstLtv->abyData[1] = 16;
            pstLtv->abyData[0] = 17;
        }
        else
        {
            pstLtv->abyData[5] = year;
            pstLtv->abyData[4] = month;
            pstLtv->abyData[3] = day;
            pstLtv->abyData[2] = hour;
            pstLtv->abyData[1] = minute;
            pstLtv->abyData[0] = second;
        }
        break;
    case ORDER_SYNC_TIMER_SWITCH: // 定时开关
        SYS_MEMCPY(pstLtv->abyData, &g_stSyncTimerParam.byTimerSwitch, byLen);
        break;
    case ORDER_SYNC_TIMER_NUM: // 定时分段数量
        SYS_MEMCPY(pstLtv->abyData, &g_stSyncTimerParam.byTimerNum, byLen);
        break;
    case ORDER_SYNC_TIMER1_START: // 定时1开始时间
        SYS_MEMCPY(pstLtv->abyData, &g_stSyncTimerParam.wTimer1Start, byLen);
        break;
    case ORDER_SYNC_TIMER1_STOP: // 定时1结束时间
        SYS_MEMCPY(pstLtv->abyData, &g_stSyncTimerParam.wTimer1Stop, byLen);
        break;
    case ORDER_SYNC_TIMER2_START: // 定时2开始时间
        SYS_MEMCPY(pstLtv->abyData, &g_stSyncTimerParam.wTimer2Start, byLen);
        break;
    case ORDER_SYNC_TIMER2_STOP: // 定时2结束时间
        SYS_MEMCPY(pstLtv->abyData, &g_stSyncTimerParam.wTimer2Stop, byLen);
        break;
    default:
        break;
    }

    return;
}

// 配置同步定时参数
// pbyData:当前监控对象
void oam_sync_timer_config(uint8 *pbyData)
{
    LTV_T *pstLtv = (LTV_T *)pbyData;
    uint8 byLen = pstLtv->wLen - 4; // 净荷长度
    uint16 wCommand = pstLtv->wTag;

    switch (wCommand)
    {
    case ORDER_SYNC_TIMER_CURRENT_TIME: // 当前时间
        SYS_MEMCPY(g_stSyncTimerParam.abyCurrentTime, pstLtv->abyData, byLen);
        oam_sync_timer_set_current_datetime(g_stSyncTimerParam.abyCurrentTime);
        break;
    case ORDER_SYNC_TIMER_SWITCH: // 定时开关
        SYS_MEMCPY(&g_stSyncTimerParam.byTimerSwitch, pstLtv->abyData, byLen);
        break;
    case ORDER_SYNC_TIMER_NUM: // 定时分段数量
        SYS_MEMCPY(&g_stSyncTimerParam.byTimerNum, pstLtv->abyData, byLen);
        break;
    case ORDER_SYNC_TIMER1_START: // 定时1开始时间
        SYS_MEMCPY(&g_stSyncTimerParam.wTimer1Start, pstLtv->abyData, byLen);
        break;
    case ORDER_SYNC_TIMER1_STOP: // 定时1结束时间
        SYS_MEMCPY(&g_stSyncTimerParam.wTimer1Stop, pstLtv->abyData, byLen);
        break;
    case ORDER_SYNC_TIMER2_START: // 定时2开始时间
        SYS_MEMCPY(&g_stSyncTimerParam.wTimer2Start, pstLtv->abyData, byLen);
        break;
    case ORDER_SYNC_TIMER2_STOP: // 定时2结束时间
        SYS_MEMCPY(&g_stSyncTimerParam.wTimer2Stop, pstLtv->abyData, byLen);
        break;
    default:
        break;
    }
}