/// <summary> /// 开启设备调度服务,重复调用时将动态更新设备和相关的点表 /// </summary> /// <returns></returns> private async Task <bool> StartDispatchingService() { try { LogHelper.Logger.Info("慧远调度服务开始启动"); foreach (var opcServer in dbContext.OpcServerDb.GetList(a => a.IsEnable == true && a.OpcType == Models.OpcType.OpcUa)) { //已经有此ua服务器,那么更新节点 if (OpcUaClientHelperList.Any(a => a.Name == opcServer.Name)) { var opcUaClientHelper = OpcUaClientHelperList.FirstOrDefault(a => a.Name == opcServer.Name); var nodes = dbContext.DeviceNodeDb.GetList(a => a.IsEnable == true && a.OpcServerId == opcServer.Id); var nd = from a in nodes select new OpcUaHelper.OpcUaDataItem { Name = a.Name.StartsWith("ns=2;s=") ? a.Name : "ns=2;s=" + a.Name, UpdateRate = a.UpdateRate, ValueType = ((TypeCode)a.DataType).ToType(), OpcUaStatusCodes = OpcUaStatusCodes.Bad }; var result = await opcUaClientHelper.RegisterNodes(nd.ToList()); LogHelper.Logger.Info($"更新节点结果:{result},{opcServer.Name },{opcServer.Uri}"); } else//没有此ua服务器,那么新增 { var nodes = dbContext.DeviceNodeDb.GetList(a => a.IsEnable == true && a.OpcServerId == opcServer.Id); OpcUaHelper.OpcUaClientHelper opcUaClientHelper = new OpcUaHelper.OpcUaClientHelper(); OpcUaClientHelperList.Add(opcUaClientHelper); opcUaClientHelper.Name = opcServer.Name; opcUaClientHelper.ServerUri = opcServer.Uri; opcUaClientHelper.OnLogHappened += OpcUaClientHelper_OnLogHappened; opcUaClientHelper.OnErrorHappened += OpcUaClientHelper_OnErrorHappened; opcUaClientHelper.OnDataChanged += OpcUaClientHelper_OnDataChanged; DispatchingHandler = new dispatchingDelegate(dispatchingExecute); OpcUaHelper.OpcUaStatusCodes opcUaStatusCodes = await opcUaClientHelper.ConnectAsync(); LogHelper.Logger.Info($"慧远调度服务启动结果:{opcUaStatusCodes},{opcServer.Name },{opcServer.Uri}"); var nd = from a in nodes select new OpcUaHelper.OpcUaDataItem { Name = a.Name.StartsWith("ns=2;s=") ? a.Name : "ns=2;s=" + a.Name, UpdateRate = a.UpdateRate, ValueType = ((TypeCode)a.DataType).ToType(), OpcUaStatusCodes = OpcUaStatusCodes.Bad }; await opcUaClientHelper.RegisterNodes(nd.ToList()); } } LogHelper.Logger.Info("慧远调度服务启动成功"); return(true); } catch (Exception ex) { LogHelper.Logger.Error("", ex); return(false); } }
public Type type2; //= dateTime.GetType(); #endregion #region 舒适化数据和变量 /// <summary> /// 初始化ua实例 /// </summary> private void intiOpcUaClientHelper() { opcUaClientHelper = new OpcUaClientHelper(); opcUaClientHelper.ServerUri = cboxOpcServers.Text; opcUaClientHelper.OnLogHappened += OpcUaClienthelper_OnLogHappened; opcUaClientHelper.OnErrorHappened += OpcUaClienthelper_OnErrorHappened; opcUaClientHelper.OnDataChanged += OpcUaClienthelper_OnDataChanged; }