Skip to content

Commit

Permalink
Update to MQTT Client
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Nov 1, 2023
1 parent c41cbbf commit a08a551
Show file tree
Hide file tree
Showing 8 changed files with 1,212 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using MTConnect.Clients;
using MTConnect.Observations;

var agentUrl = "localhost";

var client = new MTConnectMqtt2Client(agentUrl);
//client.Interval = 100;
//client.Heartbeat = 10000;
//client.ContentType = "application/json";
//client.DocumentFormat = "json-cppagent";
client.DeviceReceived += (sender, device) =>
{
Console.WriteLine($"Device Received : {device.Uuid}");
};
client.ObservationReceived += (sender, observation) =>
{
switch (observation.Representation)
{
case MTConnect.Devices.DataItemRepresentation.VALUE:
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
break;

case MTConnect.Devices.DataItemRepresentation.DATA_SET:
if (!observation.IsUnavailable)
{
var entries = DataSetObservation.GetEntries(observation.Values);
foreach (var entry in entries)
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : DATA_SET : {entry.Key} = {entry.Value} @ {observation.Timestamp}");
}
}
else
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
}
break;

case MTConnect.Devices.DataItemRepresentation.TABLE:
if (!observation.IsUnavailable)
{
var entries = TableObservation.GetEntries(observation.Values);
foreach (var entry in entries)
{
foreach (var cell in entry.Cells)
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : TABLE : {entry.Key} : {cell.Key} = {cell.Value} @ {observation.Timestamp}");
}
}
}
else
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
}
break;

case MTConnect.Devices.DataItemRepresentation.TIME_SERIES:
if (!observation.IsUnavailable)
{
var samples = TimeSeriesObservation.GetSamples(observation.Values).ToList();
Console.WriteLine($"Observation Received : {observation.DataItemId} : TIME_SERIES : {string.Join(" ", samples)} @ {observation.Timestamp}");
}
else
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
}
break;
}
};
client.AssetReceived += (sender, asset) =>
{
Console.WriteLine($"Asset Received : {asset.AssetId}");
};
client.ClientStarted += (sender, asset) =>
{
Console.WriteLine($"Client Started.");
};
client.ClientStopped += (sender, asset) =>
{
Console.WriteLine($"Client Stopped.");
};

client.Start();

Console.ReadLine();
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<Compile Remove="Assets.cs" />
<Compile Remove="ClientRequests.cs" />
<Compile Remove="CppAgentJsonClient.cs" />
<Compile Remove="CurrentStream.cs" />
<Compile Remove="EntityClient.cs" />
<Compile Remove="SampleStream.cs" />
Expand All @@ -19,6 +20,7 @@
<ItemGroup>
<None Include="Assets.cs" />
<None Include="ClientRequests.cs" />
<None Include="CppAgentJsonClient.cs" />
<None Include="CurrentStream.cs" />
<None Include="EntityClient.cs" />
<None Include="SampleStream.cs" />
Expand All @@ -27,6 +29,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\src\MTConnect.NET-HTTP\MTConnect.NET-HTTP.csproj" />
<ProjectReference Include="..\..\..\src\MTConnect.NET-JSON-cppagent\MTConnect.NET-JSON-cppagent.csproj" />
<ProjectReference Include="..\..\..\src\MTConnect.NET-MQTT\MTConnect.NET-MQTT.csproj" />
</ItemGroup>

</Project>
13 changes: 9 additions & 4 deletions src/MTConnect.NET-Common/Devices/IContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public partial interface IContainer : IMTConnectEntity
/// </summary>
string Hash { get; }

/// <summary>
///
/// </summary>
string Type { get; }

/// <summary>
/// The text description that describes what the Component Type represents
/// </summary>
string TypeDescription { get; }

/// <summary>
/// The text description that describes what the Component Type represents
/// </summary>
string TypeDescription { get; }


/// <summary>
Expand Down
52 changes: 52 additions & 0 deletions src/MTConnect.NET-JSON-cppagent/Devices/JsonDeviceContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Text.Json.Serialization;

namespace MTConnect.Devices.Json
{
public class JsonDeviceContainer
{
[JsonPropertyName("Agent")]
public JsonDevice Agent { get; set; }

[JsonPropertyName("Device")]
public JsonDevice Device { get; set; }


public JsonDeviceContainer() { }

public JsonDeviceContainer(IDevice device)
{
if (device != null)
{
switch (device.Type)
{
case Devices.Agent.TypeId:
Agent = new JsonDevice(device);
break;

default:
Device = new JsonDevice(device);
break;
}
}
}


public IDevice ToDevice()
{
if (Agent != null)
{
return Agent.ToDevice();
}

if (Device != null)
{
return Device.ToDevice();
}

return null;
}
}
}
Loading

0 comments on commit a08a551

Please sign in to comment.