private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e) { try { MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification; if (notification == null) { return; } DataValue value = notification.Value as DataValue; if (value == null) { return; } using (var encoder = new JsonEncoder(monitoredItem.Subscription.Session.MessageContext, false)) { string applicationURI = monitoredItem.Subscription.Session.Endpoint.Server.ApplicationUri; encoder.WriteString("ApplicationUri", applicationURI); encoder.WriteString("DisplayName", monitoredItem.DisplayName); encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId); // suppress output of server timestamp in json by setting it to minvalue value.ServerTimestamp = DateTime.MinValue; encoder.WriteDataValue("Value", value); string json = encoder.CloseAndReturnText(); var properties = new Dictionary <string, string>(); properties.Add("content-type", "application/opcua+uajson"); properties.Add("deviceName", Id); if (SharedAccessKey != null) { properties.Add("source", "mapping"); properties.Add("deviceKey", SharedAccessKey); } try { Module.Publish(new Message(json, properties)); } catch (Exception ex) { Console.WriteLine("Opc.Ua.Client.SampleModule: Failed to publish message, dropping..."); Console.WriteLine(ex.ToString()); } } } catch (Exception exception) { Console.WriteLine("Opc.Ua.Client.SampleModule: Error processing monitored item notification."); Console.WriteLine(exception.ToString()); } }
public string ToJson() { var encoder = new JsonEncoder(ServiceMessageContext.GlobalContext, true); encoder.WriteString("authority", AuthorityUrl); encoder.WriteString("grantType", GrantType); encoder.WriteString("tokenEndpoint", TokenEndpoint); encoder.WriteString("resource", ResourceId); encoder.WriteStringArray("scopes", Scopes); return(encoder.Close()); }
/// <summary> /// The notification that the data for a monitored item has changed on an OPC UA server /// </summary> public static void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args) { try { if (args.NotificationValue == null || monitoredItem.Subscription.Session == null) { return; } MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification; if (notification == null) { return; } DataValue value = notification.Value as DataValue; if (value == null) { return; } JsonEncoder encoder = new JsonEncoder(monitoredItem.Subscription.Session.MessageContext, false); string applicationURI = monitoredItem.Subscription.Session.Endpoint.Server.ApplicationUri; encoder.WriteString("ApplicationUri", (applicationURI + (string.IsNullOrEmpty(ShopfloorDomain) ? "" : $":{ShopfloorDomain}"))); encoder.WriteString("DisplayName", monitoredItem.DisplayName); // write NodeId as ns=x;i=y NodeId nodeId = monitoredItem.ResolvedNodeId; encoder.WriteString("NodeId", new NodeId(nodeId.Identifier, nodeId.NamespaceIndex).ToString()); // suppress output of server timestamp in json by setting it to minvalue value.ServerTimestamp = DateTime.MinValue; encoder.WriteDataValue("Value", value); string json = encoder.CloseAndReturnText(); // add message to fifo send queue Trace(Utils.TraceMasks.OperationDetail, "Enqueue a new message:"); Trace(Utils.TraceMasks.OperationDetail, " ApplicationUri: " + (applicationURI + (string.IsNullOrEmpty(ShopfloorDomain) ? "" : $":{ShopfloorDomain}"))); Trace(Utils.TraceMasks.OperationDetail, $" DisplayName: {monitoredItem.DisplayName}"); Trace(Utils.TraceMasks.OperationDetail, $" Value: {value}"); Program.IotHubMessaging.Enqueue(json); } catch (Exception e) { Trace(e, "Error processing monitored item notification"); } }
/// <summary> /// Encode Network Message Header /// </summary> private void EncodeNetworkMessageHeader(JsonEncoder jsonEncoder) { jsonEncoder.WriteString(nameof(MessageId), MessageId); jsonEncoder.WriteString(nameof(MessageType), MessageType); if ((NetworkMessageContentMask & JsonNetworkMessageContentMask.PublisherId) != 0) { jsonEncoder.WriteString(nameof(PublisherId), PublisherId); } if ((NetworkMessageContentMask & JsonNetworkMessageContentMask.DataSetClassId) != 0) { jsonEncoder.WriteString(nameof(DataSetClassId), DataSetClassId); } }
/// <summary> /// The notification that the data for a monitored item has changed on an OPC UA server. /// </summary> public void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args) { try { if (args == null || args.NotificationValue == null || monitoredItem == null || monitoredItem.Subscription == null || monitoredItem.Subscription.Session == null) { return; } MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification; if (notification == null) { return; } DataValue value = notification.Value as DataValue; if (value == null) { return; } JsonEncoder encoder = new JsonEncoder(monitoredItem.Subscription.Session.MessageContext, false); string applicationURI = monitoredItem.Subscription.Session.Endpoint.Server.ApplicationUri; encoder.WriteString("ApplicationUri", (applicationURI + (string.IsNullOrEmpty(ShopfloorDomain) ? "" : $":{ShopfloorDomain}"))); encoder.WriteString("DisplayName", monitoredItem.DisplayName); // use the node Id as configured, to also have the namespace URI in case of a ExpandedNodeId. encoder.WriteString("NodeId", ConfigNodeId); // suppress output of server timestamp in json by setting it to minvalue value.ServerTimestamp = DateTime.MinValue; encoder.WriteDataValue("Value", value); string json = encoder.CloseAndReturnText(); // add message to fifo send queue Trace(Utils.TraceMasks.OperationDetail, $"Enqueue a new message from subscription {monitoredItem.Subscription.Id} (publishing interval: {monitoredItem.Subscription.PublishingInterval}, sampling interval: {monitoredItem.SamplingInterval}):"); Trace(Utils.TraceMasks.OperationDetail, " ApplicationUri: " + (applicationURI + (string.IsNullOrEmpty(ShopfloorDomain) ? "" : $":{ShopfloorDomain}"))); Trace(Utils.TraceMasks.OperationDetail, $" DisplayName: {monitoredItem.DisplayName}"); Trace(Utils.TraceMasks.OperationDetail, $" Value: {value}"); IotHubCommunication.Enqueue(json); } catch (Exception e) { Trace(e, "Error processing monitored item notification"); } }
/// <summary> /// Encode DataSet message header /// </summary> private void EncodeDataSetMessageHeader(JsonEncoder encoder) { if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.DataSetWriterId) != 0) { encoder.WriteString(nameof(DataSetWriterId), DataSetWriterId.ToString()); } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.SequenceNumber) != 0) { encoder.WriteUInt32(nameof(SequenceNumber), SequenceNumber); } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.MetaDataVersion) != 0) { encoder.WriteEncodeable(nameof(MetaDataVersion), MetaDataVersion, typeof(ConfigurationVersionDataType)); } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.Timestamp) != 0) { encoder.WriteDateTime(nameof(Timestamp), Timestamp); } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.Status) != 0) { encoder.WriteStatusCode(nameof(Status), Status); } }
/// <summary> /// Encode ReplyTo /// </summary> private void EncodeReplyTo(JsonEncoder jsonEncoder) { if ((NetworkMessageContentMask & JsonNetworkMessageContentMask.ReplyTo) != 0) { jsonEncoder.WriteString("ReplyTo", ReplyTo); } }
/// <summary> /// The notification that the data for a monitored item has changed on an OPC UA server /// </summary> public static void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e) { try { if (e.NotificationValue == null || monitoredItem.Subscription.Session == null) { return; } MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification; if (notification == null) { return; } DataValue value = notification.Value as DataValue; if (value == null) { return; } JsonEncoder encoder = new JsonEncoder(monitoredItem.Subscription.Session.MessageContext, false); string applicationURI = monitoredItem.Subscription.Session.Endpoint.Server.ApplicationUri; encoder.WriteString("ApplicationUri", applicationURI); encoder.WriteString("DisplayName", monitoredItem.DisplayName); // write NodeId as ns=x;i=y NodeId nodeId = monitoredItem.ResolvedNodeId; encoder.WriteString("NodeId", new NodeId(nodeId.Identifier, nodeId.NamespaceIndex).ToString()); // suppress output of server timestamp in json by setting it to minvalue value.ServerTimestamp = DateTime.MinValue; encoder.WriteDataValue("Value", value); string json = encoder.CloseAndReturnText(); // add message to fifo send queue m_sendQueue.Enqueue(json); } catch (Exception exception) { Trace("Error processing monitored item notification: " + exception.ToString()); } }
/// <summary> /// The notification that the data for a monitored item has changed on an OPC UA server /// </summary> public static void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e) { try { if (e.NotificationValue == null || monitoredItem.Subscription.Session == null) { return; } MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification; if (notification == null) { return; } DataValue value = notification.Value as DataValue; if (value == null) { return; } JsonEncoder encoder = new JsonEncoder(monitoredItem.Subscription.Session.MessageContext, false); string applicationURI = monitoredItem.Subscription.Session.Endpoint.Server.ApplicationUri; encoder.WriteString("ApplicationUri", applicationURI); encoder.WriteString("DisplayName", monitoredItem.DisplayName); // write NodeId as ns=x;i=y NodeId nodeId = monitoredItem.ResolvedNodeId; encoder.WriteString("NodeId", new NodeId(nodeId.Identifier, nodeId.NamespaceIndex).ToString()); // suppress output of server timestamp in json by setting it to minvalue value.ServerTimestamp = DateTime.MinValue; encoder.WriteDataValue("Value", value); string json = encoder.CloseAndReturnText(); byte[] bytes = new UTF8Encoding(false).GetBytes(json); // publish var properties = new Dictionary <string, string>(); properties.Add("content-type", "application/opcua+uajson"); properties.Add("deviceName", m_deviceName); if (m_accessKey != null) { properties.Add("source", "mapping"); properties.Add("deviceKey", m_accessKey); } try { Publish(new Message(json, properties)); Trace("Opc.Ua.Publisher.Module: Published: " + json + " from " + m_deviceName); } catch (Exception ex) { Trace("Opc.Ua.Publisher.Module: Failed to publish message, dropping..."); Trace(ex.ToString()); } } catch (Exception exception) { Trace("Opc.Ua.Publisher.Module: Error processing monitored item notification: " + exception.ToString()); } }