Skip to content

Instantly share code, notes, and snippets.

@futhr
Last active May 3, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futhr/9b82c2bca0b548dfd86f1940f9d86967 to your computer and use it in GitHub Desktop.
Save futhr/9b82c2bca0b548dfd86f1940f9d86967 to your computer and use it in GitHub Desktop.
myDevices Cloud API sample code.
//----------------------------------
// myDevices Cloud API sample code
//----------------------------------
// Use rather request than https
const Http = require('https');
const Util = require('util');
const API_HOST = 'va-staging-corridor.mydevices.com';
const API_PORT = 443;
const API_PATH = '/provider/example';
const ACCESS_TOKEN = 'eW91ciBhY2Nlc3NfdG9rZW4gaGVyZQ==';
const REFRESH_TOKEN = 'eW91ciByZWZyZXNoX3Rva2VuIGhlcmU=';
const LIGHT_UID = 'Device_003';
// Sends a request
function sendRequest(accessToken, verb, path, body, callback) {
const options = {
hostname: API_HOST,
port: API_PORT,
path: API_PATH + path,
method: verb,
headers: {}
};
if (accessToken) {
options.headers['Authorization'] = 'Bearer ' + accessToken;
}
if (body) {
options.headers['Content-Type'] = 'application/json';
options.headers['Content-Length'] = Buffer.byteLength(body);
}
const req = Http.request(options, function(res) {
res.setEncoding('utf8');
var data = '';
res.on('data', function(d) {
data += d;
});
res.on('end', function() {
if (res.statusCode < 200 || res.statusCode > 299) {
console.log('HTTP error ' + res.statusCode + ': ' + Util.inspect(data));
return callback(new Error('HTTP error ' + res.statusCode + ': ' + Util.inspect(data)));
} else {
var json = null;
try {
json = JSON.parse(data);
} catch (error) {
// console.log(error);
}
callback(null, json);
}
});
});
req.on('error', function(error) {
console.log(error.message + ' ' + Util.inspect(error.data));
callback(error);
});
if (body) req.write(body);
req.end();
}
// Obtain a new access token from a refresh token
function refreshToken(refresh_token, callback) {
sendRequest(undefined, 'POST', '/refreshToken', {
refresh_token: refresh_token
}, function(error, access_token) {
callback(error, access_token);
});
}
// Get a list of devices
function getDevices(callback) {
sendRequest(ACCESS_TOKEN, 'GET', '/devices', undefined, function(error, devices) {
callback(error, devices);
});
}
// Get data from an individual device
function getData(device_uid, callback) {
sendRequest(ACCESS_TOKEN, 'GET', '/devices/' + device_uid + '/data', undefined, function(error, data) {
callback(error, data);
});
}
// Get a list of commands supported by a device
function getCommands(device_uid, callback) {
sendRequest(ACCESS_TOKEN, 'GET', '/devices/' + device_uid + '/commands', undefined, function(error, commands) {
callback(error, commands);
});
}
// Execute a command on a device
function sendCommand(device_uid, command, callback) {
sendRequest(ACCESS_TOKEN, 'POST', '/devices/' + device_uid + '/sendCommand', JSON.stringify(command), function(error, result) {
callback(error, result);
});
}
// Demo: Display device's data and commands
function demoDevicesDataAndCommands(devices, idx, callback) {
if (idx < devices.length) {
getData(devices[idx].uid, function(error, data) {
getCommands(devices[idx].uid, function(error, commands) {
console.log('Device: ' + Util.inspect(devices[idx]));
if (data) console.log('Data: ' + Util.inspect(data));
if (commands) {
console.log('Commands: ' + Util.inspect(commands));
}
console.log('');
demoDevicesDataAndCommands(devices, ++idx, callback);
});
});
} else {
callback();
}
}
// Demo: Toggle light state
function demoToggleLightStateOnOff(callback) {
sendCommand(LIGHT_UID, {
channel: 'cmd.setlightstate.onoff',
params: {
on: false
}
}, function(error) {
getData(LIGHT_UID, function(error, data) {
console.log('Data before: ' + Util.inspect(data));
sendCommand(LIGHT_UID, {
channel: 'cmd.setlightstate.onoff',
params: {
on: true
}
}, function(error) {
getData(LIGHT_UID, function(error, data) {
console.log('Data after: ' + Util.inspect(data));
callback();
});
});
});
});
}
// main function
function main() {
getDevices(function(error, devices) {
if (devices) {
console.log('******** demoDevicesDataAndCommands ********');
demoDevicesDataAndCommands(devices, 0, function() {
console.log('******** demoToggleLightStateOnOff ********');
demoToggleLightStateOnOff(function() {});
});
}
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment