using System.Collections.Concurrent;

namespace BLWLogServer.Services
{
    public class Device
    {
        /// <summary>
        /// 设备地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 地址类型
        /// </summary>
        //public AddressType AddressType { get; set; }
        /// <summary>
        /// 设备开关状态：1开，2关，6停（窗帘），下发1个字节
        /// </summary>
        public byte Status { get; set; }
        /// <summary>
        /// 设备开关状态：1开，2关，6停（窗帘）,接收2个字节
        /// </summary>
        public ushort StatusReceiver { get; set; }
        /// <summary>
        /// 亮度值
        /// </summary>
        public byte Brightness { get; set; }
        /// <summary>
        /// 温度
        /// </summary>
        public byte Temperature { get; set; }
        /// <summary>
        /// 风速
        /// </summary>
        public byte FanSpeed { get; set; }
        /// <summary>
        /// 模式
        /// </summary>
        public byte Mode { get; set; }
        /// <summary>
        /// 阀门开关
        /// </summary>
        public byte Valve { get; set; }
        /// <summary>
        /// 空调执行方式和内容
        /// </summary>
        public Int32 AirExecMode { get; set; }
        /// <summary>
        /// 地暖执行方式和内容
        /// </summary>
        public Int32 FloorHotExecMode { get; set; }
        /// <summary>
        /// 背景音乐执行方式和内容
        /// </summary>
        public Int32 MusicExecMode { get; set; }
        /// <summary>
        /// 色温执行方式和内容
        /// </summary>
        //public Int32 ColorTempExecMode { get; set; }
    }
    public class Status
    {
        /// <summary>
        /// RCU是否锁定
        /// </summary>
        public bool SysLock { get; set; }
        /// <summary>
        /// 房卡类型
        /// </summary>
        public byte CardType { get; set; }
        /// <summary>
        /// 房门开关
        /// </summary>
        public bool Door { get; set; }
        /// <summary>
        /// 电量
        /// </summary>
        public ushort ElecQty { get; set; }
        /// <summary>
        /// 主机温度
        /// </summary>
        public byte HostTemp { get; set; }
        /// <summary>
        /// 空调状态
        /// </summary>
        public ConcurrentDictionary<byte, AirConditionStatus> AirConditions = new ConcurrentDictionary<byte, AirConditionStatus>();
        /// <summary>
        /// 设备状态
        /// </summary>
        public ConcurrentDictionary<string, Device> Devices = new ConcurrentDictionary<string, Device>();
        /// <summary>
        /// 故障列表
        /// </summary>
        public ConcurrentDictionary<string, FaultStaus> Faults = new ConcurrentDictionary<string, FaultStaus>();
    }

    /// <summary>
    /// 空调状态
    /// </summary>
    public class AirConditionStatus
    {
        /// <summary>
        /// 空调号
        /// </summary>
        public byte AirNo { get; set; }
        /// <summary>
        /// 当前温度
        /// </summary>
        public int CurrentTemp { get; set; }
        /// <summary>
        /// 设定温度
        /// </summary>
        public int SettingTemp { get; set; }
        /// <summary>
        /// 风机开关
        /// </summary>
        public bool OnOff { get; set; }
        /// <summary>
        /// 风速：0/停止, 1/低速, 2/中速, 3/高速, 4/自动
        /// </summary>
        public int Speed { get; set; }
        /// <summary>
        /// 模式: 0/制冷，1/制热，2/送风，3/除湿
        /// </summary>
        public int Mode { get; set; }
        /// <summary>
        /// 阀状态： 1/阀开，2/阀关
        /// </summary>
        public int Valve { get; set; }
        /// <summary>
        /// 补偿温度，范围-6.0~6.0
        /// </summary>
        public float CompensatoryTemp { get; set; }
        /// <summary>
        /// 保温温度
        /// </summary>
        public int KeepTemp { get; set; }
        /// <summary>
        /// 冷热模式：0/手动，1/自动
        /// </summary>
        public int ColdHotMode { get; set; }
        /// <summary>
        /// 死区温度
        /// </summary>
        public int DeadTemp { get; set; }
        /// <summary>
        /// 热偏差
        /// </summary>
        public int HotDevition { get; set; }
        /// <summary>
        /// 冷偏差
        /// </summary>
        public int ColdDevition { get; set; }
    }

