using Shared.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter.GatewayManage
|
{
|
/// <summary>
|
/// 网关管理的界面
|
/// </summary>
|
public class GatewayListForm : EditorCommonForm
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 列表控件
|
/// </summary>
|
private VerticalListControl listview = null;
|
/// <summary>
|
/// 生成的网关状态控件暂时存入内存中
|
/// </summary>
|
private Dictionary<string, GatewayRowControl> dicRowContr = new Dictionary<string, GatewayRowControl>();
|
/// <summary>
|
/// 关闭设备管理界面(如果解除绑定的网关是当前所选择的网关的话,则关闭设备管理界面)
|
/// </summary>
|
private bool closeDeviceManagForm = false;
|
|
#endregion
|
|
#region ■ 初始化_____________________________
|
|
/// <summary>
|
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
|
/// </summary>
|
public void ShowForm()
|
{
|
//设置标题信息
|
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uGatewayManagement));
|
|
//添加图标
|
var btnAdd = new MostRightIconControl(69, 69);
|
btnAdd.UnSelectedImagePath = "Item/Add.png";
|
topFrameLayout.AddChidren(btnAdd);
|
btnAdd.InitControl();
|
btnAdd.ButtonClickEvent += (sender, e) =>
|
{
|
var form = new GatewayAdd.NewGateWaySelectForm();
|
form.AddForm();
|
};
|
|
//初始化中部控件
|
this.InitMiddleFrame();
|
}
|
|
/// <summary>
|
/// 初始化中部控件
|
/// </summary>
|
private void InitMiddleFrame()
|
{
|
//清空bodyFrame
|
this.ClearBodyFrame();
|
|
listview = new VerticalListControl(29);
|
listview.Height = bodyFrameLayout.Height + Application.GetRealHeight(6);
|
listview.Y = Application.GetRealHeight(-6);
|
listview.BackgroundColor = UserCenterColor.Current.White;
|
bodyFrameLayout.AddChidren(listview);
|
|
//设定中部信息
|
this.SetMiddleFrameInfo();
|
}
|
|
/// <summary>
|
/// 设定中部信息
|
/// </summary>
|
private void SetMiddleFrameInfo()
|
{
|
this.dicRowContr.Clear();
|
this.listview.RemoveAll();
|
|
//获取本地全部网关对象
|
List<ZbGateway> listway = HdlGatewayLogic.Current.GetAllLocalGateway();
|
if (listway.Count == 0)
|
{
|
return;
|
}
|
|
int count = listway.Count - 1;
|
for (int i = 0; i < listway.Count; i++)
|
{
|
//添加行
|
var gwId = HdlGatewayLogic.Current.GetGatewayId(listway[i]);
|
this.AddRowLayout(gwId, i != count);
|
}
|
//调整列表控件的高度
|
var realHeight = listview.ChildrenCount * listview.GetChildren(listview.ChildrenCount - 1).Height;
|
realHeight += Application.GetRealHeight(23);
|
if (realHeight < listview.Height)
|
{
|
//缩小控件高度
|
listview.Height = realHeight;
|
}
|
|
//设置接受在线状态推送
|
this.AddGatewayOnlinePush();
|
//开启网关在线监测的线程
|
this.StartGatewayOnlieCheckThread(listway);
|
}
|
|
#endregion
|
|
#region ■ 添加网关行_________________________
|
|
/// <summary>
|
/// 添加行
|
/// </summary>
|
/// <param name="strWayId">String way.</param>
|
/// <param name="addLine">是否添加底线</param>
|
private void AddRowLayout(string strWayId, bool addLine)
|
{
|
//网关控件
|
var Gateway = HdlGatewayLogic.Current.GetLocalGateway(strWayId);
|
var gatewayRow = new GatewayRowControl(Gateway, listview.rowSpace / 2);
|
listview.AddChidren(gatewayRow);
|
gatewayRow.InitControl(81);
|
//向右图标
|
gatewayRow.frameTable.AddRightArrow();
|
//添加在线状态
|
gatewayRow.AddOnLineControl();
|
if (addLine == true)
|
{
|
gatewayRow.frameTable.AddBottomLine();
|
}
|
|
//提示有新版本
|
var btnNew = new InformationTipView(gatewayRow.btnIcon);
|
btnNew.Visible = false;
|
gatewayRow.frameTable.AddChidren(btnNew, ChidrenBindMode.BindEventOnly);
|
gatewayRow.AddTag("btnNew", btnNew);
|
|
this.dicRowContr[strWayId] = gatewayRow;
|
|
//设置一个选择网关的默认值
|
if (string.IsNullOrEmpty(GatewayResourse.AppOldSelectGatewayId) == true && gatewayRow.IsOnline == true)
|
{
|
this.SaveGatewayIdToLocation(strWayId);
|
}
|
|
//编辑按钮
|
var btnEditor = new NormalViewControl(Application.GetRealWidth(177), gatewayRow.Height, false);
|
btnEditor.BackgroundColor = UserCenterColor.Current.RowEditorButtonColor;
|
btnEditor.TextAlignment = TextAlignment.Center;
|
btnEditor.TextColor = UserCenterColor.Current.White;
|
btnEditor.TextID = R.MyInternationalizationString.uEditor;
|
gatewayRow.AddRightView(btnEditor);
|
btnEditor.ButtonClickEvent += (sender, e) =>
|
{
|
//如果点击的是不在线的网关,则当什么事都没有发生
|
if (gatewayRow.IsOnline == false)
|
{
|
//指定的网关不在线
|
this.ShowMassage(ShowMsgType.Normal, Language.StringByID(R.MyInternationalizationString.uTheGatewayIsNotOnline));
|
return;
|
}
|
//点击编辑的话,直接隐藏
|
btnNew.Visible = false;
|
|
var form = new GatewayInfoEditorForm();
|
form.AddForm(Gateway);
|
};
|
|
//解除绑定
|
var btnUnBind = new NormalViewControl(Application.GetRealWidth(177), gatewayRow.Height, false);
|
btnUnBind.BackgroundColor = UserCenterColor.Current.RowDeleteButtonColor;
|
btnUnBind.TextAlignment = TextAlignment.Center;
|
btnUnBind.TextColor = UserCenterColor.Current.White;
|
btnUnBind.TextID = R.MyInternationalizationString.uUnBinded;
|
gatewayRow.AddRightView(btnUnBind);
|
btnUnBind.ButtonClickEvent += (sender, e) =>
|
{
|
//如移除网关,该网关绑定的设备列表{0}将清空,确认继续执行该操作?
|
string msg = Language.StringByID(R.MyInternationalizationString.uUnBindedMsg);
|
if (msg.Contains("{0}") == true)
|
{
|
msg = string.Format(msg, "\r\n");
|
}
|
this.ShowMassage(ShowMsgType.Confirm, msg, () =>
|
{
|
this.DeleteGateway(strWayId, gatewayRow);
|
});
|
};
|
|
//单击事件
|
gatewayRow.frameTable.ButtonClickEvent += (sender, e) =>
|
{
|
//如果点击的是不在线的网关,则当什么事都没有发生
|
if (gatewayRow.IsOnline == false)
|
{
|
//指定的网关不在线
|
this.ShowMassage(ShowMsgType.Normal, Language.StringByID(R.MyInternationalizationString.uTheGatewayIsNotOnline));
|
return;
|
}
|
//如果存在新版本,并且单击他的话
|
if (btnNew.Visible == true)
|
{
|
if (sender is InformationTipView || sender is IconViewControl)
|
{
|
//打开编辑界面
|
btnNew.Visible = false;
|
var form = new GatewayInfoEditorForm();
|
form.AddForm(Gateway);
|
return;
|
}
|
}
|
|
//是否切换到{0}网关?
|
string msg = string.Format(Language.StringByID(R.MyInternationalizationString.uConfirmWantToSwitchTheGateway), "[" + gatewayRow.btnName.Text + "]");
|
if (strWayId == GatewayResourse.AppOldSelectGatewayId)
|
{
|
//是否重新刷新{0}网关?
|
msg = string.Format(Language.StringByID(R.MyInternationalizationString.uConfirmWantToRefreshTheGateway), "[" + gatewayRow.btnName.Text + "]");
|
}
|
this.ShowMassage(ShowMsgType.Confirm, msg, () =>
|
{
|
//执行切换网关操作
|
this.DoSwitchGateway(strWayId);
|
});
|
};
|
}
|
|
#endregion
|
|
#region ■ 解绑网关___________________________
|
|
/// <summary>
|
/// 解绑网关
|
/// </summary>
|
/// <param name="strWayId"></param>
|
/// <param name="row"></param>
|
private async void DeleteGateway(string strWayId, GatewayRowControl row)
|
{
|
//删除云端网关
|
bool result = await HdlGatewayLogic.Current.DeleteGateway(strWayId);
|
if (result == false)
|
{
|
return;
|
}
|
|
if (strWayId == GatewayResourse.AppOldSelectGatewayId)
|
{
|
//如果解除绑定的网关是当前所选择的网关的话,则关闭设备管理界面
|
this.closeDeviceManagForm = true;
|
this.SaveGatewayIdToLocation(string.Empty);
|
}
|
|
//移除
|
if (this.dicRowContr.ContainsKey(strWayId) == true)
|
{
|
this.dicRowContr.Remove(strWayId);
|
}
|
row?.RemoveFromParent();
|
}
|
|
#endregion
|
|
#region ■ 网关切换___________________________
|
|
/// <summary>
|
/// 执行切换网关操作
|
/// </summary>
|
/// <param name="gatewayId"></param>
|
private async void DoSwitchGateway(string gatewayId)
|
{
|
//显示进度条
|
this.ShowProgressBar();
|
//获取网关
|
var gateway = HdlGatewayLogic.Current.GetLocalGateway(gatewayId);
|
|
//检测广播到的这个网关是否拥有住宅ID
|
ZbGateway realWay = null;
|
bool getGatewayInfo = true;
|
if (HdlGatewayLogic.Current.GetRealGateway(ref realWay, gatewayId) == true)
|
{
|
//重新设置住宅ID(这个应该是不经过APP,直接把网关恢复了出厂设置)
|
if (HdlGatewayLogic.Current.HomeIdIsEmpty(realWay.getGatewayBaseInfo.HomeId) == true)
|
{
|
int result2 = await HdlGatewayLogic.Current.ReBindNewGateway(realWay);
|
if (result2 == 0)
|
{
|
//出现未知错误,请稍后再试
|
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndResetAgain));
|
//关闭进度条
|
this.CloseProgressBar();
|
}
|
else if (result2 == -1)
|
{
|
//关闭进度条
|
this.CloseProgressBar();
|
return;
|
}
|
//重新绑定网关里面已经重新获取了网关信息
|
getGatewayInfo = false;
|
}
|
}
|
|
if (getGatewayInfo == true)
|
{
|
//获取网关信息
|
var info = await HdlGatewayLogic.Current.GetGatewayNewInfoAsync(gateway);
|
if (info == null)
|
{
|
//关闭进度条
|
this.CloseProgressBar();
|
return;
|
}
|
}
|
|
//获取全部设备
|
bool result = await Common.LocalDevice.Current.SetDeviceToMemmoryByGateway(gateway);
|
//关闭进度条
|
this.CloseProgressBar();
|
if (result == false)
|
{
|
return;
|
}
|
|
//切换网关,保存缓存
|
this.SaveGatewayIdToLocation(gatewayId);
|
//如果选择了刷新的网关,则不关闭管理界面
|
this.closeDeviceManagForm = false;
|
|
if (UserCenterResourse.DicActionForm.ContainsKey("DeviceListMainForm") == false)
|
{
|
//刷新主画面
|
var form = new Device.DeviceListMainForm();
|
this.AddFromAndRemoveNowForm(form);
|
}
|
else
|
{
|
//关闭界面
|
this.CloseForm();
|
//刷新主画面(不重新获取设备状态)
|
this.LoadFormMethodByName("DeviceListMainForm", "InitMiddleFrame", false);
|
}
|
}
|
|
/// <summary>
|
/// 保存选择的网关ID到本地
|
/// </summary>
|
/// <param name="gatewayId"></param>
|
private void SaveGatewayIdToLocation(string gatewayId)
|
{
|
GatewayResourse.AppOldSelectGatewayId = gatewayId;
|
byte[] data = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(GatewayResourse.AppOldSelectGatewayId));
|
Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.AppOldSelectGatewayFile, data);
|
}
|
|
#endregion
|
|
#region ■ 网关在线检测_______________________
|
|
/// <summary>
|
/// 上一次获取网关在线的时间(不能让它每点一次都去获取网关状态)
|
/// </summary>
|
private DateTime oldGetOnlineTime = new DateTime(1900, 1, 1);
|
|
/// <summary>
|
/// 开启网关在线监测的线程
|
/// </summary>
|
/// <param name="listway"></param>
|
private void StartGatewayOnlieCheckThread(List<ZbGateway> listway)
|
{
|
var timeValue = (DateTime.Now - oldGetOnlineTime).Milliseconds;
|
if (timeValue <= 10000)
|
{
|
//最少要间隔十秒,才去重新获取
|
return;
|
}
|
oldGetOnlineTime = DateTime.Now;
|
|
HdlThreadLogic.Current.RunThread(() =>
|
{
|
System.Threading.Thread.Sleep(300);
|
//刷新网关在线状态
|
HdlGatewayLogic.Current.RefreshGatewayOnlineStatu(listway);
|
foreach (var way in listway)
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
bool online = HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(way);
|
this.GatewayOnlinePush(way, online);
|
}
|
//网关新版本检测
|
this.CheckGatewayNewVersion(listway);
|
});
|
}
|
|
/// <summary>
|
/// 网关在线状态变更
|
/// </summary>
|
/// <param name="gateWay">网关对象</param>
|
/// <param name="online">在线状态变更后的状态</param>
|
public override void GatewayOnlinePush(ZbGateway gateWay, bool online)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
string gwid = HdlGatewayLogic.Current.GetGatewayId(gateWay);
|
if (this.dicRowContr.ContainsKey(gwid) == true && this.dicRowContr[gwid] != null)
|
{
|
this.dicRowContr[gwid].RefreshControl(gateWay);
|
}
|
});
|
}
|
|
#endregion
|
|
#region ■ 网关新版本检测_____________________
|
|
/// <summary>
|
/// 网关新版本检测
|
/// </summary>
|
/// <param name="listWays"></param>
|
private async void CheckGatewayNewVersion(List<ZbGateway> listWays)
|
{
|
foreach (var way in listWays)
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
if (HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(way) == false)
|
{
|
//不在线的不用理它
|
continue;
|
}
|
//获取最新版本
|
var result = await HdlGatewayLogic.Current.GetGatewayAllNewVersion(way, ShowErrorMode.NO);
|
if (result == null)
|
{
|
continue;
|
}
|
if (result[0] != null || result[1] != null || result[2] != null)
|
{
|
//有新版本
|
string gwid = HdlGatewayLogic.Current.GetGatewayId(way);
|
Application.RunOnMainThread(() =>
|
{
|
if (this.dicRowContr.ContainsKey(gwid) == true && this.dicRowContr[gwid] != null)
|
{
|
var btnNew = (InformationTipView)this.dicRowContr[gwid].GetTagByKey("btnNew");
|
if (btnNew != null)
|
{
|
btnNew.Visible = true;
|
}
|
}
|
});
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 画面关闭___________________________
|
|
/// <summary>
|
/// 画面关闭
|
/// </summary>
|
public override void CloseForm()
|
{
|
base.CloseForm();
|
|
if (string.IsNullOrEmpty(GatewayResourse.AppOldSelectGatewayId) == true && this.closeDeviceManagForm == true)
|
{
|
//关闭界面
|
HdlGatewayLogic.Current.RefreshAppOldSelectGatewayId();
|
this.LoadFormMethodByName("DeviceListMainForm", "CloseForm");
|
}
|
}
|
|
#endregion
|
|
#region ■ 界面重新激活事件___________________
|
|
/// <summary>
|
/// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
|
/// </summary>
|
public override int FormActionAgainEvent()
|
{
|
//初始化中部控件
|
this.InitMiddleFrame();
|
return 1;
|
}
|
|
#endregion
|
}
|
}
|