finish initial timer implementation, still need to add timer sounds.

This commit is contained in:
samerbam 2023-06-22 14:56:11 -04:00
parent d84a042d56
commit 30af7cdeab
2 changed files with 62 additions and 16 deletions

View File

@ -2,8 +2,10 @@ import requests
if __name__ == "__main__": # Handle if __name__ == "__main__": # Handle
from config import ntfy_url from config import ntfy_url
from utility import parsetime2
else: else:
from skills.config import ntfy_url from skills.config import ntfy_url
from skills.utility import parsetime2
import threading import threading
@ -63,12 +65,11 @@ class Timers:
}) })
return r return r
def _add_timer(self, time, name): def _add_timer(self, duration, name):
if len(self.timers) == 0: if len(self.timers) == 0:
self.stop_run_continuously = run_continuously() self.stop_run_continuously = run_continuously(self.schedule)
self.timers[name] = time self.timers[name] = time.mktime(duration.timetuple())
# duration = self.schedule.every().day.at(time.strftime("%H:%M:%S", duration.timetuple())).do(self._trigger_timer, name).tag(name)
self.schedule.every().day.at(time.strftime("%H:%M:%S", time.gmtime(duration))).do(self._trigger_timer(name)).tag(name)
# use https://schedule.readthedocs.io/en/stable/examples.html#run-a-job-once to trigger self._trigger_timer() # use https://schedule.readthedocs.io/en/stable/examples.html#run-a-job-once to trigger self._trigger_timer()
def _remove_timer(self, name): def _remove_timer(self, name):
@ -86,14 +87,17 @@ class Timers:
# #TODO: play timer done sound # #TODO: play timer done sound
return schedule.CancelJob return schedule.CancelJob
def get_remaining_time(self, name=""): def get_remaining_time(self, name=""): #TODO: test this function
pass """Returns time remaining for timer as seconds remaining"""
return self.timers[name]-time.mktime(datetime.now().timetuple())
# if name == "": # if name == "":
def run(self, query="", time=0, name=""): def run(self, query="", duration_string="", name=""):
if "add" in query: if "add" in query:
self._add_timer(time, name) # duration = time.mktime(parsetime2(duration_string).timetuple())
duration = parsetime2(duration_string)
self._add_timer(duration, name)
return True # Return true to indicate success return True # Return true to indicate success
if "remove" in query: if "remove" in query:
self._remove_timer(name) self._remove_timer(name)
@ -105,6 +109,7 @@ class Timers:
if __name__ == "__main__": if __name__ == "__main__":
time = Timers() dur = Timers()
time._add_timer(123, "123") dur.run("add", "2 minutes", "test timer")
time._trigger_timer("123") # dur._add_timer(123, "123")
# dur._trigger_timer("123")

View File

@ -1,10 +1,51 @@
from ctparse import ctparse #Used for parsing time, https://ctparse.readthedocs.io/en/latest/ from ctparse import ctparse #Used for parsing time (parsetime), https://github.com/comtravo/ctparse
import parsedatetime #Used for parsing time (parsetime2), https://github.com/bear/parsedatetime
from datetime import datetime from datetime import datetime
import time
def parsetime(phrase): def parsetime(phrase):
ts = datetime.now() """
return ctparse(phrase, ts=ts) Takes in natrual language time phrase, outputs datetime object
"""
ts = datetime.now()
p = ctparse(phrase, ts=ts)
if p is not None:
return p.resolution.dt
return p
# return ctparse(phrase, ts=ts)
def parsetime2(phrase):
"""
Takes in natrual language time phrase, outputs datetime object
Handles seconds better
Doesnt handle 'in the afternoon'
Does handle 'this afternoon'
"""
time_struct, parse_status = parsedatetime.Calendar().parse(phrase)
return datetime(*time_struct[:6])
if __name__ == "__main__": if __name__ == "__main__":
print(parsetime('May 5th 2:30 in the afternoon').ref_time6) t = parsetime('May 5th 2:30 in the afternoon')
print(t)
# t2 = parsetime('15 seconds')
# # print(t2)
# # print(t2)
# if t2 is not None:
# print(t2.resolution)
t2 = parsetime2('now')
print(time.mktime(t2.timetuple()))
t3 = parsetime2('in 5 minutes 30 seconds')
print(time.mktime(t3.timetuple()))
print(time.strftime("%H:%M:%S", t3.timetuple()))
# for x in t:
# print(x)