    public class FaultStaus
    {
        /// <summary>
        /// 故障号
        /// </summary>
        public string FaultNo { get; set; }
        /// <summary>
        /// 类型：1在线状态（0在线，1离线），2电量（0~100%），3电流，4 1901故障检测次数，5设备单个回路状态（0正常，1损坏）
        /// </summary>
        public byte Type { get; set; }
        /// <summary>
        /// 内容
        /// </summary>
        public byte Data { get; set; }
    }
    public class S
    {
        public static Status NewStatusParse(Stream stream)
        {
            Status roomStatus = new Status();
            try
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    roomStatus.SysLock = reader.ReadBoolean();		//RCU是否锁定：0/否，1/是
                    roomStatus.CardType = reader.ReadByte();		//房卡类型：0/无人，1/有人，2/客人，3/经理，4/服务员
                    roomStatus.Door = reader.ReadBoolean();			//门磁开关：1/开，2/关
                    roomStatus.ElecQty = reader.ReadUInt16();		//门锁电量，单位：MV
                    roomStatus.HostTemp = reader.ReadByte();		//主机温度
                    //空调数量，默认0，新的回路方式，此处无用，兼容老版本
                    int airConditionNumber = reader.ReadByte();
                    for (int i = 0; i < airConditionNumber; i++)
                    {
                        byte airNo = reader.ReadByte();
                        if (!roomStatus.AirConditions.ContainsKey(airNo))
                        {
                            roomStatus.AirConditions[airNo] = new AirConditionStatus();
                        }
                        roomStatus.AirConditions[airNo].AirNo = airNo;
                        roomStatus.AirConditions[airNo].CurrentTemp = reader.ReadSByte();
                        roomStatus.AirConditions[airNo].SettingTemp = reader.ReadSByte();
                        roomStatus.AirConditions[airNo].OnOff = reader.ReadBoolean();
                        roomStatus.AirConditions[airNo].Speed = reader.ReadByte();
                        roomStatus.AirConditions[airNo].Mode = reader.ReadByte();
                        roomStatus.AirConditions[airNo].Valve = reader.ReadByte();
                        roomStatus.AirConditions[airNo].CompensatoryTemp = reader.ReadByte();
                        roomStatus.AirConditions[airNo].KeepTemp = reader.ReadByte();
                        roomStatus.AirConditions[airNo].ColdHotMode = reader.ReadByte();
                        roomStatus.AirConditions[airNo].DeadTemp = reader.ReadByte();
                        roomStatus.AirConditions[airNo].HotDevition = reader.ReadByte();
                        roomStatus.AirConditions[airNo].ColdDevition = reader.ReadByte();
                    }
                    //设备状态数量，包含所有回路状态
                    int deviceNumber = reader.ReadByte();

                    long originalPosition = reader.BaseStream.Position;
                    // 读取前先记录当前位置
                    try
                    {
                        //0x19数据缓冲区异常问题处理
                        //0x19 取电数据不处理
                        //非0x19 取电数据处理，但是不触发 欢迎词
                        for (int i = 0; i < deviceNumber; i++)
                        {
                            var QA = reader.ReadBytes(4);
                            var Status = reader.ReadUInt16();

                            string address = String.Format("{0:000}{1:000}{2:000}", QA[0], QA[1], QA[0]);
                            if (!roomStatus.Devices.ContainsKey(address))
                            {
                                roomStatus.Devices[address] = new Device();
                            }
                            roomStatus.Devices[address].Address = address;//设备地址
                            roomStatus.Devices[address].StatusReceiver = Status;//设备状态改为2个字节,Modified By 20181213
                            originalPosition = reader.BaseStream.Position;
                        }
                    }
                    catch (Exception)
                    {
                        // 发生异常时回退到原始位置
                        reader.BaseStream.Position = originalPosition;
                    }
                    return roomStatus;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
