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 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); private static readonly UnixTimeSpan UpdateInterval = UnixTimeSpan.FromSeconds(2);
@ -85,8 +84,7 @@ internal static class Program
{ {
"Starting SaliMax".LogInfo(); "Starting SaliMax".LogInfo();
// Send the initial "service started" message to systemd Watchdog.Ready();
var sdNotifyReturn = sd_notify(0, "READY=1");
var battery48TlDevices = BatteryNodes var battery48TlDevices = BatteryNodes
.Select(n => new Battery48TlDevice(BatteriesChannel, n)) .Select(n => new Battery48TlDevice(BatteriesChannel, n))
@ -102,15 +100,15 @@ internal static class Program
StatusRecord ReadStatus() StatusRecord ReadStatus()
{ {
"Reading battery".LogInfo();
var battery = batteryDevices.Read().WriteLine();
"Reading AcDC".LogInfo(); "Reading AcDC".LogInfo();
var acDc = acDcDevices.Read(); var acDc = acDcDevices.Read();
"Reading dcDc".LogInfo(); "Reading DcDc".LogInfo();
var dcDc = dcDcDevices.Read(); var dcDc = dcDcDevices.Read();
"Reading battery".LogInfo();
var battery = batteryDevices.Read();
"Reading relays".LogInfo(); "Reading relays".LogInfo();
var relays = saliMaxRelaysDevice.Read(); var relays = saliMaxRelaysDevice.Read();
@ -204,16 +202,14 @@ internal static class Program
StatusRecord RunIteration() StatusRecord RunIteration()
{ {
sd_notify(0, "WATCHDOG=1"); Watchdog.Alive();
var t = UnixTime.Now; var t = UnixTime.Now;
var record = ReadStatus(); var record = ReadStatus();
if (record.Relays is not null) record.Relays?.ToCsv().LogInfo();
record.Relays.ToCsv().LogInfo();
record.ControlConstants(); record.ControlConstants();
record.ControlSystemState(); record.ControlSystemState();
(t + "\n").LogInfo(); (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; return t;
} }
public static T Write<T>(this T t, ConsoleColor color) public static T Write<T>(this T t, ConsoleColor color)
{ {
var c = Console.ForegroundColor; var c = Console.ForegroundColor;