Monitor Method

@bp.method('device.monitor')

This method will provide the layout and the data monitored from the device to the user interface. At the moment there is only the Plotly library supported. You will have to implement the layout as well as the data representation according to the Plotly documentation.

In order to make the dashboard work you will need to enable it in device.json. Plus you need to enable it when registering or editing the device in the user interface.

Parameters

There is one parameter passed to this function which contains the information of the requested option:

def monitor(option: str) -> str:

layout

The layout is the first thing to be requested when accessing the dashboard on the user interface.

Return it in the correct if case in a valid json format, e.g.:

if option == 'layout':
    return json.dumps(<PLOTLY_JSON_LAYOUT>)

data

The data for the dashboard will be requested periodically, depending of the status of the device. It consists of two json entries which must be present (monitoring and graph) and an optional error entry.

Return the data in a valid json format in the correct if case:

if option == 'data':
    return json.dumps(<PLOTLY_JSON_DATA>)

Data Entries

These two entries must be present in the response of the data request:

monitoring

A Boolean which tells the user interface if the device is monitoring the activity or not. When a device is not monitoring currently, the data will be requested less frequently.

Warning

The key must be named exactly monitoring.

graph

The real data to represent in the dashboard. It must also be in a valid Plotly format.

error

Optional. A single string holding the device’s current background error, or an empty string / null when there is none. It is reported on every data request (a snapshot of the current state), so it may repeat across requests. The user interface accumulates distinct error messages into a persistent, dismissable list on the device’s send page, so background errors are not missed even when they are only briefly active. Omit it (or leave it empty) when there is no error.

Note

The key must be named exactly error. The same message is shown only once (deduplicated) and stays until the user dismisses it; a message that is dismissed while still being reported does not re-appear until it clears and occurs again.

Example

A simple example of layout and data can look like this:

@bp.method('device.monitor')
def graph(option: str) -> str:

    layout = {
        'showLegend': False,
        'xaxis': {
            'title': 'Time',
            'showgrid': False,
            'zeroline': False,
            'autotick': True,
            'tick': '',
            'showticklabels': False
        },
        'yaxis': {
            'title': 'Voltage [mV]'
        }
    }

    data = {
       'monitoring': True,
       'graph': [{
           'x': [10, 20, 30, 40],
           'y': [1, 2, 3, 4]
       }],
       # optional: the current background error, empty when there is none
       'error': ''
    }

    if option == 'data':
        return json.dumps(data)

    if option == 'layout':
        return json.dumps(layout)

Note that the data normally comes from a hardware module and is not static.

It is also supported to plot multiple graphs by using subplots, as described in the Plotly documentation.