/*
 * @Author: mypx
 * @Date: 2025-07-07 14:25:47
 * @LastEditTime: 2025-10-24 13:06:47
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: 
 */
#include "pm_device.h"
#include "ad7793.h"
#include "data_process.h"
#include "data_sampling.h"
#include "et_log.h"
#include "hmi_server.h"
#include "pt100x.h"
#include "servo.h"
#include "storage.h"
#include "bsp_tec.h"

extern ad7793_dev_t ad7793_dev;

int hmi_system_control_handler(SysCtrlCode code)
{
    switch (code)
    {
    case SYS_CTRL_INIT_CODE:
        mb_sync_running_status_reg(PM_CALI_STATE);
        ET_WARN("SYS_CTRL_INIT_CODE");
        break;
    case SYS_CTRL_START_MEAS_CODE:
        ET_WARN("SYS_CTRL_START_MEAS_CODE");
        break;
    case SYS_CTRL_STOP_MEAS_CODE:
        ET_WARN("SYS_CTRL_STOP_MEAS_CODE");
        break;
    case SYS_CTRL_CLEAR_DATA_CODE:
        ET_WARN("SYS_CTRL_CLEAR_DATA_CODE");
        break;
    case SYS_CTRL_START_TEMP_CTRL_CODE:
        ET_WARN("SYS_CTRL_START_TEMP_CTRL_CODE");
        break;
    case SYS_CTRL_STOP_TEMP_CTRL_CODE:
        ET_WARN("SYS_CTRL_STOP_TEMP_CTRL_CODE");
        break;
    default:
        break;
    }
    return 0;
}

void hmi_measure_mode_handler(uint8_t mode)
{
    ET_WARN("Measure mode changed to %d, but not implemented yet!", mode);
}

void hmi_target_temp_handler(float temp)
{
    ET_WARN("Target temperature changed to %.1f, but not implemented yet!", temp);
}

void hmi_tube_length_handler(uint16_t length)
{
    ET_WARN("Tube length changed to %d, but not implemented yet!", length);
}

void hmi_meas_precision_handler(uint8_t precision)
{
    ET_WARN("Measurement precision changed to %d, but not implemented yet!", precision);
}

void hmi_auto_measure_count_handler(uint8_t count)
{
    ET_WARN("Auto measure count changed to %d, but not implemented yet!", count);
}

mb_cmd_handler_t hmi_cmd_handler = {
    .system_ctrl_handler        = hmi_system_control_handler,
    .meas_mode_handler          = hmi_measure_mode_handler,
    .target_temp_handler        = hmi_target_temp_handler,
    .tube_length_handler        = hmi_tube_length_handler,
    .meas_precision_handler     = hmi_meas_precision_handler,
    .auto_measure_count_handler = hmi_auto_measure_count_handler,
};

int pm_device_start(pm_device_t *dev)
{
    hmi_server_start();
    return 0;
}


int pm_device_init(pm_device_t *dev)
{
    int ret = 0;
    pm_board_init(); // TODO:Delete this later
    bsp_tec_init();
    pm_params_init(&dev->params);

    // hmi_server_handler_register(&dev->hmi, &hmi_cmd_handler);
    // hmi_server_init(&dev->hmi);
    pm_fsm_init(&dev->fz_fsm);

    //storage_init();
    ret = tec_control_init(&dev->tec);
    if(ret != 0)
    {
        ET_WARN("TEC control init failed!");
        return -1;
    }
    tec_control_sercie_start(&dev->tec);
    if (servo_init() != 0)
    {
        ET_ERR("Servo init failed!");
        return -1;
    }
    light_data_sampling_init();
    data_process_init();
    return 0;
}
