JLChen
2020-02-20 965290476c027deb96891cde1095d9a100581a6b
Crabtree/SmartHome/UI/SimpleControl/Phone/AC/UserDeviceToAC.cs
@@ -252,18 +252,21 @@
                    };
                    acView.AddChidren (tempSiwtch);
                    tempSiwtch.MouseUpEventHandler += (sender3, e3) => {
                        tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                        ac.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0;
                        //tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                        //ac.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0;
                        byte bPower = tempSiwtch.IsSelected ? (byte)0 : (byte)1;
                        if (ac.Type == DeviceType.CustomAC) {
                            var acCommon = common as CustomAC;
                            acCommon.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0;
                            acCommon.Power = bPower;
                            var controlCommand = acCommon.customACControlLists.Find ((obj) => obj.UniversalModeType == acCommon.Power);
                            if (controlCommand != null) {
                                Control.ControlBytesSend (Command.SetCommonSwitch, acCommon.SubnetID, acCommon.DeviceID, new byte [] { controlCommand.UniversalID, 255 }, SendCount.Zero);
                                IO.FileUtils.SaveEquipmentMessage (acCommon, acCommon.LoopID.ToString ());
                            }
                        } else {
                            acBLL.ControlAC (ac);
                            //acBLL.ControlAC (ac);
                            SendControlAc (ac, bPower);
                        }
                    };
