Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active September 21, 2017 09:29
Show Gist options
  • Save ElectricImpSampleCode/7e27074d3735cbe401e7cd159d96c4b4 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/7e27074d3735cbe401e7cd159d96c4b4 to your computer and use it in GitHub Desktop.
impCentral API Example Code: Listing Unassigned Devices
// CONSTANTS
// Replace the 'USERNAME' and 'PASSWORD' values with your own
// Replace the 'ACCESS_TOKEN' value with one retrieved using 'products.js' at
// https://gist.github.com/ElectricImpSampleCode/f0fb7f8bd04be009beeb5ee86dbd406a
const USERNAME = '...'
const PASSWORD = '...'
const ACCESS_TOKEN = '...'
const API_URL = 'api.electricimp.com'
const API_VER = '/v5/'
// GLOBALS
var https = require('https');
var devices = [];
// FUNCTIONS
function setOptions(verb, path, token) {
// Returns an HTTPS request options object primed for API usage
return {
hostname: API_URL,
path: API_VER + path,
method: verb,
headers: {
'Content-Type': 'application/vnd.api+json',
'Authorization': 'Bearer ' + token
}
};
}
function showError(resp) {
console.log(`STATUS: ${resp.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(resp.headers)}`);
}
function getUnassignedDevices() {
// Use the access token to request a list of all of the account's devices
let req = https.request(setOptions('GET', 'devices', ACCESS_TOKEN), (resp) => {
// Construct the returned response body
let body = '';
resp.setEncoding('utf8');
resp.on('data', (chunk) => { body += chunk; });
// Code that's called when the body is retrieved
resp.on('end', () => {
if (body.length > 0) {
try {
// Is the returned data valid JSON?
const data = JSON.parse(body);
if (resp.statusCode === 200) {
// 'data' is an array of device objects
for (let item of data.data) {
// Only store devices that are not assigned to a device group, ie.
// devices who do NOT have a 'devicegroup' entry in 'relationships'
if (!('devicegroup' in item.relationships)) {
let device = {};
device.id = item.id;
device.name = (item.attributes.name !== null ? item.attributes.name : item.id);
device.type = item.attributes.imp_type;
// Add device record to 'devices' array
devices.push(device);
}
}
// Print out the devices
if (devices.length > 0) {
console.log("Unassigned Devices:");
let dcount = 0;
for (let device of devices) {
console.log(` ${dcount + 1}. ${device.name} (ID: ${device.id}; Type: ${device.type})`);
dcount += 1;
}
} else {
console.log('There are no unassigned devices in your account');
}
} else {
// API Error
console.log('API ERROR: ' + data.code + ' (' + data.message + ')');
}
} catch (err) {
// JSON error
console.error(err.message);
}
} else {
// HTTP Error
showError(resp);
}
});
});
// Error callback
req.on('error', (err) => { console.error(`REQUEST ERROR: ${err.message}`); });
// Send the request
req.end();
}
// RUNTIME
getUnassignedDevices();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment