Skip to content

Commit

Permalink
Added example for writing MTConnect Observations to an InfluxDB database
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie authored Apr 19, 2023
1 parent 25d1afb commit 318bfe9
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/InfluxDB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# MTConnect > InfluxDB
Uses the MTConnect.NET MTConnectHttpClient class to read from an MTConnect Agent and write that data to an InfluxDB database

## Example
```c#
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
using MTConnect.Clients;
using MTConnect.Observations;
using MTConnect.Streams;

var influxServer = "https://eastus-1.azure.cloud2.influxdata.com";
var influxToken = "klajsdfdslkfjdslkfjsdlkfjlsdkjflakdsjf";
var influxOrganization = "example";

// Setup InfluxDB Client
var influxClient = new InfluxDBClient(influxServer, influxToken);

// Setup MTConnect Client (over HTTP)
var mtconnectClient = new MTConnectHttpClient("http://localhost:5000");
mtconnectClient.OnCurrentReceived += ObservationsReceived;
mtconnectClient.OnSampleReceived += ObservationsReceived;
mtconnectClient.Start();

Console.ReadLine();

mtconnectClient.Stop();

void ObservationsReceived(object sender, IStreamsResponseDocument response)
{
using (var write = influxClient.GetWriteApi())
{
var records = new List<PointData>();

foreach (var observation in response.GetObservations())
{
var key = $"{observation.DeviceUuid}.{observation.DataItemId}";
records.Add(PointData.Measurement(key)
.Field("value", observation.GetValue(ValueKeys.Result))
.Timestamp(observation.Timestamp, WritePrecision.Us));
}

// Write Records
write.WritePoints(records, "MTConnect.Streams", influxOrganization);
}
}
```

0 comments on commit 318bfe9

Please sign in to comment.