From d8280bdd63ccd8ce0354591178608cec4c9d50fc Mon Sep 17 00:00:00 2001 From: Chris Mancini Date: Tue, 12 Jan 2021 14:13:23 -0500 Subject: [PATCH 1/2] Convert UpTimeSensor to LastBootSensor --- README.md | 4 ++-- UserInterface/Views/AddSensorDialog.axaml.cs | 6 +++--- .../InterProcesCommunication/InterProcessApi.cs | 4 ++-- .../ServiceContractModels.cs | 2 +- .../Data/ConfigurationService.cs | 4 ++-- .../Domain/Sensors/IdleTimeSensor.cs | 2 -- .../Sensors/{UpTimeSensor.cs => LastBootSensor.cs} | 14 ++++---------- 7 files changed, 14 insertions(+), 22 deletions(-) rename hass-workstation-service/Domain/Sensors/{UpTimeSensor.cs => LastBootSensor.cs} (62%) diff --git a/README.md b/README.md index 337ebc6..466fae8 100644 --- a/README.md +++ b/README.md @@ -106,9 +106,9 @@ You can use [WMI Explorer](https://github.com/vinaypamnani/wmie2/tree/v2.0.0.2) 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. -### UpTime +### LastBoot -This sensor returns theup time from Windows in seconds. +This sensor returns the date/time that Windows was last booted. ### SessionState diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs index 3cd05a7..ced3655 100644 --- a/UserInterface/Views/AddSensorDialog.axaml.cs +++ b/UserInterface/Views/AddSensorDialog.axaml.cs @@ -142,9 +142,9 @@ namespace UserInterface.Views item.ShowWindowNameInput = false; item.UpdateInterval = 5; break; - case AvailableSensors.UpTimeSensor: - item.Description = "This sensor returns the uptime from Windows in seconds"; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#uptime"; + case AvailableSensors.LastBootSensor: + item.Description = "This sensor returns the date/time that Windows was last booted"; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#lastboot"; item.ShowQueryInput = false; item.ShowWindowNameInput = false; item.UpdateInterval = 5; diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 51e9313..8e9d287 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -118,8 +118,8 @@ namespace hass_workstation_service.Communication.InterProcesCommunication case AvailableSensors.IdleTimeSensor: sensorToCreate = new IdleTimeSensor(this._publisher,(int)model.UpdateInterval, model.Name); break; - case AvailableSensors.UpTimeSensor: - sensorToCreate = new UpTimeSensor(this._publisher, (int)model.UpdateInterval, model.Name); + case AvailableSensors.LastBootSensor: + sensorToCreate = new LastBootSensor(this._publisher, (int)model.UpdateInterval, model.Name); break; case AvailableSensors.SessionStateSensor: sensorToCreate = new SessionStateSensor(this._publisher, (int)model.UpdateInterval, model.Name); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs index e7fa359..033cffb 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -43,7 +43,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models ActiveWindowSensor, NamedWindowSensor, IdleTimeSensor, - UpTimeSensor, + LastBootSensor, SessionStateSensor } } diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index 3adbef2..1f35289 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -96,8 +96,8 @@ namespace hass_workstation_service.Data case "IdleTimeSensor": sensor = new IdleTimeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); break; - case "UpTimeSensor": - sensor = new UpTimeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); + case "LastBootSensor": + sensor = new LastBootSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); break; case "WebcamActiveSensor": sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); diff --git a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs index 5d69032..abff479 100644 --- a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs @@ -26,8 +26,6 @@ namespace hass_workstation_service.Domain.Sensors { return GetLastInputTime().ToString(); } - - static int GetLastInputTime() diff --git a/hass-workstation-service/Domain/Sensors/UpTimeSensor.cs b/hass-workstation-service/Domain/Sensors/LastBootSensor.cs similarity index 62% rename from hass-workstation-service/Domain/Sensors/UpTimeSensor.cs rename to hass-workstation-service/Domain/Sensors/LastBootSensor.cs index 33edabb..b9bcbd6 100644 --- a/hass-workstation-service/Domain/Sensors/UpTimeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/LastBootSensor.cs @@ -1,17 +1,13 @@ using hass_workstation_service.Communication; using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.InteropServices; -using System.Text; -using HWND = System.IntPtr; namespace hass_workstation_service.Domain.Sensors { - public class UpTimeSensor : AbstractSensor + public class LastBootSensor : AbstractSensor { - public UpTimeSensor(MqttPublisher publisher, int? updateInterval = 10, string name = "UpTime", Guid id = default) : base(publisher, name ?? "UpTime", updateInterval ?? 10, id) + public LastBootSensor(MqttPublisher publisher, int? updateInterval = 10, string name = "LastBoot", Guid id = default) : base(publisher, name ?? "LastBoot", updateInterval ?? 10, id) { @@ -25,15 +21,13 @@ 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 (GetTickCount64() / 1000).ToString(); //return in seconds + return (DateTime.Now - TimeSpan.FromMilliseconds(GetTickCount64())).ToString("s"); } [DllImport("kernel32")] From 076a28ee3946c2fa32494498e1a3dc4e2b5ac4a5 Mon Sep 17 00:00:00 2001 From: Chris Mancini Date: Tue, 12 Jan 2021 14:40:21 -0500 Subject: [PATCH 2/2] Convert IdleTimeSensor to LastActiveSensor --- README.md | 4 ++-- UserInterface/Views/AddSensorDialog.axaml.cs | 6 +++--- .../InterProcesCommunication/InterProcessApi.cs | 4 ++-- .../ServiceContractModels.cs | 2 +- .../Data/ConfigurationService.cs | 4 ++-- .../{IdleTimeSensor.cs => LastActiveSensor.cs} | 15 +++++++-------- 6 files changed, 17 insertions(+), 18 deletions(-) rename hass-workstation-service/Domain/Sensors/{IdleTimeSensor.cs => LastActiveSensor.cs} (76%) diff --git a/README.md b/README.md index 466fae8..a508f42 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs index ced3655..4e85944 100644 --- a/UserInterface/Views/AddSensorDialog.axaml.cs +++ b/UserInterface/Views/AddSensorDialog.axaml.cs @@ -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; diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 8e9d287..4fe7dc5 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -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); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs index 033cffb..aee1b16 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -42,7 +42,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models MicrophoneActiveSensor, ActiveWindowSensor, NamedWindowSensor, - IdleTimeSensor, + LastActiveSensor, LastBootSensor, SessionStateSensor } diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index 1f35289..4527374 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -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); diff --git a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs b/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs similarity index 76% rename from hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs rename to hass-workstation-service/Domain/Sensors/LastActiveSensor.cs index abff479..4a81342 100644 --- a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs @@ -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")]