或者更好的是,提取内容并防止硬编码:
# this is put into a config file for easy mod'ing
OSCPATH = {
# Incoming OSC from the tracking subsys
'ping': "/ping",
'start': "/pf/started",
'entry': "/pf/entry",
'exit': "/pf/exit",
'update': "/pf/update",
'frame': "/pf/frame",
'stop': "/pf/stopped",
'set': "/pf/set/",
'minx': "/pf/set/minx",
'maxx': "/pf/set/maxx",
'miny': "/pf/set/miny",
'maxy': "/pf/set/maxy",
'npeople': "/pf/set/npeople",
# Outgoing OSC updates from the conductor
'attribute': "/conducter/attribute",
'rollcall': "/conducter/rollcall",
'event': "/conducter/event",
'conx': "/conducter/conx",
}
class OSCHandler(object):
"""Set up OSC server and other handlers."""
def __init__(self, field):
self.m_server = OSCServer( (OSCHOST, OSCPORT) )
self.EVENTFUNC = {
'ping': self.event_tracking_ping,
'start': self.event_tracking_start,
'stop': self.event_tracking_stop,
'entry': self.event_tracking_entry,
'exit': self.event_tracking_exit,
'update': self.event_tracking_update,
'frame': self.event_tracking_frame,
'minx': self.event_tracking_set,
'miny': self.event_tracking_set,
'maxx': self.event_tracking_set,
'maxy': self.event_tracking_set,
'npeople': self.event_tracking_set,
}
for i in self.EVENTFUNC:
self.m_server.addMsgHandler(OSCPATH[i], self.EVENTFUNC[i])
您会看到包括 minx、miny 等在内的多个路径映射到同一个函数。这些使用路径参数来采取特定的行动来处理这些数据。