using System; using System.Collections.Generic; using System.Text; using ZigBee.Device; namespace Shared.Phone.UserCenter.Device { /// /// 搜索设备的界面 /// public class DeviceSearchForm : EditorCommonForm { #region ■ 变量声明___________________________ /// /// 新上报的设备 /// private Dictionary> dicNewDevice = new Dictionary>(); /// /// 显示设备线程是否已经开启 /// private bool isDeviceThreadStart = false; /// /// 等待设备的回馈的超时时间(单位:秒) /// private int waitDeviceTimeOut = 3; /// /// 主题超时的线程是否开启 /// private bool isTopicTimeOutThreadStart = false; /// /// 主题间的超时时间 -100:中断线程(单位:秒) /// private int topTimeOut = 120; /// /// 超时最大时间 /// private int topMaxTime = 120; /// /// 画面ID,标记它由哪个界面调用并打开的 /// private string targetFormId = string.Empty; /// /// 网关ID /// private string gatewayId = string.Empty; /// /// 进度条 /// private FrameLayout btnProgressBar = null; /// /// 进度值显示文本的整体 /// private FrameLayout frameProgress = null; /// /// 进度值的显示文本 /// private NormalViewControl btnProgressView = null; #endregion #region ■ 初始化_____________________________ /// /// 画面显示(底层会固定调用此方法,借以完成画面创建) /// /// 画面ID,标记它由哪个界面调用并打开的 public void ShowForm(string i_formId) { this.targetFormId = i_formId; //设置标题信息 base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice)); this.gatewayId = HdlGatewayLogic.Current.GetGatewayId(GatewayResourse.NowSelectGateway); //初始化中部控件 this.InitMiddleFrame(); } /// /// 初始化中部控件 /// private void InitMiddleFrame() { //清空bodyFrame this.ClearBodyFrame(); //图片 var btnPic = new PicViewControl(878, 478); btnPic.Y = Application.GetMinRealAverage(251); btnPic.Gravity = Gravity.CenterHorizontal; btnPic.UnSelectedImagePath = "Instruct/DeviceSearch.png"; bodyFrameLayout.AddChidren(btnPic); //正在搜索设备,请稍候… var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(58), false); btnSearch.TextAlignment = TextAlignment.Center; btnSearch.TextID = R.MyInternationalizationString.uDeviceSearching; btnSearch.Y = Application.GetRealHeight(1037); btnSearch.TextColor = UserCenterColor.Current.TextGrayColor3; bodyFrameLayout.AddChidren(btnSearch); //进度条 var btnProRow = new FrameLayout(); btnProRow.Gravity = Gravity.CenterHorizontal; btnProRow.Y = Application.GetRealHeight(861); btnProRow.Width = Application.GetRealWidth(559); btnProRow.Height = Application.GetRealHeight(29); btnProRow.BackgroundColor = 0xffe6e6e6; btnProRow.Radius = (uint)Application.GetRealHeight(29) / 2; bodyFrameLayout.AddChidren(btnProRow); this.btnProgressBar = new FrameLayout(); btnProgressBar.Width = 0; btnProgressBar.Height = btnProRow.Height; btnProgressBar.Radius = btnProRow.Radius; btnProgressBar.BackgroundColor = 0xfffb744a; btnProgressBar.Radius = (uint)Application.GetRealHeight(29) / 2; btnProRow.AddChidren(btnProgressBar); //进度值文本 this.frameProgress = new FrameLayout(); frameProgress.Width = Application.GetRealWidth(84); frameProgress.Height = Application.GetRealHeight(60); frameProgress.Y = Application.GetRealHeight(772); bodyFrameLayout.AddChidren(frameProgress); frameProgress.X = btnProRow.X + btnProgressBar.Right - frameProgress.Width / 2; var btnProgressPic = new PicViewControl(84, 60); btnProgressPic.UnSelectedImagePath = "Item/ProgressMsg.png"; frameProgress.AddChidren(btnProgressPic); this.btnProgressView = new NormalViewControl(84, 32, true); btnProgressView.TextSize = 10; btnProgressView.TextAlignment = TextAlignment.Center; btnProgressView.Text = "0%"; frameProgress.AddChidren(btnProgressView); HdlThreadLogic.Current.RunThread(() => { //蓝才刚说有时候网关会收不到入网的命令,所以发三次 for (int i = 0; i < 3; i++) { if (this.Parent == null) { return; } //让网关允许入网 GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(255); System.Threading.Thread.Sleep(1000); } }); //添加监视设备新上报的事件 GatewayResourse.NowSelectGateway.GwResDataAction += this.AdjustGatewayResultData; } #endregion #region ■ 新设备入网接收_____________________ /// /// 处理网关主题上报 /// /// 主题 /// 上报数据 private void AdjustGatewayResultData(string topic, string resultData) { //检测主题(-1:异常 0:不是相关的主题 1:设备最后的主题上报 2:设备最终上报之前的主题) var result = this.CheckIsDeviceComming(topic, resultData); if (result == -1) { //停止接收 GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData; Application.RunOnMainThread(() => { this.CloseForm(); }); return; } else if (result == 0) { return; } else if (result == 2) { //开启主题间隔超时线程 this.StartTopicTimeOutThread(); return; } //中断主题超时线程 this.topTimeOut = -100; lock (this.dicNewDevice) { var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData); CommonDevice.DeviceInfoData info = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (info.DriveCode != 0) { //不需要虚拟设备 return; } //根据设备Type创建对应的设备对象 var device = Common.LocalDevice.Current.NewDeviceObjectByDeviceId((DeviceType)jobject.Value("Device_ID")); if (device == null) { return; } device.DeviceInfo = info; //给新设备设置主键属性 Common.LocalDevice.Current.SetNewDeviceMainKeys(device, jobject); device.CurrentGateWayId = HdlGatewayLogic.Current.GetGatewayId(GatewayResourse.NowSelectGateway); //将DeviceInfo的属性设置到主属性中 Common.LocalDevice.Current.SetDeviceInfoToMain(device, device); //添加设备的缓存 device.IsOnline = 1; Common.LocalDevice.Current.AddDeviceToMemory(ref device); if (this.dicNewDevice.ContainsKey(device.DeviceAddr) == false) { this.dicNewDevice[device.DeviceAddr] = new List(); } //刷新超时时间 this.waitDeviceTimeOut = 3; //获取设备的固定属性 HdlDeviceFixedAttributeLogic.Current.SetAllFixedAttributeToDevice(device); if ((device is OTADevice) == false) { //不需要200端点的那个设备 this.dicNewDevice[device.DeviceAddr].Add(device); //有新设备,开启显示设备信息界面的线程(里面会等待三秒这样) this.StartShowDeviceAddSuccessFormThread(); } } } #endregion #region ■ 显示设备信息画面___________________ /// /// 开启显示设备信息界面的线程 /// private void StartShowDeviceAddSuccessFormThread() { if (this.isDeviceThreadStart == true) { //线程已经开启 return; } this.isDeviceThreadStart = true; HdlThreadLogic.Current.RunThread(() => { while (this.waitDeviceTimeOut >= 0) { //等待下一个回路 System.Threading.Thread.Sleep(1000); this.waitDeviceTimeOut--; } //停止接收 GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData; System.Threading.Thread.Sleep(500); //目前就弄一个 Application.RunOnMainThread(() => { foreach (var listDevice in this.dicNewDevice.Values) { //重新变更UI foreach (var device in listDevice) { device.IconPath = string.Empty; device.ReSave(); } //显示设备信息画面 this.ShowDeviceAddSuccessForm(listDevice); break; } }); }); } /// /// 显示设备信息画面 /// /// 新设备列表 private void ShowDeviceAddSuccessForm(List listMacDevice) { if (this.Parent == null) { return; } //关闭自身 this.CloseForm(); if (this.targetFormId != string.Empty) { //再关闭设备入网指导界面 this.CloseFormByFormName(this.targetFormId); } //添加设备 var form = new DeviceAddSuccessForm(); form.AddForm(listMacDevice[0].DeviceAddr); } #endregion #region ■ 假想动画___________________________ /// /// 开启连接的假想动画效果线程 /// private void StartConcetionAnimeteThread() { int iconSize = Application.GetMinRealAverage(23); var listPoint = new List(); for (int i = 0; i < 9; i++) { //X轴+Index*(图标大小+间距) listPoint.Add(Application.GetMinRealAverage(394) + i * (iconSize + Application.GetMinRealAverage(10))); } var btnRound = new PicViewControl(iconSize, iconSize, false); btnRound.Radius = (uint)iconSize / 2; btnRound.BackgroundColor = UserCenterColor.Current.ConcetionRoundColor; btnRound.X = listPoint[0]; btnRound.Y = Application.GetMinRealAverage(475); bodyFrameLayout.AddChidren(btnRound); HdlThreadLogic.Current.RunThread(() => { int index = 1; while (this.Parent != null) { System.Threading.Thread.Sleep(500); Application.RunOnMainThread(() => { if (btnRound != null) { btnRound.X = listPoint[index]; index++; if (index == listPoint.Count) { index = 0; } } }); } }); } #endregion #region ■ 检测主题___________________________ /// /// 检测主题(-1:异常 0:不是相关的主题 1:设备最后的主题上报 2:设备最终上报之前的主题) /// /// /// /// private int CheckIsDeviceComming(string topic, string resultData) { if (topic == gatewayId + "/Device/DeviceAnnounce_Respon") { this.topTimeOut = topMaxTime; //网关告知客户端有设备声明 this.SetDeviceProgressValue(1); return 2; } else if (topic == gatewayId + "/Device/DeviceGetActiveEP_Respon") { //网关告知客户端获取设备活动端点信息 var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData); var info = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (info.Result != 0) { //出现未知错误,请重新入网 //this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork)); HdlLogLogic.Current.WriteLog(-1, resultData); return 2; } this.topTimeOut = topMaxTime; //设置进度值 this.SetDeviceProgressValue(2); return 2; } else if (topic == gatewayId + "/Device/DeviceGetActiveEPSimpleDesc_Respon") { //网关告知客户端获取设备所有活动端点简单描述符信息 var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData); var info = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (info.Result != 0) { //出现未知错误,请重新入网 //this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork)); HdlLogLogic.Current.WriteLog(-1, resultData); return 2; } this.topTimeOut = topMaxTime; //设置进度值 this.SetDeviceProgressValue(3); return 2; } else if (topic == gatewayId + "/Device/DeviceGetDefaultBind_Respon") { //网关告知客户端获取设备默认绑定表信息 var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData); var info = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (info.Result != 0) { //出现未知错误,请重新入网 this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork)); //HdlLogLogic.Current.WriteLog(-1, resultData); return 2; } this.topTimeOut = topMaxTime; //设置进度值 this.SetDeviceProgressValue(4); return 2; } else if (topic == gatewayId + "/Device/DeviceAutoBindZBCoord_Respon") { //网关告知客户端设备自动绑定协调器信息 var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData); var info = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (info.Result != 0) { //出现未知错误,请重新入网 //this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork)); HdlLogLogic.Current.WriteLog(-1, resultData); return 2; } this.topTimeOut = topMaxTime; //设置进度值 this.SetDeviceProgressValue(5); return 2; } else if (topic == gatewayId + "/DeviceInComingRespon") { //网关最终上报节点设备信息 this.SetDeviceProgressValue(6); return 1; } return 0; } /// /// 设置设备的进度值 /// /// private void SetDeviceProgressValue(decimal value) { Application.RunOnMainThread(() => { //进度条 decimal result = value / 6; int width = (int)(result * Application.GetRealWidth(559)); if (btnProgressBar.Width >= width) { //有些设备会上报两次,这里不能让它的进度条往回走 return; } btnProgressBar.Width = width; //文本显示 btnProgressView.Text = ((int)(result * 100)) + "%"; //文本显示的那个图片框移动 frameProgress.X = Application.GetRealWidth(262) + btnProgressBar.Right - frameProgress.Width / 2; }); } /// /// 开启主题间的超时线程 /// private void StartTopicTimeOutThread() { if (this.isTopicTimeOutThreadStart == true) { return; } this.isTopicTimeOutThreadStart = true; HdlThreadLogic.Current.RunThread(() => { while (this.Parent != null && this.topTimeOut >= 0) { //等待下一个主题 System.Threading.Thread.Sleep(1000); this.topTimeOut--; } if (this.topTimeOut < 0 && this.topTimeOut >= -10) { //响应超时,请重新入网 this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndReAccessNetwork)); Application.RunOnMainThread(() => { //关闭界面 this.CloseForm(); }); } }); } #endregion #region ■ 画面关闭___________________________ /// /// 画面关闭 /// public override void CloseForm() { base.CloseForm(); //停止接收 GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData; HdlThreadLogic.Current.RunThread(() => { System.Threading.Thread.Sleep(1200); //关闭入网模式 GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(0); }); } #endregion #region ■ 结构体_____________________________ /// /// 网关推送的设备结果信息 /// private class DeviceResultInfo { /// /// 结果(0:成功,以外都是失败) /// public int Result = -1; } #endregion } }