diff --git a/src/CasaBot/CasaBotApp/Controllers/BotController.cs b/src/CasaBot/CasaBotApp/Controllers/BotController.cs index a2e135f..76bd90b 100644 --- a/src/CasaBot/CasaBotApp/Controllers/BotController.cs +++ b/src/CasaBot/CasaBotApp/Controllers/BotController.cs @@ -11,6 +11,8 @@ namespace CasaBotApp.Controllers; public class BotController : IController { + private const string MESSAGE_DISSARMING = "Disarming the door sensor 🔓"; + private const string MESSAGE_DISARM_ERROR = "Alarm is not connected 🚷"; private readonly BotHandler _botHandler; private readonly ILogger _logger; private readonly AutoScanApp _autoScanApp; @@ -103,8 +105,16 @@ public class BotController : IController [BotCommand("/disarm", "Disarm the Door Sensor")] public async Task Disarm(TextMessage msg, BotCommand ctx) { - await ctx.Responder(msg, "Disarming the door sensor"); _controlServer.RequestDisarm(); + + await ctx.Responder(msg, + _controlServer.IsConnected() ? MESSAGE_DISSARMING : MESSAGE_DISARM_ERROR); + } + + [BotCommand("/status", "Get the status of the alarm")] + public async Task Status(TextMessage msg, BotCommand ctx) + { + await ctx.Responder(msg, _controlServer.IsConnected() ? "Alarm is connected 🟢" : "Alarm is not connected 🔴"); } private void HandleReply() diff --git a/src/CasaBot/ControlServer/ControlServer.cs b/src/CasaBot/ControlServer/ControlServer.cs index eb9813c..24b5f3d 100644 --- a/src/CasaBot/ControlServer/ControlServer.cs +++ b/src/CasaBot/ControlServer/ControlServer.cs @@ -13,6 +13,8 @@ public class ControlServer : IControlServer private readonly Dictionary _events = new(); private Func? _onEventRecived; private bool _disarmRequestPending; + private DateTime _disarmRequestTime; + private DateTime _lastContactTime; public ControlServer(ILogger logger, IOptions options) { @@ -37,7 +39,13 @@ public class ControlServer : IControlServer while (!ct.IsCancellationRequested) { var ctx = await _httpListener.GetContextAsync(); - // _logger.LogDebug("Got a request"); + + //Check if the disarm request is from the last 5 minutes + if (_disarmRequestPending && (DateTime.Now - _disarmRequestTime).TotalMinutes > 5) + { + _disarmRequestPending = false; + _logger.LogWarning("Disarm request timed out"); + } var request = ctx.Request; var response = ctx.Response; @@ -53,6 +61,7 @@ public class ControlServer : IControlServer var eventDataString = eventData ?? string.Empty; var sensorEvent = new SensorEvent(eventId, eventTypeEnum, eventDataString); + _lastContactTime = DateTime.Now; if (sensorEvent.Type == EventType.Update) { @@ -186,6 +195,13 @@ public class ControlServer : IControlServer public void RequestDisarm() { _disarmRequestPending = true; + _disarmRequestTime = DateTime.Now; + } + + public bool IsConnected() + { + //Check if the last contact time was less than 5 minutes ago + return (DateTime.Now - _lastContactTime).TotalMinutes < 1; } } \ No newline at end of file diff --git a/src/CasaBot/ControlServer/IControlServer.cs b/src/CasaBot/ControlServer/IControlServer.cs index a61fc2e..20e43f9 100644 --- a/src/CasaBot/ControlServer/IControlServer.cs +++ b/src/CasaBot/ControlServer/IControlServer.cs @@ -7,4 +7,5 @@ public interface IControlServer : IHostedService public void OnEvent(Func onEventRecived); public void AuthorizeEvent(long eventId); public void RequestDisarm(); + public bool IsConnected(); } \ No newline at end of file