Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: LastActive stale report / added functionality #9

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ await Task.Run(delegate
abstractSensor = new NamedWindowSensor(sensor.WindowName, sensor.Name, sensor.FriendlyName, sensor.UpdateInterval, sensor.Id.ToString());
break;
case SensorType.LastActiveSensor:
abstractSensor = new LastActiveSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
abstractSensor = new LastActiveSensor(sensor.ApplyRounding, sensor.Round, sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
case SensorType.LastSystemStateChangeSensor:
abstractSensor = new LastSystemStateChangeSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HASS.Agent.Shared.Extensions;
using HASS.Agent.Shared.Managers;
using HASS.Agent.Shared.Models.HomeAssistant;

namespace HASS.Agent.Shared.HomeAssistant.Sensors.GeneralSensors.SingleValue
Expand All @@ -11,16 +15,28 @@ namespace HASS.Agent.Shared.HomeAssistant.Sensors.GeneralSensors.SingleValue
public class LastActiveSensor : AbstractSingleValueSensor
{
private const string DefaultName = "lastactive";

private DateTime _lastActive = DateTime.MinValue;

public LastActiveSensor(int? updateInterval = 10, string name = DefaultName, string friendlyName = DefaultName, string id = default) : base(name ?? DefaultName, friendlyName ?? null, updateInterval ?? 10, id) { }
public const int DefaultTimeWindow = 15;

public bool ApplyRounding { get; private set; }
public int Round { get; private set; }

public LastActiveSensor(bool updateOnResume, int? updateOnResumeTimeWindow, int? updateInterval = 10, string name = DefaultName, string friendlyName = DefaultName, string id = default) : base(name ?? DefaultName, friendlyName ?? null, updateInterval ?? 10, id)
{
ApplyRounding = updateOnResume;
Round = updateOnResumeTimeWindow ?? 30;
}

public override DiscoveryConfigModel GetAutoDiscoveryConfig()
{
if (Variables.MqttManager == null) return null;
if (Variables.MqttManager == null)
return null;

var deviceConfig = Variables.MqttManager.GetDeviceConfigModel();
if (deviceConfig == null) return null;
if (deviceConfig == null)
return null;

return AutoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()
{
Expand All @@ -37,10 +53,27 @@ public override DiscoveryConfigModel GetAutoDiscoveryConfig()

public override string GetState()
{
var lastInput = GetLastInputTime();

if (ApplyRounding)
{
if (SharedSystemStateManager.LastEventOccurrence.TryGetValue(Enums.SystemStateEvent.Resume, out var lastWakeEventDate) // was there a wake event
&& DateTime.Compare(lastInput, lastWakeEventDate) < 0 // was the last input before the last wake event
&& (DateTime.Now - lastWakeEventDate).TotalSeconds < Round) // are we within the time window
{

var currentPosition = Cursor.Position;
Cursor.Position = new Point(Cursor.Position.X - 10, Cursor.Position.Y - 10);
Cursor.Position = currentPosition;

lastInput = GetLastInputTime();
}
}

// changed to min. 1 sec difference
// source: https://github.com/sleevezipper/hass-workstation-service/pull/156
var lastInput = GetLastInputTime();
if ((_lastActive - lastInput).Duration().TotalSeconds > 1) _lastActive = lastInput;
if ((_lastActive - lastInput).Duration().TotalSeconds > 1)
_lastActive = lastInput;

return _lastActive.ToTimeZoneString();
}
Expand All @@ -55,13 +88,15 @@ private static DateTime GetLastInputTime()

var envTicks = Environment.TickCount;

if (!GetLastInputInfo(ref lastInputInfo)) return DateTime.Now;
if (!GetLastInputInfo(ref lastInputInfo))
return DateTime.Now;

var lastInputTick = Convert.ToDouble(lastInputInfo.dwTime);

var idleTime = envTicks - lastInputTick;
return idleTime > 0 ? DateTime.Now - TimeSpan.FromMilliseconds(idleTime) : DateTime.Now;
}

[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HASS.Agent.Shared.Enums;
using Microsoft.Win32;
Expand All @@ -23,6 +24,11 @@ public static class SharedSystemStateManager
/// </summary>
public static SystemStateEvent LastSystemStateEvent { get; private set; } = SystemStateEvent.ApplicationStarted;

/// <summary>
/// Contains the key value pair with SystemStateEvent and the last time it occurred
/// </summary>
public static Dictionary<SystemStateEvent, DateTime> LastEventOccurrence = new Dictionary<SystemStateEvent, DateTime>();

/// <summary>
/// Sets the provided system state event
/// </summary>
Expand Down Expand Up @@ -90,6 +96,8 @@ private static void SystemEventsOnSessionSwitch(object sender, SessionSwitchEven
SessionSwitchReason.SessionUnlock => SystemStateEvent.SessionUnlock,
_ => LastSystemStateEvent
};

LastEventOccurrence[LastSystemStateEvent] = DateTime.Now;
}

private static void SystemEventsOnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
Expand All @@ -102,7 +110,9 @@ private static void SystemEventsOnPowerModeChanged(object sender, PowerModeChang
PowerModes.Suspend => SystemStateEvent.Suspend,
_ => LastSystemStateEvent
};
}

LastEventOccurrence[LastSystemStateEvent] = DateTime.Now;
}

private static void SystemEventsOnSessionEnding(object sender, SessionEndingEventArgs e)
{
Expand All @@ -112,7 +122,9 @@ private static void SystemEventsOnSessionEnding(object sender, SessionEndingEven
SessionEndReasons.SystemShutdown => SystemStateEvent.SystemShutdown,
_ => LastSystemStateEvent
};
}

LastEventOccurrence[LastSystemStateEvent] = DateTime.Now;
}

private static void SystemEventsOnSessionEnded(object sender, SessionEndedEventArgs e)
{
Expand All @@ -122,6 +134,8 @@ private static void SystemEventsOnSessionEnded(object sender, SessionEndedEventA
SessionEndReasons.SystemShutdown => SystemStateEvent.SystemShutdown,
_ => LastSystemStateEvent
};
}

LastEventOccurrence[LastSystemStateEvent] = DateTime.Now;
}
}
}
Loading