move Watchdog related code into its own class

This commit is contained in:
ig 2023-08-16 09:45:34 +02:00
parent d633564b93
commit 0b8aa5a96e
3 changed files with 23 additions and 14 deletions

View File

@ -31,8 +31,7 @@ namespace InnovEnergy.App.SaliMax;
internal static class Program
{
[DllImport("libsystemd.so.0")]
private static extern Int32 sd_notify(Int32 unsetEnvironment, String state);
private static readonly UnixTimeSpan UpdateInterval = UnixTimeSpan.FromSeconds(2);
@ -62,7 +61,7 @@ internal static class Program
BatteryNodes = config
.Devices
.BatteryNodes
.Select(n=>n.ConvertTo<Byte>())
.Select(n => n.ConvertTo<Byte>())
.ToArray(config.Devices.BatteryNodes.Length);
}
@ -85,8 +84,7 @@ internal static class Program
{
"Starting SaliMax".LogInfo();
// Send the initial "service started" message to systemd
var sdNotifyReturn = sd_notify(0, "READY=1");
Watchdog.Ready();
var battery48TlDevices = BatteryNodes
.Select(n => new Battery48TlDevice(BatteriesChannel, n))
@ -102,15 +100,15 @@ internal static class Program
StatusRecord ReadStatus()
{
"Reading battery".LogInfo();
var battery = batteryDevices.Read().WriteLine();
"Reading AcDC".LogInfo();
var acDc = acDcDevices.Read();
"Reading dcDc".LogInfo();
"Reading DcDc".LogInfo();
var dcDc = dcDcDevices.Read();
"Reading battery".LogInfo();
var battery = batteryDevices.Read();
"Reading relays".LogInfo();
var relays = saliMaxRelaysDevice.Read();
@ -204,16 +202,14 @@ internal static class Program
StatusRecord RunIteration()
{
sd_notify(0, "WATCHDOG=1");
Watchdog.Alive();
var t = UnixTime.Now;
var record = ReadStatus();
if (record.Relays is not null)
record.Relays.ToCsv().LogInfo();
record.Relays?.ToCsv().LogInfo();
record.ControlConstants();
record.ControlSystemState();
(t + "\n").LogInfo();

View File

@ -0,0 +1,14 @@
using System.Runtime.InteropServices;
namespace InnovEnergy.App.SaliMax;
// https://www.freedesktop.org/software/systemd/man/sd_notify.html
public static class Watchdog
{
// "it is generally recommended to ignore the return value of this call. "
[DllImport("libsystemd.so.0")]
private static extern Int32 sd_notify(Int32 unsetEnvironment, String state);
public static void Ready() => _ = sd_notify(0, "READY=1");
public static void Alive() => _ = sd_notify(0, "WATCHDOG=1");
}

View File

@ -38,7 +38,6 @@ public static class ConsoleUtils
return t;
}
public static T Write<T>(this T t, ConsoleColor color)
{
var c = Console.ForegroundColor;