-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleepThread.py
42 lines (37 loc) · 1.33 KB
/
sleepThread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import time
from threading import Thread, Lock
from queue import Full
from modes import Modes
import logging
class SleepThread(Thread):
def __init__(self, cmd_queue):
super().__init__(daemon=True)
self.cmd_queue = cmd_queue
self.lock = Lock()
self.wakeup_time = None
self.log = logging.getLogger("Pause")
self.log.setLevel(logging.DEBUG)
def sleep(self, duration):
self.log.info("Going to sleep till %s" % time.strftime("%H:%M", time.localtime(time.time() + duration)))
with self.lock:
self.wakeup_time = time.time() + duration
try:
self.cmd_queue.put(Modes.OFF)
except Full:
self.log.critical("Command queue full")
def reset(self):
if self.wakeup_time:
self.log.info("Cancelling sleep timer due to user input")
with self.lock:
self.wakeup_time = None
def run(self) -> None:
while 1:
time.sleep(60)
if self.wakeup_time and time.time() >= self.wakeup_time:
self.log.info("Waking up from sleep mode")
with self.lock:
self.wakeup_time = None
try:
self.cmd_queue.put(Modes.ON)
except Full:
self.log.critical("Command queue full")