JLChen
2020-04-03 be95e839f40eb3ddf64706b60cba6dfcf4fad5e5
Crabtree/SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToSocket.cs
@@ -94,7 +94,7 @@
                        continue;
                    }
                    foreach (var light in room.DeviceList) {
                        if (light.Type == DeviceType.LightSwitchSocket) {
                        if (light.Type == DeviceType.LightEnergySocket) {
                            if ((light as LightSwitch).CurrentBrightness == 100)
                                brighingCount++;
                        }
@@ -149,7 +149,7 @@
                                if (roomRow.Tag.ToString () == room.Name) {
                                    int oCount = 0;
                                    foreach (var light in room.DeviceList) {
                                        if (light.Type == DeviceType.LightSwitchSocket) {
                                        if (light.Type == DeviceType.LightEnergySocket) {
                                            if ((light as LightSwitch).CurrentBrightness == 100)
                                                oCount++;
                                        }
@@ -185,8 +185,14 @@
        /// 控制所有的灯
        /// </summary>
        /// <param name="b">The blue component.</param>
        void controlAllLight (byte b)
        void controlAllLightOld (byte b)
        {
            if (CommonPage.IsRemote) {
                if (!SmartHome.MqttCommon.IsGatewayOnline) {
                    new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                    return;
                }
            }
            showTip = true;
            System.Threading.Tasks.Task.Run (() => {
@@ -201,40 +207,116 @@
                        foreach (var common in room.DeviceList) {
                            //controlDeviceIndex++;
                            byte [] returnBytes = null;
                            if (common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySocket) {
                            if (common.Type == DeviceType.LightEnergySocket) {
                                returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, b, 0, 0 });
                                if (showTip) {
                                    Application.RunOnMainThread (() => {
                                        if (returnBytes == null) {
                                            string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
                                    if (returnBytes == null) {
                                        Application.RunOnMainThread (() => {
                                            var btnTip = new Button () {
                                                Height = Application.GetRealHeight (140),
                                                Text = common.Name + tipString,// + "..." + controlDeviceIndex.ToString () + @"/" + sceneDeviceCount.ToString (),
                                                BackgroundColor = 0xFF000000,
                                                //TextColor = 0xFF000000
                                                Text = common.Name + ErrorCode.ControlFailure,
                                                BackgroundColor = 0xFFD7D7D7,
                                            };
                                            MainPage.MainFrameLayout.AddChidren (btnTip);
                                            btnTip.MouseUpEventHandler += (sender, e) => {
                                                btnTip.RemoveFromParent ();
                                                //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 (Exception ex) {
                    Console.WriteLine (ex.Message);
                    Utlis.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
        }
        /// <summary>
        /// 控制所有的灯
        /// </summary>
        /// <param name="b">The blue component.</param>
        void controlAllLight (byte b)
        {
            if (CommonPage.IsRemote) {
                if (!SmartHome.MqttCommon.IsGatewayOnline) {
                    new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                    return;
                }
            }
            showTip = true;
            System.Threading.Tasks.Task.Run (() => {
                try {
                    int mSendCount = 0;
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Start ("Sending...");
                    });
                    foreach (var room in Room.Lists) {
                        if (string.IsNullOrEmpty (room.Name)) {
                            continue;
                        }
                        foreach (var common in room.DeviceList) {
                            //controlDeviceIndex++;
                            byte [] returnBytes = null;
                            if (common.Type == DeviceType.LightEnergySocket) {
                                new System.Threading.Thread (() => {
                                    mSendCount++;
                                    returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, b, 0, 0 });
                                    if (showTip) {
                                        if (returnBytes == null) {
                                            Application.RunOnMainThread (() => {
                                                var btnTip = new Button () {
                                                    Height = Application.GetRealHeight (140),
                                                    Text = common.Name + ErrorCode.ControlFailure,
                                                    BackgroundColor = 0xFFD7D7D7,
                                                };
                                                MainPage.MainFrameLayout.AddChidren (btnTip);
                                                btnTip.MouseUpEventHandler += (sender, e) => {
                                                    btnTip.RemoveFromParent ();
                                                };
                                                System.Threading.Tasks.Task.Run (() => {
                                                    System.Threading.Thread.Sleep (3000);
                                                    Application.RunOnMainThread (() => {
                                                        btnTip.RemoveFromParent ();
                                                    });
                                                });
                                            });
                                        }
                                    }
                                    mSendCount--;
                                }) { IsBackground = true}.Start();
                                //等待100ms再发送第二个设备数据
                                System.Threading.Thread.Sleep (100);
                            }
                        }
                    }
                    while (mSendCount > 0) {
                        System.Threading.Thread.Sleep (100);
                    }
                } catch (Exception ex) {
                    Utlis.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
@@ -332,7 +414,7 @@
                    continue;
                }
                foreach (var common in room.DeviceList) {
                    if (common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySocket) {
                    if (common.Type == DeviceType.LightEnergySocket) {
                        LightCount++;
                        var light = common as LightSwitch;
                        var lightView = new FrameLayout () {
@@ -364,13 +446,15 @@
                        };
                        lightView.AddChidren (tempSwitch);
                        tempSwitch.MouseUpEventHandler += (sender3, e3) => {
                            tempSwitch.IsSelected = !tempSwitch.IsSelected;
                            if (tempSwitch.IsSelected) {
                                light.CurrentBrightness = 100;
                            } else {
                                light.CurrentBrightness = 0;
                            }
                            Control.ControlBytesSend (Command.SetSingleLight, light.SubnetID, light.DeviceID, new byte [] { light.LoopID, light.CurrentBrightness, light.DelayTimeHeight, light.DelayTimeLow });
                            //tempSwitch.IsSelected = !tempSwitch.IsSelected;
                            //if (tempSwitch.IsSelected) {
                            //    light.CurrentBrightness = 100;
                            //} else {
                            //    light.CurrentBrightness = 0;
                            //}
                            int mSetBrightness = tempSwitch.IsSelected ? 0 : 100;
                            //Control.ControlBytesSend (Command.SetSingleLight, light.SubnetID, light.DeviceID, new byte [] { light.LoopID, light.CurrentBrightness, light.DelayTimeHeight, light.DelayTimeLow });
                            SendControl.SendControlSetSingleLight (light, new byte [] { light.LoopID, (byte)mSetBrightness, light.DelayTimeHeight, light.DelayTimeLow });
                        };
                        if (light.CurrentBrightness == 100) {
@@ -420,6 +504,7 @@
                    foreach (var c in EnergtLightList) {
                        Control.ControlBytesSend (Command.ReadAnalogValue, c.SubnetID, c.DeviceID, new byte [] { 10, c.LoopID }, SendCount.Zero);
                        Control.ControlBytesSend (Command.ReadAnalogValue, c.SubnetID, c.DeviceID, new byte [] { 11, c.LoopID }, SendCount.Zero);
                        System.Threading.Thread.Sleep (200);
                    }
                    Thread.Sleep (10000);
                }
@@ -461,20 +546,31 @@
            #endregion
        }
        /// <summary>
        /// 读取标志
        /// </summary>
        static bool IsReadingAll = false;
        static System.Threading.Thread thread;
        /// <summary>
        /// 读取设备状态
        /// </summary>
        public static void readAllStatus (bool isRead = false)
        public static void readAllStatus (bool isRead = false, bool isCheckIsReadingAll = false)
        {
            if (isCheckIsReadingAll) {
                if (IsReadingAll && !isRead) {
                    Utlis.WriteLine ("IsLightReadingAll 读取中");
                    return;
                }
            }
            if (isRead) {
                foreach (Room room in Room.Lists) {
                    if (string.IsNullOrEmpty(room.Name)) {
                    if (string.IsNullOrEmpty (room.Name)) {
                        continue;
                    }
                    var list = room.DeviceList.FindAll ((obj) => {
                        return obj.Type == DeviceType.LightSwitchSocket;
               });
                        return obj.Type == DeviceType.LightEnergySocket;
                    });
                    foreach (Common common in list) {
                        common.LastUpdateTime = DateTime.MinValue;
                    }
@@ -485,29 +581,36 @@
            }
            thread = new Thread (() => {
                try {
                System.Collections.Generic.List<string> readList = new System.Collections.Generic.List<string> ();
                foreach (Room room in Room.Lists) {
                    if (string.IsNullOrEmpty(room.Name)) {
                        continue;
                    }
                    var list = room.DeviceList.FindAll ((obj) => {
                        return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && obj.Type == DeviceType.LightSwitchSocket;
                    });
                    foreach (Common common in list) {
                        var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID;
                        //已经读取过当前设备就不再读取
                        if (readList.Contains (s)) {
                    IsReadingAll = true;
                    System.Collections.Generic.List<string> readList = new System.Collections.Generic.List<string> ();
                    foreach (Room room in Room.Lists) {
                        if (string.IsNullOrEmpty (room.Name)) {
                            continue;
                        }
                        readList.Add (s);
                        Control.ControlBytesSendHasReturn (Command.ReadLightEquipmentAllLoopBrightness, common.SubnetID, common.DeviceID, new byte [] { });
                        var list = room.DeviceList.FindAll ((obj) => {
                            return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && obj.Type == DeviceType.LightEnergySocket;
                        });
                        foreach (Common common in list) {
                            var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID;
                            //已经读取过当前设备就不再读取
                            if (readList.Contains (s)) {
                                continue;
                            }
                            readList.Add (s);
                            Control.ControlBytesSend (Command.ReadLightEquipmentAllLoopBrightness, common.SubnetID, common.DeviceID, new byte [] { });
                            Thread.Sleep (200);
                        }
                    }
                }
             } catch { } finally {
                    IsReadingAll = false;
                } catch {
                    IsReadingAll = false;
                } finally {
                    thread = null;
                }
            });
            thread.Start ();
        }
    }
}