Skip to content

Commit

Permalink
Fixed some issues with merging master
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Oct 25, 2023
1 parent 921fc66 commit 6e4c0b6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 50 deletions.
20 changes: 10 additions & 10 deletions src/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public void StartAgent(IAgentApplicationConfiguration configuration, bool verbos
_mtconnectAgent.AssetsRequestReceived += AssetsRequested;
_mtconnectAgent.DeviceAssetsRequestReceived += DeviceAssetsRequested;
_mtconnectAgent.AssetsResponseSent += AssetsSent;
_mtconnectAgent.ObservationAdded += ObservationAdded;
//_mtconnectAgent.ObservationAdded += ObservationAdded;
_mtconnectAgent.InvalidComponentAdded += InvalidComponent;
_mtconnectAgent.InvalidCompositionAdded += InvalidComposition;
_mtconnectAgent.InvalidDataItemAdded += InvalidDataItem;
Expand Down Expand Up @@ -547,7 +547,7 @@ private void StartMetrics()
assetLastCount = assetCount;
};
_metricsTimer.Start();
}
}
}

#endregion
Expand Down Expand Up @@ -605,25 +605,25 @@ protected virtual void OnPrintHelpArguments() { }

private void DevicesRequested(string deviceName)
{
_agentLogger.Trace($"[Agent] : MTConnectDevices Requested : " + deviceName);
_agentLogger.Debug($"[Agent] : MTConnectDevices Requested : " + deviceName);
}

private void DevicesSent(IDevicesResponseDocument document)
{
if (document != null && document.Header != null)
{
_agentLogger.Trace($"[Agent] : MTConnectDevices Sent : " + document.Header.CreationTime);
_agentLogger.Log(_logLevel, $"[Agent] : MTConnectDevices Sent : " + document.Header.CreationTime);
}
}

private void StreamsRequested(string deviceName)
{
_agentLogger.Trace($"[Agent] : MTConnectStreams Requested : " + deviceName);
_agentLogger.Debug($"[Agent] : MTConnectStreams Requested : " + deviceName);
}

private void StreamsSent(object sender, EventArgs args)
{
_agentLogger.Trace("[Agent] : MTConnectStreams Sent");
_agentLogger.Log(_logLevel, "[Agent] : MTConnectStreams Sent");
}

private void AssetsRequested(IEnumerable<string> assetIds)
Expand All @@ -634,20 +634,20 @@ private void AssetsRequested(IEnumerable<string> assetIds)
string.Join(";", assetIds.ToArray());
}

_agentLogger.Trace($"[Agent] : MTConnectAssets Requested : AssetIds = " + ids);
_agentLogger.Debug($"[Agent] : MTConnectAssets Requested : AssetIds = " + ids);
}

private void DeviceAssetsRequested(string deviceUuid)
{
_agentLogger.Trace($"[Agent] : MTConnectAssets Requested : DeviceUuid = " + deviceUuid);
_agentLogger.Debug($"[Agent] : MTConnectAssets Requested : DeviceUuid = " + deviceUuid);
}


private void AssetsSent(IAssetsResponseDocument document)
{
if (document != null && document.Header != null)
{
_agentLogger.Trace($"[Agent] : MTConnectAssets Sent : " + document.Header.CreationTime);
_agentLogger.Log(_logLevel, $"[Agent] : MTConnectAssets Sent : " + document.Header.CreationTime);
}
}

Expand All @@ -668,7 +668,7 @@ private void ObservationAdded(object sender, IObservation observation)
{
foreach (var value in observation.Values)
{
_agentLogger.Trace($"[Agent] : Observation Added Successfully : {observation.DeviceUuid} : {observation.DataItemId} : {value.Key} = {value.Value}");
_agentLogger.Debug($"[Agent] : Observation Added Successfully : {observation.DeviceUuid} : {observation.DataItemId} : {value.Key} = {value.Value}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using Ceen;
using MTConnect.Configurations;
using MTConnect.Servers.Http;
using NLog;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace MTConnect.Applications.Agents
Expand Down Expand Up @@ -82,34 +83,35 @@ protected override void OnAgentConfigurationUpdated(AgentConfiguration configura
_configuration = configuration as IHttpAgentApplicationConfiguration;
}

protected virtual MTConnectHttpAgentServer OnHttpServerInitialize(int port)
protected virtual MTConnectHttpAgentServer OnHttpServerInitialize()
{
return new MTConnectHttpAgentServer(_configuration, Agent, null, port);
return new MTConnectHttpAgentServer(_configuration, Agent);
}

protected override void OnStartAgentBeforeLoad(IEnumerable<DeviceConfiguration> devices, bool initializeDataItems = false)
protected override void OnStartAgentBeforeLoad(IEnumerable<DeviceConfiguration> devices, bool initializeDataItems = false)
{
// Intialize the Http Server
_httpServer = OnHttpServerInitialize(_port);
_httpServer = OnHttpServerInitialize();

_httpServer.ListenerStarted += HttpListenerStarted;
_httpServer.ListenerStopped += HttpListenerStopped;
_httpServer.ServerStarted += HttpListenerStarted;
_httpServer.ServerStopped += HttpListenerStopped;
_httpServer.ServerCertificateLoaded += HttpServerCertificateLoaded;

// Setup Http Server Logging
if (_verboseLogging)
{
_httpServer.ListenerException += HttpListenerException;
_httpServer.ClientConnected += HttpClientConnected;
_httpServer.ClientDisconnected += HttpClientDisconnected;
_httpServer.ClientException += HttpClientException;
_httpServer.ResponseSent += HttpResponseSent;
}
//if (_verboseLogging)
//{
_httpServer.ServerException += HttpListenerException;
_httpServer.ClientConnected += HttpClientConnected;
_httpServer.ClientDisconnected += HttpClientDisconnected;
_httpServer.ClientException += HttpClientException;
_httpServer.ResponseSent += HttpResponseSent;
//}

// Start the Http Server
_httpServer.Start();
}

protected override void OnStopAgent()
protected override void OnStopAgent()
{
if (_httpServer != null) _httpServer.Stop();
Thread.Sleep(2000); // Delay 2 seconds to allow Http Server to stop
Expand Down Expand Up @@ -143,14 +145,19 @@ private void HttpListenerStopped(object sender, string prefix)
_httpLogger.Info($"[Http Server] : Listener Stopped for " + prefix);
}

private void HttpServerCertificateLoaded(object sender, X509Certificate2 certificate)
{
_httpLogger.Info($"[Http Server] : TLS Certificate Loaded : {certificate.ToString()}");
}

private void HttpListenerException(object sender, Exception exception)
{
_httpLogger.Warn($"[Http Server] : Listener Exception : " + exception.Message);
}

private void HttpClientConnected(object sender, HttpListenerRequest request)
private void HttpClientConnected(object sender, IHttpRequest request)
{
_httpLogger.Debug($"[Http Server] : Http Client Connected : (" + request.HttpMethod + ") : " + request.LocalEndPoint + " : " + request.Url);
_httpLogger.Debug($"[Http Server] : Http Client Connected : (" + request.Method + ") : " + request.RemoteEndPoint + " : " + request.OriginalPath);
}

private void HttpClientDisconnected(object sender, string remoteEndPoint)
Expand All @@ -166,14 +173,14 @@ private void HttpClientException(object sender, Exception exception)
private void HttpResponseSent(object sender, MTConnectHttpResponse response)
{
var totalTime = response.ResponseDuration + response.FormatDuration + response.WriteDuration;
_httpLogger.Debug($"[Http Server] : Http Response Sent : {response.StatusCode} : {response.ContentType} : Process Time {response.ResponseDuration}ms : Format Time {response.FormatDuration}ms : Write Time {response.WriteDuration}ms : Total Time {totalTime}ms");
_httpLogger.Info($"[Http Server] : Http Response Sent : {response.StatusCode} : {response.ContentType} : Agent Process Time {response.ResponseDuration}ms : Document Format Time {response.FormatDuration}ms : Write Time {response.WriteDuration}ms : Total Response Time {totalTime}ms");

// Format Messages
if (!response.FormatMessages.IsNullOrEmpty())
{
foreach (var message in response.FormatMessages)
{
_agentValidationLogger.Trace($"[Http Server] : Formatter Message : {message}");
_agentValidationLogger.Debug($"[Http Server] : Formatter Message : {message}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void AddAdapter(IShdrAdapterClientConfiguration configuration, IDevice d
{
if (configuration != null)
{
var adapterComponent = new ShdrAdapterComponent(configuration, idSuffix, device , device);
var adapterComponent = new ShdrAdapterComponent(configuration, idSuffix, device, device);

// Add Adapter Component to Agent Device
Agent.Agent.AddAdapterComponent(adapterComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using MTConnect.Configurations;
using MTConnect.Devices.DataItems;
using MTConnect.Formatters;
using MTConnect.Servers;
using MTConnect.Shdr;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -17,31 +17,26 @@ namespace MTConnect.Applications.Agents
/// </summary>
public class MTConnectShdrHttpAgentServer : MTConnectHttpAgentServer
{
public MTConnectShdrHttpAgentServer(
IHttpAgentApplicationConfiguration configuration,
IMTConnectAgentBroker mtconnectAgent,
IEnumerable<string> prefixes = null,
int port = 0
)
: base(configuration, mtconnectAgent, prefixes, port) { }
public MTConnectShdrHttpAgentServer(IHttpAgentApplicationConfiguration configuration, IMTConnectAgentBroker mtconnectAgent) : base(configuration, mtconnectAgent) { }


protected override bool OnObservationInput(string deviceKey, string dataItemKey, string input)
//protected override bool OnObservationInput(string deviceKey, string dataItemKey, string input)
protected override bool OnObservationInput(MTConnectObservationInputArgs args)
{
// Get the Devices Document from the Agent
var devicesDocument = _mtconnectAgent.GetDevicesResponseDocument(deviceKey);
var devicesDocument = _mtconnectAgent.GetDevicesResponseDocument(args.DeviceKey);
if (devicesDocument != null && !devicesDocument.Devices.IsNullOrEmpty())
{
// Get the first Device (should only be one Device)
var device = devicesDocument.Devices.FirstOrDefault();
if (device != null)
{
// Get the DataItem based on the Key
var dataItem = device.GetDataItemByKey(dataItemKey);
var dataItem = device.GetDataItemByKey(args.DataItemKey);
if (dataItem != null)
{
// Construct an SHDR Line using the DataItemId and the Input string from Http
var shdrLine = $"{dataItem.Id}|{input}";
var shdrLine = $"|{dataItem.Id}|{args.Value}";

if (dataItem.Category == DataItemCategory.CONDITION)
{
Expand Down Expand Up @@ -76,32 +71,39 @@ protected override bool OnObservationInput(string deviceKey, string dataItemKey,
}
else
{
_mtconnectAgent.OnInvalidObservationAdded(deviceKey, dataItemKey, new ValidationResult(false, $"DataItemKey \"{dataItemKey}\" not Found in Device"));
//if (_mtconnectAgent.InvalidObservationAdded != null)
//{
// _mtconnectAgent.InvalidObservationAdded.Invoke(deviceKey, dataItemKey, new ValidationResult(false, $"DataItemKey \"{dataItemKey}\" not Found in Device"));
//}
}

return true;
}
else
{
_mtconnectAgent.OnInvalidObservationAdded(deviceKey, dataItemKey, new ValidationResult(false, $"Device \"{deviceKey}\" not Found"));
//if (_mtconnectAgent.InvalidObservationAdded != null)
//{
// _mtconnectAgent.InvalidObservationAdded.Invoke(deviceKey, dataItemKey, new ValidationResult(false, $"Device \"{deviceKey}\" not Found"));
//}
}
}

return false;
}

protected override bool OnAssetInput(string assetId, string deviceKey, string assetType, byte[] requestBytes, string documentFormat = DocumentFormat.XML)
//protected override bool OnAssetInput(string assetId, string deviceKey, string assetType, byte[] requestBytes, string documentFormat = DocumentFormat.XML)
protected override bool OnAssetInput(MTConnectAssetInputArgs args)
{
if (!string.IsNullOrEmpty(deviceKey) && !string.IsNullOrEmpty(assetType))
if (!string.IsNullOrEmpty(args.DeviceKey) && !string.IsNullOrEmpty(args.AssetType))
{
//var asset = Assets.Xml.XmlAsset.FromXml(assetType, );
var result = EntityFormatter.CreateAsset(documentFormat, assetType, ReadRequestBody(requestBytes));
var result = EntityFormatter.CreateAsset(args.DocumentFormat, args.AssetType, ReadRequestBody(args.RequestBody));
if (result.Success)
{
var asset = result.Entity;
asset.AssetId = assetId;
asset.AssetId = args.AssetId;
asset.Timestamp = asset.Timestamp > 0 ? asset.Timestamp : UnixDateTime.Now;
return _mtconnectAgent.AddAsset(deviceKey, asset);
return _mtconnectAgent.AddAsset(args.DeviceKey, asset);
}
}

Expand Down

0 comments on commit 6e4c0b6

Please sign in to comment.