Merge pull request #19 from ChrisMancini/12-move-uptime--idletime-from-relative-t

12 move uptime  idletime from relative t
pull/34/head
sleevezipper 4 years ago committed by GitHub
commit cab8ea2afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -102,13 +102,13 @@ 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. 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.
### UpTime ### LastBoot
This sensor returns theup time from Windows in seconds. This sensor returns the date/time that Windows was last booted.
### SessionState ### SessionState

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

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

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

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

@ -4,10 +4,10 @@ using System.Runtime.InteropServices;
namespace hass_workstation_service.Domain.Sensors 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() public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig()
{ {
@ -17,20 +17,17 @@ namespace hass_workstation_service.Domain.Sensors
Unique_id = this.Id.ToString(), Unique_id = this.Id.ToString(),
Device = this.Publisher.DeviceConfigModel, Device = this.Publisher.DeviceConfigModel,
State_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/{this.Name}/state", State_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/{this.Name}/state",
Icon = "mdi:clock-time-three-outline", Icon = "mdi:clock-time-three-outline"
Unit_of_measurement = "seconds"
}); });
} }
public override string GetState() public override string GetState()
{ {
return GetLastInputTime().ToString(); return GetLastInputTime().ToString("s");
} }
static DateTime GetLastInputTime()
static int GetLastInputTime()
{ {
int idleTime = 0; int idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO(); LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
@ -46,9 +43,9 @@ namespace hass_workstation_service.Domain.Sensors
idleTime = envTicks - lastInputTick; idleTime = envTicks - lastInputTick;
} }
return ((idleTime > 0) ? (idleTime / 1000) : idleTime);
}
return idleTime > 0 ? DateTime.Now - TimeSpan.FromMilliseconds(idleTime) : DateTime.Now;
}
[DllImport("User32.dll")] [DllImport("User32.dll")]

@ -1,17 +1,13 @@
using hass_workstation_service.Communication; using hass_workstation_service.Communication;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using HWND = System.IntPtr;
namespace hass_workstation_service.Domain.Sensors 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(), Unique_id = this.Id.ToString(),
Device = this.Publisher.DeviceConfigModel, Device = this.Publisher.DeviceConfigModel,
State_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/{this.Name}/state", State_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/{this.Name}/state",
Icon = "mdi:clock-time-three-outline", Icon = "mdi:clock-time-three-outline"
Unit_of_measurement = "seconds"
}); });
} }
public override string GetState() public override string GetState()
{ {
return (DateTime.Now - TimeSpan.FromMilliseconds(GetTickCount64())).ToString("s");
return (GetTickCount64() / 1000).ToString(); //return in seconds
} }
[DllImport("kernel32")] [DllImport("kernel32")]
Loading…
Cancel
Save