Last active
December 18, 2015 10:29
-
-
Save cat-haines/5769096 to your computer and use it in GitHub Desktop.
This is a collection of code snipper for the blog post at: beardedinventor.com/2013/06/12/Migrating-from-HTTP-Nodes-to-Agents/ These files are actually .nut files (Squirrel) - but we store them as .js for formatting and syntax highlighting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a class to control an LED from an input port | |
| class LedController extends InputPort { | |
| led = null; | |
| constructor(name, _led) { | |
| base(name); | |
| this.led = _led; | |
| } | |
| function set(value) { | |
| led.write(value); | |
| } | |
| }; | |
| hardware.pin1.configure(DIGITAL_OUT); | |
| ledController <- LedController("LED State", hardware.pin1); | |
| imp.configure("HTTP In Example", [ledController], []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http.onrequest(function(request, response) { | |
| // do things | |
| // send a response back.. | |
| response.send(200, "OK"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http.onrequest(function(request, response) { | |
| // check if the ledStatus is in the query parameters (?ledStatus=VALUE) | |
| if("ledStatus" in request.query) | |
| { | |
| // get the query parameter | |
| local state = request.query["ledStatus"]; | |
| server.log(state); // log it | |
| device.send("setLed", state); // pass it to the device | |
| } | |
| // send a response back.. | |
| response.send(200, "OK"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| led <- hardware.pin1; | |
| led.configure(DIGITAL_OUT); | |
| imp.configure("Agent Example", [], []); | |
| agent.on("setLed", function(data){ | |
| server.log("Setting LED to " + data); | |
| led.write(data); // we're assuming data will be 1 or 0 | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment