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:23] admin [Command -> API Gateway] |
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 ==== | ==== 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 ==== | ==== 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-> | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | )); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||