using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Device
{
///
/// 搜索设备的界面
///
public class DeviceSearchForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 新上报的设备
///
private List listNewDevice = new List();
///
/// 显示设备线程是否已经开启
///
private bool isDeviceThreadStart = false;
///
/// 等待设备的回馈的超时时间(单位:百毫秒)
///
private int waitDeviceTimeOut = 20;
///
/// 主题超时的线程是否开启
///
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 ZbGateway realGateway = null;
///
/// 进度条控件
///
private ProgressRowBar btnProgressBar = null;
///
/// 网关是否允许入网的标识
///
private bool gatewayCanAddDevice = false;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 画面ID,标记它由哪个界面调用并打开的
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();
}
///
/// 初始化中部控件
///
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 ■ 新设备入网接收_____________________
///
/// 处理网关主题上报
///
/// 主题
/// 上报数据
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(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 = 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 ■ 显示设备信息画面___________________
///
/// 开启显示设备信息界面的线程
///
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();
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);
});
});
}
///
/// 显示设备信息画面
///
/// 新设备列表
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(FrameLayout framePic)
{
int iconSize = this.GetPictrueRealSize(23);
var listPoint = new List();
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 ■ 检测主题___________________________
///
/// 检测主题(-1:异常 0:不是相关的主题 1:设备最后的主题上报 2:设备最终上报之前的主题)
///
///
///
///
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(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(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(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(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(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(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;
}
///
/// 开启主题间的超时线程
///
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 ■ 允许设备入网_______________________
///
/// 允许设备入网
///
///
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 ■ 画面关闭___________________________
///
/// 画面关闭
///
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 ■ 结构体_____________________________
///
/// 网关推送的设备结果信息
///
private class DeviceResultInfo
{
///
/// 结果(0:成功,以外都是失败)
///
public int Result = -1;
}
///
/// 网关告知客户端有设备声明
///
private class DeviceAnnounceInfo
{
///
/// 蓝才刚说 6和7 这两个是设备重启上报的,目前暂时不能显示到入网步骤那里的
///
public int IsNew = -1;
}
///
/// 网关回复允许设备入网
///
private class SearchNewDeviceResult
{
///
/// 允许设备入网的时间
///
public int time = 0;
}
#endregion
}
}