use std::collections::HashMap;

use serde::{Deserialize, Serialize};

pub mod kafka_topic_contrant;

// 定义公共常量
// 所有设备只存ID
pub const REDIS_KEY_DEVICE_ALL: &str = "IOT:DEVICE:ALL";
// 设备基本信息
pub const REDIS_KEY_DEVICE_INFO: &str = "IOT:DEVICE:INFO:";

#[derive(Debug,Serialize, Deserialize, Clone)]
pub struct ConnectParam {
    //参数名称
    pub name: String,
    //参数描述（填写规则）
    pub desc: String,
    //参数值
    pub value: String,
}
/// 初始化参数
#[derive(Serialize, Deserialize, Clone)]
pub struct InitialParam {
    // 参数列表
    pub connect_params: Vec<ConnectParam>,
}

/// 驱动初始化参数属性
#[derive(Serialize, Deserialize, Clone)]
pub struct DriverAttribute {
    // 功能ID
    pub driver_function_id: String,
    // 功能名称
    pub driver_function_name: String,
    // 功能描述
    pub param_desc: String,
}

/// 驱动功能
#[derive(Serialize, Deserialize, Clone)]
pub struct DriverFunction {

    // 驱动属性列表
    pub driver_attributes: Vec<DriverAttribute>,

}

/// 属性操作枚举
#[derive(Serialize, Deserialize, Clone)]
pub enum AttributeOperationEnum {
    //读
    Read,
    //写
    Write,
}

///  物模属性行为
#[derive(Serialize, Deserialize, Clone)]
pub struct TslAttributeAction {
    // tagid
    pub id: String,
    // 设备ID
    pub device_id: String,
    // 设备编号
    pub device_number: String,
    // 点位功能标识
    pub identifier: String,
    // 驱动功能名称
    pub driver_function_name: String,
    // 驱动功能参数
    pub driver_function_param: String,
    // 值
    pub value: String,
}

/// 属性读写入参 操作帧
#[derive(Serialize, Deserialize, Clone)]
pub struct TupleFrame {
    // 操作类型：读or写
    pub operation: AttributeOperationEnum,
    // 待处理的属性列表
    pub attribute_actions: Vec<TslAttributeAction>,

}

/// 属性读写入参 操作帧
#[derive(Serialize, Deserialize, Clone)]
pub struct DriverResult {
    // 操作类型：读or写
    pub result: bool,
    // 待处理的属性列表
    pub attribute_actions: Vec<TslAttributeAction>,

}

/// 点位配置
#[derive(Debug,Serialize, Deserialize, Clone)]
pub struct DeviceTag {
    // 设备标识符
    #[serde(rename = "deviceId")]
    pub device_id: i32,  
    // 设备功能标签
    pub identifier: String,
    // 设备功能名称
    #[serde(rename = "identifierName")]
    pub identifier_name: String,
    // 采集间隔,单位ms
    #[serde(rename = "accessCycle")]
    pub access_cycle: u64,
    // 驱动功能名称
    #[serde(rename = "functionName")]
    pub function_name: Option<String>,
    // 驱动功能参数
    #[serde(rename = "functionParam")]
    pub function_param: Option<String>,
    // 数据类型 INT DOUBLE FLOAT
    #[serde(rename = "dataType")]
    pub data_type: String,
    // 读写类型： R: 只读 W: 只写 RW: 读写
    #[serde(rename = "accessMode")]
    pub access_mode: String,
}

/// 网关驱动
/// 网关驱动
#[derive(Debug,Serialize, Deserialize, Clone)]
pub struct GatewayDriver {

    // 主键
    pub id: i32,
    // 驱动id
    #[serde(rename = "driverId")]
    pub driver_id: String,
    // 驱动文件名称
    #[serde(rename = "fileName")]
    pub file_name: String,
    // 驱动存储路径
    #[serde(rename = "filePath")]
    pub file_path: String,
    // 驱动包名
    #[serde(rename = "packageName")]
    pub package_name: String,
    // 驱动类名
    #[serde(rename = "clzName")]
    pub class_name: String,

}
/// 驱动实例
#[derive(Debug,Serialize, Deserialize, Clone)]
pub struct GatewayDriverInstance {
    // 主键
    pub id: i32, 
    // 所属网关ID
    #[serde(rename = "gatewayId")]
    pub gateway_id: i64,
    // 驱动实例名称 
    pub name: String, 
    // 驱动连接参数
    #[serde(rename = "connectParams")]
    pub connect_params: Vec<ConnectParam>,
    // 关联的驱动
    #[serde(rename = "gatewayDriver")]
    pub gateway_driver: GatewayDriver,
    // 关联的设备ID列表
    #[serde(rename = "deviceIds")]
    pub device_ids: Option<Vec<i32>>,
}

