From c361b829dd094012aa044310dacbdc7b053d89d4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 8 Jan 2021 18:35:39 +0100 Subject: [PATCH] Added: UpTime sensor Changed: Icon for IdleTime sensor --- .../InterProcessApi.cs | 3 ++ .../ServiceContractModels.cs | 3 +- .../Data/ConfigurationService.cs | 3 ++ .../Domain/Sensors/IdleTimeSensor.cs | 2 +- .../Domain/Sensors/UpTimeSensor.cs | 41 +++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 hass-workstation-service/Domain/Sensors/UpTimeSensor.cs diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 27f2ad7..04bc99f 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -131,6 +131,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 ebc0bfe..1d7d34e 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -40,7 +40,8 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models MicrophoneActiveSensor, ActiveWindowSensor, NamedWindowSensor, - IdleTimeSensor + IdleTimeSensor, + UpTimeSensor } public enum WebcamDetectionMode diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index b25afda..f6909bc 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -94,6 +94,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.DetectionMode, configuredSensor.Id); break; diff --git a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs index 25ca311..4a22b11 100644 --- a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs @@ -25,7 +25,7 @@ namespace hass_workstation_service.Domain.Sensors Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, State_topic = $"homeassistant/sensor/{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..b1d461b --- /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/{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(); + } +}