-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdispatcher.py
More file actions
59 lines (52 loc) · 2.06 KB
/
dispatcher.py
File metadata and controls
59 lines (52 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*-coding:UTF-8 -*
from threading import Thread
import os
from tools.class_manager import class_loader
from missions.mission import Mission
from queue import Queue
from logging import getLogger
class Dispatcher(Thread):
def __init__(self, robot, comm):
Thread.__init__(self)
self.logger = getLogger("dispatcher")
self.robot = robot
self.queue = Queue()
self.comm = comm
self.comm.set_callback(self.add_event)
self.comm.set_dispatcher(self)
self.comm.init()
self.logger.info("Loading missions…")
self._load_all_missions('common')
self._load_all_missions(robot.name)
self.logger.info("All missions loaded !")
def _load_all_missions(self, prefix):
path = os.path.join(os.getcwd(), "missions", prefix)
missions = set(class_loader(path))
self.missions = []
for mission in missions:
if mission.__name__ != "Mission" and issubclass(mission, Mission):
m = mission()
m.robot = self.robot # proposition, on passe le robot en argument du constructeur
m.dispatcher = self # même proposition
m.post_init()
for channel in self.comm.channels:
setattr(m, channel, self.comm.channels[channel])
self.missions += [m]
self.logger.info("%s loaded" %mission.__name__)
def add_event(self, event):
self.queue.put(event, True, None)
def symmetrical(self, sym):
for channel in self.comm.channels:
channel._symmetrical = sym
def run(self):
while True:
event = self.queue.get(True, None)
if self.robot.inverted:
self.invert(event)
self.logger.info("Dispatch event %s(%s)" %(event.proto, event.name))
if event.dests == []:
for mission in self.missions:
mission.process_event(event)
else:
for mission in event.dests:
mission.process_event(event)