黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs
@@ -15,15 +15,15 @@
        /// <summary>
        /// 新上报的设备
        /// </summary>
        private Dictionary<string, List<CommonDevice>> dicNewDevice = new Dictionary<string, List<CommonDevice>>();
        private List<CommonDevice> listNewDevice = new List<CommonDevice>();
        /// <summary>
        /// 显示设备线程是否已经开启
        /// </summary>
        private bool isDeviceThreadStart = false;
        /// <summary>
        /// 等待设备的回馈的超时时间(单位:秒)
        /// 等待设备的回馈的超时时间(单位:百毫秒)
        /// </summary>
        private int waitDeviceTimeOut = 3;
        private int waitDeviceTimeOut = 20;
        /// <summary>
        /// 主题超时的线程是否开启
        /// </summary>
@@ -49,17 +49,13 @@
        /// </summary>
        private ZbGateway realGateway = null;
        /// <summary>
        /// 进度条
        /// 进度条控件
        /// </summary>
        private FrameLayout btnProgressBar = null;
        private ProgressRowBar btnProgressBar = null;
        /// <summary>
        /// 进度值显示文本的整体
        /// 网关是否允许入网的标识
        /// </summary>
        private FrameLayout frameProgress = null;
        /// <summary>
        /// 进度值的显示文本
        /// </summary>
        private NormalViewControl btnProgressView = null;
        private bool gatewayCanAddDevice = false;
        #endregion
@@ -76,7 +72,7 @@
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice));
            this.gatewayId = HdlGatewayLogic.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
            this.gatewayId = HdlGatewayResourse.NowSelectGatewayId;
            HdlGatewayLogic.Current.GetRealGateway(ref this.realGateway, this.gatewayId);
            //初始化中部控件
@@ -92,11 +88,13 @@
            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 framePic = new FrameLayout();
            framePic.Width = this.GetPictrueRealSize(878);
            framePic.Height = this.GetPictrueRealSize(478);
            framePic.Y = Application.GetRealHeight(251);
            framePic.Gravity = Gravity.CenterHorizontal;
            framePic.BackgroundImagePath = "Instruct/DeviceSearch.png";
            bodyFrameLayout.AddChidren(framePic);
            //正在搜索设备,请稍候…
            var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(58), false);
@@ -107,56 +105,22 @@
            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.BackgroundColor = 0xfffb744a;
            btnProgressBar.Radius = (uint)Application.GetRealHeight(29) / 2;
            btnProRow.AddChidren(btnProgressBar);
            this.btnProgressBar = new ProgressRowBar(559, 29);
            btnProgressBar.ProgressBarGoback = false;
            btnProgressBar.Gravity = Gravity.CenterHorizontal;
            btnProgressBar.Y = Application.GetRealHeight(861);
            bodyFrameLayout.AddChidren(btnProgressBar);
            btnProgressBar.StartMode1(true);
            //进度值文本
            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(() =>
            if (this.realGateway != null)
            {
                //蓝才刚说有时候网关会收不到入网的命令,所以发三次
                for (int i = 0; i < 3; i++)
                {
                    if (this.Parent == null)
                    {
                        return;
                    }
                    //让网关允许入网
                    this.realGateway.AddNewDeviceToGateway(180);
                    System.Threading.Thread.Sleep(1000);
                }
            });
            //添加监视设备新上报的事件
            this.realGateway.GwResDataAction += this.AdjustGatewayResultData;
                //允许设备入网
                this.StartDeviceCanAddToGateway(false);
                //添加监视设备新上报的事件
                this.realGateway.GwResDataAction += this.AdjustGatewayResultData;
            }
            //开启连接的假想动画效果线程
            this.StartConcetionAnimeteThread();
            this.StartConcetionAnimeteThread(framePic);
        }
        #endregion
