From 8540b170db5e0dc9790850e2f36a89a517c36ebc Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Thu, 12 Aug 2021 13:46:52 +0200 Subject: [PATCH] Stop timer intent + starting working on background tasks --- jarvis/skills/background_tasks.py | 28 ++++++++++++ jarvis/skills/daily/timer/__init__.py | 45 +++++++++++++++++-- .../daily/timer/vocab/fr-fr/stop_timer.intent | 2 + 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 jarvis/skills/background_tasks.py create mode 100644 jarvis/skills/daily/timer/vocab/fr-fr/stop_timer.intent diff --git a/jarvis/skills/background_tasks.py b/jarvis/skills/background_tasks.py new file mode 100644 index 0000000..e861e98 --- /dev/null +++ b/jarvis/skills/background_tasks.py @@ -0,0 +1,28 @@ +from threading import Thread + +running_tasks = dict() + + +def add_task(key, thread: Thread): + running_tasks[key] = thread + running_tasks[key].start() + + +def remove_task(key): + del running_tasks[key] + + +def contains_task(key): + if key in running_tasks.keys(): + return True + return False + + +def get_tasks_starting_with_in_name(starts_with): + matching = [] + + for key in running_tasks.keys(): + if str(key).startswith(starts_with): + matching.append(key) + + return matching diff --git a/jarvis/skills/daily/timer/__init__.py b/jarvis/skills/daily/timer/__init__.py index d5d1e9c..103964e 100644 --- a/jarvis/skills/daily/timer/__init__.py +++ b/jarvis/skills/daily/timer/__init__.py @@ -1,6 +1,9 @@ +import threading +from time import time, sleep + from lingua_franca.parse import extract_duration -from jarvis.skills import Skill, SkillRegistering +from jarvis.skills import Skill, SkillRegistering, background_tasks from jarvis.skills.decorators import intent_file_handler from jarvis.utils import languages_utils @@ -12,12 +15,15 @@ class TimerSkill(Skill, metaclass=SkillRegistering): def register(self): super(TimerSkill, self).register() - @intent_file_handler("start_timer.intent", "StartTimerIntent") def handle_start_timer(self, data): print(data) + + timestamp_in_seconds = int(time()) + if 'duration' in data: - print(extract_duration(data['duration']), languages_utils.get_language()) + duration = extract_duration(data['duration'], languages_utils.get_language())[0] + duration_in_seconds = int(duration.total_seconds()) if 'name' in data: print("Start timer for {} named {}".format(data['duration'], data['name'])) @@ -25,11 +31,42 @@ class TimerSkill(Skill, metaclass=SkillRegistering): else: print("Start timer for {} without name".format(data['duration'])) # TODO : ask for name - pass + + timer_thread_key = "timer_" + str(timestamp_in_seconds) + "_" + str( + timestamp_in_seconds + duration_in_seconds) + background_tasks.add_task( + timer_thread_key, + threading.Thread(target=timer_thread, args=[duration_in_seconds, timer_thread_key])) else: print("No amount and/or time_unit") pass + @intent_file_handler("stop_timer.intent", "StopTimerIntent") + def handle_stop_timer(self, data): + + if len(get_all_timers()) <= 1: + background_tasks.remove_task(get_all_timers()[0]) + + print("stop") + def create_skill(data): return TimerSkill(data) + + +def timer_thread(seconds, key): + print("Starting timer in thread for " + str(seconds) + "s") + + while seconds > 0 and key in background_tasks.running_tasks.keys(): + print(str(seconds)) + seconds = seconds - 1 + sleep(1) + + if key not in background_tasks.running_tasks.keys(): + print("Timer (" + key + ") killed!") + else: + print("End of timer!! BIP BIP BOUP BIP") + + +def get_all_timers(): + return background_tasks.get_tasks_starting_with_in_name("timer") diff --git a/jarvis/skills/daily/timer/vocab/fr-fr/stop_timer.intent b/jarvis/skills/daily/timer/vocab/fr-fr/stop_timer.intent new file mode 100644 index 0000000..d427473 --- /dev/null +++ b/jarvis/skills/daily/timer/vocab/fr-fr/stop_timer.intent @@ -0,0 +1,2 @@ +(Arrête|Coupe|Stop) le (minuteur|timer) (de {duration}) +(Arrête|Coupe|Stop) le (minuteur|timer) (de {duration}) (nommé|appelé|qui s'appelle|et appelle le) {name} \ No newline at end of file