diff --git a/http_interface.py b/http_interface.py index 1bb87a0fbc24412fa80e314abe58668837ef4e63..b0ddc08750fedf9afde8b2519031f0224a8e3c83 100644 --- a/http_interface.py +++ b/http_interface.py @@ -40,48 +40,48 @@ class MyServer(SimpleHTTPRequestHandler): # with open(os.path.join('.', self.path[1:]), 'r') as f: # self.wfile.write(bytes(f.read(), "utf-8")) - # def __init__(self): - # super().__init__(self) - # # set controller - # self.controller = mqtt_client.Client(f"ohmpi_{OHMPI_CONFIG['id']}_listener", clean_session=False) # create new instance - # print(colored(f"Connecting to control topic {MQTT_CONTROL_CONFIG['ctrl_topic']} on {MQTT_CONTROL_CONFIG['hostname']} broker", 'blue')) - # trials = 0 - # trials_max = 10 - # broker_connected = False - # while trials < trials_max: - # try: - # self.controller.username_pw_set(MQTT_CONTROL_CONFIG['auth'].get('username'), - # MQTT_CONTROL_CONFIG['auth']['password']) - # self.controller.connect(MQTT_CONTROL_CONFIG['hostname']) - # trials = trials_max - # broker_connected = True - # except Exception as e: - # print(f'Unable to connect control broker: {e}') - # print('trying again to connect to control broker...') - # time.sleep(2) - # trials += 1 - # if broker_connected: - # print(f"Subscribing to control topic {MQTT_CONTROL_CONFIG['ctrl_topic']}") - # self.controller.subscribe(MQTT_CONTROL_CONFIG['ctrl_topic'], MQTT_CONTROL_CONFIG['qos']) - # else: - # print(f"Unable to connect to control broker on {MQTT_CONTROL_CONFIG['hostname']}") - # self.controller = None - # self.cmd_thread = threading.Thread(target=self._control) - # - # def _control(self): - # def on_message(client, userdata, message): - # global cmd_id, rdic - # - # command = message.payload.decode('utf-8') - # print(f'Received command {command}') - # # self.process_commands(command) - # if 'reply' in command.keys and command['cmd_id'] == cmd_id : - # rdic = command['reply'] - # - # self.controller.on_message = on_message - # self.controller.loop_start() - # while True: - # time.sleep(.1) + def __init__(self, request, client_address, server): + super().__init__(request, client_address, server) + # set controller + self.controller = mqtt_client.Client(f"ohmpi_{OHMPI_CONFIG['id']}_listener", clean_session=False) # create new instance + print(colored(f"Connecting to control topic {MQTT_CONTROL_CONFIG['ctrl_topic']} on {MQTT_CONTROL_CONFIG['hostname']} broker", 'blue')) + trials = 0 + trials_max = 10 + broker_connected = False + while trials < trials_max: + try: + self.controller.username_pw_set(MQTT_CONTROL_CONFIG['auth'].get('username'), + MQTT_CONTROL_CONFIG['auth']['password']) + self.controller.connect(MQTT_CONTROL_CONFIG['hostname']) + trials = trials_max + broker_connected = True + except Exception as e: + print(f'Unable to connect control broker: {e}') + print('trying again to connect to control broker...') + time.sleep(2) + trials += 1 + if broker_connected: + print(f"Subscribing to control topic {MQTT_CONTROL_CONFIG['ctrl_topic']}") + self.controller.subscribe(MQTT_CONTROL_CONFIG['ctrl_topic'], MQTT_CONTROL_CONFIG['qos']) + else: + print(f"Unable to connect to control broker on {MQTT_CONTROL_CONFIG['hostname']}") + self.controller = None + self.cmd_thread = threading.Thread(target=self._control) + + def _control(self): + def on_message(client, userdata, message): + global cmd_id, rdic + + command = message.payload.decode('utf-8') + print(f'Received command {command}') + # self.process_commands(command) + if 'reply' in command.keys and command['cmd_id'] == cmd_id : + rdic = command + + self.controller.on_message = on_message + self.controller.loop_start() + while True: + time.sleep(.1) def do_POST(self): global cmd_id, rdic diff --git a/ohmpi.py b/ohmpi.py index 52e1dc26f9af4c3a1031e207289d8f36e921ed53..f78a370248516f8b516e6819c54e73fc15c9a75e 100644 --- a/ohmpi.py +++ b/ohmpi.py @@ -59,6 +59,7 @@ class OhmPi(object): # flags and attributes if on_pi is None: _, on_pi = OhmPi.get_platform() + self.sequence = sequence self.on_pi = on_pi # True if run from the RaspberryPi with the hardware, otherwise False for random data @@ -252,7 +253,7 @@ class OhmPi(object): pass return platform, on_pi - def read_quad(self, filename): + def load_sequence(self, filename): """Read quadrupole sequence from file. Parameters @@ -266,7 +267,7 @@ class OhmPi(object): sequence : numpy.array Array of shape (number quadrupoles * 4). """ - sequence = np.loadtxt(filename, delimiter=" ", dtype=np.int32) # load quadrupole file + sequence = np.loadtxt(filename, delimiter=" ", dtype=np.uint32) # load quadrupole file if sequence is not None: self.exec_logger.debug('Sequence of {:d} quadrupoles read.'.format(sequence.shape[0])) @@ -549,7 +550,7 @@ class OhmPi(object): # create custom sequence where MN == AB # we only check the electrodes which are in the sequence (not all might be connected) if self.sequence is None: - quads = np.array([[1, 2, 1, 2]]) + quads = np.array([[1, 2, 1, 2]], dtype=np.uint32) else: elec = np.sort(np.unique(self.sequence.flatten())) # assumed order quads = np.vstack([ @@ -681,8 +682,12 @@ class OhmPi(object): self._update_acquisition_settings(args) status = True elif cmd == 'set_sequence' and args is not None: - self.sequence = np.loadtxt(StringIO(args)) - status = True + try: + self.sequence = np.loadtxt(StringIO(args)).astype('uint32') + status = True + except Exception as e: + self.exec_logger.warning(f'Unable to set sequence: {e}') + status = False elif cmd == 'start': self.measure(cmd_id) while not self.status == 'idle': @@ -691,18 +696,13 @@ class OhmPi(object): elif cmd == 'stop': self.stop() status = True - elif cmd == 'read_sequence': + elif cmd == 'load_sequence': try: - self.read_quad(args) + self.load_sequence(args) status = True except Exception as e: - self.exec_logger.warning(f'Unable to read sequence: {e}') - elif cmd == 'set_sequence': - try: - self.sequence = np.array(args) - status = True - except Exception as e: - self.exec_logger.warning(f'Unable to set sequence: {e}') + self.exec_logger.warning(f'Unable to load sequence: {e}') + status = False elif cmd == 'rs_check': try: self.rs_check()