-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example for writing MTConnect Observations to an InfluxDB database
- Loading branch information
1 parent
25d1afb
commit 318bfe9
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
``` |