using System;
|
using System.Collections.Generic;
|
using System.Threading.Tasks;
|
using Newtonsoft.Json;
|
using ZigBee.Device;
|
using Shared.Phone.UserCenter;
|
|
namespace Shared.Common
|
{
|
/// <summary>
|
/// 本地网关公共类
|
/// </summary>
|
public class LocalGateway
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 本地网关
|
/// </summary>
|
private static LocalGateway m_Current = null;
|
/// <summary>
|
/// 本地网关
|
/// </summary>
|
public static LocalGateway Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new LocalGateway();
|
}
|
return m_Current;
|
}
|
set
|
{
|
m_Current = value;
|
}
|
}
|
/// <summary>
|
/// 网关文件的前缀名字
|
/// </summary>
|
private string gwFirstName = "Gateway_";
|
/// <summary>
|
/// 全部网关(这里保存的是虚拟网关,而不是真实物理网关对象)
|
/// </summary>
|
private Dictionary<string, ZbGateway> dicGateway = new Dictionary<string, ZbGateway>();
|
/// <summary>
|
/// 锁(foreach dicGateway的时候使用)
|
/// </summary>
|
private readonly object objDicLock = new object();
|
|
#endregion
|
|
#region ■ 刷新网关___________________________
|
|
/// <summary>
|
/// 刷新本地网关信息
|
/// </summary>
|
public void ReFreshByLocal()
|
{
|
lock (objDicLock)
|
{
|
this.dicGateway.Clear();
|
|
List<string> listFile = this.GetAllGatewayFile();
|
//反序列化添加到缓存
|
foreach (string file in listFile)
|
{
|
//从文件中反序列化出网关对象
|
var gateway = this.GetGatewayFromFile(file);
|
if (gateway == null)
|
{
|
continue;
|
}
|
//添加缓存
|
string gwID = Common.LocalGateway.Current.GetGatewayId(gateway);
|
dicGateway[gwID] = gateway;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 刷新APP前一次选择的网关ID
|
/// </summary>
|
public void RefreshAppOldSelectGatewayId()
|
{
|
GatewayResourse.AppOldSelectGatewayId = string.Empty;
|
|
//从文件中获取上一次选择的网关id
|
byte[] data = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.AppOldSelectGatewayFile);
|
if (data != null)
|
{
|
string strvalue = System.Text.Encoding.UTF8.GetString(data);
|
GatewayResourse.AppOldSelectGatewayId = JsonConvert.DeserializeObject<string>(strvalue);
|
}
|
//如果本地没有这个网关的话
|
if (this.IsGatewayExist(GatewayResourse.AppOldSelectGatewayId) == false)
|
{
|
GatewayResourse.AppOldSelectGatewayId = string.Empty;
|
lock (objDicLock)
|
{
|
//随便选一个网关
|
foreach (string wayId in this.dicGateway.Keys)
|
{
|
GatewayResourse.AppOldSelectGatewayId = wayId;
|
break;
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 同步云端的网关id,如果本地拥有云端不存在的id,则表示应该被换绑了,直接删除(切换住宅后,重新刷新网关列表和设备之后使用)
|
/// </summary>
|
/// <returns></returns>
|
public void SynchronizeDbGateway()
|
{
|
//从云端获取网列表ID
|
Dictionary<string, GatewayResult> result = HdlGatewayLogic.Current.GetGateWayListFromDataBase();
|
if (result == null)
|
{
|
return;
|
}
|
List<string> listDelete = new List<string>();
|
foreach (var gatewayId in this.dicGateway.Keys)
|
{
|
if (result.ContainsKey(gatewayId) == false)
|
{
|
//本地存在云端不存在的网关,则删除
|
listDelete.Add(gatewayId);
|
}
|
}
|
foreach (var gatewayId in listDelete)
|
{
|
//删除本地这个网关所有的设备
|
List<CommonDevice> list = Common.LocalDevice.Current.GetDeviceByGatewayID(gatewayId);
|
foreach (var device in list)
|
{
|
//删除设备,不删除房间信息
|
Common.LocalDevice.Current.DeleteMemmoryDevice(device, false);
|
}
|
//删除网关文件
|
this.DeleteGatewayFile(gatewayId);
|
}
|
}
|
|
/// <summary>
|
/// 从文件中反序列化出网关对象
|
/// </summary>
|
/// <param name="file"></param>
|
/// <returns></returns>
|
private ZbGateway GetGatewayFromFile(string file)
|
{
|
byte[] filebyte = Global.ReadFileByHomeId(file);
|
string strvalue = System.Text.Encoding.UTF8.GetString(filebyte);
|
var gateway = JsonConvert.DeserializeObject<ZbGateway>(strvalue);
|
return gateway;
|
}
|
|
#endregion
|
|
#region ■ 添加网关___________________________
|
|
/// <summary>
|
/// 添加新网关(仅限追加新的网关 1:正常 -1:异常 0:当前的网关绑定在了当前账号下的不同住宅里面)
|
/// </summary>
|
/// <param name="zbGateway">网关</param>
|
/// <param name="mode">是否显示错误</param>
|
public async Task<int> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
|
{
|
//执行添加网关到内存
|
var result = await this.DoAddGatewayToMemory(zbGateway, mode);
|
if (result != 1)
|
{
|
return result;
|
}
|
|
return 1;
|
}
|
|
/// <summary>
|
/// 创建一个虚拟的网关对象
|
/// </summary>
|
/// <param name="gatewayId">网关ID</param>
|
public void AddVirtualGateway(string gatewayId)
|
{
|
var gateWay = new ZbGateway { IsVirtual = true };
|
gateWay.getGatewayBaseInfo.gwID = gatewayId;
|
gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId;
|
gateWay.ReSave();
|
this.dicGateway[gatewayId] = gateWay;
|
}
|
|
/// <summary>
|
/// 执行添加网关到内存(1:正常 -1:异常 0:当前的网关绑定在了当前账号下的不同住宅里面)
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <param name="mode">是否显示错误</param>
|
/// <returns></returns>
|
private async Task<int> DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode)
|
{
|
if (zbGateway == null)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
//错误:网关对象丢失
|
string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
|
this.ShowTipMsg(msg);
|
}
|
return -1;
|
}
|
//获取网关的信息
|
var result = await zbGateway.GetZbGwInfoAsync();
|
//检测网关返回的共通错误状态码
|
string error = UserCenterLogic.CheckCommonErrorCode(result);
|
if (error != null)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
this.ShowTipMsg(error);
|
}
|
return -1;
|
}
|
|
if (result == null)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
//获取网关信息失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
|
this.ShowTipMsg(msg);
|
}
|
return -1;
|
}
|
|
if (result.getGwData == null)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
//获取网关信息失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
|
this.ShowTipMsg(msg);
|
}
|
return -1;
|
}
|
|
//设置住宅ID到网关
|
bool flage2 = await this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId);
|
if (flage2 == false)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
//向网关设置住宅ID失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uSetHomeIdToGatewayFail);
|
this.ShowTipMsg(msg);
|
}
|
return -1;
|
}
|
|
//更新云端数据库
|
int flage1 = await this.SetGatewayIdToDataBase(zbGateway);
|
//异常也不鸟它,0是特殊含义
|
if (flage1 == 0)
|
{
|
return flage1;
|
}
|
if (flage1 == -1)
|
{
|
//备份失败的网关ID
|
HdlGatewayLogic.Current.BackupGatewayIdOnNotNetwork(zbGateway);
|
}
|
|
//是否已经存在
|
string gwID = Common.LocalGateway.Current.GetGatewayId(zbGateway);
|
bool isEsist = Common.LocalGateway.Current.IsGatewayExist(zbGateway);
|
if (isEsist == false)
|
{
|
//新建一个虚拟的网关出来
|
zbGateway.ReSave();
|
var way = this.GetGatewayFromFile(zbGateway.FilePath);
|
this.dicGateway[gwID] = way;
|
}
|
|
//刷新的是缓存,不刷新真实物理网关
|
this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage;
|
this.dicGateway[gwID].getGwInfo = result.getGwData;
|
this.dicGateway[gwID].getGatewayBaseInfo.GwName = result.getGwData.GwName;
|
this.dicGateway[gwID].getGatewayBaseInfo.IpAddress = result.getGwData.GwIP;
|
this.dicGateway[gwID].getGatewayBaseInfo.HomeId = Common.Config.Instance.HomeId;
|
this.dicGateway[gwID].ReSave();
|
if (isEsist == false)
|
{
|
//添加备份
|
HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath);
|
}
|
|
return 1;
|
}
|
|
/// <summary>
|
/// 设置住宅ID到网关(失败时,不弹出任何错误信息,网关断网除外)
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <param name="HomeId"></param>
|
/// <returns></returns>
|
public async Task<bool> SetHomeIdToGateway(ZbGateway zbGateway, string HomeId)
|
{
|
var info = await zbGateway.GwSetHomeIdAsync(HomeId);
|
if (info != null && info.gwSetHomeIdData != null)
|
{
|
return true;
|
}
|
return false;
|
}
|
|
/// <summary>
|
/// 更新网关ID到云端数据库(1:正常 -1:异常 0:当前的网关绑定在了当前账号下的不同住宅里面)
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <returns></returns>
|
private async Task<int> SetGatewayIdToDataBase(ZbGateway zbGateway)
|
{
|
//调用接口,绑定网关(即使失败,也返回true往下走)
|
var bindGateway = new BindGatewayPra();
|
string gwID = this.GetGatewayId(zbGateway);
|
bindGateway.BindGateways.Add(gwID);
|
|
//不显示已经被绑定过的信息,NotSetAgain:假如断网时,不二次发送
|
List<string> listNotShow = new List<string>() { "NotSetAgain" };
|
var result = await UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", bindGateway, listNotShow);
|
if (result == "Error" || result == "ErrorEx")
|
{
|
return -1;
|
}
|
if (result == "BindGatewaysExists")
|
{
|
return 0;
|
}
|
|
return result == "Success" ? 1 : -1;
|
}
|
|
/// <summary>
|
/// 住宅ID是否为空
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <returns></returns>
|
public bool HomeIdIsEmpty(ZbGateway zbGateway)
|
{
|
return this.HomeIdIsEmpty(zbGateway.getGatewayBaseInfo.HomeId);
|
}
|
|
/// <summary>
|
/// 住宅ID是否为空
|
/// </summary>
|
/// <param name="HomeId"></param>
|
/// <returns></returns>
|
public bool HomeIdIsEmpty(string HomeId)
|
{
|
if (string.IsNullOrEmpty(HomeId) == true || HomeId[0] == '\0')
|
{
|
return true;
|
}
|
return false;
|
}
|
|
#endregion
|
|
#region ■ 重新绑定网关_______________________
|
|
/// <summary>
|
/// 重新绑定网关(1:正常 -1:异常 0:当前的网关绑定在了当前账号下的不同住宅里面)
|
/// </summary>
|
/// <param name="zbGateway">网关</param>
|
public async Task<int> ReBindNewGateway(ZbGateway zbGateway)
|
{
|
if (zbGateway == null)
|
{
|
//错误:网关对象丢失
|
string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
|
this.ShowTipMsg(msg);
|
return -1;
|
}
|
|
//设置住宅ID到网关
|
bool flage2 = await this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId);
|
if (flage2 == false)
|
{
|
//向网关设置住宅ID失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uSetHomeIdToGatewayFail);
|
this.ShowTipMsg(msg);
|
return -1;
|
}
|
|
//更新云端数据库
|
int flage1 = await this.SetGatewayIdToDataBase(zbGateway);
|
//异常也不鸟它,0是特殊含义
|
if (flage1 == 0)
|
{
|
return flage1;
|
}
|
if (flage1 == -1)
|
{
|
//备份失败的网关ID
|
HdlGatewayLogic.Current.BackupGatewayIdOnNotNetwork(zbGateway);
|
}
|
|
//网关内部数据变更中,请稍后
|
ProgressBar.SetValue(Language.StringByID(R.MyInternationalizationString.uGatewayDataIsChangingPleaseWhait));
|
|
await System.Threading.Tasks.Task.Delay(8000);
|
|
//获取网关的信息
|
var result = await zbGateway.GetZbGwInfoAsync();
|
//检测网关返回的共通错误状态码
|
string error = UserCenterLogic.CheckCommonErrorCode(result);
|
if (error != null)
|
{
|
this.ShowTipMsg(error);
|
return -1;
|
}
|
|
if (result == null)
|
{
|
//获取网关信息失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
|
this.ShowTipMsg(msg);
|
return -1;
|
}
|
|
if (result.getGwData == null)
|
{
|
//获取网关信息失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
|
this.ShowTipMsg(msg);
|
return -1;
|
}
|
|
//是否已经存在
|
string gwID = Common.LocalGateway.Current.GetGatewayId(zbGateway);
|
bool isEsist = Common.LocalGateway.Current.IsGatewayExist(zbGateway);
|
if (isEsist == false)
|
{
|
//新建一个虚拟的网关出来
|
zbGateway.ReSave();
|
var way = this.GetGatewayFromFile(zbGateway.FilePath);
|
this.dicGateway[gwID] = way;
|
}
|
|
//刷新的是缓存,不刷新真实物理网关
|
this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage;
|
this.dicGateway[gwID].getGwInfo = result.getGwData;
|
this.dicGateway[gwID].getGatewayBaseInfo.GwName = result.getGwData.GwName;
|
this.dicGateway[gwID].getGatewayBaseInfo.IpAddress = result.getGwData.GwIP;
|
this.dicGateway[gwID].getGatewayBaseInfo.HomeId = Common.Config.Instance.HomeId;
|
this.dicGateway[gwID].ReSave();
|
if (isEsist == false)
|
{
|
//添加备份
|
HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath);
|
}
|
return 1;
|
}
|
|
#endregion
|
|
#region ■ 修改网关___________________________
|
|
/// <summary>
|
/// 修改本地网关名字(失败时会显示信息)
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <param name="gatewayName">网关名</param>
|
public async Task<bool> ReName(ZbGateway zbGateway, string gatewayName)
|
{
|
ZbGateway realWay = null;
|
if (this.GetRealGateway(ref realWay, zbGateway) == false)
|
{
|
//获取网关对象失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail);
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
|
var result = await realWay.GwReNameAsync(gatewayName);
|
//检测网关返回的共通错误状态码
|
string error = UserCenterLogic.CheckCommonErrorCode(result);
|
if (error != null)
|
{
|
this.ShowErrorMsg(error);
|
return false;
|
}
|
|
if (result == null)
|
{
|
//网关名称修改失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGatewayReNameFail);
|
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
|
//网关修改失败
|
if (result.gwReNameData == null)
|
{
|
//网关名称修改失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGatewayReNameFail);
|
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
|
//修改缓存
|
string gwID = this.GetGatewayId(zbGateway);
|
this.dicGateway[gwID].getGatewayBaseInfo.GwName = gatewayName;
|
this.dicGateway[gwID].ReSave();
|
|
//添加自动备份
|
HdlAutoBackupLogic.AddOrEditorFile(zbGateway.FilePath);
|
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 删除网关___________________________
|
|
/// <summary>
|
/// 删除网关,包括云端和本地(失败时不会显示信息,并且会返回true)
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
public async Task<bool> DeleteGateway(ZbGateway zbGateway)
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
//移除本地网关信息
|
return await this.DeleteGateway(gwID);
|
}
|
|
/// <summary>
|
/// 删除网关,包括云端和本地(失败时不会显示信息,并且会返回true)
|
/// </summary>
|
/// <param name="zbGatewayID"></param>
|
public async Task<bool> DeleteGateway(string zbGatewayID)
|
{
|
ZbGateway realWay = null;
|
bool hadReal = this.GetRealGateway(ref realWay, zbGatewayID);
|
|
//暂不支持分享
|
if (hadReal == true && realWay.GatewayOnlineFlage == true)
|
{
|
//清空网关的住宅ID
|
bool result = await this.SetHomeIdToGateway(realWay, string.Empty);
|
if (result == false)
|
{
|
//网关解绑失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGatewayUnBindFail);
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
}
|
|
//删除云端的网关
|
await Phone.UserCenter.HdlGatewayLogic.Current.DeleteDataBaseGateway(zbGatewayID);
|
|
//删除网关文件
|
this.DeleteGatewayFile(zbGatewayID);
|
|
//移除
|
ZbGateway.GateWayList.RemoveAll((obj) => this.GetGatewayId(obj) == zbGatewayID);
|
//断开mqtt连接
|
realWay.DisConnect("GD");
|
|
return true;
|
}
|
|
/// <summary>
|
/// 删除网关文件
|
/// </summary>
|
/// <param name="zbGatewayID">网关id</param>
|
public void DeleteGatewayFile(string zbGatewayID)
|
{
|
if (dicGateway.ContainsKey(zbGatewayID) == false)
|
{
|
return;
|
}
|
//删除文件
|
string file = dicGateway[zbGatewayID].FilePath;
|
if (Global.IsExistsByHomeId(file) == true)
|
{
|
Global.DeleteFilebyHomeId(file);
|
}
|
|
//移除缓存
|
dicGateway.Remove(zbGatewayID);
|
//删除自动备份
|
HdlAutoBackupLogic.DeleteFile(file);
|
|
//删除设备文件
|
List<CommonDevice> list = Common.LocalDevice.Current.GetDeviceByGatewayID(zbGatewayID);
|
foreach (var device in list)
|
{
|
//删除设备文件
|
Common.LocalDevice.Current.DeleteMemmoryDevice(device, true);
|
//删除设备备份文件
|
Common.LocalDevice.Current.DeleteDeviceBackupFile(device);
|
}
|
}
|
|
#endregion
|
|
#region ■ 网关掉线___________________________
|
|
/// <summary>
|
/// 刷新网关的在线状态(注意,刷新的是缓存,请调用CheckGatewayOnlineByFlag来判断是否在线)
|
/// </summary>
|
/// <param name="listGateway"></param>
|
/// <param name="waitTime">局域网的时候,时候等待3秒延迟</param>
|
/// <returns></returns>
|
public void RefreshGatewayOnlineStatu(List<ZbGateway> listGateway, bool waitTime = true)
|
{
|
var listRealWay = new List<ZbGateway>();
|
for (int i = 0; i < listGateway.Count; i++)
|
{
|
ZbGateway zbTemp = null;
|
if (this.GetRealGateway(ref zbTemp, listGateway[i]) == true)
|
{
|
//真实物理网关
|
listRealWay.Add(zbTemp);
|
}
|
else
|
{
|
//虚拟物理网关
|
listRealWay.Add(listGateway[i]);
|
if (waitTime == false)
|
{
|
//不等待的话,标识指定网关为不在线
|
this.SetGatewayNotOnLineFlag(listRealWay[i]);
|
}
|
}
|
if (waitTime == true)
|
{
|
//只有等待的时候,才标识指定网关为不在线
|
this.SetGatewayNotOnLineFlag(listRealWay[i]);
|
}
|
}
|
|
if (waitTime == true)
|
{
|
//这是第一道坎,强制检查WIFI:等待3秒(因为wifi的时候,它会自动去刷新flage)
|
System.Threading.Thread.Sleep(3000);
|
//检查是否拥有网关存在于WIFi下
|
if (this.CheckHadGatewayInWifi(listRealWay) == false)
|
{
|
//第二道坎:在远程的情况下刷新网关的在线状态
|
this.RefreshGatewayOnlineOnRemode(listRealWay);
|
}
|
}
|
|
//刷新缓存的在线标识
|
foreach (var zbway in listRealWay)
|
{
|
string gwID = this.GetGatewayId(zbway);
|
if (this.dicGateway.ContainsKey(gwID) == false)
|
{
|
continue;
|
}
|
this.dicGateway[gwID].GatewayOnlineFlage = zbway.GatewayOnlineFlage;
|
this.dicGateway[gwID].ReSave();
|
}
|
}
|
|
/// <summary>
|
/// 检查是否拥有网关存在于WIFi下
|
/// </summary>
|
/// <param name="listGateway"></param>
|
/// <returns></returns>
|
private bool CheckHadGatewayInWifi(List<ZbGateway> listGateway)
|
{
|
foreach (var zbway in listGateway)
|
{
|
//是否存在网关存在于WIFI下
|
if (zbway.GatewayOnlineFlage == true)
|
{
|
return true;
|
}
|
}
|
return false;
|
}
|
|
/// <summary>
|
/// 在远程的情况下刷新网关的在线状态
|
/// </summary>
|
/// <param name="listGateway"></param>
|
/// <returns></returns>
|
private void RefreshGatewayOnlineOnRemode(List<ZbGateway> listGateway)
|
{
|
//获取云端上面的网关
|
Dictionary<string, GatewayResult> dicDbGateway = HdlGatewayLogic.Current.GetGateWayListFromDataBase();
|
if (dicDbGateway == null)
|
{
|
return;
|
}
|
foreach (var way in listGateway)
|
{
|
if (way == null || way.getGatewayBaseInfo == null)
|
{
|
continue;
|
}
|
string strId = this.GetGatewayId(way);
|
if (dicDbGateway.ContainsKey(strId) == true //如果云端上面有这个网关
|
&& dicDbGateway[strId].MqttOnlineStatus == true //如果云端上面这个网关是在线的
|
)
|
{
|
way.GatewayOnlineFlage = true;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 标识指定网关为不在线
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
public void SetGatewayNotOnLineFlag(ZbGateway zbGateway)
|
{
|
if (zbGateway == null)
|
{
|
return;
|
}
|
zbGateway.GatewayOnlineFlage = false;
|
}
|
|
/// <summary>
|
/// 根据某种标识判断指定网关是否在线
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <returns></returns>
|
public bool CheckGatewayOnlineByFlag(ZbGateway zbGateway)
|
{
|
if (zbGateway == null)
|
{
|
return false;
|
}
|
//使用缓存的,因为刷新在线状态的时候,刷新的就是缓存,而不是真实物理网关
|
string gwID = this.GetGatewayId(zbGateway);
|
if (this.dicGateway.ContainsKey(gwID) == true)
|
{
|
return this.dicGateway[gwID].GatewayOnlineFlage;
|
}
|
|
return zbGateway.GatewayOnlineFlage;
|
}
|
|
#endregion
|
|
#region ■ 获取网关___________________________
|
|
/// <summary>
|
/// 从网关获取全部的网关(以本地网关为标准)
|
/// </summary>
|
/// <returns></returns>
|
public List<ZbGateway> GetAllGatewayFromGateway()
|
{
|
//不要去Foreach 它的列表
|
List<ZbGateway> list = new List<ZbGateway>();
|
list.AddRange(ZbGateway.GateWayList);
|
|
List<ZbGateway> newlist = new List<ZbGateway>();
|
foreach (var way in list)
|
{
|
if (Config.Instance.HomeId != way.getGatewayBaseInfo.HomeId)
|
{
|
//如果不是当前住宅
|
continue;
|
}
|
string gwID = this.GetGatewayId(way);
|
if (this.dicGateway.ContainsKey(gwID) == false)
|
{
|
//如果本地并没有这个网关
|
continue;
|
}
|
newlist.Add(way);
|
}
|
return newlist;
|
}
|
|
/// <summary>
|
/// 获取本地全部的网关
|
/// </summary>
|
/// <returns>The all gateway.</returns>
|
public List<ZbGateway> GetAllLocalGateway()
|
{
|
List<ZbGateway> listData = new List<ZbGateway>();
|
lock (objDicLock)
|
{
|
foreach (var way in dicGateway.Values)
|
{
|
listData.Add(way);
|
}
|
}
|
return listData;
|
}
|
|
/// <summary>
|
/// 获取本地的网关
|
/// </summary>
|
/// <param name="gatewayId">网关ID</param>
|
/// <returns></returns>
|
public ZbGateway GetLocalGateway(string gatewayId)
|
{
|
if (this.dicGateway.ContainsKey(gatewayId) == true)
|
{
|
return this.dicGateway[gatewayId];
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 获取本地所有的网关文件
|
/// </summary>
|
/// <returns></returns>
|
public List<string> GetAllGatewayFile()
|
{
|
List<string> list = new List<string>();
|
List<string> listFile = Global.FileListByHomeId();
|
foreach (string file in listFile)
|
{
|
//只获取网关设备
|
if (file.StartsWith(gwFirstName) == false)
|
{
|
continue;
|
}
|
list.Add(file);
|
}
|
return list;
|
}
|
|
/// <summary>
|
/// 获取系统内部的真实网关对象变量
|
/// </summary>
|
/// <param name="zbGateway">真实网关</param>
|
/// <param name="tagartWay">目标网关</param>
|
/// <returns></returns>
|
public bool GetRealGateway(ref ZbGateway zbGateway, ZbGateway tagartWay)
|
{
|
if (tagartWay == null || tagartWay.getGatewayBaseInfo == null)
|
{
|
return false;
|
}
|
return this.GetRealGateway(ref zbGateway, this.GetGatewayId(tagartWay));
|
}
|
|
/// <summary>
|
/// 获取系统内部的真实网关对象变量
|
/// </summary>
|
/// <param name="zbGateway">真实网关</param>
|
/// <param name="tagartWay">目标网关</param>
|
/// <returns></returns>
|
public bool GetRealGateway(ref ZbGateway zbGateway, string gwId)
|
{
|
var realWay = ZbGateway.GateWayList.Find((obj) =>
|
{
|
return this.GetGatewayId(obj) == gwId;
|
});
|
if (realWay == null)
|
{
|
return false;
|
}
|
zbGateway = realWay;
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 获取网关GwInfo里面的属性___________
|
|
/// <summary>
|
/// 获取网关GwInfo里面的属性
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <param name="attributeName">getGwInfo里面属性的名字</param>
|
/// <param name="defult">如果获取不到时,设置的默认值</param>
|
/// <returns></returns>
|
public object GetGwInfoAttribute(ZbGateway zbGateway, string attributeName, string defult = "")
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
var localWay = this.GetLocalGateway(gwID);
|
|
object objValue = null;
|
if (localWay == null || localWay.getGwInfo == null)
|
{
|
//本地没有记录有这个东西,则直接返回参数的数据
|
if (zbGateway.getGwInfo != null)
|
{
|
objValue = zbGateway.getGwInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, zbGateway.getGwInfo, null);
|
}
|
else
|
{
|
return defult;
|
}
|
}
|
else
|
{
|
//获取本地的属性
|
objValue = localWay.getGwInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGwInfo, null);
|
}
|
|
if (objValue == null)
|
{
|
return defult;
|
}
|
return objValue;
|
}
|
|
/// <summary>
|
/// 获取网关GatewayBaseInfo里面的属性
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <param name="attributeName">GatewayBaseInfo里面属性的名字</param>
|
/// <param name="defult">如果获取不到时,设置的默认值</param>
|
/// <returns></returns>
|
public object GetGatewayBaseInfoAttribute(ZbGateway zbGateway, string attributeName, string defult = "")
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
var localWay = this.GetLocalGateway(gwID);
|
|
object objValue = null;
|
if (localWay == null || localWay.getGatewayBaseInfo == null)
|
{
|
//本地没有记录有这个东西,则直接返回参数的数据
|
if (zbGateway.getGatewayBaseInfo != null)
|
{
|
objValue = zbGateway.getGatewayBaseInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, zbGateway.getGatewayBaseInfo, null);
|
}
|
else
|
{
|
return defult;
|
}
|
}
|
else
|
{
|
//获取本地属性
|
objValue = localWay.getGatewayBaseInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGatewayBaseInfo, null);
|
}
|
|
if (objValue == null)
|
{
|
return defult;
|
}
|
return objValue;
|
}
|
|
#endregion
|
|
#region ■ 获取网关ID(有特殊意义)_____________
|
|
/// <summary>
|
/// 获取网关对象里面的网关ID(封装这个方法有特殊意义)
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <returns></returns>
|
public string GetGatewayId(ZbGateway zbGateway)
|
{
|
//这个东西不会为空
|
return zbGateway.getGatewayBaseInfo.gwID;
|
}
|
|
#endregion
|
|
#region ■ 获取名称___________________________
|
|
/// <summary>
|
/// 获取网关加特效的名称
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <param name="mode"></param>
|
/// <returns></returns>
|
public string GetGatewayName(ZbGateway zbGateway, GetNameMode mode = GetNameMode.SpecialGateway)
|
{
|
string gwId = this.GetGatewayId(zbGateway);
|
if (this.dicGateway.ContainsKey(gwId) == false)
|
{
|
return zbGateway.getGatewayBaseInfo.GwName == null ? string.Empty : zbGateway.getGatewayBaseInfo.GwName;
|
}
|
var localWay = this.dicGateway[gwId];
|
|
string name = this.GetGatewaySimpleName(localWay);
|
if (string.IsNullOrEmpty(name) == false)
|
{
|
return name;
|
}
|
|
if (mode == GetNameMode.SpecialGateway && localWay.getGwInfo != null)
|
{
|
string keyName = Common.LocalDevice.deviceDefultNameFlag + this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString();
|
if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true)
|
{
|
//没有名称时,则使用R文件里面设置的东西
|
return Language.StringByID(Common.LocalDevice.Current.dicDeviceDefultNameID[keyName]);
|
}
|
}
|
|
return string.Empty;
|
}
|
|
/// <summary>
|
/// 单纯获取网关名称
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <returns></returns>
|
private string GetGatewaySimpleName(ZbGateway zbGateway)
|
{
|
if (zbGateway == null)
|
{
|
return string.Empty;
|
}
|
return zbGateway.getGatewayBaseInfo.GwName;
|
}
|
|
/// <summary>
|
/// 获取网关镜像类型的翻译名字
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <returns></returns>
|
public string GetGatewayImageText(ZbGateway zbGateway)
|
{
|
string gwId = this.GetGatewayId(zbGateway);
|
int imageType = -1;
|
if (this.dicGateway.ContainsKey(gwId) == false || this.dicGateway[gwId].getGwInfo == null)
|
{
|
//如果这个网关没有信息,则从新获取
|
var result = this.GetGatewayNewInfo(zbGateway, ShowErrorMode.NO);
|
if (result == null)
|
{
|
//无法识别的网关设备
|
return Language.StringByID(R.MyInternationalizationString.uUnDistinguishTheGatewayDevice);
|
}
|
imageType = result.LinuxImageType;
|
}
|
else
|
{
|
imageType = Convert.ToInt32(this.GetGwInfoAttribute(this.dicGateway[gwId], "LinuxImageType"));
|
}
|
string keyName = Common.LocalDevice.deviceDefultNameFlag + imageType;
|
if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true)
|
{
|
//使用R文件里面设置的东西
|
return Language.StringByID(Common.LocalDevice.Current.dicDeviceDefultNameID[keyName]);
|
}
|
|
//无法识别的网关设备
|
return Language.StringByID(R.MyInternationalizationString.uUnDistinguishTheGatewayDevice);
|
}
|
|
#endregion
|
|
#region ■ 获取网关信息_______________________
|
|
/// <summary>
|
/// 获取网关信息(版本信息,镜像类型,基本信息等。只刷新本地网关的缓存)
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <param name="mode"></param>
|
/// <returns></returns>
|
public async Task<ZbGatewayData.GetGwData> GetGatewayNewInfoAsync(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES)
|
{
|
ZbGateway realWay = null;
|
if (this.GetRealGateway(ref realWay, zbGateway) == false)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
|
this.ShowTipMsg(msg);
|
}
|
return null;
|
}
|
//获取网关版本信息
|
var imageTypeResult = await realWay.GetZbGwInfoAsync();
|
//检测网关返回的共通错误状态码
|
string error = UserCenterLogic.CheckCommonErrorCode(imageTypeResult);
|
if (error != null)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
this.ShowTipMsg(error);
|
}
|
return null;
|
}
|
|
if (imageTypeResult == null || imageTypeResult.getGwData == null)
|
{
|
if (mode == ShowErrorMode.YES)
|
{
|
//获取网关信息失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
|
//拼接上【网关回复超时】的Msg
|
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, "回复超时");
|
this.ShowErrorMsg(msg);
|
}
|
return null;
|
}
|
|
string gwID = this.GetGatewayId(zbGateway);
|
if (this.dicGateway.ContainsKey(gwID) == true)
|
{
|
//刷新缓存
|
ZbGateway localWay = this.dicGateway[gwID];
|
localWay.getGwInfo = imageTypeResult.getGwData;
|
localWay.getGatewayBaseInfo.GwName = imageTypeResult.getGwData.GwName;
|
localWay.getGatewayBaseInfo.IpAddress = imageTypeResult.getGwData.GwIP;
|
|
localWay.ReSave();
|
}
|
|
return imageTypeResult.getGwData;
|
}
|
|
/// <summary>
|
/// 获取网关信息,非异步,会等待(版本信息,镜像类型,基本信息等。只刷新本地网关的缓存)
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <param name="mode"></param>
|
/// <returns></returns>
|
public ZbGatewayData.GetGwData GetGatewayNewInfo(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES)
|
{
|
bool bolBreak = false;
|
ZbGatewayData.GetGwData result = null;
|
new System.Threading.Thread(async () =>
|
{
|
result = await this.GetGatewayNewInfoAsync(zbGateway, mode);
|
bolBreak = true;
|
})
|
{ IsBackground = true }.Start();
|
|
while (bolBreak == false)
|
{
|
System.Threading.Thread.Sleep(500);
|
}
|
return result;
|
}
|
|
#endregion
|
|
#region ■ 清空真实网关列表___________________
|
|
/// <summary>
|
/// 清空全部的真实物理网关对象
|
/// </summary>
|
public void ClearAllRealGateway()
|
{
|
//因为那一瞬间,有可能mqtt会加回来,所以先加缓存
|
var list = new List<ZbGateway>();
|
list.AddRange(ZbGateway.GateWayList);
|
//然后清空掉
|
ZbGateway.GateWayList.Clear();
|
//最后再断开mqtt连接
|
for (int i = 0; i < list.Count; i++)
|
{
|
list[i].DisConnect("G");
|
}
|
list.Clear();
|
}
|
|
#endregion
|
|
#region ■ 检测并获取网关各种固件新版本_______
|
|
/// <summary>
|
/// 检测并获取网关各固件的新版本,没有新版本,则对应位置存的是null,直接返回null代表失败(0:Linux新版本 1:协调器新版本 2~X:都是虚拟驱动的)
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <param name="mode">错误时,是否显示错误</param>
|
/// <returns></returns>
|
public async Task<List<FirmwareVersionInfo>> GetGatewayAllNewVersion(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES)
|
{
|
//获取网关版本信息
|
var result = await Common.LocalGateway.Current.GetGatewayNewInfoAsync(zbGateway, mode);
|
if (result == null)
|
{
|
return null;
|
}
|
//使用本地缓存对象
|
string gwID = this.GetGatewayId(zbGateway);
|
var localWay = this.GetLocalGateway(gwID);
|
if (localWay == null)
|
{
|
return null;
|
}
|
|
//添加网关的升级固件(成不成功都无所谓)
|
var flage = await FirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.Linux,
|
this.GetGwInfoAttribute(localWay, "LinuxHWVersion").ToString(),
|
this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString());
|
|
//添加协调器的升级固件(成不成功都无所谓)
|
flage = await FirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.Coordinator,
|
this.GetGwInfoAttribute(localWay, "ZbHWVersion").ToString(),
|
this.GetGwInfoAttribute(localWay, "ZbImageType").ToString());
|
|
//网关的版本
|
var gatewayFirmware = FirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.Linux,
|
this.GetGwInfoAttribute(localWay, "LinuxHWVersion").ToString(),
|
this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString(),
|
Convert.ToInt32(this.GetGwInfoAttribute(localWay, "LinuxFWVersion")));
|
|
//协调器版本
|
var coordinatorFirmware = FirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.Coordinator,
|
this.GetGwInfoAttribute(localWay, "ZbHWVersion").ToString(),
|
this.GetGwInfoAttribute(localWay, "ZbImageType").ToString(),
|
Convert.ToInt32(this.GetGwInfoAttribute(localWay, "ZbFWVersion")));
|
|
//这个网关需要有虚拟驱动这个东西才行
|
FirmwareVersionInfo virtualFirmware = null;
|
string linImage = this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString();
|
if (linImage != "6")
|
{
|
//虚拟驱动号
|
var listvVode = await Common.LocalGateway.Current.GetListVDDriveCode(localWay);
|
if (listvVode != null && listvVode.Count > 0)
|
{
|
//添加虚拟驱动的升级固件(成不成功都无所谓)
|
flage = await FirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.VirtualDevice,
|
listvVode[0].DriveHwVersion.ToString(),
|
listvVode[0].DriveImageType.ToString());
|
|
//虚拟驱动
|
virtualFirmware = FirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.VirtualDevice,
|
listvVode[0].DriveHwVersion.ToString(),
|
listvVode[0].DriveImageType.ToString(),
|
listvVode[0].DriveFwVersion);
|
|
if (virtualFirmware != null)
|
{
|
virtualFirmware.VirtualCode = listvVode[0].DriveCode;
|
}
|
}
|
}
|
var list = new List<FirmwareVersionInfo>();
|
list.Add(gatewayFirmware);
|
list.Add(coordinatorFirmware);
|
list.Add(virtualFirmware);
|
|
return list;
|
}
|
|
/// <summary>
|
/// 获取网关的虚拟驱动号(返回null时代表获取失败)
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <returns></returns>
|
public async Task<List<ZbGatewayData.DriveCodeObj>> GetListVDDriveCode(ZbGateway zbGateway)
|
{
|
ZbGateway zbTemp = null;
|
if (this.GetRealGateway(ref zbTemp, zbGateway) == false)
|
{
|
return null;
|
}
|
var result = await zbTemp.CheckVDDriveCodeAsync();
|
//检测网关返回的共通错误状态码
|
string error = UserCenterLogic.CheckCommonErrorCode(result);
|
if (error != null)
|
{
|
this.ShowTipMsg(error);
|
return null;
|
}
|
|
if (result == null || result.vDriveDriveCodeResponData == null || result.vDriveDriveCodeResponData.DriveCodeList == null)
|
{
|
return null;
|
}
|
if (result.vDriveDriveCodeResponData.DriveCodeList.Count > 0)
|
{
|
return result.vDriveDriveCodeResponData.DriveCodeList;
|
}
|
return null;
|
}
|
|
#endregion
|
|
#region ■ 主网关判定_________________________
|
|
/// <summary>
|
/// 判断是否主网关(1:主网关 0:不在线 -1:远程模式,不存在啥主网关的说法)
|
/// </summary>
|
/// <param name="zbGateway">网关对象</param>
|
/// <returns></returns>
|
public int IsMainGateway(ZbGateway zbGateway)
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
return this.IsMainGateway(gwID);
|
}
|
|
/// <summary>
|
/// 判断是否主网关(1:主网关 0:不在线 -1:远程模式,不存在啥主网关的说法)
|
/// </summary>
|
/// <param name="waiID">网关id</param>
|
/// <returns></returns>
|
public int IsMainGateway(string waiID)
|
{
|
if (this.nowGwConnectMode == GatewayConnectMode.Remote)
|
{
|
return -1;
|
}
|
ZbGateway zbGateway = null;
|
if (this.GetRealGateway(ref zbGateway, waiID) == false)
|
{
|
return 0;
|
}
|
return zbGateway.IsMainGateWay == true ? 1 : 0;
|
}
|
|
#endregion
|
|
#region ■ 设置网关图片_______________________
|
|
/// <summary>
|
/// 设置真实网关的图片,规格:915X492
|
/// </summary>
|
/// <param name="button"></param>
|
/// <param name="zbGateway"></param>
|
public void SetRealGatewayPictrue(ButtonCommon button, ZbGateway zbGateway)
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
var localWay = this.GetLocalGateway(gwID);
|
if (localWay == null || localWay.getGwInfo == null)
|
{
|
//给一个线程去获取它的镜像类型
|
new System.Threading.Thread(async () =>
|
{
|
var result = await this.GetGatewayNewInfoAsync(zbGateway, ShowErrorMode.NO);
|
if (result != null)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
button.UnSelectedImagePath = "Gateway/RealGateway" + result.LinuxImageType + ".png";
|
});
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
else
|
{
|
button.UnSelectedImagePath = "Gateway/RealGateway" + this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString() + ".png";
|
}
|
}
|
|
/// <summary>
|
/// 设置网关图标,规格:110X110
|
/// </summary>
|
/// <param name="button"></param>
|
/// <param name="zbGateway"></param>
|
public void SetGatewayIcon(ButtonCommon button, ZbGateway zbGateway)
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
var localWay = this.GetLocalGateway(gwID);
|
if (localWay == null || localWay.getGwInfo == null)
|
{
|
//给一个线程去获取它的镜像类型
|
new System.Threading.Thread(async () =>
|
{
|
var result = await this.GetGatewayNewInfoAsync(zbGateway, ShowErrorMode.NO);
|
if (result != null)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
button.UnSelectedImagePath = "Gateway/GatewayIcon" + result.LinuxImageType + ".png";
|
button.SelectedImagePath = "Gateway/GatewayIcon" + result.LinuxImageType + "Selected.png";
|
});
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
else
|
{
|
string linType = this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString();
|
button.UnSelectedImagePath = "Gateway/GatewayIcon" + linType + ".png";
|
button.SelectedImagePath = "Gateway/GatewayIcon" + linType + "Selected.png";
|
}
|
}
|
|
#endregion
|
|
#region ■ 网关存在检测_______________________
|
|
/// <summary>
|
/// 网关是否已经存在
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
/// <returns></returns>
|
public bool IsGatewayExist(ZbGateway zbGateway)
|
{
|
string gwID = this.GetGatewayId(zbGateway);
|
return this.IsGatewayExist(gwID);
|
}
|
|
/// <summary>
|
/// 网关是否已经存在
|
/// </summary>
|
/// <param name="gatewayId">网关ID</param>
|
/// <returns></returns>
|
public bool IsGatewayExist(string gatewayId)
|
{
|
if (gatewayId == null)
|
{
|
return false;
|
}
|
return dicGateway.ContainsKey(gatewayId);
|
}
|
|
#endregion
|
|
#region ■ 测试网关___________________________
|
|
/// <summary>
|
/// 发送测试指令到设备(网关LED闪烁识别)
|
/// </summary>
|
/// <param name="zbGateway"></param>
|
public void SetTestCommand(ZbGateway zbGateway)
|
{
|
ZbGateway realWay = null;
|
if (this.GetRealGateway(ref realWay, zbGateway) == false)
|
{
|
return;
|
}
|
var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 85 } };
|
realWay.Send("GwLinuxLocate_Respon", jObject.ToString());
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 弹出网关不在线的
|
/// </summary>
|
private void ShowGatewayNotOnlineMsg()
|
{
|
Application.RunOnMainThread(() =>
|
{
|
//网关连接失败,请确认网络
|
string msg = Language.StringByID(R.MyInternationalizationString.uGatewayIsNotLinkAndCheckNetwork);
|
var control = new TipViewControl(msg);
|
control.ShowView();
|
});
|
}
|
|
/// <summary>
|
/// 显示错误信息窗口
|
/// </summary>
|
/// <param name="msg"></param>
|
private void ShowErrorMsg(string msg)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
var contr = new ErrorMsgControl(msg);
|
contr.Show();
|
});
|
}
|
|
/// <summary>
|
/// 显示Tip信息窗口
|
/// </summary>
|
/// <param name="msg"></param>
|
private void ShowTipMsg(string msg)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
var contr = new Phone.UserCenter.TipViewControl(msg);
|
contr.ShowView();
|
});
|
}
|
|
#endregion
|
|
#region ■ 网关监视___________________________
|
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 网关监视中
|
/// </summary>
|
private bool isGatewaySearch = false;
|
/// <summary>
|
/// 提示网络切换的界面
|
/// </summary>
|
private Phone.UserCenter.User.NetSwitchAlertForm netSwitchAlertForm = null;
|
/// <summary>
|
/// 当前的网络连接模式
|
/// </summary>
|
private GatewayConnectMode nowGwConnectMode = GatewayConnectMode.None;
|
/// <summary>
|
/// 是否存在网关正在升级
|
/// </summary>
|
private bool hadGatewayUpdate = false;
|
/// <summary>
|
/// 网络的锁
|
/// </summary>
|
private object objNetLock = new object();
|
#endregion
|
|
#region ■ 网络监视___________________________
|
|
/// <summary>
|
/// 开启监听网络(★★废弃★★)
|
/// </summary>
|
public void StartListenNetWork()
|
{
|
if (this.isGatewaySearch == true)
|
{
|
return;
|
}
|
//关闭监听网络变化
|
this.StopListenNetWork();
|
|
this.isGatewaySearch = true;
|
#if Android
|
//开启监听安卓网络变化
|
BaseActivity.NetworkStateChanged += this.ListenAnDroidNetworkStateChanged;
|
#endif
|
|
#if iOS
|
//开启监听IOS网络变化
|
GateWay.Ios.Reachability.ReachabilityChanged += ListenIOSNetworkStateChanged;
|
#endif
|
}
|
|
#if Android
|
/// <summary>
|
/// 监听安卓网络变化(★★废弃★★)
|
/// </summary>
|
/// <param name="value"></param>
|
private void ListenAnDroidNetworkStateChanged(int value)
|
{
|
//没有网络
|
if (value == 0)
|
{
|
//无网络连接
|
this.SetGatewayNetworkMode(GatewayConnectMode.NoLine);
|
}
|
//可以叫4G
|
else if (value == 1)
|
{
|
//强制走远程
|
this.CheckGatewayByConnectChanged(GatewayConnectMode.Remote);
|
}
|
//WIFI
|
else if (value == 2)
|
{
|
}
|
}
|
#endif
|
|
#if iOS
|
/// <summary>
|
/// 监听IOS网络变化(★★废弃★★)
|
/// </summary>
|
private void ListenIOSNetworkStateChanged(object sender, EventArgs e)
|
{
|
var internetStatus = GateWay.Ios.Reachability.InternetConnectionStatus();
|
//Shared.SimpleControl.EquipmentPublicClass.CheckLinkRemote((int)internetStatus);
|
if (internetStatus == GateWay.Ios.NetworkStatus.NotReachable)//没有网络连接 0
|
{
|
//无网络连接
|
this.SetGatewayNetworkMode(GatewayConnectMode.NoLine);
|
}
|
else if (internetStatus == GateWay.Ios.NetworkStatus.ReachableViaCarrierDataNetwork)//3,4G的网络连接 1
|
{
|
//强制走远程
|
this.CheckGatewayByConnectChanged(GatewayConnectMode.Remote);
|
}
|
else if (internetStatus == GateWay.Ios.NetworkStatus.ReachableViaWiFiNetwork) //wifi的网络连接 2
|
{
|
}
|
}
|
#endif
|
|
#endregion
|
|
#region ■ 网关连接方式变更___________________
|
|
/// <summary>
|
/// 当网关的连接方式改变时,检测网关,然后显示特效
|
/// </summary>
|
/// <param name="connectMode">网关变更后的连接方式</param>
|
public void CheckGatewayByConnectChanged(GatewayConnectMode connectMode)
|
{
|
lock (objNetLock)
|
{
|
if (this.nowGwConnectMode == connectMode || this.hadGatewayUpdate == true)
|
{
|
//相同的连接模式,或者有网关正在升级,则不需要操作
|
return;
|
}
|
//设置网络连接模式
|
this.SetGatewayNetworkMode(connectMode);
|
}
|
}
|
|
/// <summary>
|
/// 设置网关网络连接模式
|
/// </summary>
|
/// <param name="mode"></param>
|
private void SetGatewayNetworkMode(GatewayConnectMode mode)
|
{
|
//没有网络
|
if (mode == GatewayConnectMode.NoLine)
|
{
|
nowGwConnectMode = GatewayConnectMode.NoLine;
|
//在远程的条件下,检查网关的在线状态(网络变更时触发)
|
//this.CheckGatewayStatuByNotNet();
|
}
|
//可以叫4G
|
else if (mode == GatewayConnectMode.Remote)
|
{
|
nowGwConnectMode = GatewayConnectMode.Remote;
|
//在远程的条件下,检查网关的在线状态(网络变更时触发)
|
this.CheckGatewayStatuByRemote();
|
}
|
//WIFI
|
else if (mode == GatewayConnectMode.WIFI)
|
{
|
nowGwConnectMode = GatewayConnectMode.WIFI;
|
//在WIFI的条件下,检查网关的在线状态(网络变更时触发)
|
this.CheckGatewayStatuByWIFI();
|
}
|
}
|
|
#endregion
|
|
#region ■ 检查网关(WIFI)_____________________
|
|
/// <summary>
|
/// 在WIFI的条件下,检查网关的在线状态(网络变更时触发)
|
/// </summary>
|
private void CheckGatewayStatuByWIFI()
|
{
|
if (this.dicGateway.Count == 0)
|
{
|
//如果本地都没有网关的话,不需要处理
|
return;
|
}
|
//显示切换了网络的界面
|
//this.ShowSwitchNetFormByMode(GatewayConnectMode.WIFI);
|
|
new System.Threading.Thread(() =>
|
{
|
var dicStatu = this.GetNowOnlineStatuBeforeCheck();
|
//等个3秒,准备一下
|
System.Threading.Thread.Sleep(3000);
|
if (nowGwConnectMode != GatewayConnectMode.WIFI)
|
{
|
//网络临时变更??
|
return;
|
}
|
|
//从网关获取全部的网关
|
List<ZbGateway> list = this.GetAllGatewayFromGateway();
|
foreach (var way in list)
|
{
|
string gwId = this.GetGatewayId(way);
|
if (this.dicGateway.ContainsKey(gwId) == true)
|
{
|
//刷新一下缓存
|
this.dicGateway[gwId].GatewayOnlineFlage = way.GatewayOnlineFlage;
|
}
|
if (this.CheckGatewayOnlineByFlag(way) == true)
|
{
|
//网关在线
|
dicStatu[gwId] = true;
|
}
|
}
|
//将变化的网关推送到界面上
|
this.PushGatewayOnlineStatuToForm(dicStatu);
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
#endregion
|
|
#region ■ 检查网关(4G)_______________________
|
|
/// <summary>
|
/// 在远程的条件下,检查网关的在线状态(网络变更时触发)
|
/// </summary>
|
private void CheckGatewayStatuByRemote()
|
{
|
if (this.dicGateway.Count == 0)
|
{
|
//如果本地都没有网关的话,不需要处理
|
return;
|
}
|
|
//显示切换了网络的界面
|
//this.ShowSwitchNetFormByMode(GatewayConnectMode.Remote);
|
|
new System.Threading.Thread(() =>
|
{
|
//先获取现在全部的网关,初期値设置为不在线
|
var dicStatu = this.GetNowOnlineStatuBeforeCheck();
|
|
//获取云端上面的网关
|
Dictionary<string, GatewayResult> dicDbGateway = HdlGatewayLogic.Current.GetGateWayListFromDataBase();
|
if (nowGwConnectMode != GatewayConnectMode.Remote || dicDbGateway == null)
|
{
|
//网络临时变更??
|
return;
|
}
|
|
bool hadOnline = false;
|
lock (objDicLock)
|
{
|
foreach (var way in this.dicGateway.Values)
|
{
|
//循环处理本地全部的网关列表
|
string gwId = this.GetGatewayId(way);
|
if (dicDbGateway.ContainsKey(gwId) == true//如果云端上面有这个网关
|
&& dicDbGateway[gwId].MqttOnlineStatus == true//如果云端上面这个网关是在线的
|
)
|
{
|
dicGateway[gwId].GatewayOnlineFlage = true;
|
//它是在线的
|
dicStatu[gwId] = true;
|
hadOnline = true;
|
}
|
}
|
}
|
|
//将变化的网关推送到界面上
|
this.PushGatewayOnlineStatuToForm(dicStatu);
|
|
//如果没有网关在线
|
if (hadOnline == false)
|
{
|
//远程连接超时,没有网关在线
|
string msg = Language.StringByID(R.MyInternationalizationString.uRemoteTimeOutAndNotGatewaiOnline);
|
this.ShowTipMsg(msg);
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
#endregion
|
|
#region ■ 检查网关(无网络)___________________
|
|
/// <summary>
|
/// 在远程的条件下,检查网关的在线状态(网络变更时触发)
|
/// </summary>
|
private void CheckGatewayStatuByNotNet()
|
{
|
//关闭显示切换了网络的界面
|
this.CloseNetSwitchAlertForm();
|
//当前无网络连接,请确认网络
|
string msg = Language.StringByID(R.MyInternationalizationString.uNowIsDonotNetworkAndCheckNetwork);
|
this.ShowTipMsg(msg);
|
}
|
|
#endregion
|
|
#region ■ 关闭网络监听_______________________
|
|
/// <summary>
|
/// 关闭网络监听变化
|
/// </summary>
|
public void StopListenNetWork()
|
{
|
#if Android
|
//停止监听安卓网络变化
|
BaseActivity.NetworkStateChanged -= this.ListenAnDroidNetworkStateChanged;
|
#endif
|
|
#if iOS
|
//停止监听IOS网络变化
|
GateWay.Ios.Reachability.ReachabilityChanged -= ListenIOSNetworkStateChanged;
|
#endif
|
|
//关闭显示切换了网络的界面
|
this.CloseNetSwitchAlertForm();
|
//初始化连接模式
|
//this.nowGwConnectMode = GatewayConnectMode.None;
|
|
this.isGatewaySearch = false;
|
}
|
|
#endregion
|
|
#region ■ 网关推送___________________________
|
|
/// <summary>
|
/// 将变化的网关推送到界面上
|
/// </summary>
|
/// <param name="dicStatu"></param>
|
private void PushGatewayOnlineStatuToForm(Dictionary<string, bool> dicStatu)
|
{
|
List<ZbGateway> listChange = new List<ZbGateway>();
|
List<bool> listChangeStatu = new List<bool>();
|
foreach (string gwid in dicStatu.Keys)
|
{
|
//网关不多,直接推送,不判断是否改变了
|
ZbGateway zbGateway = this.GetLocalGateway(gwid);
|
if (zbGateway != null)
|
{
|
listChange.Add(zbGateway);
|
listChangeStatu.Add(dicStatu[gwid]);
|
|
zbGateway.GatewayOnlineFlage = dicStatu[gwid];
|
}
|
}
|
|
if (listChange.Count == 0)
|
{
|
return;
|
}
|
//获取当前接受在线状态推送的界面
|
List<UserCenterCommonForm> listForm = new List<UserCenterCommonForm>();
|
listForm.AddRange(UserCenterResourse.listGatewayOnlinePushForm);
|
if (listForm.Count > 0)
|
{
|
for (int i = 0; i < listChange.Count; i++)
|
{
|
foreach (var form in listForm)
|
{
|
form.GatewayOnlinePush(listChange[i], listChangeStatu[i]);
|
}
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 界面特效显示_______________________
|
|
/// <summary>
|
/// 根据模式显示切换了网络的界面
|
/// </summary>
|
/// <param name="mode"></param>
|
private void ShowSwitchNetFormByMode(GatewayConnectMode mode)
|
{
|
if (mode == GatewayConnectMode.None && netSwitchAlertForm != null)
|
{
|
//关闭显示切换了网络的界面
|
this.CloseNetSwitchAlertForm();
|
}
|
else if (mode == GatewayConnectMode.WIFI && netSwitchAlertForm != null)
|
{
|
//关闭显示切换了网络的界面
|
this.CloseNetSwitchAlertForm();
|
}
|
else if (mode == GatewayConnectMode.Remote && netSwitchAlertForm == null)
|
{
|
//当前为远程连接模式
|
//string msg = Language.StringByID(R.MyInternationalizationString.uNowConnectModeIsRemoteMode);
|
//this.ShowSwitchNetForm(msg);
|
}
|
}
|
|
/// <summary>
|
/// 显示切换了网络的界面
|
/// </summary>
|
/// <param name="msg"></param>
|
private void ShowSwitchNetForm(string msg)
|
{
|
if (netSwitchAlertForm == null)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
if (netSwitchAlertForm != null)
|
{
|
return;
|
}
|
netSwitchAlertForm = new Phone.UserCenter.User.NetSwitchAlertForm();
|
netSwitchAlertForm.Show();
|
});
|
|
System.Threading.Thread.Sleep(300);
|
|
Application.RunOnMainThread(() =>
|
{
|
if (netSwitchAlertForm != null)
|
{
|
netSwitchAlertForm.InitTextControl(msg);
|
}
|
});
|
}
|
else
|
{
|
Application.RunOnMainThread(() =>
|
{
|
if (netSwitchAlertForm != null)
|
{
|
netSwitchAlertForm.SetText(msg);
|
}
|
});
|
}
|
}
|
|
/// <summary>
|
/// 关闭显示切换了网络的界面
|
/// </summary>
|
private void CloseNetSwitchAlertForm()
|
{
|
Application.RunOnMainThread(() =>
|
{
|
if (netSwitchAlertForm == null)
|
{
|
return;
|
}
|
netSwitchAlertForm.CloseForm();
|
netSwitchAlertForm = null;
|
});
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 设置存在网关正在升级的标识
|
/// </summary>
|
/// <param name="update">是否有网关在升级</param>
|
public void SetHadGatewayUpdateFlage(bool update)
|
{
|
this.hadGatewayUpdate = update;
|
}
|
|
/// <summary>
|
/// 在网络连接变更之后,网关连接检测之前,设置全部网关为离线状态(以本地网关为标准)
|
/// </summary>
|
/// <returns></returns>
|
private Dictionary<string, bool> GetNowOnlineStatuBeforeCheck()
|
{
|
//先获取现在全部的网关
|
List<ZbGateway> listOldGateway = this.GetAllGatewayFromGateway();
|
var dicOldZb = new Dictionary<string, ZbGateway>();
|
foreach (var zb in listOldGateway)
|
{
|
string gwID = this.GetGatewayId(zb);
|
dicOldZb[gwID] = zb;
|
}
|
|
var dicStatu = new Dictionary<string, bool>();
|
lock (objDicLock)
|
{
|
foreach (string gwId in this.dicGateway.Keys)
|
{
|
if (dicOldZb.ContainsKey(gwId) == true)
|
{
|
//标记为不在线
|
this.SetGatewayNotOnLineFlag(dicOldZb[gwId]);
|
}
|
dicStatu[gwId] = false;
|
}
|
}
|
return dicStatu;
|
}
|
|
#endregion
|
|
#endregion
|
}
|
}
|