using System; using System.Collections.Generic; using Shared; using Shared.SimpleControl; using Shared.SimpleControl.R; namespace SuperGateWay { public class AddLogicPage : FrameLayout { public AddLogicPage () { Tag = "Logic"; } public void Show (GateWay superGateWay) { this.AddChidren (new Button { Height = Application.GetRealHeight (30), BackgroundColor = 0xFF1F1F1F, }); var topFrameLayout = new FrameLayout { Height = Application.GetRealHeight (100), Y = Application.GetRealHeight (30), BackgroundColor = 0xFF1F1F1F, }; AddChidren (topFrameLayout); var hdl = new Button { Width = Application.GetRealWidth (154), Height = Application.GetRealHeight (90), X = Application.GetRealWidth (486), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Logo/Logo.png", }; topFrameLayout.AddChidren (hdl); var titleName = new Button { TextID = MyInternationalizationString.selection, 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 middle = new FrameLayout (); middle.Y = topFrameLayout.Bottom; middle.Height = Application.GetRealHeight (Application.DesignHeight - 130); middle.BackgroundColor = 0xff2F2F2F; this.AddChidren (middle); var titlerl = new RowLayout { Height = Application.GetRealHeight (110), BackgroundColor = 0xff0f0f0f, }; middle.AddChidren (titlerl); titlerl.AddChidren ( new Button { X = Application.GetRealWidth (60), //Text = "请选择需要组合的条件", TextID = MyInternationalizationString.combination, TextSize = 17, TextAlignment = TextAlignment.CenterLeft } ); #region ----时间条件---- var timerowlayout = new RowLayout { Height = Application.GetRealHeight (100), Y=titlerl.Bottom, }; middle.AddChidren (timerowlayout); ///时间条件 var btntime = new Button { Text = Language.StringByID (MyInternationalizationString.timecondition), TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (60), }; timerowlayout.AddChidren (btntime); var btntimeback = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (550), }; timerowlayout.AddChidren (btntimeback); EventHandler timeclick = (sender, e) => { var timePage = new TimePage (); MainPage.MainFrameLayout.AddChidren (timePage); timePage.Show (superGateWay); }; btntime.MouseUpEventHandler += timeclick; btntimeback.MouseUpEventHandler += timeclick; #endregion #region ----设备状态条件---- var devicerowlayout = new RowLayout { Height = Application.GetRealHeight (100), Y=timerowlayout.Bottom, }; middle.AddChidren (devicerowlayout); ///设备状态条件 var btndevice = new Button { //Text = "设备状态条件", TextID = MyInternationalizationString.devicestate, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (60), }; devicerowlayout.AddChidren (btndevice); var btndeviceback = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (550), }; devicerowlayout.AddChidren (btndeviceback); EventHandler devicestateclick = (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 deviceStateCondition = new DeviceStateCondition (); MainPage.MainFrameLayout.AddChidren (deviceStateCondition); deviceStateCondition.Show (superGateWay); }); }); } else { superGateWay.Commons.Clear (); superGateWay.Commons = DeviceList; var deviceStateCondition = new DeviceStateCondition (); MainPage.MainFrameLayout.AddChidren (deviceStateCondition); deviceStateCondition.Show (superGateWay); } } else { var deviceStateCondition = new DeviceStateCondition (); MainPage.MainFrameLayout.AddChidren (deviceStateCondition); deviceStateCondition.Show (superGateWay); } }; devicerowlayout.MouseUpEventHandler += devicestateclick; btndevice.MouseUpEventHandler += devicestateclick; btndeviceback.MouseUpEventHandler += devicestateclick; #endregion #region ----已有自动化条件---- var existencerowlayout = new RowLayout { Height = Application.GetRealHeight (100), Y=devicerowlayout.Bottom, }; middle.AddChidren (existencerowlayout); ///已有自动化条件 var btnexistence = new Button { //Text = "已有自动化条件", TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (60), TextID = MyInternationalizationString.automation, }; existencerowlayout.AddChidren (btnexistence); var btnexistenceback = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (550), }; existencerowlayout.AddChidren (btnexistenceback); EventHandler logicclick = (sender, e) => { var logicListAutomation = new LogicListAutomation (); MainPage.MainFrameLayout.AddChidren (logicListAutomation); logicListAutomation.Show (superGateWay); }; btnexistenceback.MouseUpEventHandler += logicclick; btnexistence.MouseUpEventHandler += logicclick; existencerowlayout.MouseUpEventHandler += logicclick; #endregion } /// /// 读取所有设备列表 /// 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 (); } } }