Skip to content

Commit

Permalink
Update v6.3.0 beta
Browse files Browse the repository at this point in the history
- Updated to set InstanceId in MTConnectHttpClient and MTConnectMqttClient
- Updated to fix issue #59
- Updated to fix issue using "at" with Current request
- Fixed issue with Json DataSets and Tables when Unavailable
- Update to add SendBuffer() method in ShdrQueueAdapter and ShdrIntervalQueueAdapter
- Fixed issue with MQTT Device message format
-
  • Loading branch information
PatrickRitchie committed Apr 17, 2024
1 parent 4d39947 commit 74f43b6
Show file tree
Hide file tree
Showing 43 changed files with 920 additions and 465 deletions.
2 changes: 1 addition & 1 deletion MTConnect.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MTConnect.NET-Client-MQTT",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MTConnect.NET-Client-SHDR", "examples\MTConnect.NET-Client-SHDR\MTConnect.NET-Client-SHDR.csproj", "{A9DF36FC-4EF7-4BFB-B47D-F1C10227631A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MTConnect.NET-Agent-Embedded", "examples\MTConnect.NET-Agent-Embedded\MTConnect.NET-Agent-Embedded.csproj", "{24C98CF3-CC93-4696-A036-8FD1E16F2E7E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MTConnect.NET-Agent-Embedded", "examples\MTConnect.NET-Agent-Embedded\MTConnect.NET-Agent-Embedded.csproj", "{24C98CF3-CC93-4696-A036-8FD1E16F2E7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
26 changes: 13 additions & 13 deletions agent/MTConnect.NET-Agent/agent.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,23 @@ modules:
# privateKeyPath: c:\test-cert\trakhound.key
# privateKeyPassword: trakhound

# - mqtt-relay: # - Add MQTT Relay module (Document Structure)
# server: localhost
# port: 1883
# currentInterval: 10000
# sampleInterval: 500
# documentFormat: JSON-CPPAGENT
# topicPrefix: MTConnect/Document
# topicStructure: Document

- mqtt-relay: # - Add MQTT Relay module (Entity Structure)
- mqtt-relay: # - Add MQTT Relay module (Document Structure)
server: localhost
port: 1883
currentInterval: 10000
sampleInterval: 500
documentFormat: JSON
topicPrefix: MTConnect/Entity
topicStructure: Entity
documentFormat: JSON-CPPAGENT-MQTT
topicPrefix: MTConnect/Document
topicStructure: Document

# - mqtt-relay: # - Add MQTT Relay module (Entity Structure)
# server: localhost
# port: 1883
# currentInterval: 10000
# sampleInterval: 500
# documentFormat: JSON
# topicPrefix: MTConnect/Entity
# topicStructure: Entity

# - mqtt-relay: # - Add MQTT Relay module (TLS)
# server: localhost
Expand Down
4 changes: 2 additions & 2 deletions build/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

[assembly: AssemblyVersion("6.2.2")]
[assembly: AssemblyFileVersion("6.2.2")]
[assembly: AssemblyVersion("6.3.0")]
[assembly: AssemblyFileVersion("6.3.0")]
[assembly: AssemblyCompany("TrakHound Inc.")]
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>MTConnect.NET_Agent_Embedded</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 6 additions & 3 deletions examples/MTConnect.NET-Agent-Embedded/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ModuleConfiguration
{
public string DeviceUuid { get; set; }
public string DeviceName { get; set; }
public string SerialNumber { get; set; }
}


