#define 	MAX_AQI_DATA 		20
#define 	MAX_NODE_DATA		20
#define 	MAX_MOTION_DATA		20
#define 	MAX_LSD_DATA		20
#define 	MAX_POST_DATA		20
#define 	MAX_EIS_QUEUE_SIZE 	20

typedef struct __attribute__((packed)){
    uint32_t time;				//unix time
    int16_t temperature[2];		//Range: -40.0 to 125.0 C 		AVG, MAX
    uint16_t rh[2];				//Range: 0.0 to 100.0 %			AVG, MAX
    uint16_t pm1[2];			//Range: 0.0 to 6000.0 μg/m³	AVG, MAX
    uint16_t pm2_5[2];			//Range: 0.0 to 6000.0 μg/m³	AVG, MAX
    uint16_t pm4[2];			//Range: 0.0 to 6000.0 μg/m³	AVG, MAX
    uint16_t pm10[2];			//Range: 0.0 to 6000.0 μg/m³	AVG, MAX
    uint16_t co2[2];			//Range: 350 to 30000 ppm		AVG, MAX
    uint16_t voc[2];			//Range: 1 to 500 Index			AVG, MAX
    uint16_t co[2];				//Range: 0.0 to 1000.0 ppm		AVG, MAX
    uint16_t nox[2];			//Range: 0 to 500 Index			AVG, MAX
	uint16_t no2[2];			//Range: 0.05 to 10 ppm			AVG, MAX
	uint16_t tvoc[2];			//Range: 0 to 1200 ppb			AVG, MAX
}aqi_avg_min_max_data_t;		//52 bytes

typedef struct __attribute__((packed)){
    uint32_t time;			//unix time
    uint8_t node_state;	//Range: 1 to 5
}node_state_t;

typedef struct __attribute__((packed)){
    uint32_t time;			//unix time
    uint8_t motion_value;	//0: No motion, 1: motion
}motion_value_t;

typedef struct __attribute__((packed)){
	uint32_t last_update_time;  // Timestamp of the last motion update
	uint16_t motion_count;     // Number of detected motions
} motion_count_t;

typedef struct __attribute__((packed))
{
	uint32_t time;			//unix time
    uint8_t LSD1[2];		//LSD1 Data;
	uint8_t LSD2[2];		//LSD2 Data;
} lsd_data_t;

typedef struct __attribute__((packed)){
    uint32_t time;			//unix time
    uint8_t micro_post;	//0: POST failed, 1: POST passed
}micro_post_t;

typedef struct {
    uint16_t payload_index;    // 2 bytes
    uint8_t retries:4;         // 4 bits (part of 1 byte, bitfield)
    uint8_t is_motion_count:1; // 1 bit (part of 1 byte, bitfield)
    // 3 bits padding (implicit due to bitfield, not counted separately in packed struct)
    union {
        motion_value_t motion_value; // 5 bytes
        motion_count_t motion_count; // 6 bytes
    } payload;                    // 6 bytes (size of largest member: motion_count_t)
} motion_data_queue_t;            // Total: 9 bytes (2 + 1 + 6, bitfields packed into 1 byte)

typedef struct __attribute__((packed)) {
    struct __attribute__((packed)) {
		uint32_t valid_signature;
        aqi_avg_min_max_data_t aqi_data[MAX_AQI_DATA];
        uint8_t aqi_idx;
        time_t aqi_duration;
        time_t last_aqi_timestamp;
    } aqi_part;
    struct __attribute__((packed)) {
        node_state_t node_state[MAX_NODE_DATA];
        node_state_t last_node_state;
        uint8_t node_idx;
        time_t node_duration;
        time_t last_node_timestamp;
    } node_part;
    struct __attribute__((packed)) {
        lsd_data_t lsd_data[MAX_LSD_DATA];
        lsd_data_t last_lsd_data;
        uint8_t lsd_idx;
        time_t lsd_duration;
        time_t last_lsd_timestamp;
    } lsd_part;
    struct __attribute__((packed)) {
        micro_post_t post_data[MAX_POST_DATA];
        micro_post_t last_post_data;
        uint8_t post_idx;
        time_t post_duration;
        time_t seconds_since_last_micro_post;
        time_t last_post_timestamp;
        time_t last_last_mpost_timestamp;
    } post_part;
    struct __attribute__((packed)) {
        motion_count_t motion_cnt[MAX_MOTION_DATA];
        uint8_t motion_cnt_idx;
        time_t motion_duration;
        uint8_t queue_head;
        uint8_t queue_tail;
        uint8_t queue_count;
        uint16_t payload_index_counter;
        motion_data_queue_t motion_data_queue[MAX_EIS_QUEUE_SIZE];
        uint16_t QC_TEST_DATA;
        time_t last_motion_timestamp;
    } motion_part;
} eis_data_t;

#define  RESET_MODE_TIME_STAMP_AMOUNT          4
#define  MAX_GROU_ID_AMOUNT                    4


typedef struct __attribute__((packed)) {
    uint32_t valid_signature;							//4-bytes
    uint32_t network_key;								//4-bytes
    uint16_t node_id;									//1-byte
    uint8_t  stamp_count;								//1-byte
    int32_t   time_stamp[RESET_MODE_TIME_STAMP_AMOUNT];	//16-bytes (4*4)
    uint16_t group_ids[MAX_GROU_ID_AMOUNT];				//8-bytes (2*4)
    uint8_t ibeacon_uuid[16];
    uint16_t ibeacon_major_num;
    uint16_t ibeacon_minor_num;               // 46 + 20 = 66
	uint8_t  VersionInfo;								//1-byte
	int32_t last_correct_unix_time;						//4-bytes
	uint32_t temp_network_key;							//4-bytes
	uint16_t temp_node_id;								//2-bytes
	uint16_t temp_group_id[MAX_GROU_ID_AMOUNT];			//8-bytes (2*4)
#ifdef EXIT_SIGN
	uint16_t aqi_interval;								//2-bytes
	uint16_t lsd_interval;								//2-bytes
	uint16_t motion_interval;							//2-bytes
	motion_evt_t motion_event;							//1-byte
	uint16_t node_state_interval;						//2-bytes
	uint8_t isAqiThresholdCrossed;						//1-byte
	uint8_t isMicroPostFailed;							//1-byte
	uint8_t isOnBattery;								//1-byte
	int32_t aqi_threshold_exceed_time;					//4-bytes
	uint16_t gw_node_id;								//2-bytes
#endif
}ilumi_user_data;
