aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.py2
-rw-r--r--dial.py11
-rw-r--r--events.py93
-rwxr-xr-xmain.py5
4 files changed, 53 insertions, 58 deletions
diff --git a/config.py b/config.py
index 8e1f6c3..bbd9996 100644
--- a/config.py
+++ b/config.py
@@ -1,4 +1,4 @@
import configparser
config = configparser.ConfigParser()
-config.read('config.ini')
+config.read('/etc/phone/config.ini')
diff --git a/dial.py b/dial.py
index 8fc92a9..d5117ff 100644
--- a/dial.py
+++ b/dial.py
@@ -58,18 +58,21 @@ class DialManager:
self._load_sequences(phone_held, phone_hung_up)
def _load_sequences(self, phone_held, phone_hung_up):
- def recursive_add(classname, sequence_list, sequences):
+ def recursive_add(classname, sequence_list, sequences, args):
key = int(sequence_list[0])
if not sequence_list[1:]:
- sequences[key] = eval(classname)(self, phone_held, phone_hung_up)
+ sequences[key] = eval(classname)(self, phone_held, phone_hung_up, args)
else:
sequences[key] = {}
- recursive_add(classname, sequence_list[1:], sequences[key])
+ recursive_add(classname, sequence_list[1:], sequences[key], args)
self.sequences = {}
for sequence in config["sequences"]:
print(sequence, config["sequences"][sequence], sep="\t")
- recursive_add(config["sequences"][sequence], list(sequence), self.sequences)
+ parts = config["sequences"][sequence].split(" ", 1)
+ classname = parts[0]
+ args = parts[1:]
+ recursive_add(classname, list(sequence), self.sequences, args)
def dial(self, number):
if datetime.now() - self.update > timedelta(seconds=DIAL_RESET_TIME):
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)
diff --git a/main.py b/main.py
index 10d4bb4..c72147c 100755
--- a/main.py
+++ b/main.py
@@ -39,8 +39,9 @@ if __name__ == "__main__":
# If we matched a sequence, play out event
if response is not None:
processing_event.set()
- response.speak()
- processing_event.clear()
+ response.run()
except Exception as e:
print(e)
+ finally:
+ processing_event.clear()