aboutsummaryrefslogtreecommitdiff
path: root/events.py
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2021-07-19 20:49:51 -0500
committerMark Powers <mark@marks.kitchen>2021-07-19 20:49:51 -0500
commitdc62736d843949a4f6a01cbd1af09c25a38673a9 (patch)
tree7efe8f9add595cff625c3c4fc549ce38f35c80c7 /events.py
parent5b2a3f0da147f17f6ab32af8e3d14fb29de71fd1 (diff)
Refactor how some events work
Diffstat (limited to 'events.py')
-rw-r--r--events.py93
1 files changed, 42 insertions, 51 deletions
diff --git a/events.py b/events.py
index f177a01..fa702c0 100644
--- a/events.py
+++ b/events.py
@@ -4,22 +4,24 @@ 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])
+def run_until_hangup(command, phone_hung_up):
+ '''
+ returns true if process was finished
+ '''
+ process = subprocess.Popen(command)
+ while process.poll() is None:
+ if phone_hung_up.wait(timeout=0.1):
+ process.terminate()
+ return False
+ return True
class Event():
- def __init__(self, dial_manager, phone_held, phone_hung_up):
+ def __init__(self, dial_manager, phone_held, phone_hung_up, args):
self.dial_manager = dial_manager
self.phone_held = phone_held
self.phone_hung_up = phone_hung_up
+ self.args = args
def get_name(self):
return "generic event"
@@ -27,11 +29,8 @@ class Event():
def get_text(self):
return "event"
- def speak(self):
- process = subprocess.Popen(["espeak", self.get_text()])
- while process.poll() is None:
- if self.phone_hung_up.wait(timeout=0.1):
- process.terminate()
+ def run(self):
+ run_until_hangup(["espeak", self.get_text()], self.phone_hung_up)
class FortuneEvent(Event):
@@ -42,6 +41,7 @@ class FortuneEvent(Event):
res = subprocess.run(["fortune", "-s"], stdout=subprocess.PIPE)
return res.stdout.decode("utf-8")
+
class DirectoryEvent(Event):
def get_name(self):
return "directory"
@@ -67,14 +67,17 @@ class OperatorEvent(Event):
def get_name(self):
return "operator"
- def get_text(self):
- now = datetime.now()
- 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}."
+ def run(self):
+ loop = True
+ while loop:
+ now = datetime.now()
+ nearest_quarter = ((now.minute // 15 + 1) * 15) % 60
+ next_hour = now.hour % 12 if nearest_quarter != 0 else (now.hour + 1) % 12
+ next_hour = next_hour if next_hour != 0 else 12
+ text = f"In {nearest_quarter - now.minute} minutes it will be {next_hour} {nearest_quarter}"
+ loop = run_until_hangup(["espeak", text], self.phone_hung_up)
+ if loop:
+ loop = run_until_hangup(["aplay", "/etc/phone/sound/hold.wav"], self.phone_hung_up)
class WeatherEvent(Event):
@@ -98,42 +101,30 @@ class WeatherEvent(Event):
return f"{weather}{forecast}"
-class TimerEvent(Event):
+class RecordEvent(Event):
def get_name(self):
- return "timer"
-
- def get_text(self):
- start = datetime.now()
- self.phone_hung_up.wait()
- end = datetime.now()
- print(end - start)
- return "time"
-
-class RecordThread(Process):
- def __init__(self):
- super().__init__()
- self.daemon = True
+ return "recorder"
def run(self):
- subprocess.run(["time", "cat"])
+ run_until_hangup(["espeak", "Please record a message."], self.phone_hung_up)
+ process = subprocess.Popen(["arecord", "/tmp/out.wav"])
+ self.phone_hung_up.wait()
+ process.terminate()
+ return "recording done"
-class RecordEvent(Event):
+class PlayEvent(Event):
def get_name(self):
- return "recorder"
+ return "play"
- def get_text(self):
- start = datetime.now()
- print("timer start", start)
+ def run(self):
+ run_until_hangup(["aplay", "/tmp/out.wav"], self.phone_hung_up)
- record_thread = RecordThread()
- record_thread.start()
+class WavEvent(Event):
+ def get_name(self):
+ return self.args[0]
- self.phone_hung_up.wait()
- print("terminating")
- record_thread.terminate()
+ def run(self):
+ run_until_hangup(["aplay", self.args[0]], self.phone_hung_up)
- end = datetime.now()
- print(end - start)
- return str(end-start)