@@ -177,7 +141,7 @@
                //停止接收
                this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
                Application.RunOnMainThread(() =>
                HdlThreadLogic.Current.RunMain(() =>
                {
                    this.CloseForm();
                });
@@ -190,56 +154,40 @@
                this.StartTopicTimeOutThread();
                return;
            }
            //中断主题超时线程
            this.topTimeOut = -100;
            lock (this.dicNewDevice)
            var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
            CommonDevice.DeviceInfoData info = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceInfoData>(jobject["Data"].ToString());
            if (info.DriveCode != 0)
            {
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                CommonDevice.DeviceInfoData info = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceInfoData>(jobject["Data"].ToString());
                if (info.DriveCode != 0)
                {
                    //不需要虚拟设备
                    return;
                }
                //根据设备Type创建对应的设备对象
                var device = Common.LocalDevice.Current.NewDeviceObjectByDeviceId((DeviceType)jobject.Value<int>("Device_ID"));
                if (device == null)
                {
                    return;
                }
                device.DeviceInfo = info;
                //给新设备设置主键属性
                Common.LocalDevice.Current.SetNewDeviceMainKeys(device, jobject);
                device.CurrentGateWayId = HdlGatewayLogic.Current.GetGatewayId(this.realGateway);
                //将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<CommonDevice>();
                }
                //刷新超时时间
                this.waitDeviceTimeOut = 3;
                //获取设备的固定属性
                HdlDeviceFixedAttributeLogic.Current.SetAllFixedAttributeToDevice(device);
                if ((device is OTADevice) == false)
                {
                    //不需要200端点的那个设备
                    this.dicNewDevice[device.DeviceAddr].Add(device);
                    //有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
                    this.StartShowDeviceAddSuccessFormThread();
                }
                //不需要虚拟设备
                return;
            }
            //根据设备Type创建对应的设备对象
            var device = HdlDeviceCommonLogic.Current.NewDeviceObjectByDeviceId((DeviceType)jobject.Value<int>("Device_ID"), jobject.Value<int>("Epoint"));
            if (device == null)
            {
                return;
            }
            device.DeviceInfo = info;
            //给新设备设置主键属性
            HdlDeviceCommonLogic.Current.SetNewDeviceMainKeys(device, jobject);
            device.CurrentGateWayId = this.realGateway.GwId;
            //将DeviceInfo的属性设置到主属性中
            HdlDeviceCommonLogic.Current.SetDeviceInfoToMain(device, device);
            //添加设备的缓存
            device.IsOnline = 1;
            HdlDeviceCommonLogic.Current.AddDeviceToMemory(ref device);
            //刷新超时时间
            this.waitDeviceTimeOut = 20;
            //不需要200端点的那个设备  2020.01.13 变更:ota也加进来
            this.listNewDevice.Add(device);
            //有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
            this.StartShowDeviceAddSuccessFormThread();
        }
        #endregion
@@ -263,28 +211,48 @@
                while (this.waitDeviceTimeOut >= 0)
                {
                    //等待下一个回路
                    System.Threading.Thread.Sleep(1000);
                    System.Threading.Thread.Sleep(100);
                    this.waitDeviceTimeOut--;
                }
                //停止接收
                this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
                System.Threading.Thread.Sleep(500);
                System.Threading.Thread.Sleep(200);
                var listDevice = new List<CommonDevice>();
                for (int i = 0; i < this.listNewDevice.Count; i++)
                {
                    var device = this.listNewDevice[i];
                    if (device.DeviceAddr != this.listNewDevice[0].DeviceAddr)
                    {
                        continue;
                    }
                    listDevice.Add(device);
                    //重新变更UI
                    if (device is OTADevice)
                    {
                        continue;
                    }
                    device.IconPath = string.Empty;
                    device.ReSave();
                    //设置设备功能类型 (不能在接收回路的地方写入,不然网关可能会超负荷)
                    HdlDeviceCommonLogic.Current.RefreshDeviceFunctionType(device, device, true);
                    System.Threading.Thread.Sleep(200);
                    //获取设备的固定属性
                    if (HdlDeviceAttributeLogic.Current.ReadDeviceAllFixedAttribute(device) == true)
                    {
                        System.Threading.Thread.Sleep(200);
                    }
                }
                //读取设备的其他信息
                this.ReadDeviceOtherInfo(listDevice);
                //目前就弄一个
                Application.RunOnMainThread(() =>
                HdlThreadLogic.Current.RunMain(() =>
                {
                    foreach (var listDevice in this.dicNewDevice.Values)
                    {
                        //重新变更UI
                        foreach (var device in listDevice)
                        {
                            device.IconPath = string.Empty;
                            device.ReSave();
                        }
                        //显示设备信息画面
                        this.ShowDeviceAddSuccessForm(listDevice);
                        break;
                    }
                    //显示设备信息画面
                    this.ShowDeviceAddSuccessForm(listDevice);
                });
            });
        }
@@ -304,7 +272,7 @@
            if (this.targetFormId != string.Empty)
            {
                //再关闭设备入网指导界面
                this.CloseFormByFormName(this.targetFormId);
                HdlFormLogic.Current.CloseFormByFormName(this.targetFormId);
            }
            //添加设备
@@ -319,23 +287,23 @@
        /// <summary>
        /// 开启连接的假想动画效果线程
        /// </summary>
        private void StartConcetionAnimeteThread()
        private void StartConcetionAnimeteThread(FrameLayout framePic)
        {
            int iconSize = Application.GetMinRealAverage(23);
            int iconSize = this.GetPictrueRealSize(23);
            var listPoint = new List<int>();
            for (int i = 0; i < 9; i++)
            {
                //X轴+Index*(图标大小+间距)
                listPoint.Add(Application.GetMinRealAverage(394) + i * (iconSize + Application.GetMinRealAverage(10)));
                listPoint.Add(this.GetPictrueRealSize(300) + i * (iconSize + this.GetPictrueRealSize(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);
            btnRound.Y = this.GetPictrueRealSize(225);
            framePic.AddChidren(btnRound);
            HdlThreadLogic.Current.RunThread(() =>
            {
@@ -344,14 +312,20 @@
                while (this.Parent != null)
                {
                    System.Threading.Thread.Sleep(500);
                    timeCount++;
                    if (timeCount >= 240)
                    //网关允许设备入网后才开始计时
                    if (this.gatewayCanAddDevice == true)
                    {
                        //120秒后,再次发送网关设备入网命令
                        this.realGateway.AddNewDeviceToGateway(180);
                        timeCount = 0;
                        timeCount++;
                        if (timeCount >= 360)
                        {
                            this.gatewayCanAddDevice = false;
                            //再次发送允许设备入网
                            this.StartDeviceCanAddToGateway(true);
                            timeCount = 0;
                        }
                    }
                    Application.RunOnMainThread(() =>
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        if (btnRound != null)
                        {
@@ -379,15 +353,34 @@
        /// <returns></returns>
        private int CheckIsDeviceComming(string topic, string resultData)
        {
            if (topic == gatewayId + "/Device/DeviceAnnounce_Respon")
            if (topic == gatewayId + "/Device/SearchNewDevice")
            {
                //网关回复设备已经可以入网
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchNewDeviceResult>(jobject["Data"].ToString());
                if (info.time > 0)
                {
                    this.gatewayCanAddDevice = true;
                }
                return 0;
            }
            else if (topic == gatewayId + "/Device/DeviceAnnounce_Respon")
            {
                this.topTimeOut = topMaxTime;
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceAnnounceInfo>(jobject["Data"].ToString());
                if (info.IsNew == 6 || info.IsNew == 7)
                {
                    return 0;
                }
                //网关告知客户端有设备声明
                this.SetDeviceProgressValue(1);
                this.btnProgressBar.SetValue(1, 6);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceGetActiveEP_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端获取设备活动端点信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -398,13 +391,13 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(2);
                this.btnProgressBar.SetValue(2, 6);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceGetActiveEPSimpleDesc_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端获取设备所有活动端点简单描述符信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -415,30 +408,30 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(3);
                this.btnProgressBar.SetValue(3, 6);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceGetDefaultBind_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端获取设备默认绑定表信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
                if (info.Result != 0)
                {
                    //出现未知错误,请重新入网
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
                    //HdlLogLogic.Current.WriteLog(-1, resultData);
                    //this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(4);
                this.btnProgressBar.SetValue(4, 6);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceAutoBindZBCoord_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端设备自动绑定协调器信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -449,43 +442,27 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(5);
                this.btnProgressBar.SetValue(5, 6);
                return 2;
            }
            else if (topic == gatewayId + "/DeviceInComingRespon")
            {
                this.topTimeOut = topMaxTime;
                //设备入网时,有时候网关会上报虚拟设备
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                CommonDevice.DeviceInfoData info = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceInfoData>(jobject["Data"].ToString());
                if (info.DriveCode != 0)
                {
                    //不需要虚拟设备
                    return 0;
                }
                //网关最终上报节点设备信息
                this.SetDeviceProgressValue(6);
                this.btnProgressBar.SetValue(6, 6);
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 设置设备的进度值
        /// </summary>
        /// <param name="value"></param>
        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;
            });
        }
        /// <summary>
@@ -506,15 +483,42 @@
                    System.Threading.Thread.Sleep(1000);
                    this.topTimeOut--;
                }
                if (this.topTimeOut < 0 && this.topTimeOut >= -10)
                if (this.topTimeOut < 0)
                {
                    //响应超时,请重新入网
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndReAccessNetwork));
                    Application.RunOnMainThread(() =>
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //关闭界面
                        this.CloseForm();
                    });
                }
            });
        }
        #endregion
        #region ■ 允许设备入网_______________________
        /// <summary>
        /// 允许设备入网
        /// </summary>
        /// <param name="close"></param>
        private void StartDeviceCanAddToGateway(bool close)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                if (close == true)
                {
                    //关闭入网模式
                    this.realGateway.AddNewDeviceToGateway(0);
                    System.Threading.Thread.Sleep(2000);
                }
                while (gatewayCanAddDevice == false && this.Parent != null)
                {
                    //让网关允许入网
                    this.realGateway.AddNewDeviceToGateway(180);
                    System.Threading.Thread.Sleep(6000);
                }
            });
        }
