|
|
@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
|
|
|
using hass_workstation_service.Communication.InterProcesCommunication.Models;
|
|
|
|
using hass_workstation_service.Communication.InterProcesCommunication.Models;
|
|
|
|
using hass_workstation_service.Communication.Util;
|
|
|
|
using hass_workstation_service.Communication.Util;
|
|
|
|
using hass_workstation_service.Data;
|
|
|
|
using hass_workstation_service.Data;
|
|
|
|
|
|
|
|
using hass_workstation_service.Domain;
|
|
|
|
using hass_workstation_service.Domain.Commands;
|
|
|
|
using hass_workstation_service.Domain.Commands;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using MQTTnet;
|
|
|
|
using MQTTnet;
|
|
|
@ -97,7 +98,7 @@ namespace hass_workstation_service.Communication
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: This should take a sensor/command instead of a config.
|
|
|
|
// TODO: This should take a sensor/command instead of a config.
|
|
|
|
// Then we can ask the sensor about the topic based on ObjectId instead of referencing Name directly
|
|
|
|
// Then we can ask the sensor about the topic based on ObjectId instead of referencing Name directly
|
|
|
|
public async Task AnnounceAutoDiscoveryConfig(DiscoveryConfigModel config, string domain, bool clearConfig = false)
|
|
|
|
public async Task AnnounceAutoDiscoveryConfig(AbstractDiscoverable discoverable, string domain, bool clearConfig = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (this._mqttClient.IsConnected)
|
|
|
|
if (this._mqttClient.IsConnected)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -110,8 +111,8 @@ namespace hass_workstation_service.Communication
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
.WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{config.Name}/config")
|
|
|
|
.WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{discoverable.ObjectId}/config")
|
|
|
|
.WithPayload(clearConfig ? "" : JsonSerializer.Serialize(config, config.GetType(), options))
|
|
|
|
.WithPayload(clearConfig ? "" : JsonSerializer.Serialize(discoverable.GetAutoDiscoveryConfig(), discoverable.GetAutoDiscoveryConfig().GetType(), options))
|
|
|
|
.WithRetainFlag()
|
|
|
|
.WithRetainFlag()
|
|
|
|
.Build();
|
|
|
|
.Build();
|
|
|
|
await this.Publish(message);
|
|
|
|
await this.Publish(message);
|
|
|
@ -178,7 +179,7 @@ namespace hass_workstation_service.Communication
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (this.IsConnected)
|
|
|
|
if (this.IsConnected)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await this._mqttClient.SubscribeAsync(command.GetAutoDiscoveryConfig().Command_topic);
|
|
|
|
await this._mqttClient.SubscribeAsync(((CommandDiscoveryConfigModel) command.GetAutoDiscoveryConfig()).Command_topic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -187,7 +188,7 @@ namespace hass_workstation_service.Communication
|
|
|
|
await Task.Delay(5500);
|
|
|
|
await Task.Delay(5500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await this._mqttClient.SubscribeAsync(command.GetAutoDiscoveryConfig().Command_topic);
|
|
|
|
await this._mqttClient.SubscribeAsync(((CommandDiscoveryConfigModel) command.GetAutoDiscoveryConfig()).Command_topic);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -198,7 +199,7 @@ namespace hass_workstation_service.Communication
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (AbstractCommand command in this.Subscribers)
|
|
|
|
foreach (AbstractCommand command in this.Subscribers)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (command.GetAutoDiscoveryConfig().Command_topic == applicationMessage.Topic)
|
|
|
|
if (((CommandDiscoveryConfigModel)command.GetAutoDiscoveryConfig()).Command_topic == applicationMessage.Topic)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON")
|
|
|
|
if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON")
|
|
|
|
{
|
|
|
|
{
|
|
|
|