|
|
|
@ -5,72 +5,60 @@ using MQTTnet;
|
|
|
|
|
|
|
|
|
|
namespace hass_workstation_service.Domain.Commands
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public abstract class AbstractCommand : AbstractDiscoverable
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The update interval in seconds. It checks state only if the interval has passed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int UpdateInterval { get => 1; }
|
|
|
|
|
public static int UpdateInterval { get => 1; }
|
|
|
|
|
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 AbstractCommand(MqttPublisher publisher, string name, Guid id = default(Guid))
|
|
|
|
|
{
|
|
|
|
|
if (id == Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
this.Id = Guid.NewGuid();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.Id = id;
|
|
|
|
|
}
|
|
|
|
|
this.Name = name;
|
|
|
|
|
this.Publisher = publisher;
|
|
|
|
|
publisher.Subscribe(this);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
protected CommandDiscoveryConfigModel _autoDiscoveryConfigModel;
|
|
|
|
|
protected CommandDiscoveryConfigModel SetAutoDiscoveryConfigModel(CommandDiscoveryConfigModel config)
|
|
|
|
|
public AbstractCommand(MqttPublisher publisher, string name, Guid id = default)
|
|
|
|
|
{
|
|
|
|
|
this._autoDiscoveryConfigModel = config;
|
|
|
|
|
return config;
|
|
|
|
|
Id = id == Guid.Empty ? Guid.NewGuid() : id;
|
|
|
|
|
Name = name;
|
|
|
|
|
Publisher = publisher;
|
|
|
|
|
publisher.Subscribe(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract string GetState();
|
|
|
|
|
|
|
|
|
|
public async Task PublishStateAsync()
|
|
|
|
|
{
|
|
|
|
|
if (LastUpdated.HasValue && LastUpdated.Value.AddSeconds(this.UpdateInterval) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
// 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)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string state = this.GetState();
|
|
|
|
|
if (this.PreviousPublishedState == state)
|
|
|
|
|
{
|
|
|
|
|
// don't publish the state if it hasn't changed
|
|
|
|
|
|
|
|
|
|
string state = GetState();
|
|
|
|
|
// don't publish the state if it hasn't changed
|
|
|
|
|
if (PreviousPublishedState == state)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
|
.WithTopic(this.GetAutoDiscoveryConfig().State_topic)
|
|
|
|
|
.WithPayload(state)
|
|
|
|
|
.WithExactlyOnceQoS()
|
|
|
|
|
.WithRetainFlag()
|
|
|
|
|
.Build();
|
|
|
|
|
.WithTopic(GetAutoDiscoveryConfig().State_topic)
|
|
|
|
|
.WithPayload(state)
|
|
|
|
|
.WithExactlyOnceQoS()
|
|
|
|
|
.WithRetainFlag()
|
|
|
|
|
.Build();
|
|
|
|
|
await Publisher.Publish(message);
|
|
|
|
|
this.PreviousPublishedState = state;
|
|
|
|
|
this.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
PreviousPublishedState = state;
|
|
|
|
|
LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
public async void PublishAutoDiscoveryConfigAsync()
|
|
|
|
|
{
|
|
|
|
|
await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain);
|
|
|
|
|
}
|
|
|
|
|
public async Task UnPublishAutoDiscoveryConfigAsync()
|
|
|
|
|
|
|
|
|
|
public async void PublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, Domain);
|
|
|
|
|
|
|
|
|
|
public async Task UnPublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, Domain, true);
|
|
|
|
|
|
|
|
|
|
protected CommandDiscoveryConfigModel _autoDiscoveryConfigModel;
|
|
|
|
|
protected CommandDiscoveryConfigModel SetAutoDiscoveryConfigModel(CommandDiscoveryConfigModel config)
|
|
|
|
|
{
|
|
|
|
|
await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain, true);
|
|
|
|
|
_autoDiscoveryConfigModel = config;
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void TurnOn();
|
|
|
|
|
public abstract void TurnOff();
|
|
|
|
|
}
|
|
|
|
|