From a9f9b071f1648e3e1a959ae709a41fb3ed39ed24 Mon Sep 17 00:00:00 2001 From: pomeloy <45542782+pomeloy@users.noreply.github.com> Date: Thu, 4 Apr 2024 14:00:47 +0200 Subject: [PATCH] Calculate heartbeat interval using sleeping_time --- flathunter/heartbeat.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/flathunter/heartbeat.py b/flathunter/heartbeat.py index 8a5955e6..e643edff 100644 --- a/flathunter/heartbeat.py +++ b/flathunter/heartbeat.py @@ -1,6 +1,4 @@ """Providing heartbeat messages""" -from typing import Optional - from flathunter.abstract_notifier import Notifier from flathunter.config import YamlConfig from flathunter.logging import logger @@ -8,16 +6,16 @@ from flathunter.exceptions import HeartbeatException -def interval2counter(interval: str) -> Optional[int]: +def interval2counter(interval: str) -> int: """Transform the string interval to sleeper counter frequencies""" if interval is None: - return None + return 0 if interval.lower() == 'hour': - return 6 + return 3600 if interval.lower() == 'day': - return 144 + return 86400 if interval.lower() == 'week': - return 1008 + return 604800 raise HeartbeatException( "No valid heartbeat instruction received - no heartbeat messages will be sent.") @@ -25,7 +23,7 @@ def interval2counter(interval: str) -> Optional[int]: class Heartbeat: """Will inform the user on regular intervals whether the bot is still alive""" notifier: Notifier - interval: Optional[int] + interval: int def __init__(self, config: YamlConfig, interval: str): notifiers = config.notifiers() @@ -41,7 +39,7 @@ def __init__(self, config: YamlConfig, interval: str): else: raise HeartbeatException("No notifier configured - check 'notifiers' config section!") - self.interval = interval2counter(interval) + self.interval = int(interval2counter(interval)/int(config.loop_period_seconds())) def send_heartbeat(self, counter) -> int: """Send a new heartbeat message""" @@ -52,7 +50,7 @@ def send_heartbeat(self, counter) -> int: logger.info('Sending heartbeat message.') self.notifier.notify( 'Beep Boop. This is a heartbeat message. ' - 'Your bot is searching actively for flats.' + 'Your bot is actively searching for flats.' ) counter = 0 return counter