RADIUSdesk

logo

This is an old revision of the document!


RADIUSdesk MQTT Implementation

Introduction

  • MESHdesk and APdesk traditionally makes use of a heartbeat system to communicate and report to the back-end.
  • We now also include a MQTT based implementation to allow real-time communication between the mesh nodes or access points and the back-end.
  • This implementation is used as a compliment to the heartbeat system, making it more robust while offering you added real-time communication.
  • The MQTT implementation is not compulsory in order to have a working deployment but it does offer a lot of advantages.
  • It is ideal for hardware that is used in a IOT environment where you need immediate execution of commands.

Architecture

  • Consider the following diagram and then the subsequent discussion of each of the components.

ExtJS GUI

  • The ExtJS GUI can be used to send commands to the mesh nodes and access points managed by MESHdesk and APdesk respectively.
  • The communication between ExtJS and the CakePHP application consists of REST-like API calls using HTTP or HTTPS.
  • This means essentially that these actions can easily be automated or done with another GUI should the need arise.

CakePHP

  • If MQTT support is enabled on the system and someone initiate a command execution action from the GUI, the controller code handling this request will send the request to the API Gateway.
  • This communication between the CakePHP controller and the API Gateway also consist of REST-like API calls using HTTP or HTTPS.

API Gateway

  • The API Gateway is a Node.js based web service that acts as a middle man.
  • The MQTT implementation uses a command and response principle.
  • The API Gateway
    • Receive instructions from CakePHP and translate them to MQTT publish actions (Command) which are published to the Mosquitto MQTT Broker.
    • Subscribe to MQTT topics (Response) on the Mosquitto MQTT Broker which will get input from the mesh nodes and access points and translate them to HTTP/HTTPS based API calls to CakePHP.

Mesh nodes and access points

  • The mesh nodes and access points communicate with the CakePHP back-end using HTTP/HTTPS to fetch its configuration and do reporting.
  • If the system has MQTT support enabled the mesh node or access point will configure itself to publish and subscribe to certain topics on the Mosquitto MQTT Broker.
  • The system works on a command and response principle.
    • The mesh node or access point subscribe to a topic where it will expect commands from the API Gateway.
    • The mesh node or access point will publish to a topic where the API Gateway expect responses.
    • The API Gateway will publish to a topic where the mesh node or access point expect commands.
    • The API Gateway will subscribe to a topic where the mesh node or access points publish their responses.

Enable MQTT

  • There are two components of the MQTT setup that needs to be configured
    • Configuration settings for mesh nodes and access points (MESHdesk and APdesk)
    • Configuration settings for the MQTT API Gateway.

Looking at the code

Command -> CakePHP Controller

  • Lets look at the /var/www/html/cake3/rd_cake/src/Controller/NodeActionsController.php file.
  • When an action is added to a node and MQTT is enabled on the system this code is executed:
if ($cfg['api_mqtt_enabled'] == "1"){
    //Talk to MQTT Broker
     $data = $this->_get_node_mac_mesh_id($formData['node_id']);
     $payload = [
         'mode'     => 'mesh',
         'node_id'  => $formData['node_id'],
         'mac'      => strtoupper($data['mac']),
         'mesh_id'  => strtoupper($data['ssid']),
         'cmd_id'   => $entity->id,
         'cmd'      => $formData['command'],
         'action'   => $formData['action'],
     ];
 
     if($this->_check_server($client, $cfg['api_gateway_url'], 5)){
         try {
             $client->request('POST', $cfg['api_gateway_url'] . '/rd/mesh/command', ['json' => ['message' => $payload]]);
         } catch (\Exception $e) {
             // Do Nothing
         }
     }
}

Command -> API Gateway

  • The API call to the API Gateway will execute this piece of code in the /opt/Rdcore-API-Gateway/routes/rdmesh.js file
router.post('/mesh/command', function(req, res){
    //var data = JSON.parse(req.body.message);
    var data = req.body.message;
    var message = JSON.stringify(data);
	console.log(message);
    client.publish('/RD/MESH/' + data.node_id + '/COMMAND', message);
    console.log("Published command to Mesh node: " + data.mac + " MODE "+data.mode);
    res.json(message);
});

Command -> mqtt.lua

Response -> mqtt.lua

Response -> API Gateway

Response -> CakePHP Controller