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 Dictionary<string, List<CommonDevice>> dicNewDevice = new Dictionary<string, List<CommonDevice>>();
|
/// <summary>
|
/// 显示设备线程是否已经开启
|
/// </summary>
|
private bool isDeviceThreadStart = false;
|
/// <summary>
|
/// 等待设备的回馈的超时时间(单位:秒)
|
/// </summary>
|
private int waitDeviceTimeOut = 3;
|
/// <summary>
|
/// 主题超时的线程是否开启
|
/// </summary>
|
private bool isTopicTimeOutThreadStart = false;
|
/// <summary>
|
/// 主题间的超时时间 -100:中断线程(单位:秒)
|
/// </summary>
|
private int topTimeOut = 60;
|
/// <summary>
|
/// 画面ID,标记它由哪个界面调用并打开的
|
/// </summary>
|
private string targetFormId = string.Empty;
|
/// <summary>
|
/// 网关ID
|
/// </summary>
|
private string gatewayId = string.Empty;
|
/// <summary>
|
/// 进度条
|
/// </summary>
|
private FrameLayout btnProgressBar = null;
|
/// <summary>
|
/// 进度值显示文本的整体
|
/// </summary>
|
private FrameLayout frameProgress = null;
|
/// <summary>
|
/// 进度值的显示文本
|
/// </summary>
|
private NormalViewControl btnProgressView = null;
|
|
#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 = HdlGatewayLogic.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
|
|
//初始化中部控件
|
this.InitMiddleFrame();
|
}
|
|
/// <summary>
|
/// 初始化中部控件
|
/// </summary>
|
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);
|
|
new System.Threading.Thread(() =>
|
{
|
//蓝才刚说有时候网关会收不到入网的命令,所以发三次
|
for (int i = 0; i < 3; i++)
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
//让网关允许入网
|
GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(255);
|
System.Threading.Thread.Sleep(1000);
|
}
|
})
|
{ IsBackground = true }.Start();
|
|
//添加监视设备新上报的事件
|
GatewayResourse.NowSelectGateway.GwResDataAction += this.AdjustGatewayResultData;
|
//开启连接的假想动画效果线程
|
this.StartConcetionAnimeteThread();
|
}
|
|
#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)
|
{
|
//停止接收
|
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<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(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<CommonDevice>();
|
}
|
|
//刷新超时时间
|
this.waitDeviceTimeOut = 3;
|
|
//获取设备的固定属性
|
HdlDeviceFixedAttributeLogic.Current.SetAllFixedAttributeToDevice(device);
|
|
if ((device is OTADevice) == false)
|
{
|
//不需要200端点的那个设备
|
this.dicNewDevice[device.DeviceAddr].Add(device);
|
|
//有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
|
this.StartShowDeviceAddSuccessFormThread();
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 显示设备信息画面___________________
|
|
/// <summary>
|
/// 开启显示设备信息界面的线程
|
/// </summary>
|
private void StartShowDeviceAddSuccessFormThread()
|
{
|
if (this.isDeviceThreadStart == true)
|
{
|
//线程已经开启
|
return;
|
}
|
this.isDeviceThreadStart = true;
|
|
new System.Threading.Thread(() =>
|
{
|
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;
|
}
|
});
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
/// <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()
|
{
|
int iconSize = Application.GetMinRealAverage(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)));
|
}
|
|
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);
|
|
new System.Threading.Thread(() =>
|
{
|
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;
|
}
|
}
|
});
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
#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/DeviceAnnounce_Respon")
|
{
|
this.topTimeOut = 60;
|
//网关告知客户端有设备声明
|
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<DeviceResultInfo>(jobject["Data"].ToString());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
return -1;
|
}
|
this.topTimeOut = 60;
|
//设置进度值
|
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<DeviceResultInfo>(jobject["Data"].ToString());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
return -1;
|
}
|
this.topTimeOut = 60;
|
//设置进度值
|
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<DeviceResultInfo>(jobject["Data"].ToString());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
return -1;
|
}
|
this.topTimeOut = 60;
|
//设置进度值
|
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<DeviceResultInfo>(jobject["Data"].ToString());
|
if (info.Result != 0)
|
{
|
//出现未知错误,请重新入网
|
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndReAccessNetwork));
|
return -1;
|
}
|
this.topTimeOut = 60;
|
//设置进度值
|
this.SetDeviceProgressValue(5);
|
return 2;
|
}
|
else if (topic == gatewayId + "/DeviceInComingRespon")
|
{
|
//网关最终上报节点设备信息
|
this.SetDeviceProgressValue(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));
|
btnProgressBar.Width = width;
|
|
//文本显示
|
btnProgressView.Text = ((int)(result * 100)) + "%";
|
//文本显示的那个图片框移动
|
frameProgress.X = ControlCommonResourse.XXLeft + btnProgressBar.Right - frameProgress.Width / 2;
|
});
|
}
|
|
/// <summary>
|
/// 开启主题间的超时线程
|
/// </summary>
|
private void StartTopicTimeOutThread()
|
{
|
if (this.isTopicTimeOutThreadStart == true)
|
{
|
return;
|
}
|
this.isTopicTimeOutThreadStart = true;
|
new System.Threading.Thread(() =>
|
{
|
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();
|
});
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
#endregion
|
|
#region ■ 画面关闭___________________________
|
|
/// <summary>
|
/// 画面关闭
|
/// </summary>
|
public override void CloseForm()
|
{
|
base.CloseForm();
|
//停止接收
|
GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData;
|
|
new System.Threading.Thread(() =>
|
{
|
System.Threading.Thread.Sleep(1200);
|
//关闭入网模式
|
GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(0);
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
#endregion
|
|
#region ■ 结构体_____________________________
|
|
/// <summary>
|
/// 网关推送的设备结果信息
|
/// </summary>
|
private class DeviceResultInfo
|
{
|
/// <summary>
|
/// 结果(0:成功,以外都是失败)
|
/// </summary>
|
public int Result = -1;
|
}
|
|
#endregion
|
}
|
}
|