forked from mtconnect/dot_net_sdk
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Adapter and Agent commands (#13)
* Implemented Adapter and Agent commands - Refactored accepted TCP commands into static classes * Timestamp Updates - Update to TimeSeries DataItem to try and fix it's implementation - Added `TimestampAttribute` to try and help address potential inaccuracies of Timestamps sourced from the Adapter. Timestamps can now be supplied by the implementation. For example, if the Timestamp is provided by the `IAdapterSource` - Future discussions should be had about the potential for compensated Timestamps due to various Timestamp Drifts (now can be indicated with the `TimestampAttribute`) - Version number updates - Converted use of `DateTime.UtcNow` to use a static helper method that may be an option to override in the future (see Timestamp Drifts)
- Loading branch information
Showing
21 changed files
with
628 additions
and
118 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
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,94 @@ | ||
using System; | ||
using System.Net.NetworkInformation; | ||
using System.Reflection; | ||
|
||
namespace Mtconnect | ||
{ | ||
/// <summary> | ||
/// A collection of methods that return formatted responses to commands issued from a MTConnect Agent. | ||
/// </summary> | ||
public static class AdapterCommands | ||
{ | ||
/// <summary> | ||
/// Determines the appropriate response to the provided <paramref name="message"/>. | ||
/// </summary> | ||
/// <param name="adapter">Reference to the Adapter implementation.</param> | ||
/// <param name="message">Reference to the command issued from the MTConnect Agent.</param> | ||
/// <returns>Formatted response to the command issued by the MTConnect Agent. If the command was not recognized, then the response string is empty.</returns> | ||
public static string GetResponse(this Adapter adapter, string message) | ||
{ | ||
const string PING = "* PING"; | ||
const string GET_DATAITEMS = "* dataItems"; | ||
const string GET_DATAITEM_VALUE = "* dataItem "; | ||
const string GET_DATAITEM_DESCRIPTION = "* dataItemDescription "; | ||
|
||
message = message?.Trim(); | ||
if (message.StartsWith(PING, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return AdapterCommands.Ping(adapter); | ||
} | ||
else if (message.Equals(GET_DATAITEMS, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return AdapterCommands.DataItems(adapter); | ||
} | ||
else if (message.StartsWith(GET_DATAITEM_DESCRIPTION, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return AdapterCommands.DataItemDescription(adapter, message); | ||
} | ||
else if (message.StartsWith(GET_DATAITEM_VALUE, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return AdapterCommands.DataItem(adapter, message); | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Handles the "<c>* PONG <heartbeat></c>" command to the MTConnect Agent | ||
/// </summary> | ||
/// <returns>Informs the agent of all dataItems that can be published by the adapter</returns> | ||
public static string Ping(Adapter adapter) | ||
=> AgentCommands.Pong(adapter.Heartbeat); | ||
|
||
/// <summary> | ||
/// Handles the "<c>* dataItems: XXX</c>" command to the MTConnect Agent | ||
/// </summary> | ||
/// <returns>Informs the agent of all dataItems that can be published by the adapter</returns> | ||
public static string DataItems(Adapter adapter) | ||
=> $"* dataItems: {string.Join("|", adapter.GetDataItemNames())}"; | ||
|
||
/// <summary> | ||
/// Handles the "<c>* dataItemDescription: XXX</c>" command to the MTConnect Agent | ||
/// </summary> | ||
/// <returns>Informs the agent of the DataItem description, if any, for the provided DataItem name</returns> | ||
public static string DataItemDescription(Adapter adapter, string message) | ||
{ | ||
string dataItemName = message.Remove(0, message.LastIndexOf(' ') + 1); | ||
if (!adapter.Contains(dataItemName)) | ||
{ | ||
return AgentCommands.Error($"Cannot find DataItem '{dataItemName}'"); | ||
} | ||
|
||
if (string.IsNullOrEmpty(adapter[dataItemName].Description)) | ||
{ | ||
return AgentCommands.Error($"No description available for DataItem '{dataItemName}'"); | ||
} | ||
|
||
return $"* dataItemDescription: {adapter[dataItemName].Description}"; | ||
} | ||
|
||
/// <summary> | ||
/// Handles the "<c>* dataItem: XXX</c>" command to the MTConnect Agent | ||
/// </summary> | ||
/// <returns>Informs the agent of the current value for the provided DataItem</returns> | ||
public static string DataItem(Adapter adapter, string message) | ||
{ | ||
string dataItemName = message.Remove(0, message.LastIndexOf(' ') + 1); | ||
if (!adapter.Contains(dataItemName)) | ||
{ | ||
return AgentCommands.Error($"Cannot find DataItem '{dataItemName}'"); | ||
} | ||
return $"* dataItem: {adapter[dataItemName]}"; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.