From 96971f91b0345560f0728b52523adb449ca760fc Mon Sep 17 00:00:00 2001 From: Patrick Ritchie Date: Tue, 26 Dec 2023 22:22:30 -0500 Subject: [PATCH] Fixed issue with Adapter --- .../Adapters/MTConnectAdapter.cs | 8 +- .../Adapters/ShdrAdapter.cs | 222 +++++++++++++----- 2 files changed, 175 insertions(+), 55 deletions(-) diff --git a/libraries/MTConnect.NET-Common/Adapters/MTConnectAdapter.cs b/libraries/MTConnect.NET-Common/Adapters/MTConnectAdapter.cs index 4ae1d7d0..986c850e 100644 --- a/libraries/MTConnect.NET-Common/Adapters/MTConnectAdapter.cs +++ b/libraries/MTConnect.NET-Common/Adapters/MTConnectAdapter.cs @@ -59,6 +59,9 @@ public class MTConnectAdapter : IMTConnectAdapter /// public bool OutputTimestamps { get; set; } + public bool IgnoreTimestamps { get; set; } + + public Func, bool> WriteObservationsFunction { get; set; } public Func, bool> WriteAssetsFunction { get; set; } @@ -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; @@ -414,6 +417,8 @@ protected void UpdateLastObservations(IEnumerable observation protected bool WriteChangedObservations() { + var now = UnixDateTime.Now; + // Get a list of all Current Observations List dataItems; lock (_lock) @@ -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); } } diff --git a/libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs b/libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs index bf914600..5440af8f 100644 --- a/libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs +++ b/libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs @@ -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; @@ -18,7 +19,7 @@ namespace MTConnect.Adapters /// /// 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. /// public class ShdrAdapter { @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -410,80 +430,180 @@ protected virtual void OnLastSent() { } #region "Write" - protected bool WriteLine(string line) + private bool WriteObservations(IEnumerable observations) { - if (!string.IsNullOrEmpty(line)) + // DataItems + var dataItems = observations.Where(o => ShdrObservation.GetObservationType(o) == ShdrObservationType.DataItem); + if (!dataItems.IsNullOrEmpty()) { - try + var shdrDataItems = new List(); + 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 WriteLineAsync(string line) + private bool WriteAssets(IEnumerable 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 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 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 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 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) { @@ -693,12 +813,6 @@ public bool SendDataItem(IObservationInput observation) return _adapter.SendObservation(observation); } - - private bool WriteDataItems(IEnumerable observations) - { - return true; - } - #endregion #region "Messages"