using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.GatewayAdd
{
///
/// 网关搜索中
///
public class WiredGatewaySearchForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalScrolViewLayout listView = null;
///
/// 这是个透明的控件
///
private FrameLayout frameTransparent = null;
///
/// 线程执行中
///
private bool IsThreadAction = true;
///
/// 搜索时间
///
private int searchTime = 60;
///
/// 停止搜索的按键
///
private BottomClickButton btnStop = null;
///
/// 网关的主键(value:剩余检测次数,用于对应异常的网关)
///
private Dictionary dicGatewayId = new Dictionary();
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
//设置标题信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddWiredGateway));
//初始化中部控件
this.InitMiddleFrame();
//开启线程
this.StartThread();
}
///
/// 初始化中部控件
///
private void InitMiddleFrame()
{
//清空bodyFrame
this.ClearBodyFrame();
//网关列表,这个控件置于背部
listView = new VerticalScrolViewLayout();
listView.Height = ControlCommonResourse.ListViewRowHeight * 5;
bodyFrameLayout.AddChidren(listView);
//图片,背景要透明
frameTransparent = new FrameLayout
{
Width = bodyFrameLayout.Width,
Height = bodyFrameLayout.Height,
Gravity = Gravity.CenterHorizontal,
BackgroundColor = UserCenterColor.Current.Transparent
};
bodyFrameLayout.AddChidren(frameTransparent);
//图片
var btnimage = new PicViewControl(620, 620);
btnimage.Y = ControlCommonResourse.ListViewRowHeight * 2;
btnimage.Gravity = Gravity.CenterHorizontal;
btnimage.UnSelectedImagePath = "Gateway/SearchGateway.png";
frameTransparent.AddChidren(btnimage);
//智能网关配置网络中
var btnText1 = new NormalViewControl(bodyFrameLayout.Width, false);
btnText1.Y = btnimage.Bottom + Application.GetRealHeight(60);
btnText1.Gravity = Gravity.CenterHorizontal;
btnText1.TextID = R.MyInternationalizationString.uGatewaySetting;
frameTransparent.AddChidren(btnText1);
//请稍候
var btnText2 = new NormalViewControl(bodyFrameLayout.Width, false);
btnText2.Y = btnText1.Bottom + Application.GetRealHeight(30);
btnText2.Gravity = Gravity.CenterHorizontal;
btnText2.TextID = R.MyInternationalizationString.uPleaseWait;
frameTransparent.AddChidren(btnText2);
//停止搜索
btnStop = new BottomClickButton();
btnStop.TextID = R.MyInternationalizationString.uStopSearch;
frameTransparent.AddChidren(btnStop);
btnStop.MouseUpEventHandler += this.btnStop_MouseClick;
}
#endregion
#region ■ 开启线程___________________________
///
/// 开启搜索网关的子线程
///
private void StartThread()
{
string textValue = btnStop.Text;
//清空保存的主键
this.dicGatewayId.Clear();
//线程运行中
this.IsThreadAction = true;
//清空全部列表
HdlGatewayLogic.Current.ClearAllRealGateway();
ZigBee.Common.Application.IsSearchingGateway = true;
//开启倒计时线程
this.StartTimeCountThread(textValue);
HdlThreadLogic.Current.RunThread(async () =>
{
while (this.IsThreadAction)
{
await Task.Delay(2500);
//异步网关检测中(先等个5秒,然后等mqtt连接)
if (this.searchTime > 55)
{
continue;
}
//检测搜索到的网关,然后添加到画面的行里面
await this.CheckZbGatewayAndSetRow();
}
});
}
///
/// 开启倒计时线程
///
///
private void StartTimeCountThread(string textValue)
{
//搜索时间
this.searchTime = 60;
HdlThreadLogic.Current.RunThread(() =>
{
while (this.IsThreadAction)
{
System.Threading.Thread.Sleep(1000);
this.searchTime--;
if (this.searchTime == 0 || this.IsThreadAction == false)
{
//时间截止
this.btnStop_MouseClick(null, null);
break;
}
Application.RunOnMainThread(() =>
{
if (btnStop != null)
{
//倒计时显示
btnStop.Text = textValue + "(" + searchTime + ")";
}
});
}
Application.RunOnMainThread(() =>
{
if (btnStop != null)
{
btnStop.Text = textValue;
}
});
});
}
#endregion
#region ■ 网关检测___________________________
///
/// 检测搜索到的网关,然后添加到画面的行里面
///
private async Task CheckZbGatewayAndSetRow()
{
List listId = new List();
for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
{
string strip = HdlGatewayLogic.Current.GetGatewayBaseInfoAttribute(ZbGateway.GateWayList[i], "IpAddress").ToString();
if (strip == string.Empty)
{
//IP没有的网关,我也不知道它是干嘛的
continue;
}
string gwId = HdlGatewayLogic.Current.GetGatewayId(ZbGateway.GateWayList[i]);
//重复添加检测
if (this.dicGatewayId.ContainsKey(gwId) == false)
{
//允许五次检测
this.dicGatewayId[gwId] = 5;
listId.Add(gwId);
}
else if (this.dicGatewayId[gwId] > 0)
{
//如果前一次检测失败了的话,它的次数-1
this.dicGatewayId[gwId] = this.dicGatewayId[gwId] - 1;
listId.Add(gwId);
}
}
if (listId.Count == 0)
{
//没有新的网关,或者次数已经用完
return true;
}
for (int i = 0; i < listId.Count; i++)
{
ZbGateway way = ZbGateway.GateWayList.Find((obj) => HdlGatewayLogic.Current.GetGatewayId(obj) == listId[i]);
if (way == null)
{
continue;
}
//网关绑定模式
GatewayBindMode mode = GatewayBindMode.BindAgain;
//如果是第一次绑定,或者是以前已经绑定过了的
if (HdlGatewayLogic.Current.HomeIdIsEmpty(way) == true
|| way.getGatewayBaseInfo.HomeId == Common.Config.Instance.HomeId)
{
if (way.getGatewayBaseInfo.HomeId == Common.Config.Instance.HomeId)
{
//第一次绑定,也就是网关住宅ID为空
mode = GatewayBindMode.First;
}
else
{
//已经绑定过了
mode = GatewayBindMode.Binded;
}
//添加搜索到的网关到缓存(执行网关保存操作)
ShowErrorMode showMode = this.dicGatewayId[listId[i]] == 0 ? ShowErrorMode.YES : ShowErrorMode.NO;
var result = await this.DoSaveGateway(way, showMode, mode);
if (result == false)
{
//当使用完次数之后,并且本地不存在的,才显示出来
if (this.dicGatewayId[listId[i]] == 0 && isExist == false)
{
Application.RunOnMainThread(() =>
{
//添加失败的网关到画面
this.AddFailRowLayout(way);
});
}
continue;
}
}
//如果它已经完全成功了的话,则将它的剩余次数置零
this.dicIdCheckCount[listId[i]] = 0;
//全部处理OK后,才能设置flage
if (mode == GatewayBindMode.First)
{
//第一次绑定
this.dicZbGatewayDiv[listId[i]] = 0;
//获取到了新网关
this.newGatewayGetting = true;
}
else if (mode == GatewayBindMode.Binded)
{
//已经绑定过
this.dicZbGatewayDiv[listId[i]] = 1;
}
else
{
//需要重新绑定
this.dicZbGatewayDiv[listId[i]] = 2;
}
if (mode == GatewayBindMode.First)
{
//我觉得这里需要获取一下新网关的设备列表
await Common.LocalDevice.Current.SetDeviceToMemmoryByGateway(way);
}
Application.RunOnMainThread(() =>
{
if (this.Parent != null)
{
//添加搜索到的网关到画面
this.AddRowLayout(way, mode);
}
});
}
return true;
}
#endregion
#region ■ 添加网关行控件_____________________
///
/// 添加搜索到的网关到画面
///
/// Zb gateway.
/// 网关绑定模式
private void AddRowLayout(ZbGateway zbGateway, GatewayBindMode mode)
{
if (listView == null)
{
return;
}
//网关列表控件
var gatewayRow = new GatewayRowControl(zbGateway);
listView.AddChidren(gatewayRow);
gatewayRow.InitControl(69);
//不需要在线控件
gatewayRow.btnOnline.RemoveFromParent();
gatewayRow.btnOnline = null;
gatewayRow.frameTable.ButtonClickEvent += (sender, e) =>
{
//打开网关编辑界面
this.OpenEditorGatewayForm(gatewayRow, mode);
};
//向右图标
gatewayRow.frameTable.AddRightArrow();
//绑定
var btnBind = gatewayRow.frameTable.AddMostRightView("", 300);
if (mode != GatewayBindMode.BindAgain)
{
//已绑定
btnBind.TextID = R.MyInternationalizationString.uBinded;
btnBind.TextColor = UserCenterColor.Current.Green;
}
else
{
//需重新绑定
btnBind.TextID = R.MyInternationalizationString.uNeedReBinding;
btnBind.TextColor = UserCenterColor.Current.Red;
//这里只是临时追加,后面可以会移动到别的地方
var btnConfirm = new NormalViewControl(Application.GetRealWidth(184), gatewayRow.Height, false);
btnConfirm.Text = "网关识别";
gatewayRow.AddRightView(btnConfirm);
btnConfirm.MouseUpEventHandler += (sender, e) =>
{
HdlGatewayLogic.Current.SetTestCommand(zbGateway);
};
}
gatewayRow.AddTag("btnBind", btnBind);
}
///
/// 添加搜索到的网关到画面的行
///
/// Zb gateway.
private void AddFailRowLayout(ZbGateway zbGateway)
{
if (listView == null)
{
return;
}
//网关列表控件
var gatewayRow = new GatewayRowControl(zbGateway);
listView.AddChidren(gatewayRow);
gatewayRow.InitControl(69);
//不需要在线控件
gatewayRow.btnOnline.RemoveFromParent();
gatewayRow.btnOnline = null;
//智能跳过
var btnBind = gatewayRow.frameTable.AddMostRightView("", 300);
btnBind.TextID = R.MyInternationalizationString.uIntelligentSkip;
btnBind.TextColor = UserCenterColor.Current.Red;
}
#endregion
#region ■ 执行网关保存_______________________
///
/// 执行网关保存
///
/// 网关对象
/// 是否显示错误
/// 绑定方式
///
private async Task DoSaveGateway(ZbGateway zbGateway, ShowErrorMode mode, GatewayBindMode bindMode)
{
//添加搜索到的网关到缓存
//1:正常 -1:异常 0:当前的网关绑定在了当前账号下的不同住宅里面
int result = 0;
if (bindMode == GatewayBindMode.BindAgain)
{
result = await HdlGatewayLogic.Current.ReBindNewGateway(zbGateway);
}
else
{
result = await HdlGatewayLogic.Current.AddNewGateway(zbGateway, mode);
}
if (result == -1)
{
return false;
}
//前的网关绑定在了当前账号下的不同住宅里面
if (result == 0)
{
if (mode == ShowErrorMode.YES)
{
//网关绑定在当前账号下的其他住宅里\r\n请解除绑定后再试
string msg = Language.StringByID(R.MyInternationalizationString.uTheGatewayInOtherResidenceMsg);
if (msg.Contains("{0}") == true)
{
msg = string.Format(msg, "\r\n");
}
this.ShowMassage(ShowMsgType.Tip, msg);
}
return false;
}
return true;
}
#endregion
#region ■ 打开编辑界面_______________________
///
/// 打开网关编辑界面
///
///
///
private void OpenEditorGatewayForm(GatewayRowControl viewRow, GatewayBindMode mode)
{
//已经绑定了,就不用再显示提示
if (mode == GatewayBindMode.BindAgain && viewRow.GetTagByKey("btnBind") != null)
{
//是否重新绑定网关?
string msg = Language.StringByID(R.MyInternationalizationString.uRebindGatewayMsg);
this.ShowMassage(ShowMsgType.Confirm, msg, () =>
{
this.ShowConfirmMsgBeforShowForm(viewRow);
});
}
else
{
//打开网关名称编辑画面
var form = new GatewayInfoAddForm();
form.AddForm(viewRow.zbGateway);
form.ActionGatewayReName += ((way) =>
{
viewRow.RefreshControl(way);
});
}
}
///
/// 在打开编辑画面之前,弹出确认窗口(重新绑定专用)
///
///
private async void ShowConfirmMsgBeforShowForm(GatewayRowControl viewRow)
{
var realway = ZbGateway.GateWayList.Find((obj) =>
HdlGatewayLogic.Current.GetGatewayId(obj) == HdlGatewayLogic.Current.GetGatewayId(viewRow.zbGateway));
//显示进度条
this.ShowProgressBar();
var result = await this.DoSaveGateway(realway, ShowErrorMode.YES, GatewayBindMode.BindAgain);
if (result == false)
{
//关闭进度条
this.CloseProgressBar();
return;
}
//获取设备列表中,请稍后
this.SetProgressValue(Language.StringByID(R.MyInternationalizationString.uDeviceIsGettingPleaseWait));
//我觉得这里需要获取一下新网关的设备列表
result = await Common.LocalDevice.Current.SetDeviceToMemmoryByGateway(realway);
//关闭进度条
this.CloseProgressBar();
Application.RunOnMainThread(() =>
{
//已绑定
var btnBind = (NormalViewControl)viewRow.GetTagByKey("btnBind");
btnBind.TextID = R.MyInternationalizationString.uBinded;
btnBind.TextColor = UserCenterColor.Current.Green;
viewRow.RemoveTag("btnBind");
//打开网关名称编辑画面
var form = new GatewayInfoAddForm();
form.AddForm(viewRow.zbGateway);
form.ActionGatewayReName += ((way) =>
{
viewRow.RefreshControl(way);
});
});
}
#endregion
#region ■ 按钮事件___________________________
///
/// 停止按钮按下
///
/// Sender.
/// E.
private void btnStop_MouseClick(object sender, MouseEventArgs e)
{
//存在误按的可能,画面开启的2秒前,禁止按下停止按钮
if (this.searchTime >= 58 || this.Parent == null)
{
return;
}
//超时
if (e == null && listView.ChildrenCount == 0)
{
//没有发现新网关,请检查网关链接是否正常
string msg = Language.StringByID(R.MyInternationalizationString.uNotFoundNewGatewayPleaseCheckMsg);
this.ShowMassage(ShowMsgType.Tip, msg);
}
Application.RunOnMainThread(() =>
{
this.IsThreadAction = false;
if (frameTransparent != null)
{
frameTransparent.Visible = false;
}
if (listView != null)
{
listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(18);
}
this.searchTime = 60;
});
}
#endregion
#region ■ 界面关闭___________________________
///
/// 画面关闭
///
public override void CloseForm()
{
this.IsThreadAction = false;
ZigBee.Common.Application.IsSearchingGateway = false;
//断开没有执行绑定的网关的mqtt
var list = new List();
for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
{
if (HdlGatewayLogic.Current.IsGatewayExist(ZbGateway.GateWayList[i]) == false)
{
list.Add(ZbGateway.GateWayList[i]);
}
}
if (list.Count > 0)
{
foreach (var way in list)
{
ZbGateway.GateWayList.RemoveAll((obj) => HdlGatewayLogic.Current.GetGatewayId(obj) == HdlGatewayLogic.Current.GetGatewayId(way));
way.DisConnect("Search");
}
}
if (UserCenterResourse.listActionFormId.Contains("GatewayManagementForm") == false)
{
//刷新主页
UserView.UserPage.Instance.Fresh();
}
base.CloseForm();
}
#endregion
#region ■ 一般方法___________________________
#endregion
}
}