diff --git a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs index 0cacde5..9e63395 100644 --- a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs +++ b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs @@ -219,13 +219,11 @@ namespace hass_workstation_service.Communication { if (((CommandDiscoveryConfigModel)command.GetAutoDiscoveryConfig()).Command_topic == applicationMessage.Topic) { - if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON") + switch (Encoding.UTF8.GetString(applicationMessage?.Payload)) { - command.TurnOn(); - } - else if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "OFF") - { - command.TurnOff(); + case "PRESS": + command.Press(); + break; } } diff --git a/hass-workstation-service/Domain/Commands/AbstractCommand.cs b/hass-workstation-service/Domain/Commands/AbstractCommand.cs index 4e42b86..9795b10 100644 --- a/hass-workstation-service/Domain/Commands/AbstractCommand.cs +++ b/hass-workstation-service/Domain/Commands/AbstractCommand.cs @@ -14,7 +14,7 @@ namespace hass_workstation_service.Domain.Commands public DateTime? LastUpdated { get; protected set; } public string PreviousPublishedState { get; protected set; } public MqttPublisher Publisher { get; protected set; } - public override string Domain { get => "switch"; } + public override string Domain { get => "button"; } public AbstractCommand(MqttPublisher publisher, string name, Guid id = default) { @@ -24,15 +24,13 @@ namespace hass_workstation_service.Domain.Commands publisher.Subscribe(this); } - public abstract string GetState(); - public async Task PublishStateAsync() { // dont't even check the state if the update interval hasn't passed if (LastUpdated.HasValue && LastUpdated.Value.AddSeconds(UpdateInterval) > DateTime.UtcNow) return; - string state = GetState(); + string state = ""; // don't publish the state if it hasn't changed if (PreviousPublishedState == state) return; @@ -59,7 +57,6 @@ namespace hass_workstation_service.Domain.Commands return config; } - public abstract void TurnOn(); - public abstract void TurnOff(); + public abstract void Press(); } } \ No newline at end of file diff --git a/hass-workstation-service/Domain/Commands/CustomCommand.cs b/hass-workstation-service/Domain/Commands/CustomCommand.cs index 02d265a..5b66e5a 100644 --- a/hass-workstation-service/Domain/Commands/CustomCommand.cs +++ b/hass-workstation-service/Domain/Commands/CustomCommand.cs @@ -12,17 +12,14 @@ namespace hass_workstation_service.Domain.Commands public class CustomCommand : AbstractCommand { public string Command { get; protected set; } - public string State { get; protected set; } public Process Process { get; private set; } public CustomCommand(MqttPublisher publisher, string command, string name = "Custom", Guid id = default(Guid)) : base(publisher, name ?? "Custom", id) { this.Command = command; - this.State = "OFF"; } - public override async void TurnOn() + public override async void Press() { - this.State = "ON"; this.Process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.WindowStyle = ProcessWindowStyle.Hidden; @@ -33,7 +30,6 @@ namespace hass_workstation_service.Domain.Commands // turn off the sensor to guarantee disable the switch // useful if command changes power state of device - this.State = "OFF"; try { @@ -42,7 +38,6 @@ namespace hass_workstation_service.Domain.Commands catch (Exception e) { Log.Logger.Error($"Sensor {this.Name} failed", e); - this.State = "FAILED"; } } @@ -61,15 +56,5 @@ namespace hass_workstation_service.Domain.Commands Device = this.Publisher.DeviceConfigModel, }; } - - public override string GetState() - { - return this.State; - } - - public override void TurnOff() - { - this.Process.Kill(); - } } } diff --git a/hass-workstation-service/Domain/Commands/HibernateCommand.cs b/hass-workstation-service/Domain/Commands/HibernateCommand.cs index e2e3d7a..0fbab79 100644 --- a/hass-workstation-service/Domain/Commands/HibernateCommand.cs +++ b/hass-workstation-service/Domain/Commands/HibernateCommand.cs @@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands { public HibernateCommand(MqttPublisher publisher, string name = "Hibernate", Guid id = default(Guid)) : base(publisher, "shutdown /h", name ?? "Hibernate", id) { - this.State = "OFF"; } } } diff --git a/hass-workstation-service/Domain/Commands/KeyCommand.cs b/hass-workstation-service/Domain/Commands/KeyCommand.cs index c0b560f..23db713 100644 --- a/hass-workstation-service/Domain/Commands/KeyCommand.cs +++ b/hass-workstation-service/Domain/Commands/KeyCommand.cs @@ -42,18 +42,7 @@ namespace hass_workstation_service.Domain.Commands [DllImport("user32.dll")] public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); - - public override string GetState() - { - return "OFF"; - } - - public override void TurnOff() - { - - } - - public override void TurnOn() + public sealed override void Press() { keybd_event(this.KeyCode, 0, 0, IntPtr.Zero); keybd_event(this.KeyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero); diff --git a/hass-workstation-service/Domain/Commands/LogOffCommand.cs b/hass-workstation-service/Domain/Commands/LogOffCommand.cs index 921393e..fbb56a0 100644 --- a/hass-workstation-service/Domain/Commands/LogOffCommand.cs +++ b/hass-workstation-service/Domain/Commands/LogOffCommand.cs @@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands { public LogOffCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /l", name ?? "LogOff", id) { - this.State = "OFF"; } } } diff --git a/hass-workstation-service/Domain/Commands/RestartCommand.cs b/hass-workstation-service/Domain/Commands/RestartCommand.cs index 323b806..2560759 100644 --- a/hass-workstation-service/Domain/Commands/RestartCommand.cs +++ b/hass-workstation-service/Domain/Commands/RestartCommand.cs @@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands { public RestartCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /r", name ?? "Restart", id) { - this.State = "OFF"; } } } diff --git a/hass-workstation-service/Domain/Commands/ShutdownCommand.cs b/hass-workstation-service/Domain/Commands/ShutdownCommand.cs index 7ef5211..81b5705 100644 --- a/hass-workstation-service/Domain/Commands/ShutdownCommand.cs +++ b/hass-workstation-service/Domain/Commands/ShutdownCommand.cs @@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands { public ShutdownCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /s", name ?? "Shutdown", id) { - this.State = "OFF"; } } }