JLChen
2021-01-11 5a5a2b696866f947b6025d26c3302e8ffef46435
Crabtree/SmartHome/UI/SimpleControl/Phone/Light/UserLightPage.cs
@@ -131,7 +131,7 @@
            };
            var btnAddLightScene = new Button () {
                Width = Application.GetRealWidth (100),
                Height = Application.GetRealHeight (100),
                Height = Application.GetRealWidth (100),
                X = Application.GetRealWidth (640 - 120),
                Y = Application.GetRealHeight (20),
                UnSelectedImagePath = "Light/UserLightAddScene.png",
@@ -256,7 +256,7 @@
                };
                Button btnSIcon = new Button () { 
                    Width = Application.GetRealWidth(111),
                    Height = Application.GetRealHeight(110),
                    Height = Application.GetRealWidth(110),
                    Gravity = Gravity.CenterVertical,
                    UnSelectedImagePath = scene.SceneIconPath,
                    SelectedImagePath = scene.SceneIconPath,
@@ -279,11 +279,20 @@
                btnSceneRemark.MouseUpEventHandler += (sender, e) => {
                    try {
                        if (CommonPage.IsRemote) {
                            if (!SmartHome.MqttCommon.IsGatewayOnline) {
                                new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                                return;
                            }
                        }
                        btnLastClickScene.RemoveFromParent ();
                        lightSceneView.AddChidren (btnLastClickScene);
                        MainPage.Loading.Start ();
                        //MainPage.Loading.Start ();
                        MainPage.Loading.Start ("Sending...");
                        int controlDeviceIndex = 0;
                        System.Threading.Tasks.Task.Run (() => {
                            foreach (var deviceFilePath in scene.DeviceFilePathList) {
                                controlDeviceIndex++;
                                if (deviceFilePath.Split ('_') [1] == DeviceType.LightDimming.ToString ()) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    if (device == null) {
@@ -305,13 +314,15 @@
                                            }
                                        }
                                    }
                                    Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 0, delayTime });
                                    byte [] returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 0, delayTime });
                                    ShowErrorTip (returnBytes, device, "..." + controlDeviceIndex.ToString () + @"/" + scene.DeviceFilePathList.Count.ToString ());
                                } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightSwitch.ToString ()) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    if (device == null) {
                                        continue;
                                    }
                                    Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 0, 0 });
                                    byte [] returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 0, 0 });
                                    ShowErrorTip (returnBytes, device, "..." + controlDeviceIndex.ToString () + @"/" + scene.DeviceFilePathList.Count.ToString ());
                                } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightLogic.ToString ()) {
                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                    if (device != null) {
@@ -339,12 +350,47 @@
                                    }
                                }
                            }
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                            });
                        });
                    } catch { } finally {
                    } catch {
                        MainPage.Loading.Hide ();
                    } finally {
                        //MainPage.Loading.Hide ();
                    }
                };
            }
        }
        void ShowErrorTip (byte[] replyBytes, Common mCommon, string mes)
        {
            try {
                if (replyBytes == null) {
                    Application.RunOnMainThread (() => {
                        string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
                        var btnTip = new Button () {
                            Height = Application.GetRealHeight (140),
                            Text = mCommon.Name + tipString + mes,
                            BackgroundColor = SkinStyle.Current.TitileView,
                        };
                        MainPage.MainFrameLayout.AddChidren (btnTip);
                        btnTip.MouseUpEventHandler += (sender, e) => {
                            btnTip.RemoveFromParent ();
                        };
                        System.Threading.Tasks.Task.Run (() => {
                            System.Threading.Thread.Sleep (3000);
                            Application.RunOnMainThread (() => {
                                btnTip.RemoveFromParent ();
                            });
                        });
                    });
                }
            } catch { }
        }
        void LightSceneDialog (Scene scene = null)
@@ -603,9 +649,9 @@
                };
                lightView.AddTag ("UpdataKey", common.CommonLoopID);
                lightListView.AddChidren (lightView);
                Button BtnLightViewNull = new Button () {
                    Y = Application.GetRealHeight(127),
                    Height = Application.GetRealHeight(3),
                Button BtnLightViewNull = new Button () {
                    Y = Application.GetRealHeight (127),
                    Height = Application.GetRealHeight (3),
                    BackgroundColor = SkinStyle.Current.MainColor,
                    SelectedBackgroundColor = SkinStyle.Current.MainColor,
                };
@@ -624,8 +670,8 @@
                lightView.AddChidren (tempLightName);
                var tempSiwtch = new Button () {
                    Width = Application.GetRealWidth (85),
                    Height = Application.GetRealHeight (53),
                    Width = Application.GetMinRealAverage (85),
                    Height = Application.GetMinRealAverage (53),
                    X = Application.GetRealWidth (640 - 90 - 20),
                    Gravity = Gravity.CenterVertical,
                    UnSelectedImagePath = "Item/SwitchClose.png",
@@ -633,16 +679,21 @@
                };
                lightView.AddChidren (tempSiwtch);
                tempSiwtch.MouseUpEventHandler += (sender3, e3) => {
                    tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                    if (tempSiwtch.IsSelected) {
                        lightSwitch.CurrentBrightness = 100;
                    } else {
                        lightSwitch.CurrentBrightness = 0;
                    }
                    //tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                    //if (tempSiwtch.IsSelected) {
                    //    lightSwitch.CurrentBrightness = 100;
                    //} else {
                    //    lightSwitch.CurrentBrightness = 0;
                    //}
                    int mSetBrightness = tempSiwtch.IsSelected ? 0 : 100;
                    if (lightSwitch.Type == DeviceType.LightMixSwitch) {
                        Control.ControlBytesSend (Command.SetSingleLight, lightSwitch.SubnetID, lightSwitch.DeviceID, new byte [] { (lightSwitch as LightMixSwitch).PhysicsLoopID, (byte)lightSwitch.CurrentBrightness, lightSwitch.DelayTimeHeight, lightSwitch.DelayTimeLow });
                    } else
                        Control.ControlBytesSend (Command.SetSingleLight, lightSwitch.SubnetID, lightSwitch.DeviceID, new byte [] { lightSwitch.LoopID, lightSwitch.CurrentBrightness, lightSwitch.DelayTimeHeight, lightSwitch.DelayTimeLow });
                        SendControl.SendControlSetSingleLight (lightSwitch, new byte [] { (lightSwitch as LightMixSwitch).PhysicsLoopID, (byte)mSetBrightness, lightSwitch.DelayTimeHeight, lightSwitch.DelayTimeLow });
                        //Control.ControlBytesSend (Command.SetSingleLight, lightSwitch.SubnetID, lightSwitch.DeviceID, new byte [] { (lightSwitch as LightMixSwitch).PhysicsLoopID, (byte)lightSwitch.CurrentBrightness, lightSwitch.DelayTimeHeight, lightSwitch.DelayTimeLow });
                    } else {
                        //Control.ControlBytesSend (Command.SetSingleLight, lightSwitch.SubnetID, lightSwitch.DeviceID, new byte [] { lightSwitch.LoopID, lightSwitch.CurrentBrightness, lightSwitch.DelayTimeHeight, lightSwitch.DelayTimeLow });
                        SendControl.SendControlSetSingleLight (lightSwitch, new byte [] { lightSwitch.LoopID, (byte)mSetBrightness, lightSwitch.DelayTimeHeight, lightSwitch.DelayTimeLow });
                    }
                };
                if (lightSwitch.CurrentBrightness == 100) {
@@ -978,8 +1029,8 @@
                };
                //开关按键
                var tempSwitch = new Button () {
                    Width = Application.GetRealWidth (90),
                    Height = Application.GetRealHeight (53),
                    Width = Application.GetMinRealAverage (90),
                    Height = Application.GetMinRealAverage (53),
                    X = Application.GetRealWidth (640 - 90 - 20),
                    Gravity = Gravity.CenterVertical,
                    UnSelectedImagePath = "Item/SwitchClose.png",
@@ -1039,21 +1090,25 @@
                tempLightSeekBar.AddTag ("UpdataKey", lightDimming.CommonLoopID);
                tempSwitch.MouseUpEventHandler += (sender3, e3) => {
                    tempSwitch.IsSelected = !tempSwitch.IsSelected;
                    //tempSwitch.IsSelected = !tempSwitch.IsSelected;
                    if (lightDimming.LastOpenBrightness == 0) {
                        lightDimming.LastOpenBrightness = 100;
                    }
                    if (tempSwitch.IsSelected) {
                        tempLightSeekBar.Progress = lightDimming.LastOpenBrightness;
                        IO.FileUtils.WriteFileByBytes ("Equipment_" + typeof (LightDimming).Name + "_" + lightDimming.SubnetID.ToString () + "_" + lightDimming.DeviceID.ToString () + "_" + (lightDimming.LoopID < 10 ? "0" + lightDimming.LoopID.ToString () : lightDimming.LoopID.ToString ()),
                                                       CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (lightDimming)));
                    } else {
                        tempLightSeekBar.Progress = 0;
                    }
                    lightDimming.CurrentBrightness = (byte)tempLightSeekBar.Progress;
                    //if (!tempSwitch.IsSelected) {
                    //    lightDimming.CurrentBrightness = lightDimming.LastOpenBrightness;
                    //    //tempLightSeekBar.Progress = lightDimming.LastOpenBrightness;
                    //    IO.FileUtils.WriteFileByBytes ("Equipment_" + typeof (LightDimming).Name + "_" + lightDimming.SubnetID.ToString () + "_" + lightDimming.DeviceID.ToString () + "_" + (lightDimming.LoopID < 10 ? "0" + lightDimming.LoopID.ToString () : lightDimming.LoopID.ToString ()),
                    //                                   CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (lightDimming)));
                    //} else {
                    //    //tempLightSeekBar.Progress = 0;
                    //    lightDimming.CurrentBrightness = 0;
                    //}
                    //lightDimming.CurrentBrightness = (byte)tempLightSeekBar.Progress;
                    int mSetBrightness = tempSwitch.IsSelected ? 0 : lightDimming.LastOpenBrightness;
                    byte delayHeight = 0;
                    byte delayLow = 0;
                    if (lightDimming.CurrentBrightness == 0) {
                    if (mSetBrightness == 0) {
                        delayHeight = Convert.ToByte (lightDimming.CustomDelayTimeClose / 256);
                        delayLow = Convert.ToByte (lightDimming.CustomDelayTimeClose % 256);
                    } else {
@@ -1061,10 +1116,14 @@
                        delayLow = Convert.ToByte (lightDimming.CustomDelayTimeOpen % 256);
                    }
                    if (lightDimming.Type == DeviceType.LightMixDimming) {
                        Control.ControlBytesSend (Command.SetSingleLight, lightDimming.SubnetID, lightDimming.DeviceID, new byte [] { (lightDimming as LightMixDimming).PhysicsLoopID, (byte)tempLightSeekBar.Progress, delayHeight, delayLow }, SendCount.Zero);
                    } else
                        Control.ControlBytesSend (Command.SetSingleLight, lightDimming.SubnetID, lightDimming.DeviceID, new byte [] { lightDimming.LoopID, (byte)tempLightSeekBar.Progress, delayHeight, delayLow }, SendCount.Zero);
                    tempLightSeekBar.Progress = lightDimming.CurrentBrightness;
                        SendControl.SendControlSetSingleLight (lightDimming, new byte [] { (lightDimming as LightMixDimming).PhysicsLoopID, (byte)mSetBrightness, delayHeight, delayLow });
                        //Control.ControlBytesSend (Command.SetSingleLight, lightDimming.SubnetID, lightDimming.DeviceID, new byte [] { (lightDimming as LightMixDimming).PhysicsLoopID, (byte)tempLightSeekBar.Progress, delayHeight, delayLow }, SendCount.Zero);
                    } else {
                        //Control.ControlBytesSend (Command.SetSingleLight, lightDimming.SubnetID, lightDimming.DeviceID, new byte [] { lightDimming.LoopID, (byte)tempLightSeekBar.Progress, delayHeight, delayLow }, SendCount.Zero);
                        SendControl.SendControlSetSingleLight (lightDimming, new byte [] { lightDimming.LoopID, (byte)mSetBrightness, delayHeight, delayLow });
                    }
                    //tempLightSeekBar.Progress = lightDimming.CurrentBrightness;
                };
                if (lightDimming.CurrentBrightness > 0) {
@@ -1135,7 +1194,7 @@
                lightView.AddChidren (btnLightSetting);
                Button btnRGBSetting = new Button () {
               Width = Application.GetRealWidth (60),
               Height = Application.GetRealHeight (60),
               Height = Application.GetRealWidth (60),
               Radius = (uint)Application.GetRealHeight (30),
               X = tempLightSeekBar.Right + Application.GetRealWidth (20),
               Gravity = Gravity.CenterVertical,
@@ -1508,8 +1567,8 @@
                #endregion
                //开关按键
                var tempSwitch = new Button () {
                    Width = Application.GetRealWidth (90),
                    Height = Application.GetRealHeight (53),
                    Width = Application.GetMinRealAverage (90),
                    Height = Application.GetMinRealAverage (53),
                    X = Application.GetRealWidth (640 - 90 - 20),
                    Gravity = Gravity.CenterVertical,
                    UnSelectedImagePath = "Item/SwitchClose.png",