JLChen
2021-01-05 f500e14c0a994487070380c50c85e0929cbc8e63
Crabtree/SmartHome/UI/SimpleControl/Phone/Scene/SceneMethod.cs
@@ -333,12 +333,21 @@
            dialog.Show ();
        }
        public void ControlScene (string sceneFilePath)
        public void ControlSceneOld(string sceneFilePath)
        {
            if (CommonPage.IsRemote) {
                if (!SmartHome.MqttCommon.IsGatewayOnline) {
                    new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                    return;
                }
            }
            var tempScene = Scene.GetSceneByFilePath (sceneFilePath);
            if (tempScene == null)
                return;
            showTip = true;
            MainPage.Loading.Start ("Sending...");
            System.Threading.Tasks.Task.Run (() => {
                int sceneDeviceCount = tempScene.DeviceFilePathList.Count;
                int controlDeviceIndex = 0;
@@ -371,7 +380,7 @@
                                replyBytes = Control.ControlBytesSendHasReturn (Command.SetLogicLoopColor, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 254, device.DelayTimeHeigh,device.DelayTimeLow,
                                                    3,device.RStatus,device.GStatus,device.BStatus,0,0});
                            }
                        } else if (common.Type == DeviceType.LightSwitch ) {
                        } else if (common.Type == DeviceType.LightSwitch) {
                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                            if (device == null) {
                                continue;
@@ -388,13 +397,13 @@
                            if (device == null) {
                                continue;
                            }
                            replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, (byte)device.Status });
                            replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 17, (byte)device.CurtainProress });
                        } else if (common.Type == DeviceType.CurtainTrietex) {
                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainTrietex> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                            if (device == null) {
                                continue;
                            }
                            replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, (byte)device.Status });
                            replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 17, (byte)device.CurtainProress });
                        } else if (common.Type == DeviceType.HVAC || common.Type == DeviceType.ACInfrared) {
                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                            if (device == null) {
@@ -436,11 +445,18 @@
                            }
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, device.SubnetID, device.DeviceID, new byte [] {
                            device.AreaID,device.AreaSceneID});
                        } else if (common.Type == DeviceType.UniversalDevice) {//2020-09-02 增加通用开关
                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<UniversalDevice> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                            if (device == null) {
                                continue;
                            }
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetCommonSwitch, device.SubnetID, device.DeviceID, new byte [] { device.SendBytes [0], device.SendBytes [1] });
                        }
                        Application.RunOnMainThread (() => {
                            try {
                                if (showTip) {
                                    if (replyBytes == null) {
                        try {
                            if (showTip) {
                                if (replyBytes == null) {
                                    Application.RunOnMainThread (() => {
                                        string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
                                        var btnTip = new Button () {
                                            Height = Application.GetRealHeight (140),
@@ -450,20 +466,22 @@
                                        MainPage.MainFrameLayout.AddChidren (btnTip);
                                        btnTip.MouseUpEventHandler += (sender, e) => {
                                            btnTip.RemoveFromParent ();
                                            showTip = false;
                                            //MainPage.MainFrameLayout.Remove (btnTip);
                                        };
                                        System.Threading.Tasks.Task.Run (() => {
                                            System.Threading.Thread.Sleep (3000);
                                            Application.RunOnMainThread (() => {
                                            btnTip.RemoveFromParent ();
                                                btnTip.RemoveFromParent ();
                                                //MainPage.MainFrameLayout.Remove (btnTip);
                                                showTip = false;
                                                //showTip = false;
                                            });
                                        });
                                    }
                                    });
                                }
                            } catch { }
                        });
                            }
                        } catch { }
                    }
                } else {
                    foreach (var common in tempScene.GlobalSceneDeviceList) {
@@ -517,8 +535,310 @@
                        }
                    }
                }
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                });
            });
        }
        /// <summary>
        /// 2020-06-05 加锁
        /// </summary>
        private readonly object SendLocker = new object ();
        public void ControlScene (string sceneFilePath)
        {
            if (CommonPage.IsRemote) {
                if (!SmartHome.MqttCommon.IsGatewayOnline) {
                    new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                    return;
                }
            }
            var tempScene = Scene.GetSceneByFilePath (sceneFilePath);
            if (tempScene == null)
                return;
            showTip = true;
            MainPage.Loading.Start ("Sending...");
            System.Threading.Tasks.Task.Run (() => {
                int sceneDeviceCount = tempScene.DeviceFilePathList.Count;
                int controlDeviceIndex = 0;
                int mSendCount = 0;
                byte [] replyBytes = null;
                if (!tempScene.busScene) {
                    foreach (var deviceFilePath in tempScene.DeviceFilePathList) {
                        controlDeviceIndex++;
                        new System.Threading.Thread (() => {
                            try {
                                lock (SendLocker) {
                                    mSendCount++;
                                }
                                //mSendCount++;
                                var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (common.Type == DeviceType.LightDimming) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, device.DelayTimeHeight, device.DelayTimeLow });
                                    }
                                } else if (common.Type == DeviceType.LightEnergySocket) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightEnergySocket> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, device.DelayTimeHeight, device.DelayTimeLow });
                                    }
                                } else if (common.Type == DeviceType.LightEnergySwitch) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightEnergySwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, device.DelayTimeHeight, device.DelayTimeLow });
                                    }
                                } else if (common.Type == DeviceType.LightRGB) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetLogicLoopColor, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 254, device.DelayTimeHeigh,device.DelayTimeLow,
                                                    3,device.RStatus,device.GStatus,device.BStatus,0,0});
                                    }
                                } else if (common.Type == DeviceType.LightSwitch) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, device.DelayTimeHeight, device.DelayTimeLow });
                                    }
                                } else if (common.Type == DeviceType.CurtainModel) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainModel> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, (byte)device.Status });
                                    }
                                } else if (common.Type == DeviceType.CurtainRoller) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainRoller> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, (byte)device.Status });
                                    }
                                } else if (common.Type == DeviceType.CurtainTrietex) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainTrietex> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, (byte)device.Status });
                                    }
                                } else if (common.Type == DeviceType.HVAC || common.Type == DeviceType.ACInfrared) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, device.SubnetID, device.DeviceID, new byte [] {
                                                device.LoopID,
                                                device.TemperatureMode,
                                                device.IndoorTemperature,
                                                device.CoolTemperature,
                                                device.HeatTemperature,
                                                device.AutoTemperature,
                                                device.ChuShiTemperature,
                                                device.RealModeAndFanSpeed,
                                                device.Power,
                                                device.SetMode,
                                                device.SetFanSpeed,
                                                device.SetTemperature,
                                                device.ShaoFanMode});
                                    }
                                } else if (common.Type == DeviceType.FoolHeat) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FoolHeat> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetFoolHeat, device.SubnetID, device.DeviceID,
                                                                                    new byte [] { device.LoopID, (byte)(device.Status + device.WorkingMode * 16), 0,device.WorkingMode,device.NormalTemperature, device.DayTemperature,
                               device.NightTemperature, device.AwayTemperature , 0, 0 });
                                    }
                                } else if (common.Type == DeviceType.FanModule) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FanModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, (byte)device.WindSpeed });
                                    }
                                } else if (common.Type == DeviceType.LogicModule) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LogicModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    //if (device == null) {
                                    //    mSendCount--;
                                    //    continue;
                                    //}
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, device.SubnetID, device.DeviceID, new byte [] {
                            device.AreaID,device.AreaSceneID});
                                    }
                                } else if (common.Type == DeviceType.UniversalDevice) {//2020-09-02 增加通用开关
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<UniversalDevice> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    if (device == null) {
                                        replyBytes = new byte [] { 0x00 };
                                    } else {
                                        replyBytes = Control.ControlBytesSendHasReturn (Command.SetCommonSwitch, device.SubnetID, device.DeviceID, new byte [] { device.SendBytes [0], device.SendBytes [1] });
                                    }
                                }
                                try {
                                    if (showTip) {
                                        if (replyBytes == null) {
                                            Application.RunOnMainThread (() => {
                                                string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
                                                var btnTip = new Button () {
                                                    Height = Application.GetRealHeight (140),
                                                    Text = common.Name + tipString + "..." + controlDeviceIndex.ToString () + @"/" + sceneDeviceCount.ToString (),
                                                    BackgroundColor = SkinStyle.Current.TitileView,
                                                };
                                                MainPage.MainFrameLayout.AddChidren (btnTip);
                                                btnTip.MouseUpEventHandler += (sender, e) => {
                                                    btnTip.RemoveFromParent ();
                                                    showTip = false;
                                                    //MainPage.MainFrameLayout.Remove (btnTip);
                                                };
                                                System.Threading.Tasks.Task.Run (() => {
                                                    System.Threading.Thread.Sleep (3000);
                                                    Application.RunOnMainThread (() => {
                                                        btnTip.RemoveFromParent ();
                                                        //MainPage.MainFrameLayout.Remove (btnTip);
                                                        //showTip = false;
                                                    });
                                                });
                                            });
                                        }
                                    }
                                } catch {
                                }
                                //mSendCount--;
                            } catch { } finally {
                                lock (SendLocker) {
                                    mSendCount--;
                                }
                            }
                        }) { IsBackground = true }.Start ();
                        //等待100ms再发送第二个设备数据
                        System.Threading.Thread.Sleep (100);
                    }
                    //
                    while (mSendCount > 0) {
                        System.Threading.Thread.Sleep (100);
                    }
                } else {
                    foreach (var common in tempScene.GlobalSceneDeviceList) {
                        switch (common.obj1) {
                        case 0://0   无效 Invalid
                            break;
                        case 85://0x55  场景 Scene ||0x0002
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 86://0x56  序列 Sequence ||0x001A
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetSeries, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 88://0x58  通用开关 Universal Switch ||0xe01c
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetCommonSwitch, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 89: //0x59  单路调节 Single Channel Lighting Control  0031
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 92://0x5c  窗帘开关 Curtain Switch e3e0
                            replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 94://0x5e  GPRS控制 GPRS Control E3D4
                            replyBytes = Control.ControlBytesSendHasReturn (Command.GPRSControl, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 95://0x5f  面板控制 Panel Control e3d8
                            replyBytes = Control.ControlBytesSendHasReturn (Command.InstructionPanelKey, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 100://0x64 广播场景 Broadcast Scene ||0x0002
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 101://0x65 广播回路 Broadcast Channel 0031
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 102://0x66 消防模块 Security Module 011E
                            replyBytes = Control.ControlBytesSendHasReturn (Command.SecurityModule, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                        case 103://0x67 音乐播放 Music Control 0218
                            replyBytes = Control.ControlBytesSendHasReturn (Command.MusicControl, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            break;
                            //104 通用控制 Universal Control
                            //105 连接页 Link Page
                            //107 RGB调节 RGB Control
                            //108 红外控制 Infrared Control
                            //case 109://109 逻辑灯调节 Logic Light Adjust ||  0xE45E
                            //    replyBytes = Control.ControlBytesSendHasReturn (Command.LogicLightAdjust, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                            //    break;
                            //}
                            //110 逻辑场景 Logic Scene
                            //112 LCD & LED休眠 LCD & LED Sleep
                            //113 服务器控制   Server control
                        }
                    }
                }
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                });
            });
        }
        public void ControlBusSceneDevice (Common common)
        {