Convert IdleTimeSensor to LastActiveSensor

pull/19/head
Chris Mancini 4 years ago
parent d8280bdd63
commit 076a28ee39

@ -102,9 +102,9 @@ which results in `4008` for my PC.
You can use [WMI Explorer](https://github.com/vinaypamnani/wmie2/tree/v2.0.0.2) to find see what data is available.
### IdleTime
### LastActive
This sensor returns the amount of seconds the workstation has been idle for. It starts counting the moment you stop typing or moving your mouse.
This sensor returns the date/time that the workstation was last active. Typing or moving your mouse will reset the date/time.
### LastBoot

@ -135,9 +135,9 @@ namespace UserInterface.Views
item.ShowWindowNameInput = true;
item.UpdateInterval = 5;
break;
case AvailableSensors.IdleTimeSensor:
item.Description = "This sensor returns the amount of seconds the workstation has been idle for. ";
item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#idletime";
case AvailableSensors.LastActiveSensor:
item.Description = "This sensor returns the date/time that the workstation was last active.";
item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#lastactive";
item.ShowQueryInput = false;
item.ShowWindowNameInput = false;
item.UpdateInterval = 5;

@ -115,8 +115,8 @@ namespace hass_workstation_service.Communication.InterProcesCommunication
case AvailableSensors.NamedWindowSensor:
sensorToCreate = new NamedWindowSensor(this._publisher, model.WindowName, model.Name, (int)model.UpdateInterval);
break;
case AvailableSensors.IdleTimeSensor:
sensorToCreate = new IdleTimeSensor(this._publisher,(int)model.UpdateInterval, model.Name);
case AvailableSensors.LastActiveSensor:
sensorToCreate = new LastActiveSensor(this._publisher,(int)model.UpdateInterval, model.Name);
break;
case AvailableSensors.LastBootSensor:
sensorToCreate = new LastBootSensor(this._publisher, (int)model.UpdateInterval, model.Name);

@ -42,7 +42,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models
MicrophoneActiveSensor,
ActiveWindowSensor,
NamedWindowSensor,
IdleTimeSensor,
LastActiveSensor,
LastBootSensor,
SessionStateSensor
}

@ -93,8 +93,8 @@ namespace hass_workstation_service.Data
case "NamedWindowSensor":
sensor = new NamedWindowSensor(publisher, configuredSensor.WindowName, configuredSensor.Name, configuredSensor.UpdateInterval, configuredSensor.Id);
break;
case "IdleTimeSensor":
sensor = new IdleTimeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
case "LastActiveSensor":
sensor = new LastActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break;
case "LastBootSensor":
sensor = new LastBootSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);

@ -4,10 +4,10 @@ using System.Runtime.InteropServices;
namespace hass_workstation_service.Domain.Sensors
{
public class IdleTimeSensor : AbstractSensor
public class LastActiveSensor : AbstractSensor
{
public IdleTimeSensor(MqttPublisher publisher, int? updateInterval = 10, string name = "IdleTime", Guid id = default) : base(publisher, name ?? "IdleTime", updateInterval ?? 10, id){}
public LastActiveSensor(MqttPublisher publisher, int? updateInterval = 10, string name = "LastActive", Guid id = default) : base(publisher, name ?? "LastActive", updateInterval ?? 10, id){}
public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig()
{
@ -17,18 +17,17 @@ namespace hass_workstation_service.Domain.Sensors
Unique_id = this.Id.ToString(),
Device = this.Publisher.DeviceConfigModel,
State_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/{this.Name}/state",
Icon = "mdi:clock-time-three-outline",
Unit_of_measurement = "seconds"
Icon = "mdi:clock-time-three-outline"
});
}
public override string GetState()
{
return GetLastInputTime().ToString();
return GetLastInputTime().ToString("s");
}
static int GetLastInputTime()
static DateTime GetLastInputTime()
{
int idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
@ -44,9 +43,9 @@ namespace hass_workstation_service.Domain.Sensors
idleTime = envTicks - lastInputTick;
}
return ((idleTime > 0) ? (idleTime / 1000) : idleTime);
}
return idleTime > 0 ? DateTime.Now - TimeSpan.FromMilliseconds(idleTime) : DateTime.Now;
}
[DllImport("User32.dll")]
Loading…
Cancel
Save