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 21:29] admin [Command -> mqtt.lua] |
technical:mqtt [2022/06/19 21:48] (current) admin |
||
|---|---|---|---|
| Line 83: | Line 83: | ||
| </ | </ | ||
| ==== Command -> mqtt.lua ==== | ==== 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. | * Depending on the type of command the code in Lua will determine the correct response. | ||
| * Here is a snippet in **/ | * 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> | <code lua> | ||
| --Here depending on the value of jsonStr[' | --Here depending on the value of jsonStr[' | ||
| Line 112: | Line 122: | ||
| </ | </ | ||
| - | ==== 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 **/ | ||
| + | <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 ==== | ==== 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-> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | )); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||