Skip to content

Commit

Permalink
feat: auto detect ip
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Jun 16, 2020
1 parent f05bccb commit 4958c40
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
6 changes: 0 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ services:
build: dist/FunctionalLiving.Api/linux/
container_name: functional-living-api
hostname: functional-living-api
environment:
Serilog__WriteTo__1__Name: Seq
Serilog__WriteTo__1__Args__ServerUrl: http://functional-living-seq:9002
ports:
- "9000:5000"
depends_on:
Expand All @@ -21,10 +18,7 @@ services:
hostname: functional-living-knx
environment:
Knx__RouterIp: 10.0.4.7
Knx__LocalIp: 10.150.0.10
Api__Endpoint: http://functional-living-api:9000
Serilog__WriteTo__1__Name: Seq
Serilog__WriteTo__1__Args__ServerUrl: http://functional-living-seq:9002
depends_on:
- seq
- api
Expand Down
69 changes: 63 additions & 6 deletions src/FunctionalLiving.Knx.Sender/KnxSender.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace FunctionalLiving.Knx.Sender
{
using System;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Net.Mime;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,9 +20,9 @@ public class KnxConfiguration
{
public const string ConfigurationPath = "Knx";

public string RouterIp { get; set; }
public string? RouterIp { get; set; }
public int RouterPort { get; set; } = 3671;
public string LocalIp { get; set; } = "127.0.0.1";
public string? LocalIp { get; set; }
public int LocalPort { get; set; } = 3671;
}

Expand Down Expand Up @@ -61,11 +64,32 @@ public KnxSender(

if (useKnxConnectionTunneling.FeatureEnabled)
{
var knxConfig = knxConfiguration.Value;
if (string.IsNullOrWhiteSpace(knxConfig.RouterIp))
throw new Exception("RouterIp is not defined.");

if (string.IsNullOrWhiteSpace(knxConfig.LocalIp))
{
knxConfig.LocalIp = GetLocalIpAddress();

_logger.LogInformation(
"No LocalIp defined, automatically determined '{LocalIp}'.",
knxConfig.LocalIp);
}

logger.LogInformation(
"Using '{ConnectionMode}' from '{LocalIp}:{LocalPort}' to '{RouterIp}:{RouterPort}'.",
"Tunneling",
knxConfig.LocalIp,
knxConfig.LocalPort,
knxConfig.RouterIp,
knxConfig.RouterPort);

_connection = new KnxConnectionTunneling(
knxConfiguration.Value.RouterIp,
knxConfiguration.Value.RouterPort,
knxConfiguration.Value.LocalIp,
knxConfiguration.Value.LocalPort)
knxConfig.RouterIp,
knxConfig.RouterPort,
knxConfig.LocalIp,
knxConfig.LocalPort)
{
Debug = debugKnx.FeatureEnabled,
};
Expand Down Expand Up @@ -140,5 +164,38 @@ private HttpContent CreateHttpContent(KnxAddress knxAddress, byte[] state)
_logger.LogDebug("Sending Knx payload: {payload}", json);
return new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
}

private string GetLocalIpAddress()
{
UnicastIPAddressInformation mostSuitableIp = null;

var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

foreach (var network in networkInterfaces)
{
if (network.OperationalStatus != OperationalStatus.Up)
continue;

var properties = network.GetIPProperties();

if (properties.GatewayAddresses.Count == 0)
continue;

foreach (var address in properties.UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;

if (IPAddress.IsLoopback(address.Address))
continue;

return address.Address.ToString();
}
}

return mostSuitableIp != null
? mostSuitableIp.Address.ToString()
: "";
}
}
}
2 changes: 1 addition & 1 deletion src/FunctionalLiving.Knx.Sender/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Knx": {
"RouterIp": "10.0.4.7",
"RouterPort": 3671,
"LocalIp": "10.0.1.9",
"LocalIp": null,
"LocalPort": 3671
},

Expand Down

0 comments on commit 4958c40

Please sign in to comment.