/// 设备基本信息
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Device {
    // 设备标识符
    #[serde(rename = "deviceId")]
    pub device_id: i32,
    // 设备编号
    #[serde(rename = "deviceNo")]
    pub device_number: String,
     // 设备名称
     #[serde(rename = "deviceName")]
     pub device_name: String,
    // 通讯方式：1.THIRD_PARTY_CONNECT:第三方接口方式 ; 2.DRIVER_CONNECT:驱动方式
    #[serde(rename = "communicationMethod")]
    pub communication_method: String,
    // 设备点表配置列表
    #[serde(rename = "deviceTags")]
    pub device_tags: Vec<DeviceTag>,
    // 驱动实例
    #[serde(rename = "gatewayDriverInstance")]
    pub gateway_driver_instance: GatewayDriverInstance,
    // 租户ID
    #[serde(rename = "tenantId")]
    pub tenant_id: i32,
}

/// 消息体Packet元数据
#[derive(Serialize, Deserialize, Clone)]
pub struct Packet {
    // 设备标识符
    pub device_id: String,
    // 设备编号
    pub device_number: String,
    // 设备名称
    pub device_name: String,
    // 产品key
    pub product_key: String,
    // 设备点表配置列表
    pub identifier: String,
    // 驱动实例
    pub val: String,
    // 设备secret
    pub time: String,
    // 工控机编号
    pub control_number: String,
}


/// 消息体Packet元数据
#[derive(Serialize, Deserialize, Clone)]
pub struct PacketOld {
    // 设备类型ID
    pub categoryId: String,
    // 设备ID
    pub deviceId: String,
    // 设备名称
    pub deviceName: String,
    // 扩展
    pub extra: Option<String>,
    // 时间
    pub time: String,
    // 标识符
    pub identifier: String,
    // 值
    pub val: String,
    // 表名
    pub tableName: String,
    // 设备编号
    pub deviceNo: String,
    // 工控机编号
    pub controlNo: String,
}
/// 消息体Packet元数据
#[derive(Serialize, Deserialize, Clone)]
pub struct DeviceItem {
    // 设备ID
    pub deviceId: String,
}
/// 消息体Packet元数据
#[derive(Serialize, Deserialize, Clone)]
pub struct DeviceOnlineStatus {
    // 设备标识符
    pub device_id: String,
    // 设备编号
    pub device_number: String,
    // 设备名称
    pub device_name: String,
    // 设备在离线状态: true: 在线 false: 离线
    pub online_status: bool,
}

/// 设备Packet
#[derive(Serialize, Deserialize, Clone)]
pub struct DevicePacket {
    // 设备编号
    pub device_number: String,
    // 设备名称
    pub device_name: String,
    // 工控机编号
    pub control_no: String,
    // 设备数据
    pub data: HashMap<String, String>,
}
/// 数据
#[derive(Debug,Serialize, Deserialize, Clone)]
pub struct Updata {
    // 设备类key
    #[serde(rename = "categoryId")]
    pub category_id: Option<i32>,
    // 设备ID
    #[serde(rename = "deviceId")]
    pub device_id: i32,
    // 设备编号
    #[serde(rename = "deviceNo")]
    pub device_number: String,
    // 时间
    pub time: i64,
    // 以键值对方式上报数据 key为属性功能标识符，value为属性值
    pub params: HashMap<String, String>,
}


/// 下发指令
#[derive(Debug,Serialize, Deserialize, Clone)]
pub struct AttrUpDataPacket {
    // 操作类型
    pub operate: String,
    // 操作ID
    #[serde(rename = "operateId")]
    pub operate_id: i32,
    // 上报的数据
    pub data: Updata,
    // 响应码
    //pub code: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PubSubMessage {
    //命令
    pub target: String,
    //参数
    pub args: Option<String>,
}