def __init__(self, name, server='127.0.0.1', port=9000):
        self.server = server
        self.port = port
        self.timestamp = 0
        self.client_name = 'booth-7'

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]


        print 'about to connect to %s' % self.server
        self.ws = create_connection("ws://%s:%s" % (self.server, self.port))

        print 'connected to server'
        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            #self.brew.add_subscriber(spacebrew_name, "string")
            #self.brew.subscribe(spacebrew_name, self.handle_value)

        #self.brew.add_publisher("eeg_ecg","string")
        #self.brew.add_publisher("instruction","string")
        self.brew.add_subscriber("alpha_absolute","string")
        self.brew.add_subscriber(eeg_connect_string,"string")
        self.brew.add_subscriber(eeg_disconnect_string,"string")
    def __init__(self, name, server):
        # get app name and server from query string
        self.name = name
        server = server

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)

        # cassandra
        self.muse_cassandra_repo = CassandraRepository(KEYSPACE, MUSE_COLUMN_FAMILY)
        self.batches = {}
        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]

        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            self.brew.add_subscriber(spacebrew_name, "string")
            self.brew.subscribe(spacebrew_name, self.handle_value)
    def __init__(self, muse_port, muse_id, server):
        # Configuring the Muse OSC client
        ServerThread.__init__(self, muse_port)

        # configure the Spacebrew client
        self.brew = SpacebrewApp(muse_id, server=server)

        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]

        self.paths = [path['address'] for path in self.osc_paths]

        for path in self.paths:
            spacebrew_name = path.split('/')[-1]
            self.brew.add_publisher(spacebrew_name, "string")

        # connect to spacebrew
        self.brew.start()
class SpacebrewClient(object):
    def __init__(self, name, server):
        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]

        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            self.brew.add_subscriber(spacebrew_name, "string")
            self.brew.subscribe(spacebrew_name, self.handle_value)

    def handle_value(self, string_value):

        value = json.loads(string_value)

        # YOUR CODE HERE


    def start(self):
        self.brew.start()
class SpacebrewClient(object):

    def __init__(self, name, server):
        # get app name and server from query string
        self.name = name
        server = server

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)

        # cassandra
        self.muse_cassandra_repo = CassandraRepository(KEYSPACE, MUSE_COLUMN_FAMILY)
        self.batches = {}
        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]

        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            self.brew.add_subscriber(spacebrew_name, "string")
            self.brew.subscribe(spacebrew_name, self.handle_value)

    def handle_value(self, string_value):
        value = json.loads(string_value)
        path = value[0]
        if path in self.paths:
            row_key, column = convert_muse_data_to_cassandra_column(path, value)

            if len(self.batches[path]) < BATCH_MAX_SIZE:
                self.batches[path][row_key] = column

                # time stats
                timestamp = row_key.split('_')[1]
                print "column added to batch for %s -- %s ms" % (path, timestamp)

            elif len(self.batches[path]) == BATCH_MAX_SIZE:
                self.muse_cassandra_repo.add_batch(self.batches[path])
                self.batches[path] = {}

                # time stats
                timestamp = row_key.split('_')[1]
                print "column batch stored in cassandra for %s -- %s ms" % (path, timestamp)

    def start(self):
        self.brew.start()
class SpacebrewClient(object):
    def __init__(self, name, server='127.0.0.1', port=9000):
        self.server = server
        self.port = port
        self.timestamp = 0
        self.client_name = 'booth-7'

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]


        print 'about to connect to %s' % self.server
        self.ws = create_connection("ws://%s:%s" % (self.server, self.port))

        print 'connected to server'
        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            #self.brew.add_subscriber(spacebrew_name, "string")
            #self.brew.subscribe(spacebrew_name, self.handle_value)

        #self.brew.add_publisher("eeg_ecg","string")
        #self.brew.add_publisher("instruction","string")
        self.brew.add_subscriber("alpha_absolute","string")
        self.brew.add_subscriber(eeg_connect_string,"string")
        self.brew.add_subscriber(eeg_disconnect_string,"string")

    def set_handle_value(self,metric,handler):
        self.brew.subscribe(metric,handler)


    def handle_value(self, string_value):

        print "received string: %s" % string_value
        value = string_value.split(',')
        path = value[0]
        timestamp = value[5]

        '''if (int(timestamp) % 5 == 0):
            instruction = {"message": {
                "value" : {"instruction_name": "DISPLAY_INSTRUCTION", "instruction_text": "testing 1 2 3 yeah"},
                "type": "string", "name": "instruction", "clientName": self.client_name}}

            sb_server_2.ws.send(json.dumps(instruction))'''

        #print "path: %s" % path

        if path == "alpha_absolute":
            self.timestamp+=1 #should start incrementing (internal) timestamps after we've acquired signal from both EEG and ECG
            ecg = random.random();
            value_out = [timestamp] + [(float(value[2])+float(value[3]))/2] + [ecg]
            message = {"message": { #send synced EEG & ECG data here
                "value": value_out,
                "type": "string", "name": "eeg_ecg", "clientName": self.client_name}}
            instruction = {"message": {
                "value" : "BASELINE_INSTRUCTIONS",
                "type": "string", "name": "instruction", "clientName": self.client_name}}


            sb_server_2.ws.send(json.dumps(message))
            #sb_server.ws.send(json.dumps(instruction))

    def start(self):
        self.brew.start()
Example #7
0
    def __init__(self, muse_port, muse_id, server):
        # Configuring the Muse OSC client
        ServerThread.__init__(self, muse_port)

        # configure the Spacebrew client
        self.brew = SpacebrewApp(muse_id, server=server)

        self.osc_paths = [{
            'address': "/muse/eeg",
            'arguments': 4
        }, {
            'address': "/muse/eeg/quantization",
            'arguments': 4
        }, {
            'address': "/muse/eeg/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/acc",
            'arguments': 3
        }, {
            'address': "/muse/acc/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/batt",
            'arguments': 4
        }, {
            'address': "/muse/drlref",
            'arguments': 2
        }, {
            'address': "/muse/elements/low_freqs_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/touching_forehead",
            'arguments': 1
        }, {
            'address': "/muse/elements/horseshoe",
            'arguments': 4
        }, {
            'address': "/muse/elements/is_good",
            'arguments': 4
        }, {
            'address': "/muse/elements/blink",
            'arguments': 1
        }, {
            'address': "/muse/elements/jaw_clench",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/concentration",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/mellow",
            'arguments': 1
        }]

        self.paths = [path['address'] for path in self.osc_paths]

        for path in self.paths:
            spacebrew_name = path.split('/')[-1]
            self.brew.add_publisher(spacebrew_name, "string")

        # connect to spacebrew
        self.brew.start()
Example #8
0
class SpacebrewServer(ServerThread):
    def __init__(self, muse_port, muse_id, server):
        # Configuring the Muse OSC client
        ServerThread.__init__(self, muse_port)

        # configure the Spacebrew client
        self.brew = SpacebrewApp(muse_id, server=server)

        self.osc_paths = [{
            'address': "/muse/eeg",
            'arguments': 4
        }, {
            'address': "/muse/eeg/quantization",
            'arguments': 4
        }, {
            'address': "/muse/eeg/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/acc",
            'arguments': 3
        }, {
            'address': "/muse/acc/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/batt",
            'arguments': 4
        }, {
            'address': "/muse/drlref",
            'arguments': 2
        }, {
            'address': "/muse/elements/low_freqs_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/touching_forehead",
            'arguments': 1
        }, {
            'address': "/muse/elements/horseshoe",
            'arguments': 4
        }, {
            'address': "/muse/elements/is_good",
            'arguments': 4
        }, {
            'address': "/muse/elements/blink",
            'arguments': 1
        }, {
            'address': "/muse/elements/jaw_clench",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/concentration",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/mellow",
            'arguments': 1
        }]

        self.paths = [path['address'] for path in self.osc_paths]

        for path in self.paths:
            spacebrew_name = path.split('/')[-1]
            self.brew.add_publisher(spacebrew_name, "string")

        # connect to spacebrew
        self.brew.start()

    def close(self):
        self.brew.stop()
        self.stop()

    # handle all messages if the OSC path is in self.paths
    @make_method(None, None)
    def fallback(self, path, args, types, src):

        if path in self.paths:
            data = [path] + args

            sb_name = path.split('/')[-1]
            self.brew.publish(sb_name, json.dumps(data))
class SpacebrewServer(ServerThread):
    def __init__(self, muse_port, muse_id, server):
        # Configuring the Muse OSC client
        ServerThread.__init__(self, muse_port)

        # configure the Spacebrew client
        self.brew = SpacebrewApp(muse_id, server=server)

        self.osc_paths = [
            {'address': "/muse/eeg", 'arguments': 4},
            {'address': "/muse/eeg/quantization", 'arguments': 4},
            {'address': "/muse/eeg/dropped_samples", 'arguments': 1},
            {'address': "/muse/acc", 'arguments': 3},
            {'address': "/muse/acc/dropped_samples", 'arguments': 1},
            {'address': "/muse/batt", 'arguments': 4},
            {'address': "/muse/drlref", 'arguments': 2},
            {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_absolute", 'arguments': 4},
            {'address': "/muse/elements/theta_absolute", 'arguments': 4},
            {'address': "/muse/elements/alpha_absolute", 'arguments': 4},
            {'address': "/muse/elements/beta_absolute", 'arguments': 4},
            {'address': "/muse/elements/gamma_absolute", 'arguments': 4},
            {'address': "/muse/elements/delta_relative", 'arguments': 4},
            {'address': "/muse/elements/theta_relative", 'arguments': 4},
            {'address': "/muse/elements/alpha_relative", 'arguments': 4},
            {'address': "/muse/elements/beta_relative", 'arguments': 4},
            {'address': "/muse/elements/gamma_relative", 'arguments': 4},
            {'address': "/muse/elements/delta_session_score", 'arguments': 4},
            {'address': "/muse/elements/theta_session_score", 'arguments': 4},
            {'address': "/muse/elements/alpha_session_score", 'arguments': 4},
            {'address': "/muse/elements/beta_session_score", 'arguments': 4},
            {'address': "/muse/elements/gamma_session_score", 'arguments': 4},
            {'address': "/muse/elements/touching_forehead", 'arguments': 1},
            {'address': "/muse/elements/horseshoe", 'arguments': 4},
            {'address': "/muse/elements/is_good", 'arguments': 4},
            {'address': "/muse/elements/blink", 'arguments': 1},
            {'address': "/muse/elements/jaw_clench", 'arguments': 1},
            {'address': "/muse/elements/experimental/concentration", 'arguments': 1},
            {'address': "/muse/elements/experimental/mellow", 'arguments': 1}
        ]

        self.paths = [path['address'] for path in self.osc_paths]

        for path in self.paths:
            spacebrew_name = path.split('/')[-1]
            self.brew.add_publisher(spacebrew_name, "string")

        # connect to spacebrew
        self.brew.start()


    def close(self):
        self.brew.stop()
        self.stop()


    # handle all messages if the OSC path is in self.paths
    @make_method(None, None)
    def fallback(self, path, args, types, src):

        if path in self.paths:
            data = [path] + args

            sb_name = path.split('/')[-1]
            self.brew.publish(sb_name, json.dumps(data))
Example #10
0
    def __init__(self, name, server):
        # get app name and server from query string
        self.name = name
        server = server

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)

        # cassandra
        self.muse_cassandra_repo = CassandraRepository(KEYSPACE,
                                                       MUSE_COLUMN_FAMILY)
        self.batches = {}
        self.osc_paths = [{
            'address': "/muse/eeg",
            'arguments': 4
        }, {
            'address': "/muse/eeg/quantization",
            'arguments': 4
        }, {
            'address': "/muse/eeg/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/acc",
            'arguments': 3
        }, {
            'address': "/muse/acc/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/batt",
            'arguments': 4
        }, {
            'address': "/muse/drlref",
            'arguments': 2
        }, {
            'address': "/muse/elements/low_freqs_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/touching_forehead",
            'arguments': 1
        }, {
            'address': "/muse/elements/horseshoe",
            'arguments': 4
        }, {
            'address': "/muse/elements/is_good",
            'arguments': 4
        }, {
            'address': "/muse/elements/blink",
            'arguments': 1
        }, {
            'address': "/muse/elements/jaw_clench",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/concentration",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/mellow",
            'arguments': 1
        }]

        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            self.brew.add_subscriber(spacebrew_name, "string")
            self.brew.subscribe(spacebrew_name, self.handle_value)
