diff --git a/UserInterface/UserInterface.csproj b/UserInterface/UserInterface.csproj index 3938707..c29402c 100644 --- a/UserInterface/UserInterface.csproj +++ b/UserInterface/UserInterface.csproj @@ -1,7 +1,7 @@  WinExe - netcoreapp3.1 + net5.0 diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 2ff15d8..27f2ad7 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -128,6 +128,9 @@ 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); + 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 e0d5e6d..ebc0bfe 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -39,7 +39,8 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models WebcamActiveSensor, MicrophoneActiveSensor, ActiveWindowSensor, - NamedWindowSensor + NamedWindowSensor, + IdleTimeSensor } public enum WebcamDetectionMode diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index fb2029e..177d700 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -91,6 +91,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 "WebcamActiveSensor": sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.DetectionMode, configuredSensor.Id); break; diff --git a/hass-workstation-service/Data/ConfiguredMqttBroker.cs b/hass-workstation-service/Data/ConfiguredMqttBroker.cs index a86e249..ae80372 100644 --- a/hass-workstation-service/Data/ConfiguredMqttBroker.cs +++ b/hass-workstation-service/Data/ConfiguredMqttBroker.cs @@ -5,8 +5,30 @@ namespace hass_workstation_service.Data { public class ConfiguredMqttBroker { + private string username; + private string password; public string Host { get; set; } - public string Username { get; set; } - public string Password { get; set; } + + public string Username + { + get + { + if (username != null) return username; + + return ""; + } + set => username = value; + } + + public string Password + { + get + { + if (password != null) return password; + + return ""; + } + set => password = value; + } } } \ No newline at end of file diff --git a/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs new file mode 100644 index 0000000..25ca311 --- /dev/null +++ b/hass-workstation-service/Domain/Sensors/IdleTimeSensor.cs @@ -0,0 +1,76 @@ +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 IdleTimeSensor : AbstractSensor + { + + public IdleTimeSensor(MqttPublisher publisher, int? updateInterval = 10, string name = "IdleTime", Guid id = default) : base(publisher, name ?? "IdleTime", 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:window-maximize", + }); + } + + public override string GetState() + { + + return GetLastInputTime().ToString(); + } + + + + + static int GetLastInputTime() + { + int idleTime = 0; + LASTINPUTINFO lastInputInfo = new LASTINPUTINFO(); + lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo); + lastInputInfo.dwTime = 0; + + int envTicks = Environment.TickCount; + + if (GetLastInputInfo(ref lastInputInfo)) + { + int lastInputTick = Convert.ToInt32(lastInputInfo.dwTime); + + idleTime = envTicks - lastInputTick; + } + + return ((idleTime > 0) ? (idleTime / 1000) : idleTime); + } + + + + [DllImport("User32.dll")] + private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + [StructLayout(LayoutKind.Sequential)] + struct LASTINPUTINFO + { + public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO)); + + [MarshalAs(UnmanagedType.U4)] + public int cbSize; + [MarshalAs(UnmanagedType.U4)] + public UInt32 dwTime; + } + } +} diff --git a/hass-workstation-service/UserInterface.exe b/hass-workstation-service/UserInterface.exe deleted file mode 100644 index 86049d1..0000000 Binary files a/hass-workstation-service/UserInterface.exe and /dev/null differ diff --git a/hass-workstation-service/hass-workstation-service.csproj b/hass-workstation-service/hass-workstation-service.csproj index 1fb7a53..06efa19 100644 --- a/hass-workstation-service/hass-workstation-service.csproj +++ b/hass-workstation-service/hass-workstation-service.csproj @@ -1,7 +1,7 @@ - + - netcoreapp3.1 + net5.0 dotnet-hass_workstation_service-C65C2EBE-1977-4C24-AC6B-6921877E1390 hass_workstation_service WinExe @@ -28,12 +28,6 @@ - - - PreserveNewest - - - Never