using System; using System.Collections.Generic; using Newtonsoft.Json.Linq; using Shared; using Shared.SimpleControl; using Shared.SimpleControl.Phone; using Shared.SimpleControl.R; using SmartHome; namespace SuperGateWay { class GatewayAddDevice : FrameLayout { GateWay gateWay; Scene scene; VerticalScrolViewLayout middleVerticalScrolViewLayout; public void Show (GateWay superGateWay, Scene superGateWayScene, Action action) { this.gateWay = superGateWay; this.scene = superGateWayScene; AddChidren (new Button { Height = Application.GetRealHeight (36), BackgroundColor = 0xFF2f2f2f }); var frameLayout = new FrameLayout () { Height = Application.GetRealHeight (100), Width = LayoutParams.MatchParent, Y = Application.GetRealHeight (36), BackgroundColor = 0xFF2f2f2f, }; AddChidren (frameLayout); var backButton = new Button { X = Application.GetRealWidth (0), Height = Application.GetRealHeight (100), Width = Application.GetRealWidth (85), UnSelectedImagePath = "Item/Back.png", SelectedImagePath = "Item/BackSelected.png", Gravity = Gravity.CenterVertical, }; backButton.MouseUpEventHandler += (sender, e) => { RemoveFromParent (); action (); gateWay.save ("SceneListFilePath"); }; frameLayout.AddChidren (backButton); var textButton = new Button { X = Application.GetRealWidth (20) + backButton.Right, Height = Application.GetRealHeight (50), Width = Application.GetRealWidth (400), Text = superGateWayScene.name, Gravity = Gravity.CenterVertical, TextAlignment = TextAlignment.CenterLeft, }; frameLayout.AddChidren (textButton); var buteditor = new Button { Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (70), UnSelectedImagePath = "Item/Editor.png", SelectedImagePath = "Item/EditorSelected.png", Gravity = Gravity.CenterVertical, X = textButton.Right + Application.GetRealWidth (50), }; frameLayout.AddChidren (buteditor); ///编辑场景按钮 buteditor.MouseUpEventHandler += (sender, e) => { ScenePhoneMethod.AddOrUpdataSuperGateWaySceneBaseMassage (superGateWay, scene, () => { textButton.Text = scene.name; //Application.RunOnMainThread (() => { // RemoveFromParent (); // GatewayAddDevice gatewayAddDevice = new GatewayAddDevice (); // MainPage.MainFrameLayout.AddChidren (gatewayAddDevice); // gatewayAddDevice.Show (gateWay, scene); //}); }); }; var devicelistnameframelayout = new FrameLayout { Height = Application.GetRealHeight (80), BackgroundColor = 0xFF000000, Y = frameLayout.Bottom, }; AddChidren (devicelistnameframelayout); var btndevicelistnametext = new Button { Height = Application.GetRealHeight (80), X = Application.GetRealWidth (30), Text = Language.StringByID (MyInternationalizationString.DeviceList), TextAlignment = TextAlignment.CenterLeft, }; devicelistnameframelayout.AddChidren (btndevicelistnametext); middleVerticalScrolViewLayout = new VerticalScrolViewLayout { Height = Application.GetRealHeight (1134 - 136 - 80 - 100), Y = devicelistnameframelayout.Bottom, BackgroundColor = 0xFF2f2f2f, }; AddChidren (middleVerticalScrolViewLayout); if (scene.deviceList.Count == 0) { ///显示设备的方法 loadDevices (); } else { if (gateWay.Commons.Count != 0) { loadDevices (); } else { var devicestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile ("DeviceListFilePath")); var DeviceList = Newtonsoft.Json.JsonConvert.DeserializeObject> (devicestring); MainPage.Loading.Start (); if (DeviceList==null) { refreshDeviceList (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); ///加载所有设备界面的方法; loadDevices (); }); }); } else { gateWay.Commons.Clear (); gateWay.Commons = DeviceList; Application.RunOnMainThread (() => { MainPage.Loading.Hide (); ///加载所有设备界面的方法; loadDevices (); }); } } } var adddevicefl = new FrameLayout { Height = Application.GetRealHeight (100), BackgroundColor = 0xFF2f2f2f, Y = middleVerticalScrolViewLayout.Bottom, }; AddChidren (adddevicefl); ///添加设备按钮 var btnadddevice = new Button () { TextAlignment = TextAlignment.Center, TextID = MyInternationalizationString.AddNewDevice, }; adddevicefl.AddChidren (btnadddevice); var ther = new Button () { BackgroundColor = 0xFFD7D7D7, Height = Application.GetRealHeight (2), }; adddevicefl.AddChidren (ther); ///添加设备按钮点击事件; btnadddevice.MouseUpEventHandler += (sender, e) => { var dialog = new Dialog () { BackgroundColor = 0xFFF5F6F7, Width = Application.GetRealWidth (480), Height = Application.GetRealHeight (720), }; #region 标题 var titleFrameLayout = new FrameLayout { Width = dialog.Width, BackgroundColor = 0xFF484848, Height = Application.GetRealHeight (80), }; dialog.AddChidren (titleFrameLayout); var btnTitle = new Button () { Width = dialog.Width, TextAlignment = TextAlignment.Center, TextID = MyInternationalizationString.AddNewDevice, }; titleFrameLayout.AddChidren (btnTitle); ///搜索所有设备按钮 var searchdevice = new Button { Width = Application.GetMinRealAverage (60), Height = Application.GetMinRealAverage (80), X = Application.GetRealWidth (380), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "MusicIcon/seekdevice.png", }; titleFrameLayout.AddChidren (searchdevice); var dropDownLayout = new FrameLayout { Width = dialog.Width, Height = Application.GetRealHeight (70), Y = btnTitle.Height, BackgroundColor = 0xFF000000, //BackgroundImagePath = "Item/DialogDropdown.png", // dropDownLayout.BackgroundImagePath = "Item/DialogDropdownSelected.png"; }; dialog.AddChidren (dropDownLayout); var 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); var btnEquipmentType = new Button () { Width = Application.GetRealHeight (460), Height = Application.GetRealHeight (70), X = btnEquipmentIcon.Right + Application.GetRealWidth (20), TextAlignment = TextAlignment.CenterLeft, Text = Language.StringByID (MyInternationalizationString.All), }; dropDownLayout.AddChidren (btnEquipmentType); //Button btnDrodIcon = new Button () { // // X = dialogBodyView.Width - Application.GetRealWidth (80), // Height = Application.GetRealHeight (46), // Width = Application.GetRealWidth (47), // UnSelectedImagePath = "Item/Down.png", // SelectedImagePath = "Item/DownSelected.png", // Gravity = Gravity.CenterVertical, //}; //dropDownLayout.AddChidren (btnDrodIcon); var btnDrodIcon = new Button { Width = Application.GetRealWidth (47), Height = Application.GetRealHeight (46), Gravity = Gravity.CenterVertical, X = Application.GetRealWidth (380), UnSelectedImagePath = "Item/Down.png", SelectedImagePath = "Item/DownSelected.png", }; dropDownLayout.AddChidren (btnDrodIcon); #endregion btnDrodIcon.IsSelected = true; var deviceList = new VerticalScrolViewLayout { Height = Application.GetRealHeight (490), Width = Application.GetRealWidth (500), Y = dropDownLayout.Bottom, }; dialog.AddChidren (deviceList); ///搜索所有设备点击事件 searchdevice.MouseUpEventHandler += (sender1, e1) => { btnDrodIcon.IsSelected = true; btnEquipmentIcon.UnSelectedImagePath = "Item/PointBig.png"; btnEquipmentType.Text = Language.StringByID (MyInternationalizationString.All); deviceList.RemoveAll (); MainPage.Loading.Start (); ///读取所有设备; refreshDeviceList (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); ///加载所有设备界面的方法; //deviceListView (deviceList, DeviceType.UnKown); deviceListView (deviceList, new List ()); }); }); }; ///所有设备类型点击事件 EventHandler selecteddevice = (sender12, e12) => { btnDrodIcon.IsSelected = false; deviceList.BackgroundColor = 0xFF484848; deviceList.RemoveAll (); ///创建一个string类型列表,添加所有设备类型; var typeTempList = new List{ Language.StringByID(MyInternationalizationString.All), Language.StringByID(MyInternationalizationString.Lights), Language.StringByID(MyInternationalizationString.Curtains), Language.StringByID(MyInternationalizationString.AC), Language.StringByID(MyInternationalizationString.MusicModel), Language.StringByID(MyInternationalizationString.FoolHeat), Language.StringByID(MyInternationalizationString.ReshAirRead), Language.StringByID(MyInternationalizationString.Button), Language.StringByID(MyInternationalizationString.Sensor), Language.StringByID(MyInternationalizationString.DoorLock), Language.StringByID(MyInternationalizationString.DryContactPanel), }; ///显示所有设备类型界面(例如:灯光,...) foreach (var typename in typeTempList) { var rowtype = new RowLayout { Height = Application.GetRealHeight (80), //BackgroundColor = 0xFFD7D7D7, }; deviceList.AddChidren (rowtype); var btnbypeIcon = new Button { Width = Application.GetRealHeight (65), Height = Application.GetRealHeight (65), UnSelectedImagePath = "Item/PointBig.png", X = Application.GetRealWidth (10), Gravity = Gravity.CenterVertical, }; rowtype.AddChidren (btnbypeIcon); var btndevicename = new Button { Gravity = Gravity.CenterVertical, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (70), //TextColor = 0xff121212, SelectedBackgroundColor = 0xFFE9652D, Text = typename, }; rowtype.AddChidren (btndevicename); ///某个设备类型点击事件(例如:灯光,....) EventHandler handler = (sender33, e33) => { btnDrodIcon.IsSelected = true; deviceList.BackgroundColor = 0xFFD7D7D7; btnEquipmentType.Text = typename; List list = new List { }; ///分类显示(例如灯光:{灯光1,灯光2...}) if (typename == Language.StringByID (MyInternationalizationString.Lights)) { btnEquipmentIcon.UnSelectedImagePath = "Item/Light.png"; list.Add (DeviceType.LightDimming); list.Add (DeviceType.LightSwitch); } else if (typename == Language.StringByID (MyInternationalizationString.Curtains)) { btnEquipmentIcon.UnSelectedImagePath = "Curtain/Curtain.png"; list.Add (DeviceType.CurtainModel); list.Add (DeviceType.CurtainRoller); } else if (typename == Language.StringByID (MyInternationalizationString.AC)) { btnEquipmentIcon.UnSelectedImagePath = @"AC/AC.png"; //deviceType = DeviceType.AC; list.Add (DeviceType.HVAC); } else if (typename == Language.StringByID (MyInternationalizationString.MusicModel)) { btnEquipmentIcon.UnSelectedImagePath = "Item/Music.png"; //deviceType = DeviceType.Music; list.Add (DeviceType.MusicModel); } else if (typename == Language.StringByID (MyInternationalizationString.FoolHeat)) { btnEquipmentIcon.UnSelectedImagePath = "FH/FH_Heat.png"; list.Add (DeviceType.FoolHeat); } else if (typename == Language.StringByID (MyInternationalizationString.Button)) { btnEquipmentIcon.UnSelectedImagePath = "Item/Panel.png"; list.Add (DeviceType.MechanicalSwitch); list.Add (DeviceType.AutomaticSwitch); } else if (typename == Language.StringByID (MyInternationalizationString.ReshAirRead)) { btnEquipmentIcon.UnSelectedImagePath = "FreshAir/FreshAir.png"; list.Add (DeviceType.FreshAir); } else if (typename == Language.StringByID (MyInternationalizationString.Sensor)) { btnEquipmentIcon.UnSelectedImagePath = "Sensor/Sensor.png"; list.Add (DeviceType.Sensor); } else if (typename == Language.StringByID (MyInternationalizationString.DoorLock)) { list.Add (DeviceType.DoorLock); btnEquipmentIcon.UnSelectedImagePath = "DoorLockPic/doorLockHomePage.png"; } else if (typename == Language.StringByID (MyInternationalizationString.DryContactPanel)) { btnEquipmentIcon.UnSelectedImagePath = "Item/DryContact.png"; list.Add (DeviceType.DryContact); } else { btnEquipmentIcon.UnSelectedImagePath = "Item/PointBig.png"; } ///加载界面方法 deviceListView (deviceList, list); }; rowtype.MouseUpEventHandler += handler; btnbypeIcon.MouseUpEventHandler += handler; btndevicename.MouseUpEventHandler += handler; } }; btnDrodIcon.MouseUpEventHandler += selecteddevice; btnEquipmentType.MouseUpEventHandler += selecteddevice; btnEquipmentIcon.MouseUpEventHandler += selecteddevice; var BottomView = new FrameLayout { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (80), BackgroundColor = 0xF0121212, Y = dialog.Height - Application.GetRealHeight (80), }; dialog.AddChidren (BottomView); var btnBack = new Button { Width = Application.GetRealWidth (240), TextAlignment = TextAlignment.Center, TextID = MyInternationalizationString.Cancel, }; BottomView.AddChidren (btnBack); ///创建一个common列表,记录scene.ControlTargetList的数据. var targetList = new List (); targetList.AddRange (scene.deviceList.ToArray ()); ///取消按钮事件 btnBack.MouseUpEventHandler += (sender1, e1) => { dialog.Close (); scene.deviceList.Clear (); scene.deviceList.AddRange (targetList.ToArray ()); }; var btnDline = new Button () { Width = 1, BackgroundColor = 0xF0313131,//0xFFe2e3e4, X = btnBack.Right, }; BottomView.AddChidren (btnDline); var btnSave = new Button { Width = Application.GetRealWidth (240), X = btnDline.Right, TextAlignment = TextAlignment.Center, TextID = MyInternationalizationString.confirm, Radius = 1, }; BottomView.AddChidren (btnSave); ///保存按钮点击事件 btnSave.MouseUpEventHandler += (sender11, e11) => { dialog.Close (); loadDevices (); Control.ModifyScene (scene, (obj) => { }); }; dialog.Show (); deviceListView (deviceList, new List ()); }; } /// /// 加载所有设备界面的方法 /// void loadDevices () { middleVerticalScrolViewLayout.RemoveAll (); ///加载设备界面代码 foreach (var common in scene.deviceList) { switch (common.Type) { case DeviceType.LightSwitch: scenedevicelistview (common, "Item/Light.png"); break; case DeviceType.LightDimming: scenedevicelistview (common, "Item/Light.png"); break; case DeviceType.CurtainModel: scenedevicelistview (common, "Curtain/Curtain.png"); break; case DeviceType.CurtainRoller: scenedevicelistview (common, "Curtain/Curtain.png"); break; case DeviceType.HVAC: scenedevicelistview (common, @"AC/AC.png"); break; case DeviceType.MechanicalSwitch: scenedevicelistview (common, "Item/Panel.png"); break; case DeviceType.AutomaticSwitch: scenedevicelistview (common, "Item/Panel.png"); break; case DeviceType.FreshAir: scenedevicelistview (common, "FreshAir/FreshAir.png"); break; case DeviceType.FoolHeat: scenedevicelistview (common, "FH/FH_Heat.png"); break; case DeviceType.DryContact: scenedevicelistview (common, "Item/DryContact.png"); break; case DeviceType.DoorLock: scenedevicelistview (common, "DoorLockPic/doorLockHomePage.png"); break; case DeviceType.Sensor: scenedevicelistview (common, "Sensor/Sensor.png"); break; case DeviceType.MusicModel: scenedevicelistview (common, "Item/Music.png"); break; } } } /// /// 读取所有设备列表 /// 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; } // 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.GetBySid (o.sid); 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); } } } } } //if (common != null) { // foreach (Dictionary v in common.objects) { // var device = SuperCommon.GetBySid (v ["sid"]?.ToString ()); // if (device != null) { // //前找出当前回路的对象有没有存在 // if (gateWay.Commons.Find ((c) => // c.GetType () == device.GetType () && c.CommonLoopID == device.CommonLoopID // ) == null) { // gateWay.Commons.Add (device); // } // if (v ["attr"] != null) { // var attr = JObject.Parse (v ["attr"].ToString()); // device.Name = attr ["name"]?.ToString (); // } else{ // device.Name =""; // } // } // } //} } } catch { } finally { Application.RunOnMainThread (() => { action (); }); } }); } ///加载类型设备界面(例如:灯光类型,....) void deviceListView (VerticalScrolViewLayout deviceListViewv, List deviceType) { deviceListViewv.RemoveAll (); deviceListViewv.BackgroundColor = 0xFFD7D7D7; foreach (var common in gateWay.Commons) { if (deviceType.Count != 0 && !deviceType.Contains (common.Type)) { continue; } var row = new RowLayout { Height = Application.GetRealHeight (80), }; deviceListViewv.AddChidren (row); var PointBig = new Button { Width = Application.GetRealHeight (65), Height = Application.GetRealHeight (65), UnSelectedImagePath = "Item/PointBig.png", X = Application.GetRealWidth (10), Gravity = Gravity.CenterVertical, }; row.AddChidren (PointBig); var btnlightdevicename = new Button { Gravity = Gravity.CenterVertical, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (70), Width = Application.GetRealWidth (300), TextColor = 0xff121212, //SelectedBackgroundColor = 0xFFE9652D, Text = common.Name, }; row.AddChidren (btnlightdevicename); var btnCheck = new Button { Width = Application.GetRealWidth (60), Height = Application.GetRealHeight (59), Gravity = Gravity.CenterVertical, X = Application.GetRealWidth (380), SelectedImagePath = "Light/CheckOn.png", UnSelectedImagePath = "Light/Check.png", }; row.AddChidren (btnCheck); if (scene.deviceList.Find ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID) != null) { btnCheck.IsSelected = true; } EventHandler isselected = (sender1, e1) => { btnCheck.IsSelected = !btnCheck.IsSelected; if (btnCheck.IsSelected) { if (null == scene.deviceList.Find ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID)) { scene.deviceList.Add (common); } //scene.ControlTargetList.Add (common); } else { scene.deviceList.RemoveAll ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID); //scene.ControlTargetList.Remove (common); } }; row.MouseUpEventHandler += isselected; PointBig.MouseUpEventHandler += isselected; btnlightdevicename.MouseUpEventHandler += isselected; btnCheck.MouseUpEventHandler += isselected; } } ///加载场景里所有设备界面 void scenedevicelistview (SuperGateWayCommon common, string iconfilepath) { var row = new RowLayout { Height = Application.GetRealHeight (93), }; middleVerticalScrolViewLayout.AddChidren (row); var del = new Button { TextID = MyInternationalizationString.Musicdel, BackgroundColor = 0xFFFF0000, }; row.AddRightView (del); del.MouseUpEventHandler += (sender, e) => { if (scene.deviceList.Find ((c) => c.Type == common.Type&& c.CommonLoopID == common.CommonLoopID) != null) { scene.deviceList.Remove (common); row.RemoveFromParent (); Control.ModifyScene (scene, (obj) => { }); } }; var btnIcon = new Button { Width = Application.GetRealHeight (43), Height = Application.GetRealHeight (43), X = Application.GetRealWidth (30), UnSelectedImagePath = iconfilepath, Gravity = Gravity.CenterVertical, }; row.AddChidren (btnIcon); var device = gateWay.Commons.Find ((obj) => obj.Type == common.Type&& obj.CommonLoopID == common.CommonLoopID); var btnName = new Button { Width = Application.GetRealWidth (342), Height = LayoutParams.MatchParent, Text = common.Name == null ? "unkown" : common.Name, TextAlignment = TextAlignment.CenterLeft, X = btnIcon.X + btnIcon.Width + Application.GetRealWidth (10), }; row.AddChidren (btnName); if (device != null && device.Name != null) { btnName.Text = device.Name; common.functions= device.functions; } var btnRight = new Button { Width = Application.GetRealWidth (28), Height = Application.GetRealHeight (40), Gravity = Gravity.CenterVertical, X = Application.GetRealWidth (580), UnSelectedImagePath = "Item/Right.png", SelectedImagePath = "Item/RightSelected.png", }; row.AddChidren (btnRight); ///设备信息(例如:灯光.....) EventHandler handler = (sender, e) => { new DeviceIfonChangedataPage ().Show (scene, common, btnName.Text); }; row.MouseUpEventHandler += handler; btnIcon.MouseUpEventHandler += handler; btnName.MouseUpEventHandler += handler; btnRight.MouseUpEventHandler += handler; } } }