Expand Down Expand Up @@ -46,12 +47,14 @@ public Module(IMTConnectAgentBroker agent, object configuration) : base(agent)
protected override IDevice OnAddDevice()
{
var device = new Device();
device.Uuid = "7E647B2D-C6A3-40BF-9CE9-FB09834850C9";
device.Id = "dev-001";
device.Uuid = _configuration.DeviceUuid;
device.Id = _configuration.DeviceName;
device.Name = _configuration.DeviceName;
device.Description = new Description()
{
Manufacturer = "ACME",
Model = "dm-500"
Model = "dm-500",
SerialNumber = _configuration.SerialNumber
};

// Add an Availability DataItem to the Device
Expand Down
3 changes: 3 additions & 0 deletions examples/MTConnect.NET-Agent-Embedded/agent.config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
modules:

- demo:
deviceUuid: 7E647B2D-C6A3-40BF-9CE9-FB09834850C9
deviceName: dev-001
serialNumber: 123456
readInterval: 1000

- http-server:
Expand Down
15 changes: 9 additions & 6 deletions examples/MTConnect.NET-Client-HTTP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ static void Main(string[] args)
static void DocumentClient()
{
//var client = new MTConnectHttpClient("http://mtconnect.mazakcorp.com/", 5719);
var client = new MTConnectHttpClient("localhost", 5000);
//var client = new MTConnectHttpClient("localhost", 5006, "OKUMA-Lathe");
//var client = new MTConnectHttpClient("localhost", 5006);
var client = new MTConnectHttpClient("localhost", 5000, "Okuma-Lathe");
//var client = new MTConnectHttpClient("localhost", 5001);
client.Interval = 100;
//client.Heartbeat = 0;
Expand All @@ -40,8 +42,8 @@ static void DocumentClient()
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");

var validationResult = observation.Validate();
Console.WriteLine($"Observation Validation : {observation.DataItemId} : {validationResult.IsValid} : {validationResult.Message}");
//var validationResult = observation.Validate();
//Console.WriteLine($"Observation Validation : {observation.DataItemId} : {validationResult.IsValid} : {validationResult.Message}");
}
}
}
Expand All @@ -57,8 +59,8 @@ static void DocumentClient()
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");

var validationResult = observation.Validate();
Console.WriteLine($"Observation Validation : {observation.DataItemId} : {validationResult.IsValid} : {validationResult.Message}");
//var validationResult = observation.Validate();
//Console.WriteLine($"Observation Validation : {observation.DataItemId} : {validationResult.IsValid} : {validationResult.Message}");
}
}
}
Expand All @@ -72,7 +74,8 @@ static void DocumentClient()
}
};

client.Start();
client.StartFromBuffer();
//client.Start();
}

static void EntityClient()
Expand Down
89 changes: 49 additions & 40 deletions examples/MTConnect.NET-Client-MQTT/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MTConnect.Formatters;
using MTConnect.Configurations;
using MTConnect.Formatters;
using MTConnect.Observations;

namespace MTConnect.Clients.HTTP
{
Expand All @@ -14,53 +16,60 @@ static void Main(string[] args)

static void DocumentClient()
{
var client = new MTConnectMqttClient("localhost", 1883);
var config = new MTConnectMqttClientConfiguration();
config.Server = "localhost";
config.Port = 1883;
config.TopicPrefix = "MTConnect/Document";

var client = new MTConnectMqttClient(config);
client.ClientStarted += (s, args) => { Console.WriteLine("Client Started"); };
client.ClientStopped += (s, args) => { Console.WriteLine("Client Stopped"); };

//client.MessageReceived += (topic, payload) => Console.WriteLine($"Message Received : {topic} : {payload.Length}");
client.DeviceReceived += (topic, device) => Console.WriteLine($"Device Received : {device.Uuid} : {device.Name}");
client.ObservationReceived += (topic, observation) => Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");

client.ProbeReceived += (s, response) =>
{
foreach (var device in response.Devices) Console.WriteLine($"Device Received : {device.Uuid} : {device.Name}");
};
//client.ProbeReceived += (s, response) =>
//{
// foreach (var device in response.Devices) Console.WriteLine($"Device Received : {device.Uuid} : {device.Name}");
//};

client.CurrentReceived += (s, response) =>
{
foreach (var stream in response.Streams)
{
foreach (var componentStream in stream.ComponentStreams)
{
foreach (var observation in componentStream.Observations)
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");
}
}
}
};
//client.CurrentReceived += (s, response) =>
//{
// foreach (var stream in response.Streams)
// {
// foreach (var componentStream in stream.ComponentStreams)
// {
// foreach (var observation in componentStream.Observations)
// {
// Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");
// }
// }
// }
//};

client.SampleReceived += (s, response) =>
{
foreach (var stream in response.Streams)
{
foreach (var componentStream in stream.ComponentStreams)
{
foreach (var observation in componentStream.Observations)
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");
}
}
}
};
//client.SampleReceived += (s, response) =>
//{
// foreach (var stream in response.Streams)
// {
// foreach (var componentStream in stream.ComponentStreams)
// {
// foreach (var observation in componentStream.Observations)
// {
// Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}");
// }
// }
// }
//};

client.AssetsReceived += (s, response) =>
{
foreach (var asset in response.Assets)
{
var result = EntityFormatter.Format("XML", asset);
//if (result.Success) Console.WriteLine(System.Text.Encoding.UTF8.GetString(result.Content));
}
};
//client.AssetsReceived += (s, response) =>
//{
// foreach (var asset in response.Assets)
// {
// var result = EntityFormatter.Format("XML", asset);
// //if (result.Success) Console.WriteLine(System.Text.Encoding.UTF8.GetString(result.Content));
// }
//};

client.Start();
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/MTConnect.NET-Common/Adapters/IMTConnectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public interface IMTConnectAdapter
/// </summary>
bool SendLast(long timestamp = 0);

bool SendBuffer();


/// <summary>
/// Set all items to Unavailable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ public interface IMTConnectAgentBroker : IMTConnectAgent
/// <returns>MTConnectStreams Response Document</returns>
IStreamsResponseOutputDocument GetDeviceStreamsResponseDocument(ulong at, uint count = 0, Version mtconnectVersion = null, string deviceType = null);

/// <summary>
/// Get a MTConnectStreams Document containing all devices.
/// </summary>
/// <param name="dataItemIds">A list of DataItemId's to specify what observations to include in the response</param>
/// <param name="count">The maximum number of observations to include in the response</param>
/// <returns>MTConnectStreams Response Document</returns>
IStreamsResponseOutputDocument GetDeviceStreamsResponseDocument(IEnumerable<string> dataItemIds, uint count = 0, Version mtconnectVersion = null, string deviceType = null);


/// <summary>
/// Get a MTConnectStreams Document containing all devices.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions libraries/MTConnect.NET-Common/Agents/MTConnectAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class MTConnectAgent : IMTConnectAgent, IDisposable

private readonly ConcurrentDictionary<string, IObservation> _currentObservations = new ConcurrentDictionary<string, IObservation>();
private readonly ConcurrentDictionary<string, IEnumerable<IObservation>> _currentConditions = new ConcurrentDictionary<string, IEnumerable<IObservation>>();
//private readonly ConcurrentDictionary<string, IObservationInput> _currentObservations = new ConcurrentDictionary<string, IObservationInput>();
//private readonly ConcurrentDictionary<string, IEnumerable<IObservationInput>> _currentConditions = new ConcurrentDictionary<string, IEnumerable<IObservationInput>>();
private readonly List<string> _assetIds = new List<string>();
private MTConnectAgentMetrics _metrics;
private readonly string _uuid;
Expand Down
45 changes: 42 additions & 3 deletions libraries/MTConnect.NET-Common/Agents/MTConnectAgentBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public IDevicesResponseDocument GetDevicesResponseDocument(string deviceKey, Ver

#region "Internal"

private IObservationBufferResults GetObservations(IEnumerable<int> bufferKeys, ulong from = 0, ulong to = 0, ulong at = 0, uint count = 0)
private IObservationBufferResults GetObservations(IEnumerable<int> bufferKeys, ulong from = 0, ulong to = 0, ulong? at = null, uint count = 0)
{
IObservationBufferResults results;
if (from > 0 || to > 0)
Expand All @@ -619,9 +619,9 @@ private IObservationBufferResults GetObservations(IEnumerable<int> bufferKeys, u
{
results = _observationBuffer.GetObservations(bufferKeys, count: count);
}
else if (at > 0)
else if (at.HasValue)
{
results = _observationBuffer.GetCurrentObservations(bufferKeys, at);
results = _observationBuffer.GetCurrentObservations(bufferKeys, at.Value);
}
else
{
Expand Down Expand Up @@ -701,6 +701,45 @@ public IStreamsResponseOutputDocument GetDeviceStreamsResponseDocument(ulong at,
return null;
}

/// <summary>
/// Get a MTConnectStreams Document containing all devices.
/// </summary>
/// <param name="dataItemIds">A list of DataItemId's to specify what observations to include in the response</param>
/// <param name="count">The maximum number of observations to include in the response</param>
/// <returns>MTConnectStreams Response Document</returns>
public IStreamsResponseOutputDocument GetDeviceStreamsResponseDocument(IEnumerable<string> dataItemIds, uint count = 0, Version mtconnectVersion = null, string deviceType = null)
{
StreamsRequestReceived?.Invoke(null);

if (_observationBuffer != null)
{
var devices = GetDevices(deviceType);
if (!devices.IsNullOrEmpty())
{
// Create list of BufferKeys
var bufferKeys = new List<int>();
foreach (var device in devices)
{
var deviceBufferKeys = GenerateBufferKeys(device.Uuid, dataItemIds);
if (!deviceBufferKeys.IsNullOrEmpty()) bufferKeys.AddRange(deviceBufferKeys);
}

// Query the Observation Buffer
var results = GetObservations(bufferKeys, count: count);

// Create Response Document
var document = CreateDeviceStreamsDocument(devices, ref results, mtconnectVersion);
if (document != null)
{
StreamsResponseSent?.Invoke(this, new EventArgs());
return document;
}
}
}

return null;
}

/// <summary>
/// Get a MTConnectStreams Document containing all devices.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,14 @@ public IObservationBufferResults GetCurrentObservations(IEnumerable<int> bufferK

lock (_lock)
{
firstSequence = Math.Max(1, _sequence - BufferSize);
firstSequence = _sequence > BufferSize ? _sequence - BufferSize : 1;
lastSequence = _sequence > 1 ? _sequence - 1 : 1;
nextSequence = _sequence;

// Determine Indexes
var atIndex = (int)(at - firstSequence);
int atIndex;
if (at < 1) atIndex = 0;
else atIndex = (int)(at - firstSequence);

if (_archiveObservations.Size > 0)
{
Expand All @@ -420,13 +422,21 @@ public IObservationBufferResults GetCurrentObservations(IEnumerable<int> bufferK
// Get From Current Observations
if (_currentObservations.TryGetValue(oBufferKeys[i], out var observation))
{
observations[i] = observation;
if (observation.IsValid)
{
if (at < 1 && observation.Sequence <= firstSequence) observations[i] = observation;
else if (observation.Sequence <= at) observations[i] = observation;
}
}

// Get From Current Observations
if (_currentConditions.TryGetValue(oBufferKeys[i], out var conditions))
{
observations[i] = observation;
if (observation.IsValid)
{
if (at < 1 && observation.Sequence <= firstSequence) observations[i] = observation;
else if (observation.Sequence <= at) observations[i] = observation;
}
}
}
}
Expand Down Expand Up @@ -483,7 +493,6 @@ public IObservationBufferResults GetObservations(IEnumerable<int> bufferKeys, ul
nextSequence = _sequence;

// Determine Indexes
//var fromIndex = (int)Math.Max(0, from - firstSequence);
int fromIndex = from > firstSequence ? (int)(from - firstSequence) : 0;
int toIndex = (int)(lastSequence - firstSequence);
if (to > 0)
Expand Down
Loading

0 comments on commit 74f43b6

Please sign in to comment.