MQTT and API implementation

MQTT clients and related functionality

MQTT-class for communication between Gateway and ReCalc/Cloud at ReMoni

Platform

Python 3.5.10 on Linux

Synopsis

This module implements a class for MQTT Client

Authors

Steffen Breinbjerg, Janus Bo Andersen

Latest update

18 November 2020

Version

1.2

  • Ver. 1.0: Setup MQTT class with loaded settings.

  • Ver. 1.1: Implement support functions for return codes and better printout for on_connect.

  • Ver. 1.2: Implemented TLS and better handling of reason codes.

MqttClient class

class mqtt.MqttClient.MqttClient(name, on_message, on_publish, param_settings='mqtt_local')[source]

This class wraps a Paho MQTT client and sets it up using a profile from settings/secrets.yaml. On instantiation, all setup steps run up to and including connect.

__init__(name, on_message, on_publish, param_settings='mqtt_local')[source]

Handles all setup and connection when object is initialized.

Parameters
  • name (str) – Client ID (must be unique)

  • on_message (function_ptr) – On message callback function

  • on_publish (function_ptr) – On publish callback function

  • param_settings (str) – Profile from secrets.yaml to use

disconnect()[source]

Gracefully disconnects from server.

loop_forever()[source]

Will block the program, and only handle callback functions and disconnects.

loop_start()[source]

Start loop in new thread. This thread will handle disconnects to MQTT broker.

loop_stop()[source]

Stops the loop thread.

static on_connect(client: paho.mqtt.client.Client, userdata, flags, rc)[source]

on_connect callback rc argument value meaning:

  • 0: Connection successful

  • 1: Connection refused - incorrect protocol version

  • 2: Connection refused - invalid client identifier

  • 3: Connection refused - server unavailable

  • 4: Connection refused - bad username or password

  • 5: Connection refused - not authorised 6-255: Currently unused.

publish(topic, payload)[source]
Parameters
  • topic (str) – Topic to publish to

  • payload (str) – Message payload

Returns

(result, mid)

Return type

tuple

  • result: 0 on success, 4 if no connection

  • mid is message id for tracked message

subscribe(topic, qos)[source]
Parameters
  • topic (str) – Topic to subscribe to.

  • qos (int) – Quality of service level (0, 1, 2).

Returns

(result, mid)

Return type

tuple

  • result: 0 on success, 4 if no connection

  • mid is message id for tracked message

Supporting functions

mqtt.MqttClient.connection_rc_str(rc: Tuple)str[source]

Returns text description of return codes for a connection attempt to server. Based on return code spec. from Paho MQTT. Alternatively, use mqtt.connack_string().

mqtt.MqttClient.publish_rc_str(rc: Tuple)str[source]

Returns text description of return codes for publishing a message. Based on return code spec. from Paho MQTT. Alternatively, use mqtt.error_string().

mqtt.MqttClient.publish_rc_bool(rc: Tuple)bool[source]

Returns True if message was sent correctly, otherwise False. Based on return code spec. from Paho MQTT. The rc tuple is (reason_code, message_id). Alternatively, use mqtt.error_string().

API implementation

API compliant messages

Synopsis

Implements functionality to send and receive correctly formatted messages as spec’ed by ReMoni API v2.

Authors

Jakob, Steffen, Janus

Last update

18 Nov. 2020.

mqtt.api.build_api_message_from_log_obj(m: meter.MeterMeasurement.MeterMeasurement) → List[Any][source]

Due to bug in ReCalc, this currently only returns a list of Python dicts. In the future, should return the same dumped to JSON.

mqtt.api.config_json()str[source]

Returns a JSON-formatted string to config OmniPower in ReCalc API.