def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(LogicExperiment, self).__init__(*args, **kwargs) self.circuit_generator = CircuitGenerator() self._cfg_manager = cfg_manager try: self.webcam_url = self._cfg_manager.get_value(CFG_WEBCAM_URL, "") except: self.webcam_url = '' interfaces = [] if not is_testing(): interfaces.append(ConsoleInterface()) if self._cfg_manager.get_value(XILINX_ENABLED, False): interfaces.append(XilinxInterface(cfg_manager)) self.interfaces = HardwareInterfaceCollector(interfaces)
def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(LogicExperiment,self).__init__(*args, **kwargs) self.circuit_generator = CircuitGenerator() self._cfg_manager = cfg_manager try: self.webcam_url = self._cfg_manager.get_value(CFG_WEBCAM_URL, "") except: self.webcam_url = '' interfaces = [] if not is_testing(): interfaces.append(ConsoleInterface()) if self._cfg_manager.get_value(XILINX_ENABLED, False): interfaces.append(XilinxInterface(cfg_manager)) self.interfaces = HardwareInterfaceCollector(interfaces)
class LogicExperiment(Experiment.Experiment): def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(LogicExperiment, self).__init__(*args, **kwargs) self.circuit_generator = CircuitGenerator() self._cfg_manager = cfg_manager try: self.webcam_url = self._cfg_manager.get_value(CFG_WEBCAM_URL, "") except: self.webcam_url = '' interfaces = [] if not is_testing(): interfaces.append(ConsoleInterface()) if self._cfg_manager.get_value(XILINX_ENABLED, False): interfaces.append(XilinxInterface(cfg_manager)) self.interfaces = HardwareInterfaceCollector(interfaces) @Override(Experiment.Experiment) def do_get_api(self): return "2" @Override(Experiment.Experiment) def do_start_experiment(self, *args, **kwargs): self.tries = 0 self.best_tries = 0 self.current_circuit = self.circuit_generator.generate() self.active = True self.threads = [] self.interfaces.initialize() try: self.interfaces.send_message("Welcome!") except Exception as e: print "excepcion cuando tocaba LCD Welcome!", str(e) return json.dumps({ "initial_configuration": "{ \"webcam\" : \"%s\" }" % self.webcam_url, "batch": False }) @Override(Experiment.Experiment) def do_should_finish(self): if self.active: return 5 # Ask again in 5 seconds return -1 # Not active. Finish this session @Override(Experiment.Experiment) def do_dispose(self): try: self.interfaces.clear() except: pass for thread in self.threads: thread.join() if self.tries > self.best_tries: self.best_tries = self.tries return json.dumps({ Coordinator.FINISH_FINISHED_MESSAGE: True, Coordinator.FINISH_DATA_MESSAGE: "%s" % self.best_tries }) @threaded() def wait_and_turn_off(self): if is_testing(): pass else: time.sleep(7) self.interfaces.turn_off() @Override(Experiment.Experiment) def do_send_command_to_device(self, command): if command.startswith('SOLVE '): try: # "SOLVE XOR" operation = command[len('SOLVE '):].strip().lower() self.current_circuit.set_unknown(operation) except: return "Error: invalid operation: %s" % command else: if self.current_circuit.turned: self.tries += 1 self.current_circuit = self.circuit_generator.generate() self.interfaces.send_message("OK! %s" % self.tries) self.interfaces.turn_on() self.threads.append(self.wait_and_turn_off()) return "OK: %s" % self.tries else: if self.tries > self.best_tries: self.best_tries = self.tries self.tries = 0 self.active = False self.interfaces.send_message("Fail :-(") self.interfaces.turn_off() return "FAIL" elif command == 'GET_CIRCUIT': return json.dumps(self.current_circuit.to_dict()) else: return 'Error: Invalid command: %s' % command @Override(Experiment.Experiment) def do_send_file_to_device(self, content, file_info): return "ok"
class LogicExperiment(Experiment.Experiment): def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(LogicExperiment,self).__init__(*args, **kwargs) self.circuit_generator = CircuitGenerator() self._cfg_manager = cfg_manager try: self.webcam_url = self._cfg_manager.get_value(CFG_WEBCAM_URL, "") except: self.webcam_url = '' interfaces = [] if not is_testing(): interfaces.append(ConsoleInterface()) if self._cfg_manager.get_value(XILINX_ENABLED, False): interfaces.append(XilinxInterface(cfg_manager)) self.interfaces = HardwareInterfaceCollector(interfaces) @Override(Experiment.Experiment) def do_get_api(self): return "2" @Override(Experiment.Experiment) def do_start_experiment(self, *args, **kwargs): self.tries = 0 self.best_tries = 0 self.current_circuit = self.circuit_generator.generate() self.active = True self.threads = [] self.interfaces.initialize() try: self.interfaces.send_message("Welcome!") except Exception as e: print "excepcion cuando tocaba LCD Welcome!", str(e) return json.dumps({ "initial_configuration" : "{ \"webcam\" : \"%s\" }" % self.webcam_url, "batch" : False }) @Override(Experiment.Experiment) def do_should_finish(self): if self.active: return 5 # Ask again in 5 seconds return -1 # Not active. Finish this session @Override(Experiment.Experiment) def do_dispose(self): try: self.interfaces.clear() except: pass for thread in self.threads: thread.join() if self.tries > self.best_tries: self.best_tries = self.tries return json.dumps({ Coordinator.FINISH_FINISHED_MESSAGE : True, Coordinator.FINISH_DATA_MESSAGE : "%s" % self.best_tries}) @threaded() def wait_and_turn_off(self): if is_testing(): pass else: time.sleep(7) self.interfaces.turn_off() @Override(Experiment.Experiment) def do_send_command_to_device(self, command): if command.startswith('SOLVE '): try: # "SOLVE XOR" operation = command[len('SOLVE '):].strip().lower() self.current_circuit.set_unknown(operation) except: return "Error: invalid operation: %s" % command else: if self.current_circuit.turned: self.tries += 1 self.current_circuit = self.circuit_generator.generate() self.interfaces.send_message("OK! %s" % self.tries) self.interfaces.turn_on() self.threads.append(self.wait_and_turn_off()) return "OK: %s" % self.tries else: if self.tries > self.best_tries: self.best_tries = self.tries self.tries = 0 self.active = False self.interfaces.send_message("Fail :-(") self.interfaces.turn_off() return "FAIL" elif command == 'GET_CIRCUIT': return json.dumps(self.current_circuit.to_dict()) else: return 'Error: Invalid command: %s' % command @Override(Experiment.Experiment) def do_send_file_to_device(self, content, file_info): return "ok"