Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
        private async void btnUpdateDataItems_Click(object sender, RoutedEventArgs e)
        {
            string message;

            if (!opcUaClientHelper.IsConnected)
            {
                message = DateTime.Now.ToString(dateString) + "请先连接服务器" + System.Environment.NewLine;
                asyncUpMessage(message);
                return;
            }
            //var strList = txtOpcDataItems.Text.Split('\r', '\n');

            //List<OpcUaDataItem> opcUaDataItems = new List<OpcUaDataItem>(strList.Count());
            ////txtOpcDataItems .Text.Split (System.Environment.NewLine ):
            //foreach (var strOpcDataItem in strList)
            //{
            //    var strOpcDataItemTmp = strOpcDataItem.Split(';');
            //    if (strOpcDataItemTmp.Count() < 2)
            //    {
            //        continue;
            //    }
            //    OpcDataItem opcDataItem =
            //        new OpcDataItem(strOpcDataItemTmp[0], int.Parse(strOpcDataItemTmp[1]), strOpcDataItemTmp[2], strOpcDataItemTmp[3], (OpcResult)Enum.Parse(typeof(OpcResult), strOpcDataItemTmp[4]));
            //    opcDataItems.Add(opcDataItem);
            //}
            //opcClienthelper.RegisterOpcDataItemsAsync(opcDataItems);
            //dataGridDataSource = new ObservableCollection<OpcDataItem>(opcDataItems);
            //gvOpcUaDataItems.ItemsSource = dataGridDataSource;
            //this.txtb.Text = "(" + dataGridDataSource.Count(a => a.Quality == OpcResult.S_OK) + "/" + dataGridDataSource.Count() + ")";
            var opcUaStatusCodes = await opcUaClientHelper.RegisterNodes(opcUaDataItems.ToList());

            string msg = $"{ DateTime.Now.ToString(dateString)}订阅数据结果:{opcUaStatusCodes}{System.Environment.NewLine}";

            asyncUpMessage(msg);
            gvOpcUaDataItems.ItemsSource = opcUaDataItems;
        }