aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikkelsv <mikkel.svagard@gmail.com>2017-12-04 01:05:52 +0100
committerMikkelsv <mikkel.svagard@gmail.com>2017-12-04 01:05:52 +0100
commit7db59deffff7e70d22430e8c16ec8984bd4f2bfe (patch)
treefb6bd332dd29f0b043fe426fdfb10ccaf3f98a2e
parent97ae43fa9538c6759367369e1344b82e6fb88baa (diff)
downloadsaitama-7db59deffff7e70d22430e8c16ec8984bd4f2bfe.tar.gz
Add Repeating Event functionality
-rw-r--r--Message_parser.py15
-rw-r--r--timeEventQueuer.py37
2 files changed, 46 insertions, 6 deletions
diff --git a/Message_parser.py b/Message_parser.py
index 1adda5d..ac52c8d 100644
--- a/Message_parser.py
+++ b/Message_parser.py
@@ -31,12 +31,15 @@ class Message_parser():
print("Posting Stop event")
self.eb.post(event(EVENTID_CLIENT_STOP,""))
- if text.startswith('!rm'):
+ elif text.startswith('!rm'):
self.parseRemindMe(message_data)
- if text.startswith('!help'):
+ elif text.startswith('!help'):
self.parseHelp(message_data)
+ elif text.startswith('!repeat'):
+ self.parseRepeat(message_data)
+
def get_message_time(self,i,c):
if c == 's':
return timedelta(seconds = int(i))
@@ -80,6 +83,14 @@ class Message_parser():
message_data.set_text("Help is on the way")
Fb_client.post_message_event(self.eb,message_data)
+ def parseRepeat(self,message_data):
+ print("Parsing Repeat")
+ message_data.set_text("Reapting This lol")
+ send_message_event = event(EVENTID_CLIENT_SEND,message_data)
+ start_time = datetime.now() + timedelta(seconds=5)
+ repeat_delta = timedelta(seconds=5)
+ repeat_event = Repeating_event_object(send_message_event,start_time,repeat_delta)
+ Time_event_queuer.post_repeating_event(self.eb,repeat_event)
diff --git a/timeEventQueuer.py b/timeEventQueuer.py
index d736f1c..e89d2f4 100644
--- a/timeEventQueuer.py
+++ b/timeEventQueuer.py
@@ -10,6 +10,7 @@ from datetime import datetime, timedelta
import heapq
EVENTID_TIME = "Topic: Time Event"
+EVENTID_REPEATING = "Topic: Repeating Event"
EVENTID_QUEUE_START = "Topic: Start Event Queue"
EVENTID_QUEUE_STOP = "Topic: Stop Event Queue"
@@ -36,11 +37,18 @@ class Time_event_queuer(threading.Thread,subscriber):
def execute_event(self):
e = heapq.heappop(self.heap)
if(isinstance(e[1],event)):
- self.eb.post(e[1])
- print("{} - Executing {}".format(datetime.now(),e[1]))
+ new_event = e[1]
+ if(new_event.get_topic() == EVENTID_REPEATING):
+ repeating_event = new_event.get_data()
+ self.eb.post(repeating_event.get_event())
+ print("POSTING NEW TIME EVEEENT")
+ Time_event_queuer.post_repeating_event(self.eb,repeating_event)
+ else:
+ self.eb.post(e[1])
#Listens on the bus
def process(self,new_event):
+ print(new_event.get_topic())
if not isinstance(new_event,event):
print("Invalid event type passed")
return
@@ -96,7 +104,7 @@ class Time_event_queuer(threading.Thread,subscriber):
return
print("TimeQueuer Idle - Execute _flag: {}".format(self.execute_flag))
with self.cv:
- self.cv.wait(3)
+ self.cv.wait()
print("Queuer Stopped from Idle State")
#Calculage sleep time until top of heap event
@@ -133,10 +141,31 @@ class Time_event_queuer(threading.Thread,subscriber):
@staticmethod
def post_time_event(eb,data,delta_time):
event_time = datetime.now() + delta_time #ms removed
- print("{} scheduled at {}".format(data,event_time))
+ print("EVENT scheduled at {}".format(event_time))
data = (event_time,data)
eb.post(event(EVENTID_TIME,data))
+ @staticmethod
+ def post_repeating_event(eb,repeating_event_object):
+ print("Posting REPEATING event")
+ event_time = repeating_event_object.occurence_start
+ now = datetime.now()
+ while(now > event_time):
+ event_time += repeating_event_object.repeat_delta
+ delta_time = event_time - datetime.now()
+ Time_event_queuer.post_time_event(eb,event(EVENTID_REPEATING,repeating_event_object),
+ delta_time)
+
+class Repeating_event_object():
+ def __init__(self,event,occurence_start,repeat_delta):
+ self.event = event #Event to be posted
+ self.occurence_start = occurence_start
+ self.repeat_delta = repeat_delta
+
+ def get_event(self): return self.event
+
+
+
class Thread_handler():
def __init__(self,eb):
self.threads = []