Skip to content

Configuration

When using an adapter, unique information about each physical instance is needed. Each piece may have different identifiers, data paths, and settings. The configuration file provides this individual context by bridging the general adapter code with the specific details of each setup. This configuration allows the adapter system to customise its behaviour for each instance, enabling a single adapter class to be reused across multiple pieces of equipment, each with its configuration.

The configuration file is where you define how your equipment adapters will operate. You can edit the configuration file to the specifications of your equipment, such as connection parameters, data handling, and output settings. The configuration is written in YAML format, which is easy to read and write.

LEAF reads config.yaml from the path given with -c. Without -c, it uses configuration.yaml inside the installed leaf package, so keep your own configuration outside the package with -c.

An example:

EQUIPMENT_INSTANCES:
- equipment:
adapter: Example
data:
instance_id: demo_device
organisation: university_lab
department: research
root: xyz
requirements:
interval: 1
OUTPUTS:
- plugin: MQTT
broker: test.mosquitto.org
port: 1883

Each configuration file consists of two main sections: EQUIPMENT_INSTANCES and OUTPUTS.

  • EQUIPMENT_INSTANCES: This section defines the equipment adapters you want to use, along with their specific settings and requirements.
  • OUTPUTS: This section defines how the data from the equipment adapters will be outputted or stored, such as through MQTT, SQL, or to a local file.

Any value can come from an environment variable: ${VAR}, or ${VAR:default} with a default. For example password: ${MQTT_PASSWORD} or broker: ${MQTT_BROKER:localhost}. This keeps passwords and tokens out of the file. If VAR is not set and there is no default, LEAF does not start the adapters and shows the error in the web interface. The substitution runs on the whole file before it is read as YAML, so it also applies to ${…} inside comments.

The EQUIPMENT_INSTANCES section of the config file defines defines the equipment adapters you want to use (adapter), any metadata about the device (data) and adapter specific requirements (requirements). Depending on the adapter chosen, the requirements will differ.

Each entry under EQUIPMENT_INSTANCES represents an individual EquipmentAdapter instance for a piece of equipment (A single process can support running multiple adapters). This section provides information necessary to distinguish each instance and configure it according to specific needs:

  • equipment:
    • adapter: This field specifies the adapter type, which is the equipment ID within the device.json file of the given adapter (e.g.,ConcreteAdapter1), which is the name of the adapter class. It is needed for adapter discovery. Below, we discuss each aspect of a config file. Due to the config file’s specific nature, some examples for specific adapters are given, denoted with the (EquipmentAdapter specific) tag.

    • data: This field contains identifiers/metadata. It is required for discovery and ensuring uniqueness within the larger network of equipment. Keep instance_id unique: a duplicate only gives a warning, and the two instances overwrite each other’s state.

      • instance_id: A unique identifier for this equipment instance.
      • organisation: The organisation where the equipment is based.
      • department: The department where the equipment is based.
      • root (optional): Prefix for all MQTT topics of this instance. Topics are <organisation>/<department>/<adapter_id>/<instance_id>/…, or <root>/<organisation>/… when root is set. OAK expects leaf.
    • requirements: Defines adapter-specific required operational parameters:

      • write_file (EquipmentAdapter specific): Specifies the path where data from the equipment will be written.
      • broker/host (EquipmentAdapter specific): Connection information, such as the host address.
      • port (EquipmentAdapter specific): The connection port for the host.
      • token (EquipmentAdapter specific): Code for authentication.
    • simulation (optional): This nested data is only required for adapters running in simulated mode, which varies by adapter. Not all adapters support simulations.

      • filename: (EquipmentAdapter specific) A path to a file with simulated data (Note this may change for different adapters).
      • interval: (EquipmentAdapter specific) The interval in seconds when the simulation will feed the adapter another chunk of data (measurement).
    • maximum_message_size (optional): If present, it sets the maximum number of measurements that will be transmitted within a single message. The higher this option, the fewer messages will be sent, but each message will be a larger payload.

    • experiment_timeout (optional): If present, when an experiment is running, and a new measurement hasn’t been taken within this timeout period, the experiment will be stopped. This is used for cases where the lack of measurement data will be due to an error in a given time.

    • control (optional): If true, LEAF subscribes to the instance’s control topic and hands each message to the adapter’s controller, and the instance appears in the Control tab of the LEAF UI. Requires an MQTT output and an adapter with a controller.py. See Controlling Equipment.

    • control_limits (optional): Narrower min/max per control for this instance, e.g. a T_sp key with min: 295 and max: 305 on separate lines below it. Limits can only tighten the adapter’s own range. See Checks and limits.

    • external_input (optional): If present, it will use the external input modules that take data—typically commands—from sources other than the equipment the adapter is designed for. This external source could be another tool for analysing the outgoing data. The external input serves as the entry point to “close the loop”—that is, to feed control signals back into the system based on the analysis. Note that this system is in early development and isn’t fully developed. See (here) for progress. The ExternalEventWatcher names are added as the plugin name and requirements provided below. This process is the same as defining an OutputModule discussed below.

Each entry under OUTPUTS represents an OutputModule for transmitting or storing data. Output modules can be chained with fallback: when an output fails, its messages go to the output named in its fallback. An output without a fallback (that is not itself a fallback or parallel of another output) falls back to a JSON file in the system temp folder, leaf/<PLUGIN>_fallback.json. Each plugin can appear only once in OUTPUTS. Like the requirement section within the EquipmentAdapters, most of the fields within an OUTPUTS element are specific to the type of module that is defined to be used.

  • plugin: This specifies the type of output module: MQTT, SQL or FILE (case-insensitive).
  • broker/host (OutputModule specific): Connection information, such as the broker or host address for MQTT outputs.
  • port (OutputModule specific): The connection port for the output module.
  • username/password (OutputModule specific): Credentials for authentication (for secure output modules like MQTT).
  • fallback: Defines the next output in the chain if the primary output fails. For example, an MQTT output can fall back to a FILE or SQL output, which keeps the messages and sends them once the broker is reachable again.
  • parallel: Name (or list of names) of other outputs that receive every message as well.
  • filename (OutputModule specific): Specifies the path to a file where data will be stored locally if other outputs are unavailable (Used in FILE or SQL output modules).

This structure enables flexible data transmission. Primary outputs can be backed up by secondary methods, ensuring data persistence even if one output method encounters issues. This data must be specified within a file known as a “yaml” file. This is essentially a dictionary of keys and values, as seen below.

The OUTPUT section of the config file defines how the data from the equipment adapters will be outputted or stored. Users can currently choose MQTT, SQL, and FILE as possible outputs.

EQUIPMENT_INSTANCES:
- equipment:
adapter: Example
data:
instance_id: demo_device
organisation: university_lab
department: research
requirements:
interval: 1
OUTPUTS:
# MQTT output requires 'broker'; 'port' defaults to 1883
# optionally 'username', 'password', 'clientid', 'protocol', 'transport', 'tls', 'heartbeat_interval'
# and optionally an alternative 'fallback' output
- plugin: MQTT
broker: test.mosquitto.org
port: 1883
# username: my_user #OPTIONAL expects a str
# password: my_password #OPTIONAL expects a str
# clientid: my_leaf_01 #OPTIONAL expects a unique str
# protocol: v3 #OPTIONAL expects "v3" "v5"
# transport: tcp #OPTIONAL expects "tcp", "websockets", "unix"
# tls: False #OPTIONAL expects True, False
# fallback: SQL #OPTIONAL expects "SQL", "FILE"
# heartbeat_interval: 300 #OPTIONAL seconds, default 300, 0 turns it off
# SQL output stores messages in a SQLite file, 'filename' (default leaf_messages.db)
# and optionally an alternative 'fallback' output
# - plugin: SQL
# filename: messages.db
# fallback: FILE #OPTIONAL expects "MQTT", "FILE"
# FILE output requires 'filename'
# and optionally an alternative 'fallback' output
# - plugin: FILE
# filename: local.json
# fallback: SQL #OPTIONAL expects "MQTT", "SQL"

The MQTT output adapter uses MQTT protocol to transmit data to an MQTT broker. It requires broker; port defaults to 1883. Users can optionally add username and password (only used together), clientid, protocol, transport, tls and heartbeat_interval, as well as an alternative fallback output. With tls: True the connection is encrypted, but the broker’s certificate hostname is not checked. Messages are sent with QoS 0.

Leave out optional keys you don’t need rather than setting them to None: YAML reads None as the string "None", not as empty. This matters most for clientid, which must be unique per broker. Two LEAF instances with the same client id will keep disconnecting each other.

LEAF publishes a heartbeat for every equipment instance on <root>/<organisation>/<department>/<adapter_id>/<instance_id>/heartbeat (retained): {"status": "alive", "interval": <seconds>, "timestamp": <epoch>, "datetime": "<local time>"}, once when the instance starts and then every heartbeat_interval seconds (default 300; 0 turns it off). When an instance stops it publishes "status": "stopped". LEAF sets no Last Will, so after a crash the retained message stays alive: treat an instance as gone when its timestamp is older than interval. Heartbeats are not buffered in the fallback. The old heartbeat_topic option is ignored.

OUTPUTS:
- plugin: MQTT
broker: test.mosquitto.org
port: 1883
# username: my_user #OPTIONAL expects a str
# password: my_password #OPTIONAL expects a str
# clientid: my_leaf_01 #OPTIONAL expects a unique str, omit to auto-generate
protocol: v3 #OPTIONAL expects "v3" "v5"
transport: tcp #OPTIONAL expects "tcp", "websockets", "unix"
tls: False #OPTIONAL expects True, False
fallback: SQL #OPTIONAL expects "SQL", "FILE"
heartbeat_interval: 300 #OPTIONAL seconds between heartbeats, default 300, 0 turns it off

The SQL output adapter stores messages in a local SQLite database file, filename (default leaf_messages.db). Users can opt to add an alternative fallback output.

OUTPUTS:
- plugin: SQL
filename: messages.db
fallback: FILE #OPTIONAL expects "MQTT", "FILE"

The FILE output adapter appends each message as a line of JSON to filename; use a filename ending in .gz to compress it. Users can opt to add an alternative fallback output.

OUTPUTS:
- plugin: FILE
filename: local.json
fallback: SQL #OPTIONAL expects "MQTT", "SQL"

Adapter management

Configuring adapters can be done with the Graphical User Interface (GUI). As shown in the example image, on the right there is a dropdown menu available where you can select an example for each of the installed adapters.

At the bottom of the configuration page, Save & Restart writes the editor to the configuration file and restarts LEAF with it. The Restart and Stop icons in the top bar restart LEAF with the saved file (unsaved edits are not applied) or stop it, after asking for confirmation.

If the configuration cannot be loaded, LEAF keeps the web interface running and shows the error at the top, so you can fix it here and Save & Restart. If one equipment instance fails to start, it is skipped and the others keep running.