old mode 100755
new mode 100644
| | |
| | | /// </summary> |
| | | /// <param name="zbGateway">网关</param> |
| | | /// <param name="mode">是否显示错误</param> |
| | | public bool AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode) |
| | | /// <param name="logMode">是否写Log</param> |
| | | public bool AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode, WriteLogMode logMode = WriteLogMode.NO) |
| | | {
|
| | | //设置网关的经纬度
|
| | | bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.NO); |
| | | if (falge == false)
|
| | | {
|
| | | if (logMode == WriteLogMode.YES) |
| | | { |
| | | HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 设置经纬度失败"); |
| | | } |
| | | return falge;
|
| | | } |
| | | //执行添加网关到内存 |
| | | var result = this.DoAddGatewayToMemory(zbGateway, mode); |
| | | var result = this.DoAddGatewayToMemory(zbGateway, mode, logMode); |
| | | //前的网关绑定在了当前账号下的不同住宅里面
|
| | | if (result == 0)
|
| | | {
|
| | |
| | | }
|
| | | this.ShowTipMsg(msg);
|
| | | }
|
| | | if (logMode == WriteLogMode.YES) |
| | | { |
| | | HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 网关绑定在了当前账号下的不同住宅里面,云端不给绑定"); |
| | | } |
| | | return false;
|
| | | }
|
| | | if (result == -1)
|
| | |
| | | } |
| | | //添加网关的话,强制主页刷新 |
| | | UserView.UserPage.Instance.RefreshAllForm = true; |
| | | |
| | | if (Config.Instance.Home.TemplateMode == 2) |
| | | { |
| | | //如果这个类型的网关只有一个模板,则自动匹配 |
| | | var dicName = TemplateData.TemplateCommonLogic.Current.GetGatewayCanSelectTemplateName(zbGateway); |
| | | if (dicName.Count == 1) |
| | | { |
| | | foreach (var tempId in dicName.Keys) |
| | | { |
| | | TemplateData.TemplateCommonLogic.Current.AddGatewayTemplateSelect(zbGateway.GwId, tempId); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return true; |
| | | }
|
| | |
| | | /// </summary>
|
| | | /// <param name="zbGateway">网关对象</param>
|
| | | /// <param name="mode">是否显示错误</param>
|
| | | /// <param name="logMode">是否写Log</param> |
| | | /// <returns></returns>
|
| | | private int DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode)
|
| | | private int DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode, WriteLogMode logMode = WriteLogMode.NO) |
| | | {
|
| | | if (zbGateway == null)
|
| | | {
|
| | |
| | | string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail);
|
| | | this.ShowTipMsg(msg);
|
| | | }
|
| | | if (logMode == WriteLogMode.YES) |
| | | { |
| | | HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 获取网关信息失败"); |
| | | } |
| | | return -1;
|
| | | }
|
| | |
|
| | | //设置住宅ID到网关
|
| | | if (result.HomeId != Common.Config.Instance.HomeId)
|
| | | {
|
| | | bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, mode);
|
| | | bool flage2 = this.SetHomeIdToGateway(zbGateway.GwId, Common.Config.Instance.HomeId, mode); |
| | | if (flage2 == false)
|
| | | {
|
| | | if (mode == ShowErrorMode.YES)
|
| | |
| | | //向网关设置住宅ID失败
|
| | | string msg = Language.StringByID(R.MyInternationalizationString.uSetHomeIdToGatewayFail);
|
| | | this.ShowTipMsg(msg);
|
| | | } |
| | | if (logMode == WriteLogMode.YES) |
| | | { |
| | | HdlLogLogic.Current.WriteLog(-1, zbGateway.GwIP + " 向网关设置住宅ID失败"); |
| | | }
|
| | | return -1;
|
| | | }
|
| | |
| | | }
|
| | |
|
| | | //刷新的是缓存,不刷新真实物理网关
|
| | | this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage;
|
| | | this.dicGateway[gwID].OnlineTime = DateTime.Now; |
| | | //将网关的数据设置到本地缓存中
|
| | | this.SetGatewayDataToLocalMemmory(this.dicGateway[gwID], result);
|
| | | //顺便这个变量也设置一下
|
| | |
| | | /// <summary>
|
| | | /// 设置住宅ID到网关(失败时,不弹出任何错误信息,网关断网除外)
|
| | | /// </summary>
|
| | | /// <param name="zbGateway"></param>
|
| | | /// <param name="i_gwId"></param> |
| | | /// <param name="HomeId"></param>
|
| | | /// <returns></returns> |
| | | public bool SetHomeIdToGateway(ZbGateway zbGateway, string HomeId, ShowErrorMode mode)
|
| | | public bool SetHomeIdToGateway(string i_gwId, string HomeId, ShowErrorMode mode) |
| | | {
|
| | | //账号ID
|
| | | string accountId = string.Empty;
|
| | |
| | | var data = new Newtonsoft.Json.Linq.JObject { { "HomeId", HomeId }, { "AccountId", accountId } };
|
| | | jObject.Add("Data", data);
|
| | | //住宅ID的设置,固定使用局域网,不存在远程的说法
|
| | | var result = this.SendJobjectDataToGateway(zbGateway, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
|
| | | var result = this.SendJobjectDataToGateway(i_gwId, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true); |
| | | if (result.ErrorMsgDiv == -1)
|
| | | {
|
| | | if (mode == ShowErrorMode.YES)
|
| | |
| | | }
|
| | |
|
| | | //设置住宅ID到网关
|
| | | bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, ShowErrorMode.YES);
|
| | | bool flage2 = this.SetHomeIdToGateway(zbGateway.GwId, Common.Config.Instance.HomeId, ShowErrorMode.YES); |
| | | if (flage2 == false)
|
| | | {
|
| | | //向网关设置住宅ID失败
|
| | |
| | | }
|
| | |
|
| | | //刷新的是缓存,不刷新真实物理网关
|
| | | this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage;
|
| | | this.dicGateway[gwID].OnlineTime = DateTime.Now; |
| | | //将网关的数据设置到本地缓存中
|
| | | this.SetGatewayDataToLocalMemmory(this.dicGateway[gwID], result);
|
| | | //顺便这个变量也设置一下
|
| | |
| | |
|
| | | //添加网关的话,强制主页刷新
|
| | | UserView.UserPage.Instance.RefreshAllForm = true;
|
| | | |
| | | if (Config.Instance.Home.TemplateMode == 2) |
| | | { |
| | | //如果这个类型的网关只有一个模板,则自动匹配 |
| | | var dicName = TemplateData.TemplateCommonLogic.Current.GetGatewayCanSelectTemplateName(zbGateway); |
| | | if (dicName.Count == 1) |
| | | { |
| | | foreach (var tempId in dicName.Keys) |
| | | { |
| | | TemplateData.TemplateCommonLogic.Current.AddGatewayTemplateSelect(zbGateway.GwId, tempId); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | |
|
| | | return 1; |
| | | } |
| | |
| | | var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 91 } };
|
| | | var data = new Newtonsoft.Json.Linq.JObject { { "GwName", gatewayName } };
|
| | | jObject.Add("Data", data);
|
| | | var result = this.SendJobjectDataToGateway(zbGateway, "GwReName", jObject.ToString(), "GwReName_Respon");
|
| | | var result = this.SendJobjectDataToGateway(zbGateway.GwId, "GwReName", jObject.ToString(), "GwReName_Respon"); |
| | | if (result.ErrorMsg != null)
|
| | | {
|
| | | this.ShowTipMsg(result.ErrorMsg);
|
| | |
| | | }
|
| | |
|
| | | //获取全部设备
|
| | | int result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay, false);
|
| | | int result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay.GwId, false); |
| | | //关闭进度条
|
| | | ProgressBar.Close();
|
| | | if (result == -1)
|
| | |
| | | //清空网关的住宅ID 网关解绑失败 不理它,因为网关可以按按键强制搜索得到
|
| | | if (realWay != null)
|
| | | {
|
| | | this.SetHomeIdToGateway(realWay, string.Empty, ShowErrorMode.NO);
|
| | | this.SetHomeIdToGateway(realWay.GwId, string.Empty, ShowErrorMode.NO); |
| | | }
|
| | |
|
| | | //删除云端的网关
|
| | |
| | | //断开mqtt连接 |
| | | if (realWay != null)
|
| | | {
|
| | | realWay.DisConnectLocalMqttClient("GD");
|
| | | realWay.CloseLocalConnectionOnForce(); |
| | | } |
| | | |
| | | return true; |
| | |
| | | //虚拟物理网关
|
| | | listRealWay.Add(listGateway[i]);
|
| | | }
|
| | | //标识指定网关为不在线
|
| | | listRealWay[i].GatewayOnlineFlage = false;
|
| | | }
|
| | |
|
| | | //这是第一道坎,强制检查WIFI:等待2秒(因为wifi的时候,它会自动去刷新flage)
|
| | | System.Threading.Thread.Sleep(2000);
|
| | | //检查是否拥有网关存在于WIFi下
|
| | | if (this.CheckHadGatewayInWifi(listRealWay) == false)
|
| | | {
|
| | | //第二道坎:在远程的情况下刷新网关的在线状态
|
| | | this.RefreshGatewayOnlineOnRemode(listRealWay);
|
| | | }
|
| | |
|
| | | //刷新缓存的在线标识
|
| | | foreach (var zbway in listRealWay)
|
| | | {
|
| | | string gwID = zbway.GwId;
|
| | | if (this.dicGateway.ContainsKey(gwID) == false)
|
| | | {
|
| | | continue;
|
| | | }
|
| | | this.dicGateway[gwID].GatewayOnlineFlage = zbway.GatewayOnlineFlage;
|
| | | }
|
| | | //wifi情况下,由Application自己刷新标识 |
| | | } |
| | | |
| | | /// <summary>
|
| | |
| | | {
|
| | | foreach (var zbway in listGateway)
|
| | | {
|
| | | //是否存在网关存在于WIFI下
|
| | | if (zbway.GatewayOnlineFlage == true)
|
| | | //如果广播得到网关,则会刷新OnlineTime,如果与当前时间相差少于指定秒数,则判断为存在网关存在于WIFI下 |
| | | if ((DateTime.Now - zbway.OnlineTime).TotalSeconds <= ZigBee.Common.Application.SendTime * 2) |
| | | {
|
| | | return true;
|
| | | }
|
| | |
| | | private void RefreshGatewayOnlineOnRemode(List<ZbGateway> listGateway)
|
| | | {
|
| | | //获取云端上面的网关
|
| | | Dictionary<string, GatewayResult> dicDbGateway = HdlGatewayLogic.Current.GetGateWayListFromDataBase();
|
| | | Dictionary<string, GatewayResult> dicDbGateway = this.GetGateWayListFromDataBase(); |
| | | if (dicDbGateway == null)
|
| | | {
|
| | | return;
|
| | |
| | | continue;
|
| | | }
|
| | | string strId = way.GwId;
|
| | | if (dicDbGateway.ContainsKey(strId) == true) //如果云端上面有这个网关
|
| | | if (dicDbGateway.ContainsKey(strId) == true |
| | | && dicDbGateway[strId].MqttOnlineStatus == true) //如果云端上面有这个网关 |
| | | {
|
| | | way.GatewayOnlineFlage = dicDbGateway[strId].MqttOnlineStatus;
|
| | | way.OnlineTime = DateTime.Now; |
| | | }
|
| | | }
|
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 根据某种标识判断指定网关是否在线
|
| | | /// 根据本地缓存判断指定网关是否在线 |
| | | /// </summary>
|
| | | /// <param name="zbGateway"></param>
|
| | | /// <returns></returns> |
| | | public bool CheckGatewayOnlineByFlag(ZbGateway zbGateway)
|
| | | public bool CheckGatewayOnlineByMemory(ZbGateway zbGateway) |
| | | {
|
| | | if (zbGateway == null)
|
| | | {
|
| | |
| | | string gwID = zbGateway.GwId;
|
| | | if (this.dicGateway.ContainsKey(gwID) == true)
|
| | | {
|
| | | return this.dicGateway[gwID].GatewayOnlineFlage;
|
| | | return this.CheckGatewayOnlineByFlage(dicGateway[gwID]); |
| | | }
|
| | |
|
| | | return zbGateway.GatewayOnlineFlage;
|
| | | return this.CheckGatewayOnlineByFlage(zbGateway); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 刷新网关的在线时间点 |
| | | /// </summary> |
| | | /// <param name="i_gatewayId">网关ID</param> |
| | | public void RefreshGatewayOnlineTime(string i_gatewayId) |
| | | { |
| | | try |
| | | { |
| | | if (this.dicGateway.ContainsKey(i_gatewayId) == true) |
| | | { |
| | | this.dicGateway[i_gatewayId].OnlineTime = DateTime.Now; |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据指定标识,判断网关是否在线 |
| | | /// </summary> |
| | | /// <param name="zbGateway"></param> |
| | | /// <returns></returns> |
| | | private bool CheckGatewayOnlineByFlage(ZbGateway zbGateway) |
| | | { |
| | | if (this.nowGwConnectMode == GatewayConnectMode.Remote) |
| | | { |
| | | //远程每20秒检测一次 |
| | | return (DateTime.Now - zbGateway.OnlineTime).TotalSeconds <= 40; |
| | | } |
| | | //如果广播得到网关,则会刷新OnlineTime,如果与当前时间相差少于10秒,则判断为在线 |
| | | return (DateTime.Now - zbGateway.OnlineTime).TotalSeconds <= 15; |
| | | } |
| | | |
| | | #endregion |
| | |
| | | {
|
| | | //获取网关版本信息
|
| | | var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 95 } };
|
| | | var result = this.SendJobjectDataToGateway(zbGateway, "GetZbGwInfo", jObject.ToString(), "GetZbGwInfo_Respon", 5, useLocalConnect);
|
| | | var result = this.SendJobjectDataToGateway(zbGateway.GwId, "GetZbGwInfo", jObject.ToString(), "GetZbGwInfo_Respon", 5, useLocalConnect); |
| | |
|
| | | if (result.ErrorMsgDiv != 1)
|
| | | {
|
| | |
| | | /// <param name="saveFile">是否保存文件</param>
|
| | | private void SetGatewayDataToLocalMemmory(ZbGateway localWay, ZbGatewayData.GetGwData data, bool saveFile = true)
|
| | | {
|
| | | if (data == null) { return; } |
| | | |
| | | localWay.GwId = data.GwId;
|
| | | localWay.GwName = data.GwName;
|
| | | localWay.GwSerialNum = data.GWSN;
|
| | | localWay.IsMainGateWay = data.IsDominant == 1 ? true : false;
|
| | | localWay.GwIP = data.GwIP;
|
| | | localWay.GwMac = data.GwMac; |
| | | localWay.LinuxImageType = data.LinuxImageType;
|
| | | localWay.LinuxHardVersion = data.LinuxHWVersion;
|
| | | localWay.LinuxFirmwareVersion = data.LinuxFWVersion;
|
| | |
| | | localWay.CoordinatorImageId = data.ZbImageType;
|
| | | localWay.DriveCodeList = data.DriveCodeList;
|
| | | localWay.GwVersionDate = data.AppVersion;
|
| | | localWay.OnlineTime = DateTime.Now; |
| | | |
| | | if (saveFile == true)
|
| | | {
|
| | | localWay.ReSave();
|
| | | }
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 网关房间相关_______________________
|
| | |
|
| | | /// <summary>
|
| | | /// 获取网关所在的房间
|
| | | /// </summary>
|
| | | /// <param name="zbGateway">网关对象</param>
|
| | | /// <returns></returns>
|
| | | public Room GetRoomByGateway(ZbGateway zbGateway)
|
| | | {
|
| | | return this.GetRoomByGateway(zbGateway.GwId);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 获取网关所在的房间
|
| | | /// </summary>
|
| | | /// <param name="gatewayId">网关ID</param>
|
| | | /// <returns></returns>
|
| | | public Room GetRoomByGateway(string gatewayId)
|
| | | {
|
| | | var localGateway = this.GetLocalGateway(gatewayId);
|
| | | if (localGateway == null)
|
| | | {
|
| | | return null;
|
| | | }
|
| | | return HdlRoomLogic.Current.GetRoomById(localGateway.RoomId);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 变更网关房间
|
| | | /// </summary>
|
| | | /// <param name="gwId">网关Id</param>
|
| | | /// <param name="roomId">房间ID</param>
|
| | | public void ChangedGatewayRoom(string gwId, string roomId)
|
| | | {
|
| | | var localGateway = this.GetLocalGateway(gwId);
|
| | | if (localGateway != null)
|
| | | {
|
| | | localGateway.RoomId = roomId;
|
| | | localGateway.ReSave();
|
| | | //添加备份
|
| | | HdlAutoBackupLogic.AddOrEditorFile(localGateway.FilePath);
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | //断开mqtt连接(即使保存在内存当中也没问题,因为如果广播不到,则它不会建立链接)
|
| | | for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
|
| | | {
|
| | | ZbGateway.GateWayList[i].DisConnectLocalMqttClient("G");
|
| | | ZbGateway.GateWayList[i].CloseLocalConnectionOnForce(); |
| | | }
|
| | | //加载本地网关对象
|
| | | if (roadGateway == true)
|
| | |
| | | list.Add(coordinatorFirmware);
|
| | |
|
| | | //这个网关需要有虚拟驱动这个东西才行
|
| | | if (localWay.LinuxImageType != 6)
|
| | | if (this.CheckGatewayHadDriveCode(localWay) == true) |
| | | {
|
| | | //虚拟驱动号
|
| | | foreach (var data in localWay.DriveCodeList)
|
| | |
| | | return localgw != null ? zbGateway.DriveCodeList : zbGateway.DriveCodeList;
|
| | | }
|
| | | var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 505 } };
|
| | | var result = this.SendJobjectDataToGateway(zbGateway, "VirtualDrive/CatDriveCode", jObject.ToString(), "VirtualDrive/CatDriveCode_Respon");
|
| | | var result = this.SendJobjectDataToGateway(zbGateway.GwId, "VirtualDrive/CatDriveCode", jObject.ToString(), "VirtualDrive/CatDriveCode_Respon"); |
| | | if (result.ErrorMsg != null)
|
| | | {
|
| | | this.ShowTipMsg(result.ErrorMsg);
|
| | |
| | | #region ■ 设置网关图片_______________________
|
| | |
|
| | | /// <summary>
|
| | | /// 设置真实网关的图片
|
| | | /// 设置真实网关的图片(已废弃) |
| | | /// </summary>
|
| | | /// <param name="button"></param>
|
| | | /// <param name="zbGateway"></param>
|
| | |
| | | {
|
| | | if (zbGateway.LinuxImageType != -1)
|
| | | {
|
| | | button.UnSelectedImagePath = "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png";
|
| | | this.SetgatewayIcon(button, "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png"); |
| | | }
|
| | | else
|
| | | {
|
| | |
| | | this.SetGatewayDataToLocalMemmory(zbGateway, result, false);
|
| | | HdlThreadLogic.Current.RunMain(() =>
|
| | | {
|
| | | button.UnSelectedImagePath = "Gateway/GatewayIcon" + result.LinuxImageType + ".png";
|
| | | this.SetgatewayIcon(button, "Gateway/GatewayIcon" + result.LinuxImageType + ".png"); |
| | | });
|
| | | }
|
| | | });
|
| | |
| | | }
|
| | | else
|
| | | {
|
| | | button.UnSelectedImagePath = "Gateway/GatewayIcon" + localWay.LinuxImageType + ".png";
|
| | | this.SetgatewayIcon(button, "Gateway/GatewayIcon" + localWay.LinuxImageType + ".png"); |
| | | }
|
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置网关图标 |
| | | /// </summary> |
| | | /// <param name="button"></param> |
| | | /// <param name="i_IconName"></param> |
| | | public void SetgatewayIcon(Button button, string i_IconName) |
| | | { |
| | | //这个图片本地是否存在? |
| | | if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(i_IconName)) == true) |
| | | { |
| | | //则使用共通图片 |
| | | i_IconName = "Gateway/GatewayIcon.png"; |
| | | } |
| | | button.UnSelectedImagePath = i_IconName; |
| | | }
|
| | |
|
| | | #endregion
|
| | |
| | | /// <summary>
|
| | | /// 重新发送命令去绑定断网情况下备份的网关
|
| | | /// </summary>
|
| | | public void ResetComandToBindBackupGateway()
|
| | | {
|
| | | HdlThreadLogic.Current.RunThread(() =>
|
| | | public bool ResetComandToBindBackupGateway() |
| | | {
|
| | | var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile);
|
| | | if (fileData == null)
|
| | | {
|
| | | return;
|
| | | return true; |
| | | }
|
| | | this.listBackupGwId = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(System.Text.Encoding.UTF8.GetString(fileData));
|
| | | var listTempId = new List<string>();
|
| | |
| | | {
|
| | | System.IO.File.Delete(file);
|
| | | }
|
| | | return true; |
| | | }
|
| | | else
|
| | | {
|
| | | //备份
|
| | | HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId);
|
| | | return false; |
| | | }
|
| | | });
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
| | | /// <param name="waitTime">超时时间(秒)</param>
|
| | | /// <param name="useLocalConnect">是否强制使用本地连接发送</param>
|
| | | /// <returns>网关返回的数据</returns>
|
| | | public ReceiptGatewayResult SendJobjectDataToGateway(ZbGateway gateway, string sendTopic, string sendData, string receiptTopic, int waitTime = 5, bool useLocalConnect = false)
|
| | | public ReceiptGatewayResult SendJobjectDataToGateway(string gatewayId, string sendTopic, string sendData, string receiptTopic, int waitTime = 5, bool useLocalConnect = false) |
| | | {
|
| | | var reResult = new ReceiptGatewayResult();
|
| | |
|
| | | ZbGateway myGateway = null;
|
| | | if (this.GetRealGateway(ref myGateway, gateway) == false)
|
| | | {
|
| | | //获取网关对象失败
|
| | | reResult.ErrorMsg = Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail);
|
| | | reResult.ErrorMsgDiv = -1;
|
| | | return reResult;
|
| | | }
|
| | | //网关ID
|
| | | string gatewayID = gateway.GwId;
|
| | | //错误主题
|
| | | string errorTopic = gatewayID + "/" + "Error_Respon";
|
| | | string errorTopic = gatewayId + "/" + "Error_Respon"; |
| | | //检测对象的主题
|
| | | string checkTopic = gatewayID + "/" + receiptTopic;
|
| | | string checkTopic = gatewayId + "/" + receiptTopic; |
| | |
|
| | | Action<string, string> receiptAction = (topic, message) =>
|
| | | HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) => |
| | | {
|
| | | var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
|
| | |
|
| | |
| | | {
|
| | | reResult.ReceiptData = jobject["Data"].ToString();
|
| | | }
|
| | | };
|
| | | myGateway.Actions += receiptAction;
|
| | | }); |
| | | |
| | | //发送数据
|
| | | if (useLocalConnect == false)
|
| | | {
|
| | | myGateway.Send(sendTopic, sendData);
|
| | | }
|
| | | else
|
| | | {
|
| | | myGateway.SendLocation(sendTopic, System.Text.Encoding.UTF8.GetBytes(sendData));
|
| | | }
|
| | | this.SendJobjectData(gatewayId, sendTopic, sendData, useLocalConnect); |
| | |
|
| | | //超时时间
|
| | | int TimeOut = 0;
|
| | |
| | | System.Threading.Thread.Sleep(50);
|
| | | TimeOut++;
|
| | | }
|
| | | myGateway.Actions -= receiptAction;
|
| | | receiptAction = null;
|
| | | //移除网关监听事件 |
| | | HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent(); |
| | | |
| | | if (reResult.ReceiptData == null)
|
| | | {
|
| | | reResult.ErrorMsgDiv = 0;
|
| | | }
|
| | |
|
| | | return reResult;
|
| | | } |
| | | |
| | | /// <summary> |
| | | /// 单纯执行发送json数据到网关 |
| | | /// </summary> |
| | | /// <param name="i_gatewayId">网关id</param> |
| | | /// <param name="topic">主题</param> |
| | | /// <param name="sendData">发送的json数据</param> |
| | | /// <param name="useLocalConnect">是否强制使用本地连接发送</param> |
| | | public void SendJobjectData(string i_gatewayId, string topic, string sendData, bool useLocalConnect = false) |
| | | { |
| | | ZbGateway realWay = null; |
| | | for (int i = 0; i < ZbGateway.GateWayList.Count; i++) |
| | | { |
| | | //寻找真实物理网关对象 |
| | | if (ZbGateway.GateWayList[i].GwId == i_gatewayId) |
| | | { |
| | | realWay = ZbGateway.GateWayList[i]; |
| | | break; |
| | | } |
| | | } |
| | | if (realWay == null) |
| | | { |
| | | //如果网关对象丢失了,则创建个新的 |
| | | realWay = new ZbGateway { IsVirtual = true, }; |
| | | realWay.GwId = i_gatewayId; |
| | | realWay.HomeId = Config.Instance.HomeId; |
| | | ZbGateway.GateWayList.Add(realWay); |
| | | } |
| | | if (useLocalConnect == false) |
| | | { |
| | | realWay.Send(topic, System.Text.Encoding.UTF8.GetBytes(sendData)); |
| | | } |
| | | else |
| | | { |
| | | //强制使用本地连接 |
| | | realWay.SendLocation(topic, System.Text.Encoding.UTF8.GetBytes(sendData)); |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 更换网关___________________________ |
| | | |
| | | /// <summary> |
| | | /// 执行更换网关(里面是子线程执行) |
| | | /// </summary> |
| | | /// <param name="i_realGateway">新网关</param> |
| | | /// <param name="i_targetGwId">被替换的目标网关</param> |
| | | /// <param name="i_info">备份数据</param> |
| | | /// <param name="finishEvent">-1:异常失败 0:没有备份数据 1:成功</param> |
| | | public void DoReplaceGateway(ZbGateway i_realGateway, string i_targetGwId, BackupListNameInfo i_info, Action<int> finishEvent) |
| | | { |
| | | HdlThreadLogic.Current.RunThread(() => |
| | | { |
| | | //正在获取配置数据 |
| | | ProgressFormBar.Current.Start(); |
| | | ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGettingConfigurationData)); |
| | | |
| | | System.Threading.Thread.Sleep(1000); |
| | | |
| | | //首先的更新云端 |
| | | |
| | | //然后发数据给网关,让网关去下载数据 |
| | | var result = HdlGatewayBackupLogic.Current.RecoverGateway(i_realGateway, i_info.Id, i_info.IsAutoBack, false); |
| | | if (result == false) |
| | | { |
| | | finishEvent?.Invoke(-1); |
| | | return; |
| | | } |
| | | //正在变更本地配置 |
| | | ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uChangingLocalConfiguration)); |
| | | ProgressFormBar.Current.ResetProgressBar(); |
| | | |
| | | System.Threading.Thread.Sleep(1500); |
| | | |
| | | //格式化旧网关 |
| | | string strComand = "{\"Cluster_ID\":0,\"Command\":160,\"Data\": {\"DelAllInfo\": 1}}"; |
| | | this.SendJobjectData(i_targetGwId, "Formate", strComand, true); |
| | | |
| | | //删除掉被替换的网关 |
| | | this.DeleteGatewayFile(i_targetGwId); |
| | | ProgressFormBar.Current.SetValue(1); |
| | | |
| | | System.Threading.Thread.Sleep(500); |
| | | ProgressFormBar.Current.Close(); |
| | | |
| | | finishEvent?.Invoke(1); |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 在网关替换之前,获取网关的一个备份数据 |
| | | /// </summary> |
| | | /// <param name="i_gwId">网关ID</param> |
| | | /// <param name="statu">-1:异常失败 0:没有备份数据 1:成功</param> |
| | | /// <returns></returns> |
| | | private BackupListNameInfo GetBackupInfoBeforeReplace(string i_gwId, ref int statu) |
| | | { |
| | | //获取自动备份的数据 |
| | | var pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(3, i_gwId); |
| | | if (pageData == null) |
| | | { |
| | | statu = -1; |
| | | return null; |
| | | } |
| | | //如果获取不到自动备份,则去获取手动备份 |
| | | if (pageData.Count == 0) |
| | | { |
| | | //从云端获取数据 |
| | | pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(2, i_gwId); |
| | | if (pageData == null) |
| | | { |
| | | statu = -1; |
| | | return null; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | //自动备份 |
| | | pageData[0].IsAutoBack = 1; |
| | | } |
| | | if (pageData.Count == 0) |
| | | { |
| | | //没有备份 |
| | | statu = 0; |
| | | return null; |
| | | } |
| | | statu = 1; |
| | | return pageData[0]; |
| | | }
|
| | |
|
| | | #endregion
|
| | |
| | | }
|
| | | else if (this.nowGwConnectMode == GatewayConnectMode.WIFI)
|
| | | {
|
| | | //局域网每5秒检测一次
|
| | | if (waitCount < 5) { continue; }
|
| | | //局域网每8秒检测一次 |
| | | if (waitCount < 8) { continue; } |
| | | }
|
| | | waitCount = 0;
|
| | |
|
| | |
| | | private void CheckGatewayStatuByWIFI(Dictionary<string, bool> dicOldOnline)
|
| | | {
|
| | | //从网关获取全部的网关
|
| | | List<ZbGateway> list = this.GetAllGatewayFromGateway();
|
| | | foreach (var way in list)
|
| | | {
|
| | | //将标识置为false
|
| | | way.GatewayOnlineFlage = false;
|
| | | }
|
| | | //等个2秒
|
| | | System.Threading.Thread.Sleep(2000);
|
| | | List<ZbGateway> list = this.GetAllLocalGateway(); |
| | |
|
| | | //2020.05.25追加:此住宅是否拥有网关在线
|
| | | var hadGwOnline = false;
|
| | | foreach (var way in list)
|
| | | {
|
| | | if (dicOldOnline.ContainsKey(way.GwId) == true)
|
| | | {
|
| | | if (way.GatewayOnlineFlage == true)
|
| | | if (dicOldOnline.ContainsKey(way.GwId) == true |
| | | && this.CheckGatewayOnlineByFlage(way) == true) |
| | | {
|
| | | //有一个网关在线,即在线
|
| | | hadGwOnline = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | if (dicOldOnline.ContainsKey(gwId) == true)
|
| | | {
|
| | | //网关也不多,直接推送吧
|
| | | this.PushGatewayOnlineStatuToForm(gwId, way.GatewayOnlineFlage, hadGwOnline);
|
| | | bool online = this.CheckGatewayOnlineByFlage(way); |
| | | this.PushGatewayOnlineStatuToForm(gwId, online, hadGwOnline); |
| | | }
|
| | | else
|
| | | {
|
| | |
| | | private void CheckGatewayStatuByRemote(Dictionary<string, bool> dicOldOnline)
|
| | | {
|
| | | //获取云端上面的网关
|
| | | Dictionary<string, GatewayResult> dicDbGateway = HdlGatewayLogic.Current.GetGateWayListFromDataBase();
|
| | | Dictionary<string, GatewayResult> dicDbGateway = this.GetGateWayListFromDataBase(); |
| | | if (dicDbGateway == null)
|
| | | {
|
| | | //如果网络不通,则也往下走
|
| | |
| | | foreach (var zbway in this.dicGateway.Values)
|
| | | {
|
| | | //获取前回的在线状态
|
| | | dicOldOnline[zbway.GwId] = zbway.GatewayOnlineFlage;
|
| | | dicOldOnline[zbway.GwId] = this.CheckGatewayOnlineByFlage(zbway); |
| | | }
|
| | | return dicOldOnline;
|
| | | }
|
| | |
| | | if (zbway != null)
|
| | | {
|
| | | //切换住宅时,这个东西有可能是null
|
| | | zbway.GatewayOnlineFlage = online;
|
| | | if (online == true) |
| | | { |
| | | zbway.OnlineTime = DateTime.Now; |
| | | } |
| | | UserCenterResourse.DicActionForm[formId]?.GatewayOnlinePush(zbway, online, hadGwOnline);
|
| | | }
|
| | | }
|
| | |
| | | #region ■ 一般方法___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 是否是mini网关 |
| | | /// </summary> |
| | | /// <param name="i_zbGateway">网关对象</param> |
| | | /// <returns></returns> |
| | | public bool IsMiniGateway(ZbGateway i_zbGateway) |
| | | { |
| | | if (i_zbGateway == null) { return false; } |
| | | |
| | | if (i_zbGateway.LinuxImageType == 11 || i_zbGateway.LinuxImageType == 13) |
| | | { |
| | | //这个网关是mini网关 |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 判断网关是否拥有虚拟驱动 |
| | | /// </summary> |
| | | /// <param name="i_zbGateway">网关对象</param> |
| | | /// <returns></returns> |
| | | public bool CheckGatewayHadDriveCode(ZbGateway i_zbGateway) |
| | | { |
| | | if (i_zbGateway == null) { return false; } |
| | | |
| | | if (i_zbGateway.LinuxImageType != 6 && i_zbGateway.LinuxImageType != 12) |
| | | { |
| | | //这个网关没有虚拟驱动 |
| | | return false; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 显示错误信息窗口
|
| | | /// </summary>
|
| | | /// <param name="msg"></param>
|