diff --git a/README.md b/README.md index 27805d3..77e1ef0 100644 --- a/README.md +++ b/README.md @@ -106,6 +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 + +This sensor returns the up time from windows in seconds ### Dummy This sensor spits out a random number every second. Useful for testing, maybe you'll find some other use for it. diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs index c13c2ec..9803984 100644 --- a/UserInterface/Views/AddSensorDialog.axaml.cs +++ b/UserInterface/Views/AddSensorDialog.axaml.cs @@ -143,6 +143,13 @@ namespace UserInterface.Views item.ShowWindowNameInput = false; item.UpdateInterval = 5; break; + case AvailableSensors.UpTimeSensor: + item.Description = "This sensor returns the up time from windows in seconds"; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#uptime"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; default: item.Description = null; item.MoreInfoLink = null; diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 088a94e..2765c63 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -118,6 +118,9 @@ 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); + break; default: Log.Logger.Error("Unknown sensortype"); break; diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs index 08a16ce..8c67523 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -42,6 +42,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models MicrophoneActiveSensor, ActiveWindowSensor, NamedWindowSensor, - IdleTimeSensor + IdleTimeSensor, + UpTimeSensor } } diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index f001900..b1093aa 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -99,6 +99,9 @@ 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); + break; case "WebcamActiveSensor": sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); break; diff --git a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs index 870bf79..03e7b61 100644 --- a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs @@ -17,7 +17,7 @@ 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:window-maximize", + Icon = "mdi:clock-time-three-outline", }); } diff --git a/hass-workstation-service/Domain/Sensors/UpTimeSensor.cs b/hass-workstation-service/Domain/Sensors/UpTimeSensor.cs new file mode 100644 index 0000000..c1e4260 --- /dev/null +++ b/hass-workstation-service/Domain/Sensors/UpTimeSensor.cs @@ -0,0 +1,41 @@ +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 UpTimeSensor(MqttPublisher publisher, int? updateInterval = 10, string name = "UpTime", Guid id = default) : base(publisher, name ?? "UpTime", updateInterval ?? 10, id) + { + + + } + + public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig() + { + return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new AutoDiscoveryConfigModel() + { + Name = this.Name, + 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", + }); + } + + public override string GetState() + { + + return (GetTickCount64() / 1000).ToString(); //return in seconds + } + + [DllImport("kernel32")] + extern static UInt64 GetTickCount64(); + } +}