Configure the Azure Hub event messages sent to the product
- Last UpdatedJul 24, 2022
- 2 minute read
Event messages sent from the Event Hub or IoT Hub to AVEVA Insight must have a specific format that adheres to the format defined in a separate mapping file uploaded to AVEVA Insight. For more information on this mapping file, see Processing Event data from an Event Hub/IoT Hub data source. The sample code in this section is based on the example mapping file provided below:
{
"docType": "EventMapping",
"version": "1.0",
"requiredMapping": {
"id": {
"type": "Guid",
"mappedProp": "EventId"
},
"type": {
"type": "string",
"mappedProp": "Quality"
},
"eventtime": {
"type": "datetimeoffset",
"mappedProp": "EventTime"
}
},
"additionalMapping": {
"comment": {
"type": "String",
"description": "Comment of the event",
"mappedProp": "MyComment"
},
"location": {
"type": "String",
"description": "",
"mappedProp": "MyLocation"
}
}
}
Message format
An example of the message format for sending data from an Event Hub or IoT Hub can be found in the support article "Send events to and receive events from Azure Event Hubs - .NET (Azure.Messaging.EventHubs)" in Microsoft's documentation at Microsoft Documentation.
Modify the message format described in the link above by following the snippet below.
Important: You must update private const string connectionString with your key and private const string eventHubName with your Event Hub or IoT Hub name.
{
"EventId": "<<eventid>>"
"Quality": “<<Good/Questionable/Bad>>",
"EventTime": “<<time in UTC>>",
"MyComment": “<<comment>>",
"MyLocation": “<<locaiton>>"
}
Example: C# code to generate messages
using System;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using System.Text;
namespace EventHubToInsight
{
class Program
{
private const string connectionString = "<YOUR KEY>";
private const string eventHubName = "<YOUR HUB NAME>";
static async Task Main()
{
while (true)
{
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
var insightEvent = new
{
EventId = Guid.NewGuid(), // own id can be passed here
Quality = "Good",
EventTime = DateTime.UtcNow, // Time should always be in UTC
MyComment = "Some description",
MyLocation = "Some location"
};
// Create a batch of events
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
var msg = Newtonsoft.Json.JsonConvert.SerializeObject(insightEvent);
var eventData = new EventData(Encoding.UTF8.GetBytes(msg));
eventData.Properties.Add("isEvent", true); // This is required when sending events
eventBatch.TryAdd(eventData);
await producerClient.SendAsync(eventBatch);
}
System.Threading.Thread.Sleep(2000);
}
}
}
}