using System; using Shared; using Shared.SimpleControl.R; using System.Collections.Generic; using Shared.SimpleControl.Phone; using Shared.SimpleControl; using Newtonsoft.Json.Linq; namespace SuperGateWay { public class ScenePage : FrameLayout { VerticalScrolViewLayout addscenelView; FrameLayout LongPressFrameLayout = new FrameLayout (); Button beforeClickButton = new Button (); Button btnlogiclist; /// /// 场景按键 /// Button btnscenelist; GateWay gateWay; public static bool isScene; public static byte SubnetID; public static byte DeviceID; public void Show (GateWay superGateWay) { //Shared.IO.FileUtils.DeleteFile ("SceneListFilePath"); //var s = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile ("SceneListFilePath")); //var SceneList = Newtonsoft.Json.JsonConvert.DeserializeObject> (s); this.BackgroundColor = 0xFF1F1F1F; this.gateWay = superGateWay; #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 (MyInternationalizationString.gateway), TextSize = 19, }; topView.AddChidren (title); var btnadd = new Button { X = Application.GetRealWidth (640 - 106), Width = Application.GetRealWidth (80), Height = Application.GetRealHeight (60), UnSelectedImagePath = "MusicIcon/add.png", //UnSelectedImagePath = "Logo/Logo.png", Gravity = Gravity.CenterVertical, }; topView.AddChidren (btnadd); var back = new Button () { Height = Application.GetRealHeight (100), Width = Application.GetRealWidth (85), UnSelectedImagePath = "Item/Back.png", SelectedImagePath = "Item/BackSelected.png", Gravity = Gravity.CenterVertical, }; topView.AddChidren (back); back.MouseUpEventHandler += (sender, e) => { RemoveFromParent (); }; var btnl = new Button () { Height = 1, BackgroundColor = 0xFF2F2F2F, Y = topView.Height - 1 }; topView.AddChidren (btnl); #endregion #region titelView var TitleView = new FrameLayout { Height = Application.GetRealHeight (110), BackgroundColor = 0xff0f0f0f, Y = topView.Bottom, }; AddChidren (TitleView); ///场景列表 btnscenelist = new Button () { Width = Application.GetRealWidth (320), TextSize = 16, TextColor = 0xFFEB642C, Text = Language.StringByID (MyInternationalizationString.SceneList), }; TitleView.AddChidren (btnscenelist); ///逻辑列表 btnlogiclist = new Button () { Width = Application.GetRealWidth (320), X = btnscenelist.Right, TextSize = 16, //SelectedTextColor = 0xFFEB642C, //TextColor =0xFFEB642C, Text = Language.StringByID (MyInternationalizationString.LogicList), }; TitleView.AddChidren (btnlogiclist); var btnLine = new Button { Width = 1, BackgroundColor = 0xFF2D2D2D, X = btnscenelist.Right, }; TitleView.AddChidren (btnLine); var btnLine1 = new Button { Height = 1, Y = TitleView.Height - 1, BackgroundColor = 0xFF2D2D2D, }; TitleView.AddChidren (btnLine1); #endregion var fra = new VerticalScrolViewLayout { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (Application.DesignHeight - 80) - TitleView.Bottom, Y = TitleView.Bottom, BackgroundColor = 0xff2F2F2F, }; AddChidren (fra); addscenelView = new VerticalScrolViewLayout (); fra.AddChidren (addscenelView); var refreshfl = new FrameLayout { Height = Application.GetRealHeight (80), Y = fra.Bottom, }; AddChidren (refreshfl); ///更新场景 var btnrefresscene = new Button { TextSize = 16, Text = Language.StringByID (MyInternationalizationString.UpdateScene), }; refreshfl.AddChidren (btnrefresscene); var ther = new Button () { BackgroundColor = 0xFFD7D7D7, Height = Application.GetRealHeight (2), }; //refreshfl.AddChidren (ther); if (!isScene) { var scenestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile ("SceneListFilePath")); var SceneList = Newtonsoft.Json.JsonConvert.DeserializeObject> (scenestring); if (SceneList != null) { for (int i = 0; i < SceneList.Count; i++) { var sidvalue = SceneList [i]; sidvalue.SidToDevice (); } } if (gateWay.Scenes.Count == 0) { if (SceneList == null) { addscenelView.RemoveAll (); refreshscenelist (() => { addview (); }); } else { gateWay.Scenes.Clear (); gateWay.Scenes = SceneList; MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); addview (); }); }); } } else { MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); addview (); }); }); } } else { addlogicview (); btnlogiclist.TextColor = 0xFFEB642C; btnscenelist.TextColor = 0xFFFFFFFF; btnrefresscene.Text = Language.StringByID (MyInternationalizationString.Updatelogic); } //场景列表 btnscenelist.MouseUpEventHandler += (sender, e) => { isScene = false; addscenelView.RemoveAll (); btnscenelist.TextColor = 0xFFEB642C; btnlogiclist.TextColor = 0xFFFFFFFF; btnrefresscene.Text = Language.StringByID (MyInternationalizationString.UpdateScene); MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); addview (); }); }); }; //逻辑列表 btnlogiclist.MouseUpEventHandler += (sender, e) => { var logicstring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile ("LogicListFilePath")); var LogicList = Newtonsoft.Json.JsonConvert.DeserializeObject> (logicstring); isScene = true; addscenelView.RemoveAll (); btnlogiclist.TextColor = 0xFFEB642C; btnscenelist.TextColor = 0xFFFFFFFF; btnrefresscene.Text = Language.StringByID (MyInternationalizationString.Updatelogic); if (gateWay.Logics.Count == 0) { ///读取逻辑列表的方法 if (LogicList == null) { refreshlogiclist (() => { addlogicview (); }); } else { gateWay.Logics.Clear (); gateWay.Logics = LogicList; MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); addlogicview (); }); }); } } else { MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); addlogicview (); }); }); } }; ///更新(场景/逻辑)点击事件 btnrefresscene.MouseUpEventHandler += (sender, e) => { addscenelView.RemoveAll (); if (!isScene) { refreshscenelist (() => { addview (); }); } else { refreshlogiclist (() => { addlogicview (); }); } }; ///新添加(场景/逻辑)点击事件 btnadd.MouseUpEventHandler += (sender, e) => { if (!isScene) { ScenePhoneMethod.AddOrUpdataSuperGateWaySceneBaseMassage (gateWay, null, () => { addview (); }); } else { //gateWay.Tag = "View"; Logic.CurrentLogic = new Logic (); var addLogicPage = new AddLogicPage (); MainPage.MainFrameLayout.AddChidren (addLogicPage); addLogicPage.Show (superGateWay); } }; } /// /// 加载场景界面的方法 /// void addview () { addscenelView.RemoveAll (); FrameLayout scenesRowView = null; var list = new List (gateWay.Scenes); //默认在最后增加添加场景的图标 for (int i = 0; i < list.Count; i++) { var scene = list [i]; if (i % 2 == 0) { scenesRowView = new FrameLayout { Height = Application.GetRealHeight (250), }; addscenelView.AddChidren (scenesRowView); } var sceneView = new FrameLayout { Width = Application.GetRealWidth (320), BackgroundImagePath = scene.BackgoundImagePath, //BackgroundImagePath = Shared.IO.FileUtils.Exists ("Image_SuperGateWayScene_" + scene.sid) ? "Image_SuperGateWayScene_" + scene.sid : scene.BackgoundImagePath, }; scenesRowView.AddChidren (sceneView); if (i % 2 == 1) { sceneView.X = Application.GetRealWidth (320); } var btnSceneName = new Button { Height = Application.GetRealHeight (40), Y = Application.GetRealHeight (210), BackgroundColor = SkinStyle.Current.Black50Transparent, }; sceneView.AddChidren (btnSceneName); var btnBGC = new Button { UnSelectedImagePath = "Item/nextbutton.png", SelectedImagePath = "Item/SceneSettingBackground.png", TextAlignment = TextAlignment.BottomCenter, Text = scene.name, TextColor = 0xffffffff, TextSize = 15, }; sceneView.AddChidren (btnBGC); btnBGC.MouseLongEventHandler += (sender, e) => { if (string.IsNullOrEmpty (scene.name)) return; beforeClickButton.IsSelected = false; LongPressFrameLayout.RemoveFromParent (); LongPressFrameLayout = new FrameLayout { BackgroundImagePath = "Item/SceneSettingBackground.png", }; sceneView.AddChidren (LongPressFrameLayout); LongPressFrameLayout.MouseUpEventHandler += (sender2, e2) => { LongPressFrameLayout.RemoveFromParent (); }; var btnEditor = new Button { Width = Application.GetRealWidth (70), Height = Application.GetRealHeight (76), UnSelectedImagePath = "Item/UserSceneEditIcon.png", X = Application.GetRealWidth (220), Y = Application.GetRealHeight (13), }; LongPressFrameLayout.AddChidren (btnEditor); btnEditor.MouseUpEventHandler += (sender1, e1) => { LongPressFrameLayout.RemoveFromParent (); var gatewayAddDevice = new GatewayAddDevice (); MainPage.MainFrameLayout.AddChidren (gatewayAddDevice); gatewayAddDevice.Show (gateWay, scene, () => { btnBGC.Text = scene.name; sceneView.BackgroundImagePath = scene.BackgoundImagePath; }); }; var btnDel = new Button () { Width = Application.GetRealWidth (70), Height = Application.GetRealHeight (76), UnSelectedImagePath = "Item/UserSceneDelIcon.png", X = Application.GetRealWidth (40), Y = Application.GetRealHeight (20), }; LongPressFrameLayout.AddChidren (btnDel); btnDel.MouseUpEventHandler += (senderDel, eDel) => { //var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.DissolvedGroup), // Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm)); //alert.ResultEventHandler += (sender1, e1) => { // if (e1) { } //}; gateWay.Scenes.Remove (scene); addview (); Control.DeleteScene (scene); gateWay.save ("SceneListFilePath"); }; }; btnBGC.MouseUpEventHandler += (sender, e) => { LongPressFrameLayout.RemoveFromParent (); beforeClickButton.IsSelected = false; beforeClickButton = btnBGC; btnBGC.IsSelected = true; Control.ControlScene (scene); }; } } /// /// 获取场景列表以及场景设备信息 /// async void refreshscenelist (Action action) { gateWay.Scenes.Clear (); MainPage.Loading.Start (); await System.Threading.Tasks.Task.Run (() => { try { var tempList = Control.getSceneList (); if (tempList == null) { return; } var array = tempList.ToArray (); Array.Sort (array); foreach (var uid in array) { try { var result = Control.getSceneByUID (uid.Replace (".json", "")); if (result == null) { continue; } var scene = Newtonsoft.Json.JsonConvert.DeserializeObject (result); if (scene != null) { var dev= gateWay.Scenes.Find ((obj)=> { return obj.uid == scene.uid; }); if (dev == null) { scene.SidToDevice (); gateWay.Scenes.Add (scene); gateWay.save ("SceneListFilePath"); } } } catch { } } } catch { } }); MainPage.Loading.Hide (); action (); } /// /// 加载逻辑界面的方法 /// void addlogicview () { addscenelView.RemoveAll (); foreach (var logic in gateWay.Logics) { var logicrowlayout = new RowLayout { Height = Application.GetRealHeight (100), }; addscenelView.AddChidren (logicrowlayout); var btnname = new Button { //Text = "逻辑一", Text = logic.name, X = Application.GetRealWidth (50), TextAlignment = TextAlignment.CenterLeft, Gravity = Gravity.CenterVertical, }; logicrowlayout.AddChidren (btnname); var btnswitch = new Button { Width = Application.GetMinRealAverage (90), Height = Application.GetMinRealAverage (53), UnSelectedImagePath = "Item/SwitchClose.png", SelectedImagePath = "Item/SwitchOpen.png", X = Application.GetRealWidth (520), Gravity = Gravity.CenterVertical, }; logicrowlayout.AddChidren (btnswitch); btnswitch.MouseUpEventHandler += (sender1, e1) => { btnswitch.IsSelected = !btnswitch.IsSelected; if (btnswitch.IsSelected) { //逻辑开 control (1, logic); } else { //逻辑关 control (0, logic); } }; if (logic.ByteState != null && logic.ByteState [2] == 1) { btnswitch.IsSelected = true; } else { btnswitch.IsSelected = false; } var btndelay = new Button { Width = Application.GetRealWidth (150), Height = Application.GetRealHeight (50), X = Application.GetRealWidth (300), Y = Application.GetRealHeight (25), TextAlignment = TextAlignment.Center, TextColor = 0xffcccccc, }; logicrowlayout.AddChidren (btndelay); ///延时控件 var btndelaytext = new Button { BackgroundColor = 0xff676767, Text = Language.StringByID (MyInternationalizationString.delayed), }; logicrowlayout.AddRightView (btndelaytext); if (logic.param.ContainsKey ("delay")) { var delayvalue = logic.param ["delay"]?.ToString (); if (delayvalue != "" && delayvalue != "0") { var l = int.Parse (delayvalue) / 10; btndelay.Text = l.ToString () + "s"; } } btndelaytext.MouseUpEventHandler += (sender, e) => { Dialog window = new Dialog (); window.Show (); var frameLayout = new FrameLayout { Width = Application.GetRealWidth (580), Height = Application.GetRealHeight (300), X = Application.GetRealWidth (30), Y = Application.GetRealHeight (330), BackgroundColor = 0xffffffff, }; window.AddChidren (frameLayout); var Modifyname = new Button { Width = Application.GetRealWidth (580), Height = Application.GetRealHeight (80), TextID = MyInternationalizationString.devicedelaytime, //Text = "请输入设备延时的时间(单位/s)", Y = Application.GetRealHeight (10), TextSize = 16, TextColor = SkinStyle.Current.MusicTipTextColor, }; frameLayout.AddChidren (Modifyname); var delaytext = new EditText { Width = Application.GetRealWidth (540), Height = Application.GetRealHeight (80), Y = Application.GetRealHeight (80), X = Application.GetRealWidth (20), Gravity = Gravity.CenterVertical, BackgroundColor = SkinStyle.Current.MusicTipBackgroundColor, //TextColor = 0xff000000, Radius = 1, BorderWidth = 1, BorderColor = SkinStyle.Current.MusicEditBorderColor, TextColor = SkinStyle.Current.MusicTextColor, }; frameLayout.AddChidren (delaytext); var line = new Button { Height = Application.GetRealHeight (2), Y = Application.GetRealHeight (218), BackgroundColor = 0xff666666, }; frameLayout.AddChidren (line); var cancelrow = new RowLayout { Y = Application.GetRealHeight (220), Height = Application.GetRealHeight (80), Width = Application.GetRealWidth (289), BackgroundColor = 0xffcccccc, }; frameLayout.AddChidren (cancelrow); var cancel = new Button { TextID = MyInternationalizationString.cancel, //TextColor = 0xff000000, TextSize = 18, TextColor = SkinStyle.Current.MusicTextColor, }; cancelrow.AddChidren (cancel); cancel.MouseUpEventHandler += (sender1, e1) => { window.Close (); }; var ther = new Button { Y = Application.GetRealHeight (220), Height = Application.GetRealHeight (80), Width = Application.GetRealWidth (2), X = Application.GetRealHeight (289), BackgroundColor = 0xff666666, }; frameLayout.AddChidren (ther); var confirmrow = new RowLayout { Y = Application.GetRealHeight (220), X = Application.GetRealWidth (291), Height = Application.GetRealHeight (80), Width = Application.GetRealWidth (289), BackgroundColor = 0xffcccccc, }; frameLayout.AddChidren (confirmrow); var confirm = new Button { TextID = MyInternationalizationString.confirm, //TextColor = 0xff000000, TextSize = 18, TextColor = SkinStyle.Current.MusicTextColor, }; confirmrow.AddChidren (confirm); //if (logic.param.ContainsKey ("delay")) { // var delayvalue = logic.param ["delay"]?.ToString (); // if (delayvalue != "" && delayvalue != "0") { // var l = int.Parse (delayvalue) / 10; // delaytext.Text = l.ToString () + "s"; // } //} confirm.MouseUpEventHandler += (sender1, e1) => { var isNumber = delaytext.Text.Trim (); if (!int.TryParse (isNumber, out int number) || isNumber == "") { new Alert (Language.StringByID (MyInternationalizationString.Tip), Language.StringByID (MyInternationalizationString.inputnumber), Language.StringByID (MyInternationalizationString.Close)).Show (); return; } var vel = int.Parse (isNumber); if (logic.param.ContainsKey ("delay")) { logic.param.Remove ("delay"); logic.param.Add ("delay", vel * 10); } else { logic.param.Add ("delay", vel * 10); } btndelay.Text = isNumber + "s"; window.Close (); System.Threading.Tasks.Task.Run (() => { Control.modifylogic (logic); Application.RunOnMainThread (() => { addlogicview (); }); }); gateWay.save ("LogicListFilePath"); }; }; ///编辑 var edit = new Button { //TextID = MyInternationalizationString.Musicdel BackgroundColor = 0xFF253451, Text = Language.StringByID (MyInternationalizationString.edit), }; logicrowlayout.AddRightView (edit); edit.MouseUpEventHandler += (sender, e) => { Logic.CurrentLogic = logic; Logic.CurrentLogic.Tag = "true"; if (gateWay.Commons.Count == 0) { var devicestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile ("DeviceListFilePath")); var DeviceList = Newtonsoft.Json.JsonConvert.DeserializeObject> (devicestring); if (DeviceList == null) { refreshDeviceList (() => { Application.RunOnMainThread (() => { var logicCommunalPage = new LogicCommunalPage (); MainPage.MainFrameLayout.AddChidren (logicCommunalPage); logicCommunalPage.Show (gateWay, () => { btnname.Text = logic.name; addlogicview (); }); }); }); } else { var logicCommunalPage = new LogicCommunalPage (); MainPage.MainFrameLayout.AddChidren (logicCommunalPage); logicCommunalPage.Show (gateWay, () => { btnname.Text = logic.name; addlogicview (); }); } } else { var logicCommunalPage = new LogicCommunalPage (); MainPage.MainFrameLayout.AddChidren (logicCommunalPage); logicCommunalPage.Show (gateWay, () => { btnname.Text = logic.name; addlogicview (); }); } }; ///删除 var del = new Button { //TextID = MyInternationalizationString.Musicdel, BackgroundColor = 0xFFFF0000, Text = Language.StringByID (MyInternationalizationString.del), }; logicrowlayout.AddRightView (del); del.MouseUpEventHandler += (sender, e) => { gateWay.Logics.Remove (logic); Control.DeleteLogic (logic); gateWay.save ("LogicListFilePath"); addlogicview (); }; } } /// /// 获取逻辑列表 /// async void refreshlogiclist (Action action) { gateWay.Logics.Clear (); MainPage.Loading.Start (); await System.Threading.Tasks.Task.Run (() => { try { var logicuidlist = Control.getLogiclist (); if (logicuidlist == null) { return; } var array = logicuidlist.ToArray (); Array.Sort (array); foreach (var logicuid in array) { try { var result = Control.getLogicByUID (logicuid.Replace (".json", "")); //string result="{\"module\":\"logic\",\"name\":\"logic_name\",\"time\":\"12:25:00-16:25:00\",\"sid\":\"3001550602000003\",\"param\":{\"operator\":\"&&\",\"delay\":\"10\"},\"input\":[{\"sid\":\"1001560306010001\",\"value\":\"1\",\"continue\":\"5\",\"compare\":\"=\"},{\"sid\":\"1001590405010001\",\"value\":\"1\",\"continue\":\"5\",\"compare\":\"=\",\"chinese_calendar\":\"false\"}],\"output\":[{\"objects\":[{\"sid\":\"1001560305000002\",\"value\":\"0\",\"delay\":\"10\"},{\"sid\":\"1001780302000002\",\"value\":\"0\",\"delay\":\"10\"}]},{\"objects\":[{\"sid\":\"1001780302010002\",\"value\":\"1\",\"delay\":\"10\"},{\"sid\":\"1001780305010002\",\"value\":\"1\",\"delay\":\"10\"}]}]}"; if (result == null) { continue; } var logicstring = Newtonsoft.Json.JsonConvert.DeserializeObject (result); if (logicstring != null) { var dev = gateWay.Logics.Find ((obj) => { return obj.sid == logicstring.sid; }); if (dev == null) { ///加入逻辑列表 gateWay.Logics.Add (logicstring); gateWay.save ("LogicListFilePath"); ///读取逻辑状态(开或关); control (3, logicstring); } } } catch (Exception e) { var s = e.Message; } } } catch { } }); MainPage.Loading.Hide (); action (); } /// /// 读取所有设备列表 /// async void refreshDeviceList (Action action) { gateWay.Commons.Clear (); await System.Threading.Tasks.Task.Run (() => { try { var uidList = Control.GetDevicesUidList (); if (uidList == null) { return; } foreach (var uid in uidList) { var result = Control.GetDeviceByUID (uid.Replace (".json", "")); if (result == null) { continue; } var common = Newtonsoft.Json.JsonConvert.DeserializeObject (result); if (common != null) { foreach (var @object in common.objects) { var functionString = @object ["function"]?.ToString (); if (functionString == null) { continue; } var functions = Newtonsoft.Json.JsonConvert.DeserializeObject> (functionString); if (functions == null) { continue; } foreach (var function in functions) { var device = SuperCommon.GetCommon (function.sid, gateWay); if (device != null) { //前找出当前回路的对象有没有存在 var dev = gateWay.Commons.Find ((c) => c.Type == device.Type && c.CommonLoopID == device.CommonLoopID); if (dev == null) { gateWay.Commons.Add (device); if (@object ["name"] != null) { device.Name = @object ["name"]?.ToString (); } else { device.Name = ""; } gateWay.save ("DeviceListFilePath"); } if (function.sid != null) { device.functions.Add (function); } } } } } } } catch { } finally { Application.RunOnMainThread (() => { action (); }); } }); } void control (byte val, Logic logic) { try { if (logic == null || logic.sid == null) { return; } var sidUlong = Convert.ToUInt64 (logic.sid, 16); var zoneId = (byte)((sidUlong >> 40) & 0xFF); var logicId = (byte)((sidUlong >> 32) & 0xFF); if (val == 0 || val == 1) { Shared.Control.ControlBytesSend(Command.SetSeries, gateWay.SubnetID, gateWay.DeviceID, new byte [] { zoneId, logicId, val }, SendCount.Three); } else { var stateBytes = Shared.Control.ControlBytesSendHasReturn (Command.LogicstateControl, gateWay.SubnetID, gateWay.DeviceID, new byte [] { zoneId, logicId }); if (stateBytes != null) { if (stateBytes [0] == zoneId && stateBytes [1] == logicId) { logic.ByteState = stateBytes; } } } } catch { } } } }