make sure the worker doesn't crash when a sensor fails

pull/19/head
sleevezipper 4 years ago
parent 7dde2a19ea
commit 61d819ea8a

@ -9,6 +9,7 @@ using hass_workstation_service.Domain.Sensors;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet.Client; using MQTTnet.Client;
using Serilog;
namespace hass_workstation_service namespace hass_workstation_service
{ {
@ -50,9 +51,17 @@ namespace hass_workstation_service
_logger.LogDebug("Worker running at: {time}", DateTimeOffset.Now); _logger.LogDebug("Worker running at: {time}", DateTimeOffset.Now);
foreach (AbstractSensor sensor in sensors) foreach (AbstractSensor sensor in sensors)
{
try
{ {
await sensor.PublishStateAsync(); await sensor.PublishStateAsync();
} }
catch (Exception ex)
{
Log.Logger.Warning("Sensor failed: " + sensor.Name, ex);
}
}
// announce autodiscovery every 30 seconds // announce autodiscovery every 30 seconds
if (_mqttPublisher.LastConfigAnnounce < DateTime.UtcNow.AddSeconds(-30)) if (_mqttPublisher.LastConfigAnnounce < DateTime.UtcNow.AddSeconds(-30))
{ {

Loading…
Cancel
Save