This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
technical:mqtt [2022/06/19 20:58] admin [API Gateway] |
technical:mqtt [2022/06/19 21:48] (current) admin |
||
|---|---|---|---|
| Line 43: | Line 43: | ||
| ===== Looking at the code ===== | ===== Looking at the code ===== | ||
| + | ==== Command -> CakePHP Controller ==== | ||
| + | * Lets look at the **/ | ||
| + | * When an action is added to a node and MQTT is enabled on the system this code is executed: | ||
| + | <code php> | ||
| + | if ($cfg[' | ||
| + | //Talk to MQTT Broker | ||
| + | $data = $this-> | ||
| + | | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | | ||
| + | try { | ||
| + | | ||
| + | } catch (\Exception $e) { | ||
| + | // Do Nothing | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ==== Command -> API Gateway ==== | ||
| + | * The API call to the API Gateway will execute this piece of code in the **/ | ||
| + | <code javascript> | ||
| + | router.post('/ | ||
| + | //var data = JSON.parse(req.body.message); | ||
| + | var data = req.body.message; | ||
| + | var message = JSON.stringify(data); | ||
| + | console.log(message); | ||
| + | client.publish('/ | ||
| + | console.log(" | ||
| + | res.json(message); | ||
| + | }); | ||
| + | </ | ||
| + | ==== Command -> mqtt.lua ==== | ||
| + | * Here is a snippet in **/ | ||
| + | * This is the command which it will then respond to. | ||
| + | <code lua> | ||
| + | client.ON_MESSAGE = function(mid, | ||
| + | -- Parse/ | ||
| + | local jsonStr | ||
| + | -- Check if message belongs to us (MAC Address) | ||
| + | </ | ||
| + | ==== Response -> mqtt.lua ==== | ||
| + | * Depending on the type of command the code in Lua will determine the correct response. | ||
| + | * Here is a snippet in **/ | ||
| + | * This is part of the code which are processing the command that the mesh node or access point received (inside the **ON_MESSAGE** event) | ||
| + | <code lua> | ||
| + | --Here depending on the value of jsonStr[' | ||
| + | if(jsonStr[' | ||
| + | print(" | ||
| + | if(mode == ' | ||
| + | message = luci_json.stringify({mode=mode, | ||
| + | end | ||
| + | if(mode == ' | ||
| + | message = luci_json.stringify({mode=mode, | ||
| + | end | ||
| + | | ||
| + | local cl_execute = mqtt.new(); | ||
| + | cl_execute: | ||
| + | cl_execute: | ||
| + | --Connected now publish | ||
| + | cl_execute.ON_CONNECT = function() | ||
| + | cl_execute: | ||
| + | end | ||
| + | --Done publishing - now execute command | ||
| + | cl_execute.ON_PUBLISH = function() | ||
| + | cl_execute: | ||
| + | os.execute(jsonStr[' | ||
| + | end | ||
| + | cl_execute: | ||
| + | end | ||
| + | |||
| + | </ | ||
| + | ==== 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 **/ | ||
| + | <code javascript> | ||
| + | default: | ||
| + | request.put({ | ||
| + | url: mesh_controller + '/ | ||
| + | form: data | ||
| + | }, | ||
| + | function (err, res, body) { | ||
| + | if (err) { | ||
| + | console.error(' | ||
| + | } | ||
| + | |||
| + | console.log(body); | ||
| + | } | ||
| + | ); | ||
| + | break; | ||
| + | </ | ||
| + | ==== 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 **/ | ||
| + | <code php> | ||
| + | //--This comes from the NodeJS API Gateway Application in response to ' | ||
| + | //--This comes from the NodeJS API Gateway Application in FIRST response to ' | ||
| + | public function nodeCommand(){ | ||
| + | |||
| + | if($this-> | ||
| + | $data = $this-> | ||
| + | if((!empty($data[' | ||
| + | // update command status to fetched | ||
| + | $model = ' | ||
| + | if($data[' | ||
| + | $model = ' | ||
| + | } | ||
| + | | ||
| + | $entity | ||
| + | if($entity){ | ||
| + | $entity-> | ||
| + | $this-> | ||
| + | } | ||
| + | |||
| + | $this-> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | )); | ||
| + | } else { | ||
| + | $this-> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | )); | ||
| + | } | ||
| + | |||
| + | } else { | ||
| + | $this-> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | )); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||