From f500e14c0a994487070380c50c85e0929cbc8e63 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期二, 05 一月 2021 10:48:04 +0800
Subject: [PATCH] 2021-01-05 1.更新

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Scene/SceneMethod.cs |  346 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 333 insertions(+), 13 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Scene/SceneMethod.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Scene/SceneMethod.cs
index 39d63af..1acb95e 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Scene/SceneMethod.cs
+++ b/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)
         {

--
Gitblit v1.8.0