using Shared.SimpleControl.Phone; using System; using System.Collections.Generic; using System.Threading; namespace Shared.SimpleControl.Pad { /// /// 通过灯光设备类型,显示出所有房间的灯光 /// public class UserDeviceToSocket : Shared.Dialog { /// /// 当前设备界面 /// static UserDeviceToSocket curView; VerticalScrolViewLayout RoomListScrolView; Button btnShowAll; VerticalScrolViewLayout ControlBodyView; /// /// 构造函数 /// public UserDeviceToSocket () { curView = this; showAllRoomLights (); readAllStatus (); } Thread readEnergyThead; List EnergtLightList = new List (); public static void UpdataEnergy (string updataFlag, int energy) { Application.RunOnMainThread (() => { if (curView == null) { return; } for (int i = 0; i < curView.ControlBodyView.ChildrenCount; i++) { if (curView.ControlBodyView.GetChildren (i).GetType () == typeof (HorizontalScrolViewLayout)) { var lightRow = (HorizontalScrolViewLayout)curView.ControlBodyView.GetChildren (i); if (lightRow.Tag.ToString () == "horSocketView") { for (int k = 0; k < lightRow.ChildrenCount; k++) { if (lightRow.GetChildren (i).GetType () == typeof (FrameLayout)) { var socketView = (FrameLayout)lightRow.GetChildren (k); var o = socketView.Tag.ToString (); if (o != null && o.ToString () == updataFlag) { for (int j = 0; j < socketView.ChildrenCount; j++) { if (socketView.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)socketView.GetChildren (j); if (btn.Tag != null && btn.Tag.ToString () == DeviceType.LightEnergySwitch.ToString ()) { btn.Text = energy.ToString () + " W"; if (energy > 0) { btn.IsSelected = true; } else btn.IsSelected = false; } } } } } } } } } }); } /// /// 更新当前灯光 /// public static void UpdateStatus (string updataFlag, byte brightness) { #region 更新用户主界面灯光点亮总数 Application.RunOnMainThread (() => { int brighingCount = 0; foreach (var room in Room.Lists) { if (room.Name == "") { continue; } foreach (var light in room.DeviceList) { if (light.Type == DeviceType.LightSwitchSocket) { if ((light as LightSwitch).CurrentBrightness == 100) brighingCount++; } } } UserMiddle.UpdataLightLightingCount (brighingCount, Language.StringByID (R.MyInternationalizationString.ElectricalControl)); }); #endregion System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { if (curView == null) return; for (int i = 0; i < curView.ControlBodyView.ChildrenCount; i++) { if (curView.ControlBodyView.GetChildren (i).GetType () == typeof (HorizontalScrolViewLayout)) { var lightRow = (HorizontalScrolViewLayout)curView.ControlBodyView.GetChildren (i); if (lightRow.Tag.ToString () == "horSocketView") { for (int k = 0; k < lightRow.ChildrenCount; k++) { if (lightRow.GetChildren (i).GetType () == typeof (FrameLayout)) { var socketView = (FrameLayout)lightRow.GetChildren (k); var o = socketView.Tag.ToString (); if (o != null && o.ToString () == updataFlag) { for (int j = 0; j < socketView.ChildrenCount; j++) { if (socketView.GetChildren (j).GetType () == typeof (Button)) { var btn = (Button)socketView.GetChildren (j); if (brightness == 0) { btn.IsSelected = false; } else { btn.IsSelected = true; } curView.updateLigthOnCount (); return; } } } } } } } } }); }); } /// /// 显示当前多少个灯是亮的 /// void updateLigthOnCount () { Application.RunOnMainThread (() => { if (curView == null) return; for (int i = 0; i < curView.RoomListScrolView.ChildrenCount; i++) { if (curView.RoomListScrolView.GetChildren (i).GetType () == typeof (FrameLayout)) { var roomRow = (FrameLayout)curView.RoomListScrolView.GetChildren (i); if (roomRow.Tag != null) { foreach (var room in Room.Lists) { if (room.Name == "") { continue; } if (roomRow.Tag.ToString () == room.Name) { int oCount = 0; foreach (var light in room.DeviceList) { if (light.Type == DeviceType.LightSwitchSocket || light.Type == DeviceType.LightEnergySwitch) { if ((light as LightSwitch).CurrentBrightness == 100) oCount++; } } for (int j = 0; j < roomRow.ChildrenCount; j++) { if (roomRow.GetChildren (j).GetType () == typeof (EditText)) { var et = (EditText)roomRow.GetChildren (j); et.Text = oCount.ToString (); if (oCount > 0) { et.TextColor = SkinStyle.Current.SelectedColor; } else { et.TextColor = SkinStyle.Current.TextColor1; et.Text = room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.LightSwitchSocket || obj.Type == DeviceType.LightEnergySwitch; }).Count.ToString (); } } } } } } } } }); } bool showTip = true; /// /// 控制所有的灯 /// /// The blue component. void controlAllLight (byte b) { showTip = true; System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Start ("Sending..."); }); foreach (var room in Room.Lists) { if (room.Name == "") { continue; } foreach (var common in room.DeviceList) { //controlDeviceIndex++; byte [] returnBytes = null; if (common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySwitch) { 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); var btnTip = new Button () { Height = Application.GetRealHeight (140), Text = common.Name + tipString, BackgroundColor = 0xFF000000, }; MainPage.MainFrameLayout.AddChidren (btnTip); btnTip.MouseUpEventHandler += (sender, e) => { MainPage.MainFrameLayout.Remove (btnTip); }; System.Threading.Tasks.Task.Run (() => { System.Threading.Thread.Sleep (3000); Application.RunOnMainThread (() => { MainPage.MainFrameLayout.Remove (btnTip); showTip = false; }); }); } }); } } } } Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); }); } /// /// 通过设备类型方式选择设备 /// void showAllRoomLights () { #region 标题 FrameLayout topBackView = new FrameLayout () { Height = Application.GetRealHeight (150), BackgroundColor = 0xFF2f2f2f }; AddChidren (topBackView); 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.ElectricalControl), TextSize = 21, }; 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 (120), Width = Application.GetRealWidth (95), UnSelectedImagePath = "Item/Back.png", SelectedImagePath = "Item/BackSelected.png", Gravity = Gravity.CenterVertical, }; topView.AddChidren (back); back.MouseUpEventHandler += (sender, e) => { curView = null; if (readEnergyThead != null) readEnergyThead.Abort (); Close (); }; var btnl = new Button () { Height = 1, BackgroundColor = 0xFF2f2f2f, Y = topView.Height - 1 }; topView.AddChidren (btnl); #endregion FrameLayout bordorView = new FrameLayout () { Width = Application.GetRealWidth (480), Height = Application.GetRealHeight (Application.DesignHeight - 150), Y = Application.GetRealHeight (150), BackgroundColor = 0xFF2f2f2f }; AddChidren (bordorView); #region 全开-全关 FrameLayout powerView = new FrameLayout () { Height = Application.GetRealHeight (140), Width = Application.GetRealWidth (480), BackgroundColor = SkinStyle.Current.MainColor, }; bordorView.AddChidren (powerView); var btnAllON = new Button () { Width = Application.GetRealWidth (240), TextSize = 16, TextID = R.MyInternationalizationString.ALLON, SelectedTextColor = SkinStyle.Current.SelectedColor, TextColor = SkinStyle.Current.TextColor1, }; powerView.AddChidren (btnAllON); var btnAllOff = new Button () { Width = Application.GetRealWidth (240), X = btnAllON.Right, TextSize = 16, TextID = R.MyInternationalizationString.ALLOFF, SelectedTextColor = SkinStyle.Current.SelectedColor, TextColor = SkinStyle.Current.TextColor1, }; var btnLine = new Button () { Width = 1, BackgroundColor = 0xFF2f2f2f, X = btnAllON.Right, }; powerView.AddChidren (btnLine); var btnLine1 = new Button () { Height = 1, Y = powerView.Height - 1, BackgroundColor = 0xFF2f2f2f, }; powerView.AddChidren (btnLine1); btnAllON.MouseUpEventHandler += (sender, e) => { btnAllON.TextColor = SkinStyle.Current.SelectedColor; btnAllOff.TextColor = SkinStyle.Current.TextColor1; Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureOpenAllLight), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.Show (); alert.ResultEventHandler += (sender2, e2) => { if (e2) { controlAllLight (100); } }; }; powerView.AddChidren (btnAllOff); btnAllOff.MouseUpEventHandler += (sender, e) => { btnAllON.TextColor = SkinStyle.Current.TextColor1; btnAllOff.TextColor = SkinStyle.Current.SelectedColor; Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureCloseAllLight), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.Show (); alert.ResultEventHandler += (sender2, e2) => { if (e2) { controlAllLight (0); } }; }; #endregion RoomListScrolView = new VerticalScrolViewLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (Application.DesignHeight - 300 - 140), Y = powerView.Bottom, }; bordorView.AddChidren (RoomListScrolView); int devcieCount = 0; foreach (var room in Room.Lists) { if (room.Name == "") { continue; } if (null == room.DeviceList.Find ((obj) => { return obj.Type == DeviceType.LightSwitchSocket || obj.Type == DeviceType.LightEnergySwitch; })) { continue; } EventHandler ShowLightRoom = (button, mouseEventArgs) => { btnShowAll.IsSelected = false; for (int i = 0; i < RoomListScrolView.ChildrenCount; i++) { var view = RoomListScrolView.GetChildren (i); if (!string.IsNullOrEmpty (view.Tag.ToString ())) { if (view.Tag.ToString () == room.Name) { for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) { var btn = (view as FrameLayout).GetChildren (j); if (btn.GetType () == typeof (Button)) (btn as Button).IsSelected = true; } } else { for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) { var btn = (view as FrameLayout).GetChildren (j); if (btn.GetType () == typeof (Button)) (btn as Button).IsSelected = false; } } } } }; FrameLayout roomView = new FrameLayout () { Height = Application.GetRealHeight (140), Tag = room.Name }; RoomListScrolView.AddChidren (roomView); Button tempDeviceName = new Button () { Height = Application.GetRealHeight (140), TextAlignment = TextAlignment.CenterLeft, SelectedTextColor = SkinStyle.Current.SelectedColor, TextColor = SkinStyle.Current.TextColor1, BackgroundColor = SkinStyle.Current.Transparent, SelectedBackgroundColor = SkinStyle.Current.MainColor, Padding = new Padding (0, 35, 0, 0), Text = room.Name, }; roomView.AddChidren (tempDeviceName); tempDeviceName.MouseUpEventHandler += ShowLightRoom; Button btnPoint = new Button () { Width = Application.GetRealWidth (15), Height = Application.GetRealHeight (15), X = Application.GetRealWidth (45), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/Point.png", SelectedImagePath = "Item/PointSelected.png", }; roomView.AddChidren (btnPoint); EditText btnNumber = new EditText () { Width = Application.GetMinRealAverage (50), Height = Application.GetMinRealAverage (35), X = bordorView.Width - Application.GetRealWidth (70), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/LightingCount.png", SelectedImagePath = "Item/LightingCount.png", Enable = false, Text = room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.LightSwitchSocket || obj.Type == DeviceType.LightEnergySwitch; }).Count.ToString (), TextColor = SkinStyle.Current.TextColor1, TextAlignment = TextAlignment.Center, Tag = room.Name }; roomView.AddChidren (btnNumber); Button btnLightIcon = new Button () { Width = Application.GetRealWidth (100), Height = Application.GetRealHeight (80), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/RH_Lights.png", SelectedImagePath = "Item/RH_LightsSelected.png", X = btnNumber.Right }; roomView.AddChidren (btnLightIcon); Button btnNull = new Button () { Y = Application.GetRealHeight (138), Height = Application.GetRealHeight (5), BackgroundColor = SkinStyle.Current.MainColor, }; roomView.AddChidren (btnNull); Button btnNull2 = new Button () { Width = 1, X = roomView.Width - 1, BackgroundColor = 0xFF2f2f2f, }; roomView.AddChidren (btnNull2); Button btnSelected = new Button () { X = roomView.Width - 5, Width = 5, SelectedBackgroundColor = SkinStyle.Current.SelectedColor, BackgroundColor = SkinStyle.Current.Transparent, }; roomView.AddChidren (btnSelected); if (devcieCount == 0) { tempDeviceName.IsSelected = true; btnPoint.IsSelected = true; ControlBodyView = new VerticalScrolViewLayout () { Width = Application.GetRealWidth (Application.DesignWidth) - bordorView.Width, Height = Application.GetRealHeight (Application.DesignHeight - 150), X = bordorView.Right, Y = bordorView.Y, BackgroundColor = SkinStyle.Current.MainColor }; AddChidren (ControlBodyView); InitSocketView (room); btnSelected.IsSelected = true; tempDeviceName.IsSelected = true; btnPoint.IsSelected = true; } devcieCount++; } updateLigthOnCount (); #region 显示全部 btnShowAll = new Button () { Y = bordorView.Height - Application.GetRealHeight (150), Height = Application.GetRealHeight (150), TextAlignment = TextAlignment.Center, BackgroundColor = SkinStyle.Current.MainColor, SelectedBackgroundColor = SkinStyle.Current.MainColor, TextColor = SkinStyle.Current.TextColor1, SelectedTextColor = SkinStyle.Current.SelectedColor, TextSize = 18, Text = Language.StringByID (R.MyInternationalizationString.ShowAll), }; bordorView.AddChidren (btnShowAll); btnShowAll.MouseUpEventHandler += (sender, e) => { for (int i = 0; i < RoomListScrolView.ChildrenCount; i++) { var view = RoomListScrolView.GetChildren (i); if (view.GetType () == typeof (FrameLayout)) for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) { var btn = (view as FrameLayout).GetChildren (j); if (btn.GetType () == typeof (Button)) (btn as Button).IsSelected = false; } } btnShowAll.IsSelected = true; if (ControlBodyView != null) ControlBodyView.RemoveAll (); foreach (var room in Room.Lists) { if (room.Name == "") { continue; } InitSocketView (room); } }; #endregion } void InitSocketView (Room room) { HorizontalScrolViewLayout horSocketView = new HorizontalScrolViewLayout () { Height = Application.GetRealHeight (320), Tag = "horSocketView" }; ControlBodyView.AddChidren (horSocketView); foreach (var common in room.DeviceList) { if (common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySwitch) { var light = common as LightSwitch; FrameLayout lightView = new FrameLayout () { Width = Application.GetRealWidth (350 + 40), Height = Application.GetRealHeight (280 + 40), Tag = common.CommonLoopID }; horSocketView.AddChidren (lightView); //灯名 var tempDeviceName = new Button () { Gravity = Gravity.Center, Width = Application.GetRealWidth (350), Height = Application.GetRealHeight (280), TextAlignment = TextAlignment.Center, Text = light.Name, Radius = 1, BackgroundColor = 0xFF2f2f2f, Padding = new Padding (Application.GetRealHeight (120), 0, 0, 0), UnSelectedImagePath = "PadAdding/PadSocket.png", SelectedImagePath = "PadAdding/PadSocketOn.png", SelectedTextColor = SkinStyle.Current.SelectedColor, TextColor = SkinStyle.Current.TextColor1, Tag = light.CommonLoopID }; lightView.AddChidren (tempDeviceName); tempDeviceName.MouseUpEventHandler += (sender, e) => { tempDeviceName.IsSelected = !tempDeviceName.IsSelected; if (tempDeviceName.IsSelected) { light.CurrentBrightness = 100; } else { light.CurrentBrightness = 0; } Control.ControlBytesSend (Command.SetSingleLight, light.SubnetID, light.DeviceID, new byte [] { light.LoopID, light.CurrentBrightness, 0, 0 }); }; if (light.CurrentBrightness == 100) { tempDeviceName.IsSelected = true; } if (common.Type == DeviceType.LightEnergySwitch) { Button btnEnergy = new Button () { Width = Application.GetRealWidth (90), Height = Application.GetRealHeight (30), X = lightView.Width - Application.GetRealWidth (110), Y = Application.GetRealHeight (20), Text = "0 W", SelectedTextColor = SkinStyle.Current.SelectedColor, TextColor = SkinStyle.Current.TextColor1, Tag = "LightEnergySwitch" }; lightView.AddChidren (btnEnergy); EnergtLightList.Add (common); } } } readEnergyThead = new Thread ((obj) => { while (true) { foreach (var c in EnergtLightList) { Control.ControlBytesSend (Command.ReadAnalogValue, c.SubnetID, c.DeviceID, new byte [] { 10, c.LoopID }, SendCount.Zero); } System.Threading.Thread.Sleep (1000); } }); readEnergyThead.Start (); } /// /// 读取设备状态 /// public static void readAllStatus () { System.Threading.Tasks.Task.Run (() => { 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.LightSwitchSocket; }); foreach (Common common in list) { var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID; //已经读取过当前设备就不再读取 if (readList.Contains (s)) { continue; } readList.Add (s); Control.ControlBytesSendHasReturn (Command.ReadLightEquipmentAllLoopBrightness, common.SubnetID, common.DeviceID, new byte [] { }); } } }); } } }