aboutsummaryrefslogtreecommitdiff
path: root/events.py
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2021-08-15 04:52:18 +0100
committerMark Powers <mark@marks.kitchen>2021-08-15 04:52:18 +0100
commitdb747d54bece564ff881cda3e21e38b45c91f35e (patch)
tree55582958a39c48222d76022cc714d4aaf7875ab6 /events.py
parentdc62736d843949a4f6a01cbd1af09c25a38673a9 (diff)
Refactor some things
Diffstat (limited to 'events.py')
-rw-r--r--events.py45
1 files changed, 19 insertions, 26 deletions
diff --git a/events.py b/events.py
index fa702c0..36ca972 100644
--- a/events.py
+++ b/events.py
@@ -4,24 +4,19 @@ from config import config
import requests
from multiprocessing import Process
-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
-
+def tts_command(text):
+ if config["engine"]["tts"] == "espeak":
+ return ["espeak", text]
+ elif config["engine"]["tts"] == "festival":
+ filename = "/tmp/phone_input.txt"
+ with open(filename, "w") as f:
+ f.write(text)
+ return ["festival", "--tts", filename]
class Event():
- 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
+ def __init__(self, phone, args):
self.args = args
+ self.phone = phone
def get_name(self):
return "generic event"
@@ -30,7 +25,7 @@ class Event():
return "event"
def run(self):
- run_until_hangup(["espeak", self.get_text()], self.phone_hung_up)
+ self.phone.run_until_hangup(tts_command(self.get_text()))
class FortuneEvent(Event):
@@ -56,7 +51,7 @@ class DirectoryEvent(Event):
else:
sequence_list.append((sequence, sequences[key]))
return sequence_list
- sequences = self.dial_manager.sequences
+ sequences = self.phone.dial_manager.sequences
text = ""
for sequence, event in recursive_build("", sequences):
text += f"For {event.get_name()} dial {sequence}. "
@@ -75,9 +70,8 @@ class OperatorEvent(Event):
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)
+ commands = [tts_command(text), ["aplay", "/etc/phone/sound/hold.wav"]]
+ self.phone.run_many_until_hangup(commands)
class WeatherEvent(Event):
@@ -106,9 +100,10 @@ class RecordEvent(Event):
return "recorder"
def run(self):
- run_until_hangup(["espeak", "Please record a message."], self.phone_hung_up)
+ self.phone.run_until_hangup(tts_command("Please record a message."))
+
process = subprocess.Popen(["arecord", "/tmp/out.wav"])
- self.phone_hung_up.wait()
+ self.phone.wait_until_hung_up()
process.terminate()
return "recording done"
@@ -118,13 +113,11 @@ class PlayEvent(Event):
return "play"
def run(self):
- run_until_hangup(["aplay", "/tmp/out.wav"], self.phone_hung_up)
+ self.phone.run_until_hangup(["aplay", "/tmp/out.wav"])
class WavEvent(Event):
def get_name(self):
return self.args[0]
def run(self):
- run_until_hangup(["aplay", self.args[0]], self.phone_hung_up)
-
-
+ self.phone.run_until_hangup(["aplay", self.args[0]])