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 (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;
}
}

@ -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();
}
}

@ -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();
}
}
}

@ -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";
}
}
}

@ -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);

@ -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";
}
}
}

@ -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";
}
}
}

@ -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";
}
}
}

Loading…
Cancel
Save