Refactor some abstract classes. No functional changes, just syntactic sugar.

merge-updating
Stefan Roelofs 4 years ago
parent 58bf3a04b5
commit 1bfeb9c45f

@ -12,16 +12,8 @@ namespace hass_workstation_service.Domain
{ {
public abstract string Domain { get; } public abstract string Domain { get; }
public string Name { get; protected set; } public string Name { get; protected set; }
public string ObjectId => Regex.Replace(Name, "[^a-zA-Z0-9_-]", "_");
public string ObjectId
{
get
{
return Regex.Replace(this.Name, "[^a-zA-Z0-9_-]", "_");
}
}
public Guid Id { get; protected set; } public Guid Id { get; protected set; }
public abstract DiscoveryConfigModel GetAutoDiscoveryConfig(); public abstract DiscoveryConfigModel GetAutoDiscoveryConfig();
} }
} }

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

@ -1,12 +1,10 @@
using System; using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using hass_workstation_service.Communication; using hass_workstation_service.Communication;
using MQTTnet; using MQTTnet;
namespace hass_workstation_service.Domain.Sensors namespace hass_workstation_service.Domain.Sensors
{ {
public abstract class AbstractSensor : AbstractDiscoverable public abstract class AbstractSensor : AbstractDiscoverable
{ {
/// <summary> /// <summary>
@ -17,61 +15,48 @@ namespace hass_workstation_service.Domain.Sensors
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 => "sensor"; } public override string Domain { get => "sensor"; }
public AbstractSensor(MqttPublisher publisher, string name, int updateInterval = 10, Guid id = default(Guid))
{
if (id == Guid.Empty)
{
this.Id = Guid.NewGuid();
}
else
{
this.Id = id;
}
this.Name = name;
this.Publisher = publisher;
this.UpdateInterval = updateInterval;
} public AbstractSensor(MqttPublisher publisher, string name, int updateInterval = 10, Guid id = default)
protected SensorDiscoveryConfigModel _autoDiscoveryConfigModel;
protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscoveryConfigModel config)
{ {
this._autoDiscoveryConfigModel = config; Id = id == Guid.Empty ? Guid.NewGuid() : id;
return config; Name = name;
Publisher = publisher;
UpdateInterval = updateInterval;
} }
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 SensorDiscoveryConfigModel _autoDiscoveryConfigModel;
protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscoveryConfigModel config)
{ {
await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain, true); _autoDiscoveryConfigModel = config;
return config;
} }
} }
} }
Loading…
Cancel
Save