@@ -274,10 +277,22 @@
                }
            }
            if (devcieCount > 0) {
                btnAllON.MouseUpEventHandler += (sender, e) => {
                    btnAllON.TextColor = SkinStyle.Current.TextColor1;
                    controlAllAC (1);
                    Alert alert = new Alert ("",
                                    "Open All AC",
                                   Language.StringByID (R.MyInternationalizationString.Cancel),
                                   Language.StringByID (R.MyInternationalizationString.Confrim));
                    alert.Show ();
                    alert.ResultEventHandler += (sender2, e2) => {
                        if (e2) {
                            controlAllAC (1);
                        }
                    };
                    //controlAllAC (1);
                };
                btnAllON.MouseDownEventHandler += (ss, dd) => {
                    btnAllON.TextColor = SkinStyle.Current.SelectedColor;
@@ -285,9 +300,20 @@
                powerView.AddChidren (btnAllOff);
                btnAllOff.MouseUpEventHandler += (sender, e) => {
                    btnAllOff.TextColor = SkinStyle.Current.TextColor1;
                    controlAllAC (0);
                    //controlAllAC (0);
                    Alert alert = new Alert ("",
                              "Close All AC",
                             Language.StringByID (R.MyInternationalizationString.Cancel),
                             Language.StringByID (R.MyInternationalizationString.Confrim));
                    alert.Show ();
                    alert.ResultEventHandler += (sender2, e2) => {
                        if (e2) {
                            controlAllAC (0);
                        }
                    };
                };
                btnAllON.MouseDownEventHandler += (ss, dd) => {
                btnAllOff.MouseDownEventHandler += (ss, dd) => {
                    btnAllOff.TextColor = SkinStyle.Current.SelectedColor;
                };
            }
@@ -299,6 +325,14 @@
        /// </summary>
        void controlAllAC (byte b)
        {
            if (CommonPage.IsRemote) {
                if (!SmartHome.MqttCommon.IsGatewayOnline) {
                    new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                    return;
                }
            }
            MainPage.Loading.Start ("Sending...");
            System.Threading.Tasks.Task.Run (() => {
                foreach (var room in Room.Lists) {
                    if (room == null)
@@ -310,9 +344,46 @@
                        return obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared || obj.Type == DeviceType.HVAC;
                    });
                    foreach (var common in list) {
                        //2020-02-10
                        byte [] returnBytes = null;
                        var acCommon = common as AC;
                        acCommon.Power = b;
                        acBLL.ControlAC (acCommon);
                        //acBLL.ControlAC (acCommon);
                        if (acCommon.Type == DeviceType.ACPanel) {
                            returnBytes = Control.ControlBytesSendHasReturn (Command.InstructionPanelKey, acCommon.SubnetID, acCommon.DeviceID, new byte [] { 3, acCommon.Power, acCommon.LoopID });
                        } else if (acCommon.Type == DeviceType.ACDevice || acCommon.Type == DeviceType.HVAC || acCommon.Type == DeviceType.ACInfrared) {
                            returnBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, acCommon.SubnetID, acCommon.DeviceID,
                                new byte [] {acCommon.LoopID,acCommon.TemperatureMode,acCommon.IndoorTemperature,acCommon.CoolTemperature,
                                    acCommon.HeatTemperature,acCommon.AutoTemperature,acCommon.ChuShiTemperature,acCommon.RealModeAndFanSpeed,
                                    acCommon.Power,acCommon.SetMode,acCommon.SetFanSpeed,acCommon.SetTemperature,acCommon.ShaoFanMode});
                        } else {
                            continue;
                        }
                        if (returnBytes == null) {
                            Application.RunOnMainThread (() => {
                                string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
                                var btnTip = new Button () {
                                    Height = Application.GetRealHeight (140),
                                    Text = acCommon.Name + tipString,// + "..." + controlDeviceIndex.ToString () + @"/" + sceneDeviceCount.ToString (),
                                    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 ();
                                    });
                                });
                            });
                        }
                    }
                    var listCustomAC = room.DeviceList.FindAll ((obj) => {
                        return obj.Type == DeviceType.CustomAC;
@@ -326,19 +397,36 @@
                        }
                    }
                }
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                });
            });
        }
        /// <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) {
                    Console.WriteLine ("IsReadingAll AC 读取中");
                    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) => {
@@ -354,6 +442,7 @@
            }
            thread = new Thread (() => {
                try {
                    IsReadingAll = true;
                    System.Collections.Generic.List<string> readList = new System.Collections.Generic.List<string> ();
                    foreach (Room room in Room.Lists) {
                        var list = room.DeviceList.FindAll ((obj) => {
@@ -374,14 +463,45 @@
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 8, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 19, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadPanleTemp, ac.SubnetID, ac.DeviceID, new byte [] { ac.LoopID }, SendCount.Zero);
                            }
                            }
                        }
                    }
                } catch { } finally {
                    IsReadingAll = false;
                } catch {
                    IsReadingAll = false;
                } finally {
                    thread = null;
                }
            });
            thread.Start ();
        }
        /// <summary>
        /// 空调控制,带返回结果
        /// </summary>
        /// <param name="mAcData"></param>
        /// <param name="bPower"></param>
        void SendControlAc (AC mAcData, byte bPower)
        {
            MainPage.Loading.Start ("Sending...");
            System.Threading.Tasks.Task.Run (() => {
                byte [] returnBytes = null;
                if (mAcData.Type == DeviceType.ACPanel) {
                    returnBytes = Control.ControlBytesSendHasReturn (Command.InstructionPanelKey, mAcData.SubnetID, mAcData.DeviceID, new byte [] { 3, bPower, mAcData.LoopID });
                } else if (mAcData.Type == DeviceType.ACDevice || mAcData.Type == DeviceType.HVAC || mAcData.Type == DeviceType.ACInfrared) {
                    returnBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, mAcData.SubnetID, mAcData.DeviceID, new [] { mAcData.LoopID, mAcData.TemperatureMode, mAcData.IndoorTemperature, mAcData.CoolTemperature, mAcData.HeatTemperature, mAcData.AutoTemperature, mAcData.ChuShiTemperature, mAcData.RealModeAndFanSpeed, bPower, mAcData.SetMode, mAcData.SetFanSpeed, mAcData.SetTemperature, mAcData.ShaoFanMode });
                }
                Application.RunOnMainThread (() => {
                    if (returnBytes == null) {
                        new Alert ("", mAcData.Name + ErrorCode.ControlFailure, "Close").Show ();
                    }
                    MainPage.Loading.Hide ();
                });
            });
        }
    }
}