RADIUSdesk

logo

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
technical:mqtt [2022/06/19 21:35]
admin
technical:mqtt [2022/06/19 21:48] (current)
admin
Line 122: Line 122:
  
 </code> </code>
-==== Response -> mqtt.lua ==== 
 ==== Response -> API Gateway ==== ==== Response -> API Gateway ====
 +  * The API Gateway subscribe to the topic which the mesh node or access point publishes to.
 +  * Here is a snippet from the **/opt/Rdcore-API-Gateway/routes/rdmesh.js** file that execute some code when a message is received on that topic
 +<code javascript>
 +default:
 +    request.put({
 +            url: mesh_controller + '/cake3/rd_cake/node-actions/node-command.json',
 +            form: data
 +        },
 +        function (err, res, body) {
 +            if (err) {
 +                console.error('Error Occurred: ' + err);
 +            }
 +
 +            console.log(body);
 +        }
 +    );
 +    break;
 +</code>
 ==== Response -> CakePHP Controller ==== ==== Response -> CakePHP Controller ====
 +  * Finally we can look at the CakePHP code that process the response so our system know and can indicate the mesh node or access point did receive the instruction.
 +  * Lets look at the **/var/www/html/cake3/rd_cake/src/Controller/NodeActionsController.php** file.
 +<code php>
 +//--This comes from the NodeJS API Gateway Application in response to 'execute' type node_actions
 +//--This comes from the NodeJS API Gateway Application in FIRST response to 'execute_and_reply' type node_actions
 +public function nodeCommand(){
 +
 +    if($this->request->is('put')){
 +        $data = $this->request->data;
 +        if((!empty($data['node_id']))||(!empty($data['ap_id']))){
 +            // update command status to fetched
 +            $model = 'NodeActions';
 +            if($data['mode'] == 'ap'){
 +                $model = 'ApActions';
 +            }
 +            
 +            $entity  = $this->{$model}->find()->where(['id' => $data['cmd_id']])->first();
 +            if($entity){
 +                $entity->status = 'fetched';
 +                $this->{$model}->save($entity);
 +            }
 +
 +            $this->set(array(
 +                'data'          => $data,
 +                'success'       => true,
 +                '_serialize'    => array('data','success')
 +            ));
 +        } else {
 +            $this->set(array(
 +                'message'     => 'Node ID not found',
 +                'success'       => false,
 +                '_serialize'    => array('message','success')
 +            ));
 +        }
 +
 +    } else {
 +        $this->set(array(
 +            'message'         => 'Send only PUT request',
 +            'success'       => false,
 +            '_serialize'    => array('message','success')
 +        ));
 +    }
 +}
 +</code>
 +