public void ValidateMqttLocalPubSubConnectionWithUadp( [Values((byte)1, (UInt16)1, (UInt32)1, (UInt64)1, "abc")] object publisherId) { RestartMosquitto("mosquitto"); //Arrange UInt16 writerGroupId = 1; string mqttLocalBrokerUrl = "mqtt://localhost:1883"; ITransportProtocolConfiguration mqttConfiguration = new MqttClientProtocolConfiguration(version: EnumMqttProtocolVersion.V500); UadpNetworkMessageContentMask uadpNetworkMessageContentMask = UadpNetworkMessageContentMask.PublisherId | UadpNetworkMessageContentMask.WriterGroupId | UadpNetworkMessageContentMask.PayloadHeader; UadpDataSetMessageContentMask uadpDataSetMessageContentMask = UadpDataSetMessageContentMask.None; DataSetFieldContentMask dataSetFieldContentMask = DataSetFieldContentMask.None; DataSetMetaDataType[] dataSetMetaDataArray = new DataSetMetaDataType[] { MessagesHelper.CreateDataSetMetaData1("DataSet1"), MessagesHelper.CreateDataSetMetaData2("DataSet2"), MessagesHelper.CreateDataSetMetaData3("DataSet3") }; PubSubConfigurationDataType publisherConfiguration = MessagesHelper.CreatePublisherConfiguration( Profiles.PubSubMqttUadpTransport, mqttLocalBrokerUrl, publisherId: publisherId, writerGroupId: writerGroupId, uadpNetworkMessageContentMask: uadpNetworkMessageContentMask, uadpDataSetMessageContentMask: uadpDataSetMessageContentMask, dataSetFieldContentMask: dataSetFieldContentMask, dataSetMetaDataArray: dataSetMetaDataArray, nameSpaceIndexForData: kNamespaceIndexAllTypes); Assert.IsNotNull(publisherConfiguration, "publisherConfiguration should not be null"); // Configure the mqtt publisher configuration with the MQTTbroker PubSubConnectionDataType mqttPublisherConnection = MessagesHelper.GetConnection(publisherConfiguration, publisherId); Assert.IsNotNull(mqttPublisherConnection, "The MQTT publisher connection is invalid."); mqttPublisherConnection.ConnectionProperties = mqttConfiguration.ConnectionProperties; Assert.IsNotNull(mqttPublisherConnection.ConnectionProperties, "The MQTT publisher connection properties are not valid."); // Create publisher application for multiple datasets UaPubSubApplication publisherApplication = UaPubSubApplication.Create(publisherConfiguration); MessagesHelper.LoadData(publisherApplication, kNamespaceIndexAllTypes); IUaPubSubConnection publisherConnection = publisherApplication.PubSubConnections.First(); Assert.IsNotNull(publisherConnection, "Publisher first connection should not be null"); Assert.IsNotNull(publisherConfiguration.Connections.First(), "publisherConfiguration first connection should not be null"); Assert.IsNotNull(publisherConfiguration.Connections.First().WriterGroups.First(), "publisherConfiguration first writer group of first connection should not be null"); var networkMessages = publisherConnection.CreateNetworkMessages(publisherConfiguration.Connections.First().WriterGroups.First(), new WriterGroupPublishState()); Assert.IsNotNull(networkMessages, "connection.CreateNetworkMessages shall not return null"); Assert.GreaterOrEqual(networkMessages.Count, 1, "connection.CreateNetworkMessages shall have at least one network message"); UadpNetworkMessage uaNetworkMessage = networkMessages[0] as UadpNetworkMessage; Assert.IsNotNull(uaNetworkMessage, "networkMessageEncode should not be null"); bool hasDataSetWriterId = (uadpNetworkMessageContentMask & UadpNetworkMessageContentMask.PayloadHeader) != 0; PubSubConfigurationDataType subscriberConfiguration = MessagesHelper.CreateSubscriberConfiguration( Profiles.PubSubMqttUadpTransport, mqttLocalBrokerUrl, publisherId: publisherId, writerGroupId: writerGroupId, setDataSetWriterId: hasDataSetWriterId, uadpNetworkMessageContentMask: uadpNetworkMessageContentMask, uadpDataSetMessageContentMask: uadpDataSetMessageContentMask, dataSetFieldContentMask: dataSetFieldContentMask, dataSetMetaDataArray: dataSetMetaDataArray, nameSpaceIndexForData: kNamespaceIndexAllTypes); Assert.IsNotNull(subscriberConfiguration, "subscriberConfiguration should not be null"); // Create subscriber application for multiple datasets UaPubSubApplication subscriberApplication = UaPubSubApplication.Create(subscriberConfiguration); Assert.IsNotNull(subscriberApplication, "subscriberApplication should not be null"); Assert.IsNotNull(subscriberApplication.PubSubConnections.First(), "subscriberConfiguration first connection should not be null"); // Configure the mqtt subscriber configuration with the MQTTbroker PubSubConnectionDataType mqttSubcriberConnection = MessagesHelper.GetConnection(subscriberConfiguration, publisherId); Assert.IsNotNull(mqttSubcriberConnection, "The MQTT subscriber connection is invalid."); mqttSubcriberConnection.ConnectionProperties = mqttConfiguration.ConnectionProperties; Assert.IsNotNull(mqttSubcriberConnection.ConnectionProperties, "The MQTT subscriber connection properties are not valid."); var dataSetReaders = subscriberApplication.PubSubConnections.First().GetOperationalDataSetReaders(); Assert.IsNotNull(dataSetReaders, "dataSetReaders should not be null"); IUaPubSubConnection subscriberConnection = subscriberApplication.PubSubConnections.First(); Assert.IsNotNull(subscriberConnection, "Subscriber first connection should not be null"); //Act // it will signal if the uadp message was received from local ip m_uaDataShutdownEvent = new ManualResetEvent(false); subscriberApplication.DataReceived += UaPubSubApplication_DataReceived; subscriberConnection.Start(); publisherConnection.Start(); //Assert if (!m_uaDataShutdownEvent.WaitOne(kEstimatedPublishingTime)) { Assert.Fail("The UADP message was not received"); } subscriberConnection.Stop(); publisherConnection.Stop(); }
public void ValidateMqttLocalPubSubConnectionWithJson( [Values((byte)1, (UInt16)1, (UInt32)1, (UInt64)1, "abc")] object publisherId, [Values(0, 10000)] double metaDataUpdateTime) { RestartMosquitto("mosquitto"); //Arrange UInt16 writerGroupId = 1; string mqttLocalBrokerUrl = "mqtt://localhost:1883"; ITransportProtocolConfiguration mqttConfiguration = new MqttClientProtocolConfiguration(version: EnumMqttProtocolVersion.V500); JsonNetworkMessageContentMask jsonNetworkMessageContentMask = JsonNetworkMessageContentMask.NetworkMessageHeader | JsonNetworkMessageContentMask.PublisherId | JsonNetworkMessageContentMask.DataSetMessageHeader; JsonDataSetMessageContentMask jsonDataSetMessageContentMask = JsonDataSetMessageContentMask.None; DataSetFieldContentMask dataSetFieldContentMask = DataSetFieldContentMask.None; DataSetMetaDataType[] dataSetMetaDataArray = new DataSetMetaDataType[] { MessagesHelper.CreateDataSetMetaData1("DataSet1"), MessagesHelper.CreateDataSetMetaData2("DataSet2"), MessagesHelper.CreateDataSetMetaData3("DataSet3"), MessagesHelper.CreateDataSetMetaDataAllTypes("DataSet4") }; PubSubConfigurationDataType publisherConfiguration = MessagesHelper.CreatePublisherConfiguration( Profiles.PubSubMqttJsonTransport, mqttLocalBrokerUrl, publisherId: publisherId, writerGroupId: writerGroupId, jsonNetworkMessageContentMask: jsonNetworkMessageContentMask, jsonDataSetMessageContentMask: jsonDataSetMessageContentMask, dataSetFieldContentMask: dataSetFieldContentMask, dataSetMetaDataArray: dataSetMetaDataArray, nameSpaceIndexForData: NamespaceIndexAllTypes, metaDataUpdateTime); Assert.IsNotNull(publisherConfiguration, "publisherConfiguration should not be null"); // Configure the mqtt publisher configuration with the MQTTbroker PubSubConnectionDataType mqttPublisherConnection = MessagesHelper.GetConnection(publisherConfiguration, publisherId); Assert.IsNotNull(mqttPublisherConnection, "The MQTT publisher connection is invalid."); mqttPublisherConnection.ConnectionProperties = mqttConfiguration.ConnectionProperties; Assert.IsNotNull(mqttPublisherConnection.ConnectionProperties, "The MQTT publisher connection properties are not valid."); // Create publisher application for multiple datasets UaPubSubApplication publisherApplication = UaPubSubApplication.Create(publisherConfiguration); MessagesHelper.LoadData(publisherApplication, NamespaceIndexAllTypes); IUaPubSubConnection publisherConnection = publisherApplication.PubSubConnections.First(); Assert.IsNotNull(publisherConnection, "Publisher first connection should not be null"); Assert.IsNotNull(publisherConfiguration.Connections.First(), "publisherConfiguration first connection should not be null"); Assert.IsNotNull(publisherConfiguration.Connections.First().WriterGroups.First(), "publisherConfiguration first writer group of first connection should not be null"); var networkMessages = publisherConnection.CreateNetworkMessages(publisherConfiguration.Connections.First().WriterGroups.First(), new WriterGroupPublishState()); Assert.IsNotNull(networkMessages, "connection.CreateNetworkMessages shall not return null"); Assert.GreaterOrEqual(networkMessages.Count, 1, "connection.CreateNetworkMessages shall have at least one network message"); List <JsonNetworkMessage> uaNetworkMessages = MessagesHelper.GetJsonUaDataNetworkMessages(networkMessages.Cast <JsonNetworkMessage>().ToList()); Assert.IsNotNull(uaNetworkMessages, "Json ua-data entries are missing from configuration!"); List <JsonNetworkMessage> uaMetaDataNetworkMessages = MessagesHelper.GetJsonUaMetaDataNetworkMessages(networkMessages.Cast <JsonNetworkMessage>().ToList()); Assert.IsNotNull(uaMetaDataNetworkMessages, "Json ua-metadata entries are missing from configuration!"); bool hasDataSetWriterId = (jsonNetworkMessageContentMask & JsonNetworkMessageContentMask.DataSetMessageHeader) != 0 && (jsonDataSetMessageContentMask & JsonDataSetMessageContentMask.DataSetWriterId) != 0; PubSubConfigurationDataType subscriberConfiguration = MessagesHelper.CreateSubscriberConfiguration( Profiles.PubSubMqttJsonTransport, mqttLocalBrokerUrl, publisherId: publisherId, writerGroupId: writerGroupId, setDataSetWriterId: hasDataSetWriterId, jsonNetworkMessageContentMask: jsonNetworkMessageContentMask, jsonDataSetMessageContentMask: jsonDataSetMessageContentMask, dataSetFieldContentMask: dataSetFieldContentMask, dataSetMetaDataArray: dataSetMetaDataArray, nameSpaceIndexForData: NamespaceIndexAllTypes); Assert.IsNotNull(subscriberConfiguration, "subscriberConfiguration should not be null"); // Create subscriber application for multiple datasets UaPubSubApplication subscriberApplication = UaPubSubApplication.Create(subscriberConfiguration); Assert.IsNotNull(subscriberApplication, "subscriberApplication should not be null"); Assert.IsNotNull(subscriberApplication.PubSubConnections.First(), "subscriberConfiguration first connection should not be null"); // Configure the mqtt subscriber configuration with the MQTTbroker PubSubConnectionDataType mqttSubcriberConnection = MessagesHelper.GetConnection(subscriberConfiguration, publisherId); Assert.IsNotNull(mqttSubcriberConnection, "The MQTT subscriber connection is invalid."); mqttSubcriberConnection.ConnectionProperties = mqttConfiguration.ConnectionProperties; Assert.IsNotNull(mqttSubcriberConnection.ConnectionProperties, "The MQTT subscriber connection properties are not valid."); var dataSetReaders = subscriberApplication.PubSubConnections.First().GetOperationalDataSetReaders(); Assert.IsNotNull(dataSetReaders, "dataSetReaders should not be null"); IUaPubSubConnection subscriberConnection = subscriberApplication.PubSubConnections.First(); Assert.IsNotNull(subscriberConnection, "Subscriber first connection should not be null"); //Act // it will signal if the mqtt message was received from local ip m_uaDataShutdownEvent = new ManualResetEvent(false); // it will signal if the mqtt metadata message was received from local ip m_uaMetaDataShutdownEvent = new ManualResetEvent(false); // it will signal if the changed configuration message was received on local ip m_uaConfigurationUpdateEvent = new ManualResetEvent(false); subscriberApplication.DataReceived += UaPubSubApplication_DataReceived; subscriberApplication.MetaDataReceived += UaPubSubApplication_MetaDataReceived; subscriberApplication.ConfigurationUpdating += UaPubSubApplication_ConfigurationUpdating; subscriberConnection.Start(); publisherConnection.Start(); //Assert if (!m_uaDataShutdownEvent.WaitOne(EstimatedPublishingTime)) { Assert.Fail("The JSON message was not received"); } if (!m_uaMetaDataShutdownEvent.WaitOne(EstimatedPublishingTime)) { Assert.Fail("The JSON metadata message was not received"); } subscriberConnection.Stop(); publisherConnection.Stop(); }
/// <summary> /// Creates a Subscriber PubSubConfiguration object for UDP & UADP programmatically. /// </summary> /// <returns></returns> private static PubSubConfigurationDataType CreateSubscriberConfiguration_MqttUadp(string urlAddress) { // Define a PubSub connection with PublisherId 3 PubSubConnectionDataType pubSubConnection1 = new PubSubConnectionDataType(); pubSubConnection1.Name = "Subscriber Connection MQTT UADP"; pubSubConnection1.Enabled = true; pubSubConnection1.PublisherId = (UInt16)3; pubSubConnection1.TransportProfileUri = Profiles.PubSubMqttUadpTransport; NetworkAddressUrlDataType address = new NetworkAddressUrlDataType(); // Specify the local Network interface name to be used // e.g. address.NetworkInterface = "Ethernet"; // Leave empty to subscribe on all available local interfaces. address.NetworkInterface = String.Empty; address.Url = urlAddress; pubSubConnection1.Address = new ExtensionObject(address); // Configure the mqtt specific configuration with the MQTTbroker ITransportProtocolConfiguration mqttConfiguration = new MqttClientProtocolConfiguration(version: EnumMqttProtocolVersion.V500); pubSubConnection1.ConnectionProperties = mqttConfiguration.ConnectionProperties; string brokerQueueName = "Uadp_WriterGroup_1"; string brokerMetaData = "$Metadata"; #region Define ReaderGroup1 ReaderGroupDataType readerGroup1 = new ReaderGroupDataType(); readerGroup1.Name = "ReaderGroup 1"; readerGroup1.Enabled = true; readerGroup1.MaxNetworkMessageSize = 1500; readerGroup1.MessageSettings = new ExtensionObject(new ReaderGroupMessageDataType()); readerGroup1.TransportSettings = new ExtensionObject(new ReaderGroupTransportDataType()); #region Define DataSetReader 'Simple' for PublisherId = (UInt16)1, DataSetWriterId = 1 DataSetReaderDataType dataSetReaderSimple = new DataSetReaderDataType(); dataSetReaderSimple.Name = "Reader 1 MQTT UADP"; dataSetReaderSimple.PublisherId = (UInt16)3; dataSetReaderSimple.WriterGroupId = 0; dataSetReaderSimple.DataSetWriterId = 1; dataSetReaderSimple.Enabled = true; dataSetReaderSimple.DataSetFieldContentMask = (uint)DataSetFieldContentMask.RawData; dataSetReaderSimple.KeyFrameCount = 1; BrokerDataSetReaderTransportDataType brokerTransportSettings = new BrokerDataSetReaderTransportDataType() { QueueName = brokerQueueName, MetaDataQueueName = $"{brokerQueueName}/{brokerMetaData}", }; dataSetReaderSimple.TransportSettings = new ExtensionObject(brokerTransportSettings); UadpDataSetReaderMessageDataType uadpDataSetReaderMessage = new UadpDataSetReaderMessageDataType() { GroupVersion = 0, NetworkMessageNumber = 0, NetworkMessageContentMask = (uint)(uint)(UadpNetworkMessageContentMask.PublisherId | UadpNetworkMessageContentMask.GroupHeader | UadpNetworkMessageContentMask.WriterGroupId | UadpNetworkMessageContentMask.PayloadHeader | UadpNetworkMessageContentMask.GroupVersion | UadpNetworkMessageContentMask.NetworkMessageNumber | UadpNetworkMessageContentMask.SequenceNumber), DataSetMessageContentMask = (uint)(UadpDataSetMessageContentMask.Status | UadpDataSetMessageContentMask.SequenceNumber), }; dataSetReaderSimple.MessageSettings = new ExtensionObject(uadpDataSetReaderMessage); // Create and set DataSetMetaData for DataSet Simple DataSetMetaDataType simpleMetaData = CreateDataSetMetaDataSimple(); dataSetReaderSimple.DataSetMetaData = simpleMetaData; // Create and set SubscribedDataSet TargetVariablesDataType subscribedDataSet = new TargetVariablesDataType(); subscribedDataSet.TargetVariables = new FieldTargetDataTypeCollection(); foreach (var fieldMetaData in simpleMetaData.Fields) { subscribedDataSet.TargetVariables.Add(new FieldTargetDataType() { DataSetFieldId = fieldMetaData.DataSetFieldId, TargetNodeId = new NodeId(fieldMetaData.Name, NamespaceIndexSimple), AttributeId = Attributes.Value, OverrideValueHandling = OverrideValueHandling.OverrideValue, OverrideValue = new Variant(TypeInfo.GetDefaultValue(fieldMetaData.DataType, (int)ValueRanks.Scalar)) }); } dataSetReaderSimple.SubscribedDataSet = new ExtensionObject(subscribedDataSet); #endregion readerGroup1.DataSetReaders.Add(dataSetReaderSimple); #region Define DataSetReader 'AllTypes' for PublisherId = (UInt16)1, DataSetWriterId = 2 DataSetReaderDataType dataSetReaderAllTypes = new DataSetReaderDataType(); dataSetReaderAllTypes.Name = "Reader 2 MQTT UADP"; dataSetReaderAllTypes.PublisherId = (UInt16)3; dataSetReaderAllTypes.WriterGroupId = 0; dataSetReaderAllTypes.DataSetWriterId = 2; dataSetReaderAllTypes.Enabled = true; dataSetReaderAllTypes.DataSetFieldContentMask = (uint)DataSetFieldContentMask.RawData; dataSetReaderAllTypes.KeyFrameCount = 1; dataSetReaderAllTypes.TransportSettings = new ExtensionObject(brokerTransportSettings); uadpDataSetReaderMessage = new UadpDataSetReaderMessageDataType() { GroupVersion = 0, NetworkMessageNumber = 0, NetworkMessageContentMask = (uint)(uint)(UadpNetworkMessageContentMask.PublisherId | UadpNetworkMessageContentMask.GroupHeader | UadpNetworkMessageContentMask.WriterGroupId | UadpNetworkMessageContentMask.PayloadHeader | UadpNetworkMessageContentMask.GroupVersion | UadpNetworkMessageContentMask.NetworkMessageNumber | UadpNetworkMessageContentMask.SequenceNumber), DataSetMessageContentMask = (uint)(UadpDataSetMessageContentMask.Status | UadpDataSetMessageContentMask.SequenceNumber), }; dataSetReaderAllTypes.MessageSettings = new ExtensionObject(uadpDataSetReaderMessage); // Create and set DataSetMetaData for DataSet AllTypes DataSetMetaDataType allTypesMetaData = CreateDataSetMetaDataAllTypes(); dataSetReaderAllTypes.DataSetMetaData = allTypesMetaData; // Create and set SubscribedDataSet subscribedDataSet = new TargetVariablesDataType(); subscribedDataSet.TargetVariables = new FieldTargetDataTypeCollection(); foreach (var fieldMetaData in allTypesMetaData.Fields) { subscribedDataSet.TargetVariables.Add(new FieldTargetDataType() { DataSetFieldId = fieldMetaData.DataSetFieldId, TargetNodeId = new NodeId(fieldMetaData.Name, NamespaceIndexAllTypes), AttributeId = Attributes.Value, OverrideValueHandling = OverrideValueHandling.OverrideValue, OverrideValue = new Variant(TypeInfo.GetDefaultValue(fieldMetaData.DataType, (int)ValueRanks.Scalar)) }); } dataSetReaderAllTypes.SubscribedDataSet = new ExtensionObject(subscribedDataSet); #endregion readerGroup1.DataSetReaders.Add(dataSetReaderAllTypes); #endregion pubSubConnection1.ReaderGroups.Add(readerGroup1); //create pub sub configuration root object PubSubConfigurationDataType pubSubConfiguration = new PubSubConfigurationDataType(); pubSubConfiguration.Connections = new PubSubConnectionDataTypeCollection() { pubSubConnection1 }; return(pubSubConfiguration); }
/// <summary> /// Creates a PubSubConfiguration object for MQTT & Json programmatically. /// </summary> /// <returns></returns> private static PubSubConfigurationDataType CreatePublisherConfiguration_MqttJson(string urlAddress) { // Define a PubSub connection with PublisherId 100 PubSubConnectionDataType pubSubConnection1 = new PubSubConnectionDataType(); pubSubConnection1.Name = "Publisher Connection3 MQTT Json"; pubSubConnection1.Enabled = true; pubSubConnection1.PublisherId = (UInt16)2; pubSubConnection1.TransportProfileUri = Profiles.PubSubMqttJsonTransport; NetworkAddressUrlDataType address = new NetworkAddressUrlDataType(); // Specify the local Network interface name to be used // e.g. address.NetworkInterface = "Ethernet"; // Leave empty to publish on all available local interfaces. address.NetworkInterface = String.Empty; address.Url = urlAddress; pubSubConnection1.Address = new ExtensionObject(address); // Configure the mqtt specific configuration with the MQTTbroker ITransportProtocolConfiguration mqttConfiguration = new MqttClientProtocolConfiguration(version: EnumMqttProtocolVersion.V500); pubSubConnection1.ConnectionProperties = mqttConfiguration.ConnectionProperties; string brokerQueueName = "Json_WriterGroup_1"; string brokerMetaData = "$Metadata"; #region Define WriterGroup1 - Json WriterGroupDataType writerGroup1 = new WriterGroupDataType(); writerGroup1.Name = "WriterGroup 1"; writerGroup1.Enabled = true; writerGroup1.WriterGroupId = 1; writerGroup1.PublishingInterval = 5000; writerGroup1.KeepAliveTime = 5000; writerGroup1.MaxNetworkMessageSize = 1500; JsonWriterGroupMessageDataType jsonMessageSettings = new JsonWriterGroupMessageDataType() { NetworkMessageContentMask = (uint)(JsonNetworkMessageContentMask.NetworkMessageHeader | JsonNetworkMessageContentMask.DataSetMessageHeader | JsonNetworkMessageContentMask.PublisherId | JsonNetworkMessageContentMask.DataSetClassId | JsonNetworkMessageContentMask.ReplyTo) }; writerGroup1.MessageSettings = new ExtensionObject(jsonMessageSettings); writerGroup1.TransportSettings = new ExtensionObject(new BrokerWriterGroupTransportDataType() { QueueName = brokerQueueName, } ); // Define DataSetWriter 'Simple' Variant encoding DataSetWriterDataType dataSetWriter1 = new DataSetWriterDataType(); dataSetWriter1.Name = "Writer Variant Encoding"; dataSetWriter1.DataSetWriterId = 1; dataSetWriter1.Enabled = true; dataSetWriter1.DataSetFieldContentMask = 0;// Variant encoding; dataSetWriter1.DataSetName = "Simple"; dataSetWriter1.KeyFrameCount = 1; JsonDataSetWriterMessageDataType jsonDataSetWriterMessage = new JsonDataSetWriterMessageDataType() { DataSetMessageContentMask = (uint)(JsonDataSetMessageContentMask.DataSetWriterId | JsonDataSetMessageContentMask.MetaDataVersion | JsonDataSetMessageContentMask.SequenceNumber | JsonDataSetMessageContentMask.Status | JsonDataSetMessageContentMask.Timestamp), }; dataSetWriter1.MessageSettings = new ExtensionObject(jsonDataSetWriterMessage); BrokerDataSetWriterTransportDataType jsonDataSetWriterTransport = new BrokerDataSetWriterTransportDataType() { QueueName = brokerQueueName, MetaDataQueueName = $"{brokerQueueName}/{brokerMetaData}", MetaDataUpdateTime = 10000 }; dataSetWriter1.TransportSettings = new ExtensionObject(jsonDataSetWriterTransport); writerGroup1.DataSetWriters.Add(dataSetWriter1); // Define DataSetWriter 'Simple' - Variant encoding DataSetWriterDataType dataSetWriter2 = new DataSetWriterDataType(); dataSetWriter2.Name = "Writer RawData Encoding"; dataSetWriter2.DataSetWriterId = 2; dataSetWriter2.Enabled = true; dataSetWriter2.DataSetFieldContentMask = (uint)DataSetFieldContentMask.RawData; dataSetWriter2.DataSetName = "AllTypes"; dataSetWriter2.KeyFrameCount = 1; jsonDataSetWriterMessage = new JsonDataSetWriterMessageDataType() { DataSetMessageContentMask = (uint)(JsonDataSetMessageContentMask.DataSetWriterId | JsonDataSetMessageContentMask.SequenceNumber | JsonDataSetMessageContentMask.Status | JsonDataSetMessageContentMask.Timestamp), }; dataSetWriter2.MessageSettings = new ExtensionObject(jsonDataSetWriterMessage); dataSetWriter2.TransportSettings = new ExtensionObject(jsonDataSetWriterTransport); writerGroup1.DataSetWriters.Add(dataSetWriter2); pubSubConnection1.WriterGroups.Add(writerGroup1); #endregion // Define PublishedDataSet Simple PublishedDataSetDataType publishedDataSetSimple = CreatePublishedDataSetSimple(); // Define PublishedDataSet AllTypes PublishedDataSetDataType publishedDataSetAllTypes = CreatePublishedDataSetAllTypes(); //create the PubSub configuration root object PubSubConfigurationDataType pubSubConfiguration = new PubSubConfigurationDataType(); pubSubConfiguration.Connections = new PubSubConnectionDataTypeCollection() { pubSubConnection1 }; pubSubConfiguration.PublishedDataSets = new PublishedDataSetDataTypeCollection() { publishedDataSetSimple, publishedDataSetAllTypes }; return(pubSubConfiguration); }
/// <summary> /// Creates a Subscriber PubSubConfiguration object for MQTT & Json programmatically. /// </summary> /// <returns></returns> private static PubSubConfigurationDataType CreateSubscriberConfiguration_MqttJson(string urlAddress) { // Define a PubSub connection with PublisherId 2 PubSubConnectionDataType pubSubConnection1 = new PubSubConnectionDataType(); pubSubConnection1.Name = "Subscriber Connection3 MQTT Json"; pubSubConnection1.Enabled = true; pubSubConnection1.PublisherId = (UInt16)2; pubSubConnection1.TransportProfileUri = Profiles.PubSubMqttJsonTransport; NetworkAddressUrlDataType address = new NetworkAddressUrlDataType(); // Specify the local Network interface name to be used // e.g. address.NetworkInterface = "Ethernet"; // Leave empty to subscribe on all available local interfaces. address.NetworkInterface = String.Empty; address.Url = urlAddress; pubSubConnection1.Address = new ExtensionObject(address); // Configure the mqtt specific configuration with the MQTTbroker ITransportProtocolConfiguration mqttConfiguration = new MqttClientProtocolConfiguration(version: EnumMqttProtocolVersion.V500); pubSubConnection1.ConnectionProperties = mqttConfiguration.ConnectionProperties; // Define "Simple" MetaData DataSetMetaDataType simpleMetaData = CreateDataSetMetaDataSimple(); // Define "AllTypes" Metadata DataSetMetaDataType allTypesMetaData = CreateDataSetMetaDataAllTypes(); #region Define ReaderGroup1 ReaderGroupDataType readerGroup1 = new ReaderGroupDataType(); readerGroup1.Name = "ReaderGroup 1"; readerGroup1.Enabled = true; readerGroup1.MaxNetworkMessageSize = 1500; readerGroup1.MessageSettings = new ExtensionObject(new ReaderGroupMessageDataType()); readerGroup1.TransportSettings = new ExtensionObject(new ReaderGroupTransportDataType()); #region Define DataSetReader1 'Simple' for PublisherId = (UInt16)3, DataSetWriterId = 1 DataSetReaderDataType dataSetReaderSimple = new DataSetReaderDataType(); dataSetReaderSimple.Name = "Reader 1 MQTT JSON Variant Encoding"; dataSetReaderSimple.PublisherId = (UInt16)2; dataSetReaderSimple.WriterGroupId = 1; dataSetReaderSimple.DataSetWriterId = 1; dataSetReaderSimple.Enabled = true; dataSetReaderSimple.DataSetFieldContentMask = 0;// Variant encoding; dataSetReaderSimple.KeyFrameCount = 1; dataSetReaderSimple.DataSetMetaData = simpleMetaData; BrokerDataSetReaderTransportDataType brokerTransportSettings = new BrokerDataSetReaderTransportDataType() { QueueName = "Json_WriterGroup_1", }; dataSetReaderSimple.TransportSettings = new ExtensionObject(brokerTransportSettings); JsonDataSetReaderMessageDataType jsonDataSetReaderMessage = new JsonDataSetReaderMessageDataType() { NetworkMessageContentMask = (uint)(uint)(JsonNetworkMessageContentMask.NetworkMessageHeader | JsonNetworkMessageContentMask.DataSetMessageHeader | JsonNetworkMessageContentMask.PublisherId | JsonNetworkMessageContentMask.DataSetClassId | JsonNetworkMessageContentMask.ReplyTo), DataSetMessageContentMask = (uint)(JsonDataSetMessageContentMask.DataSetWriterId | JsonDataSetMessageContentMask.MetaDataVersion | JsonDataSetMessageContentMask.SequenceNumber | JsonDataSetMessageContentMask.Status | JsonDataSetMessageContentMask.Timestamp), }; dataSetReaderSimple.MessageSettings = new ExtensionObject(jsonDataSetReaderMessage); TargetVariablesDataType subscribedDataSet = new TargetVariablesDataType(); subscribedDataSet.TargetVariables = new FieldTargetDataTypeCollection(); foreach (var fieldMetaData in simpleMetaData.Fields) { subscribedDataSet.TargetVariables.Add(new FieldTargetDataType() { DataSetFieldId = fieldMetaData.DataSetFieldId, TargetNodeId = new NodeId(fieldMetaData.Name, NamespaceIndexSimple), AttributeId = Attributes.Value, OverrideValueHandling = OverrideValueHandling.OverrideValue, OverrideValue = new Variant(TypeInfo.GetDefaultValue(fieldMetaData.DataType, (int)ValueRanks.Scalar)) }); } dataSetReaderSimple.SubscribedDataSet = new ExtensionObject(subscribedDataSet); #endregion readerGroup1.DataSetReaders.Add(dataSetReaderSimple); #region Define DataSetReader2 'AllTypes' for PublisherId = (UInt16)2, DataSetWriterId = 1 DataSetReaderDataType dataSetReaderAllTypes = new DataSetReaderDataType(); dataSetReaderAllTypes.Name = "Reader 2 MQTT JSON RawData Encoding"; dataSetReaderAllTypes.PublisherId = (UInt16)2; dataSetReaderAllTypes.WriterGroupId = 1; dataSetReaderAllTypes.DataSetWriterId = 2; dataSetReaderAllTypes.Enabled = true; dataSetReaderAllTypes.DataSetFieldContentMask = (uint)DataSetFieldContentMask.RawData; dataSetReaderAllTypes.KeyFrameCount = 1; dataSetReaderAllTypes.DataSetMetaData = allTypesMetaData; brokerTransportSettings = new BrokerDataSetReaderTransportDataType() { QueueName = "Json_WriterGroup_1", }; dataSetReaderAllTypes.TransportSettings = new ExtensionObject(brokerTransportSettings); jsonDataSetReaderMessage = new JsonDataSetReaderMessageDataType() { NetworkMessageContentMask = (uint)(JsonNetworkMessageContentMask.NetworkMessageHeader | JsonNetworkMessageContentMask.DataSetMessageHeader | JsonNetworkMessageContentMask.PublisherId | JsonNetworkMessageContentMask.DataSetClassId | JsonNetworkMessageContentMask.ReplyTo), DataSetMessageContentMask = (uint)(JsonDataSetMessageContentMask.DataSetWriterId | JsonDataSetMessageContentMask.MetaDataVersion | JsonDataSetMessageContentMask.SequenceNumber | JsonDataSetMessageContentMask.Status | JsonDataSetMessageContentMask.Timestamp), }; dataSetReaderAllTypes.MessageSettings = new ExtensionObject(jsonDataSetReaderMessage); subscribedDataSet = new TargetVariablesDataType(); subscribedDataSet.TargetVariables = new FieldTargetDataTypeCollection(); foreach (var fieldMetaData in allTypesMetaData.Fields) { subscribedDataSet.TargetVariables.Add(new FieldTargetDataType() { DataSetFieldId = fieldMetaData.DataSetFieldId, TargetNodeId = new NodeId(fieldMetaData.Name, NamespaceIndexSimple), AttributeId = Attributes.Value, OverrideValueHandling = OverrideValueHandling.OverrideValue, OverrideValue = new Variant(TypeInfo.GetDefaultValue(fieldMetaData.DataType, (int)ValueRanks.Scalar)) }); } dataSetReaderAllTypes.SubscribedDataSet = new ExtensionObject(subscribedDataSet); #endregion readerGroup1.DataSetReaders.Add(dataSetReaderAllTypes); #endregion pubSubConnection1.ReaderGroups.Add(readerGroup1); //create pub sub configuration root object PubSubConfigurationDataType pubSubConfiguration = new PubSubConfigurationDataType(); pubSubConfiguration.Connections = new PubSubConnectionDataTypeCollection() { pubSubConnection1 }; return(pubSubConfiguration); }
/// <summary> /// Creates a PubSubConfiguration object for MQTT & UADP programmatically. /// </summary> /// <returns></returns> private static PubSubConfigurationDataType CreatePublisherConfiguration_MqttUadp(string urlAddress) { // Define a PubSub connection with PublisherId 3 PubSubConnectionDataType pubSubConnection1 = new PubSubConnectionDataType(); pubSubConnection1.Name = "Publisher Connection MQTT UADP"; pubSubConnection1.Enabled = true; pubSubConnection1.PublisherId = (UInt16)3; pubSubConnection1.TransportProfileUri = Profiles.PubSubMqttUadpTransport; NetworkAddressUrlDataType address = new NetworkAddressUrlDataType(); // Specify the local Network interface name to be used // e.g. address.NetworkInterface = "Ethernet"; // Leave empty to publish on all available local interfaces. address.NetworkInterface = String.Empty; address.Url = urlAddress; pubSubConnection1.Address = new ExtensionObject(address); // Configure the mqtt specific configuration with the MQTTbroker ITransportProtocolConfiguration mqttConfiguration = new MqttClientProtocolConfiguration(version: EnumMqttProtocolVersion.V500); pubSubConnection1.ConnectionProperties = mqttConfiguration.ConnectionProperties; string brokerQueueName = "Uadp_WriterGroup_1"; string brokerMetaData = "$Metadata"; #region Define WriterGroup1 WriterGroupDataType writerGroup1 = new WriterGroupDataType(); writerGroup1.Name = "WriterGroup 1"; writerGroup1.Enabled = true; writerGroup1.WriterGroupId = 1; writerGroup1.PublishingInterval = 5000; writerGroup1.KeepAliveTime = 5000; writerGroup1.MaxNetworkMessageSize = 1500; writerGroup1.HeaderLayoutUri = "UADP-Cyclic-Fixed"; UadpWriterGroupMessageDataType uadpMessageSettings = new UadpWriterGroupMessageDataType() { DataSetOrdering = DataSetOrderingType.AscendingWriterId, GroupVersion = 0, NetworkMessageContentMask = (uint)(UadpNetworkMessageContentMask.PublisherId | UadpNetworkMessageContentMask.GroupHeader | UadpNetworkMessageContentMask.WriterGroupId | UadpNetworkMessageContentMask.PayloadHeader | UadpNetworkMessageContentMask.GroupVersion | UadpNetworkMessageContentMask.NetworkMessageNumber | UadpNetworkMessageContentMask.SequenceNumber) }; writerGroup1.MessageSettings = new ExtensionObject(uadpMessageSettings); // initialize Broker transport settings writerGroup1.TransportSettings = new ExtensionObject(new BrokerWriterGroupTransportDataType() { QueueName = brokerQueueName, }); // Define DataSetWriter 'Simple' DataSetWriterDataType dataSetWriter1 = new DataSetWriterDataType(); dataSetWriter1.Name = "Writer 1"; dataSetWriter1.DataSetWriterId = 1; dataSetWriter1.Enabled = true; dataSetWriter1.DataSetFieldContentMask = (uint)DataSetFieldContentMask.RawData; dataSetWriter1.DataSetName = "Simple"; dataSetWriter1.KeyFrameCount = 1; UadpDataSetWriterMessageDataType uadpDataSetWriterMessage = new UadpDataSetWriterMessageDataType() { ConfiguredSize = 32, DataSetOffset = 15, NetworkMessageNumber = 1, DataSetMessageContentMask = (uint)(UadpDataSetMessageContentMask.Status | UadpDataSetMessageContentMask.SequenceNumber), }; dataSetWriter1.MessageSettings = new ExtensionObject(uadpDataSetWriterMessage); BrokerDataSetWriterTransportDataType uadpDataSetWriterTransport = new BrokerDataSetWriterTransportDataType() { QueueName = brokerQueueName, MetaDataQueueName = $"{brokerQueueName}/{brokerMetaData}", MetaDataUpdateTime = 60000 }; dataSetWriter1.TransportSettings = new ExtensionObject(uadpDataSetWriterTransport); writerGroup1.DataSetWriters.Add(dataSetWriter1); // Define DataSetWriter 'AllTypes' DataSetWriterDataType dataSetWriter2 = new DataSetWriterDataType(); dataSetWriter2.Name = "Writer 2"; dataSetWriter2.DataSetWriterId = 2; dataSetWriter2.Enabled = true; dataSetWriter2.DataSetFieldContentMask = (uint)DataSetFieldContentMask.RawData; dataSetWriter2.DataSetName = "AllTypes"; dataSetWriter2.KeyFrameCount = 1; uadpDataSetWriterMessage = new UadpDataSetWriterMessageDataType() { ConfiguredSize = 32, DataSetOffset = 47, NetworkMessageNumber = 1, DataSetMessageContentMask = (uint)(UadpDataSetMessageContentMask.Status | UadpDataSetMessageContentMask.SequenceNumber), }; dataSetWriter2.MessageSettings = new ExtensionObject(uadpDataSetWriterMessage); dataSetWriter2.TransportSettings = new ExtensionObject(uadpDataSetWriterTransport); writerGroup1.DataSetWriters.Add(dataSetWriter2); pubSubConnection1.WriterGroups.Add(writerGroup1); #endregion // Define PublishedDataSet Simple PublishedDataSetDataType publishedDataSetSimple = CreatePublishedDataSetSimple(); // Define PublishedDataSet AllTypes PublishedDataSetDataType publishedDataSetAllTypes = CreatePublishedDataSetAllTypes(); //create the PubSub configuration root object PubSubConfigurationDataType pubSubConfiguration = new PubSubConfigurationDataType(); pubSubConfiguration.Connections = new PubSubConnectionDataTypeCollection() { pubSubConnection1 }; pubSubConfiguration.PublishedDataSets = new PublishedDataSetDataTypeCollection() { publishedDataSetSimple, publishedDataSetAllTypes }; return(pubSubConfiguration); }