using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Device
{
///
/// 搜索设备的界面
///
public class SearchDeviceForm : UserCenterCommonForm
{
#region ■ 变量声明___________________________
///
/// 新上报的设备
///
private Dictionary> dicNewDevice = new Dictionary>();
///
/// 线程是否已经开启
///
private bool isThreadStart = false;
///
/// 锁
///
private object objLock = new object();
///
/// 设备上报的主题标识
///
private string deviceComingTopic = string.Empty;
///
/// 等待设备的回馈的超时时间(单位:100毫秒)
///
private int waitDeviceTimeOut = 30;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
//设置标题信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice));
this.deviceComingTopic = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway) + "/" + "DeviceInComingRespon";
//初始化中部控件
this.InitMiddleFrame();
}
///
/// 初始化中部控件
///
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;
}
///
/// 显示配置中的文本信息
///
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 ■ 新设备入网接收_____________________
///
/// 处理网关主题上报
///
/// 主题
/// 上报数据
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(jobject["Data"].ToString());
if (info.DriveCode != 0)
{
//不需要虚拟设备
return;
}
//根据设备Type创建对应的设备对象
var device = Common.LocalDevice.Current.NewDeviceObjectByDeviceId((DeviceType)jobject.Value("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();
}
//刷新超时时间
this.waitDeviceTimeOut = 30;
if ((device is OTADevice) == false)
{
//不需要200端点的那个设备
this.dicNewDevice[device.DeviceAddr].Add(device);
//有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
this.StartShowDeviceMacInfoAddFormThread();
}
}
}
#endregion
#region ■ 显示设备信息画面___________________
///
/// 开启显示设备信息界面的线程
///
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();
}
///
/// 显示设备信息画面
///
/// 新设备列表
private void ShowDeviceMacInfoAddForm(List listMacDevice)
{
if (this.Parent == null)
{
return;
}
//关闭自身
this.CloseForm();
//再关闭设备入网指导界面
this.CloseFormByFormName("AddDeviceDirectionForm");
//添加设备
var form = new DeviceMacInfoAddForm();
this.AddForm(form, listMacDevice);
}
#endregion
#region ■ 画面关闭___________________________
///
/// 画面关闭
///
/// 是否关闭界面,false的时候,只会调用关闭函数里面的附加功能
public override void CloseForm(bool isCloseForm = true)
{
base.CloseForm(isCloseForm);
lock (objLock)
{
//关闭入网模式
GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(0);
//停止接收
GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData;
}
}
#endregion
}
}