Skip to content

Commit

Permalink
Fixed issue with Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 27, 2023
1 parent 7c4bcfe commit 96971f9
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 55 deletions.
8 changes: 7 additions & 1 deletion libraries/MTConnect.NET-Common/Adapters/MTConnectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class MTConnectAdapter : IMTConnectAdapter
/// </summary>
public bool OutputTimestamps { get; set; }

public bool IgnoreTimestamps { get; set; }


public Func<IEnumerable<IObservationInput>, bool> WriteObservationsFunction { get; set; }

public Func<IEnumerable<IAssetInput>, bool> WriteAssetsFunction { get; set; }
Expand Down Expand Up @@ -271,7 +274,7 @@ public void AddObservation(IObservationInput observation)
newObservation.DeviceKey = DeviceKey;

// Set Timestamp (if not already set)
if (newObservation.Timestamp <= 0) newObservation.Timestamp = UnixDateTime.Now;
if (newObservation.Timestamp <= 0 && !IgnoreTimestamps) newObservation.Timestamp = UnixDateTime.Now;

// Get the Current Observation (if exists)
IObservationInput currentObservation;
Expand Down Expand Up @@ -414,6 +417,8 @@ protected void UpdateLastObservations(IEnumerable<IObservationInput> observation

protected bool WriteChangedObservations()
{
var now = UnixDateTime.Now;

// Get a list of all Current Observations
List<IObservationInput> dataItems;
lock (_lock)
Expand All @@ -433,6 +438,7 @@ protected bool WriteChangedObservations()

var sendItem = new ObservationInput(item);
if (!OutputTimestamps) sendItem.Timestamp = 0;
else if (IgnoreTimestamps) sendItem.Timestamp = now;
dataItems.Add(sendItem);
}
}
Expand Down
222 changes: 168 additions & 54 deletions libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using MTConnect.Shdr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
Expand All @@ -18,7 +19,7 @@ namespace MTConnect.Adapters
/// <summary>
/// An Adapter class for communicating with an MTConnect Agent using the SHDR protocol.
/// Supports multiple concurrent Agent connections.
/// Uses a queue to collect changes to Observations and sends the most recent changes at the specified interval.
/// Uses a queue to collect changes to Observations and Assets and sends the most recent changes at the specified interval.
/// </summary>
public class ShdrAdapter
{
Expand Down Expand Up @@ -134,7 +135,10 @@ public ShdrAdapter(int port = 7878, int heartbeat = 10000)
Timeout = 5000;

var adapter = new MTConnectAdapter(null, false);
adapter.WriteObservationsFunction = WriteDataItems;
adapter.OutputTimestamps = OutputTimestamps;
adapter.WriteObservationsFunction = WriteObservations;
adapter.WriteAssetsFunction = WriteAssets;
adapter.WriteDevicesFunction = WriteDevices;
_adapter = adapter;

_connectionListener = new AgentClientConnectionListener(Port, heartbeat);
Expand All @@ -155,7 +159,10 @@ public ShdrAdapter(string deviceKey, int port = 7878, int heartbeat = 10000)
Timeout = 5000;

var adapter = new MTConnectAdapter(null, false);
adapter.WriteObservationsFunction = WriteDataItems;
adapter.OutputTimestamps = OutputTimestamps;
adapter.WriteObservationsFunction = WriteObservations;
adapter.WriteAssetsFunction = WriteAssets;
adapter.WriteDevicesFunction = WriteDevices;
_adapter = adapter;

_connectionListener = new AgentClientConnectionListener(Port, heartbeat);
Expand All @@ -179,7 +186,11 @@ public ShdrAdapter(ShdrAdapterClientConfiguration configuration)
Timeout = 5000;

var adapter = new MTConnectAdapter(null, false);
adapter.WriteObservationsFunction = WriteDataItems;
adapter.IgnoreTimestamps = configuration.IgnoreTimestamps;
adapter.OutputTimestamps = OutputTimestamps;
adapter.WriteObservationsFunction = WriteObservations;
adapter.WriteAssetsFunction = WriteAssets;
adapter.WriteDevicesFunction = WriteDevices;
_adapter = adapter;

_connectionListener = new AgentClientConnectionListener(Port, Heartbeat);
Expand All @@ -200,7 +211,10 @@ protected ShdrAdapter(int port = 7878, int heartbeat = 10000, int? interval = nu
Timeout = 5000;

var adapter = new MTConnectAdapter(interval, bufferEnabled);
adapter.WriteObservationsFunction = WriteDataItems;
adapter.OutputTimestamps = OutputTimestamps;
adapter.WriteObservationsFunction = WriteObservations;
adapter.WriteAssetsFunction = WriteAssets;
adapter.WriteDevicesFunction = WriteDevices;
_adapter = adapter;

_connectionListener = new AgentClientConnectionListener(Port, heartbeat);
Expand All @@ -221,7 +235,10 @@ protected ShdrAdapter(string deviceKey, int port = 7878, int heartbeat = 10000,
Timeout = 5000;

var adapter = new MTConnectAdapter(interval, bufferEnabled);
adapter.WriteObservationsFunction = WriteDataItems;
adapter.OutputTimestamps = OutputTimestamps;
adapter.WriteObservationsFunction = WriteObservations;
adapter.WriteAssetsFunction = WriteAssets;
adapter.WriteDevicesFunction = WriteDevices;
_adapter = adapter;

_connectionListener = new AgentClientConnectionListener(Port, heartbeat);
Expand All @@ -245,7 +262,10 @@ protected ShdrAdapter(ShdrAdapterClientConfiguration configuration, int? interva
Timeout = 5000;

var adapter = new MTConnectAdapter(interval, bufferEnabled);
adapter.WriteObservationsFunction = WriteDataItems;
adapter.IgnoreTimestamps = configuration.IgnoreTimestamps;
adapter.WriteObservationsFunction = WriteObservations;
adapter.WriteAssetsFunction = WriteAssets;
adapter.WriteDevicesFunction = WriteDevices;
_adapter = adapter;

_connectionListener = new AgentClientConnectionListener(Port, Heartbeat);
Expand Down Expand Up @@ -410,80 +430,180 @@ protected virtual void OnLastSent() { }

#region "Write"

protected bool WriteLine(string line)
private bool WriteObservations(IEnumerable<IObservationInput> observations)
{
if (!string.IsNullOrEmpty(line))
// DataItems
var dataItems = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.DataItem);
if (!dataItems.IsNullOrEmpty())
{
try
var shdrDataItems = new List<ShdrDataItem>();
foreach (var x in dataItems) shdrDataItems.Add(new ShdrDataItem(x));
var shdrLine = ShdrDataItem.ToString(shdrDataItems, !OutputTimestamps);
if (!WriteLine(shdrLine)) return false;
}

// Messages
var messages = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.Message);
if (!messages.IsNullOrEmpty())
{
foreach (var x in messages)
{
// Write Line to each client in stored client list
var clients = GetAgentClients();
if (!clients.IsNullOrEmpty())
{
var success = true;
var shdrModel = new ShdrMessage(x);
var shdrLine = shdrModel.ToString();
if (!WriteLine(shdrLine)) return false;
}
}

foreach (var client in clients)
{
if (!WriteLineToClient(client, line)) success = false;
}
// Conditions
var conditions = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.Condition);
if (!conditions.IsNullOrEmpty())
{
foreach (var x in conditions)
{
var shdrModel = new ShdrFaultState(new ConditionFaultStateObservationInput(x));
var shdrLine = shdrModel.ToString();
if (!WriteLine(shdrLine)) return false;
}
}

return success;
}
// DataSets
var dataSets = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.DataSet);
if (!dataSets.IsNullOrEmpty())
{
foreach (var x in dataSets)
{
var shdrModel = new ShdrDataSet(new DataSetObservationInput(x));
var shdrLine = shdrModel.ToString();
if (!WriteLine(shdrLine)) return false;
}
catch { }
}

return false;
}
// Tables
var tables = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.Table);
if (!tables.IsNullOrEmpty())
{
foreach (var x in tables)
{
var shdrModel = new ShdrTable(new TableObservationInput(x));
var shdrLine = shdrModel.ToString();
if (!WriteLine(shdrLine)) return false;
}
}

private bool WriteLine(string clientId, string line)
{
if (!string.IsNullOrEmpty(line))
// TimeSeries
var timeSeries = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.TimeSeries);
if (!timeSeries.IsNullOrEmpty())
{
var client = GetAgentClient(clientId);
if (client != null)
foreach (var x in timeSeries)
{
return WriteLineToClient(client, line);
var shdrModel = new ShdrTimeSeries(new TimeSeriesObservationInput(x));
var shdrLine = shdrModel.ToString();
if (!WriteLine(shdrLine)) return false;
}
}

return false;
return true;
}

private async Task<bool> WriteLineAsync(string line)
private bool WriteAssets(IEnumerable<IAssetInput> assets)
{
if (!string.IsNullOrEmpty(line))
foreach (var asset in assets)
{
// Write Line to each client in stored client list
var clients = GetAgentClients();
if (!clients.IsNullOrEmpty())
{
foreach (var client in clients)
{
await WriteLineToClientAsync(client, line);
}
var shdrModel = new ShdrAsset(asset.Asset);
var shdrLine = shdrModel.ToString(MultilineAssets);
if (!WriteLine(shdrLine)) return false;
}

return true;
}
return true;
}

private bool WriteDevices(IEnumerable<IDeviceInput> devices)
{
foreach (var device in devices)
{
var shdrModel = new ShdrDevice(device.Device);
var shdrLine = shdrModel.ToString(MultilineDevices);
if (!WriteLine(shdrLine)) return false;
}

return false;
return true;
}

