parent
a5820a5911
commit
2590bd50b0
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using hass_workstation_service.Communication;
|
||||
|
||||
namespace hass_workstation_service.Domain.Sensors
|
||||
{
|
||||
public class DummySensor : AbstractSensor
|
||||
{
|
||||
private readonly Random _random;
|
||||
public DummySensor(MqttPublisher publisher, string name = "Dummy")
|
||||
{
|
||||
this.Id = Guid.NewGuid();
|
||||
this.Name = name;
|
||||
this.Publisher = publisher;
|
||||
this._random = new Random();
|
||||
}
|
||||
|
||||
public DummySensor(MqttPublisher publisher, string name, Guid id)
|
||||
{
|
||||
this.Id = id;
|
||||
this.Name = name;
|
||||
this.Publisher = publisher;
|
||||
this._random = new Random();
|
||||
}
|
||||
public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig()
|
||||
{
|
||||
return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new AutoDiscoveryConfigModel()
|
||||
{
|
||||
Name = this.Name,
|
||||
Unique_id = this.Id.ToString(),
|
||||
Device = this.Publisher.DeviceConfigModel,
|
||||
State_topic = $"homeassistant/sensor/{this.Name}/state"
|
||||
});
|
||||
}
|
||||
|
||||
public override string GetState()
|
||||
{
|
||||
return _random.Next(0, 100).ToString();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.ServiceProcess;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace hass_workstation_service.ServiceHost
|
||||
{
|
||||
|
||||
public class ServiceBaseLifetime : ServiceBase, IHostLifetime
|
||||
{
|
||||
private IHostApplicationLifetime ApplicationLifetime { get; }
|
||||
private readonly TaskCompletionSource<object> _delayStart = new TaskCompletionSource<object>();
|
||||
|
||||
public ServiceBaseLifetime(IHostApplicationLifetime applicationLifetime)
|
||||
{
|
||||
ApplicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime));
|
||||
}
|
||||
|
||||
|
||||
public Task WaitForStartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.Register(() => _delayStart.TrySetCanceled());
|
||||
ApplicationLifetime.ApplicationStopping.Register(Stop);
|
||||
|
||||
new Thread(Run).Start(); // Otherwise this would block and prevent IHost.StartAsync from finishing.
|
||||
return _delayStart.Task;
|
||||
}
|
||||
|
||||
private void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
Run(this); // This blocks until the service is stopped.
|
||||
_delayStart.TrySetException(new InvalidOperationException("Stopped without starting"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_delayStart.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Stop();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// Called by base.Run when the service is ready to start.
|
||||
protected override void OnStart(string[] args)
|
||||
{
|
||||
_delayStart.TrySetResult(null);
|
||||
base.OnStart(args);
|
||||
}
|
||||
|
||||
// Called by base.Stop. This may be called multiple times by service Stop, ApplicationStopping, and StopAsync.
|
||||
// That's OK because StopApplication uses a CancellationTokenSource and prevents any recursion.
|
||||
protected override void OnStop()
|
||||
{
|
||||
ApplicationLifetime.StopApplication();
|
||||
base.OnStop();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace hass_workstation_service.ServiceHost
|
||||
{
|
||||
|
||||
public static class ServiceBaseLifetimeHostExtensions
|
||||
{
|
||||
public static IHostBuilder UseServiceBaseLifetime(this IHostBuilder hostBuilder)
|
||||
{
|
||||
return hostBuilder.ConfigureServices((hostContext, services) => services.AddSingleton<IHostLifetime, ServiceBaseLifetime>());
|
||||
}
|
||||
|
||||
public static Task RunAsServiceAsync(this IHostBuilder hostBuilder, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return hostBuilder.UseServiceBaseLifetime().Build().RunAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
"MqttBroker": {
|
||||
"Host": "192.168.2.6",
|
||||
"Username": "tester",
|
||||
"Password": "tester"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue