wei
2021-02-01 8da70e1a39328e1769b02bb1e05303916bb30eb5
HDL_ON/DAL/DriverLayer/Control.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.Entity;
@@ -82,11 +82,27 @@
            {
                if (_gatewayOnline != value)
                {
                    _gatewayOnline = value;
                    //修改主页连接状态
                    UI.HomePage.LoadEvent_CheckLinkStatus();
                    if (value)
                    if(value)
                    {
                        if (IsRemote)//如果是远程
                        {
                            if(DB_ResidenceData.Instance.HomeGateway == null)
                            {
                                return;
                            }
                            if (!DB_ResidenceData.Instance.HomeGateway.gatewayStatus)//远程情况下,网关未链接服务器不能修改主页网关状态
                            {
                                new System.Threading.Thread(() => {
                                    System.Threading.Thread.Sleep(3000);
                                    var pm = new DAL.Server.HttpServerRequest();
                                    pm.GetGatewayInfo();
                                }) { IsBackground = true }.Start();
                                return;
                            }
                        }
                        _gatewayOnline = value;
                        //修改主页连接状态
                        HomePage.LoadEvent_CheckLinkStatus();
                        MainPage.Log($"网关在线,刷新设备状态");
                        new System.Threading.Thread(() =>
                        {
@@ -94,6 +110,12 @@
                            FunctionList.List.ReadAllFunctionStatus();
                        })
                        { IsBackground = true, Priority = System.Threading.ThreadPriority.AboveNormal }.Start();
                    }
                    else
                    {
                        _gatewayOnline = value;
                        //修改主页连接状态
                        HomePage.LoadEvent_CheckLinkStatus();
                    }
                }
            }
@@ -103,23 +125,10 @@
        /// </summary>
        public string GatewayId = "";
        bool _isRemote = false;
        /// <summary>
        /// 是否为远程连接
        /// </summary>
        public bool IsRemote
        {
            get
            {
                return _isRemote;
            }
            set
            {
                _isRemote = value;
                //修改主页连接状态
                HomePage.LoadEvent_CheckLinkStatus();
            }
        }
        public bool IsRemote = false;
        /// <summary>
        /// 通讯地址IP
        /// </summary>
@@ -183,7 +192,14 @@
        /// </summary>
        public void SearchLoaclGateway()
        {
            MainPage.Log($"搜索本地网关列表,网关类型:{DB_ResidenceData.Instance.GatewayType}");
            //2021-01-15 : 住宅没有绑定网关的时候不用搜索,并且不能链接mqtt
            if(DB_ResidenceData.Instance.HomeGateway == null)
            {
                return;
            }
            var ggg = DB_ResidenceData.Instance.GatewayType == 0 ? "一端口" : "A网关";
            var ggg1 = MainPage.InternetStatus == 1 ? "4G" : "wifi";
            MainPage.Log($"搜索网关列表,网关类型:{ggg};网络类型:{ggg1}");
            if (MainPage.InternetStatus == 0)
            {
                Ins.GatewayOnline = false;
@@ -225,8 +241,6 @@
                        //本地搜索不到网关
                        if (!Ins.IsRemote)
                        {
                            //连接mqtt前,先断开--2020-12-21
                            DAL.Mqtt.MqttClient.DisConnectRemote();
                            Ins.IsRemote = true;
                            DAL.Mqtt.MqttClient.InitState();
                        }
@@ -243,7 +257,11 @@
        {
            if (Ins.IsRemote || DB_ResidenceData.Instance.GatewayType == 1)
            {
                ControlAProtocolScene(scene);
                new System.Threading.Thread(() =>
                {
                    ControlAProtocolScene(scene);
                })
                { IsBackground = true }.Start();
            }
            else
            {
@@ -261,10 +279,11 @@
        /// </summary>
        /// <param name="commandString"></param>
        /// <param name="function"></param>
        /// <param name="useRemote">是否直接使用远程发送</param>
        /// <returns></returns>
        public void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary)
        public void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary, bool useRemote = false)
        {
            function.usageCount++;
            function.controlCounter++;
            function.refreshTime = DateTime.Now;
            //如果是控制调光的开时,亮度值不能为0
@@ -289,8 +308,10 @@
                }
            }
            MainPage.Log($"发送数据:{Newtonsoft.Json.JsonConvert.SerializeObject(commandDictionary)}");
            //远程通讯
            if (Ins.IsRemote)
            if (Ins.IsRemote || useRemote == true)
            {
                DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
                //ALink控制、Bus控制使用同一个接口控制,由云端负责解析
@@ -298,7 +319,6 @@
                var actionObjs = new List<ApiAlinkControlActionObj>();
                actionObjs.Add(apiControlData);
                var pack = httpServer.ControlDevice(actionObjs);
                //MainPage.Log($"{pack.Code}:{pack.Data}");
            }
            //本地通讯
            else
@@ -325,6 +345,35 @@
                    MainPage.Log($"发送HDL-Link数据:{functionControlDataJson}");
                }
            }
        }
        /// <summary>
        /// 全开全关功能
        /// </summary>
        public void SwtichFunctions(bool open,List<Function> functions)
        {
            var count = 0;
            var logString = open ? "打开\r\n" : "关闭\r\n";
            List<ApiAlinkControlActionObj> actionObjs = new List<ApiAlinkControlActionObj>();
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add(FunctionAttributeKey.OnOff, open ? "on" : "off");
            var pm = new DAL.Server.HttpServerRequest();
            foreach (var temp in functions)
            {
                logString += temp.spk + ":" + temp.sid + "\r\n";
                var apiControlData = temp.GetApiControlData(d);
                actionObjs.Add(apiControlData);
                count++;
                if (count > 9)
                {
                    var result = pm.ControlDevice(actionObjs);
                    actionObjs = new List<ApiAlinkControlActionObj>();
                    count = 0;
                    MainPage.Log(logString);
                    logString = "";
                }
            }
            var pack = pm.ControlDevice(actionObjs);
            MainPage.Log(logString);
        }
        public void SendApiReadCommand(List<string> functionIds)
@@ -403,8 +452,16 @@
        {
            if (Ins.IsRemote)
            {
                var pm = new HDL_ON.DAL.Server.HttpServerRequest();
                pm.ExecuteScene(scene.userSceneId);
                //云端做处理发送一端口场景
                //if (DB_ResidenceData.Instance.GatewayType == 0)
                //{
                //    new Control_Udp().ControlBusScenes(scene);
                //}
                //else
                {
                    var pm = new DAL.Server.HttpServerRequest();
                    var result = pm.ExecuteScene(scene.userSceneId);
                }
            }
            else
            {
@@ -497,7 +554,7 @@
                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceModule>(bodyJObj.objects.ToString());
                    if (device.device_mac == DB_ResidenceData.Instance.residenceGatewayMAC)
                    {
                        //本地搜索网关成功
                        MainPage.Log("本地搜索网关成功");
                        Control.Ins.IsSearchLocalGatewaySuccessful = true;
                        Ins.GatewayOnline = true;
                        if (!string.IsNullOrEmpty(device.gatewayId))
@@ -819,14 +876,28 @@
                                    }
                                    break;
                                case SPK.ElectricTV:
                                    break;
                                case SPK.ElectricTuyaAirCleaner:
                                case SPK.ElectricTuyaFan:
                                case SPK.ElectricTuyaWeepRobot:
                                case SPK.ElectricTuyaWaterValve:
                                    //设备状态推送
                                    var myDevice = Stan.HdlDeviceStatuPushLogic.Current.UpdateDeviceStatu(updateTemp);
                                    if (myDevice != null)
                                    {
                                        localObj = myDevice;
                                    }
                                    break;
                                default:
                                    break;
                            }
                            HomePage.UpdataFunctionStates(localObj);
                            RoomPage.UpdataStates(localObj);
                            FunctionPage.UpdataStates(localObj);
                            ClassificationPage.UpdataInfo(localObj);
                            if (localObj != null)
                            {
                                HomePage.UpdataFunctionStates(localObj);
                                RoomPage.UpdataStates(localObj);
                                FunctionPage.UpdataStates(localObj);
                                ClassificationPage.UpdataInfo(localObj);
                            }
                        }
                    }
                    catch (Exception ex)