From a7f07afc14d2576d433b44cca9cf6b5079ab4cf3 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Thu, 26 Sep 2024 18:13:58 -0700 Subject: [PATCH] consolidateTime --- mesh_bot.py | 36 ++++++++---------------------------- modules/system.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/mesh_bot.py b/mesh_bot.py index f43d8c3..8f2b396 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -543,22 +543,12 @@ def handle_lheard(message, nodeid, deviceID, isDM): if interface2_enabled: bot_response += " P2:" + str(chutil2) + "%" + "/" + str(airUtilTx2) + "%" # convert uptime to minutes, hours, or days - if uptimeSeconds > 0 or uptimeSeconds2 > 0: - uptimeSeconds = round(uptimeSeconds / 60) - uptimeSeconds2 = round(uptimeSeconds2 / 60) - designator = "m" - if uptimeSeconds > 60 or uptimeSeconds2 > 60: - uptimeSeconds = round(uptimeSeconds / 60) - uptimeSeconds2 = round(uptimeSeconds2 / 60) - designator = "h" - if uptimeSeconds > 24 or uptimeSeconds2 > 24: - uptimeSeconds = round(uptimeSeconds / 24) - uptimeSeconds2 = round(uptimeSeconds2 / 24) - designator = "d" + uptimeSeconds = getPrettyTime(uptimeSeconds) + uptimeSeconds2 = getPrettyTime(uptimeSeconds2) # add uptime and battery info to the bot response - bot_response += "\nUptime:" + str(uptimeSeconds) + designator + bot_response += "\nUptime:" + str(uptimeSeconds) if interface2_enabled: - bot_response += f" P2:" + {uptimeSeconds2} + {designator} + bot_response += f" P2:" + {uptimeSeconds2} if not batteryLevel == 101: bot_response += f" Bat: {batteryLevel}% Volt: {voltage}" if interface2_enabled and not batteryLevel2 == 101: @@ -580,13 +570,8 @@ def handle_history(message, nodeid, deviceID, isDM, lheard=False): # show the last commands from the user to the bot elif not lheard: for i in range(len(cmdHistory)): - prettyTime = round((time.time() - cmdHistory[i]['time']) / 600) * 5 - if prettyTime < 60: - prettyTime = str(prettyTime) + "m" - elif prettyTime < 1440: - prettyTime = str(round(prettyTime/60)) + "h" - else: - prettyTime = str(round(prettyTime/1440)) + "d" + cmdTime = round((time.time() - cmdHistory[i]['time']) / 600) * 5 + prettyTime = getPrettyTime(cmdTime) # history display output if nodeid in bbs_admin_list and cmdHistory[i]['nodeID'] not in lheardCmdIgnoreNode: @@ -606,13 +591,8 @@ def handle_history(message, nodeid, deviceID, isDM, lheard=False): else: # sort the cmdHistory list by time, return the username and time into a new list which used for display for i in range(len(cmdHistory)): - prettyTime = round((time.time() - cmdHistory[i]['time']) / 600) * 5 - if prettyTime < 60: - prettyTime = str(prettyTime) + "m" - elif prettyTime < 1440: - prettyTime = str(round(prettyTime/60)) + "h" - else: - prettyTime = str(round(prettyTime/1440)) + "d" + cmdTime = round((time.time() - cmdHistory[i]['time']) / 600) * 5 + prettyTime = getPrettyTime(cmdTime) if cmdHistory[i]['nodeID'] not in lheardCmdIgnoreNode: # add line to a new list for display diff --git a/modules/system.py b/modules/system.py index 27b2c88..dbfd19b 100644 --- a/modules/system.py +++ b/modules/system.py @@ -586,6 +586,23 @@ def get_wikipedia_summary(search_term): return summary +def getPrettyTime(seconds): + # convert unix time to minutes, hours, or days, or years for simple display + designator = "s" + if seconds > 0: + seconds = round(seconds / 60) + designator = "m" + if seconds > 60: + seconds = round(seconds / 60) + designator = "h" + if seconds > 24: + seconds = round(seconds / 24) + designator = "d" + if seconds > 365: + seconds = round(seconds / 365) + designator = "y" + return str(seconds) + designator + def messageTrap(msg): # Check if the message contains a trap word, this is the first filter for listning to messages # after this the message is passed to the command_handler in the bot.py which is switch case filter for applying word to function