using System; using System.Collections.Generic; using Shared; using Shared.SimpleControl; using Shared.SimpleControl.R; using SmartHome; namespace SuperGateWay { public class DeviceTarget : FrameLayout { public DeviceTarget () { Tag = "Logic"; } public void Show (GateWay superGateWay) { this.BackgroundColor = 0xFF1F1F1F; this.AddChidren (new Button { Height = Application.GetRealHeight (30), }); var topFrameLayout = new FrameLayout { Height = Application.GetRealHeight (100), Y = Application.GetRealHeight (30), }; AddChidren (topFrameLayout); var titleName = new Button { //Text = "添加执行目标", TextID = MyInternationalizationString.addImplementationgoals, TextSize = 17, }; topFrameLayout.AddChidren (titleName); var back = new Button { Width = Application.GetRealWidth (82), Height = Application.GetRealHeight (89), X = Application.GetRealWidth (10), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "MusicIcon/HomepageBack.png", }; topFrameLayout.AddChidren (back); back.MouseDownEventHandler += (sender, e) => { RemoveFromParent (); }; var tetleframeLayout = new FrameLayout { Y = topFrameLayout.Bottom, Height = Application.GetRealHeight (100), BackgroundColor=0xff0f0f0f, }; AddChidren (tetleframeLayout); var tetlebtn = new Button { Width = Application.GetRealWidth (200), TextID = MyInternationalizationString.selecttype, //Text = "请选择类型", TextSize = 16, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (40), }; tetleframeLayout.AddChidren (tetlebtn); var middle = new FrameLayout (); middle.Y = tetleframeLayout.Bottom; middle.Height = Application.GetRealHeight (Application.DesignHeight - 130 - 100); middle.BackgroundColor = 0xff2F2F2F; this.AddChidren (middle); var deviceRowLayout = new RowLayout { Height = Application.GetRealHeight (100), BackgroundColor = 0xff323232, }; middle.AddChidren (deviceRowLayout); var device = new Button { Width = Application.GetRealWidth (200), TextID = MyInternationalizationString.device, //Text = "设备", TextSize = 16, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (40), }; deviceRowLayout.AddChidren (device); var btndeviceback = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (525),//550 }; deviceRowLayout.AddChidren (btndeviceback); EventHandler deviceclick = (sender, e) => { var devicestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile ("DeviceListFilePath")); var DeviceList = Newtonsoft.Json.JsonConvert.DeserializeObject> (devicestring); if (superGateWay.Commons.Count == 0) { if (DeviceList == null) { refreshDeviceList (superGateWay,() => { Application.RunOnMainThread (() => { var logicDevicePage = new LogicDevicePage (); MainPage.MainFrameLayout.AddChidren (logicDevicePage); logicDevicePage.Show (superGateWay); }); }); } else { superGateWay.Commons.Clear (); superGateWay.Commons = DeviceList; var logicDevicePage = new LogicDevicePage (); MainPage.MainFrameLayout.AddChidren (logicDevicePage); logicDevicePage.Show (superGateWay); } } else { var logicDevicePage = new LogicDevicePage (); MainPage.MainFrameLayout.AddChidren (logicDevicePage); logicDevicePage.Show (superGateWay); } }; deviceRowLayout.MouseUpEventHandler += deviceclick; device.MouseUpEventHandler += deviceclick; btndeviceback.MouseUpEventHandler += deviceclick; var sceneRowLayout = new RowLayout { Height = Application.GetRealHeight (100), BackgroundColor = 0xff323232, Y=deviceRowLayout.Bottom, }; middle.AddChidren (sceneRowLayout); var scene = new Button { Width = Application.GetRealWidth (200), TextID = MyInternationalizationString.scene, //Text = "场景", TextSize = 16, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (40), }; sceneRowLayout.AddChidren (scene); var btnsceneback = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (525),//550 }; sceneRowLayout.AddChidren (btnsceneback); EventHandler sceneclick = (sender, e) => { MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); var addScenePage = new AddScenePage (); MainPage.MainFrameLayout.AddChidren (addScenePage); addScenePage.Show (superGateWay); }); }); }; sceneRowLayout.MouseUpEventHandler += sceneclick; scene.MouseUpEventHandler += sceneclick; btnsceneback.MouseUpEventHandler += sceneclick; ///已有自动化 var existenceRowLayout = new RowLayout { Height = Application.GetRealHeight (100), BackgroundColor = 0xff323232, Y=sceneRowLayout.Bottom, }; //middle.AddChidren (existenceRowLayout); var existence = new Button { Width = Application.GetRealWidth (200), TextID = MyInternationalizationString.Alreadyautomated, //Text = "已有自动化", TextSize = 16, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (40), }; existenceRowLayout.AddChidren (existence); var btnexistenceback = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (525),//550 }; existenceRowLayout.AddChidren (btnexistenceback); EventHandler existenceclick = (sender, e) => { var logicListPage = new LogicListPage (); MainPage.MainFrameLayout.AddChidren (logicListPage); logicListPage.Show (superGateWay); }; existenceRowLayout.MouseUpEventHandler+=existenceclick; existence.MouseUpEventHandler+=existenceclick; btnexistenceback.MouseUpEventHandler+=existenceclick; } /// /// 读取所有设备列表 /// async void refreshDeviceList (GateWay superGateWay,Action action) { superGateWay.Commons.Clear (); MainPage.Loading.Start (); 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; } // result = "{\"module\":\"multi\",\"Name\":\"混合模块\",\"uid\":\"45678941\",\"objects\":[{\"name\":\"空调1\",\"type\":\"air_conditioner\",\"function\":[{\"sid\":\"1001560506000001\",\"attr\":{\"type\":\"switch\"}},{\"sid\":\"1001560506010001\",\"attr\":{\"type\":\"switch\"}}]},{\"name\":\"空调2\",\"type\":\"air_conditioner\",\"function\":[{\"sid\":\"1001660806000001\",\"attr\":{\"type\":\"switch\"}},{\"sid\":\"1001660806010001\",\"attr\":{\"type\":\"switch\"}}]}]}"; 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, superGateWay); if (device != null) { //前找出当前回路的对象有没有存在 var dev = superGateWay.Commons.Find ((c) => c.Type == device.Type && c.CommonLoopID == device.CommonLoopID); if (dev == null) { superGateWay.Commons.Add (device); if (@object ["name"] != null) { device.Name = @object ["name"]?.ToString (); } else { device.Name = ""; } superGateWay.save ("DeviceListFilePath"); } if (function.sid != null) { device.functions.Add (function); } } } } } } } catch { } finally { Application.RunOnMainThread (() => { action (); }); } }); MainPage.Loading.Hide (); } } }