using System; using Shared; using Shared.SimpleControl.R; using Shared.SimpleControl; using System.Collections.Generic; namespace SmartHome.UI.SimpleControl.Phone { public class AddDevice : FrameLayout { YIPanel yipanel; public static List displaydevicelist = new List (); public static bool Isbool; public void Show (YIPanel yipanel, string name,List devicelist,byte byteIs) { this.yipanel = yipanel; 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 = name, //TextID = MyInternationalizationString.adddevice, TextSize = 17, }; topFrameLayout.AddChidren (titleName); var add = new Button { Width = Application.GetRealWidth (72), Height = Application.GetRealHeight (58), UnSelectedImagePath = "MusicIcon/add.png", X = Application.GetRealWidth (525), Gravity = Gravity.CenterVertical, }; topFrameLayout.AddChidren (add); 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 VerticalScrolViewLayout (); middle.Y = topFrameLayout.Bottom; middle.Height = Application.GetRealHeight (1136 - 130 - 80); middle.BackgroundColor = 0xff2F2F2F; this.AddChidren (middle); ///添加本地设备点击事件 add.MouseUpEventHandler += (sender, e) => { var localdeviceList = new List (); localdeviceList.Clear (); #region 测试模拟设备 //var lii = new LightLogic () { Type = DeviceType.LightRGB, LoopID = 1, DeviceID = 4, SubnetID = 42, Name = "RGB" }; //Shared.IO.FileUtils.SaveEquipmentMessage (lii, lii.LoopID.ToString ()); //var lii1 = new LightSwitch () { LoopID = 1, DeviceID = 6, SubnetID = 42, Name = "灯光一" }; //Shared.IO.FileUtils.SaveEquipmentMessage (lii1, lii1.LoopID.ToString ()); //var lii11 = new LightDimming () { LoopID = 1, DeviceID = 9, SubnetID = 42, Name = "调光灯" }; //Shared.IO.FileUtils.SaveEquipmentMessage (lii11, lii11.LoopID.ToString ()); //var lii111 = new CurtainRoller () { LoopID = 11, DeviceID = 19, SubnetID = 82, Name = "百分比" }; //Shared.IO.FileUtils.SaveEquipmentMessage (lii111, lii111.LoopID.ToString ()); //var lii1111 = new CurtainModel () { LoopID = 4, DeviceID = 56, SubnetID = 52, Name = "开合帘" }; //Shared.IO.FileUtils.SaveEquipmentMessage (lii1111, lii1111.LoopID.ToString ()); #endregion var filesList = Shared.IO.FileUtils.ReadFiles ().FindAll ((obj) => { string [] str = obj.Split ('_'); return obj.StartsWith ("Equipment_") && str.Length == 5; }); foreach (var file in filesList) { if (!devicelist.Contains (file.Split ('_') [1])) { continue; } var devicestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (file)); if (devicestring == null) { continue; } var jObject = Newtonsoft.Json.Linq.JObject.Parse (devicestring); if (jObject == null) { continue; } YiPanelCommon device = new YiPanelCommon (); device.Name = jObject ["Name"]?.ToString (); device.Type = (DeviceType)jObject.Value ("Type"); device.SubnetID = jObject.Value ("SubnetID"); device.DeviceID = jObject.Value ("DeviceID"); device.LoopID = jObject.Value ("LoopID"); localdeviceList.Add (device); } MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); var localDeviceListPage = new LocalDeviceListPage (); MainPage.MainFrameLayout.AddChidren (localDeviceListPage); localDeviceListPage.Show (() => { devicetypeview (middle,devicelist); }, localdeviceList, name); }); }); }; if (!Isbool) { ///标记设备类型是否读取设备列表; bool IsEnd = false; yipanel.yIPaneDeviceList.Clear (); foreach (var devcie in yipanel.yIPaneDeviceList) { if (devicelist.Count == 0 || (devicelist.Count != 0 && !devicelist.Contains (devcie.Type.ToString ()))) { continue; } IsEnd = true; break; } if (!IsEnd) { refreshdevicelist (() => { Application.RunOnMainThread (() => { devicetypeview (middle, devicelist); }); }, byteIs); } else { devicetypeview (middle, devicelist); } } else { devicetypeview (middle, devicelist); } var btnText = new Button { Y = middle.Bottom, Height = Application.GetRealHeight (80), TextID = MyInternationalizationString.synchronizepaneldata, TextSize = 16, }; this.AddChidren (btnText); btnText.MouseUpEventHandler += (sender,e) => { var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.synchronizepaneldata), Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm)); alert.ResultEventHandler += (sender1, e1) => { if (e1) { syncdevicedata (name); } }; alert.Show (); //syncdevicedata (name); }; } void devicetypeview(VerticalScrolViewLayout middle,Listdevicelist){ middle.RemoveAll (); if (!Isbool) { displaydevicelist.Clear (); foreach (var devcie in yipanel.yIPaneDeviceList) { if (devicelist.Count == 0 || (devicelist.Count != 0 && !devicelist.Contains (devcie.Type.ToString ()))) { continue; } displaydevicelist.Add (devcie); } } foreach (var devcie in displaydevicelist) { var deviceRowLayout = new RowLayout { Height = Application.GetRealHeight (100), BackgroundColor = 0xff323232, }; middle.AddChidren (deviceRowLayout); var devicetypename = new Button { Width = Application.GetRealWidth (200), Text = devcie.Name, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (40), }; deviceRowLayout.AddChidren (devicetypename); var editordevicename = new Button { Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (70), UnSelectedImagePath = "Item/Editor.png", SelectedImagePath = "Item/EditorSelected.png", Gravity = Gravity.CenterVertical, X = Application.GetRealWidth (525), }; deviceRowLayout.AddChidren (editordevicename); editordevicename.MouseUpEventHandler += (sender, e) => { #region 编辑名称View var dialog = new Dialog { Height = Application.GetRealHeight (350), Width = Application.GetRealWidth (520), BackgroundColor = 0xffffffff, }; var btnname = new Button { Gravity = Gravity.CenterHorizontal, Width = Application.GetRealWidth (520), Height = Application.GetRealHeight (80), //Text = "修改名称", TextID = MyInternationalizationString.thename, TextSize = 16, BackgroundColor = 0xff838383, }; dialog.AddChidren (btnname); var deviceTextBox = new EditText () { Y = btnname.Bottom + Application.GetRealHeight (55), Width = Application.GetRealWidth (460), Height = Application.GetRealHeight (80), X = Application.GetRealWidth (30), Radius = (uint)Application.GetRealHeight (2), Text = devcie.Name, BorderWidth = 1, BorderColor = 0xff838383, TextColor = 0xff838383, TextAlignment = TextAlignment.Center, }; dialog.AddChidren (deviceTextBox); var BottomView = new FrameLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (80), Y = dialog.Height - Application.GetRealHeight (80), }; dialog.AddChidren (BottomView); var cancel = new Button { //Text = "取消", TextID = MyInternationalizationString.cancel, TextSize = 16, Width = Application.GetRealWidth (259), Height = Application.GetRealHeight (80), BackgroundColor = 0xff838383, }; BottomView.AddChidren (cancel); cancel.MouseUpEventHandler += (sender11, e11) => { dialog.Close (); }; var btnLineS = new Button () { Width = Application.GetRealWidth (2), Height = Application.GetRealHeight (80), X = cancel.Right, }; BottomView.AddChidren (btnLineS); var Confirm = new Button { X = btnLineS.Right, //Text = "确定", TextID = MyInternationalizationString.confirm, TextSize = 16, BackgroundColor = 0xff838383, Width = Application.GetRealWidth (260), Height = Application.GetRealHeight (80), }; BottomView.AddChidren (Confirm); Confirm.MouseUpEventHandler += (sender1, e1) => { var name = deviceTextBox.Text.Trim (); if (string.IsNullOrEmpty (name)) { new Alert (Language.StringByID (MyInternationalizationString.Tip), Language.StringByID (MyInternationalizationString.thename), Language.StringByID (MyInternationalizationString.Close)).Show (); return; } var dev = displaydevicelist.Find ((c) => c.Name == name); if (dev != null) { new Alert (Language.StringByID (MyInternationalizationString.Tip), Language.StringByID (MyInternationalizationString.Rename), Language.StringByID (MyInternationalizationString.Close)).Show (); return; } else { devcie.Name = name; } var devices = yipanel.yIPaneDeviceList.Find ((c) => { return (c.Type == devcie.Type) && (c.CommonLoopID == devcie.CommonLoopID); }); if (devices != null) { devices.Name = name; } dialog.Close (); System.Threading.Tasks.Task.Run ((() => { try { var bytes = new byte [24]; bytes [0] = devcie.large; bytes [1] = devcie.sub; bytes [2] = devcie.channelnumber; bytes [3] = 0; var pathBytes = System.Text.Encoding.UTF8.GetBytes (devcie.Name); Array.Copy (pathBytes, 0, bytes, 4, pathBytes.Length > 20 ? 20 : pathBytes.Length); Shared.Control.ControlBytesSend (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes); } catch { } finally { Application.RunOnMainThread (() => { devicetypeview (middle, devicelist); }); } })); }; var btnLine1 = new Button () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (2), BackgroundColor = 0xff121212, }; BottomView.AddChidren (btnLine1); dialog.Show (); #endregion }; ///删除 var del = new Button { //TextID = MyInternationalizationString.Musicdel, BackgroundColor = 0xFFFF0000, Text = Language.StringByID (MyInternationalizationString.del), }; deviceRowLayout.AddRightView (del); del.MouseUpEventHandler += (sender, e) => { yipanel.yIPaneDeviceList.RemoveAll ((dev) => dev.Type == devcie.Type && dev.CommonLoopID == devcie.CommonLoopID); displaydevicelist.RemoveAll ((dev) => dev.Type == devcie.Type && dev.CommonLoopID == devcie.CommonLoopID); System.Threading.Tasks.Task.Run ((() => { try { var bytes = new byte [10]; bytes [0] = devcie.large; bytes [1] = devcie.sub; bytes [2] = devcie.channelnumber; bytes [3] = devcie.PropertyValue; bytes [4] =0; bytes [5] = devcie.SubnetID; bytes [6] = devcie.DeviceID; bytes [7] = devcie.large; bytes [8] = devcie.typevalue; bytes [9] = devcie.LoopID; Control.ControlBytesSend (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes); } catch { } finally { Application.RunOnMainThread (() => { devicetypeview (middle, devicelist); }); } })); }; } } /// /// 读取面板设备 /// /// Action. /// Name. void refreshdevicelist (Action action, byte byteIs) { byte bytevalue = 0; List intvalue = new List (); intvalue.Clear (); var type = yipanel.DeviceTypeList.Find ((obj) => { return obj.largeclass == byteIs; }); if (type == null) { return; } MainPage.Loading.Start (); new System.Threading.Thread (() => { try { var validBytes = Shared.Control.ControlBytesSendHasReturn (Command.YIPanelDevice, yipanel.SubnetID, yipanel.DeviceID, new byte [2] { type.largeclass, type.subclass }); if (validBytes != null) { string len = ""; switch (validBytes [0]) { case 1: { bytevalue = 8; for (int i = 3; i <= 10; i++) { var bytes = validBytes [i]; var stringlen = System.Convert.ToString (bytes, 2); var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length)); if (str.Length == 8) { for (int j = 7; j >= 0; j--) { len += str.Substring (j, 1); } } } for (int j = 0; j < len.Length; j++) { var strvalue = len.Substring (j, 1); if (strvalue == "1") { intvalue.Add (j + 1); } } } break; case 2: { bytevalue = 4; for (int i = 3; i < 4; i++) { var bytes = validBytes [i]; var stringlen = System.Convert.ToString (bytes, 2); var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length)); if (str.Length == 8) { for (int j = 7; j >= 0; j--) { len += str.Substring (j, 1); } } } for (int j = 0; j < len.Length; j++) { var strvalue = len.Substring (j, 1); if (strvalue == "1") { intvalue.Add (j + 1); } } } break; case 7: { bytevalue = 10; for (int i = 3; i < 4; i++) { var bytes = validBytes [i]; var stringlen = System.Convert.ToString (bytes, 2); var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length)); if (str.Length == 8) { for (int j = 7; j >= 0; j--) { len += str.Substring (j, 1); } } } for (int j = 0; j < len.Length; j++) { var strvalue = len.Substring (j, 1); if (strvalue == "1") { intvalue.Add (j + 1); } } } break; case 8: { bytevalue = 10; for (int i = 3; i < 4; i++) { var bytes = validBytes [i]; var stringlen = System.Convert.ToString (bytes, 2); var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length)); if (str.Length == 8) { for (int j = 7; j >= 0; j--) { len += str.Substring (j, 1); } } } for (int j = 0; j < len.Length; j++) { var strvalue = len.Substring (j, 1); if (strvalue == "1") { intvalue.Add (j + 1); } } } break; case 9: { bytevalue = 3; for (int i = 3; i < 4; i++) { var bytes = validBytes [i]; var stringlen = System.Convert.ToString (bytes, 2); var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length)); if (str.Length == 8) { for (int j = 7; j >= 0; j--) { len += str.Substring (j, 1); } } } for (int j = 0; j < len.Length; j++) { var strvalue = len.Substring (j, 1); if (strvalue == "1") { intvalue.Add (j + 1); } } } break; case 19: { bytevalue = 7; for (int i = 3; i < 4; i++) { var bytes = validBytes [i]; var stringlen = System.Convert.ToString (bytes, 2); var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length)); if (str.Length == 8) { for (int j = 7; j >= 0; j--) { len += str.Substring (j, 1); } } } for (int j = 0; j < len.Length; j++) { var strvalue = len.Substring (j, 1); if (strvalue == "1") { intvalue.Add (j + 1); } } } break; } } for (int i = 0; i < intvalue.Count; i++) { var value = (byte)intvalue [i]; var common = new YiPanelCommon (); ///这条命令是读取获取设备名称20byte var nameBytes= Shared.Control.ControlBytesSendHasReturn (Command.YIPanelDeviceInof, yipanel.SubnetID, yipanel.DeviceID, new byte [4] { type.largeclass, type.subclass, value, 0 }); if (nameBytes != null) { var bytes = new byte [20]; for (int j = 0; j < 20; j++) { bytes [j] = nameBytes [j + 4]; } common.Name = System.Text.Encoding.UTF8.GetString (bytes).Replace ("\0", ""); } ///这条命令是读取设备子网号和设备号等信息; var deviceinfoBytes= Shared.Control.ControlBytesSendHasReturn (Command.YIPanelDeviceInof, yipanel.SubnetID, yipanel.DeviceID, new byte [4] { type.largeclass, type.subclass, value, bytevalue }); if (deviceinfoBytes != null) { common.large = deviceinfoBytes [0];//大类 common.sub = deviceinfoBytes [1];//小类 common.channelnumber = deviceinfoBytes [2];//设备通道号; common.PropertyValue = deviceinfoBytes [3];//属性 common.SubnetID = deviceinfoBytes [5];//子网号 common.DeviceID = deviceinfoBytes [6];//设备号 common.LoopID = deviceinfoBytes [9];//回路号 common.typevalue = deviceinfoBytes [8];//设备 } switch (deviceinfoBytes [0]) { case 1: { if (deviceinfoBytes [8] == 0) { common.Type = DeviceType.LightDimming; } else if (deviceinfoBytes [8] == 1) { common.Type = DeviceType.LightSwitch; } } break; case 2: { //0开合帘,1卷帘,2窗帘模块 if (deviceinfoBytes [8] == 0) { common.Type = DeviceType.CurtainTrietex; } else if (deviceinfoBytes [8] == 1) { common.Type = DeviceType.CurtainRoller; }else if (deviceinfoBytes [8] == 2) { common.Type = DeviceType.CurtainModel; } } break; case 7: { //if (deviceinfoBytes [8] == 0) { // common.Type = DeviceType.HVAC; //} common.Type = DeviceType.HVAC; } break; case 8: { //if (deviceinfoBytes [8] == 0) { // common.Type = DeviceType.FoolHeat; //} else if (deviceinfoBytes [8] == 1) { // common.Type = DeviceType.FoolHeatPanel; //} common.Type = DeviceType.FoolHeat; } break; case 9: { //if (deviceinfoBytes [8] == 0) { // common.Type = DeviceType.MusicModel; //} else if (deviceinfoBytes [8] == 2) { // common.Type = DeviceType.MusicPanel; //} common.Type = DeviceType.MusicModel; } break; case 19: { common.Type = DeviceType.FreshAir; } break; } yipanel.yIPaneDeviceList.Add (common); } } catch (Exception e) { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); action (); }); } }) { IsBackground = true }.Start (); } /// /// 写入面板数据 /// /// Name. void syncdevicedata (string name) { if (displaydevicelist.Count == 0) { return; } var commonlist = new List (); commonlist.Clear (); foreach (var device in displaydevicelist) { if (device.channelnumber == 0) { continue; } var dev = yipanel.yIPaneDeviceList.Find ((c) => c.Type == device.Type && c.CommonLoopID == device.CommonLoopID && device.channelnumber == c.channelnumber); if (dev == null) { commonlist.Add (device); } } if (commonlist.Count == 0) { return; } MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.insync) + "..."); new System.Threading.Thread (() => { try { for (int i = 0; i <= commonlist.Count; i++) { var common = commonlist [i]; ///写入设备给面板数据; byte [] bytes = new byte [10]; bytes [2] = common.channelnumber; bytes [4] = 1; bytes [5] = common.SubnetID; bytes [6] = common.DeviceID; bytes [9] = common.LoopID; ///写入设备名称给面板数据; byte [] bytes1 = new byte [24]; bytes1 [2] = common.channelnumber; bytes1 [3] = 0;//属性ID switch (common.Type) { case DeviceType.LightSwitch: { ///写入设备给面板数据; bytes [0] = 1; bytes [1] = 12; bytes [3] = 8;//属性ID bytes [7] = 1; bytes [8] = 1; ///写入设备名称给面板数据; bytes1 [0] = 1; bytes1 [1] = 12; } break; case DeviceType.LightDimming: { ///写入设备给面板数据; bytes [0] = 1;//大类 bytes [1] = 12;//小类 bytes [3] = 8;//属性ID bytes [7] = 1; bytes [8] = 0; ///写入设备名称给面板数据; bytes1 [0] = 1;//大类 bytes1 [1] = 12;//小类 } break; case DeviceType.CurtainModel: { ///写入设备给面板数据; bytes [0] = 2;//大类 bytes [1] = 3;//小类 bytes [3] = 4;//属性ID bytes [7] = 2; bytes [8] = 2; ///写入设备名称给面板数据; bytes1 [0] = 2;//大类 bytes1 [1] = 3;//小类 } break; case DeviceType.CurtainTrietex: { ///写入设备给面板数据; bytes [0] = 2;//大类 bytes [1] = 3;//小类 bytes [3] = 4;//属性ID bytes [7] = 2; bytes [8] = 1; ///写入设备名称给面板数据; bytes1 [0] = 2;//大类 bytes1 [1] = 3;//小类 } break; case DeviceType.CurtainRoller: { ///写入设备给面板数据; bytes [0] = 2;//大类 bytes [1] = 3;//小类 bytes [3] = 4;//属性ID bytes [7] = 2; bytes [8] = 0; ///写入设备名称给面板数据; bytes1 [0] = 2;//大类 bytes1 [1] = 3;//小类 } break; case DeviceType.HVAC: { ///写入设备给面板数据; bytes [0] = 7;//大类 bytes [1] = 0;//小类 bytes [3] = 10;//属性ID bytes [7] = 7; bytes [8] = 0; ///写入设备名称给面板数据; bytes1 [0] = 7;//大类 bytes1 [1] = 0;//小类 } break; case DeviceType.MusicModel: { ///写入设备给面板数据; bytes [0] = 9;//大类 bytes [1] = 1;//小类 bytes [3] = 3;//属性ID bytes [7] = 9; bytes [8] = 0; ///写入设备名称给面板数据; bytes1 [0] = 9;//大类 bytes1 [1] = 1;//小类 } break; case DeviceType.FoolHeat: { ///写入设备给面板数据; bytes [0] = 8;//大类 bytes [1] = 2;//小类 bytes [3] = 10;//属性ID bytes [7] = 8; bytes [8] = 0; ///写入设备名称给面板数据; bytes1 [0] = 8;//大类 bytes1 [1] = 2;//小类 } break; case DeviceType.FreshAir: { ///写入设备给面板数据; bytes [0] = 19;//大类 bytes [1] = 0;//小类 bytes [3] = 7;//属性ID bytes [7] = 19; bytes [8] = 0; ///写入设备名称给面板数据; bytes1 [0] = 19;//大类 bytes1 [1] = 0;//小类 } break; } ///写入设备给面板命令; /// Control.ControlBytesSend (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes); var data= Control.ControlBytesSendHasReturn (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes); ///写入设备名称给面板命令; var pathBytes = System.Text.Encoding.UTF8.GetBytes (common.Name); Array.Copy (pathBytes, 0, bytes1, 4, pathBytes.Length > 20 ? 20 : pathBytes.Length); var dataname=Control.ControlBytesSendHasReturn (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes1); yipanel.yIPaneDeviceList.Add (common); } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }){IsBackground=true}.Start(); } } }