pylab_ml.common.mqtt_client.mqtt_init
- class mqtt_init(*args, **kwargs)[source]
Bases:
objectInitialise the mqtt connection and provide functions for the communication.
- __init__(typ='control', logger=None)[source]
Initialise the class mqtt_init.
- Parameters:
typ (str) – ‘instrument’ for instruments ‘control’ for guis or extern controlling (default)
logger (mylogger or None) – Logger instance for logging messages. If None, no logging is performed.
Methods
__init__([typ, logger])Initialise the class mqtt_init.
clearpublish(attr)Remove retained publish message from broker
close()Close the MQTT client connection and clean up resources.
init([broker, port, message_client, ...])Normally call from plugin semi-control, you have not to call this function, if you use semi-control.
mqtt_add(instrument[, hostname])Add first callback to get mqtt-commands from control, and append instrument to instruments-list.
mqtt_disconnect([instrument, hostname])Remove instrument from the instruments-list, if instruments-list is empty than remove callback.
mqtt_receive(topic, msg)publish(attr, value[, qos, retain])Publish a message to the MQTT broker.
- clearpublish(attr)[source]
Remove retained publish message from broker
- Parameters:
attr (str) – The topic attribute for which the retained message should be removed.
- Return type:
None
- init(broker='127.0.0.1', port=1883, message_client=None, username='', userpasswd='', qos=0, retain=False)[source]
Normally call from plugin semi-control, you have not to call this function, if you use semi-control.
- Parameters:
broker (str) – The address of the MQTT broker (default: “127.0.0.1”)
port (int) – The port of the MQTT broker (default: 1883)
message_client (str or None) – None if client the instrument, like e.q. smu, digital-multimeter or thermostreamer. eg. ‘DT1604092’ if control and you want connect from an extern computer to DT1604092 (but is not checked until now!!)
username (str) – The username for MQTT authentication (default: “”)
userpasswd (str) – The password for MQTT authentication (default: “”)
qos (int) – The Quality of Service level for MQTT messages (default: 0)
retain (bool) – Whether to retain MQTT messages (default: False)
- Returns:
True if the MQTT client was successfully initialized and connected to the broker, False otherwise.
- Return type:
bool
- mqtt_add(instrument, hostname=None)[source]
Add first callback to get mqtt-commands from control, and append instrument to instruments-list.
- Parameters:
instrument (object) – The instrument to be added to the MQTT client.
hostname (str or None) – The hostname for the MQTT topic. If None, the client’s hostname will be used (default: None).
- Return type:
None
- mqtt_disconnect(instrument=None, hostname=None)[source]
Remove instrument from the instruments-list, if instruments-list is empty than remove callback.
- Parameters:
instrument (object or None) – The instrument to be removed from the MQTT client. If None, no instrument will be removed (default: None).
hostname (str or None) – The hostname for the MQTT topic. If None, the client’s hostname will be used (default: None).
- Return type:
None
- publish(attr, value, qos=None, retain=None)[source]
Publish a message to the MQTT broker.
- Parameters:
attr (str) – The topic attribute to publish.
value (any) – The value to publish. This will be converted to a JSON string before publishing.
qos (int or None) – The Quality of Service level for the MQTT message. If None, the default QoS level of the client will be used (default: None).
retain (bool or None) – Whether to retain the MQTT message. If None, the default retain flag of the client will be used (default: None).
- Return type:
None