using System; using System.Threading; namespace Shared.SimpleControl.Phone { /// /// 空调设备房间 /// public class UserDeviceToAC : FrameLayout { /// /// 当前界面 /// static UserDeviceToAC curView; VerticalScrolViewLayout equipmentListScrolView; static ACMethod acBLL; /// /// 构造函数 /// public UserDeviceToAC () { curView = this; BackgroundColor = SkinStyle.Current.MainColor; readAllStatus (); } /// /// 更新当前空调开关 - 针对单一属性更新 /// public static void UpdatePower (string updateFalg,byte power) { #region 更新用户主界面灯光点亮总数 int acOponeCount = 0; foreach (var room in Room.Lists) { if (string.IsNullOrEmpty (room.Name)) { continue; } foreach (var acTemp in room.DeviceList) { if (acTemp.Type == DeviceType.HVAC || acTemp.Type == DeviceType.ACInfrared || acTemp.Type == DeviceType.ACPanel || acTemp.Type == DeviceType.LongXiAC) if ((acTemp as AC).Power == 1) { acOponeCount++; } } } UserDeviceView.UpdataDeviceCountNumber (acOponeCount, R.MyInternationalizationString.AC); #endregion Application.RunOnMainThread (() => { if (curView == null) { return; } for (int i = 0; i < curView.equipmentListScrolView.ChildrenCount; i++) { if (curView.equipmentListScrolView.GetChildren (i).GetType () == typeof (RowLayout)) { var acRow = (RowLayout)curView.equipmentListScrolView.GetChildren (i); if (acRow.Tag.ToString () != updateFalg) { continue; } //注意全局变量只能针对同一个空调,这里是多个空调,所以变量不能用全局变量 for (int j = 0; j < acRow.ChildrenCount; j++) { if (acRow.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)acRow.GetChildren (j); if (btn.Tag == null) { continue; } if (btn.Tag.ToString () == "Switch") { if (power == 0) { btn.IsSelected = false; } else { btn.IsSelected = true; } } } } } } }); } /// /// 更新当前空调工作温度 - 针对单一属性更新 /// public static void UpdateWorkingTemp (string updateFalg, byte setWorkingTemp) { Application.RunOnMainThread (() => { if (curView == null) { return; } for (int i = 0; i < curView.equipmentListScrolView.ChildrenCount; i++) { if (curView.equipmentListScrolView.GetChildren (i).GetType () == typeof (RowLayout)) { var acRow = (RowLayout)curView.equipmentListScrolView.GetChildren (i); if (acRow.Tag.ToString () != updateFalg) { continue; } //注意全局变量只能针对同一个空调,这里是多个空调,所以变量不能用全局变量 for (int j = 0; j < acRow.ChildrenCount; j++) { if (acRow.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)acRow.GetChildren (j); if (btn.Tag == null) { continue; } if (btn.Tag.ToString () == "Temperature") { btn.Text = setWorkingTemp + "°C"; } } } } } }); } /// /// 更新当前空调工作模式 - 针对单一属性更新 /// public static void UpdateSetMode (string updateFalg, byte setMode) { Application.RunOnMainThread (() => { if (curView == null) { return; } for (int i = 0; i < curView.equipmentListScrolView.ChildrenCount; i++) { if (curView.equipmentListScrolView.GetChildren (i).GetType () == typeof (RowLayout)) { var acRow = (RowLayout)curView.equipmentListScrolView.GetChildren (i); if (acRow.Tag.ToString () != updateFalg) { continue; } //注意全局变量只能针对同一个空调,这里是多个空调,所以变量不能用全局变量 for (int j = 0; j < acRow.ChildrenCount; j++) { if (acRow.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)acRow.GetChildren (j); if (btn.Tag == null) { continue; } if (btn.Tag.ToString () == "Mode") { acBLL.UpdataACHostModeIcon (setMode, btn); } } } } } }); } /// /// 更新当前空调工作模式 - 针对单一属性更新 /// public static void UpdateFanSpeed (string updateFalg, byte setFanSpeed) { Application.RunOnMainThread (() => { if (curView == null) { return; } for (int i = 0; i < curView.equipmentListScrolView.ChildrenCount; i++) { if (curView.equipmentListScrolView.GetChildren (i).GetType () == typeof (RowLayout)) { var acRow = (RowLayout)curView.equipmentListScrolView.GetChildren (i); if (acRow.Tag.ToString () != updateFalg) { continue; } //注意全局变量只能针对同一个空调,这里是多个空调,所以变量不能用全局变量 for (int j = 0; j < acRow.ChildrenCount; j++) { if (acRow.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)acRow.GetChildren (j); if (btn.Tag == null) { continue; } if (btn.Tag.ToString () == "Wind") { acBLL.UpdataACHostWindIcon (setFanSpeed, btn); } } } } } }); } /// /// 更新当前空调 /// public static void UpdateStatus (AC ac) { #region 更新用户主界面灯光点亮总数 int acOponeCount = 0; foreach (var room in Room.Lists) { if (string.IsNullOrEmpty(room.Name)) { continue; } foreach (var acTemp in room.DeviceList) { if (acTemp.Type == DeviceType.HVAC || acTemp.Type == DeviceType.ACInfrared || acTemp.Type == DeviceType.ACPanel || acTemp.Type == DeviceType.LongXiAC) if ((acTemp as AC).Power == 1) { acOponeCount++; } } } UserDeviceView.UpdataDeviceCountNumber (acOponeCount, R.MyInternationalizationString.AC); #endregion Application.RunOnMainThread (() => { if (curView == null) { return; } for (int i = 0; i < curView.equipmentListScrolView.ChildrenCount; i++) { if (curView.equipmentListScrolView.GetChildren (i).GetType () == typeof (RowLayout)) { var acRow = (RowLayout)curView.equipmentListScrolView.GetChildren (i); if (acRow.Tag.ToString () != ac.CommonLoopID) { continue; } //注意全局变量只能针对同一个空调,这里是多个空调,所以变量不能用全局变量 for (int j = 0; j < acRow.ChildrenCount; j++) { if (acRow.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)acRow.GetChildren (j); if (btn.Tag == null) { continue; } if (btn.Tag.ToString () == "Switch") { if (ac.Power == 0) { btn.IsSelected = false; } else { btn.IsSelected = true; } } else if (btn.Tag.ToString () == "Temperature") { acBLL.UpdataACModeTemperature (ac, ac.SetMode, btn); } else if (btn.Tag.ToString () == "Mode") { if (ac.Type == DeviceType.LongXiAC) { btn.Text = (ac as LongXiAC).modeText; } else { acBLL.UpdataACHostModeIcon (ac.SetMode, btn); } } else if (btn.Tag.ToString () == "Wind") { if (ac.Type == DeviceType.LongXiAC) { btn.Text = (ac as LongXiAC).windText; } else { acBLL.UpdataACHostWindIcon (ac.SetFanSpeed, btn); } } } } } } }); } /// /// 显示房间的所有空调 /// public void showAllRoomAC () { acBLL = new ACMethod (); #region 标题 var topView = new FrameLayout () { Y = Application.GetRealHeight (36), Height = Application.GetRealHeight (90), }; AddChidren (topView); var title = new Button () { TextAlignment = TextAlignment.Center, Text = Language.StringByID (R.MyInternationalizationString.AC), TextSize = 19, TextColor = SkinStyle.Current.TextColor1 }; topView.AddChidren (title); var logo = new Button () { Width = Application.GetRealWidth (154), Height = Application.GetRealHeight (90), X = Application.GetRealWidth (486), UnSelectedImagePath = MainPage.LogoString, }; topView.AddChidren (logo); var back = new Button () { Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (85), UnSelectedImagePath = "Item/Back.png", SelectedImagePath = "Item/BackSelected.png", }; topView.AddChidren (back); back.MouseUpEventHandler += (sender, e) => { (Parent as PageLayout).PageIndex -= 1; curView = null; }; #endregion #region 全开-全关 var powerView = new FrameLayout () { Height = Application.GetRealHeight (110), BackgroundColor = SkinStyle.Current.TitileView, Y = topView.Bottom }; AddChidren (powerView); var btnAllON = new Button () { Width = Application.GetRealWidth (320), TextSize = 16, TextID = R.MyInternationalizationString.ALLON, TextColor = SkinStyle.Current.TextColor1 }; powerView.AddChidren (btnAllON); var btnAllOff = new Button () { Width = Application.GetRealWidth (320), X = btnAllON.Right, TextSize = 16, TextID = R.MyInternationalizationString.ALLOFF, TextColor = SkinStyle.Current.TextColor1 }; var btnLine = new Button () { Width = 1, BackgroundColor = SkinStyle.Current.White20Transparent, X = btnAllON.Right, }; powerView.AddChidren (btnLine); btnAllON.MouseUpEventHandler += (sender, e) => { btnAllON.TextColor = SkinStyle.Current.SelectedColor; btnAllOff.TextColor = SkinStyle.Current.TextColor1; controlAllAC (1); }; powerView.AddChidren (btnAllOff); btnAllOff.MouseUpEventHandler += (sender, e) => { btnAllON.TextColor = SkinStyle.Current.TextColor1; btnAllOff.TextColor = SkinStyle.Current.SelectedColor; controlAllAC (0); }; #endregion equipmentListScrolView = new VerticalScrolViewLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (Application.DesignHeight - 126 - 110), Y = powerView.Bottom, BackgroundColor = SkinStyle.Current.ViewColor }; AddChidren (equipmentListScrolView); #region 显示全部的空调 int devcieCount = 0; foreach (var room in Room.Lists) { if (string.IsNullOrEmpty(room.Name)) { continue; } foreach (var common in room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.LongXiAC; })) { var ac = common as LongXiAC; EventHandler openACControlPageEvent = (button, mouseEventArgs) => { var userACView = new UserLongXiACPage (ac, room); UserMiddle.DevicePageView.AddChidren (userACView); userACView.showRoomAC (); UserMiddle.DevicePageView.PageIndex = 2; }; RowLayout acView = new RowLayout () { Height = Application.GetRealHeight (140), Tag = ac.CommonLoopID }; equipmentListScrolView.AddChidren (acView); acView.MouseUpEventHandler += openACControlPageEvent; Button btnACViewBottomNull = new Button () { Y = Application.GetRealHeight (135), Height = Application.GetRealHeight (5), BackgroundColor = SkinStyle.Current.MainColor, SelectedBackgroundColor = SkinStyle.Current.MainColor, }; acView.AddChidren (btnACViewBottomNull); var tempDeviceName = new Button () { X = Application.GetRealWidth (30), Height = Application.GetRealHeight (60), TextAlignment = TextAlignment.CenterLeft, Text = room.Name + " - " + ac.Name, TextColor = SkinStyle.Current.TextColor1 }; acView.AddChidren (tempDeviceName); tempDeviceName.MouseUpEventHandler += openACControlPageEvent; Button btnMode = new Button () { X = Application.GetRealWidth (40), Y = Application.GetRealHeight (40), Width = Application.GetMinRealAverage (100), Height = Application.GetMinRealAverage (100), Text = ac.modeText, Tag = "Mode" }; acView.AddChidren (btnMode); btnMode.MouseUpEventHandler += openACControlPageEvent; Button btnWind = new Button () { X = btnMode.Right + Application.GetRealWidth (20), Y = Application.GetRealHeight (40), Width = Application.GetMinRealAverage (100), Height = Application.GetMinRealAverage (100), Text = ac.windText, Tag = "Wind" }; acView.AddChidren (btnWind); btnWind.MouseUpEventHandler += openACControlPageEvent; var btnSetTemperature = new Button () { Width = Application.GetRealWidth (310), X = Application.GetRealWidth (280), Y = Application.GetRealHeight (20), TextSize = 26, TextAlignment = TextAlignment.Center, Tag = "Temperature", TextColor = SkinStyle.Current.TextColor1 }; acView.AddChidren (btnSetTemperature); acBLL.UpdataACModeTemperature (ac, ac.SetMode, btnSetTemperature); btnSetTemperature.MouseUpEventHandler += openACControlPageEvent; var tempSiwtch = new Button () { Width = Application.GetMinRealAverage (90), Height = Application.GetMinRealAverage (53), X = Application.GetRealWidth (640 - 90 - 20), Y = Application.GetRealHeight (67), UnSelectedImagePath = "Item/SwitchClose.png", SelectedImagePath = "Item/SwitchOpen.png", Tag = "Switch" }; acView.AddChidren (tempSiwtch); tempSiwtch.MouseUpEventHandler += (sender3, e3) => { tempSiwtch.IsSelected = !tempSiwtch.IsSelected; ac.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0; acBLL.ControlAC (ac); }; if (0 < ac.Power) { tempSiwtch.IsSelected = true; } devcieCount++; } foreach (var common in room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.HVAC || obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared || obj.Type == DeviceType.CustomAC;})) { var ac = common as AC; EventHandler openACControlPageEvent = (button, mouseEventArgs) => { var userACView = new UserACPage (ac, room); UserMiddle.DevicePageView.AddChidren (userACView); userACView.showRoomAC (); UserMiddle.DevicePageView.PageIndex = 2; }; RowLayout acView = new RowLayout () { Height = Application.GetRealHeight (140), Tag = ac.CommonLoopID }; equipmentListScrolView.AddChidren (acView); acView.MouseUpEventHandler += openACControlPageEvent; Button btnACViewBottomNull = new Button () { Y = Application.GetRealHeight (135), Height = Application.GetRealHeight (5), BackgroundColor = SkinStyle.Current.MainColor, SelectedBackgroundColor = SkinStyle.Current.MainColor, }; acView.AddChidren (btnACViewBottomNull); var tempDeviceName = new Button () { X = Application.GetRealWidth (30), Height = Application.GetRealHeight (60), TextAlignment = TextAlignment.CenterLeft, Text = room.Name + " - " + ac.Name, TextColor = SkinStyle.Current.TextColor1 }; acView.AddChidren (tempDeviceName); tempDeviceName.MouseUpEventHandler += openACControlPageEvent; Button btnMode = new Button () { X = Application.GetRealWidth (40), Y = Application.GetRealHeight (40), Width = Application.GetMinRealAverage (100), Height = Application.GetMinRealAverage (100), Tag = "Mode" }; acView.AddChidren (btnMode); acBLL.UpdataACHostModeIcon (ac.SetMode, btnMode); btnMode.MouseUpEventHandler += openACControlPageEvent; Button btnWind = new Button () { X = btnMode.Right + Application.GetRealWidth (20), Y = Application.GetRealHeight (40), Width = Application.GetMinRealAverage (100), Height = Application.GetMinRealAverage (100), Tag = "Wind" }; acView.AddChidren (btnWind); acBLL.UpdataACHostWindIcon (ac.SetFanSpeed, btnWind); btnWind.MouseUpEventHandler += openACControlPageEvent; var btnSetTemperature = new Button () { Width = Application.GetRealWidth (310), X = Application.GetRealWidth (280), Y = Application.GetRealHeight (20), TextSize = 26, TextAlignment = TextAlignment.Center, Tag = "Temperature", TextColor = SkinStyle.Current.TextColor1 }; acView.AddChidren (btnSetTemperature); acBLL.UpdataACModeTemperature (ac, ac.SetMode, btnSetTemperature); btnSetTemperature.MouseUpEventHandler += openACControlPageEvent; var tempSiwtch = new Button () { Width = Application.GetMinRealAverage (90), Height = Application.GetMinRealAverage (53), X = Application.GetRealWidth (640 - 90 - 20), Y = Application.GetRealHeight (67), UnSelectedImagePath = "Item/SwitchClose.png", SelectedImagePath = "Item/SwitchOpen.png", Tag = "Switch" }; acView.AddChidren (tempSiwtch); tempSiwtch.MouseUpEventHandler += (sender3, e3) => { tempSiwtch.IsSelected = !tempSiwtch.IsSelected; ac.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0; if (ac.Type == DeviceType.CustomAC) { var acCommon = common as CustomAC; acCommon.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0; 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); } }; if (0 < ac.Power) { tempSiwtch.IsSelected = true; } devcieCount++; } } #endregion } /// /// 控制所有空调 /// void controlAllAC (byte b) { System.Threading.Tasks.Task.Run (() => { foreach (var room in Room.Lists) { if (room == null) continue; if (string.IsNullOrEmpty(room.Name)) { continue; } var list = room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared || obj.Type == DeviceType.HVAC || obj.Type == DeviceType.LongXiAC; }); foreach (var common in list) { var acCommon = common as AC; acCommon.Power = b; acBLL.ControlAC (acCommon,true); } var listCustomAC = room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.CustomAC; }); foreach (var common in listCustomAC) { var acCommon = common as CustomAC; 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 ()); } } } }); } static System.Threading.Thread thread; /// /// 读取设备状态 /// public static void readAllStatus (bool isRead = false) { if (isRead) { foreach (Room room in Room.Lists) { if (room == null) { continue; } var list = room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.HVAC || obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared; }); foreach (Common common in list) { common.LastUpdateTime = DateTime.MinValue; } } } if (thread != null) { thread.Abort (); } thread = new Thread (() => { try { System.Collections.Generic.List readList = new System.Collections.Generic.List (); foreach (Room room in Room.Lists) { var list = room.DeviceList.FindAll ((obj) => { return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && (obj.Type == DeviceType.HVAC || obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared || obj.Type == DeviceType.LongXiAC); }); foreach (Common ac in list) { var s = ac.Type + "_" + ac.SubnetID + "_" + ac.DeviceID + "_" + ac.LoopID; //已经读取过当前设备就不再读取 if (readList.Contains (s)) { continue; } readList.Add (s); Thread.Sleep (500); if (ac.Type == DeviceType.ACPanel) { Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 3, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 4, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 5, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 6, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 7, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 8, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 19, ac.LoopID, ac.LoopID } ); Control.ControlBytesSend (Command.ReadPanleTemp, ac.SubnetID, ac.DeviceID, new byte [] { ac.LoopID } ); } else if (ac.Type == DeviceType.HVAC || ac.Type == DeviceType.ACInfrared|| ac.Type == DeviceType.LongXiAC) { Control.ControlBytesSendHasReturn (Command.ReadACMode, ac.SubnetID, ac.DeviceID, new byte [] { ac.LoopID }); } } } } catch { } finally { thread = null; } }); thread.Start (); } } }