thin-edge.io offers multiple ways to monitor the connection locally and do some follow up action.
Option 1: Monitoring on the local MQTT broker
Checking the bridge status will depend on a thin-edge.io setting, mqtt.bridge.built_in, which controls whether the mosquitto bridge is being used, or the Rust based built-in bridge which is included inside each cloud mapper.
You can check which bridge type you’re using by checking the following tedge config value.
$ tedge config get mqtt.bridge.built_in
true
Once you have the value, then you can subscribe to the following MQTT topics. Below shows how to subscribe to the MQTT topic using the tedge command, but you can use any tool to subscribe to the MQTT topic (e.g. mosquitto_sub or any specific library).
Note: In the future, we will be change the default to use the built-in bridge as it gives us more control over the connection, and we can support other features like proxies, HSMs etc.
When using mosquitto bridge (mqtt.bridge.built_in=false)
$ tedge mqtt sub --count 1 'te/device/main/service/mosquitto-c8y-bridge/status/health'
[te/device/main/service/mosquitto-c8y-bridge/status/health] 1
When using built-in bridge (mqtt.bridge.built_in=true)
$ tedge mqtt sub --count 1 'te/device/main/service/tedge-mapper-bridge-c8y/status/health'
[te/device/main/service/tedge-mapper-bridge-c8y/status/health] {"status":"up"}
Option 2: Using tedge connect c8y --test
Another option is to use the tedge connect c8y --test command to verify the connection. This connection will do a full connection check by actively sending a message to the server to verify that it is actually working successful, so it will result in more data being sent, but depending on how often you plan on invoking it, then the increase might be negligible.
This method is convenient to use in scripts as the exit code of the tedge connect c8y --test command will be determined based on the outcome of the test, so this means you can use it in an if statement nicely:
#!/bin/sh
if ! tedge connect c8y --test; then
echo "Device is not currently connected to Cumulocity"
else
echo "Device is currently connected"
fi