Exemple #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("OPC UA Console Reference Publisher");

            try
            {
                // Define the configuration of UA Publisher application
                PubSubConfigurationDataType pubSubConfiguration = CreatePublisherConfiguration();

                // Create the UA Publisher application
                using (UaPubSubApplication uaPubSubApplication = UaPubSubApplication.Create(pubSubConfiguration))
                {
                    // Start values simulator
                    PublishedValuesWrites valuesSimulator = new PublishedValuesWrites(uaPubSubApplication);
                    valuesSimulator.Start();

                    // Start the publisher
                    uaPubSubApplication.Start();

                    Console.WriteLine("Publisher Started. Press Ctrl-C to exit...");

                    ManualResetEvent quitEvent = new ManualResetEvent(false);
                    try
                    {
                        Console.CancelKeyPress += (sender, eArgs) =>
                        {
                            quitEvent.Set();
                            eArgs.Cancel = true;
                        };
                    }
                    catch
                    {
                    }

                    // wait for timeout or Ctrl-C
                    quitEvent.WaitOne();
                }

                Console.WriteLine("Program ended.");
                Console.WriteLine("Press any key to finish...");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("OPC UA Console Reference Publisher");

            // command line options
            bool   showHelp     = false;
            bool   useMqttJson  = true;
            bool   useUdpUadp   = false;
            string publisherUrl = null;

            Mono.Options.OptionSet options = new Mono.Options.OptionSet {
                { "h|help", "Show usage information", v => showHelp = v != null },
                { "m|mqtt_json", "Use MQTT with Json encoding Profile. This is the default option.", v => useMqttJson = v != null },
                { "u|udp_uadp", "Use UDP with UADP encoding Profile", v => useUdpUadp = v != null },
                { "url|publisher_url=", "Publisher Url Address", v => publisherUrl = v },
            };

            IList <string> extraArgs = null;

            try
            {
                extraArgs = options.Parse(args);
                if (extraArgs.Count > 0)
                {
                    foreach (string extraArg in extraArgs)
                    {
                        Console.WriteLine("Error: Unknown option: {0}", extraArg);
                        showHelp = true;
                    }
                }
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                showHelp = true;
            }

            if (showHelp)
            {
                Console.WriteLine("Usage: dotnet ConsoleReferencePublisher.dll/exe [OPTIONS]");
                Console.WriteLine();

                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }
            try
            {
                InitializeLog();

                PubSubConfigurationDataType pubSubConfiguration = null;
                if (useUdpUadp)
                {
                    // set default UDP Publisher Url to local multicast if not sent in args.
                    if (string.IsNullOrEmpty(publisherUrl))
                    {
                        publisherUrl = "opc.udp://239.0.0.1:4840";
                    }

                    // Create configuration using UDP protocol and UADP Encoding
                    pubSubConfiguration = CreatePublisherConfiguration_UdpUadp(publisherUrl);
                    Console.WriteLine("The Pubsub Connection was initialized using UDP & UADP Profile.");
                }
                else
                {
                    // set default MQTT Broker Url to localhost if not sent in args.
                    if (string.IsNullOrEmpty(publisherUrl))
                    {
                        publisherUrl = "mqtt://localhost:1883";
                    }

                    // Create configuration using MQTT protocol and JSON Encoding
                    pubSubConfiguration = CreatePublisherConfiguration_MqttJson(publisherUrl);
                    Console.WriteLine("The Pubsub Connection was initialized using MQTT & JSON Profile.");
                }

                // Create the UA Publisher application using configuration file
                using (UaPubSubApplication uaPubSubApplication = UaPubSubApplication.Create(pubSubConfiguration))
                {
                    // Start values simulator
                    PublishedValuesWrites valuesSimulator = new PublishedValuesWrites(uaPubSubApplication);
                    valuesSimulator.Start();

                    // Start the publisher
                    uaPubSubApplication.Start();

                    Console.WriteLine("Publisher Started. Press Ctrl-C to exit...");

                    ManualResetEvent quitEvent = new ManualResetEvent(false);
                    try
                    {
                        Console.CancelKeyPress += (sender, eArgs) => {
                            quitEvent.Set();
                            eArgs.Cancel = true;
                        };
                    }
                    catch
                    {
                    }

                    // wait for timeout or Ctrl-C
                    quitEvent.WaitOne();
                }

                Console.WriteLine("Program ended.");
                Console.WriteLine("Press any key to finish...");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }