-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e25701f
commit 9dee54a
Showing
8 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import time | ||
import logging | ||
import threading | ||
|
||
from ..sentry import Sentry | ||
from ..repeattimer import RepeatTimer | ||
|
||
# A simple class to watch for the bed to cooldown and then fires a notification. | ||
class BedCooldownWatcher: | ||
|
||
# The amount of time between checks. | ||
c_checkIntervalSec = 2 | ||
|
||
# The max amount of time we will allow this to keep watching. | ||
c_maxWatcherRuntimeSec = 60 * 20 | ||
|
||
|
||
_Instance = None | ||
|
||
|
||
@staticmethod | ||
def Init(logger:logging.Logger, notificationHandler, printerStateInterface) -> None: | ||
BedCooldownWatcher._Instance = BedCooldownWatcher(logger, notificationHandler, printerStateInterface) | ||
|
||
|
||
@staticmethod | ||
def Get() -> 'BedCooldownWatcher': | ||
return BedCooldownWatcher._Instance | ||
|
||
|
||
def __init__(self, logger:logging.Logger, notificationHandler, printerStateInterface): | ||
self.Logger = logger | ||
self.NotificationHandler = notificationHandler | ||
self.PrinterStateInterface = printerStateInterface | ||
self.Timer = None | ||
self.TimerStartSec = None | ||
self.Lock = threading.Lock() | ||
|
||
|
||
# Starts the waiter if it's not running. | ||
def Start(self) -> None: | ||
with self.Lock: | ||
# Stop any running timer. | ||
self._stopTimerUnderLock() | ||
|
||
self.Logger.info("Bed cooldown watcher starting") | ||
self.TimerStartSec = time.time() | ||
|
||
# Start a new timer. | ||
self.Timer = RepeatTimer(self.Logger, BedCooldownWatcher.c_checkIntervalSec, self._timerCallback) | ||
self.Timer.start() | ||
|
||
|
||
# Stops the timer if it's running. | ||
def Stop(self): | ||
with self.Lock: | ||
self._stopTimerUnderLock() | ||
|
||
|
||
|
||
def _stopTimerUnderLock(self): | ||
if self.Timer is not None: | ||
self.Logger.info("Bed cooldown watcher stopped.") | ||
self.Timer.Stop() | ||
self.Timer = None | ||
|
||
|
||
def _timerCallback(self): | ||
try: | ||
# Check if we should stop watching. | ||
if time.time() - self.TimerStartSec > BedCooldownWatcher.c_maxWatcherRuntimeSec: | ||
self.Logger.info("Bed cooldown watcher, max runtime reached. Stopping.") | ||
self.Stop() | ||
return | ||
|
||
# Try to get the current temps | ||
(_, bedTemp) = self.PrinterStateInterface.GetTemps() | ||
if bedTemp is None: | ||
self.Logger.info("Bed cooldown watcher, no bed temp available. Stopping.") | ||
self.Stop() | ||
return | ||
|
||
# When the bed is under ~90F, we will consider it cooled down. | ||
if bedTemp > 33: | ||
# Keep waiting. | ||
self.Logger.debug(f"Bed cooldown watcher, bed temp is {bedTemp}. Waiting...") | ||
return | ||
|
||
# The bed is cooled down. | ||
self.Logger.info("Bed cooldown watcher, bed is cooled down.") | ||
self.Stop() | ||
|
||
# Fire the notification. | ||
self.NotificationHandler.OnBedCooldownComplete(bedTemp) | ||
|
||
except Exception as e: | ||
Sentry.Exception("BedCooldownWatcher exception in timer callback", e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters