From 1a361c6bbac35fc61358ea8a01f682176581b4d4 Mon Sep 17 00:00:00 2001 From: Guillermo Marcel Date: Fri, 9 May 2025 00:27:07 -0300 Subject: [PATCH] feat: add support for last time connected in the status command --- .../CasaBotApp/Controllers/BotController.cs | 16 +++++++++++++++- src/CasaBot/ControlServer/ControlServer.cs | 14 ++++++++++++-- src/CasaBot/ControlServer/IControlServer.cs | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/CasaBot/CasaBotApp/Controllers/BotController.cs b/src/CasaBot/CasaBotApp/Controllers/BotController.cs index 76bd90b..a1c1424 100644 --- a/src/CasaBot/CasaBotApp/Controllers/BotController.cs +++ b/src/CasaBot/CasaBotApp/Controllers/BotController.cs @@ -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() diff --git a/src/CasaBot/ControlServer/ControlServer.cs b/src/CasaBot/ControlServer/ControlServer.cs index 24b5f3d..e4310da 100644 --- a/src/CasaBot/ControlServer/ControlServer.cs +++ b/src/CasaBot/ControlServer/ControlServer.cs @@ -14,7 +14,7 @@ public class ControlServer : IControlServer private Func? _onEventRecived; private bool _disarmRequestPending; private DateTime _disarmRequestTime; - private DateTime _lastContactTime; + private DateTime? _lastContactTime; public ControlServer(ILogger logger, IOptions 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; + } + } \ No newline at end of file diff --git a/src/CasaBot/ControlServer/IControlServer.cs b/src/CasaBot/ControlServer/IControlServer.cs index 20e43f9..4ade315 100644 --- a/src/CasaBot/ControlServer/IControlServer.cs +++ b/src/CasaBot/ControlServer/IControlServer.cs @@ -8,4 +8,5 @@ public interface IControlServer : IHostedService public void AuthorizeEvent(long eventId); public void RequestDisarm(); public bool IsConnected(); + public TimeSpan? GetLastContactTime(); } \ No newline at end of file