Skip to content

Instantly share code, notes, and snippets.

@cat-haines
Last active August 29, 2015 13:59
Show Gist options
  • Save cat-haines/10999504 to your computer and use it in GitHub Desktop.
Save cat-haines/10999504 to your computer and use it in GitHub Desktop.
Code for the SkyNet blog post
data <- {
platform = "electric imp",
online = true,
token = "4e64e622-f456-44a6-af00-b996b6bea7a1",
type = "temp",
lat = 37.39674,
long = -122.10473
}
http.post(SKYNET_BASE + "devices", {}, http.urlencode(data)).sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
data <- {
token = "4e64e622-f456-44a6-af00-b996b6bea7a1",
}
http.request("DELETE", SKYNET_BASE + DEVICES + "/" + deviceId, { "Content-Type": "application/json" }, http.jsonencode(data)).sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
http.get(SKYNET_BASE + DATA + "/" + deviceId +"?token=" + "4e64e622-f456-44a6-af00-b996b6bea7a1").sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
deviceId <- "c61e9231-c65c-11e3-808c-e1edaeb5122d";
http.get(SKYNET_BASE + DEVICES + "/" + deviceId).sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
searchData <- {
platform = "electric imp"
}
http.get(SKYNET_BASE + DEVICES + "?" + http.urlencode(searchData)).sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
const DATA = "data";
local data <- {
token = "4e64e622-f456-44a6-af00-b996b6bea7a1",
temp = 103,
unit = "F"
}
http.post(SKYNET_BASE + DATA + "/" + deviceID, headers, http.jsonencode(data)).sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
const SKYNET_BASE = "https://skynet.im/"
http.get(SKYNET_BASE + "status").sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
function StreamData(autoReconnect, onDataCallback = null) {
// build the Url
local url = format("%s/%s/%s?token=%s&stream=true", SKYNET_BASE, DATA, deviceId, token);
server.log("Opening stream..");
local streamingRequest = http.get(url).sendasync(function(resp) {
// this function is executed whenever the stream is closed
server.log(format("Stream closed (%s - %s)", resp.statuscode.tostring(), resp.body));
if (autoReconnect) {
StreamData(true);
}
}.bindenv(this), function(data) {
// this function is executed whenever we get data through the stream
if (onDataCallback) {
try {
local d = http.jsondecode(data);
onDataCallback(d);
} catch (ex) {
server.log("Error in onData callback: " + ex);
server.log("data=" + data);
}
}
}.bindenv(this));
}
StreamData(true, function(d) {
// whenever we get data, log it:
server.log(http.jsonencode(d));
});
data <- {
token = "4e64e622-f456-44a6-af00-b996b6bea7a1",
x = 5
}
http.put(SKYNET_BASE + DEVICES + "/" + deviceId, { "Content-Type": "application/json" }, http.jsonencode(data)).sendasync(function(resp) {
server.log(resp.statuscode + ": " + resp.body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment