Innovenergy_trunk/csharp/App/SchneiderMeterDriver/Program.cs

79 lines
2.7 KiB
C#
Raw Normal View History

2024-06-04 13:28:17 +00:00
using InnovEnergy.App.SchneiderDriver;
2024-06-04 10:23:19 +00:00
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
2024-06-04 13:28:17 +00:00
Console.WriteLine("Starting Schneider Driver " + Config.Version);
2024-06-04 10:23:19 +00:00
var networkInterfaces = await Nic.GetNetworkInterfaces();
2024-06-12 12:05:29 +00:00
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)}");
}
2024-06-04 10:23:19 +00:00
var candidates = networkInterfaces.Where(n => n.IsUp &&
n.IsEthernet &&
2024-06-05 14:17:49 +00:00
(!n.Ip4Addresses.Any() || n.Ip4Addresses.Contains(Config.OwnAddress)));
2024-06-04 10:23:19 +00:00
2024-06-12 12:05:29 +00:00
if (!candidates.Any())
{
Console.WriteLine("No suitable network interfaces found.");
}
2024-06-04 10:23:19 +00:00
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}");
2024-06-12 12:05:29 +00:00
//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);
2024-06-04 10:23:19 +00:00
2024-06-04 13:28:17 +00:00
Console.WriteLine($"{nameof(SchneiderMeterDriver)} FAILED with\n{ex}");
2024-06-04 10:23:19 +00:00
}
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");
}
2024-06-04 13:28:17 +00:00
Console.WriteLine("Stopping SchneiderMeter Driver");