Пример #1
0
        public static void Main()
        {
            using (var client = new OpcClient("opc.tcp://localhost:4840")) {
                client.Connect();

                var filter = OpcFilter.Using(client)
                             .FromEvents("ns=2;i=1042")
                             .Select();

                client.SubscribeEvent("ns=3;i=5031", filter, (sender, e) => {
                    if (e.Event is MachineToolAlarmCondition alarm)
                    {
                        Console.WriteLine(new Guid(alarm.EventId).ToString());
                        Console.WriteLine("- AlarmIdentifier: {0}", alarm.AlarmIdentifier);
                        Console.WriteLine("- AuxParameters: {0}", alarm.AuxParameters);
                    }
                });

                Console.WriteLine("Press any key to exit.");
                Console.ReadKey(true);
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            //// If the server domain name does not match localhost just replace it
            //// e.g. with the IP address or name of the server machine.

            var client = new OpcClient("opc.tcp://localhost:4840/SampleServer");

            client.Connect();

            var positionNode    = nodes.Add(client, machineId, "Position");
            var temperatureNode = nodes.Add(client, machineId, "Temperature");
            var statusNode      = nodes.Add(client, machineId, "Status");

            var severity   = new OpcSimpleAttributeOperand(OpcEventTypes.Event, "Severity");
            var sourceName = new OpcSimpleAttributeOperand(OpcEventTypes.Event, "SourceName");

            var filter = OpcFilter.Using(client)
                         // Construct the filter to use for event subscriptions...
                         .FromEvents(
                // ... define the types of events to include in the filter
                // this will automatically add all properties defined by
                // the types of events specified ...
                OpcEventTypes.AlarmCondition,
                OpcEventTypes.ExclusiveLimitAlarm,
                OpcEventTypes.DialogCondition)
                         .Where(
                // ... restrict the event information received from the server
                // by specifying the types of events and ...
                OpcFilterOperand.OfType(OpcEventTypes.AlarmCondition)
                | OpcFilterOperand.OfType(OpcEventTypes.ExclusiveLimitAlarm)
                | OpcFilterOperand.OfType(OpcEventTypes.DialogCondition))
                                    // ... maybe additional conditions to fulfill, like the required severity
                                    // using the severity operand defined above like follows:
                                    //// severity > OpcEventSeverity.Medium
                                    // ... these conditions can then enhanced logical operators like follows:
                                    //// & sourceName.Like("Limit")
                         .Select(); // ... finally using Select() will query all the necessary event

            // information required and will create the event filter setup with the
            // givent event types, constraints and additional selection fields.

            // All in one Subscription
            {
                // Subscribe to data changes (value changes committed using ApplyChanges(...)
                // calls) on the status, position and temperature node. Also subscribe to all
                // events reported through notifiers assigned to the machine node. Each of
                // these subscribe tasks represent a single monitored item instance which is used
                // to define the reporting characteristics to use. The so defined monitored items
                // are then maintained by one single subscription.
                var subscription = client.SubscribeNodes(
                    new OpcSubscribeDataChange(statusNode.Id, HandleDataChanges),
                    new OpcSubscribeDataChange(positionNode.Id, HandleDataChanges),
                    new OpcSubscribeDataChange(temperatureNode.Id, HandleDataChanges),
                    new OpcSubscribeEvent(machineId, filter, HandleDataEvents));

                // In case there the client is interested in the current event information (which
                // is not explicitly re-published through the server when a subscription is
                // created) the client have to query a "ConditionRefresh" to query the latest
                // event information known by the server. In general the server does not need to
                // hold something like a history of events.
                subscription.RefreshConditions();
            }

            // Everyone in its own Subscription
            ////{
            ////    var eventsSubscription = client.SubscribeEvent(machineId, filter, HandleDataEvents);
            ////    eventsSubscription.RefreshConditions();

            ////    // In case there only following events need to be known, the "ConditionRefresh" can
            ////    // be omitted and the subscription variable can be removed:
            ////    //// client.SubscribeEvent(machineId, filter, HandleDataEvents);

            ////    // The following created subscription do not need to trigger a "ConditionRefresh",
            ////    // because in general only event information may be important and the current
            ////    // value can be just read, too.
            ////    client.SubscribeDataChange(statusNode.Id, HandleDataChanges);
            ////    client.SubscribeDataChange(positionNode.Id, HandleDataChanges);
            ////    client.SubscribeDataChange(positionNode.Id, HandleDataChanges);
            ////    client.SubscribeDataChange(temperatureNode.Id, HandleDataChanges);
            ////}

            // Handle global (server-wide) events
            {
                var conditionName = new OpcSimpleAttributeOperand(OpcEventTypes.Condition, "ConditionName");

                var globalFilter = OpcFilter.Using(client)
                                   .FromEvents(OpcEventTypes.AlarmCondition)
                                   .Where(severity > OpcEventSeverity.Medium & conditionName.Like("Temperature"))
                                   .Select();

                client.SubscribeEvent(OpcObjectTypes.Server, globalFilter, HandleGlobalEvents);
            }

            Console.CancelKeyPress += (sender, e) => cancelSemaphore.Release();

            do
            {
                UpdateConsole(client);
            }while (!cancelSemaphore.Wait(1000));

            client.Disconnect();
        }