Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 11, 2023
1 parent d17dcd3 commit ce47711
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 600 deletions.
2 changes: 1 addition & 1 deletion MTConnect.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MTConnect.NET-AgentModule-MqttBroker", "agent\Modules\MTConnect.NET-AgentModule-MqttBroker\MTConnect.NET-AgentModule-MqttBroker.csproj", "{93EC3021-51BC-48D2-8AB2-F7F427952DDD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MTConnect.NET-Client-MQTT", "clients\MTConnect.NET-Client-MQTT\MTConnect.NET-Client-MQTT.csproj", "{2844E2F1-8CC0-437B-A730-9045A25971F6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MTConnect.NET-Client-MQTT", "clients\MTConnect.NET-Client-MQTT\MTConnect.NET-Client-MQTT.csproj", "{2844E2F1-8CC0-437B-A730-9045A25971F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MTConnect.Configurations
{
Expand All @@ -11,29 +10,21 @@ namespace MTConnect.Configurations
/// </summary>
public class ModuleConfiguration : HttpServerConfiguration
{
[JsonPropertyName("devicesNamespaces")]
public IEnumerable<NamespaceConfiguration> DevicesNamespaces { get; set; }

[JsonPropertyName("streamsNamespaces")]
public IEnumerable<NamespaceConfiguration> StreamsNamespaces { get; set; }

[JsonPropertyName("assetsNamespaces")]
public IEnumerable<NamespaceConfiguration> AssetsNamespaces { get; set; }

[JsonPropertyName("errorNamespaces")]
public IEnumerable<NamespaceConfiguration> ErrorNamespaces { get; set; }


[JsonPropertyName("devicesStyle")]
public StyleConfiguration DevicesStyle { get; set; }

[JsonPropertyName("streamsStyle")]
public StyleConfiguration StreamsStyle { get; set; }

[JsonPropertyName("assetsStyle")]
public StyleConfiguration AssetsStyle { get; set; }

[JsonPropertyName("errorStyle")]
public StyleConfiguration ErrorStyle { get; set; }
}
}
14 changes: 14 additions & 0 deletions agent/Modules/MTConnect.NET-AgentModule-HttpServer/README-Nuget.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This Agent Module implements a MTConnect REST Protocol Http server
allowPut: true
indentOutput: true
documentFormat: xml
accept:
text/xml: xml
application/json: json-cppagent
responseCompression:
- gzip
- br
Expand All @@ -27,6 +30,8 @@ This Agent Module implements a MTConnect REST Protocol Http server

* `port` - The port number the agent binds to for requests.

* `accept` - Maps HTTP Accept Headers to the corresponding Document Format ID

* `responseCompression` - Sets the List of Encodings (ex. gzip, br, deflate) to pass to the Accept-Encoding HTTP Header

* `version` - Sets the MTConnect Version to use for the response document. This can be used to request a document using an older version for legacy clients
Expand Down Expand Up @@ -98,6 +103,15 @@ Specify the port and hostname with TLS (PFX Certificate)
certificatePassword: mtconnect
```

### Example 5
Specify with custom Accept headers
```yaml
- http-server:
accept:
text/xml: xml
application/json: json-cppagent
```


## Contribution / Feedback
- Please use the [Issues](https://github.com/TrakHound/MTConnect.NET/issues) tab to create issues for specific problems that you may encounter
Expand Down
71 changes: 18 additions & 53 deletions agent/Modules/MTConnect.NET-AgentModule-MqttAdapter/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ public class Module : MTConnectAgentModule
private readonly IMTConnectAgentBroker _mtconnectAgent;
private readonly MqttFactory _mqttFactory;
private readonly IMqttClient _mqttClient;

private readonly string _server;
private readonly int _port;
private readonly int _qos;
private readonly int _interval;
private readonly int _retryInterval;
private readonly string _username;
private readonly string _password;
private readonly string _clientId;
private readonly string _caCertPath;
private readonly string _pemClientCertPath;
private readonly string _pemPrivateKeyPath;
private readonly bool _allowUntrustedCertificates;
private readonly bool _useTls;
private readonly string _topic;
private readonly string _deviceKey;

private CancellationTokenSource _stop;


Expand All @@ -56,24 +39,6 @@ public Module(IMTConnectAgentBroker mtconnectAgent, object configuration) : base
_mqttClient.ApplicationMessageReceivedAsync += MessageReceived;

_configuration = AgentApplicationConfiguration.GetConfiguration<ModuleConfiguration>(configuration);
if (_configuration != null)
{
_server = _configuration.Server;
_port = _configuration.Port;
_interval = _configuration.Interval;
_retryInterval = _configuration.RetryInterval;
_qos = _configuration.QoS;
_username = _configuration.Username;
_password = _configuration.Password;
_clientId = _configuration.ClientId;
_caCertPath = _configuration.CertificateAuthority;
_pemClientCertPath = _configuration.PemCertificate;
_pemPrivateKeyPath = _configuration.PemPrivateKey;
_allowUntrustedCertificates = _configuration.AllowUntrustedCertificates;
_useTls = _configuration.UseTls;
_topic = _configuration.Topic;
_deviceKey = _configuration.DeviceKey;
}
}


Expand Down Expand Up @@ -104,28 +69,28 @@ private async Task Worker()
try
{
// Declare new MQTT Client Options with Tcp Server
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(_server, _port);
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(_configuration.Server, _configuration.Port);

// Set Client ID
if (!string.IsNullOrEmpty(_clientId))
if (!string.IsNullOrEmpty(_configuration.ClientId))
{
clientOptionsBuilder.WithClientId(_clientId);
clientOptionsBuilder.WithClientId(_configuration.ClientId);
}

var certificates = new List<X509Certificate2>();

// Add CA (Certificate Authority)
if (!string.IsNullOrEmpty(_caCertPath))
if (!string.IsNullOrEmpty(_configuration.CertificateAuthority))
{
certificates.Add(new X509Certificate2(GetFilePath(_caCertPath)));
certificates.Add(new X509Certificate2(GetFilePath(_configuration.CertificateAuthority)));
}

// Add Client Certificate & Private Key
if (!string.IsNullOrEmpty(_pemClientCertPath) && !string.IsNullOrEmpty(_pemPrivateKeyPath))
if (!string.IsNullOrEmpty(_configuration.PemCertificate) && !string.IsNullOrEmpty(_configuration.PemPrivateKey))
{

#if NET5_0_OR_GREATER
certificates.Add(new X509Certificate2(X509Certificate2.CreateFromPemFile(GetFilePath(_pemClientCertPath), GetFilePath(_pemPrivateKeyPath)).Export(X509ContentType.Pfx)));
certificates.Add(new X509Certificate2(X509Certificate2.CreateFromPemFile(GetFilePath(_configuration.PemCertificate), GetFilePath(_configuration.PemPrivateKey)).Export(X509ContentType.Pfx)));
#else
throw new Exception("PEM Certificates Not Supported in .NET Framework 4.8 or older");
#endif
Expand All @@ -134,23 +99,23 @@ private async Task Worker()
{
UseTls = true,
SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
IgnoreCertificateRevocationErrors = _allowUntrustedCertificates,
IgnoreCertificateChainErrors = _allowUntrustedCertificates,
AllowUntrustedCertificates = _allowUntrustedCertificates,
IgnoreCertificateRevocationErrors = _configuration.AllowUntrustedCertificates,
IgnoreCertificateChainErrors = _configuration.AllowUntrustedCertificates,
AllowUntrustedCertificates = _configuration.AllowUntrustedCertificates,
Certificates = certificates
});
}

// Add Credentials
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
if (!string.IsNullOrEmpty(_configuration.Username) && !string.IsNullOrEmpty(_configuration.Password))
{
if (_useTls)
if (_configuration.UseTls)
{
clientOptionsBuilder.WithCredentials(_username, _password).WithTls();
clientOptionsBuilder.WithCredentials(_configuration.Username, _configuration.Password).WithTls();
}
else
{
clientOptionsBuilder.WithCredentials(_username, _password);
clientOptionsBuilder.WithCredentials(_configuration.Username, _configuration.Password);
}
}

Expand All @@ -161,9 +126,9 @@ private async Task Worker()
_mqttClient.ConnectAsync(clientOptions).Wait();


if (!string.IsNullOrEmpty(_topic))
if (!string.IsNullOrEmpty(_configuration.Topic))
{
await _mqttClient.SubscribeAsync($"{_topic}/#");
await _mqttClient.SubscribeAsync($"{_configuration.Topic}/#");
}
else
{
Expand All @@ -183,7 +148,7 @@ private async Task Worker()
//if (ConnectionError != null) ConnectionError.Invoke(this, ex);
}

await Task.Delay(_retryInterval, _stop.Token);
await Task.Delay(_configuration.RetryInterval, _stop.Token);
}
catch (TaskCanceledException) { }
catch (Exception ex)
Expand Down Expand Up @@ -211,7 +176,7 @@ private Task MessageReceived(MqttApplicationMessageReceivedEventArgs args)
var observations = ProcessPayload(args.ApplicationMessage.Payload);
if (!observations.IsNullOrEmpty())
{
_mtconnectAgent.AddObservations(_deviceKey, observations);
_mtconnectAgent.AddObservations(_configuration.DeviceKey, observations);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using MTConnect.Input;
using System;
using System.Collections.Generic;

namespace MTConnect.Adapters
{
Expand Down
Loading

0 comments on commit ce47711

Please sign in to comment.