/***
 * @Author: mbw
 * @Date: 2024-10-09 08:42:14
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2025-02-21 11:30:29
 * @FilePath: \ble_bjq_ch303rct6_ml307\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"

#define SYS_PREHEAT_TIME_S       (10U)
#define SYS_MUTE_TIME_S          (uint8_t)(10 * 3)
#define MAX_EXPIRATION_DAYS      (uint16_t)(365 * 5 + 30 * 3)


#define SYS_HW_VERSION           (0X11U)
#define SYS_SW_VERSION           (0X11U)
#define SYS_IOT_UPLOAD_CYCLE_MIN (0X003CU) // 高位在后，低位在前
#define SYS_IOT_RETRY            (0X03U)
#define SYS_TEMP_ALARM_THRESHOLD (0X32U)
#define SYS_ALARM_VALVE          (0x06D6U)//1750
#define SYS_EMV_SWITCH           (0X04U)
#define SYS_RELAY_SWITCH         (0X04U)
#define SYS_ALARM_VALVE_MAX      (0X30U)


// 正式平台链接
#define SYS_IOT_URL              ("8.130.122.162")
#define SYS_IOT_PORT             ("7153")

//测试平台链接
// #define SYS_IOT_URL              ("8.130.105.38")
// #define SYS_IOT_PORT             ("7133")


#define K                        (8 * 1000)

// 定义宏来打印编译时间和日期
#define COMPILE_TIME                                               \
    do {                                                           \
        rt_kprintf("Compiled on: %s at %s\n", __DATE__, __TIME__); \
    } while (0)

#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

typedef enum
{
    kPowerOnEvent,
    kPreheatingEvent,
    kNormalDetectionEvents,
    kAlarmEvent,
    kAlarmRcyEvent,
    kFaultEvent,
    kFaultRcyEvent,
    kSensorFailureEvent,
    kPowerDownEvent,
    kSelfCheckEvent,
    KMuteEvent,
    kAlarmExceptionEvent, // 浓度异常报警
    kMaxEventcnt,
} AlarmEvent;

// 定义系统状态枚举类型
typedef enum
{
    kNotCalibrated = 0, //  未标定
    kSysGasCalibStatus, // 已经标定

} TeCalibrationStatus;
// 定义开关状态枚举类型
typedef enum
{
    kStatusOff = 0, // 关闭状态
    kStatusOn       // 打开状态
} TeStatus;

// 定义系统标志位结构体
typedef struct
{
    TeStatus valve_status; // 阀门状态
    TeStatus relay_status; // 继电器状态
} TsDeviceStatus;

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

extern volatile rt_uint16_t work_duration; // 工作天数
extern volatile rt_uint8_t device_state_flag;
extern TeCalibrationStatus g_Calibration_status;
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);
rt_uint8_t XOR_CheckSum(const rt_uint8_t *data, rt_size_t len);
rt_uint32_t IMEI_Delay(void);

int BSP_SYS_Init(void);

#endif //!__USER_SYS_H__
