Skip to content

Commit

Permalink
Upgraded HomeAutio.Core and added broker TLS support, as well as chan…
Browse files Browse the repository at this point in the history
…ged to sending a KeyPress instead of just a SendCommand event
  • Loading branch information
i8beef committed Jul 29, 2019
1 parent 6ad33fa commit e8e8080
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/HomeAutio.Mqtt.Harmony/HarmonyMqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class HarmonyMqttService : ServiceBase

private IClient _client;
private string _harmonyName;
private int _harmonyKeyPressLength;
private HarmonyConfig _harmonyConfig;

/// <summary>
Expand All @@ -38,11 +39,13 @@ public class HarmonyMqttService : ServiceBase
/// <param name="logger">Logging instance.</param>
/// <param name="harmonyClient">The Harmony client.</param>
/// <param name="harmonyName">The Harmony name.</param>
/// <param name="harmonyKeyPressLength">The Harmony key press length.</param>
/// <param name="brokerSettings">MQTT broker settings.</param>
public HarmonyMqttService(
ILogger<HarmonyMqttService> logger,
IClient harmonyClient,
string harmonyName,
int harmonyKeyPressLength,
BrokerSettings brokerSettings)
: base(logger, brokerSettings, "harmony/" + harmonyName)
{
Expand All @@ -55,6 +58,7 @@ public HarmonyMqttService(
// Setup harmony client
_client = harmonyClient;
_harmonyName = harmonyName;
_harmonyKeyPressLength = harmonyKeyPressLength;
_client.CurrentActivityUpdated += Harmony_CurrentActivityUpdated;

// Harmony client logging
Expand Down Expand Up @@ -113,7 +117,7 @@ await _client.StartActivityAsync(int.Parse(activity.Id))
var command = _topicActionMap[e.ApplicationMessage.Topic];
if (command != null)
{
await _client.SendCommandAsync(command)
await _client.SendKeyPressAsync(command, _harmonyKeyPressLength)
.ConfigureAwait(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HomeAutio.Mqtt.Harmony/HomeAutio.Mqtt.Harmony.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="HarmonyHub" Version="3.0.0.10" />
<PackageReference Include="HomeAutio.Mqtt.Core" Version="3.0.0.45" />
<PackageReference Include="HomeAutio.Mqtt.Core" Version="3.0.0.51" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
Expand Down
49 changes: 48 additions & 1 deletion src/HomeAutio.Mqtt.Harmony/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using HarmonyHub;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -85,13 +88,57 @@ private static IHostBuilder CreateHostBuilder(IConfiguration config)
BrokerIp = config.GetValue<string>("mqtt:brokerIp"),
BrokerPort = config.GetValue<int>("mqtt:brokerPort"),
BrokerUsername = config.GetValue<string>("mqtt:brokerUsername"),
BrokerPassword = config.GetValue<string>("mqtt:brokerPassword")
BrokerPassword = config.GetValue<string>("mqtt:brokerPassword"),
BrokerUseTls = config.GetValue<bool>("mqtt:brokerUseTls", false)
};

// TLS settings
if (brokerSettings.BrokerUseTls)
{
var brokerTlsSettings = new Core.BrokerTlsSettings
{
AllowUntrustedCertificates = config.GetValue<bool>("mqtt:brokerTlsSettings:allowUntrustedCertificates", false),
IgnoreCertificateChainErrors = config.GetValue<bool>("mqtt:brokerTlsSettings:ignoreCertificateChainErrors", false),
IgnoreCertificateRevocationErrors = config.GetValue<bool>("mqtt:brokerTlsSettings:ignoreCertificateRevocationErrors", false)
};

switch (config.GetValue<string>("mqtt:brokerTlsSettings:protocol", "1.2"))
{
case "1.0":
brokerTlsSettings.SslProtocol = System.Security.Authentication.SslProtocols.Tls;
break;
case "1.1":
brokerTlsSettings.SslProtocol = System.Security.Authentication.SslProtocols.Tls11;
break;
case "1.2":
default:
brokerTlsSettings.SslProtocol = System.Security.Authentication.SslProtocols.Tls12;
break;
}

var brokerTlsCertificatesSection = config.GetSection("mqtt:brokerTlsSettings:certificates");
brokerTlsSettings.Certificates = brokerTlsCertificatesSection.GetChildren()
.Select(x =>
{
var file = x.GetValue<string>("file");
var passPhrase = x.GetValue<string>("passPhrase");

if (!File.Exists(file))
throw new FileNotFoundException($"Broker Certificate '{file}' is missing!");

return !string.IsNullOrEmpty(passPhrase) ?
new X509Certificate2(file, passPhrase) :
new X509Certificate2(file);
}).ToList();

brokerSettings.BrokerTlsSettings = brokerTlsSettings;
}

return new HarmonyMqttService(
serviceProvider.GetRequiredService<ILogger<HarmonyMqttService>>(),
serviceProvider.GetRequiredService<IClient>(),
config.GetValue<string>("harmony:harmonyName"),
config.GetValue<int>("harmony:harmonyKeyPressLength", 100),
brokerSettings);
});
});
Expand Down
4 changes: 3 additions & 1 deletion src/HomeAutio.Mqtt.Harmony/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"harmonyHost": "blank",
"harmonyUsername": "",
"harmonyPassword": "",
"harmonyKeyPressLength": 100,
"bypassLogitechLogin": true
},
"mqtt": {
"brokerIp": "localhost",
"brokerPort": 1883,
"brokerUsername": null,
"brokerPassword": null
"brokerPassword": null,
"brokerUseTls": false
},
"Serilog": {
"Enrich": [ "FromLogContext" ],
Expand Down

0 comments on commit e8e8080

Please sign in to comment.