Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: CGS-2L #58

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, tgb2 ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion src/ism7config/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static async Task Main(string[] args)

var unitOfWork = new InMemoryDatabase();
var textService = new TextService();
var nc = new NetworkConnector(unitOfWork, textService, new NetworkConnectorSettings{TcpClientConnectTimeoutMs = 1000}, new BusconfigPendingGatewaysSingleton());
var nc = new NetworkConnector(unitOfWork, textService, new NetworkConnectorSettings{TcpClientConnectTimeoutMs = 60000}, new BusconfigPendingGatewaysSingleton());
var streamHandler = new XplatStreamHandler();

var gw = new GatewayBO
Expand Down
48 changes: 28 additions & 20 deletions src/ism7config/XplatStreamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,38 @@ public XplatStreamHandler()
}
public async Task<Stream> GetStream(TcpClient tcpClient, int port)
{
var certificate = (X509Certificate)_loadCertMethodInfo.Invoke(new SslStreamHandler(), null);
var ssl = new SslStream(tcpClient.GetStream(), false, (a, b, c, d) => true);

var sslOptions = new SslClientAuthenticationOptions
{
TargetHost = "ism7.server",
ClientCertificates = new X509Certificate2Collection(new X509Certificate2(certificate)),
};
if (!OperatingSystem.IsWindows())
try
{
try
var certificate = (X509Certificate)_loadCertMethodInfo.Invoke(new SslStreamHandler(), null);
var ssl = new SslStream(tcpClient.GetStream(), false, (a, b, c, d) => true);

var sslOptions = new SslClientAuthenticationOptions
{
sslOptions.CipherSuitesPolicy = new CipherSuitesPolicy(new[]
{
TlsCipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256
});
}
catch (PlatformNotSupportedException)
TargetHost = "ism7.server",
ClientCertificates = new X509Certificate2Collection(new X509Certificate2(certificate)),
};
if (!OperatingSystem.IsWindows())
{
//older linux or mac https://github.com/dotnet/runtime/issues/33649
try
{
sslOptions.CipherSuitesPolicy = new CipherSuitesPolicy(new[]
{
TlsCipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256
});
}
catch (PlatformNotSupportedException)
{
//older linux or mac https://github.com/dotnet/runtime/issues/33649
}
}
using var cts = new CancellationTokenSource(60000);
await ssl.AuthenticateAsClientAsync(sslOptions, cts.Token);
return ssl;
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
using var cts = new CancellationTokenSource(5000);
await ssl.AuthenticateAsClientAsync(sslOptions, cts.Token);
return ssl;
}
}
25 changes: 23 additions & 2 deletions src/ism7mqtt/HomeAssistant/HaDiscovery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
Expand All @@ -11,6 +12,8 @@ public class HaDiscovery
{
private readonly Ism7Config _config;

public bool EnableDebug { get; set; }

public HaDiscovery(Ism7Config config)
{
_config = config;
Expand Down Expand Up @@ -168,9 +171,27 @@ private string GetHomeAssistantType(ParameterDescriptor descriptor)
if (numeric.IsWritable)
{
if (numeric.MinValueCondition != null)
yield return("min", Double.Parse(numeric.MinValueCondition));
{
if (Double.TryParse(numeric.MinValueCondition, NumberStyles.Number, CultureInfo.InvariantCulture, out var min))
{
yield return("min", min);
}
else if (EnableDebug)
{
Console.WriteLine($"Cannot parse MinValueCondition '{numeric.MinValueCondition}' for PTID {descriptor.PTID}");
}
}
if (numeric.MaxValueCondition != null)
yield return ("max", Double.Parse(numeric.MaxValueCondition));
{
if (Double.TryParse(numeric.MaxValueCondition, NumberStyles.Number, CultureInfo.InvariantCulture, out var max))
{
yield return ("max", max);
}
else if (EnableDebug)
{
Console.WriteLine($"Cannot parse MaxValueCondition '{numeric.MaxValueCondition}' for PTID {descriptor.PTID}");
}
}
if (numeric.StepWidth != null)
yield return ("step", numeric.StepWidth);
}
Expand Down
17 changes: 16 additions & 1 deletion src/ism7mqtt/ISM7/Ism7Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private async Task OnCommandAsync(List<InfoWrite> writeRequests, CancellationTok
var request = new TelegramBundleReq
{
AbortOnError = true,
BundleId = NextBundleId(),
BundleId = "1099",
GatewayId = "1",
TelegramBundleType = TelegramBundleType.write,
InfoWriteTelegrams = writeRequests
Expand Down Expand Up @@ -276,9 +276,11 @@ private async Task OnPushResponseAsync(IResponse response, CancellationToken can

private async Task LoadInitialValuesAsync(CancellationToken cancellationToken)
{
var visibleDevices = new List<string>();
foreach (var device in _devices.Values)
{
if (!_config.AddDevice(_ipAddress.ToString(), device.Ba)) continue;
visibleDevices.Add(device.Ba);
var infoReads = _config.GetInfoReadForDevice(device.Ba).ToList();
var bundleId = NextBundleId();
_dispatcher.SubscribeOnce(
Expand Down Expand Up @@ -306,6 +308,19 @@ await SendAsync(new TelegramBundleReq
{
await OnInitializationFinishedAsync(_config, cancellationToken);
}
var bn = NextBundleId();
await SendAsync(new TelegramBundleReq
{
AbortOnError = false,
BundleId = bn,
GatewayId = "1",
TelegramBundleType = TelegramBundleType.push,
EStRead = new TelegramBundleReq.ErrorStateRead
{
Seq = bn,
VisibleDeviceAdresses = String.Join(',', visibleDevices)
}
}, cancellationToken);
}

private async Task OnInitialValuesAsync(IResponse response, CancellationToken cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/ism7mqtt/ISM7/Ism7Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public IEnumerable<InfoWrite> GetWriteRequest(ReadOnlyMemory<string> propertyPar
foreach (var result in results)
{
result.BusAddress = WriteAddress;
result.Seq = "";
result.Seq = "A;142";
yield return result;
}
}
Expand All @@ -240,7 +240,7 @@ public IEnumerable<InfoWrite> GetWriteRequest(JsonObject data)
foreach (var result in results)
{
result.BusAddress = WriteAddress;
result.Seq = "";
result.Seq = "A;142";
yield return result;
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/ism7mqtt/ISM7/Protocol/XmlPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,26 @@ public abstract class XmlPayload : IPayload

public byte[] Serialize()
{
using var sw = new StringWriter();
using var sw = new Utf8StringWriter();
var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {Indent = false});

var serializer = _serializers.GetOrAdd(GetType(), x => new XmlSerializer(x));
serializer.Serialize(xmlWriter, this);

//suppress xmlns:xsi and xmlns:xsd
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");

serializer.Serialize(xmlWriter, this, ns);
sw.Flush();
var xml = sw.ToString();
return Encoding.UTF8.GetBytes(xml);
}

public abstract PayloadType Type { get; }

class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
}
}
6 changes: 3 additions & 3 deletions src/ism7mqtt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static async Task Main(string[] args)

if (_discoveryId != null)
{
client.OnInitializationFinishedAsync = (config, c) => PublishDiscoveryInfo(config, mqttClient, c);
client.OnInitializationFinishedAsync = (config, c) => PublishDiscoveryInfo(config, mqttClient, enableDebug, c);
}
await client.RunAsync(password, cts.Token);
}
Expand Down Expand Up @@ -168,9 +168,9 @@ private static int GetEnvInt32(string name, int defaultValue = default)
return (int)parsed;
}

private static async Task PublishDiscoveryInfo(Ism7Config config, IMqttClient mqttClient, CancellationToken cancellationToken)
private static async Task PublishDiscoveryInfo(Ism7Config config, IMqttClient mqttClient, bool debug, CancellationToken cancellationToken)
{
var discovery = new HaDiscovery(config);
var discovery = new HaDiscovery(config) { EnableDebug = debug };
foreach (var message in discovery.GetDiscoveryInfo(_discoveryId))
{
var data = JsonSerializer.Serialize(message.Content);
Expand Down