using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter.Device
|
{
|
/// <summary>
|
/// 搜索设备的界面
|
/// </summary>
|
public class DeviceSearchForm : EditorCommonForm
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 新上报的设备
|
/// </summary>
|
private List<CommonDevice> listNewDevice = new List<CommonDevice>();
|
/// <summary>
|
/// 显示设备线程是否已经开启
|
/// </summary>
|
private bool isDeviceThreadStart = false;
|
/// <summary>
|
/// 等待设备的回馈的超时时间(单位:百毫秒)
|
/// </summary>
|
private int waitDeviceTimeOut = 20;
|
/// <summary>
|
/// 主题超时的线程是否开启
|
/// </summary>
|
private bool isTopicTimeOutThreadStart = false;
|
/// <summary>
|
/// 主题间的超时时间 -100:中断线程(单位:秒)
|
/// </summary>
|
private int topTimeOut = 120;
|
/// <summary>
|
/// 超时最大时间
|
/// </summary>
|
private int topMaxTime = 120;
|
/// <summary>
|
/// 画面ID,标记它由哪个界面调用并打开的
|
/// </summary>
|
private string targetFormId = string.Empty;
|
/// <summary>
|
/// 网关ID
|
/// </summary>
|
private string gatewayId = string.Empty;
|
/// <summary>
|
/// 真实网关
|
/// </summary>
|
private ZbGateway realGateway = null;
|
/// <summary>
|
/// 进度条控件
|
/// </summary>
|
private ProgressRowBar btnProgressBar = null;
|
/// <summary>
|
/// 网关是否允许入网的标识
|
/// </summary>
|
private bool gatewayCanAddDevice = false;
|
|
#endregion
|
|
#region ■ 初始化_____________________________
|
|
/// <summary>
|
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
|
/// </summary>
|
/// <param name="i_formId">画面ID,标记它由哪个界面调用并打开的</param>
|
public void ShowForm(string i_formId)
|
{
|
this.targetFormId = i_formId;
|
|
//设置标题信息
|
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice));
|
|
this.gatewayId = GatewayResourse.NowSelectGatewayId;
|
HdlGatewayLogic.Current.GetRealGateway(ref this.realGateway, this.gatewayId);
|
|
//初始化中部控件
|
this.InitMiddleFrame();
|
}
|
|
/// <summary>
|
/// 初始化中部控件
|
/// </summary>
|
private void InitMiddleFrame()
|
{
|
//清空bodyFrame
|
this.ClearBodyFrame();
|
|
//图片
|
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);
|
btnSearch.TextAlignment = TextAlignment.Center;
|
btnSearch.TextID = R.MyInternationalizationString.uDeviceSearching;
|
btnSearch.Y = Application.GetRealHeight(1037);
|
btnSearch.TextColor = UserCenterColor.Current.TextGrayColor3;
|
bodyFrameLayout.AddChidren(btnSearch);
|
|
//进度条
|
this.btnProgressBar = new ProgressRowBar(559, 29);
|
btnProgressBar.ProgressBarGoback = false;
|
btnProgressBar.Gravity = Gravity.CenterHorizontal;
|
btnProgressBar.Y = Application.GetRealHeight(861);
|
bodyFrameLayout.AddChidren(btnProgressBar);
|
btnProgressBar.StartMode1(true);
|
|
if (this.realGateway != null)
|
{
|
//允许设备入网
|
this.StartDeviceCanAddToGateway(false);
|
//添加监视设备新上报的事件
|
this.realGateway.GwResDataAction += this.AdjustGatewayResultData;
|
}
|
//开启连接的假想动画效果线程
|
this.StartConcetionAnimeteThread(framePic);
|
}
|
|
#endregion
|
|
#region ■ 新设备入网接收_____________________
|
|
/// <summary>
|
/// 处理网关主题上报
|
/// </summary>
|
/// <param name="topic">主题</param>
|
/// <param name="resultData">上报数据</param>
|
private void AdjustGatewayResultData(string topic, string resultData)
|
{
|
//检测主题(-1:异常 0:不是相关的主题 1:设备最后的主题上报 2:设备最终上报之前的主题)
|
var result = this.CheckIsDeviceComming(topic, resultData);
|
if (result == -1)
|
{
|
//停止接收
|
this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
|
|
Application.RunOnMainThread(() =>
|
{
|
this.CloseForm();
|
});
|
return;
|
}
|
else if (result == 0) { return; }
|
else if (result == 2)
|
{
|
//开启主题间隔超时线程
|
this.StartTopicTimeOutThread();
|
return;
|
}
|
|
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 = this.realGateway.GwId;
|
//将DeviceInfo的属性设置到主属性中
|
Common.LocalDevice.Current.SetDeviceInfoToMain(device, device);
|
|
//添加设备的缓存
|
device.IsOnline = 1;
|
Common.LocalDevice.Current.AddDeviceToMemory(ref device);
|
|
//刷新超时时间
|
this.waitDeviceTimeOut = 20;
|
|
//获取设备的固定属性
|
HdlDeviceFixedAttributeLogic.Current.SetAllFixedAttributeToDevice(device);
|
|
//不需要200端点的那个设备 2020.01.13 变更:ota也加进来
|
this.listNewDevice.Add(device);
|
|
//有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
|
this.StartShowDeviceAddSuccessFormThread();
|
}
|
|
#endregion
|
|
#region ■ 显示设备信息画面___________________
|
|
/// <summary>
|
/// 开启显示设备信息界面的线程
|
/// </summary>
|
private void StartShowDeviceAddSuccessFormThread()
|
{
|
if (this.isDeviceThreadStart == true)
|
{
|
//线程已经开启
|
return;
|
}
|
this.isDeviceThreadStart = true;
|
|
HdlThreadLogic.Current.RunThread(() =>
|
{
|
while (this.waitDeviceTimeOut >= 0)
|
{
|
//等待下一个回路
|
System.Threading.Thread.Sleep(100);
|
this.waitDeviceTimeOut--;
|
}
|
//停止接收
|
this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
|
System.Threading.Thread.Sleep(200);
|
|
//目前就弄一个
|
Application.RunOnMainThread(() =>
|
{
|
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();
|
}
|
//显示设备信息画面
|
this.ShowDeviceAddSuccessForm(listDevice);
|
});
|
});
|
}
|
|
/// <summary>
|
/// 显示设备信息画面
|
/// </summary>
|
/// <param name="listMacDevice">新设备列表</param>
|
private void ShowDeviceAddSuccessForm(List<CommonDevice> 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 ■ 假想动画___________________________
|
|
/// <summary>
|
/// 开启连接的假想动画效果线程
|
/// </summary>
|
private void StartConcetionAnimeteThread(FrameLayout framePic)
|
{
|
int iconSize = this.GetPictrueRealSize(23);
|
|
var listPoint = new List<int>();
|
for (int i = 0; i < 9; i++)
|
{
|
//X轴+Index*(图标大小+间距)
|
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 = this.GetPictrueRealSize(225);
|
framePic.AddChidren(btnRound);
|
|
HdlThreadLogic.Current.RunThread(() =>
|
{
|
int index = 1;
|
int timeCount = 0;
|
while (this.Parent != null)
|
{
|
System.Threading.Thread.Sleep(500);
|
|
//网关允许设备入网后才开始计时
|
if (this.gatewayCanAddDevice == true)
|
{
|
timeCount++;
|
if (timeCount >= 360)
|
{
|
this.gatewayCanAddDevice = false;
|
//再次发送允许设备入网
|
this.StartDeviceCanAddToGateway(true);
|
timeCount = 0;
|
}
|
}
|
Application.RunOnMainThread(() =>
|
{
|
if (btnRound != null)
|
{
|
btnRound.X = listPoint[index];
|
index++;
|
if (index == listPoint.Count)
|
{
|
index = 0;
|
}
|
}
|
});
|
}
|
});
|
}
|
|
#endregion
|
|
#region ■ 检测主题___________________________
|
|
/// <summary>
|
/// 检测主题(-1:异常 0:不是相关的主题 1:设备最后的主题上报 2:设备最终上报之前的主题)
|
/// </summary>
|
/// <param name="topic"></param>
|
/// <param name="resultData"></param>
|
/// <returns></returns>
|
private int CheckIsDeviceComming(string topic, string resultData)
|
{
|
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.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());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
//this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
HdlLogLogic.Current.WriteLog(-1, resultData);
|
return 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());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
//this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
HdlLogLogic.Current.WriteLog(-1, resultData);
|
return 2;
|
}
|
//设置进度值
|
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);
|
return 2;
|
}
|
//设置进度值
|
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());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
//this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
HdlLogLogic.Current.WriteLog(-1, resultData);
|
return 2;
|
}
|
//设置进度值
|
this.btnProgressBar.SetValue(5, 6);
|
return 2;
|
}
|
else if (topic == gatewayId + "/DeviceInComingRespon")
|
{
|
this.topTimeOut = topMaxTime;
|
//网关最终上报节点设备信息
|
this.btnProgressBar.SetValue(6, 6);
|
return 1;
|
}
|
return 0;
|
}
|
|
/// <summary>
|
/// 开启主题间的超时线程
|
/// </summary>
|
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.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndReAccessNetwork));
|
Application.RunOnMainThread(() =>
|
{
|
//关闭界面
|
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);
|
}
|
});
|
}
|
|
#endregion
|
|
#region ■ 画面关闭___________________________
|
|
/// <summary>
|
/// 画面关闭
|
/// </summary>
|
public override void CloseFormBefore()
|
{
|
if (this.realGateway != null)
|
{
|
//停止接收
|
this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
|
|
HdlThreadLogic.Current.RunThread(() =>
|
{
|
System.Threading.Thread.Sleep(1200);
|
//关闭入网模式
|
this.realGateway.AddNewDeviceToGateway(0);
|
});
|
}
|
base.CloseFormBefore();
|
}
|
|
#endregion
|
|
#region ■ 结构体_____________________________
|
|
/// <summary>
|
/// 网关推送的设备结果信息
|
/// </summary>
|
private class DeviceResultInfo
|
{
|
/// <summary>
|
/// 结果(0:成功,以外都是失败)
|
/// </summary>
|
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
|
}
|
}
|