Create commands using the domain "button" instead of "swtich"

pull/163/head
Caliel Lima da Costa 3 years ago
parent 7fd20a4fc5
commit 01ddcb4ca4

@ -219,13 +219,11 @@ namespace hass_workstation_service.Communication
{ {
if (((CommandDiscoveryConfigModel)command.GetAutoDiscoveryConfig()).Command_topic == applicationMessage.Topic) if (((CommandDiscoveryConfigModel)command.GetAutoDiscoveryConfig()).Command_topic == applicationMessage.Topic)
{ {
if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON") switch (Encoding.UTF8.GetString(applicationMessage?.Payload))
{ {
command.TurnOn(); case "PRESS":
} command.Press();
else if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "OFF") break;
{
command.TurnOff();
} }
} }

@ -14,7 +14,7 @@ namespace hass_workstation_service.Domain.Commands
public DateTime? LastUpdated { get; protected set; } public DateTime? LastUpdated { get; protected set; }
public string PreviousPublishedState { get; protected set; } public string PreviousPublishedState { get; protected set; }
public MqttPublisher Publisher { 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) public AbstractCommand(MqttPublisher publisher, string name, Guid id = default)
{ {
@ -24,15 +24,13 @@ namespace hass_workstation_service.Domain.Commands
publisher.Subscribe(this); publisher.Subscribe(this);
} }
public abstract string GetState();
public async Task PublishStateAsync() public async Task PublishStateAsync()
{ {
// dont't even check the state if the update interval hasn't passed // dont't even check the state if the update interval hasn't passed
if (LastUpdated.HasValue && LastUpdated.Value.AddSeconds(UpdateInterval) > DateTime.UtcNow) if (LastUpdated.HasValue && LastUpdated.Value.AddSeconds(UpdateInterval) > DateTime.UtcNow)
return; return;
string state = GetState(); string state = "";
// don't publish the state if it hasn't changed // don't publish the state if it hasn't changed
if (PreviousPublishedState == state) if (PreviousPublishedState == state)
return; return;
@ -59,7 +57,6 @@ namespace hass_workstation_service.Domain.Commands
return config; return config;
} }
public abstract void TurnOn(); public abstract void Press();
public abstract void TurnOff();
} }
} }

@ -12,17 +12,14 @@ namespace hass_workstation_service.Domain.Commands
public class CustomCommand : AbstractCommand public class CustomCommand : AbstractCommand
{ {
public string Command { get; protected set; } public string Command { get; protected set; }
public string State { get; protected set; }
public Process Process { get; private 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) public CustomCommand(MqttPublisher publisher, string command, string name = "Custom", Guid id = default(Guid)) : base(publisher, name ?? "Custom", id)
{ {
this.Command = command; this.Command = command;
this.State = "OFF";
} }
public override async void TurnOn() public override async void Press()
{ {
this.State = "ON";
this.Process = new Process(); this.Process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(); ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.WindowStyle = ProcessWindowStyle.Hidden;
@ -33,7 +30,6 @@ namespace hass_workstation_service.Domain.Commands
// turn off the sensor to guarantee disable the switch // turn off the sensor to guarantee disable the switch
// useful if command changes power state of device // useful if command changes power state of device
this.State = "OFF";
try try
{ {
@ -42,7 +38,6 @@ namespace hass_workstation_service.Domain.Commands
catch (Exception e) catch (Exception e)
{ {
Log.Logger.Error($"Sensor {this.Name} failed", 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, Device = this.Publisher.DeviceConfigModel,
}; };
} }
public override string GetState()
{
return this.State;
}
public override void TurnOff()
{
this.Process.Kill();
}
} }
} }

@ -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) public HibernateCommand(MqttPublisher publisher, string name = "Hibernate", Guid id = default(Guid)) : base(publisher, "shutdown /h", name ?? "Hibernate", id)
{ {
this.State = "OFF";
} }
} }
} }

@ -42,18 +42,7 @@ namespace hass_workstation_service.Domain.Commands
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public sealed override void Press()
public override string GetState()
{
return "OFF";
}
public override void TurnOff()
{
}
public override void TurnOn()
{ {
keybd_event(this.KeyCode, 0, 0, IntPtr.Zero); keybd_event(this.KeyCode, 0, 0, IntPtr.Zero);
keybd_event(this.KeyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero); keybd_event(this.KeyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero);

@ -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) public LogOffCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /l", name ?? "LogOff", id)
{ {
this.State = "OFF";
} }
} }
} }

@ -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) public RestartCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /r", name ?? "Restart", id)
{ {
this.State = "OFF";
} }
} }
} }

@ -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) public ShutdownCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /s", name ?? "Shutdown", id)
{ {
this.State = "OFF";
} }
} }
} }

Loading…
Cancel
Save