Skip to content

Commit

Permalink
Updated Static Response Handler and Fixed issue with root DeviceKey r…
Browse files Browse the repository at this point in the history
…equest
  • Loading branch information
PatrickRitchie committed May 20, 2023
1 parent b1010d9 commit 08c26ba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 388 deletions.
43 changes: 22 additions & 21 deletions src/MTConnect.NET-Applications-Agents/MTConnectHttpAgentServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MTConnectHttpAgentServer : MTConnectHttpServer
private readonly IHttpAgentApplicationConfiguration _agentConfiguration;


public MTConnectHttpAgentServer(IHttpAgentApplicationConfiguration configuration, IMTConnectAgentBroker mtconnectAgent, IEnumerable<string> prefixes = null, int port = 0) : base(configuration, mtconnectAgent, prefixes, port)
public MTConnectHttpAgentServer(IHttpAgentApplicationConfiguration configuration, IMTConnectAgentBroker mtconnectAgent) : base(configuration, mtconnectAgent)
{
_agentConfiguration = configuration;
}
Expand All @@ -43,32 +43,33 @@ protected override void OnConfigureServer(ServerConfig serverConfig)

protected override byte[] OnProcessStatic(MTConnectStaticFileRequest request)
{
if (_agentConfiguration.DevicesStyle != null && (request.LocalPath == _agentConfiguration.DevicesStyle.Location || request.LocalPath.Replace('\\', '/') == _agentConfiguration.StreamsStyle.Location))
if (request.LocalPath != null)
{
return ReadDevicesStylesheet(request.FilePath, request.Version);
}
else if (_agentConfiguration.StreamsStyle != null && (request.LocalPath == _agentConfiguration.StreamsStyle.Location || request.LocalPath.Replace('\\', '/') == _agentConfiguration.StreamsStyle.Location))
{
return ReadStreamsStylesheet(request.FilePath, request.Version);
if (_agentConfiguration.DevicesStyle != null && _agentConfiguration.DevicesStyle.Location != null)
{
var requestPath = !request.LocalPath.StartsWith('/') ? "/" + request.LocalPath : request.LocalPath;

// Check to see if the request is for the Devices Stylesheet set in the Agent Configuration
if (requestPath == _agentConfiguration.DevicesStyle.Location || requestPath.Replace('\\', '/') == _agentConfiguration.DevicesStyle.Location)
{
return ReadDevicesStylesheet(request.FilePath, request.Version);
}
}
else if (_agentConfiguration.StreamsStyle != null && _agentConfiguration.StreamsStyle.Location != null)
{
var requestPath = !request.LocalPath.StartsWith('/') ? "/" + request.LocalPath : request.LocalPath;

// Check to see if the request is for the Streams Stylesheet set in the Agent Configuration
if (requestPath == _agentConfiguration.StreamsStyle.Location || requestPath.Replace('\\', '/') == _agentConfiguration.StreamsStyle.Location)
{
return ReadStreamsStylesheet(request.FilePath, request.Version);
}
}
}

return null;
}

//protected override byte[] OnProcessStatic(IHttpRequest httpRequest, string absolutePath, string relativePath, Version version = null)
//{
// if (_agentConfiguration.DevicesStyle != null && (relativePath == _agentConfiguration.DevicesStyle.Location || relativePath.Replace('\\', '/') == _agentConfiguration.StreamsStyle.Location))
// {
// return ReadDevicesStylesheet(absolutePath, version);
// }
// else if (_agentConfiguration.StreamsStyle != null && (relativePath == _agentConfiguration.StreamsStyle.Location || relativePath.Replace('\\', '/') == _agentConfiguration.StreamsStyle.Location))
// {
// return ReadStreamsStylesheet(absolutePath, version);
// }

// return null;
//}

