From 0b8aa5a96e28d50b8278f62a16dd3135d68bcf76 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 16 Aug 2023 09:45:34 +0200 Subject: [PATCH] move Watchdog related code into its own class --- csharp/App/SaliMax/src/Program.cs | 22 +++++++++------------- csharp/App/SaliMax/src/Watchdog.cs | 14 ++++++++++++++ csharp/Lib/Utils/ConsoleUtils.cs | 1 - 3 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 csharp/App/SaliMax/src/Watchdog.cs diff --git a/csharp/App/SaliMax/src/Program.cs b/csharp/App/SaliMax/src/Program.cs index 75b6c90a2..a5b1d271a 100644 --- a/csharp/App/SaliMax/src/Program.cs +++ b/csharp/App/SaliMax/src/Program.cs @@ -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()) + .Select(n => n.ConvertTo()) .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(); diff --git a/csharp/App/SaliMax/src/Watchdog.cs b/csharp/App/SaliMax/src/Watchdog.cs new file mode 100644 index 000000000..b304cb6dd --- /dev/null +++ b/csharp/App/SaliMax/src/Watchdog.cs @@ -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"); +} \ No newline at end of file diff --git a/csharp/Lib/Utils/ConsoleUtils.cs b/csharp/Lib/Utils/ConsoleUtils.cs index 9d5c42013..bfee3b9df 100644 --- a/csharp/Lib/Utils/ConsoleUtils.cs +++ b/csharp/Lib/Utils/ConsoleUtils.cs @@ -38,7 +38,6 @@ public static class ConsoleUtils return t; } - public static T Write(this T t, ConsoleColor color) { var c = Console.ForegroundColor;