Exemple #1
0
        private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                if (e.NotificationValue == null || monitoredItem.Subscription.Session == null)
                {
                    return;
                }

                JsonEncoder encoder = new JsonEncoder(
                    monitoredItem.Subscription.Session.MessageContext, false);
                encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                e.NotificationValue.Encode(encoder);

                var json  = encoder.Close();
                var bytes = new UTF8Encoding(false).GetBytes(json);

                foreach (var publisher in m_publishers)
                {
                    try
                    {
                        publisher.Publish(new ArraySegment <byte>(bytes));
                    }
                    catch (Exception ex)
                    {
                        Utils.Trace(ex, "Failed to publish message, dropping....");
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
        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());
            }
        }
Exemple #3
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        ((DataChangeNotification)((NotificationData)item.NotificationMessage.NotificationData[0].Body)).MonitoredItems[0].Encode(encoder);

                        string json = encoder.Close();
                        json = json.Replace("\\", "");
                        byte[] bytes = new UTF8Encoding(false).GetBytes(json);

                        foreach (var publisher in m_publishers)
                        {
                            try
                            {
                                publisher.Publish(new ArraySegment <byte>(bytes));
                                Utils.Trace(null, "Publishing: " + json);
                            }
                            catch (Exception ex)
                            {
                                Utils.Trace(ex, "Failed to publish message, dropping....");
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
Exemple #4
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        item.NotificationMessage.Encode(encoder);

                        var json = encoder.Close();
                        try
                        {
                            m_massTransitPublisher.Publish(json);
                            Utils.Trace(null, "Publishing: " + json);
                        }
                        catch (Exception ex)
                        {
                            Utils.Trace(ex, "Failed to publish message, dropping....");
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
        private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                if (e.NotificationValue == null || monitoredItem.Subscription.Session == null)
                {
                    return;
                }

                JsonEncoder encoder = new JsonEncoder(
                    monitoredItem.Subscription.Session.MessageContext, false);
                encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                e.NotificationValue.Encode(encoder);

                var json = encoder.Close();
                var bytes = new UTF8Encoding(false).GetBytes(json);

                foreach (var publisher in m_publishers)
                {
                    try
                    {
                        publisher.Publish(new ArraySegment<byte>(bytes));
                    }
                    catch(Exception ex)
                    {
                        Utils.Trace(ex, "Failed to publish message, dropping....");
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
Exemple #6
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        item.NotificationMessage.Encode(encoder);

                        var json = encoder.Close();
                        try
                        {
                            m_massTransitPublisher.Publish(json);
                            Utils.Trace(null, "Publishing: " + json);
                        }
                        catch (Exception ex)
                        {
                            Utils.Trace(ex, "Failed to publish message, dropping....");
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        ((DataChangeNotification)((NotificationData)item.NotificationMessage.NotificationData[0].Body)).MonitoredItems[0].Encode(encoder);

                        string json = encoder.Close();
                        json = json.Replace("\\", "");
                        byte[] bytes = new UTF8Encoding(false).GetBytes(json);

                        foreach (var publisher in m_publishers)
                        {
                            try
                            {
                                publisher.Publish(new ArraySegment<byte>(bytes));
                                Utils.Trace(null, "Publishing: " + json);
                            }
                            catch (Exception ex)
                            {
                                Utils.Trace(ex, "Failed to publish message, dropping....");
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }