Commit 46e5a1cc authored by Arnaud WATLET's avatar Arnaud WATLET
Browse files

Merge branch 'mqtt' of https://gitlab.irstea.fr/reversaal/OhmPi into mqtt

Showing with 65 additions and 63 deletions
+65 -63
......@@ -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
......
......@@ -55,12 +55,13 @@ class OhmPi(object):
sequence: 1, 2, 3, 4 is used.
"""
def __init__(self, settings=None, sequence=None, mqtt=True, on_pi=None):
def __init__(self, settings=None, sequence=None, use_mux=False, mqtt=True, on_pi=None):
# flags and attributes
if on_pi is None:
_, on_pi = OhmPi.get_platform()
self.sequence = sequence
self.sequence = sequence
self.use_mux = use_mux
self.on_pi = on_pi # True if run from the RaspberryPi with the hardware, otherwise False for random data
self.status = 'idle' # either running or idle
self.thread = None # contains the handle for the thread taking the measurement
......@@ -121,7 +122,7 @@ class OhmPi(object):
# read quadrupole sequence
if sequence is not None:
self.read_quad(sequence)
self.load_sequence(sequence)
# connect to components on the OhmPi board
if self.on_pi:
......@@ -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]))
......@@ -309,8 +310,10 @@ class OhmPi(object):
role : str
Either 'A', 'B', 'M' or 'N', so we can assign it to a MUX board.
"""
if self.sequence is None: # only 4 electrodes so no MUX
pass
if not self.use_mux:
pass # no MUX or don't use MUX
elif self.sequence is None:
self.exec_logger.warning('Unable to switch MUX without a sequence')
else:
# choose with MUX board
tca = adafruit_tca9548a.TCA9548A(self.i2c, self.board_addresses[role])
......@@ -548,8 +551,8 @@ 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]])
if self.sequence is None or not self.use_mux:
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 +684,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 +698,13 @@ class OhmPi(object):
elif cmd == 'stop':
self.stop()
status = True
elif cmd == 'read_sequence':
try:
self.read_quad(args)
status = True
except Exception as e:
self.exec_logger.warning(f'Unable to read sequence: {e}')
elif cmd == 'set_sequence':
elif cmd == 'load_sequence':
try:
self.sequence = np.array(args)
self.load_sequence(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()
......@@ -812,7 +814,7 @@ class OhmPi(object):
exit()
VERSION = '2.1.0'
VERSION = '2.1.5'
print(colored(r' ________________________________' + '\n' +
r'| _ | | | || \/ || ___ \_ _|' + '\n' +
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment