Commit f1d2cb40 authored by Guillaume Blanchy's avatar Guillaume Blanchy
Browse files

improving index-mqtt.html putting cmd_id as part of kwargs

Showing with 51 additions and 25 deletions
+51 -25
......@@ -145,11 +145,14 @@
let callbacks = {} // store callback (might not be needed)
// function with MQTT
let topic = '/World'
let hostname = 'localhost'
let topic = 'ohmpi_0001' // we could change this through a drop-down to connect to a different ohmpi
let topic_ctrl = topic + '/ctrl'
let topic_exec = topic + '/exec'
let topic_data = topic + '/data'
let hostname = location.hostname
let port = 9001
let clientId = 'ohmpi_0001_html'
let message = null
// create client
client = new Paho.MQTT.Client(hostname, port, clientId);
......@@ -159,28 +162,42 @@
function onConnect() {
console.log("onConnect")
client.subscribe(topic)
client.subscribe(topic_data)
client.subscribe(topic_exec)
// send welcome message
message = new Paho.MQTT.Message("Hello from index.html")
message.destinationName = topic
message.destinationName = topic_ctrl
client.send(message)
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0)
console.log("onConnectionLost:"+responseObject.errorMessage)
console.log("onConnectionLost:" + responseObject.errorMessage)
}
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString)
console.log("onMessageArrived:" + message.payloadString)
try {
let response = JSON.parse(message.payloadString)
console.log('response=', response)
// check ID of message against our dictionnary of callback
let cmd_id = response['cmd_id']
if (callbacks.hasOwnProperty(cmd_id)) {
console.log('++ execute callback')
callbacks[cmd_id](response['content']) // execute callback
let payload = message.payloadString
if (message.topic == topic_data) {
// process data
console.log('DATA', payload)
// usually these don't have a cmd_id so we are not sure when
} else if (message.topic == topic_exec) {
// display it in the log
console.log('EXEC LOG:', payload)
}
// let response = JSON.parse(message.payloadString)
// console.log('response=', response)
// // check ID of message against our dictionnary of callback
// let cmd_id = response['cmd_id']
// if (callbacks.hasOwnProperty(cmd_id)) {
// console.log('++ execute callback')
// callbacks[cmd_id](response['content']) // execute callback
// }
} catch (e) {
console.log(e)
}
......@@ -188,6 +205,14 @@
}
// useful functions
function generateUniqSerial() {
return 'xxxx-xxxx-xxx-xxxx'.replace(/[x]/g, (c) => {
const r = Math.floor(Math.random() * 16);
return r.toString(16);
});
}
// sending commands to the OhmPi
function sendCommand(query, callback=null) {
// dic in the form: {'cmd': X, ...} as JSON
if (callback == null) {
......@@ -211,19 +236,19 @@
*/
// generate a unique command id to be associated with the commands
let uuid = crypto.randomUUID()
let uuid = generateUniqSerial()
commands[uuid] = query
callbacks[uuid] = callback // store the callback to be processed later when message arrives
let payload = '{"cmd_id": "' + uuid + '",' + query.slice(1)
console.log('sendCommand()', payload)
message = new Paho.MQTT.Message(payload)
message.destinationName = topic
message.destinationName = topic_ctrl
client.send(message)
}
// run button
function runBtnFunc() {
sendCommand('{"cmd": "run_sequence"}', function(x) {
sendCommand('{"cmd": "run_multiple_sequences"}', function(x) {
console.log(x['status'])
if (x['status'] == 'running') {
output.innerHTML = 'Status: measuring...'
......@@ -258,7 +283,9 @@
function configCallback() {
sendCommand(JSON.stringify({
'cmd': 'update_settings',
'config': formVals
'kwargs': {
'config': formVals
}
}), function(x) {
console.log('update_settings', x)
})
......
......@@ -872,15 +872,13 @@ class OhmPi(object):
return d
def run_multiple_sequences(self, cmd_id=None, sequence_delay=None, nb_meas=None, **kwargs):
def run_multiple_sequences(self, sequence_delay=None, nb_meas=None, **kwargs):
"""Runs multiple sequences in a separate thread for monitoring mode.
Can be stopped by 'OhmPi.interrupt()'.
Additional arguments are passed to run_measurement().
Parameters
----------
cmd_id :
sequence_delay : int, optional
Number of seconds at which the sequence must be started from each others.
nb_meas : int, optional
......@@ -916,7 +914,7 @@ class OhmPi(object):
self.thread = threading.Thread(target=func)
self.thread.start()
def run_sequence(self, cmd_id=None, **kwargs):
def run_sequence(self, **kwargs):
"""Runs sequence synchronously (=blocking on main thread).
Additional arguments are passed to run_measurement().
"""
......@@ -962,9 +960,9 @@ class OhmPi(object):
self.switch_mux_off(quad)
# add command_id in dataset
acquired_data.update({'cmd_id': cmd_id})
# acquired_data.update({'cmd_id': cmd_id}) // in run_measurement()
# log data to the data logger
# self.data_logger.info(f'{acquired_data}')
# self.data_logger.info(f'{acquired_data}') // in run_measurement()
# save data and print in a text file
self.append_and_save(filename, acquired_data)
self.exec_logger.debug(f'quadrupole {i + 1:d}/{n:d}')
......@@ -1177,7 +1175,8 @@ class OhmPi(object):
def set_sequence(self, sequence=None):
try:
self.sequence = np.loadtxt(StringIO(sequence)).astype('uint32')
self.sequence = np.array(sequence)
#self.sequence = np.loadtxt(StringIO(sequence)).astype('uint32')
status = True
except Exception as e:
self.exec_logger.warning(f'Unable to set sequence: {e}')
......
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