public async Task Setup()
        {
            this.referenceDescriptionCollection = new ReferenceDescriptionCollection()
            {
                new ReferenceDescription()
                {
                    NodeId = new ExpandedNodeId(new NodeId(0))
                },
                new ReferenceDescription()
                {
                    NodeId = new ExpandedNodeId(new NodeId(1))
                },
                new ReferenceDescription()
                {
                    NodeId = new ExpandedNodeId(new NodeId(2))
                }
            };

            this.statusBarViewModel = new Mock <IStatusBarControlViewModel>();
            this.statusBarViewModel.Setup(x => x.Append(It.IsAny <string>(), It.IsAny <StatusBarMessageSeverity>()));

            this.reconnectHandler = new Mock <IOpcSessionReconnectHandler>();
            this.reconnectHandler.Setup(x => x.Activate());
            this.reconnectHandler.Setup(x => x.Deactivate());
            this.reconnectHandler.Setup(x => x.BeginReconnect(It.IsAny <Session>(), It.IsAny <EventHandler>(), It.IsAny <int>()));

            this.sessionHandler = new Mock <IOpcSessionHandler>();
            this.sessionHandler.Setup(x => x.NamespaceUris).Returns(new NamespaceTable());
            this.sessionHandler.Setup(x => x.KeepAliveStopped).Returns(false);
            this.sessionHandler.Setup(x => x.DefaultSubscription).Returns(new Subscription());
            this.sessionHandler.Setup(x => x.AddSubscription(It.IsAny <Subscription>())).Returns(true);
            this.sessionHandler.Setup(x => x.RemoveSubscription(It.IsAny <Subscription>())).Returns(true);
            this.sessionHandler.Setup(x => x.ClearSubscriptions()).Returns(true);
            this.sessionHandler.Setup(x => x.FetchReferences(It.IsAny <NodeId>())).Returns(this.referenceDescriptionCollection);
            this.sessionHandler.Setup(x => x.SetSession(this.reconnectHandler.Object));
            this.sessionHandler.Setup(x => x.SelectEndpoint(Endpoint, true, 15000)).Returns(new EndpointDescription(Endpoint));
            this.sessionHandler.Setup(x => x.CallMethod(It.IsAny <NodeId>(), It.IsAny <NodeId>(), It.IsAny <object[]>())).Returns(new List <object>()
            {
                1
            });

            this.sessionHandler.Setup(x => x.Browse(It.IsAny <NodeId>(), It.IsAny <NodeId>(), It.IsAny <bool>(), It.IsAny <uint>(), out It.Ref <byte[]> .IsAny, out this.referenceDescriptionCollection));

            this.sessionHandler.Setup(x => x.CreateSession(It.IsAny <ApplicationConfiguration>(), It.IsAny <ConfiguredEndpoint>(), It.IsAny <bool>(),
                                                           It.IsAny <string>(), It.IsAny <uint>(), It.IsAny <IUserIdentity>(), It.IsAny <IList <string> >())).Returns(Task.CompletedTask);

            this.client = new OpcClientService(this.statusBarViewModel.Object, this.sessionHandler.Object, this.reconnectHandler.Object)
            {
                RefreshInterval = 1000
            };
            await this.client.Connect(Endpoint);
        }
        public async Task VerifyConnect()
        {
            this.client = new OpcClientService(this.statusBarViewModel.Object, this.sessionHandler.Object, this.reconnectHandler.Object)
            {
                RefreshInterval = 1000
            };
            await this.client.Connect(Endpoint);

            Assert.AreEqual(OpcClientStatusCode.Connected, this.client.OpcClientStatusCode);
            Assert.IsTrue(this.client.References.Count > 0);

            this.sessionHandler.Setup(x => x.Browse(It.IsAny <NodeId>(), It.IsAny <NodeId>(), It.IsAny <bool>(), It.IsAny <uint>(), out It.Ref <byte[]> .IsAny, out this.referenceDescriptionCollection))
            .Throws <HttpRequestException>();

            Assert.ThrowsAsync <HttpRequestException>(async() => await this.client.Connect(Endpoint));
        }
Exemplo n.º 3
0
        public static void Run(string pathToXml, string connectionString, string databaseName)
        {
            try
            {
                var _opcClient = new OpcClientService();
                if (pathToXml == null || pathToXml.Length == 0)
                {
                    throw new ArgumentException();
                }
                _connectionString = connectionString;
                _databaseName     = databaseName;
                var _operations = XmlSerializer.GetOperations(pathToXml);
                _operationTriggers = new Dictionary <string, OpcOperation>();
                _branchs           = new List <string>()
                {
                    "plc1", "hmi_recipe"
                };
                _operationManager = new OperationManager(_logger);

                _applicationConfiguration = _applicationConfiguration ?? _opcClient.GetApplicationConfiguration();
                _applicationInstance      = _applicationInstance ?? _opcClient.GetApplicationInstance(_applicationConfiguration);
                _ifSertificate            = _opcClient.CheckSertificate(_applicationInstance);
                _endpointConfiguration    = _endpointConfiguration ?? _opcClient.GetEndpointConfiguration(_applicationConfiguration);
                try
                {
                    _endpointDescription = _endpointDescription ?? _opcClient.GetEndpointDescription();
                }
                catch (ServiceResultException ex)
                {
                    throw ex;
                }
                _configuredEndPoint = _configuredEndPoint ?? _opcClient.GetConfiguredEndpoint(_endpointDescription, _endpointConfiguration);
                _session            = _opcClient.GetSession(_applicationConfiguration, _configuredEndPoint);

                if (!_session.Connected)
                {
                    Thread.Sleep(10000);
                    return;
                }

                if (_session == null)
                {
                    _session.KeepAliveInterval = 2000;
                    _session.KeepAlive        += new KeepAliveEventHandler(Session_KeepAlive);
                }

                SearchDataInTree(_session, ObjectIds.ObjectsFolder);
                foreach (var s in _branchs)
                {
                    var _element = s;
                    SearchDataInTree(_session, new NodeId((_references.FirstOrDefault(x => 0 == string.Compare(x.DisplayName.ToString(), _element, true)).NodeId.ToString())));
                }

                _operationTriggers = _operationManager.GetOpcOperationCollection(_operations, _references);
                var _operationsListForSubscribe = _references.FindAll(x => true == _operationTriggers.Any(z => 0 == string.Compare(z.Value.key, x.DisplayName.ToString(), true)));
                var _subscription = new Subscription(_session.DefaultSubscription)
                {
                    PublishingInterval = 1000
                };
                var _monitoredItems = Subscribe(_subscription, _operationsListForSubscribe);
                _monitoredItems.ForEach(i => i.Notification += OnNotification);
                Console.WriteLine("Step 1 - Add a list of items you wish to monitor to the subscription.");
                _subscription.AddItems(_monitoredItems);
                Console.WriteLine("Step 2 - Add the subscription to the session.");
                _session.AddSubscription(_subscription);
                _subscription.Create();

                Console.WriteLine("===================//--//=========================");

                Console.ReadKey(true);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error SearchDataInTree method: {0}", ex.Message);
            }
        }