Stop timer intent + starting working on background tasks
This commit is contained in:
parent
4b76a0ce1f
commit
8540b170db
28
jarvis/skills/background_tasks.py
Normal file
28
jarvis/skills/background_tasks.py
Normal file
@ -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
|
@ -1,6 +1,9 @@
|
|||||||
|
import threading
|
||||||
|
from time import time, sleep
|
||||||
|
|
||||||
from lingua_franca.parse import extract_duration
|
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.skills.decorators import intent_file_handler
|
||||||
from jarvis.utils import languages_utils
|
from jarvis.utils import languages_utils
|
||||||
|
|
||||||
@ -12,12 +15,15 @@ class TimerSkill(Skill, metaclass=SkillRegistering):
|
|||||||
def register(self):
|
def register(self):
|
||||||
super(TimerSkill, self).register()
|
super(TimerSkill, self).register()
|
||||||
|
|
||||||
|
|
||||||
@intent_file_handler("start_timer.intent", "StartTimerIntent")
|
@intent_file_handler("start_timer.intent", "StartTimerIntent")
|
||||||
def handle_start_timer(self, data):
|
def handle_start_timer(self, data):
|
||||||
print(data)
|
print(data)
|
||||||
|
|
||||||
|
timestamp_in_seconds = int(time())
|
||||||
|
|
||||||
if 'duration' in data:
|
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:
|
if 'name' in data:
|
||||||
print("Start timer for {} named {}".format(data['duration'], data['name']))
|
print("Start timer for {} named {}".format(data['duration'], data['name']))
|
||||||
@ -25,11 +31,42 @@ class TimerSkill(Skill, metaclass=SkillRegistering):
|
|||||||
else:
|
else:
|
||||||
print("Start timer for {} without name".format(data['duration']))
|
print("Start timer for {} without name".format(data['duration']))
|
||||||
# TODO : ask for name
|
# 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:
|
else:
|
||||||
print("No amount and/or time_unit")
|
print("No amount and/or time_unit")
|
||||||
pass
|
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):
|
def create_skill(data):
|
||||||
return TimerSkill(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")
|
||||||
|
2
jarvis/skills/daily/timer/vocab/fr-fr/stop_timer.intent
Normal file
2
jarvis/skills/daily/timer/vocab/fr-fr/stop_timer.intent
Normal file
@ -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}
|
Reference in New Issue
Block a user