feat: discard disarm request after timeout
feat: Is Connected command
This commit is contained in:
parent
b11aac2857
commit
aecd3e2bdd
@ -11,6 +11,8 @@ namespace CasaBotApp.Controllers;
|
|||||||
|
|
||||||
public class BotController : IController
|
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 BotHandler _botHandler;
|
||||||
private readonly ILogger<BotController> _logger;
|
private readonly ILogger<BotController> _logger;
|
||||||
private readonly AutoScanApp _autoScanApp;
|
private readonly AutoScanApp _autoScanApp;
|
||||||
@ -103,8 +105,16 @@ public class BotController : IController
|
|||||||
[BotCommand("/disarm", "Disarm the Door Sensor")]
|
[BotCommand("/disarm", "Disarm the Door Sensor")]
|
||||||
public async Task Disarm(TextMessage msg, BotCommand ctx)
|
public async Task Disarm(TextMessage msg, BotCommand ctx)
|
||||||
{
|
{
|
||||||
await ctx.Responder(msg, "Disarming the door sensor");
|
|
||||||
_controlServer.RequestDisarm();
|
_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()
|
private void HandleReply()
|
||||||
|
@ -13,6 +13,8 @@ public class ControlServer : IControlServer
|
|||||||
private readonly Dictionary<long, SensorEvent> _events = new();
|
private readonly Dictionary<long, SensorEvent> _events = new();
|
||||||
private Func<SensorEvent, Task>? _onEventRecived;
|
private Func<SensorEvent, Task>? _onEventRecived;
|
||||||
private bool _disarmRequestPending;
|
private bool _disarmRequestPending;
|
||||||
|
private DateTime _disarmRequestTime;
|
||||||
|
private DateTime _lastContactTime;
|
||||||
|
|
||||||
public ControlServer(ILogger<ControlServer> logger, IOptions<ControlServerOptions> options)
|
public ControlServer(ILogger<ControlServer> logger, IOptions<ControlServerOptions> options)
|
||||||
{
|
{
|
||||||
@ -37,7 +39,13 @@ public class ControlServer : IControlServer
|
|||||||
while (!ct.IsCancellationRequested)
|
while (!ct.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var ctx = await _httpListener.GetContextAsync();
|
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 request = ctx.Request;
|
||||||
var response = ctx.Response;
|
var response = ctx.Response;
|
||||||
@ -53,6 +61,7 @@ public class ControlServer : IControlServer
|
|||||||
var eventDataString = eventData ?? string.Empty;
|
var eventDataString = eventData ?? string.Empty;
|
||||||
var sensorEvent = new SensorEvent(eventId, eventTypeEnum, eventDataString);
|
var sensorEvent = new SensorEvent(eventId, eventTypeEnum, eventDataString);
|
||||||
|
|
||||||
|
_lastContactTime = DateTime.Now;
|
||||||
|
|
||||||
if (sensorEvent.Type == EventType.Update)
|
if (sensorEvent.Type == EventType.Update)
|
||||||
{
|
{
|
||||||
@ -186,6 +195,13 @@ public class ControlServer : IControlServer
|
|||||||
public void RequestDisarm()
|
public void RequestDisarm()
|
||||||
{
|
{
|
||||||
_disarmRequestPending = true;
|
_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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -7,4 +7,5 @@ public interface IControlServer : IHostedService
|
|||||||
public void OnEvent(Func<SensorEvent, Task> onEventRecived);
|
public void OnEvent(Func<SensorEvent, Task> onEventRecived);
|
||||||
public void AuthorizeEvent(long eventId);
|
public void AuthorizeEvent(long eventId);
|
||||||
public void RequestDisarm();
|
public void RequestDisarm();
|
||||||
|
public bool IsConnected();
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user