feat: add support for last time connected in the status command

This commit is contained in:
Guillermo Marcel 2025-05-09 00:27:07 -03:00
parent aecd3e2bdd
commit 1a361c6bba
3 changed files with 28 additions and 3 deletions

View File

@ -114,7 +114,21 @@ public class BotController : IController
[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 🔴");
await ctx.Responder(msg, _controlServer.IsConnected() ?
$"Alarm is connected 🟢\nLast Contact: {FormatTimeSpan(_controlServer.GetLastContactTime())} ago" :
$"Alarm is not connected 🔴\nLast Contact: {FormatTimeSpan(_controlServer.GetLastContactTime())} ago");
}
private string FormatTimeSpan(TimeSpan? timeSpan)
{
//return a string with the format HH:mm:ss for HH > 0 and mm:ss for HH = 0
if(timeSpan == null || timeSpan == TimeSpan.Zero)
return "Never";
var ts = timeSpan.Value;
return ts.Hours > 0 ?
$"{ts.Hours:D2}:{ts.Minutes:D2}:{ts.Seconds:D2}" :
$"{ts.Minutes:D2}:{ts.Seconds:D2}";
}
private void HandleReply()

View File

@ -14,7 +14,7 @@ public class ControlServer : IControlServer
private Func<SensorEvent, Task>? _onEventRecived;
private bool _disarmRequestPending;
private DateTime _disarmRequestTime;
private DateTime _lastContactTime;
private DateTime? _lastContactTime;
public ControlServer(ILogger<ControlServer> logger, IOptions<ControlServerOptions> options)
{
@ -201,7 +201,17 @@ public class ControlServer : IControlServer
public bool IsConnected()
{
//Check if the last contact time was less than 5 minutes ago
return (DateTime.Now - _lastContactTime).TotalMinutes < 1;
if(_lastContactTime is null)
return false;
return (DateTime.Now - _lastContactTime.Value).TotalMinutes < 1;
}
//function to return how long ago was the last contact as timespan
public TimeSpan? GetLastContactTime()
{
if(_lastContactTime is null)
return null;
return DateTime.Now - _lastContactTime;
}
}

View File

@ -8,4 +8,5 @@ public interface IControlServer : IHostedService
public void AuthorizeEvent(long eventId);
public void RequestDisarm();
public bool IsConnected();
public TimeSpan? GetLastContactTime();
}