private async Task<bool> WriteLineAsync(string clientId, string line)

protected bool WriteLine(string line)
{
if (!string.IsNullOrEmpty(line))
{
var client = GetAgentClient(clientId);
if (client != null)
try
{
return await WriteLineToClientAsync(client, line);
// Write Line to each client in stored client list
var clients = GetAgentClients();
if (!clients.IsNullOrEmpty())
{
var success = true;

foreach (var client in clients)
{
if (!WriteLineToClient(client, line)) success = false;
}

return success;
}
}
catch { }
}

return false;
}

//private bool WriteLine(string clientId, string line)
//{
// if (!string.IsNullOrEmpty(line))
// {
// var client = GetAgentClient(clientId);
// if (client != null)
// {
// return WriteLineToClient(client, line);
// }
// }

// return false;
//}

//private async Task<bool> WriteLineAsync(string line)
//{
// if (!string.IsNullOrEmpty(line))
// {
// // Write Line to each client in stored client list
// var clients = GetAgentClients();
// if (!clients.IsNullOrEmpty())
// {
// foreach (var client in clients)
// {
// await WriteLineToClientAsync(client, line);
// }

// return true;
// }
// }

// return false;
//}

//private async Task<bool> WriteLineAsync(string clientId, string line)
//{
// if (!string.IsNullOrEmpty(line))
// {
// var client = GetAgentClient(clientId);
// if (client != null)
// {
// return await WriteLineToClientAsync(client, line);
// }
// }

// return false;
//}


private bool WriteLineToClient(AgentClient client, string line)
{
Expand Down Expand Up @@ -693,12 +813,6 @@ public bool SendDataItem(IObservationInput observation)
return _adapter.SendObservation(observation);
}


private bool WriteDataItems(IEnumerable<IObservationInput> observations)
{
return true;
}

#endregion

#region "Messages"
Expand Down

0 comments on commit 96971f9

Please sign in to comment.