Browse Source

Main loop : sleep and print

Olivier Marty 8 years ago
parent
commit
402af85a29
2 changed files with 22 additions and 6 deletions
  1. 3 0
      event.py
  2. 19 6
      main.py

+ 3 - 0
event.py

@@ -20,3 +20,6 @@ class HeapEvent():
 
     def top(self):
         return self.data[0][1]
+
+    def empty(self):
+        return not self.data

+ 19 - 6
main.py

@@ -1,21 +1,34 @@
 import source
 import config
 from event import Event, HeapEvent
+from datetime import datetime, timedelta
+from time import sleep
 import notification
 from itertools import chain
 
 
 def main():
     heap = HeapEvent()
-    heap.push(Event(1, "Villejuif", "descr Villejuif"))
-    heap.push(Event(0, "Cachan", "descr Cachan"))
-    heap.push(Event(2, "université paris 7", "descr p7"))
+    heap.push(Event(datetime.now()+timedelta(minutes=30, seconds=10), "Villejuif", "descr Villejuif"))
+    heap.push(Event(datetime.now()+timedelta(minutes=30, seconds=3), "Cachan", "descr Cachan"))
+    heap.push(Event(datetime.now()+timedelta(minutes=30, seconds=12), "université paris 7", "descr p7"))
+    gap = timedelta(minutes=30) # 30 minutes : time to check trafic before an event
     while True:
         # TODO feed heap
-        # then sleep the min between 1 minutes and the next event (- 30 minutes ?)
         # then look for problems
-        print(heap.top().location)
-        print(heap.pop().location)
+
+        # sleep the min between 1 minute and the next event - gap
+        next = timedelta(seconds=1) # TODO 1 minute
+        if not heap.empty():
+            next = min(next, heap.top().date-datetime.now()-gap)
+        if next.total_seconds() > 0:
+            print("Sleeping " + str(next))
+            sleep(next.total_seconds())
+
+        # next event
+        if not heap.empty() and heap.top().date-datetime.now() < gap:
+            event = heap.pop()
+            print(event.description + " at " + event.location + ", " + str(event.date))
 
 if __name__ == "__main__":
     main()