class SpacebrewServer(object): def __init__(self, muse_id=7777, server='127.0.0.1', port=9000): self.muse_id = muse_id self.server = server self.port = port name = 'muse-%s' % muse_id self.brew = SpacebrewApp(name, server=server) for path in CASSANDRA_METRICS: publisher_metric_name = calculate_spacebrew_name(path) self.brew.add_publisher(publisher_metric_name, "string", '0') print publisher_metric_name def start(self): self.brew.start() while 1: for path in CASSANDRA_METRICS: spacebrew_name = calculate_spacebrew_name(path) num_arguments = CASSANDRA_METRICS[path] values = [0.1] * num_arguments #string value value = ','.join([str(v) for v in values]) self.brew.publish(spacebrew_name, value) print spacebrew_name, value time.sleep(0.1) def stop(self): self.brew.stop()
class SpacebrewServer(ServerThread): def __init__(self, muse_port, muse_id, server): self.muse_port = muse_port self.muse_id = muse_id # Configuring the Muse OSC client ServerThread.__init__(self, muse_port) logger.info('Muse %s connected to museIO on port %s' % (muse_id, muse_port)) # configure the Spacebrew client self.brew = SpacebrewApp(muse_id, server=server) self.osc_paths = [ '/muse/eeg', '/muse/eeg/quantization', # '/muse/eeg/dropped_samples', '/muse/acc', # '/muse/acc/dropped_samples', '/muse/batt', # '/muse/drlref', #'/muse/elements/raw_fft0', #'/muse/elements/raw_fft1', #'/muse/elements/raw_fft2', #'/muse/elements/raw_fft3', '/muse/elements/low_freqs_absolute', '/muse/elements/delta_absolute', '/muse/elements/theta_absolute', '/muse/elements/alpha_absolute', '/muse/elements/beta_absolute', '/muse/elements/gamma_absolute', #'/muse/elements/delta_relative', #'/muse/elements/theta_relative', #'/muse/elements/alpha_relative', #'/muse/elements/beta_relative', #'/muse/elements/gamma_relative', #'/muse/elements/delta_session_score', #'/muse/elements/theta_session_score', #'/muse/elements/alpha_session_score', #'/muse/elements/beta_session_score', #'/muse/elements/gamma_session_score', '/muse/elements/touching_forehead', '/muse/elements/horseshoe', '/muse/elements/is_good', '/muse/elements/blink', '/muse/elements/jaw_clench', '/muse/elements/experimental/concentration', '/muse/elements/experimental/mellow' ] for osc_path in self.osc_paths: spacebrew_name = self.calculate_spacebrew_name(osc_path) self.brew.add_publisher(spacebrew_name, 'string') logger.debug('Spacebrew publisher %s added for muse with ID %s' % (spacebrew_name, self.muse_id)) # Connect to spacebrew self.brew.start() logger.debug('Initialization completed for muse with ID %s' % self.muse_id) def close(self): self.brew.stop() self.stop() logger.info('Connector stopped for muse with ID %s' % self.muse_id) # Handle all messages if the OSC path is in self.osc_paths @make_method(None, None) def fallback(self, path, args, types, src): if path in self.osc_paths: spacebrew_name = self.calculate_spacebrew_name(path) value = ','.join([str(arg) for arg in args]) self.brew.publish(spacebrew_name, value) # logger.debug('Muse with ID %s is publishing to spacebrew. Publisher name: %s. Data: %s' (self.muse_id, spacebrew_name, value)) def calculate_spacebrew_name(self, osc_path): spacebrew_name = osc_path.split('/')[-1] return self.disambiguate_name_collisions(spacebrew_name, osc_path) def disambiguate_name_collisions(self, spacebrew_name, osc_path): if spacebrew_name == 'dropped_samples': return osc_path.split('/')[-2] + '_' + osc_path.split('/')[-1] else: return spacebrew_name