protected override List<KeyValuePair<string, string>> OnCreateFormatOptions(MTConnectFormatOptionsArgs args)
{
var x = new List<KeyValuePair<string, string>>();
Expand Down
82 changes: 48 additions & 34 deletions src/MTConnect.NET-HTTP/Servers/MTConnectHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ public class MTConnectHttpServer : IDisposable
public event EventHandler<Exception> ClientException;


public MTConnectHttpServer(
IHttpAgentConfiguration configuration,
IMTConnectAgentBroker mtconnectAgent,
IEnumerable<string> prefixes = null,
int port = 0
)
public MTConnectHttpServer(IHttpAgentConfiguration configuration, IMTConnectAgentBroker mtconnectAgent)
{
_mtconnectAgent = mtconnectAgent;
_configuration = configuration;
Expand All @@ -84,35 +79,29 @@ public MTConnectHttpServer(
/// Method run when an Observation is attempted to be added to the MTConnect Agent from an HTTP PUT request
/// </summary>
/// <returns>Returns False if a Device cannot be found from the specified DeviceKey</returns>
protected virtual bool OnObservationInput(string deviceKey, string dataItemKey, string input)
{
return false;
}
protected virtual bool OnObservationInput(string deviceKey, string dataItemKey, string input) { return false; }

/// <summary>
/// Method run when an Asset is attempted to be added to the MTConnect Agent from an HTTP PUT request
/// </summary>
/// <returns>Returns False if a Device cannot be found from the specified DeviceKey</returns>
protected virtual bool OnAssetInput(string assetId, string deviceKey, string assetType, byte[] requestBody, string documentFormat = DocumentFormat.XML)
{
return false;
}
protected virtual bool OnAssetInput(string assetId, string deviceKey, string assetType, byte[] requestBody, string documentFormat = DocumentFormat.XML) { return false; }

/// <summary>
/// Method run after the initial ServerConfig is set but before the server is started. Used to edit the configuration and/or to add routes
/// </summary>
protected virtual void OnConfigureServer(ServerConfig serverConfig)
{

}
protected virtual void OnConfigureServer(ServerConfig serverConfig) { }

/// <summary>
/// Method run on a Static File request
/// </summary>
protected virtual byte[] OnProcessStatic(MTConnectStaticFileRequest request) { return null; }

/// <summary>
/// Method run when creating the Format Options after an MTConnect REST response is processed and before it is returned
/// </summary>
protected virtual List<KeyValuePair<string, string>> OnCreateFormatOptions(MTConnectFormatOptionsArgs args) { return null; }

//protected virtual byte[] OnProcessStatic(IHttpRequest httpRequest, string absolutePath, string relativePath, Version version = null) { return null; }



public void Start()
{
Expand Down Expand Up @@ -219,10 +208,6 @@ private ServerConfig CreateServerConfig()
probeHandler.ClientDisconnected += ClientDisconnected;
probeHandler.ClientException += ClientException;
probeHandler.CreateFormatOptionsFunction = OnCreateFormatOptions;
serverConfig.AddRoute("/", probeHandler);
//serverConfig.AddRoute("[^\\/((?!probe|current|sample|assets).)*$]", probeHandler);
serverConfig.AddRoute("/probe", probeHandler);
serverConfig.AddRoute("/*/probe", probeHandler);

// Setup the Current Request Handler
var currentHandler = new MTConnectCurrentResponseHandler(_configuration, _mtconnectAgent);
Expand All @@ -231,8 +216,6 @@ private ServerConfig CreateServerConfig()
currentHandler.ClientDisconnected += ClientDisconnected;
currentHandler.ClientException += ClientException;
currentHandler.CreateFormatOptionsFunction = OnCreateFormatOptions;
serverConfig.AddRoute("/current", currentHandler);
serverConfig.AddRoute("/*/current", currentHandler);

// Setup the Sample Request Handler
var sampleHandler = new MTConnectSampleResponseHandler(_configuration, _mtconnectAgent);
Expand All @@ -241,8 +224,6 @@ private ServerConfig CreateServerConfig()
sampleHandler.ClientDisconnected += ClientDisconnected;
sampleHandler.ClientException += ClientException;
sampleHandler.CreateFormatOptionsFunction = OnCreateFormatOptions;
serverConfig.AddRoute("/sample", sampleHandler);
serverConfig.AddRoute("/*/sample", sampleHandler);

// Setup the Assets Request Handler
var assetsHandler = new MTConnectAssetsResponseHandler(_configuration, _mtconnectAgent);
Expand All @@ -251,8 +232,6 @@ private ServerConfig CreateServerConfig()
assetsHandler.ClientDisconnected += ClientDisconnected;
assetsHandler.ClientException += ClientException;
assetsHandler.CreateFormatOptionsFunction = OnCreateFormatOptions;
serverConfig.AddRoute("/assets", assetsHandler);
serverConfig.AddRoute("/*/assets", assetsHandler);

// Setup the Asset Request Handler
var assetHandler = new MTConnectAssetResponseHandler(_configuration, _mtconnectAgent);
Expand All @@ -261,7 +240,6 @@ private ServerConfig CreateServerConfig()
assetHandler.ClientDisconnected += ClientDisconnected;
assetHandler.ClientException += ClientException;
assetHandler.CreateFormatOptionsFunction = OnCreateFormatOptions;
serverConfig.AddRoute("/asset/*", assetHandler);

// Setup the Static Request Handler
var staticHandler = new MTConnectStaticResponseHandler(_configuration, _mtconnectAgent);
Expand All @@ -271,10 +249,46 @@ private ServerConfig CreateServerConfig()
staticHandler.ClientException += ClientException;
staticHandler.ProcessFunction = OnProcessStatic;
staticHandler.CreateFormatOptionsFunction = OnCreateFormatOptions;
//staticHandler.FileRequested += StaticFileRequested;
serverConfig.AddRoute("[^\\/((?!probe|current|sample|assets).)*$]", staticHandler);


// Setup Routes (Processed in Order)
serverConfig.AddRoute("/", probeHandler);
serverConfig.AddRoute("/probe", probeHandler);
serverConfig.AddRoute("/*/probe", probeHandler);
serverConfig.AddRoute("/current", currentHandler);
serverConfig.AddRoute("/*/current", currentHandler);
serverConfig.AddRoute("/sample", sampleHandler);
serverConfig.AddRoute("/*/sample", sampleHandler);
serverConfig.AddRoute("/assets", assetsHandler);
serverConfig.AddRoute("/*/assets", assetsHandler);
serverConfig.AddRoute("/asset/*", assetHandler);
serverConfig.AddRoute(async (context) =>
{
return await DeviceRootHandler(probeHandler, context);
});
serverConfig.AddRoute(staticHandler);

return serverConfig;
}

private async Task<bool> DeviceRootHandler(MTConnectProbeResponseHandler probeHandler, IHttpContext context)
{
if (context != null && context.Request != null && context.Request.Path != null)
{
var deviceKey = context.Request.Path.Trim('/');
if (!string.IsNullOrEmpty(deviceKey))
{
var device = _mtconnectAgent.GetDevice(deviceKey);
if (device != null)
{
await probeHandler.HandleAsync(context);

return true;
}
}
}

return false;
}
}
}
Loading

0 comments on commit 08c26ba

Please sign in to comment.