From 5b2a3f0da147f17f6ab32af8e3d14fb29de71fd1 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Mon, 12 Jul 2021 20:27:50 -0500 Subject: Fix handler so events cannot be dialed while one is playing, and so events can be terminated --- events.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'events.py') diff --git a/events.py b/events.py index cbd17ea..f177a01 100644 --- a/events.py +++ b/events.py @@ -4,6 +4,17 @@ from config import config import requests from multiprocessing import Process +class SpeakProcess(Process): + def __init__(self, text): + super().__init__() + self.daemon = True + self.text = text + + def run(self): + print("speaking:", self.text) + subprocess.run(["espeak", self.text]) + + class Event(): def __init__(self, dial_manager, phone_held, phone_hung_up): self.dial_manager = dial_manager @@ -17,9 +28,11 @@ class Event(): return "event" def speak(self): - text = self.get_text() - print("speaking:", text) - subprocess.run(["espeak", text]) + process = subprocess.Popen(["espeak", self.get_text()]) + while process.poll() is None: + if self.phone_hung_up.wait(timeout=0.1): + process.terminate() + class FortuneEvent(Event): def get_name(self): @@ -56,7 +69,12 @@ class OperatorEvent(Event): def get_text(self): now = datetime.now() - return f"Right now, it is {now.hour} {now.minute}. Dial 4 1 1 for directory." + nearest_quarter = ((n.minute // 15 + 1) * 15) % 60 + hour = now.hour % 12 + day_half = "A M" + if hour != now.hour: + day_half = "P M" + return f"Right now, it is {hour} {now.minute} {day_half}." class WeatherEvent(Event): -- cgit v1.2.3