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