JLChen
2020-12-09 e87985ec1dcb69beedaf9f95e8e7aba14b7c08d6
HDL_ON/Entity/DB_ResidenceData.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using HDL_ON.DAL;
using System.Threading;
using HDL_ON.DAL.Server;
using Shared;
namespace HDL_ON.Entity
{
@@ -11,12 +13,9 @@
        }
        /// <summary>
        /// 0:A协议网关
        /// 1:bus网关
        /// 2:knx网关
        /// 主人token
        /// </summary>
        public int GatewayType = 0;
        public string MasterToken;
        /// <summary>
        /// 住宅基础信息
        /// </summary>
@@ -24,37 +23,73 @@
        {
            get
            {
                if (MainPage.LocalMode)
                if (MainPage.NoLoginMode)
                {
                    return new RegionInfoRes() { Name = "本地模式" };
                }
                else
                {
                    return MainPage.LoginUser.regionList.Find((obj) => obj.RegionID == CurReginID);
                    var curRegion = UserInfo.Current.regionList.Find((obj) => obj.RegionID == CurReginID);
                    if (curRegion == null)
                    {
                        if (UserInfo.Current.regionList != null && UserInfo.Current.regionList.Count > 0)
                        {
                            CurReginID = UserInfo.Current.regionList[0].RegionID;
                            //恢复备份
                            HDLCommon.Current.RestoreHomeBackup(CurReginID);
                            return UserInfo.Current.regionList[0];
                        }
                        else
                        {
                            return new RegionInfoRes();
                        }
                    }
                    //if (curRegion == null)
                    //{
                    //    CurReginID =  UserInfo.Current.regionList[0].RegionID;
                    //    return  UserInfo.Current.regionList[0];
                    //}
                    return curRegion;
                }
            }
        }
        ///// <summary>
        ///// 获取网关列表
        ///// </summary>
        //void GetHomeGatewayList()
        //{
        //    new Thread(() =>
        //    {
        //        //切换住宅后,查询一次网关列表
        //        new HttpServerRequest().GetHomeGatewayList();
        //    })
        //    { IsBackground = true }.Start();
        //}
        //string curReginID;
        ///// <summary>
        ///// 当前选择的住宅索引
        ///// </summary>
        //public string CurReginID
        //{
        //    set
        //    {
        //        curReginID = value;
        //        GetHomeGatewayList();
        //    }
        //    get
        //    {
        //        return curReginID;
        //    }
        //}
        /// <summary>
        /// 当前选择的住宅索引
        /// </summary>
        public string CurReginID = "";
        /// <summary>
        /// 楼层列表
        /// </summary>
        public List<Floor> floors = new List<Floor>();
        /// <summary>
        /// 房间列表
        /// </summary>
        public List<Room> rooms = new List<Room>();
        /// <summary>
        /// 功能列表
        /// </summary>
        public FunctionList functionList = new FunctionList();
        /// <summary>
        /// 功能类型列表
        /// </summary>
@@ -70,8 +105,9 @@
                    try
                    {
                        var residenceDataBytes = FileUtils.ReadFile("DB_ResidenceData");
                        var userConfigString = CommonPage.MyEncodingUTF8.GetString(residenceDataBytes);
                        var userConfigString = System.Text.Encoding.UTF8.GetString(residenceDataBytes);
                        DB_ResidenceData temp = null;
                        if (!string.IsNullOrEmpty(userConfigString))
                        {
                            temp = Newtonsoft.Json.JsonConvert.DeserializeObject<DB_ResidenceData>(userConfigString);
@@ -95,386 +131,9 @@
                                    ShowFunction.Panel,ShowFunction.SecurityMonitoring,
                                    ShowFunction.Sensor,ShowFunction.VideoIntercom
                                });
                        }
