2021-08-12 13:46:52 +02:00
|
|
|
import threading
|
|
|
|
from time import time, sleep
|
|
|
|
|
2021-08-02 18:23:11 +02:00
|
|
|
from lingua_franca.parse import extract_duration
|
|
|
|
|
2021-08-12 13:46:52 +02:00
|
|
|
from jarvis.skills import Skill, SkillRegistering, background_tasks
|
2021-08-02 12:01:32 +02:00
|
|
|
from jarvis.skills.decorators import intent_file_handler
|
2021-08-02 18:23:11 +02:00
|
|
|
from jarvis.utils import languages_utils
|
2021-08-02 12:01:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TimerSkill(Skill, metaclass=SkillRegistering):
|
|
|
|
def __init__(self, data=dict):
|
|
|
|
super().__init__("TimerSkill", data)
|
|
|
|
|
2021-08-02 23:07:06 +02:00
|
|
|
def register(self):
|
|
|
|
super(TimerSkill, self).register()
|
|
|
|
|
2021-08-02 12:01:32 +02:00
|
|
|
@intent_file_handler("start_timer.intent", "StartTimerIntent")
|
|
|
|
def handle_start_timer(self, data):
|
|
|
|
print(data)
|
2021-08-12 13:46:52 +02:00
|
|
|
|
|
|
|
timestamp_in_seconds = int(time())
|
|
|
|
|
2021-08-02 23:07:06 +02:00
|
|
|
if 'duration' in data:
|
2021-08-12 13:46:52 +02:00
|
|
|
duration = extract_duration(data['duration'], languages_utils.get_language())[0]
|
|
|
|
duration_in_seconds = int(duration.total_seconds())
|
2021-08-02 18:23:11 +02:00
|
|
|
|
|
|
|
if 'name' in data:
|
2021-08-02 23:07:06 +02:00
|
|
|
print("Start timer for {} named {}".format(data['duration'], data['name']))
|
2021-08-02 18:23:11 +02:00
|
|
|
pass
|
|
|
|
else:
|
2021-08-02 23:07:06 +02:00
|
|
|
print("Start timer for {} without name".format(data['duration']))
|
2021-08-02 18:23:11 +02:00
|
|
|
# TODO : ask for name
|
2021-08-12 13:46:52 +02:00
|
|
|
|
|
|
|
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]))
|
2021-08-02 18:23:11 +02:00
|
|
|
else:
|
|
|
|
print("No amount and/or time_unit")
|
2021-08-02 12:01:32 +02:00
|
|
|
pass
|
|
|
|
|
2021-08-12 13:46:52 +02:00
|
|
|
@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")
|
|
|
|
|
2021-08-02 12:01:32 +02:00
|
|
|
|
|
|
|
def create_skill(data):
|
|
|
|
return TimerSkill(data)
|
2021-08-12 13:46:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
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")
|