using Shared.SimpleControl.Phone; using System; using System.Collections.Generic; using Shared.SimpleControl.Pad; using System.Text; namespace Shared.SimpleControl.Pad { /// /// 房间场景 /// public class UserAddSceneDevice : FrameLayout { string roomFilePath; string sceneFilePath; Scene scene; Action action; public UserAddSceneDevice (string roomFilePath, string sceneFilePath, Action outAction) { action = outAction; this.roomFilePath = roomFilePath; this.sceneFilePath = sceneFilePath; BackgroundColor = 0xFF000000; scene = Scene.GetSceneByFilePath (sceneFilePath); } public void ShowAddSceneDeviceView () { #region 标题 var AddSceneDeviceTopView = new FrameLayout () { Height = Application.GetRealHeight (140), Width = LayoutParams.MatchParent, }; AddChidren (AddSceneDeviceTopView); var backButton = new Button () { X = Application.GetRealWidth (30), Height = Application.GetRealHeight (100), Width = Application.GetRealWidth (110), Gravity = Gravity.CenterVertical, TextID = R.MyInternationalizationString.CLOSE }; backButton.MouseUpEventHandler += (sender, e) => { action (sceneFilePath); RemoveFromParent (); }; AddSceneDeviceTopView.AddChidren (backButton); EditText textButton = new EditText () { Height = Application.GetRealHeight (50), Width = Application.GetRealWidth (400), Text = scene.Name, TextAlignment = TextAlignment.Center, Gravity = Gravity.Center, UnSelectedImagePath = "Item/Transparent.png", SelectedBackgroundColor = 0xFF000000, Enable = false, TextSize = 15, }; AddSceneDeviceTopView.AddChidren (textButton); Button editor = new Button () { X = Application.GetRealWidth (1568 - 100), Width = Application.GetRealWidth (70), Height = Application.GetRealHeight (90), UnSelectedImagePath = "Item/Editor.png", SelectedImagePath = "Item/EditorSelected.png", Gravity = Gravity.CenterVertical, }; AddSceneDeviceTopView.AddChidren (editor); editor.MouseUpEventHandler += (sender, e) => { if (editor.IsSelected == true) { } else { SceneMethod.AddOrUpdataSceneBaseMassage (roomFilePath, sceneFilePath, (newSceneFilePath) => { sceneFilePath = newSceneFilePath; scene = Scene.GetSceneByFilePath (sceneFilePath); textButton.Text = scene.Name; }); } }; #endregion FrameLayout DeviceFrameLayout = new FrameLayout () { X = Application.GetRealHeight (40), Y = AddSceneDeviceTopView.Bottom, Width = Application.GetRealWidth (740), Height = Application.GetRealHeight (1230), BackgroundColor = SkinStyle.Current.MainColor, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 1, Radius = 5 }; AddChidren (DeviceFrameLayout); ShowSceneDevice (DeviceFrameLayout); FrameLayout DeviceListView = new FrameLayout () { X = DeviceFrameLayout.Right + Application.GetRealWidth (20), Y = AddSceneDeviceTopView.Bottom, Width = Application.GetRealWidth (750), Height = Application.GetRealHeight (1230), BackgroundColor = SkinStyle.Current.MainColor, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 1, Radius = 5 }; AddChidren (DeviceListView); ShowDevcieListView (DeviceFrameLayout, DeviceListView); } void ShowSceneDevice (FrameLayout DeviceFrameLayout) { DeviceFrameLayout.RemoveAll (); Button btnEquipmentModel = new Button () { Height = Application.GetRealHeight (120), BackgroundColor = 0xFF2f2f2f, Text = Language.StringByID (R.MyInternationalizationString.Scenes) + " " + Language.StringByID (R.MyInternationalizationString.Device), TextAlignment = TextAlignment.Center, TextColor = SkinStyle.Current.TextColor1, TextSize = 15, }; DeviceFrameLayout.AddChidren (btnEquipmentModel); #region 根据添加设备的顺序排列设备 VerticalScrolViewLayout v = new VerticalScrolViewLayout () { Height = Application.GetRealHeight (1110), Y = btnEquipmentModel.Bottom }; DeviceFrameLayout.AddChidren (v); foreach (var deviceFilePath in scene.DeviceFilePathList) { Common common = Newtonsoft.Json.JsonConvert.DeserializeObject (CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (null == common) { continue; } if (deviceFilePath.Split ('_') [1] == DeviceType.LightDimming.ToString ()) {// && ((LightDimming)common).DimmingFunction) { initDevice (DeviceType.LightDimming.ToString (), common.Name, @"Item/Light.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightSwitch.ToString ()) { initDevice (DeviceType.LightSwitch.ToString (), common.Name, @"Item/Light.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightRGB.ToString ()) { initDevice (DeviceType.LightRGB.ToString (), common.Name, @"Item/Light.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainModel.ToString ()) { initDevice (DeviceType.CurtainModel.ToString (), common.Name, @"Curtain/Curtain.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainTrietex.ToString ()) { initDevice (DeviceType.CurtainTrietex.ToString (), common.Name, @"Curtain/Curtain.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainRoller.ToString ()) { initDevice (DeviceType.CurtainRoller.ToString (), common.Name, @"Curtain/Curtain.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.HVAC.ToString () || deviceFilePath.Split ('_') [1] == DeviceType.ACInfrared.ToString () || deviceFilePath.Split ('_') [1] == DeviceType.ACPanel.ToString ()) { if (deviceFilePath.Split ('_') [1] == DeviceType.ACInfrared.ToString ()) { initDevice (DeviceType.ACInfrared.ToString (), common.Name, @"AC/AC.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.HVAC.ToString ()) { initDevice (DeviceType.HVAC.ToString (), common.Name, @"AC/AC.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.ACPanel.ToString ()) { initDevice (DeviceType.ACPanel.ToString (), common.Name, @"AC/AC.png", deviceFilePath, v); } } else if (deviceFilePath.Split ('_') [1] == DeviceType.FoolHeat.ToString ()) { initDevice (DeviceType.FoolHeat.ToString (), common.Name, @"Item/FloorHeating.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.DryContact.ToString ()) { initDevice (DeviceType.DryContact.ToString (), common.Name, @"Item/DryContact.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.FanModule.ToString ()) { initDevice (DeviceType.FanModule.ToString (), common.Name, @"Fan/Fan.png", deviceFilePath, v); } else if (deviceFilePath.Split ('_') [1] == DeviceType.LogicModule.ToString ()) { initDevice (DeviceType.LogicModule.ToString (), common.Name, @"Item/LogicModule.png", deviceFilePath, v); } } #endregion } public void initDevice (string deviceType, string remark, string iconPath, string deviceFilePath, VerticalScrolViewLayout v) { RowLayout row = new RowLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (130), }; v.AddChidren (row); Button btnDelFile = new Button () { Text = "Del", BackgroundColor = SkinStyle.Current.DelColor, }; row.AddRightView (btnDelFile); btnDelFile.MouseUpEventHandler += (sender, e) => { Scene scene = Scene.GetSceneByFilePath (sceneFilePath);//找到所有的场景 if (scene == null) return; scene.DeviceFilePathList.Remove (deviceFilePath);//将场景中的设备路径删掉 if (deviceType != DeviceType.LogicModule.ToString ()) IO.FileUtils.DeleteFile (deviceFilePath);//将场景中的设备文件删掉 IO.FileUtils.WriteFileByBytes (sceneFilePath, CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (scene))); ShowAddSceneDeviceView (); }; Button btnIcon = new Button () { Width = Application.GetRealHeight (86), Height = Application.GetRealHeight (86), X = Application.GetRealWidth (30), UnSelectedImagePath = iconPath, Gravity = Gravity.CenterVertical, }; row.AddChidren (btnIcon); Button btnName = new Button () { X = btnIcon.X + btnIcon.Width + Application.GetRealWidth (10), Width = Application.GetRealWidth (342), Height = LayoutParams.MatchParent, Text = remark, TextAlignment = TextAlignment.CenterLeft, }; row.AddChidren (btnName); Button btnRight = new Button () { X = Application.GetRealWidth (620), Y = Application.GetRealHeight (20), Width = Application.GetRealWidth (81), Height = Application.GetRealHeight (79), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "PadAdding/PadSetting.png", }; if (deviceType != DeviceType.LogicModule.ToString ()) row.AddChidren (btnRight); EventHandler eventRoomScene = (sender, e) => { if (deviceType == DeviceType.LogicModule.ToString ()) return; new SystemRoomScenesMethod ().SetDialogForAddEquipment (deviceType, deviceFilePath); }; row.MouseUpEventHandler += eventRoomScene; btnRight.MouseUpEventHandler += eventRoomScene; btnName.MouseUpEventHandler += eventRoomScene; } void ShowDevcieListView (FrameLayout DeviceFrameLayout, FrameLayout DeviceListView) { Button btnTitle = new Button () { Height = Application.GetRealHeight (120), BackgroundColor = 0xFF2f2f2f, TextAlignment = TextAlignment.Center, TextID = R.MyInternationalizationString.AddNewDevice, Enable = false }; DeviceListView.AddChidren (btnTitle); FrameLayout dropDownLayout = new FrameLayout () { Y = btnTitle.Bottom, Height = Application.GetRealHeight (100), BackgroundColor = 0xFF121212, }; DeviceListView.AddChidren (dropDownLayout); Button btnEquipmentIcon = new Button () { Width = Application.GetRealHeight (65), Height = Application.GetRealHeight (65), UnSelectedImagePath = "Item/PointBig.png", X = Application.GetRealWidth (10), Gravity = Gravity.CenterVertical, }; dropDownLayout.AddChidren (btnEquipmentIcon); Button btnDrodIcon = new Button () { X = DeviceListView.Width - Application.GetRealWidth (100), Height = Application.GetRealHeight (80), Width = Application.GetRealWidth (80), UnSelectedImagePath = "Item/Down.png", SelectedImagePath = "Item/DownSelected.png", Gravity = Gravity.CenterVertical, }; dropDownLayout.AddChidren (btnDrodIcon); Button btnEquipmentType = new Button () { Height = Application.GetRealHeight (70), X = btnEquipmentIcon.Right + Application.GetRealWidth (20), Gravity = Gravity.CenterVertical, TextAlignment = TextAlignment.CenterLeft, Text = Language.StringByID (R.MyInternationalizationString.All), }; dropDownLayout.AddChidren (btnEquipmentType); //存放设备的view VerticalScrolViewLayout DeviceListScrolView = new VerticalScrolViewLayout () { Height = DeviceListView.Height - Application.GetRealHeight (220), Y = dropDownLayout.Bottom + Application.GetRealHeight (5), }; DeviceListView.AddChidren (DeviceListScrolView); FrameLayout inScrolView_DeviceListView = new FrameLayout (); DeviceListScrolView.AddChidren (inScrolView_DeviceListView); // ---显示在本地找到的所有设备--- initAllLocalEquipment (DeviceFrameLayout, inScrolView_DeviceListView); VerticalScrolViewLayout typeLayout = new VerticalScrolViewLayout () { Height = 0, Y = dropDownLayout.Bottom, }; DeviceListView.AddChidren (typeLayout); #region ---Add Device Dialog--- btnEquipmentType.MouseUpEventHandler += (sender33, e55) => { //下拉列表的数据源(选项为设备类型有关) List typeTemp = new List{ Language.StringByID(R.MyInternationalizationString.All), Language.StringByID(R.MyInternationalizationString.Lights), Language.StringByID(R.MyInternationalizationString.Curtains), Language.StringByID(R.MyInternationalizationString.AC), //Language.StringByID(R.MyInternationalizationString.MusicModel), Language.StringByID(R.MyInternationalizationString.FoolHeat), Language.StringByID(R.MyInternationalizationString.TV), Language.StringByID(R.MyInternationalizationString.ElectricalControl), Language.StringByID(R.MyInternationalizationString.Fan), }; if (sceneFilePath == null) { typeTemp.Add (Language.StringByID (R.MyInternationalizationString.MusicModel)); } else { typeTemp.Add (Language.StringByID (R.MyInternationalizationString.LogicModule)); } //如果当前显示的是所有的设备,就隐藏 if (DeviceListScrolView.Height != 0) { DeviceListScrolView.Height = 0; typeLayout.Height = Application.GetRealHeight (1110); btnDrodIcon.IsSelected = true; if (typeLayout.ChildrenCount != 0) { return; } foreach (string typeName in typeTemp) { FrameLayout typeDropDownLayout1 = new FrameLayout () { Height = Application.GetRealHeight (82), BackgroundColor = 0xFF333333 }; typeLayout.AddChidren (typeDropDownLayout1); Button btnLine = new Button () { Width = LayoutParams.MatchParent, Height = 1, BackgroundColor = 0xFF333333 }; typeLayout.AddChidren (btnLine); Button btnEquipmentIcon2 = new Button () { Width = Application.GetRealHeight (65), Height = Application.GetRealHeight (65), X = Application.GetRealWidth (30), UnSelectedImagePath = "Item/PointBig.png", Gravity = Gravity.CenterVertical, }; typeDropDownLayout1.AddChidren (btnEquipmentIcon2); Button btnTypeName = new Button () { Width = Application.GetRealHeight (400), Height = Application.GetRealHeight (82), X = btnEquipmentIcon.Right + Application.GetRealWidth (10), Text = typeName, TextAlignment = TextAlignment.CenterLeft, }; typeDropDownLayout1.AddChidren (btnTypeName); EventHandler typeEvent = (sender1, e1) => { typeLayout.Height = 0; DeviceListScrolView.Height = Application.GetRealHeight (1110); btnEquipmentType.Text = typeName; string deviceType = typeName; if (typeName == Language.StringByID (R.MyInternationalizationString.Lights)) { btnEquipmentIcon.UnSelectedImagePath = "Item/Light.png"; deviceType = "Light"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.Curtains)) { btnEquipmentIcon.UnSelectedImagePath = "Curtain/Curtain.png"; deviceType = "Curtain"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.AC)) { btnEquipmentIcon.UnSelectedImagePath = "AC/AC.png"; deviceType = "AC"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.FoolHeat)) { btnEquipmentIcon.UnSelectedImagePath = "Item/FloorHeating.png"; deviceType = "FoolHeat"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.DryContactPanel)) { btnEquipmentIcon.UnSelectedImagePath = "Item/DryContact.png"; deviceType = "DryContact"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.MusicModel)) { btnEquipmentIcon.UnSelectedImagePath = "Item/Music.png"; deviceType = "Music"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.InfraredMode)) { btnEquipmentIcon.UnSelectedImagePath = "Item/InfraredTV.png"; deviceType = "InfraredMode"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.TV)) { btnEquipmentIcon.UnSelectedImagePath = "Item/InfraredTV.png"; deviceType = "InfraredMode"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.ElectricalControl)) { btnEquipmentIcon.UnSelectedImagePath = "Light/Socket.png"; deviceType = "EC";//电器控制 } else if (typeName == Language.StringByID (R.MyInternationalizationString.Fan)) { btnEquipmentIcon.UnSelectedImagePath = "Fan/Fan.png"; deviceType = "Fan"; } else if (typeName == Language.StringByID (R.MyInternationalizationString.LogicModule)) { btnEquipmentIcon.UnSelectedImagePath = "Item/PointBig.png"; deviceType = "LogicModule"; } else { btnEquipmentIcon.UnSelectedImagePath = "Item/LogicModule.png"; deviceType = "All"; } initAllLocalEquipment (DeviceFrameLayout, inScrolView_DeviceListView, deviceType); }; typeDropDownLayout1.MouseUpEventHandler += typeEvent; btnTypeName.MouseUpEventHandler += typeEvent; btnEquipmentIcon2.MouseUpEventHandler += typeEvent; } } else { typeLayout.Height = Application.GetRealHeight (0); DeviceListScrolView.Height = Application.GetRealHeight (1110); btnDrodIcon.IsSelected = false; } }; #endregion } /// /// 显示在本地找到的所有设备 /// void initAllLocalEquipment (FrameLayout DeviceFrameLayout, FrameLayout inScrolView_DeviceListView, string selectedDeviceType = null) { var filesList = IO.FileUtils.ReadFiles (); inScrolView_DeviceListView.RemoveAll (); int btnEquipmentIndex = 0; //找出需要显示的设备 var localEquipments = filesList.FindAll ((obj) => { string [] str = obj.Split ('_'); return obj.StartsWith ("Equipment_") && str.Length == 5 && (str [1] == DeviceType.LightSwitch.ToString () || str [1] == DeviceType.LightDimming.ToString () || str [1] == DeviceType.LightRGB.ToString () || str [1] == DeviceType.CurtainModel.ToString () || str [1] == DeviceType.CurtainRoller.ToString () || str [1] == DeviceType.CurtainTrietex.ToString () || str [1] == DeviceType.ACInfrared.ToString () || str [1] == DeviceType.HVAC.ToString () || str [1] == DeviceType.FoolHeat.ToString () || (str [1] == DeviceType.InfraredMode.ToString () && sceneFilePath == null) || (str [1] == DeviceType.MusicModel.ToString () && sceneFilePath == null) || str [1] == DeviceType.FanModule.ToString () || (str [1] == DeviceType.LogicModule.ToString () && sceneFilePath != null) ); }); int i = 0; foreach (string deviceFilePath in localEquipments) { i++; string [] str = deviceFilePath.Split ('_'); if (str [4].Length == 2 && str [4] [0] == '0') { str [4] = str [4] [1].ToString (); } if (selectedDeviceType != null && selectedDeviceType != "All") { //只显示当前选择的设备类型的设备 if (selectedDeviceType == "EC") { if (str [1].Contains (DeviceType.LightSwitch.ToString ())) { var b = IO.FileUtils.ReadFile (deviceFilePath); LightSwitch c = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (b)); if (!c.IsSocket) { continue; } } else continue; } else if (selectedDeviceType == "Light") { if (str [1].Contains (DeviceType.LightSwitch.ToString ())) { var b = IO.FileUtils.ReadFile (deviceFilePath); LightSwitch c = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (b)); if (c.IsSocket) { continue; } } else if (!str [1].Contains (selectedDeviceType)) continue; } else { if (!str [1].Contains (selectedDeviceType)) { continue; } } } var deviceType = str [1]; Button btnEquipment = new Button () { Width = (inScrolView_DeviceListView.Width - Application.GetRealWidth (70)) / 2, Height = Application.GetRealHeight (110), X = Application.GetRealHeight (30), Y = Application.GetRealHeight (20), TextAlignment = TextAlignment.Center, BackgroundColor = 0xFFDBDDDF, SelectedBackgroundColor = SkinStyle.Current.SelectedColor, TextColor = 0xFF000000, Text = i.ToString () }; var commonBytes = IO.FileUtils.ReadFile (deviceFilePath); if (commonBytes.Length == 0) { Console.WriteLine ("length loss"); continue; } if (commonBytes.Length == 1) { Console.WriteLine ("length loss 1"); continue; } Common commonDevice = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (commonBytes)); if (btnEquipmentIndex < 2) { btnEquipment.X = btnEquipmentIndex * (btnEquipment.Width + Application.GetRealWidth (25)) + Application.GetRealWidth (20); btnEquipment.Y = Application.GetRealHeight (30); btnEquipmentIndex++; } else { btnEquipment.X = btnEquipmentIndex % 2 * (btnEquipment.Width + Application.GetRealWidth (25)) + Application.GetRealWidth (20); btnEquipment.Y = (int)btnEquipmentIndex / 2 * (btnEquipment.Height + Application.GetRealHeight (30)) + Application.GetRealHeight (30); btnEquipmentIndex++; } btnEquipment.Text = commonDevice.Name; btnEquipment.Tag = deviceFilePath; inScrolView_DeviceListView.AddChidren (btnEquipment); if (roomFilePath == Scene.GlobalSceneFilePath) { //场景设备 if (null == scene) { return; } foreach (var filePath in scene.DeviceFilePathList) { System.Console.WriteLine (filePath); if (filePath.EndsWith (deviceFilePath)) { btnEquipment.IsSelected = true; break; } } } else { if (sceneFilePath == null) { foreach (var roomF in Room.Lists) { if (roomF.DeviceFilePathList.Contains (btnEquipment.Tag.ToString ())) { btnEquipment.BackgroundColor = 0xFB757575; } } } else { //场景设备 if (null == scene) { return; } foreach (var filePath in scene.DeviceFilePathList) { if (filePath.EndsWith (deviceFilePath)) { btnEquipment.IsSelected = true; break; } } } } btnEquipment.MouseUpEventHandler += (sendere, ee) => { btnEquipment.IsSelected = !btnEquipment.IsSelected; if (btnEquipment.IsSelected) { if (null == scene) { return; } if (null == scene.DeviceFilePathList.Find ((obj) => { return obj.EndsWith (btnEquipment.Tag.ToString ()); })) { string filePath = "RoomScene" + DateTime.Now.Ticks + "_" + deviceType + "_" + btnEquipment.Tag.ToString (); if (roomFilePath == Scene.GlobalSceneFilePath) { filePath = "GlobalScene" + DateTime.Now.Ticks + "_" + deviceType + "_" + btnEquipment.Tag.ToString (); } if (deviceType == DeviceType.LogicModule.ToString ()) { filePath = btnEquipment.Tag.ToString (); } else IO.FileUtils.WriteFileByBytes (filePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (commonDevice))); scene.DeviceFilePathList.Add (filePath); scene.Save (sceneFilePath); } } else { if (null == scene) { return; } var filePath = scene.DeviceFilePathList.Find ((obj) => { return obj.EndsWith (btnEquipment.Tag.ToString ()); }); if (null != filePath) { scene.DeviceFilePathList.Remove (filePath); if (deviceType != DeviceType.LogicModule.ToString ()) { IO.FileUtils.DeleteFile (filePath); } scene.Save (sceneFilePath); } } scene = Scene.GetSceneByFilePath (sceneFilePath); this.ShowSceneDevice (DeviceFrameLayout); }; if (btnEquipmentIndex > 14) { inScrolView_DeviceListView.Height = (btnEquipmentIndex / 2 + 1) * Application.GetRealHeight (150); } else { inScrolView_DeviceListView.Height = Application.GetRealHeight (1110); } } } } }