using System; using System.Collections.Generic; using System.Text; using ZigBee.Device; namespace Shared.Phone.UserCenter.Device { /// /// 设备管理的主界面 /// public class DeviceManagementMainForm : UserCenterCommonForm { #region ■ 变量声明___________________________ /// /// 锁 /// private object objLock = new object(); /// /// 网关控件 /// private GatewayViewRow gatewayViewRow = null; /// /// 列表控件 /// private VerticalScrolViewLayout listView = null; /// /// 搜索控件 /// //private SearchEditText txtSearchControl = null; /// /// 行控件的信息(Keys:Mac地址) /// private Dictionary dicRowInfo = new Dictionary(); /// /// 重新获取设备的在线状态 /// private bool reGetDeviceOnlineStatu = false; #endregion #region ■ 初始化_____________________________ /// /// 画面显示(底层会固定调用此方法,借以完成画面创建) /// public void ShowForm() { //设置标题信息 base.SetTitleText(Language.StringByID(R.MyInternationalizationString.DeviceManagement)); //右上添加按钮 var btnAddDeviceIcon = new TopLayoutMostRightView(); btnAddDeviceIcon.UnSelectedImagePath = "Item/Add.png"; btnAddDeviceIcon.SelectedImagePath = "Item/AddSelected.png"; topFrameLayout.AddChidren(btnAddDeviceIcon); btnAddDeviceIcon.MouseUpEventHandler += (sender, e) => { ZbGateway realWay = null; if (GatewayResourse.NowSelectGateway == null || Common.LocalGateway.Current.GetRealGateway(ref realWay, GatewayResourse.NowSelectGateway) == false) { //网关对象异常,请重新选择网关 string msg = Language.StringByID(R.MyInternationalizationString.uGatewayIsErrorAndReSelect); this.ShowErrorMsg(msg, "OpenGatewayManagementForm"); return; } GatewayResourse.NowSelectGateway = realWay; var form = new Direction.AddDeviceTypeListForm(); base.AddForm(form); }; //初始化中部控件 this.InitMiddleFrame(); } /// /// 初始化中部控件(外部可以调用) /// /// 重新获取设备的在线状态 public void InitMiddleFrame(bool i_reGetDeviceOnlineStatu = true) { this.reGetDeviceOnlineStatu = i_reGetDeviceOnlineStatu; //移除全部 bodyFrameLayout.RemoveAll(); this.gatewayViewRow = null; //初始化网关行控件 this.InitGatewayControl(); //初始化搜索控件 this.InitSearchControl(); new System.Threading.Thread(() => { //初始化设备列表控件 this.InitDeviceListControl(); }) { IsBackground = true }.Start(); } #endregion #region ■ 初始化网关行控件___________________ /// /// 初始化网关行控件 /// private void InitGatewayControl() { //网关行 GatewayResourse.NowSelectGateway = Common.LocalGateway.Current.GetLocalGateway(GatewayResourse.AppOldSelectGatewayId); this.gatewayViewRow = new GatewayViewRow(GatewayResourse.NowSelectGateway); bodyFrameLayout.AddChidren(this.gatewayViewRow); this.gatewayViewRow.InitControl(); this.gatewayViewRow.RemoveBaseClickEvent(); this.gatewayViewRow.MouseUpEvent += (sender, e) => { //如果点击的是不在线的网关,则当什么事都没有发生 if (this.gatewayViewRow.IsOnline == false) { //指定的网关不在线 this.ShowNormalMsg(Language.StringByID(R.MyInternationalizationString.uTheGatewayIsNotOnline)); return; } var form = new Gateway.GatewayInfoEditorForm(); this.AddForm(form, GatewayResourse.NowSelectGateway); }; //三个点 var btnMore = new MostRightEmptyView(); btnMore.UseClickStatu = true; btnMore.UnSelectedImagePath = "Item/More.png"; btnMore.SelectedImagePath = "Item/MoreSelected.png"; this.gatewayViewRow.AddChidren(btnMore, ChidrenBindMode.NotBind); btnMore.MouseUpEventHandler += (sender, e) => { //打开网关列表界面 this.OpenGatewayManagementForm(); }; //设置网关接受在线状态推送 this.AddGatewayOnlinePush(); } #endregion #region ■ 初始化搜索控件_____________________ /// /// 初始化搜索控件 /// private void InitSearchControl() { ////搜索 //txtSearchControl = new SearchEditText(); //bodyFrameLayout.AddChidren(txtSearchControl); //txtSearchControl.Y += this.gatewayViewRow.Bottom; ////绑定回调函数 //txtSearchControl.BindEvent(this.SetRowDataBySearchKeys); var btnTemp = new RowLayout(); btnTemp.Y = this.gatewayViewRow.Bottom; btnTemp.Height = Application.GetRealHeight(50); btnTemp.BackgroundColor = UserCenterColor.Current.TopFrameLayout; bodyFrameLayout.AddChidren(btnTemp); //列表控件 listView = new VerticalScrolViewLayout(); //listView.Y = txtSearchControl.Bottom + Application.GetRealHeight(30); //listView.Height = bodyFrameLayout.Height - txtSearchControl.Bottom - Application.GetRealHeight(50); listView.Y = btnTemp.Bottom; listView.Height = bodyFrameLayout.Height - btnTemp.Bottom; bodyFrameLayout.AddChidren(listView); } #endregion #region ■ 初始化设备列表控件_________________ /// /// 初始化设备列表控件 /// private void InitDeviceListControl() { //显示进度条 this.ShowProgressBar(); //获取设备列表 string gwID = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway); var listDevice = Common.LocalDevice.Current.GetDeviceByGatewayID(gwID); this.dicRowInfo.Clear(); //没有设备 if (listDevice.Count == 0) { Application.RunOnMainThread(() => { //关闭进度条 this.CloseProgressBar(); //在界面中间显示添加设备的提示消息 this.ShowAddDeviceMsg(); }); } else { //根据MAC合并设备列表 this.MargeAllDeviceByMac(listDevice); var listOta = new List(); //添加设备的菜单行 int count = 0; foreach (var rowInfo in this.dicRowInfo.Values) { //获取ota设备 var ota = Common.LocalDevice.Current.GetOTADevice(rowInfo.listDevice[0].DeviceAddr); if (ota != null) { listOta.Add(ota); } Application.RunOnMainThread(() => { count++; //添加设备的菜单行 this.AddDeviceMenuRow(rowInfo.listDevice); if (count == this.dicRowInfo.Count) { //开启网关在线监测的线程 this.StartGatewayOnlieCheckThread(); //检测设备新版本 this.CheckDeviceNewVersion(listOta); //关闭进度条 this.CloseProgressBar(); } }); } } } #endregion #region ■ 添加设备菜单行_____________________ /// /// 添加设备的菜单行 /// /// 设备对象 private void AddDeviceMenuRow(List listDevice) { var rowInfo = this.dicRowInfo[listDevice[0].DeviceAddr]; if (rowInfo.frameLayout != null) { //直接沿用上一次的控件(按键值搜索专用) listView.AddChidren(rowInfo.frameLayout); 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 btnNew = new InformationTipView(rowlayout.btnIcon); btnNew.Visible = false; rowlayout.AddChidren(btnNew, ChidrenBindMode.BindEventOnly); rowlayout.AddTag("btnNew", btnNew); //编辑 var btnEditor = new RowEditorButton(); rowlayout.AddRightView(btnEditor); btnEditor.MouseUpEventHandler += (sender, e) => { //打开设备信息界面 this.ShowDeviceMacInfoEditorForm(listDevice); }; rowlayout.MouseUpEvent += (sender, e) => { if (btnNew.Visible == true) { //在有新版本的情况下,单击的是图标的话,跳转真实设备信息界面 if (sender is InformationTipView || sender is RowLeftIconView) { btnNew.Visible = false; //打开设备信息界面 this.ShowDeviceMacInfoEditorForm(listDevice); return; } } //如果单击的是图标的话,跳转设备回路配置界面 var form = new ConfigureDeviceListForm(); 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 DeviceEpointInfoForm(); this.AddForm(form, device); form.ActionNameChangedEvent += (deviceName, listName) => { //变更房间 Common.Room.CurrentRoom.ChangedRoom(device, listName); //刷新全部信息信息 rowDevice.listRoom = listName; rowDevice.RefreshControlInfo(device); }; }; } #endregion #region ■ 打开设备信息界面___________________ /// /// 打开设备信息界面 /// /// private void ShowDeviceMacInfoEditorForm(List listDevice) { string macAddr = listDevice[0].DeviceAddr; //修改设备名字 var form = new DeviceMacInfoEditorForm(); form.AddForm(form, this.dicRowInfo[macAddr].listDevice); form.ActionFormClose += (deviceName) => { if (string.IsNullOrEmpty(deviceName) == false) { this.dicRowInfo[macAddr].MenuRow.btnDeviceName.Text = deviceName; } }; } #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(() => { //添加设备的行 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(() => { //添加设备的行 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 StartGatewayOnlieCheckThread() { if (GatewayResourse.NowSelectGateway == null) { return; } string selectGwId = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway); new System.Threading.Thread(() => { ZbGateway zbGateway = Common.LocalGateway.Current.GetLocalGateway(selectGwId); if (zbGateway == null) { return; } //刷新网关在线状态 Common.LocalGateway.Current.RefreshGatewayOnlineStatu(new List() { zbGateway }); Application.RunOnMainThread(() => { if (this.gatewayViewRow != null && Common.LocalGateway.Current.GetGatewayId(this.gatewayViewRow.zbGateway) == selectGwId) { this.gatewayViewRow.zbGateway = zbGateway; bool online = Common.LocalGateway.Current.CheckGatewayOnlineByFlag(zbGateway); this.gatewayViewRow.RefreshControl(online); //开启设备在线线程 this.StartDeviceListControlThread(online); } }); }) { IsBackground = true }.Start(); } /// /// 网关在线状态变更 /// /// 网关对象 /// 在线状态变更后的状态 public override void GatewayOnlinePush(ZbGateway gateWay, bool online) { if (Common.LocalGateway.Current.GetGatewayId(gateWay) == Common.LocalGateway.Current.GetGatewayId(this.gatewayViewRow.zbGateway)) { Application.RunOnMainThread(() => { this.gatewayViewRow.zbGateway = gateWay; this.gatewayViewRow.RefreshControl(online); }); } } #endregion #region ■ 开启设备在线线程___________________ /// /// 开启设备在线线程 /// /// 网关的在线状态 private void StartDeviceListControlThread(bool gatewayOnline) { if (gatewayOnline == false) { //设置全部设备离线 this.SetAllDeviceOffLine(); return; } //开启传感器报警监视 this.StartCheckDeviceAlarm(); //开启设备在线监测 this.StartCheckDeviceOnline(); } #endregion #region ■ 设备在线___________________________ /// /// 开启设备在线监测 /// private void StartCheckDeviceOnline() { lock (objLock) { //添加接受网关自动推送的事件 DeviceAttributeLogic.Current.AddReceiveDeviceOnlinePushEvent("DeviceListFormReceivePushOnline", this.ReceiveDeviceStatuPush); } //外部调用的话,不再重新获取设备状态 if (GatewayResourse.NowSelectGateway == null || this.reGetDeviceOnlineStatu == false) { return; } string gwId = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway); new System.Threading.Thread(async () => { //这里主要只是获取在线状态 var zbway = Common.LocalGateway.Current.GetLocalGateway(gwId); var result = await Common.LocalDevice.Current.GetDeviceListFromGateway(zbway, this.ReceiveDeviceStatuPush); }) { 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); }); } } /// /// 设置全部设备离线 /// private void SetAllDeviceOffLine() { lock (objLock) { foreach (var rowInfo in this.dicRowInfo.Values) { Application.RunOnMainThread(() => { rowInfo.MenuRow.SetOnlineStatu(false); }); } } } #endregion #region ■ 设备新版本_________________________ /// /// 检测设备新版本 /// /// ota设备 private void CheckDeviceNewVersion(List list) { new System.Threading.Thread(async () => { await System.Threading.Tasks.Task.Delay(2000); foreach (var ota in list) { if (this.Parent == null) { return; } //添加升级固件信息(成不成功都无所谓) var result = await FirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.ZigbeeDevice, ota.HwVersion.ToString(), ota.ImgTypeId.ToString()); //获取设备最新版本 var deviceFirmware = FirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.ZigbeeDevice, ota.HwVersion.ToString(), ota.ImgTypeId.ToString(), ota.ImgVersion); if (deviceFirmware == null) { continue; } //拥有新版本 Application.RunOnMainThread(() => { if (this.dicRowInfo.ContainsKey(ota.DeviceAddr) == true) { var row = this.dicRowInfo[ota.DeviceAddr].MenuRow; if (row != null) { var btnNew = (InformationTipView)row.GetTagByKey("btnNew"); btnNew.Visible = true; } } }); } }) { IsBackground = true }.Start(); } #endregion #region ■ 传感器报警_________________________ /// /// 开启传感器报警监视(仅供外部调用,网关在线状态确认后执行) /// private void StartCheckDeviceAlarm() { DeviceAttributeLogic.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 ■ 实现外部调用_______________________ /// /// 添加新的设备到界面桌布中 /// /// List device. public void AddDeviceToFormTable(List listDevice) { if (listDevice.Count == 0) { return; } //if (listView.ChildrenCount == 0) //{ // this.txtSearchControl.Visible = true; //} string macKey = listDevice[0].DeviceAddr; //新建一个对象 if (this.dicRowInfo.ContainsKey(macKey) == false) { var rowNewInfo = new DeviceObjRowInfo(); rowNewInfo.listDevice = listDevice; rowNewInfo.MacName = Common.LocalDevice.Current.GetDeviceMacName(listDevice[0]); rowNewInfo.DeviveTypeName = Common.LocalDevice.Current.GetDeviceObjectText(listDevice); this.dicRowInfo[macKey] = rowNewInfo; //创建新的行 this.AddDeviceMenuRow(listDevice); return; } //如果存在的话 var rowInfo = this.dicRowInfo[macKey]; if (rowInfo.dicDetailRow == null) { //这个东西还没有展开过 return; } foreach (var device in listDevice) { string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(device); if (rowInfo.dicDetailRow.ContainsKey(mainkey) == false) { //这种情况应该不会出现 continue; } var row = rowInfo.dicDetailRow[mainkey]; //刷新控件信息 row.RefreshControlInfo(device); } } /// /// 变更网关名字(仅供外部调用) /// /// public void ChangedGatewayName(ZbGateway zbGateway) { if (Common.LocalGateway.Current.GetGatewayId(zbGateway) == Common.LocalGateway.Current.GetGatewayId(this.gatewayViewRow.zbGateway)) { this.gatewayViewRow.btnName.Text = Common.LocalGateway.Current.GetGatewayName(zbGateway); this.gatewayViewRow.zbGateway = zbGateway; } } /// /// 移除指定设备(仅供外部调用) /// /// Listdevice. public void RemoveDeviceFromMemory(List listdevice) { //缓存中删除设备 string macAddr = listdevice[0].DeviceAddr; DeviceObjRowInfo rowInfo = null; if (macAddr == null || this.dicRowInfo.ContainsKey(macAddr) == false) { return; } rowInfo = this.dicRowInfo[macAddr]; Application.RunOnMainThread(() => { //移除 this.dicRowInfo.Remove(macAddr); rowInfo.frameLayout.RemoveFromParent(); if (listView.ChildrenCount == 0) { //在界面中间显示添加设备的提示消息 this.ShowAddDeviceMsg(); } }); } #endregion #region ■ 合并数据___________________________ /// /// 根据MAC合并设备列表 /// /// private void MargeAllDeviceByMac(List listDevice) { //设备排序 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.MacName = Common.LocalDevice.Current.GetDeviceMacName(rowInfo.listDevice[0]); rowInfo.DeviveTypeName = Common.LocalDevice.Current.GetDeviceObjectText(rowInfo.listDevice); } } #endregion #region ■ 一般方法___________________________ /// /// 在界面中间显示添加设备的提示消息 /// private void ShowAddDeviceMsg() { //如果没有设备,则显示一个白板 //this.txtSearchControl.Visible = false; var btnmsg = new MsgViewControl(1000, true); btnmsg.TextID = R.MyInternationalizationString.uClickTopRightToAdd; btnmsg.Gravity = Gravity.Center; bodyFrameLayout.AddChidren(btnmsg); } /// /// 打开网关列表界面 /// public void OpenGatewayManagementForm() { var form = new Gateway.GatewayManagementForm(); this.AddForm(form); } #endregion #region ■ 关闭界面___________________________ /// /// 画面关闭 /// /// 是否关闭界面,false的时候,只会调用关闭函数里面的附加功能 public override void CloseForm(bool isCloseForm = true) { DeviceAttributeLogic.Current.RemoveEvent("DeviceListFormSensor"); DeviceAttributeLogic.Current.RemoveEvent("DeviceListFormReceivePushOnline"); GatewayResourse.NowSelectGateway = null; base.CloseForm(isCloseForm); } #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 } }