Example #11
0
class SpacebrewClient(object):
    def __init__(self, name, server):
        # get app name and server from query string
        self.name = name
        server = server

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)

        # cassandra
        self.muse_cassandra_repo = CassandraRepository(KEYSPACE,
                                                       MUSE_COLUMN_FAMILY)
        self.batches = {}
        self.osc_paths = [{
            'address': "/muse/eeg",
            'arguments': 4
        }, {
            'address': "/muse/eeg/quantization",
            'arguments': 4
        }, {
            'address': "/muse/eeg/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/acc",
            'arguments': 3
        }, {
            'address': "/muse/acc/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/batt",
            'arguments': 4
        }, {
            'address': "/muse/drlref",
            'arguments': 2
        }, {
            'address': "/muse/elements/low_freqs_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/touching_forehead",
            'arguments': 1
        }, {
            'address': "/muse/elements/horseshoe",
            'arguments': 4
        }, {
            'address': "/muse/elements/is_good",
            'arguments': 4
        }, {
            'address': "/muse/elements/blink",
            'arguments': 1
        }, {
            'address': "/muse/elements/jaw_clench",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/concentration",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/mellow",
            'arguments': 1
        }]

        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            self.brew.add_subscriber(spacebrew_name, "string")
            self.brew.subscribe(spacebrew_name, self.handle_value)

    def handle_value(self, string_value):
        value = json.loads(string_value)
        path = value[0]
        if path in self.paths:
            row_key, column = convert_muse_data_to_cassandra_column(
                path, value)

            if len(self.batches[path]) < BATCH_MAX_SIZE:
                self.batches[path][row_key] = column

                # time stats
                timestamp = row_key.split('_')[1]
                print "column added to batch for %s -- %s ms" % (path,
                                                                 timestamp)

            elif len(self.batches[path]) == BATCH_MAX_SIZE:
                self.muse_cassandra_repo.add_batch(self.batches[path])
                self.batches[path] = {}

                # time stats
                timestamp = row_key.split('_')[1]
                print "column batch stored in cassandra for %s -- %s ms" % (
                    path, timestamp)

    def start(self):
        self.brew.start()
    def __init__(self, name, server='127.0.0.1', port=9000):
        self.server = server
        self.port = port
        self.timestamp = 0
        self.client_name = 'booth-7'

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        self.osc_paths = [{
            'address': "/muse/eeg",
            'arguments': 4
        }, {
            'address': "/muse/eeg/quantization",
            'arguments': 4
        }, {
            'address': "/muse/eeg/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/acc",
            'arguments': 3
        }, {
            'address': "/muse/acc/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/batt",
            'arguments': 4
        }, {
            'address': "/muse/drlref",
            'arguments': 2
        }, {
            'address': "/muse/elements/low_freqs_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/touching_forehead",
            'arguments': 1
        }, {
            'address': "/muse/elements/horseshoe",
            'arguments': 4
        }, {
            'address': "/muse/elements/is_good",
            'arguments': 4
        }, {
            'address': "/muse/elements/blink",
            'arguments': 1
        }, {
            'address': "/muse/elements/jaw_clench",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/concentration",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/mellow",
            'arguments': 1
        }]

        print 'about to connect to %s' % self.server
        self.ws = create_connection("ws://%s:%s" % (self.server, self.port))

        print 'connected to server'
        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            #self.brew.add_subscriber(spacebrew_name, "string")
            #self.brew.subscribe(spacebrew_name, self.handle_value)

        #self.brew.add_publisher("eeg_ecg","string")
        #self.brew.add_publisher("instruction","string")
        self.brew.add_subscriber("alpha_absolute", "string")
        self.brew.add_subscriber(eeg_connect_string, "string")
        self.brew.add_subscriber(eeg_disconnect_string, "string")
class SpacebrewClient(object):
    def __init__(self, name, server='127.0.0.1', port=9000):
        self.server = server
        self.port = port
        self.timestamp = 0
        self.client_name = 'booth-7'

        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        self.osc_paths = [{
            'address': "/muse/eeg",
            'arguments': 4
        }, {
            'address': "/muse/eeg/quantization",
            'arguments': 4
        }, {
            'address': "/muse/eeg/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/acc",
            'arguments': 3
        }, {
            'address': "/muse/acc/dropped_samples",
            'arguments': 1
        }, {
            'address': "/muse/batt",
            'arguments': 4
        }, {
            'address': "/muse/drlref",
            'arguments': 2
        }, {
            'address': "/muse/elements/low_freqs_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_absolute",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_relative",
            'arguments': 4
        }, {
            'address': "/muse/elements/delta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/theta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/alpha_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/beta_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/gamma_session_score",
            'arguments': 4
        }, {
            'address': "/muse/elements/touching_forehead",
            'arguments': 1
        }, {
            'address': "/muse/elements/horseshoe",
            'arguments': 4
        }, {
            'address': "/muse/elements/is_good",
            'arguments': 4
        }, {
            'address': "/muse/elements/blink",
            'arguments': 1
        }, {
            'address': "/muse/elements/jaw_clench",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/concentration",
            'arguments': 1
        }, {
            'address': "/muse/elements/experimental/mellow",
            'arguments': 1
        }]

        print 'about to connect to %s' % self.server
        self.ws = create_connection("ws://%s:%s" % (self.server, self.port))

        print 'connected to server'
        for path in self.osc_paths:
            spacebrew_name = path['address'].split('/')[-1]
            #self.brew.add_subscriber(spacebrew_name, "string")
            #self.brew.subscribe(spacebrew_name, self.handle_value)

        #self.brew.add_publisher("eeg_ecg","string")
        #self.brew.add_publisher("instruction","string")
        self.brew.add_subscriber("alpha_absolute", "string")
        self.brew.add_subscriber(eeg_connect_string, "string")
        self.brew.add_subscriber(eeg_disconnect_string, "string")

    def set_handle_value(self, metric, handler):
        self.brew.subscribe(metric, handler)

    def handle_value(self, string_value):

        print "received string: %s" % string_value
        value = string_value.split(',')
        path = value[0]
        timestamp = value[5]
        '''if (int(timestamp) % 5 == 0):
            instruction = {"message": {
                "value" : {"instruction_name": "DISPLAY_INSTRUCTION", "instruction_text": "testing 1 2 3 yeah"},
                "type": "string", "name": "instruction", "clientName": self.client_name}}

            sb_server_2.ws.send(json.dumps(instruction))'''

        #print "path: %s" % path

        if path == "alpha_absolute":
            self.timestamp += 1  #should start incrementing (internal) timestamps after we've acquired signal from both EEG and ECG
            ecg = random.random()
            value_out = [timestamp] + [(float(value[2]) + float(value[3])) / 2
                                       ] + [ecg]
            message = {
                "message": {  #send synced EEG & ECG data here
                    "value": value_out,
                    "type": "string",
                    "name": "eeg_ecg",
                    "clientName": self.client_name
                }
            }
            instruction = {
                "message": {
                    "value": "BASELINE_INSTRUCTIONS",
                    "type": "string",
                    "name": "instruction",
                    "clientName": self.client_name
                }
            }

            sb_server_2.ws.send(json.dumps(message))
            #sb_server.ws.send(json.dumps(instruction))

    def start(self):
        self.brew.start()