Skip to content

Commit

Permalink
Added Occupancy config
Browse files Browse the repository at this point in the history
  • Loading branch information
ibebbs committed Jan 9, 2020
1 parent 41916c1 commit 72df35f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/Wallboard/Occupancy/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Wallboard.Occupancy
{
public class Config
{
public int MinimumLuminance { get; set; } = 50;

public int DebounceInSeconds { get; set; } = 10;
}
}
6 changes: 3 additions & 3 deletions src/Wallboard/Occupancy/Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Wallboard.Occupancy
{
public static class Logic
{
public static IObservable<State> WhenOccupancyChanges(IObservable<Message> messages, IScheduler scheduler = null)
public static IObservable<State> WhenOccupancyChanges(IObservable<Message> messages, Config config, IScheduler scheduler = null)
{
scheduler = scheduler ?? Scheduler.Default;

Expand Down Expand Up @@ -50,9 +50,9 @@ public static IObservable<State> WhenOccupancyChanges(IObservable<Message> messa
turnOnWhenShelfPresenceSensorReportsPresence,
turnOffwhenShelfPresenceSensorReportsAbscence)
// ... but only allow a State.Present to propagate when illuminance is above 50 ...
.WithLatestFrom(illuminance, (s, i) => i > 50 ? s : State.Abscent)
.WithLatestFrom(illuminance, (s, i) => i > config.MinimumLuminance ? s : State.Abscent)
// ... debounce to prevent rapid changes ...
.Throttle(TimeSpan.FromSeconds(10), scheduler)
.Throttle(TimeSpan.FromSeconds(config.DebounceInSeconds), scheduler)
// ... and prevent duplicates status' from being emitted ...
.DistinctUntilChanged();
}
Expand Down
1 change: 1 addition & 0 deletions src/Wallboard/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
services.AddSingleton<Display.IController, Display.Controller>();
services.AddOptions<Mqtt.Config>().ValidateDataAnnotations().Bind(hostContext.Configuration.GetSection("Mqtt"));
services.AddSingleton<Mqtt.IConnection, Mqtt.Connection>();
services.AddOptions<Occupancy.Config>().ValidateDataAnnotations().Bind(hostContext.Configuration.GetSection("Occupancy"));
services.AddHostedService<Service>();
})
.ConfigureLogging((hostContext, logging) => logging.AddConsole());
Expand Down
7 changes: 5 additions & 2 deletions src/Wallboard/Service.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Reactive;
Expand All @@ -13,14 +14,16 @@ public class Service : IHostedService
{
private readonly Display.IController _controller;
private readonly Mqtt.IConnection _connection;
private readonly IOptions<Occupancy.Config> _options;
private readonly ILogger<Service> _logger;

private IDisposable _subscription;

public Service(Display.IController controller, Mqtt.IConnection connection, ILogger<Service> logger)
public Service(Display.IController controller, Mqtt.IConnection connection, IOptions<Occupancy.Config> options, ILogger<Service> logger)
{
_controller = controller;
_connection = connection;
_options = options;
_logger = logger;
}

Expand Down Expand Up @@ -48,7 +51,7 @@ private void Log(Occupancy.State occupancy)
public async Task StartAsync(CancellationToken cancellationToken)
{
_subscription = Occupancy.Logic
.WhenOccupancyChanges(_connection.Messages)
.WhenOccupancyChanges(_connection.Messages, _options.Value)
.Do(Log)
.Select(occupancy => occupancy == Occupancy.State.Abscent ? new Func<Task<Unit>>(PowerOff) : new Func<Task<Unit>>(PowerOn))
.SelectMany(async action => await action())
Expand Down

0 comments on commit 72df35f

Please sign in to comment.