def test_Command(self): """Should support Commands with arguments.""" signal = bcixml.BciSignal(None, [['foo', dict()]], bcixml.INTERACTION_SIGNAL) xml = self.encoder.encode_packet(signal) signal2 = self.decoder.decode_packet(xml) self.assertEqual(signal.commands, signal2.commands)
def _handle_is(self, signal): """Handle Interaction Signal.""" self.logger.info("Got interaction signal: %s" % str(signal)) cmd = signal.commands[0][0] if len(signal.commands) > 0 else None # A few commands need to be handled by the Feedback Controller, the # rest goes to the Feedback if cmd == bcixml.CMD_GET_FEEDBACKS: reply = bcixml.BciSignal( {"feedbacks": self.fbProcCtrl.get_feedbacks()}, None, bcixml.REPLY_SIGNAL) reply.peeraddr = signal.peeraddr self.send_to_peer(reply) return elif cmd == bcixml.CMD_GET_VARIABLES: # Put it in the pipe and hope that the reply will appear on our end. self.send_to_feedback(signal) return elif cmd == bcixml.CMD_QUIT_FEEDBACK_CONTROLLER: self.stop() if cmd == bcixml.CMD_QUIT: self.send_to_feedback(signal) self.ipcchannel.close_channel() self.fbProcCtrl.stop_feedback() elif cmd == bcixml.CMD_SEND_INIT: name = signal.data["_feedback"] self.fbProcCtrl.start_feedback(name, port=self.ppport) else: self.send_to_feedback(signal)
def send_init(self, feedback): """Send 'send_init(feedback)' to Feedback Controller. :param feedback: Feedback """ signal = bcixml.BciSignal({"_feedback": str(feedback)}, [(bcixml.CMD_SEND_INIT, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def testControl_Signal(self): """Should support Control Signal type.""" data = {"foo": "bar", "baz": 3} signal = bcixml.BciSignal(data, None, bcixml.CONTROL_SIGNAL) xml = self.encoder.encode_packet(signal) signal2 = self.decoder.decode_packet(xml) self.assertEqual(signal2.type, signal.type) self.assertEqual(signal2.data, data)
def testInteraction_Signal(self): """Should support Interaction Signal type.""" data = {"foo": "bar", "baz": 3} signal = bcixml.BciSignal(data, None, bcixml.INTERACTION_SIGNAL) xml = self.encoder.encode_packet(signal) signal2 = self.decoder.decode_packet(xml) self.assertEqual(signal2.type, signal.type) self.assertEqual(signal2.data, data)
def save_configuration(self, filename): """Tell the Feedback to store it's variables to the given file. :param filename: Name of the file where to store the variables. :type filename: str """ signal = bcixml.BciSignal(None, [(bcixml.CMD_SAVE_VARIABLES, {'filename' : filename})], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def load_configuration(self, filename): """Tell the Feedback to load variable values from given file. :param filename: Name of the file where to load the variables from :type filename: str """ signal = bcixml.BciSignal(None, [(bcixml.CMD_LOAD_VARIABLES, {'filename' : filename})], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def __convert_and_compare(self, name, value, value2=None): signal = bcixml.BciSignal({name: value}, None, bcixml.INTERACTION_SIGNAL) xml = self.encoder.encode_packet(signal) signal2 = self.decoder.decode_packet(xml) if value2 == None: value2 = value self.assertTrue(signal2.data.has_key(name)) self.assertEqual(signal2.data[name], value2) self.assertEqual(type(signal2.data[name]), type(value2))
def makeSignal(self, modifiedOnly=False): """Create an Interaction Signal from the Variables in the Table.""" data = {} for elem in self.model.entry: if not modifiedOnly or (modifiedOnly and elem.modified): data[elem.name] = elem.value # FIXME: should elem.modified = False signal = bcixml.BciSignal(data, None, bcixml.INTERACTION_SIGNAL) return signal
def getAvailableFeedbacks(self): """Get available Feedbacks from Feedback Controller. :returns: Feedbacks """ signal = bcixml.BciSignal(None, [(bcixml.CMD_GET_FEEDBACKS, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal) data, addr = self.receive(TIMEOUT) if not data: self.logger.info("Did not receive an answer on getAvailableFeedbacks") return None answer = self.xmldecoder.decode_packet(data) return answer.data.get("feedbacks")
def get_variables(self): """Get variables (name, type and value) from currently running Feedback. :returns: variables """ signal = bcixml.BciSignal(None, [(bcixml.CMD_GET_VARIABLES, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal) data, addr = self.receive(TIMEOUT) if not data: self.logger.info("Did not receive answer on get_variables") return None answer = self.xmldecoder.decode_packet(data) return answer.data.get("variables")
def handle_message(self, message): """Handle message from Feedback Controller. :param message: Message """ self.feedback.logger.debug("Processing signal") if message.type == bcixml.CONTROL_SIGNAL: self.feedback._on_control_event(message.data) return cmd = message.commands[0][0] if len(message.commands) > 0 else None if cmd == bcixml.CMD_GET_VARIABLES: reply = bcixml.BciSignal( {"variables": self.feedback._get_variables()}, None, bcixml.REPLY_SIGNAL) reply.peeraddr = message.peeraddr self.feedback.logger.debug("Sending variables") self.send_message(reply) self.feedback._on_interaction_event(message.data) if cmd == bcixml.CMD_PLAY: self.feedback._playEvent.set() elif cmd == bcixml.CMD_PAUSE: self.feedback._on_pause() elif cmd == bcixml.CMD_STOP: self.feedback._on_stop() elif cmd == bcixml.CMD_QUIT: self.feedback._on_quit() elif cmd == bcixml.CMD_SEND_INIT: self.feedback._on_init() elif cmd == bcixml.CMD_SAVE_VARIABLES: filename = message.commands[0][1]['filename'] self.feedback.save_variables(filename) elif cmd == bcixml.CMD_LOAD_VARIABLES: filename = message.commands[0][1]['filename'] self.feedback.load_variables(filename)
def quit_feedback_controller(self): """Send 'quit_feedback_controller' to Feedback Controller.""" signal = bcixml.BciSignal(None, [(bcixml.CMD_QUIT_FEEDBACK_CONTROLLER, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def stop(self): """Send 'stop' to Feedback Controller.""" signal = bcixml.BciSignal(None, [(bcixml.CMD_STOP, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def test_Reply_Signal(self): """Should support Reply Signal type.""" signal = bcixml.BciSignal(None, None, bcixml.REPLY_SIGNAL) xml = self.encoder.encode_packet(signal) signal2 = self.decoder.decode_packet(xml) self.assertEqual(signal.type, signal2.type)
def play(self): """Send 'play' to Feedback Controller.""" signal = bcixml.BciSignal(None, [(bcixml.CMD_PLAY, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def quit(self): """Send 'quit' to Feedback Controller.""" signal = bcixml.BciSignal(None, [(bcixml.CMD_QUIT, dict())], bcixml.INTERACTION_SIGNAL) self.send_signal(signal)
def testEncodeUnsupportedSignalType(self): """Should throw an Exception on encoding an unknown signal type.""" signal = bcixml.BciSignal(None, None, "foo") self.assertRaises(bcixml.EncodingError, self.encoder.encode_packet, signal)