@@ -528,16 +532,48 @@
        /// </summary>
        public override void CloseFormBefore()
        {
            //停止接收
            this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
            HdlThreadLogic.Current.RunThread(() =>
            if (this.realGateway != null)
            {
                System.Threading.Thread.Sleep(1200);
                //关闭入网模式
                this.realGateway.AddNewDeviceToGateway(0);
            });
                //停止接收
                this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
                HdlThreadLogic.Current.RunThread(() =>
                {
                    System.Threading.Thread.Sleep(1200);
                    //关闭入网模式
                    this.realGateway.AddNewDeviceToGateway(0);
                });
            }
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 读取设备的其他信息
        /// </summary>
        /// <param name="listDevice"></param>
        private void ReadDeviceOtherInfo(List<CommonDevice> listDevice)
        {
            var myTypeInfo = HdlDeviceCommonLogic.Current.GetMyDeviceEnumInfo(listDevice);
            if (myTypeInfo.BeloneType == DeviceBeloneType.A窗帘 && listDevice[0] is Rollershade)
            {
                HdlThreadLogic.Current.RunThread(async () =>
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        //重置窗帘
                        var result = await HdlDeviceCurtainLogic.Current.RestoreCurtain((Rollershade)listDevice[0], i != 3 ? ShowErrorMode.NO : ShowErrorMode.YES);
                        if (result == true)
                        {
                            return;
                        }
                        await System.Threading.Tasks.Task.Delay(1000);
                    }
                });
            }
        }
        #endregion
@@ -555,6 +591,28 @@
            public int Result = -1;
        }
        /// <summary>
        /// 网关告知客户端有设备声明
        /// </summary>
        private class DeviceAnnounceInfo
        {
            /// <summary>
            /// 蓝才刚说 6和7 这两个是设备重启上报的,目前暂时不能显示到入网步骤那里的
            /// </summary>
            public int IsNew = -1;
        }
        /// <summary>
        /// 网关回复允许设备入网
        /// </summary>
        private class SearchNewDeviceResult
        {
            /// <summary>
            /// 允许设备入网的时间
            /// </summary>
            public int time = 0;
        }
        #endregion
    }
}