Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
84 lines (69 sloc) 3.29 KB
import time
import websockets
from threading import Thread
from pythonosc import osc_message_builder
from pythonosc import udp_client
from wsserver import WSServer
class Router():
def __init__(self):
self.osc_client = None
self.ws_server = None
self.file_handle = None
self.osc_prefix = None
self.osc_address = None
self.filename = None
def digital_out():
pass
self.digital_out_func = digital_out
def init_destinations(self, connections):
destinations = []
for connection in connections.values():
destinations += connection
if 'osc' in destinations:
print('connecting osc to ' + self.osc_address)
address_port = self.osc_address.split(':')
self.osc_client = udp_client.SimpleUDPClient(address_port[0], int(address_port[1]))
if 'file' in destinations:
self.file_handle = open(self.filename, 'w')
print("initializing file " + self.filename)
if 'ws' in destinations:
self.ws_server = WSServer()
def route_data(self, source, connections, features, raw_data):
source_connections = []
if source in connections:
source_connections += connections[source]
current_time = time.time()
# raw data
if 'r' + source in connections:
source_connections += connections['r' + source]
for destination in connections['r' + source]:
osc_dest = '/{}/raw'.format(source)
if destination == 'osc':
for i, channel in enumerate(raw_data):
for data in channel:
osc_address = self.osc_prefix + osc_dest
if i > 0:
osc_address += str(i)
self.osc_client.send_message(osc_address, data)
elif destination == 'file':
self.file_handle.write('{} {}: {}\n'.format(current_time, osc_dest, str(raw_data)))
elif destination == 'ws':
self.ws_server.add_message('{}|{}/{}'.format(current_time, osc_dest, str(raw_data)))
# features
if features:
for destination in source_connections:
for i, features_chan in enumerate(features):
for feature in features_chan.keys():
osc_dest = '/{}/{}'.format(source, feature)
if i > 0:
osc_dest += str(i)
for data in features_chan[feature]:
if destination == 'osc':
self.osc_client.send_message(self.osc_prefix + osc_dest, data)
elif destination == 'file':
self.file_handle.write('{} {}: {}\n'.format(current_time, osc_dest, str(data)))
elif destination == 'ws':
self.ws_server.add_message('{}|{}/{}'.format(current_time, osc_dest, str(raw_data)))
elif destination == 'digital':
if self.digital_out_func:
self.digital_out_func()
You can’t perform that action at this time.