Last active
January 2, 2016 20:49
-
-
Save cat-haines/8359728 to your computer and use it in GitHub Desktop.
Code for head First into AT&T M2X blog post.
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
| /***** Include Hannah class here *****/ | |
| hannah <- Hannah(); | |
| function poll() { | |
| agent.send("temp", { temp = hannah.temp.get() }); | |
| imp.onidle(function() { server.sleepfor(30.0); }); | |
| } | |
| poll(); |
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
| function ProcessHttpResponse(resp) { | |
| server.log(format("HTTP Response: %i - %s", resp.statuscode, resp.body)); | |
| } | |
| device.on("temp", function(data) { | |
| local streamUrl = "http://api-m2x.att.com/v1/feeds/6e356b52ae812790f9b790ac0029774a/streams/temp"; | |
| local body = http.jsonencode({ "value": data.temp }); | |
| server.log(body); | |
| local headers = { | |
| "X-M2X-KEY": "YOUR_API_KEY", | |
| "Content-Type": "application/json" | |
| }; | |
| local request = http.put(streamUrl, headers, body); | |
| request.sendasync(ProcessHttpResponse); | |
| }); |
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
| class M2XFeed { | |
| _apiKey = null; | |
| _feedId = null; | |
| _baseUrl = null; | |
| _headers = null; | |
| constructor(apiKey, feedId) { | |
| _apiKey = apiKey; | |
| _feedId = feedId; | |
| _baseUrl = format("http://api-m2x.att.com/v1/feeds/%s", _feedId); | |
| _headers = { | |
| "X-M2X-KEY": _apiKey, | |
| "Content-Type": "application/json" | |
| }; | |
| } | |
| function push(streamName, value, callback = null) { | |
| if (callback == null) callback = _defaultCallback.bindenv(this); | |
| local streamUrl = format("%s/streams/%s", _baseUrl, streamName); | |
| local body = http.jsonencode({ "value": value }); | |
| http.put(streamUrl, _headers, body).sendasync(callback); | |
| } | |
| /********** Private Functions - don't call these **********/ | |
| function _defaultCallback(resp) { | |
| server.log(format("HTTP Response - %i: %s", resp.statuscode, resp.body)); | |
| } | |
| } | |
| officeFeed <- M2XFeed("YOUR_API_KEY", "6e356b52ae812790f9b790ac0029774a"); | |
| device.on("temp", function(data) { | |
| officeFeed.push("temp", data.temp); | |
| }); |
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
| function get() { | |
| local requestUrl = format("%s/streams", _baseUrl); | |
| local resp = http.get(requestUrl, _headers).sendsync(); | |
| local streams = {}; | |
| if (resp.statuscode != 200) { | |
| server.log(format("Error getting feed - %i: %s", resp.statuscode, resp.body)); | |
| return; | |
| } | |
| try { | |
| local data = http.jsondecode(resp.body); | |
| if ("streams" in data) { | |
| return data.streams; | |
| } else { | |
| server.log("Error getting feed - 'streams' not in response body.") | |
| return; | |
| } | |
| } catch(ex) { | |
| server.log(format("Error getting feed - %s", ex)); | |
| return; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment