using System; using System.Collections.Generic; using System.Text; using ZigBee.Device; namespace Shared.Phone.UserCenter.DeviceShard { /// /// 分享设备管理的主界面 /// public class ShardDeviceManagementMainForm : UserCenterCommonForm { #region ■ 变量声明___________________________ /// /// 锁 /// private object objLock = new object(); /// /// 列表控件 /// private VerticalScrolViewLayout listView = null; /// /// 搜索控件 /// //private SearchEditText txtSearchControl = null; /// /// 行控件的信息(Keys:Mac地址) /// private Dictionary dicRowInfo = new Dictionary(); #endregion #region ■ 初始化_____________________________ /// /// 画面显示(底层会固定调用此方法,借以完成画面创建) /// public void ShowForm() { //设置标题信息 base.SetTitleText(Language.StringByID(R.MyInternationalizationString.DeviceManagement)); //添加菜单 this.AddTopMenu(); //初始化中部控件 this.InitMiddleFrame(); } /// /// 初始化中部控件 /// public void InitMiddleFrame() { //清空bodyFrame this.ClearBodyFrame(); //初始化搜索控件 this.InitSearchControl(); new System.Threading.Thread(() => { //初始化设备列表控件 this.InitDeviceListControl(); }) { IsBackground = true }.Start(); } #endregion #region ■ 初始化搜索控件_____________________ /// /// 初始化搜索控件 /// private void InitSearchControl() { //搜索 //if (txtSearchControl != null) //{ // txtSearchControl.CloseThead(); //} //txtSearchControl = new SearchEditText(); //bodyFrameLayout.AddChidren(txtSearchControl); ////绑定回调函数 //txtSearchControl.BindEvent(this.SetRowDataBySearchKeys); //列表控件 listView = new VerticalScrolViewLayout(); //listView.Y = txtSearchControl.Bottom + Application.GetRealHeight(30); //listView.Height = bodyFrameLayout.Height - txtSearchControl.Bottom - Application.GetRealHeight(30); listView.Height = bodyFrameLayout.Height; bodyFrameLayout.AddChidren(listView); } #endregion #region ■ 初始化设备列表控件_________________ /// /// 初始化设备列表控件 /// private void InitDeviceListControl() { //根据MAC合并设备列表 this.MargeAllDeviceByMac(); //没有设备 if (this.dicRowInfo.Count == 0) { Application.RunOnMainThread(() => { if (this.Parent != null) { //在界面中间显示无分享设备的提示消息 this.ShowNotShardDeviceMsg(); } }); } else { //显示进度条 this.ShowProgressBar(); //添加全部的设备菜单行 this.DoAddAllMenuRow(true); //关闭进度条 this.CloseProgressBar(); } } /// /// 添加全部的设备菜单行 /// /// 是否读取设备在线状态 private void DoAddAllMenuRow(bool readOnline) { //添加设备的菜单行 int count = 0; foreach (var rowInfo in this.dicRowInfo.Values) { Application.RunOnMainThread(() => { if (this.Parent == null) { return; } count++; //添加设备的菜单行 this.AddDeviceMenuRow(rowInfo.listDevice); if (count == this.dicRowInfo.Count && readOnline == true) { //开启设备在线线程 this.StartDeviceListControlThread(); } }); } } #endregion #region ■ 添加设备菜单行_____________________ /// /// 添加设备的菜单行 /// /// 设备对象 private void AddDeviceMenuRow(List listDevice) { var rowInfo = this.dicRowInfo[listDevice[0].DeviceAddr]; if (rowInfo.frameLayout != null) { //直接沿用上一次的控件(按键值搜索专用) listView.AddChidren(rowInfo.frameLayout); //重新刷新信息 if (rowInfo.dicDetailRow != null) { foreach (var tempDevice in listDevice) { string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(tempDevice); if (rowInfo.dicDetailRow.ContainsKey(mainkeys) == true) { rowInfo.dicDetailRow[mainkeys].RefreshControlInfo(tempDevice); } } } return; } //创建一个可以展开和收缩的FrameLayout,相当于菜单栏 var frame = new FrameLayout(); frame.Height = ControlCommonResourse.ListViewRowHeight; listView.AddChidren(frame); rowInfo.frameLayout = frame; //控件 var rowlayout = new DeviceObjectViewRow(listDevice); frame.AddChidren(rowlayout); rowlayout.InitControl(); rowInfo.MenuRow = rowlayout; //向右的图标 var btnRight = new MostRightEmptyView(); btnRight.UnSelectedImagePath = "Item/Next.png"; btnRight.SelectedImagePath = "Item/Down.png"; rowlayout.AddChidren(btnRight, ChidrenBindMode.NotBind); //单击事件 btnRight.MouseUpEventHandler += (sender, e) => { btnRight.IsSelected = !btnRight.IsSelected; //展开或者隐藏列表 this.ShowDetailList(listDevice, btnRight.IsSelected); }; //编辑 var btnEditor = new RowEditorButton(); rowlayout.AddRightView(btnEditor); btnEditor.MouseUpEventHandler += (sender, e) => { //打开设备信息界面 string macAddr = listDevice[0].DeviceAddr; //修改设备名字 var form = new ShardDeviceMacInfoEditorForm(); form.AddForm(form, this.dicRowInfo[macAddr].listDevice); form.ActionFormClose += (deviceName) => { if (string.IsNullOrEmpty(deviceName) == false) { this.dicRowInfo[macAddr].MenuRow.btnDeviceName.Text = deviceName; } }; }; rowlayout.MouseUpEvent += (sender, e) => { //如果单击的是图标的话,跳转设备回路配置界面 var form = new ShardConfigureDeviceListForm(); this.AddForm(form, listDevice); }; } #endregion #region ■ 添加设备明细行_____________________ /// /// 添加设备的明细行 /// /// 容器 /// private void AddDeviceDetailRow(FrameLayout frame, CommonDevice device) { var rowInfo = this.dicRowInfo[device.DeviceAddr]; if (rowInfo.dicDetailRow == null) { rowInfo.dicDetailRow = new Dictionary(); } //行控件 var rowDevice = new DeviceRoomViewRow(device); rowDevice.Y = frame.ChildrenCount * ControlCommonResourse.ListViewRowHeight; frame.AddChidren(rowDevice); rowDevice.InitControl(); //保存控件 string maikey = Common.LocalDevice.Current.GetDeviceMainKeys(device); rowInfo.dicDetailRow[maikey] = rowDevice; //检测设备是否拥有测试的功能 if (Common.LocalDevice.Current.DeviceIsCanTest(device) == true) { //测试 var btnTest = new RowSecondRightIconView(); btnTest.UnSelectedImagePath = "Item/Test.png"; btnTest.SelectedImagePath = "Item/TestSelected.png"; rowDevice.AddChidren(btnTest, ChidrenBindMode.NotBind); btnTest.MouseUpEventHandler += (sender, e) => { //测试 Common.LocalDevice.Current.SetTestCommand(device); }; } //展开模式时,子控件行与顶部行的X的偏移量 int Xvalue = ControlCommonResourse.ChidrenXvalue; rowDevice.btnIcon.X += Xvalue; rowDevice.btnDevie.X += Xvalue; rowDevice.btnRoom.X += Xvalue; rowDevice.MouseUpEvent += (sender, e) => { var form = new ShardDeviceEpointInfoForm(); this.AddForm(form, device); form.ActionNameChangedEvent += (deviceName, listName) => { //变更房间 Common.Room.CurrentRoom.ChangedRoom(device, listName); //刷新全部信息信息 rowDevice.listRoom = listName; rowDevice.RefreshControlInfo(device); }; }; } #endregion #region ■ 添加菜单___________________________ /// /// 添加菜单 /// private void AddTopMenu() { var btnIcon = new TopLayoutMostRightView(); btnIcon.UnSelectedImagePath = "Item/More.png"; btnIcon.SelectedImagePath = "Item/MoreSelected.png"; topFrameLayout.AddChidren(btnIcon); btnIcon.MouseUpEventHandler += ((sender, e) => { var frame = new TopRightMenuControl(this, 1); //同步共享数据 string deviceMenu = Language.StringByID(R.MyInternationalizationString.uSynchronizeShardData); frame.AddRowMenu(deviceMenu, (obj) => { //确认是否同步共享数据? string msg = Language.StringByID(R.MyInternationalizationString.uConfirmSynchronizeShardDataMsg); this.ShowConfirmMsg(msg, "RefreshShardData"); }); }); } #endregion #region ■ 刷新共享___________________________ /// /// 刷新共享 /// public void RefreshShardData() { new System.Threading.Thread(async () => { var result = await HdlShardLogic.Current.SynchronizeDbSharedContent(); if (result == false) { //同步共享数据失败 this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uSynchronizeShardDataFail)); return; } //初始化本地的网关信息 Common.LocalGateway.Current.ReFreshByLocal(); //初始化本地的设备信息 Common.LocalDevice.Current.ReFreshByLocal(); //从本地重新加载全部的房间 Common.Room.RefreshAllRoomByLocation(); Application.RunOnMainThread(() => { //重新初始化界面 this.InitMiddleFrame(); }); }) { IsBackground = true }.Start(); } #endregion #region ■ 键值搜索___________________________ /// /// 根据搜索键值,设定列表数据 /// /// Search key. private void SetRowDataBySearchKeys(string searchKey) { lock (objLock) { Application.RunOnMainThread(() => { //首先先移除列表所有控件 this.listView.RemoveAll(); }); //如果搜索键值为空,则还原为原始状态:显示设备类型总览 if (searchKey == string.Empty) { foreach (var rowInfo in dicRowInfo.Values) { Application.RunOnMainThread(() => { if (this.Parent != null) { //添加设备的行 this.AddDeviceMenuRow(rowInfo.listDevice); } }); } } else { //搜索名字里面包含键值的设备 foreach (var rowInfo in this.dicRowInfo.Values) { if ((rowInfo.MacName != null && rowInfo.MacName.Contains(searchKey) == true) || rowInfo.DeviveTypeName.Contains(searchKey) == true) { Application.RunOnMainThread(() => { if (this.Parent != null) { //添加设备的行 this.AddDeviceMenuRow(rowInfo.listDevice); } }); } } } } } #endregion #region ■ 展开折叠___________________________ /// /// 展开或者隐藏列表 /// /// 设备明细 /// 是否展开 private void ShowDetailList(List listDevice, bool isShow) { var rowInfo = this.dicRowInfo[listDevice[0].DeviceAddr]; //它原来的高度 int oldHeight = rowInfo.frameLayout.Height; //变更的高度,默认为列表隐藏 int heightValue = ControlCommonResourse.ListViewRowHeight; if (isShow == true) { //展开模式时,扩大依据为:它有几个子控件 heightValue = (listDevice.Count + 1) * ControlCommonResourse.ListViewRowHeight; //标题自己就是一个子控件 if (rowInfo.frameLayout.ChildrenCount == 1) { new System.Threading.Thread(() => { foreach (CommonDevice info in listDevice) { Application.RunOnMainThread(() => { //加载它的列表 this.AddDeviceDetailRow(rowInfo.frameLayout, info); }); } }) { IsBackground = true }.Start(); } } //自身高度变更 rowInfo.frameLayout.Height = heightValue; } #endregion #region ■ 开启设备在线线程___________________ /// /// 开启设备在线线程 /// private void StartDeviceListControlThread() { //开启设备在线监测 this.StartCheckDeviceOnline(); //开启传感器报警监视 this.StartCheckDeviceAlarm(); } #endregion #region ■ 设备在线___________________________ /// /// 开启设备在线监测 /// private void StartCheckDeviceOnline() { //添加接受网关自动推送的事件 HdlDeviceAttributeLogic.Current.AddReceiveDeviceOnlinePushEvent("DeviceListFormReceivePushOnline", this.ReceiveDeviceStatuPush); //从设备列表中获取在线状态 new System.Threading.Thread(async () => { //首先获取这些传感器所在的网关 var listWay = new List(); var listDevice = Common.LocalDevice.Current.listAllDevice; foreach (var device in listDevice) { ZbGateway realWay = null; //是否存在真实物理网关 if (Common.LocalGateway.Current.GetRealGateway(ref realWay, device.CurrentGateWayId) == false) { continue; } listWay.Add(realWay); } foreach (var zbway in listWay) { if (this.Parent == null) { //界面关闭 return; } await Common.LocalDevice.Current.GetDeviceListFromGateway(zbway, this.ReceiveDeviceStatuPush, ShowErrorMode.NO); } }) { IsBackground = true }.Start(); } /// /// 接受设备在线推送(网关在线推送即在线) /// /// private void ReceiveDeviceStatuPush(CommonDevice device) { lock (objLock) { if (device == null || this.Parent == null) { return; } DeviceObjectViewRow row = null; if (this.dicRowInfo.ContainsKey(device.DeviceAddr) == true) { row = this.dicRowInfo[device.DeviceAddr].MenuRow; } if (row == null) { return; } //刷新设备的在线状态 string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device); var localDevice = Common.LocalDevice.Current.GetDevice(mainkeys); if (localDevice != null) { //在线状态一样的话,不需要刷新 if (localDevice.IsOnline == device.IsOnline) { return; } //保存状态 localDevice.IsOnline = device.IsOnline; localDevice.ReSave(); } Application.RunOnMainThread(() => { row?.SetOnlineStatu(device.IsOnline == 1); }); } } #endregion #region ■ 传感器报警_________________________ /// /// 开启传感器报警监视(仅供外部调用,网关在线状态确认后执行) /// private void StartCheckDeviceAlarm() { HdlDeviceAttributeLogic.Current.AddSafetyAlarmEvent("DeviceListFormSensor", this.SetAlarmInfoByInterfaceResult); } /// /// 根据接口推送,设置报警信息 /// /// /// 是否是安防设备报警 private void SetAlarmInfoByInterfaceResult(CommonDevice common, bool safetyDevice) { lock (objLock) { if (this.dicRowInfo.ContainsKey(common.DeviceAddr) == false) { return; } var row = this.dicRowInfo[common.DeviceAddr].MenuRow; if (row == null) { return; } //获取传感器报警信息的翻译文本 var msgInfo = Common.LocalSafeguard.Current.GetSensorAlarmInfo(common); if (msgInfo == null) { return; } Application.RunOnMainThread(() => { row?.StartSelectStatuThread(3000); }); } } #endregion #region ■ 合并数据___________________________ /// /// 根据MAC合并设备列表 /// private void MargeAllDeviceByMac() { lock (objLock) { this.dicRowInfo.Clear(); //获取设备列表 var listDevice = Common.LocalDevice.Current.listAllDevice; //设备排序 List listSort = Common.LocalDevice.Current.SortDeviceList(listDevice); foreach (CommonDevice device in listSort) { if (device == null || device.DeviceAddr == null) { continue; } if (this.dicRowInfo.ContainsKey(device.DeviceAddr) == false) { this.dicRowInfo[device.DeviceAddr] = new DeviceObjRowInfo(); } this.dicRowInfo[device.DeviceAddr].listDevice.Add(device); } //收集检索用的信息 foreach (var rowInfo in this.dicRowInfo.Values) { rowInfo.listDevice.Sort((CommonDevice device1, CommonDevice device2) => { if (device1.DeviceEpoint > device2.DeviceEpoint) { return 1; } return -1; }); rowInfo.MacName = Common.LocalDevice.Current.GetDeviceMacName(rowInfo.listDevice[0]); rowInfo.DeviveTypeName = Common.LocalDevice.Current.GetDeviceObjectText(rowInfo.listDevice); } } } #endregion #region ■ 一般方法___________________________ /// /// 在界面中间显示无分享设备的提示消息 /// private void ShowNotShardDeviceMsg() { //请联系管理员配置共享设备 //this.txtSearchControl.Visible = false; var btnmsg = new MsgViewControl(1000, true); btnmsg.TextID = R.MyInternationalizationString.uPleaseAdminToConfigureShardDeviceMsg; btnmsg.Gravity = Gravity.Center; bodyFrameLayout.AddChidren(btnmsg); } /// /// 打开网关列表界面 /// public void OpenGatewayManagementForm() { var form = new GatewayManage.GatewayManagementForm(); this.AddForm(form); } #endregion #region ■ 界面重新激活事件___________________ /// /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件 /// public override void FormActionAgainEvent() { //重新刷新界面 this.listView.RemoveAll(); //添加全部的设备菜单行 this.DoAddAllMenuRow(false); } #endregion #region ■ 关闭界面___________________________ /// /// 画面关闭 /// public override void CloseForm() { HdlDeviceAttributeLogic.Current.RemoveEvent("DeviceListFormSensor"); HdlDeviceAttributeLogic.Current.RemoveEvent("DeviceListFormReceivePushOnline"); base.CloseForm(); } #endregion #region ■ 结构体类___________________________ /// /// 设备行信息 /// private class DeviceObjRowInfo { /// /// 设备的MAC名字 /// public string MacName = string.Empty; /// /// 设备类型的文本信息 /// public string DeviveTypeName = string.Empty; /// /// 桌布FrameLayout /// public FrameLayout frameLayout = null; /// /// 菜单行 /// public DeviceObjectViewRow MenuRow = null; /// /// 明细行 /// public Dictionary dicDetailRow = null; /// /// 设备列表 /// public List listDevice = new List(); } #endregion } }