#if DEBUG
                        if (DB_ResidenceData.residenceData.functionList.scenes.Count == 0)
                        {
                            var s0 = new Scene()
                            {
                                sid = "030101123456780A0A010001ABCD",
                                name = "全关场景",
                                sceneType = SceneType.OrdinaryScenes,
                                roomIdList = new List<string>() { "0001" },
                                bus_SceneData = new Dictionary<string, byte[]>(),
                            };
                            s0.bus_SceneData.Add("030101123456780202010002ABCD", new byte[] { 1, 0, 0, 0 });
                            s0.bus_SceneData.Add("030101123456780202020005ABCD", new byte[] { 1, 0, 0, 0 });
                            s0.bus_SceneData.Add("030101123456780202040006ABCD", new byte[] { 1, 0, 254, 0, 0, 3, 255, 0, 0, 0, 0 });
                            s0.bus_SceneData.Add("030101123456780604010001ABCD", new byte[] { 1, 0, 32, 32, 32, 32, 32, 0, 0, 0, 1, 28, 0 });
                            s0.bus_SceneData.Add("030101123456780303010003ABCD", new byte[] { 1, 2 });
                            DB_ResidenceData.residenceData.functionList.scenes.Add(s0);
                            var s1 = new Scene()
                            {
                                sid = "030101123456780A0A00010002ABCD",
                                name = "全开场景",
                                sceneType = SceneType.OrdinaryScenes,
                                roomIdList = new List<string>() { "0001" },
                                bus_SceneData = new Dictionary<string, byte[]>(),
                            };
                            s1.bus_SceneData.Add("030101123456780202010002ABCD", new byte[] { 1, 100, 0, 0 });
                            s1.bus_SceneData.Add("030101123456780202020005ABCD", new byte[] { 1, 100, 0, 0 });
                            s1.bus_SceneData.Add("030101123456780202040006ABCD", new byte[] { 1, 100, 254, 0, 0, 3, 255, 0, 0, 0, 0 });
                            s1.bus_SceneData.Add("030101123456780604010001ABCD", new byte[] { 1, 0, 32, 32, 32, 32, 32, 0, 1, 0, 1, 28, 0 });
                            s1.bus_SceneData.Add("030101123456780303010003ABCD", new byte[] { 1, 1 });
                            DB_ResidenceData.residenceData.functionList.scenes.Add(s1);
                            var s2 = new Scene()
                            {
                                sid = "030101123456780A0A010004ABCD",
                                name = "电影场景",
                                sceneType = SceneType.MovieScene,
                                roomIdList = new List<string>() { "0001" },
                            };
                            DB_ResidenceData.residenceData.functionList.scenes.Add(s2);
                            var s3 = new Scene()
                            {
                                sid = "030101123456780A0A010005ABCD",
                                name = "电影场景2",
                                sceneType = SceneType.MovieScene,
                                roomIdList = new List<string>() { "0001" },
                            };
                            DB_ResidenceData.residenceData.functionList.scenes.Add(s3);
                        }
                        if (instance.rooms.Count == 0)
                        {
                            //--------------------
                            var r111 = new Room() { sid = "0001", name = "Room-1", floorId = "", backgroundImage = "Classification/Room/Roombg.png" };
                            instance.rooms.Add(r111);
                            instance.floors.Add(new Floor() { sid = "", name = "1 F" });
                            instance.functionList.aCs.Add(new AC()
                            {
                                sid = "030101123456780604010001ABCD",
                                name = "空调",
                                function = new List<Trait>() {
                                        new Trait { attri="on_off", max=1,min = 0, value= new List<string> { "on","off"} },
                                        new Trait { attri="mode", max = 2,min =0,value = new List<string>{ "auto", "heat", "cool","dry" } },
                                        new Trait { attri = "fan",max = 3,min =0,value = new List<string>{ "low", "medium", "high" ,"auto"} },
                                        new Trait { attri = "temperature", max = 32,min=16,value = new List<string>{"up","down" } },
                                    },
                                roomIdList = new List<string>() { "0001" },
                                lastState = "制冷 中风 18°C",
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 8,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.lights.Add(new Light()
                            {
                                sid = "030101123456780202010002ABCD",
                                name = "调光灯",
                                function = new List<Trait>() {
                                        new Trait { attri="brightness", max=100,min = 0, value= new List<string> { "up","down"} },
                                    },//"brightness",
                                roomIdList = new List<string>() { "0001" },
                                lastState = "20%",
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 9,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.curtains.Add(new Curtain()
                            {
                                sid = "030101123456780303010003ABCD",
                                name = "窗帘模块",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                        new Trait { attri="on_off", max=2,min = 0, value= new List<string> { "on","off","stop"} },
                                    },// "curtain",
                                lastState = "开",
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 7,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.curtains.Add(new Curtain()
                            {
                                sid = "030101123456780303030011ABCD",
                                name = "开合帘",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                        new Trait { attri="on_off", max=100,min = 0, value= new List<string> { "on","off","stop"} },
                                    },//"curtaintype",
                                lastState = "20%",
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 7,
                                    LoopID = 11,
                                },
                            });
                            instance.functionList.curtains.Add(new Curtain()
                            {
                                sid = "030101123456780503040012ABCD",
                                name = "卷帘",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                        new Trait { attri="on_off", max=2,min = 0, value= new List<string> { "on","off","stop"} },
                                        new Trait { attri="openLevel", max=100,min = 0, value= new List<string> { "percent" } },
                                    },// "rollingshutter",
                                lastState = "20%",
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 17,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.floorHeatings.Add(new FloorHeating()
                            {
                                sid = "030101123456780404020004ABCD",
                                name = "地热",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                        new Trait { attri="on_off", max=1,min = 0, value= new List<string> { "on","off"} },
                                        new Trait { attri="mode", max = 2,min =0,value = new List<string>{ "ordinary", "day", "night","timer","away" } },
                                        new Trait { attri = "temperature", max = 32,min=16,value = new List<string>{"up","down" } },
                                    },//
                                lastState = "自动 25°C",
                                bus_Data = new BusData
                                {
                                    SubnetID = 111,
                                    DeviceID = 12,
                                    LoopID = 1
                                }
                            });
                            instance.functionList.lights.Add(new Light()
                            {
                                sid = "030101123456780202020005ABCD",
                                name = "吊灯",
                                function = new List<Trait>() {
                                        new Trait { attri="on_off", max=100,min = 0, value= new List<string> { "on","off"} },
                                    },//"on_off",
                                roomIdList = new List<string>() { "0001" },
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 6,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.lights.Add(new Light()
                            {
                                sid = "030101123456780202040006ABCD",
                                name = "RGB灯",
                                function = new List<Trait>() {
                                        new Trait { attri="brightness", max=100,min = 0, value= new List<string> { "on","off"} },
                                        new Trait { attri="color", max=100,min = 0, value= new List<string> { "255", "255", "255" } },
                                    },//"brightness,color",
                                roomIdList = new List<string>() { "0001" },
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 4,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.fans.Add(new Fan()
                            {
                                sid = "030101123456781010020006ABCD",
                                name = "风扇",
                                function = new List<Trait>() {
                                    new Trait { attri="on_off", max=1,min = 0, value= new List<string> { "on","off"} },
                                    new Trait { attri="openLevel", max=7,min = 0, value= new List<string> { "percent" } },
                                },//"brightness,color",
                                roomIdList = new List<string>() { "0001" },
                                bus_Data = new BusData
                                {
                                    SubnetID = 142,
                                    DeviceID = 4,
                                    LoopID = 11,
                                },
                            });
                            instance.functionList.tVs.Add(new TV()
                            {
                                sid = "030101123456781010030006ABCD",
                                name = "电视",
                                function = new List<Trait>() {
                                    new Trait { attri="on_off", max=1,min = 0, value= new List<string> { "on","off"} },
                                },
                                roomIdList = new List<string>() { "0001" },
                                bus_Data = new BusData
                                {
                                    SubnetID = 142,
                                    DeviceID = 4,
                                    LoopID = 111,
                                },
                            });
                            instance.functionList.switchSockets.Add(new SwitchSocket()
                            {
                                sid = "030101123456780101020006ABCD",
                                name = "插座",
                                function = new List<Trait>() {
                                    new Trait { attri="on_off", max=1,min = 0, value= new List<string> { "on","off"} },
                                },//"brightness,color",
                                roomIdList = new List<string>() { "0001" },
                                bus_Data = new BusData
                                {
                                    SubnetID = 42,
                                    DeviceID = 6,
                                    LoopID = 1,
                                },
                            });
                            //instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            //{
                            //    sid = "03010112345678100B010006ABCD",
                            //    name = "PM1.0",
                            //    roomIdList = new List<string>() { "0001" },
                            //    function = new List<Trait>() {
                            //        new Trait { attri="values", max=1,min = 0, value= new List<string> { "135"} },
                            //    },
                            //    bus_Data = new BusData
                            //    {
                            //        SubnetID = 111,
                            //        DeviceID = 15,
                            //        LoopID = 1,
                            //    },
                            //});;
                            instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            {
                                sid = "03010112345678100B060006ABCD",
                                name = "PM2.5",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                    new Trait { attri="values", max=1,min = 0, value= new List<string> { "22"} },
                                },
                                bus_Data = new BusData
                                {
                                    SubnetID = 111,
                                    DeviceID = 15,
                                    LoopID = 1,
                                },
                            });
                            //instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            //{
                            //    sid = "03010112345678100B030006ABCD",
                            //    name = "噪音",
                            //    roomIdList = new List<string>() { "0001" },
                            //    propertList = new List<Trait>() {
                            //        new Trait { attri="values", max=1,min = 0, value= new List<string> { "120"} },
                            //    },
                            //    bus_Data = new BusData
                            //    {
                            //        SubnetID = 142,
                            //        DeviceID = 4,
                            //        LoopID = 111,
                            //    },
                            //});
                            //instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            //{
                            //    sid = "03010112345678100B040006ABCD",
                            //    name = "风力",
                            //    roomIdList = new List<string>() { "0001" },
                            //    propertList = new List<Trait>() {
                            //        new Trait { attri="values", max=1,min = 0, value= new List<string> { "1.8"} },
                            //    },
                            //    bus_Data = new BusData
                            //    {
                            //        SubnetID = 142,
                            //        DeviceID = 4,
                            //        LoopID = 111,
                            //    },
                            //});
                            instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            {
                                sid = "03010112345678100B070006ABCD",
                                name = "CO2",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                    new Trait { attri="values", max=1,min = 0, value= new List<string> { "1352"} },
                                },
                                bus_Data = new BusData
                                {
                                    SubnetID = 111,
                                    DeviceID = 15,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            {
                                sid = "03010112345678100B020006ABCD",
                                name = "温度",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                    new Trait { attri="values", max=1,min = 0, value= new List<string> { "28"} },
                                },
                                bus_Data = new BusData
                                {
                                    SubnetID = 111,
                                    DeviceID = 15,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            {
                                sid = "03010112345678100B050006ABCD",
                                name = "TVOC",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                    new Trait { attri="values", max=1,min = 0, value= new List<string> { "0.3"} },
                                },
                                bus_Data = new BusData
                                {
                                    SubnetID = 111,
                                    DeviceID = 15,
                                    LoopID = 1,
                                },
                            });
                            instance.functionList.sensorsEnvironmentalScience.Add(new Sensor()
                            {
                                sid = "03010112345678100B030006ABCD",
                                name = "湿度",
                                roomIdList = new List<string>() { "0001" },
                                function = new List<Trait>() {
                                    new Trait { attri="values", max=1,min = 0, value= new List<string> { "15"} },
                                },
                                bus_Data = new BusData
                                {
                                    SubnetID = 111,
                                    DeviceID = 15,
                                    LoopID = 1,
                                },
                            });
                            instance.SaveResidenceData();
                        }
#endif
                        //})
                        //{ IsBackground = true }.Start();
                    }
                    catch (Exception ex)
@@ -483,7 +142,7 @@
                    }
                    instance.residenceImage = "Classification/Room/Roombg.png";
                    //初始化住宅功能数据
                    InitRoomFunction();
                    SpatialInfo.CurrentSpatial.InitRoomFunction();
                }
                return instance;
@@ -495,6 +154,88 @@
        public void EixtAccount()
        {
            instance = null;
            Rooms = null;
            ins_OidList = null;
            functionList = null;
            HomeGateway = null;
            if (MainPage.IsRemote)
            {
                DAL.Mqtt.MqttClient.DisConnectRemote("退出登录,或者切换住宅");
            }
        }
        /// <summary>
        /// 网关详细信息
        /// </summary>
        public HomeGatewayInfo HomeGateway;
        /// <summary>
        /// 0:bus网关
        /// 1:A协议网关
        /// </summary>
        public int GatewayType
        {
            get
            {
                //#if DEBUG
                //return 0;
                //#endif
                if (HomeGateway == null)
                    return -1;
                if (HomeGateway.gatewayType == "AGATEWAY")
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
        /// <summary>
        /// 检测住宅是否绑定了网关
        /// </summary>
        /// <returns></returns>
        public bool CheckWhetherGatewayIsBound()
        {
            if (HomeGateway != null && !string.IsNullOrEmpty(HomeGateway.mac))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 当前住宅的网关MAC
        /// </summary>
        public string residenceGatewayMAC
        {
            get
            {
                if (CheckWhetherGatewayIsBound())
                {
                    return HomeGateway.mac.ToUpper();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (CheckWhetherGatewayIsBound())
                {
                    HomeGateway.mac = value;
                }
                else
                {
                    HomeGateway = new HomeGatewayInfo() { mac = value };
                }
            }
        }
        /// <summary>
@@ -502,99 +243,103 @@
        /// </summary>
        public string residenceImage;
        /// <summary>
        /// 住宅数据是否存在变化
        /// </summary>
        bool hasChange = false;
        /// <summary>
        /// 当前住宅的网关MAC
        /// </summary>
        public string residenceGatewayMAC = "4E47323347591243";
        //public string residenceGatewayMAC = "4E47323347591243";//"0281B9078C000000";// "00964E19C4467B1E";
        /// <summary>
        /// 保存住宅数据
        /// </summary>
        public void SaveResidenceData()
        {
            //if (hasChange)
            //    return;
            //hasChange = true;
            //new System.Threading.Thread(() =>
            //{
            //    System.Threading.Thread.Sleep(5000);
            if (this == null|| this.residecenInfo==null)
            if (this == null || this.residecenInfo == null)
                return;
            var ssd = CommonPage.MyEncodingUTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtils.WriteFileByBytes("DB_ResidenceData", ssd);
            MainPage.Log("Save DB_ResidenceData");
            //    hasChange = false;
            //})
            //{ IsBackground = true }.Start();
        }
        /// <summary>
        /// 刷新住宅里面的搜索设备状态
        /// 当前选择的楼层
        /// </summary>
        public void RefreshResidenceFunctionStatus()
        public string CurFoor = Language.StringByID(StringId.All);
        /// <summary>
        /// 房间列表
        /// </summary>
        public List<Room> Rooms = SpatialInfo.CurrentSpatial.RoomList;
        #region 功能数据
        static FunctionList _functionList;
        public static FunctionList functionList
        {
            new System.Threading.Thread(() =>
            get
            {
                try
                if (_functionList == null)
                {
                    foreach (var function in functionList.functions)
                    _functionList = FunctionList.List;
                }
                return _functionList;
            }
            set
            {
                _functionList = value;
            }
        }
        #endregion
        #region oid列表 a协议转bus协议控制使用
        /// <summary>
        /// oid数据保存的文件名
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public static string OidSavePathName = "AProtocolData_FunctionOid";
        static List<FunctionOid> ins_OidList;
        public static List<FunctionOid> functionOidList
        {
            get
            {
                if (ins_OidList == null)
                {
                    var bytes = FileUtils.ReadFile(OidSavePathName);
                    if (bytes == null || bytes.Length == 0)
                    {
                        System.Threading.Thread.Sleep(100);
                        //if (function.bus_Data != null)
                        ins_OidList = new List<FunctionOid>();
                    }
                    else
                    {
                        var jsonString = System.Text.Encoding.UTF8.GetString(bytes);
                        if (string.IsNullOrEmpty(jsonString))
                        {
                            switch (function.functionCategory)
                            ins_OidList = new List<FunctionOid>();
                        }
                        else
                        {
                            var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FunctionOid>>(jsonString);
                            if (list == null)
                            {
                                case FunctionCategory.Light:
                                case FunctionCategory.Electrical:
                                case FunctionCategory.SwitchDevice:
                                case FunctionCategory.Curtain:
                                case FunctionCategory.Thermostat:
                                    Control.SendReadCommand(function);
                                    break;
                                ins_OidList = new List<FunctionOid>();
                            }
                            else
                            {
                                ins_OidList = list;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log($"Refresh Residence Function Status Error : {ex.Message}");
                }
            })
            { IsBackground = true }.Start();
                return ins_OidList;
            }
        }
        /// <summary>
        /// 保存oid数据,每次保存都会覆盖
        /// </summary>
        public static void SaveOidList(string oidJsonString)
        {
            var oidBytes = System.Text.Encoding.UTF8.GetBytes(oidJsonString);
            FileUtils.WriteFileByBytes(OidSavePathName, oidBytes);
        }
        /// <summary>
        /// 初始化每个房间的功能数据
        /// </summary>
        public static void InitRoomFunction()
        {
            new System.Threading.Thread(() =>
            {
                try
                {
                    //初始化住宅功能数据
                    instance.functionList.GetAllFunction();
                    //初始化住宅所有房间功能数据
                    foreach (var r in instance.rooms)
                    {
                        r.functions.Clear();
                        foreach (var func in instance.functionList.functions)
                        {
                            MainPage.Log($"init room function sid {func.functionCategory.ToString()}:{r.sid}");
                            if (func.roomIdList.Contains(r.sid))
                            {
                                r.functions.Add(func);
                            }
                        }
                    }
                }catch(Exception ex)
                {
                    MainPage.Log($"Init room function error : {ex.Message}");
                }
            })
            { IsBackground = true }.Start();
        }
        #endregion
    }
}