Innovenergy_trunk/csharp/App/SchneiderMeterDriver/Program.cs

79 lines
2.7 KiB
C#

using InnovEnergy.App.SchneiderDriver;
using InnovEnergy.Lib.Protocols.DBus;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Utils.Net;
// dotnet publish EmuMeter.csproj -c Release -r linux-arm -p:PublishSingleFile=true --self-contained true && \
// rsync -av bin/Release/net6.0/linux-arm/publish/ root@10.2.1.6:/home/root/emu && clear && \
// ssh root@10.2.1.6 /home/root/emu/EmuMeter
Console.WriteLine("Starting Schneider Driver " + Config.Version);
var networkInterfaces = await Nic.GetNetworkInterfaces();
Console.WriteLine("Retrieved network interfaces");
// Log all network interfaces and their properties
foreach (var nic in networkInterfaces)
{
Console.WriteLine($"Interface: {nic.Name}");
Console.WriteLine($" IsUp: {nic.IsUp}");
Console.WriteLine($" IsEthernet: {nic.IsEthernet}");
Console.WriteLine($" IP4 Addresses: {string.Join(", ", nic.Ip4Addresses)}");
}
var candidates = networkInterfaces.Where(n => n.IsUp &&
n.IsEthernet &&
(!n.Ip4Addresses.Any() || n.Ip4Addresses.Contains(Config.OwnAddress)));
if (!candidates.Any())
{
Console.WriteLine("No suitable network interfaces found.");
}
foreach (var nic in candidates)
{
Console.WriteLine($"Found new network interface: {nic.Name}");
if (!nic.Ip4Addresses.Contains(Config.OwnAddress))
{
Console.WriteLine($"Configuring Point-to-Point connection on {nic.Name}");
Console.WriteLine($" own address: {Config.OwnAddress}");
Console.WriteLine($" peer address: {Config.PeerAddress}");
var success = await nic.AddPointToPoint($"{Config.OwnAddress}/16", $"{Config.PeerAddress}/16");
if (!success)
{
Console.WriteLine($"Failed to configure network interface: {nic.Name}");
continue;
}
}
Console.WriteLine($"Pinging peer @ {Config.PeerAddress}");
var ping = await Config.PeerAddress.Ping();
if (ping)
{
Console.WriteLine($"Got answer from {Config.PeerAddress}");
//SchneiderMeterDriver.Run(Config.PeerAddress, Config.PeerPort, Bus.System);
//Console.WriteLine($"{nameof(SchneiderMeterDriver)} FAILED with\n");
var ex = await SchneiderMeterDriver.Run(Config.PeerAddress, Config.PeerPort, Bus.System);
Console.WriteLine($"{nameof(SchneiderMeterDriver)} FAILED with\n{ex}");
}
else
{
Console.WriteLine($"No answer from {Config.PeerAddress}");
}
Console.Write($"Removing Point-to-Point connection on {nic.Name} ...");
var removed = await nic.RemoveAddress($"{Config.OwnAddress}/16");
Console.WriteLine(removed ? "done" : "failed");
}
Console.WriteLine("Stopping SchneiderMeter Driver");