| | |
| | | //添加网关的话,强制主页刷新 |
| | | 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; |
| | | }
|
| | |
|
| | |
| | | }
|
| | |
|
| | | //刷新的是缓存,不刷新真实物理网关
|
| | | this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage;
|
| | | this.dicGateway[gwID].OnlineTime = DateTime.Now;
|
| | | //将网关的数据设置到本地缓存中
|
| | | this.SetGatewayDataToLocalMemmory(this.dicGateway[gwID], result);
|
| | | //顺便这个变量也设置一下
|
| | |
| | | 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(zbGateway.GwId, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true);
|
| | | if (result.ErrorMsgDiv == -1)
|
| | | {
|
| | | if (mode == ShowErrorMode.YES)
|
| | |
| | | }
|
| | |
|
| | | //刷新的是缓存,不刷新真实物理网关
|
| | | 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)
|
| | |
| | | //虚拟物理网关
|
| | | 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)
|
| | | if (this.CheckGatewayOnlineByFlage(zbway) == true)
|
| | | {
|
| | | 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="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)
|
| | | {
|
| | |
| | | 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();
|
| | |
| | | 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);
|
| | |
| | | /// <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
|
| | |
| | | }
|
| | | else if (this.nowGwConnectMode == GatewayConnectMode.WIFI)
|
| | | {
|
| | | //局域网每5秒检测一次
|
| | | if (waitCount < 5) { continue; }
|
| | | //局域网每8秒检测一次
|
| | | if (waitCount < 8) { continue; }
|
| | | }
|
| | | waitCount = 0;
|
| | |
|
| | |
| | | {
|
| | | //从网关获取全部的网关
|
| | | List<ZbGateway> list = this.GetAllGatewayFromGateway();
|
| | | foreach (var way in list)
|
| | | {
|
| | | //将标识置为false
|
| | | way.GatewayOnlineFlage = false;
|
| | | }
|
| | | //等个2秒
|
| | | System.Threading.Thread.Sleep(2000);
|
| | |
|
| | | //2020.05.25追加:此住宅是否拥有网关在线
|
| | | var hadGwOnline = false;
|
| | | foreach (var way in list)
|
| | | {
|
| | | if (dicOldOnline.ContainsKey(way.GwId) == true)
|
| | | if (dicOldOnline.ContainsKey(way.GwId) == true
|
| | | && this.CheckGatewayOnlineByFlage(way) == true)
|
| | | {
|
| | | if (way.GatewayOnlineFlage == true)
|
| | | {
|
| | | //有一个网关在线,即在线
|
| | | hadGwOnline = true;
|
| | | break;
|
| | | }
|
| | | //有一个网关在线,即在线
|
| | | 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);
|
| | | }
|
| | | }
|