using Shared.SimpleControl.Phone; using System; using System.Collections.Generic; namespace Shared.SimpleControl.Pad { public class SceneMethod { public SceneMethod () { } static bool showTip = true; /// /// 添加或者修改房间场景 /// /// Room name. /// 代理方法,用来刷新界面 /// Scene.(为null是表示是增加场景) public static void AddOrUpdataSceneBaseMassage (string roomFilePath, string sceneFilePath = null, Action action = null) { Room room = null; if (roomFilePath == Scene.GlobalSceneFilePath) { //GlobalScene } else { room = Room.GetRoomByFilePath (roomFilePath); if (room == null) return; } var scene = Scene.GetSceneByFilePath (sceneFilePath); if (null == scene) { scene = new Scene (); } #region Load Dialog dialog = new Dialog () { Width = Application.GetRealWidth (860), Height = Application.GetRealHeight (1060), }; FrameLayout ModifyDialogBodyView = new FrameLayout () { BackgroundColor = SkinStyle.Current.TextColor1, BorderWidth = 1, Radius = 5, BorderColor = 0xFF333333 }; dialog.AddChidren (ModifyDialogBodyView); Button btnEquipmentTitle = new Button () { Height = Application.GetRealHeight (100), BackgroundColor = 0xFF333333, TextID = R.MyInternationalizationString.AddNewScenes, TextAlignment = TextAlignment.Center, Gravity = Gravity.CenterHorizontal, }; ModifyDialogBodyView.AddChidren (btnEquipmentTitle); Button lblTitleName = new Button () { Gravity = Gravity.CenterHorizontal, Y = Application.GetRealHeight (140), Width = Application.GetRealWidth (780), Height = Application.GetRealHeight (50), TextID = R.MyInternationalizationString.RoomName, TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.MainColor, }; ModifyDialogBodyView.AddChidren (lblTitleName); EditText sceneTextBox = new EditText () { Gravity = Gravity.CenterHorizontal, Y = lblTitleName.Bottom + Application.GetRealHeight (10), Width = Application.GetRealWidth (780), Height = Application.GetRealHeight (70), TextAlignment = TextAlignment.CenterLeft, BackgroundColor = 0xFF000000, SelectedBackgroundColor = 0xFF000000, TextColor = SkinStyle.Current.MainColor, Text = scene.Name, }; ModifyDialogBodyView.AddChidren (sceneTextBox); Button lblIcon = new Button () { Gravity = Gravity.CenterHorizontal, Y = sceneTextBox.Bottom, Width = Application.GetRealWidth (780), Height = Application.GetRealHeight (90), TextID = R.MyInternationalizationString.Backgroud, TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.MainColor, }; ModifyDialogBodyView.AddChidren (lblIcon); VerticalScrolViewLayout verIconLayout = new VerticalScrolViewLayout () { Gravity = Gravity.CenterHorizontal, Y = lblIcon.Bottom + Application.GetRealHeight (5), Width = sceneTextBox.Width, Height = Application.GetRealHeight (350), }; ModifyDialogBodyView.AddChidren (verIconLayout); FrameLayout inVerFramelayout = new FrameLayout (); verIconLayout.AddChidren (inVerFramelayout); Button btnBG = new Button () { Width = Application.GetRealWidth (470), Height = LayoutParams.MatchParent, UnSelectedImagePath = scene.BackgroundImagePath, }; inVerFramelayout.AddChidren (btnBG); Button btnTakePictrue = new Button () { X = btnBG.Right, Width = inVerFramelayout.Width - btnBG.Width, Height = inVerFramelayout.Height / 2, TextID = R.MyInternationalizationString.TakePhoto, TextColor = 0xFF000000, BackgroundColor = 0xFFDBDDDF, TextSize = 16 }; inVerFramelayout.AddChidren (btnTakePictrue); btnTakePictrue.MouseUpEventHandler += (sender, e) => { Camera.TakePicture ((obj) => { if (obj != null) { btnBG.UnSelectedImagePath = "SecenPicture"; } }, "SecenPicture"); }; Button btnLineP = new Button () { Width = btnTakePictrue.Width, Height = 1, BackgroundColor = SkinStyle.Current.TextColor1, X = btnBG.Right, Y = btnTakePictrue.Bottom, }; inVerFramelayout.AddChidren (btnLineP); Button btnSelectPictrue = new Button () { X = btnBG.Right, Y = btnLineP.Bottom, Width = btnTakePictrue.Width, Height = btnTakePictrue.Height, TextColor = 0xFF000000, TextID = R.MyInternationalizationString.SelectPicture, BackgroundColor = 0xFFDBDDDF, TextSize = 16 }; inVerFramelayout.AddChidren (btnSelectPictrue); btnSelectPictrue.MouseUpEventHandler += (sender, e) => { Camera.SelectPicture ((obj) => { if (obj != null) { btnBG.UnSelectedImagePath = "SecenPicture"; } }, "SecenPicture"); }; var systempictureLayout = new FrameLayout { Y = verIconLayout.Bottom + Application.GetRealHeight (10), }; ModifyDialogBodyView.AddChidren (systempictureLayout); HorizontalScrolViewLayout horizontalScrolViewLayout = new HorizontalScrolViewLayout () { Width = Application.GetRealWidth (780), Height = Application.GetRealHeight (180), Y = Application.GetRealHeight (30), Gravity = Gravity.CenterHorizontal, }; systempictureLayout.AddChidren (horizontalScrolViewLayout); for (int i = 0; i < 22; i++) { Button butscene = new Button () { Width = Application.GetRealWidth (210), Height = LayoutParams.MatchParent, }; butscene.MouseUpEventHandler += (sender, e) => { btnBG.UnSelectedImagePath = butscene.UnSelectedImagePath; }; butscene.UnSelectedImagePath = "Scene/s" + (i).ToString () + ".png"; horizontalScrolViewLayout.AddChidren (butscene); Button btnNull = new Button () { Width = Application.GetRealWidth (3), BackgroundColor = SkinStyle.Current.TextColor1 }; horizontalScrolViewLayout.AddChidren (btnNull); } FrameLayout BottomView = new FrameLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (120), BackgroundColor = SkinStyle.Current.MainColor, Y = ModifyDialogBodyView.Height - Application.GetRealHeight (120), }; ModifyDialogBodyView.AddChidren (BottomView); Button btnBack = new Button () { Width = Application.GetRealWidth (428), Height = LayoutParams.MatchParent, TextID = R.MyInternationalizationString.Close, TextAlignment = TextAlignment.Center, TextSize = 16 }; BottomView.AddChidren (btnBack); btnBack.MouseUpEventHandler += (sender1, e1) => { dialog.Close (); }; Button btnLineH = new Button () { Width = Application.GetRealWidth (5), Height = LayoutParams.MatchParent, BackgroundColor = 0xFF333333, X = btnBack.Right, }; BottomView.AddChidren (btnLineH); Button btnSave = new Button () { Width = Application.GetRealWidth (410), Height = LayoutParams.MatchParent, X = btnLineH.Right, TextID = R.MyInternationalizationString.SAVE, TextAlignment = TextAlignment.Center, TextSize = 16 }; BottomView.AddChidren (btnSave); #endregion if (null == scene) { btnEquipmentTitle.TextID = R.MyInternationalizationString.AddNewScenes; } lblTitleName.TextID = R.MyInternationalizationString.SceneName; if (roomFilePath == Scene.GlobalSceneFilePath) { var sceneFileList = Newtonsoft.Json.JsonConvert.DeserializeObject> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (Scene.GlobalSceneFilePath))); btnEquipmentTitle.TextID = R.MyInternationalizationString.ChangeInformation; //增加或者修改场景 btnSave.MouseUpEventHandler += (sender, e) => { string newScenFilePath = "GlobalScene_" + sceneTextBox.Text.Trim (); if (string.IsNullOrEmpty (sceneTextBox.Text.Trim ())) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.PleaseEnterSceneName), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } //更改过场景图 if (btnBG.UnSelectedImagePath == "SecenPicture") {//将选择或拍摄的背景更改对应的名称保存下来 IO.FileUtils.ReNameFile (btnBG.UnSelectedImagePath, "Image_" + newScenFilePath); btnBG.UnSelectedImagePath = "Image_" + newScenFilePath; } scene.BackgroundImagePath = btnBG.UnSelectedImagePath; scene.Name = sceneTextBox.Text.Trim (); //新加场景 if (sceneFilePath == null) { if (IO.FileUtils.Exists (newScenFilePath)) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.HaveTheSame), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } sceneFileList.Add (newScenFilePath); } else { //更改的场景名称已经存在 if (!sceneFileList.Contains (newScenFilePath) && IO.FileUtils.Exists (newScenFilePath)) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.HaveTheSame), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } //场景名更改 if (sceneFilePath != newScenFilePath) { //如果是场景更改,那原文件名也需要更新 IO.FileUtils.ReNameFile (sceneFilePath, newScenFilePath); for (int i = 0; i < sceneFileList.Count; i++) { if (sceneFileList [i] == sceneFilePath) { sceneFileList [i] = newScenFilePath; } } } } // 更新修改后的数据 IO.FileUtils.WriteFileByBytes (newScenFilePath, CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (scene))); IO.FileUtils.WriteFileByBytes (Scene.GlobalSceneFilePath, CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (sceneFileList))); if (action != null) { action (Scene.GlobalSceneFilePath); } dialog.Close (); }; } else { btnEquipmentTitle.TextID = R.MyInternationalizationString.ChangeInformation; btnSave.Width = Application.GetRealWidth (400); //增加或者修改场景 btnSave.MouseUpEventHandler += (sender, e) => { string newScenFilePath = "RoomScene_" + room.Name + "_" + sceneTextBox.Text.Trim (); if (string.IsNullOrEmpty (sceneTextBox.Text.Trim ())) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.PleaseEnterSceneName), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } //更改过场景图 if (btnBG.UnSelectedImagePath == "SecenPicture") {//将选择或拍摄的背景更改对应的名称保存下来 IO.FileUtils.ReNameFile (btnBG.UnSelectedImagePath, "Image_" + newScenFilePath); btnBG.UnSelectedImagePath = "Image_" + newScenFilePath; IO.FileUtils.WriteFileByBytes (sceneFilePath, CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (scene))); } scene.BackgroundImagePath = btnBG.UnSelectedImagePath; scene.Name = sceneTextBox.Text.Trim (); //新加场景 if (sceneFilePath == null) { if (IO.FileUtils.Exists (newScenFilePath)) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.HaveTheSame), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } room.SceneFilePathList.Add (newScenFilePath); } else { //更改的场景名称已经存在 if (!room.SceneFilePathList.Contains (newScenFilePath) && IO.FileUtils.Exists (newScenFilePath)) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.HaveTheSame), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } //场景名更改 if (sceneFilePath != newScenFilePath) { //如果是场景更改,那原文件名也需要更新 IO.FileUtils.ReNameFile (sceneFilePath, newScenFilePath); for (int i = 0; i < room.SceneFilePathList.Count; i++) { if (room.SceneFilePathList [i] == sceneFilePath) { room.SceneFilePathList [i] = newScenFilePath; } } } } // 更新修改后的数据 IO.FileUtils.WriteFileByBytes (newScenFilePath, CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (scene))); room.Save (roomFilePath); //刷新当前房间的数据 if (action != null) { action (roomFilePath); } dialog.Close (); }; } dialog.Show (); } public static void ControlScene (Scene tempScene) { showTip = true; System.Threading.Tasks.Task.Run (() => { int sceneDeviceCount = tempScene.DeviceFilePathList.Count; int controlDeviceIndex = 0; byte [] replyBytes = null; foreach (var deviceFilePath in tempScene.DeviceFilePathList) { controlDeviceIndex++; var common = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (deviceFilePath.Split ('_') [1] == DeviceType.LightDimming.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, device.DelayTimeHeight, device.DelayTimeLow }); } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightRGB.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device != null) { replyBytes = Control.ControlBytesSendHasReturn (Command.SetLogicLoopColor, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 254, device.DelayTimeHeigh,device.DelayTimeLow, 3,device.RStatus,device.GStatus,device.BStatus,0,0}); } } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightSwitch.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, device.DelayTimeHeight, device.DelayTimeLow }); } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainModel.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, (byte)device.Status }); } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainRoller.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1,(byte)device.Status}); } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainTrietex.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1,(byte)device.Status}); } else if (deviceFilePath.Split ('_') [1] == DeviceType.HVAC.ToString () || deviceFilePath.Split ('_') [1] == DeviceType.ACInfrared.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.TemperatureMode, device.IndoorTemperature, device.CoolTemperature, device.HeatTemperature, device.AutoTemperature, device.ChuShiTemperature, device.RealModeAndFanSpeed, device.Power, device.SetMode, device.SetFanSpeed, device.SetTemperature, device.ShaoFanMode}); } else if (deviceFilePath.Split ('_') [1] == DeviceType.FoolHeat.ToString ()) { } else if (deviceFilePath.Split ('_') [1] == DeviceType.FanModule.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { 1,(byte)device.WindSpeed}); } else if (deviceFilePath.Split ('_') [1] == DeviceType.LogicModule.ToString ()) { var device = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath))); if (device == null) { continue; } replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, device.SubnetID, device.DeviceID, new byte [] { device.AreaID,device.AreaSceneID}); } Application.RunOnMainThread (() => { try { if (showTip) { if (replyBytes == null) { string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure); var btnTip = new Button () { Height = Application.GetRealHeight (140), Text = common.Name + tipString + "..." + controlDeviceIndex.ToString () + @"/" + sceneDeviceCount.ToString (), 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; }); }); } } } catch { } }); } }); } } }