Last active
December 14, 2015 00:19
-
-
Save thatguydan/4998419 to your computer and use it in GitHub Desktop.
On{X} NFC Rule
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
var ip = 'api.ninja.is'; // Can also be the IP address of the machine running the client code | |
var ACCESS_TOKEN='API_ACCESS_TOKEN'; // Obtained from https://a.ninja.is/hacking | |
var port = 443; // Should be 8000 if talking locally | |
var NODE = "F9C342600F237BBE"; // In your client code terminal | |
var G = '01'; | |
var V = 0; | |
var D = 1007; | |
//Register for NFC event to handle discovered tag. | |
device.nfc.on("found", handleNfcTag); | |
function handleNfcTag(tag) { | |
var firstRecord = tag.messages[0].records[0]; | |
changeColour(firstRecord.text); | |
} | |
function changeColour(colour) { | |
var guid = [NODE,G,V,D].join('_'); | |
var payload = JSON.stringify({ DA:colour }); | |
var opts = { | |
url: 'http://'+ip+':'+port+'/rest/v0/device/'+guid+'?user_access_token='+ACCESS_TOKEN, | |
type: 'POST', | |
data: payload, | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}; | |
console.log(opts.url); | |
console.log(opts.data); | |
console.log("Attempting to change to ",colour) | |
device.ajax(opts, | |
function onSuccess(body, textStatus, response) { | |
var parsedBody; | |
if(!(body && (parsedBody = JSON.parse(body)))) { | |
var error = {}; | |
error.message = 'invalid body format'; | |
error.content = body; | |
console.error('error: ',error); | |
} | |
console.info('successfully received http response!'); | |
notification.content = 'successfully received http response!'; | |
notification.show(); | |
}, | |
function onError(textStatus, response) { | |
var error = {}; | |
error.message = textStatus; | |
error.statusCode = response.status; | |
console.error('error: ',error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment