Created
July 30, 2014 15:01
-
-
Save imp-guru/ff8e3bea02a3f00f7211 to your computer and use it in GitHub Desktop.
Create a REST API for Electric Imp using Blocking device.on()
This file contains 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
// ----------------------Agent Code----------------------- | |
function requestHandler(request, response) { | |
try { | |
// Send a read request to the device | |
device.send("getRequest", request); | |
// Blocking wait to receive response from the imp | |
device.on("getResponse", function(data) { | |
response.send(200,data); | |
}); | |
} catch (ex) { | |
response.send(500, "Internal Server Error: " + ex); | |
} | |
} | |
// register the HTTP handler | |
http.onrequest(requestHandler); | |
// ----------------------Device Code---------------------- | |
// Define test response data | |
local responseData = "Hello" | |
function getRequest(params) { | |
agent.send("getResponse", responseData) | |
} | |
agent.on("getRequest", getRequest); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment