|
| 1 | +// Copyright 2022 Jon Skeet. All rights reserved. |
| 2 | +// Use of this source code is governed by the Apache License 2.0, |
| 3 | +// as found in the LICENSE.txt file.using NodaTime; |
| 4 | + |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using NodaTime; |
| 7 | +using NodaTime.Text; |
| 8 | + |
| 9 | +namespace EvChargerTiming; |
| 10 | + |
| 11 | +public class EvChargerController |
| 12 | +{ |
| 13 | + // Note: Noda Time exposes a ZonedClock which could be used for this, |
| 14 | + // which just composes a clock and a zone, but I've separated them out for |
| 15 | + // clarity. |
| 16 | + private readonly DateTimeZone zone; |
| 17 | + private readonly IClock clock; |
| 18 | + private readonly ChargingSchedule schedule; |
| 19 | + private readonly EvCharger charger; |
| 20 | + private readonly ILogger logger; |
| 21 | + |
| 22 | + public EvChargerController(EvCharger charger, ChargingSchedule schedule, DateTimeZone zone, IClock clock, ILogger logger) |
| 23 | + { |
| 24 | + this.charger = charger; |
| 25 | + this.schedule = schedule; |
| 26 | + this.zone = zone; |
| 27 | + this.clock = clock; |
| 28 | + this.logger = logger; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Infinite loop which just checks periodically whether the charger should be on or not, |
| 33 | + /// based on the schedule. |
| 34 | + /// </summary> |
| 35 | + /// <param name="pollingInterval">The interval at which to check whether or not the charger should be on.</param> |
| 36 | + public void MainLoop(TimeSpan pollingInterval) |
| 37 | + { |
| 38 | + // In a real system we'd want ways of shutting down, updating the schedule, |
| 39 | + // changing the target time zone, updating the time zone database etc. |
| 40 | + while (true) |
| 41 | + { |
| 42 | + Instant now = clock.GetCurrentInstant(); |
| 43 | + |
| 44 | + // Note: converting an instant into a local date/time is always unambiguous. (Every |
| 45 | + // instant maps to exactly one local date/time.) Conversions in the opposite |
| 46 | + // direction may be ambiguous or invalid. |
| 47 | + ZonedDateTime nowInTimeZone = now.InZone(zone); |
| 48 | + |
| 49 | + bool shouldBeOn = schedule.IsChargingEnabled(nowInTimeZone.LocalDateTime); |
| 50 | + if (charger.On != shouldBeOn) |
| 51 | + { |
| 52 | + logger.LogInformation("At {now} ({local} local), changing state to {state}", |
| 53 | + InstantPattern.ExtendedIso.Format(now), |
| 54 | + ZonedDateTimePattern.GeneralFormatOnlyIso.Format(nowInTimeZone), |
| 55 | + shouldBeOn); |
| 56 | + |
| 57 | + charger.ChangeState(shouldBeOn); |
| 58 | + } |
| 59 | + |
| 60 | + // We *could* predict when we'll next need to turn the charger on. |
| 61 | + // However, that's significantly more fiddly than just checking periodically. |
| 62 | + // A check of something "once per minute" will take very little power, |
| 63 | + // and be much simpler than trying to predict how long to sleep for. |
| 64 | + // The reality of EV charging is that we don't need to be massively accurate here; |
| 65 | + // sleeping for several seconds or even a few minutes between checks should be fine. |
| 66 | + Thread.Sleep(pollingInterval); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments