/***
 * @Author: mbw
 * @Date: 2024-10-09 08:42:14
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2024-11-12 14:19:50
 * @FilePath: \JT-DT-YD4N02A_RTT_MRS-4G\applications\user_sys.h
 * @Description:
 * @
 * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
#ifndef __USER_SYS_H__
#define __USER_SYS_H__

#include "rtdef.h"
#include "bsp_flash.h"


#define MAX_EXPIRATION_YEARS     (10U)
#define MAX_EXPIRATION_DAYS      (365 * MAX_EXPIRATION_YEARS + 30 * 3U)

#define SYS_HW_VERSION           (0X22U)
#define SYS_SW_VERSION           (0X14U)
#define SYS_IOT_UPLOAD_CYCLE_MIN (0X3C00U) // 高位在前，低位在后
#define SYS_IOT_RETRY            (0X03U)
#define SYS_TEMP_ALARM_THRESHOLD (0X30U)
#define SYS_ALARM_VALVE          (0X0AU)
#define SYS_EMV_SWITCH           (0X04U)
#define SYS_RELAY_SWITCH         (0X04U)
#define SYS_ALARM_VALVE_MAX      (0X30U)

#define SYS_IOT_URL              ("8.130.117.149")
#define SYS_IOT_PORT             ("7137")

    typedef enum
{
    kPowerOnEvent,
    kPreheatingEvent,
    kNormalDetectionEvents,
    kAlarmEvent,
    kAlarmRcyEvent,
    kFaultEvent,
    kFaultRcyEvent,
    kSensorFailureEvent,
    kPowerDownEvent,
    kSelfCheckEvent,
    KMuteEvent,
    kAlarmExceptionEvent, // alarm_exception
    kMaxEventcnt,
} AlarmEvent;

// 定义开关状态枚举类型
typedef enum
{
    kStatusOff = 0, // 关闭状态
    kStatusOn       // 打开状态
} TeStatus;
// 定义系统标志位结构体
typedef struct
{
    TeStatus valve_status; // 阀门状态
    TeStatus relay_status; // 继电器状态
} TsDeviceStatus;

// 定义系统状态枚举类型
typedef enum
{
    kSysToBeCalibStatus = 0, // 系统待校准状态
    kSysGasCalibStatus,      // 气体校准状态
    KSysTestStatus,          // 测试状态

    kSysSelfCheckingStatus, // 自检状态

    kSysPowerUpStatus,   // 上电状态
    kSysPowerDownStatus, // 下电状态

    kSysStartupPreheatingStatus, // 启动预热状态
    kSysRuningStatus,            // 运行中状态

    kSysAlarmStatus, // 报警状态
    kSysMuteStatus,  // 消音状态

    kSysFaultStatus,           // 故障状态
    kSysSensorEndOfLifeStatus, // 传感器寿命结束状态

    kSysRestoreFactoryStatus, // 恢复出厂设置状态
} TeSysStatus;

// 定义系统控制结构体
typedef struct
{
    AlarmEvent status;      // 当前状态
    AlarmEvent last_status; // 上一次状态
} TsSysControl;

extern volatile rt_uint16_t work_duration; // 工作天数
extern volatile rt_uint8_t device_state_flag;

extern struct rt_event alarm_event;
extern TsSysControl SysControl;
void Send_Laser_Alarm_Event(AlarmEvent event);

unsigned short crc1021(const char *data, unsigned int length);
void HexStrToBytes(const char *hexString, unsigned char *byteArray, size_t byteCount);
void String2Hex(char *hex_arry, char *str);
unsigned int VerToHex(const char *version);
int Extract_Value(const char *str, const char *key);
int _Self_Check_Mode(void);
int Convert_To_Hex(const struct flash_sever_info *sever_info, uint8_t *hex_array);
int BSP_SYS_Init(void);
rt_uint32_t IMEI_Delay(void);
#endif //!__USER_SYS_H__
