using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter.Device
|
{
|
/// <summary>
|
/// 搜索设备的界面
|
/// </summary>
|
public class SearchDeviceForm : UserCenterCommonForm
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 新上报的设备
|
/// </summary>
|
private Dictionary<string, List<CommonDevice>> dicNewDevice = new Dictionary<string, List<CommonDevice>>();
|
/// <summary>
|
/// 线程是否已经开启
|
/// </summary>
|
private bool isThreadStart = false;
|
/// <summary>
|
/// 锁
|
/// </summary>
|
private object objLock = new object();
|
/// <summary>
|
/// 设备上报的主题标识
|
/// </summary>
|
private string deviceComingTopic = string.Empty;
|
/// <summary>
|
/// 等待设备的回馈的超时时间(单位:100毫秒)
|
/// </summary>
|
private int waitDeviceTimeOut = 30;
|
|
#endregion
|
|
#region ■ 初始化_____________________________
|
|
/// <summary>
|
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
|
/// </summary>
|
public void ShowForm()
|
{
|
//设置标题信息
|
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice));
|
|
this.deviceComingTopic = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway) + "/" + "DeviceInComingRespon";
|
|
//初始化中部控件
|
this.InitMiddleFrame();
|
}
|
|
/// <summary>
|
/// 初始化中部控件
|
/// </summary>
|
private void InitMiddleFrame()
|
{
|
//显示配置中的文本信息
|
this.ShowDeviceSettingMsg();
|
|
new System.Threading.Thread(() =>
|
{
|
//蓝才刚说有时候网关会收不到入网的命令,所以发三次
|
for (int i = 0; i < 3; i++)
|
{
|
lock (objLock)
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
//让网关允许入网
|
GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(255);
|
}
|
System.Threading.Thread.Sleep(1000);
|
}
|
})
|
{ IsBackground = true }.Start();
|
|
//添加监视设备新上报的事件
|
GatewayResourse.NowSelectGateway.GwResDataAction += this.AdjustGatewayResultData;
|
}
|
|
/// <summary>
|
/// 显示配置中的文本信息
|
/// </summary>
|
private void ShowDeviceSettingMsg()
|
{
|
//进度条一样的东西,没什么逻辑
|
var line = new ProgressLine();
|
bodyFrameLayout.AddChidren(line);
|
line.SetValue(45);
|
|
var frameMsg = new FrameLayout();
|
frameMsg.Height = Application.GetRealHeight(200);
|
frameMsg.Y = Application.GetRealHeight(500);
|
frameMsg.Gravity = Gravity.CenterHorizontal;
|
bodyFrameLayout.AddChidren(frameMsg);
|
|
//正在发现设备···
|
var button1 = new MsgViewControl(frameMsg.Width, Application.GetRealHeight(100));
|
button1.TextID = R.MyInternationalizationString.uDeviceSearching;
|
frameMsg.AddChidren(button1);
|
|
//请稍候
|
var button2 = new MsgViewControl(frameMsg.Width, Application.GetRealHeight(100));
|
button2.TextID = R.MyInternationalizationString.uPleaseWait;
|
button2.Y = button1.Bottom;
|
frameMsg.AddChidren(button2);
|
|
//停止配置
|
var btnStop = new BottomClickButton();
|
btnStop.TextID = R.MyInternationalizationString.uBreakSettion;
|
bodyFrameLayout.AddChidren(btnStop);
|
btnStop.MouseUpEventHandler += (sender, e) =>
|
{
|
this.CloseForm();
|
};
|
}
|
|
#endregion
|
|
#region ■ 新设备入网接收_____________________
|
|
/// <summary>
|
/// 处理网关主题上报
|
/// </summary>
|
/// <param name="topic">主题</param>
|
/// <param name="resultData">上报数据</param>
|
private void AdjustGatewayResultData(string topic, string resultData)
|
{
|
if (topic != this.deviceComingTopic)
|
{
|
//不是设备上报主题
|
return;
|
}
|
lock (objLock)
|
{
|
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;
|
}
|
//给新设备设置主键属性
|
Common.LocalDevice.Current.SetNewDeviceMainKeys(device, jobject);
|
device.CurrentGateWayId = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
|
//将DeviceInfo的属性设置到主属性中
|
Common.LocalDevice.Current.SetDeviceInfoToMain(device, device);
|
|
//添加设备的缓存(Ota的话,重置镜像类型)
|
Common.LocalDevice.Current.AddDeviceToMemory(ref device, true);
|
device.IsOnline = 1;
|
|
if (this.dicNewDevice.ContainsKey(device.DeviceAddr) == false)
|
{
|
this.dicNewDevice[device.DeviceAddr] = new List<CommonDevice>();
|
}
|
|
//刷新超时时间
|
this.waitDeviceTimeOut = 30;
|
|
if ((device is OTADevice) == false)
|
{
|
//不需要200端点的那个设备
|
this.dicNewDevice[device.DeviceAddr].Add(device);
|
|
//有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
|
this.StartShowDeviceMacInfoAddFormThread();
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 显示设备信息画面___________________
|
|
/// <summary>
|
/// 开启显示设备信息界面的线程
|
/// </summary>
|
private void StartShowDeviceMacInfoAddFormThread()
|
{
|
if (this.isThreadStart == true)
|
{
|
//线程已经开启
|
return;
|
}
|
this.isThreadStart = true;
|
|
new System.Threading.Thread(() =>
|
{
|
while (this.waitDeviceTimeOut >= 0)
|
{
|
System.Threading.Thread.Sleep(100);
|
this.waitDeviceTimeOut--;
|
}
|
lock (objLock)
|
{
|
//停止接收
|
GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData;
|
foreach (var listDevice in this.dicNewDevice.Values)
|
{
|
if (listDevice[0].ModelIdentifier == string.Empty)
|
{
|
//没有模块ID的话,再等等呗
|
System.Threading.Thread.Sleep(1500);
|
}
|
//重新变更UI
|
foreach (var device in listDevice)
|
{
|
var ui = Common.LocalDevice.Current.GetDeviceUI(device);
|
ui.IconPath = string.Empty;
|
ui.ReSave();
|
}
|
//目前就弄一个
|
Application.RunOnMainThread(() =>
|
{
|
//显示设备信息画面
|
this.ShowDeviceMacInfoAddForm(listDevice);
|
});
|
break;
|
}
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
/// <summary>
|
/// 显示设备信息画面
|
/// </summary>
|
/// <param name="listMacDevice">新设备列表</param>
|
private void ShowDeviceMacInfoAddForm(List<CommonDevice> listMacDevice)
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
//关闭自身
|
this.CloseForm();
|
//再关闭设备入网指导界面
|
this.CloseFormByFormName("AddDeviceDirectionForm");
|
//添加设备
|
var form = new DeviceMacInfoAddForm();
|
this.AddForm(form, listMacDevice);
|
}
|
|
#endregion
|
|
#region ■ 画面关闭___________________________
|
|
/// <summary>
|
/// 画面关闭
|
/// </summary>
|
/// <param name="isCloseForm">是否关闭界面,false的时候,只会调用关闭函数里面的附加功能</param>
|
public override void CloseForm(bool isCloseForm = true)
|
{
|
base.CloseForm(isCloseForm);
|
|
lock (objLock)
|
{
|
//关闭入网模式
|
GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(0);
|
//停止接收
|
GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData;
|
}
|
|
}
|
|
#endregion
|
}
|
}
|