def subscribe(self): logger.info("subscribe") msg = OSCMessage("/subscribe") msg.appendTypedArg(self.args.client_host, "s") msg.appendTypedArg(self.args.client_port, "i") msg.appendTypedArg(self.args.authenticate, "s") if self.args.subscriber_label is not None: msg.appendTypedArg(self.args.subscriber_label, "s") self.osc_sock.writeDatagram(QByteArray(msg.encode_osc()), QHostAddress(self.args.chaosc_host), self.args.chaosc_port)
def subscribe_me(self): logger.info("%s: subscribing to '%s:%d' with label %r", datetime.now().strftime("%x %X"), self.chaosc_address[0], self.chaosc_address[1], self.args.subscriber_label) msg = OSCMessage("/subscribe") msg.appendTypedArg(self.client_address[0], "s") msg.appendTypedArg(self.client_address[1], "i") msg.appendTypedArg(self.args.authenticate, "s") if self.args.subscriber_label is not None: msg.appendTypedArg(self.args.subscriber_label, "s") self.osc_sock.sendto(msg.encode_osc(), self.chaosc_address)
def test_toggle_night_view(args, cam_id, sock): for i in ("on", "off"): message = OSCMessage("/toggleNightView") message.appendTypedArg(cam_id, "i") message.appendTypedArg(i, "s") binary = message.encode_osc() send(sock, binary, args)
def test_focus_cam(args, cam_id, sock): for i in (1, 5000, 9999): message = OSCMessage("/focusCam") message.appendTypedArg(cam_id, "i") message.appendTypedArg(i, "i") binary = message.encode_osc() send(sock, binary, args)
def test_use_cam_preset(args, cam_id, sock): for i in range(10): message = OSCMessage("/useCamPreset") message.appendTypedArg(cam_id, "i") message.appendTypedArg(i, "i") binary = message.encode_osc() send(sock, binary, args)
def handleConnResult(self, connection, *args, **kwargs): conn_result = connection.getresponse() if "client_address" in kwargs: response = OSCMessage("/Response") response.appendTypedArg(kwargs["cmd"], "s") for arg in args: response.append(arg) response.appendTypedArg(conn_result.status, "i") response.appendTypedArg(conn_result.reason, "s") self.socket.sendto(response.encode_osc(), kwargs["client_address"]) if conn_result.status != 200: print "%s. Error: %d, %s" % (datetime.now().strftime("%x %X"), conn_result.status, conn_result.reason)
def test_move_cam(args, cam_id, sock): directions = ("home", "up", "down", "left", "right", "upleft", "upright", "downleft", "downright") for direction in directions: message = OSCMessage("/moveCam") message.appendTypedArg(cam_id, "i") message.appendTypedArg(direction, "s") binary = message.encode_osc() send(sock, binary, args)
def unsubscribe_me(self): if self.args.keep_subscribed: return logger.info("%s: unsubscribing from '%s:%d'", datetime.now().strftime("%x %X"), self.chaosc_address[0], self.chaosc_address[1]) msg = OSCMessage("/unsubscribe") msg.appendTypedArg(self.client_address[0], "s") msg.appendTypedArg(self.client_address[1], "i") msg.appendTypedArg(self.args.authenticate, "s") self.osc_sock.sendto(msg.encode_osc(), self.chaosc_address)
def test_osc_message(self): msg = CMessage("/my/osc/address") msg.appendTypedArg(4, "i") msg.appendTypedArg("foo", "s") msg2 = CMessage("/other/address") msg.appendTypedArg(4., "f") bundle = CBundle() bundle.append(msg) bundle.append(msg2) binary = bundle.encode_osc() address, typetags, args = c_decode_osc(binary, 0, len(binary)) self.assertEqual(typetags, bundle.timetag) self.assertEqual(list(args), [ ('/my/osc/address', ['i', 's', 'f'], [4, 'foo', 4.0]), ('/other/address', [], [])] )
def handle_read(self, osc_sock): t = ord(self.serial.read(1)) osc_message = OSCMessage("/%s/ekg" % self.actor) osc_message.appendTypedArg(t, "i") osc_sock.sendall(osc_message.encode_osc())
def handle_read(self, osc_sock): data = self.serial.readline()[:-2] try: airFlow, emg, temp = data.split(";") except ValueError: return try: airFlow = int(airFlow) emg = int(emg) temp = int(temp) except ValueError: return osc_message = OSCMessage("/%s/airFlow" % self.actor) osc_message.appendTypedArg(airFlow, "i") osc_sock.sendall(osc_message.encode_osc()) osc_message = OSCMessage("/%s/emg" % self.actor) osc_message.appendTypedArg(emg, "i") osc_sock.sendall(osc_message.encode_osc()) osc_message = OSCMessage("/%s/temperature" % self.actor) osc_message.appendTypedArg(temp, "i") osc_sock.sendall(osc_message.encode_osc())
def main(): parser = argparse.ArgumentParser(prog='psychose_actor') parser.add_argument("-H", '--chaosc_host', required=True, type=str, help='host of chaosc instance to control') parser.add_argument("-p", '--chaosc_port', required=True, type=int, help='port of chaosc instance to control') args = parser.parse_args(sys.argv[1:]) osc_sock = socket.socket(2, 2, 17) osc_sock.connect((args.chaosc_host, args.chaosc_port)) naming = { "/dev/ttyUSB0": ["bjoern", "ehealth"], "/dev/ttyACM0": ["bjoern", "ekg"], "/dev/ttyACM1": ["bjoern", "pulse"], "/dev/ttyUSB1": ["merle", "ehealth"], "/dev/ttyACM2": ["merle", "ekg"], "/dev/ttyACM3": ["merle", "pulse"], "/dev/ttyUSB2": ["uwe", "ehealth"], "/dev/ttyACM4": ["uwe", "ekg"], "/dev/ttyACM5": ["uwe", "pulse"] } used_devices = dict() while 1: for device, description in naming.iteritems(): if os.path.exists(device): if device not in used_devices: actor, platform = naming[device] if description[1] == "ehealth": print device, actor, platform used_devices[device] = EHealth2OSC( actor, platform, device) elif description[1] == "ekg": print device, actor, platform used_devices[device] = EKG2OSC(actor, platform, device) elif description[1] == "pulse": print device, actor, platform used_devices[device] = Pulse2OSC( actor, platform, device) else: raise ValueError( "unknown description %r for device %r" % (description, device)) else: print "device missing", device message = OSCMessage("/DeviceMissing") message.appendTypedArg(description[0], "s") message.appendTypedArg(description[1], "s") osc_sock.sendall(message.encode_osc()) read_map = {} for forwarder in used_devices.values(): read_map[forwarder.serial] = forwarder.handle_read readers, writers, errors = select.select(read_map, [], [], 0.1) for reader in readers: read_map[reader](osc_sock)
def handle_read(self, osc_sock): t = ord(self.serial.read(1)) self.buf.append(t) if t == 0: try: heart_signal, heart_rate, o2, pulse = self.buf.getData() if pulse == 245 and not self.heartbeat_on: osc_message = OSCMessage("/%s/heartbeat" % self.actor) osc_message.appendTypedArg(1, "i") osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(o2, "i") osc_sock.sendall(osc_message.encode_osc()) #print "heartbeat", datetime.datetime.now(), heart_signal self.heartbeat_on = True elif pulse == 1 and self.heartbeat_on: #print "off heartbeat", datetime.datetime.now(), heart_signal self.heartbeat_on = False osc_message = OSCMessage("/%s/heartbeat" % self.actor) osc_message.appendTypedArg(0, "i") osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(o2, "i") osc_sock.sendall(osc_message.encode_osc()) except ValueError, e: print e
def test_osc_message(self): msg = CMessage("/my/osc/address") msg.append('something') msg.insert(0, 'something else') msg[1] = 'entirely' msg.extend([1,2,3.]) msg.appendTypedArg(4, "i") msg.appendTypedArg(5, "i") msg.append(6.) del msg[3:6] self.assertEqual(msg.pop(-2), 5) self.assertEqual(msg.__str__(), "/my/osc/address ['something else', 'entirely', 1, 6.0]") binary = msg.encode_osc() self.assertEqual(binary, '/my/osc/address\x00,ssif\x00\x00\x00something else\x00\x00entirely\x00\x00\x00\x00\x00\x00\x00\x01@\xc0\x00\x00') msg2 = c_decode_osc(binary, 0, len(binary)) self.assertEqual(msg2.__repr__(), "('/my/osc/address', ['s', 's', 'i', 'f'], ['something else', 'entirely', 1, 6.0])")
def test_set_cam_preset(args, cam_id, sock): message = OSCMessage("/setCamPreset") message.appendTypedArg(cam_id, "i") message.appendTypedArg(0, "i") binary = message.encode_osc() send(sock, binary, args)
def __init__(self, args): SimpleOSCServer.__init__(self, args) self.addMsgHandler("X", self.stats_handler) if "unsubscribe" == args.subparser_name: msg = OSCMessage("/unsubscribe") msg.appendTypedArg(args.host, "s") msg.appendTypedArg(args.port, "i") msg.appendTypedArg(args.authenticate, "s") self.sendto(msg, self.chaosc_address) logger.info("unsubscribe %r:%r from %r:%r", args.host, args.port, args.chaosc_host, args.chaosc_port) elif "subscribe" == args.subparser_name: msg = OSCMessage("/subscribe") msg.appendTypedArg(args.host, "s") msg.appendTypedArg(args.port, "i") msg.appendTypedArg(args.authenticate, "s") if args.subscriber_label: msg.appendTypedArg(args.subscriber_label, "s") self.sendto(msg, self.chaosc_address) logger.info("subscribe %r:%r to %r:%r", args.host, args.port, args.chaosc_host, args.chaosc_port) elif "list" == args.subparser_name: msg = OSCMessage("/list") msg.appendTypedArg(args.client_host, "s") msg.appendTypedArg(args.client_port, "i") self.sendto(msg, self.chaosc_address) elif "save" == args.subparser_name: msg = OSCMessage("/save") msg.appendTypedArg(args.authenticate, "s") self.sendto(msg, self.chaosc_address) elif "pause" == args.subparser_name: msg = OSCMessage("/pause") msg.appendTypedArg(args.pause_state, "i") self.sendto(msg, self.chaosc_address) else: raise Exception("unknown command") sys.exit(1)