Skip to content

Instantly share code, notes, and snippets.

@don
Created December 5, 2013 22:45
Show Gist options
  • Select an option

  • Save don/7815374 to your computer and use it in GitHub Desktop.

Select an option

Save don/7815374 to your computer and use it in GitHub Desktop.
Node script to poll the Ninja Blocks cloud server and send the information to The Thing System. For a Ninja Block Driver see https://github.com/don/ninja-thing
// Send Ninja Block Device info to The Thing System via TSRP
//
// This script polls the ninja cloud to fetch device information
//
// $ node ninja-thing.js
//
// (c) 2013 Don Coleman
/* jshint unused: vars, camelcase: false */
'use strict';
var http = require('https'),
dgram = require('dgram'),
startTime = new Date().getTime(),
requestId = 0,
scanInterval = 2 * 60 * 1000, // milliseconds
apiToken,
sensorNames;
// Get your API access token from the API tab of https://a.ninja.is/you
apiToken = 'Tn24WBCTJ9Co0BObXiCtfeooC9GbbSQxAriaBU97go';
// Adjust these names to match the GUIDs of your devices
sensorNames = {
'0101': 'Outside',
'0102': 'TV Room',
'0103': 'Kitchen',
'0104': 'Bedroom',
'0201': 'Office'
};
// get the ninja block information
// callback receives an array of ninja devices
var fetchDevices = function(callback) {
var options = {
host: 'a.ninja.is',
port: 443,
path: '/rest/v0/devices/?&user_access_token=' + apiToken,
json: true,
};
var request = http.get(options, function(response) {
var data = '';
response.on('data', function (chunk) {
data += chunk;
});
response.on('end', function(){
var json = JSON.parse(data);
// extract devices from the response
var devices = Object.keys(json.data).map(function(key) {
return json.data[key];
});
callback(devices);
});
});
request.on('error', function(e) {
console.log('Error getting devices from Ninja Block');
console.log(e);
});
request.end();
};
// Translate the NinjaBlock device info into TSRP Sensor Data
var toSensorData = function(device) {
var sensor = {};
sensor.name = sensorNames[device.gid] || device.gid;
sensor.status = 'present';
sensor.unit = {
serial: device.gid,
udn: device.gid
};
sensor.info = {};
sensor.info[device.device_type] = device.last_data.DA;
console.log(sensor.name + ' (' + device.gid + ') - ' + device.device_type + ' = ' + device.last_data.DA);
sensor.uptime = new Date().getTime() - startTime;
return sensor;
};
var createMessage = function(sensorData) {
requestId++;
var message = {};
message.path = '/api/v1/thing/reporting';
message.requestID = requestId.toString();
message.things = {
'/device/climate/arduino/sensor': { // TODO /device/climate/ninja/sensor
prototype: {
device: {
name: 'Ninja.Block.Temperature.and.Humidity.Sensor',
maker: 'Ninja.Blocks'
},
name: true,
status: ['present', 'absent', 'recent'],
properties: {
temperature: 'celsius',
humidity: 'percentage'
}
},
instances: [
sensorData
]
}
};
return message;
};
var sendMessage = function(message) {
// console.log(JSON.stringify(message, null, 2));
var buffer = new Buffer(JSON.stringify(message), 'UTF-8');
var client = dgram.createSocket('udp4');
client.send(buffer, 0, buffer.length, 22601, '224.192.32.20', function(err, bytes) {
if (err) {
console.log('ERROR ' + err);
}
client.close();
});
};
var sendReport = function(device) {
if (device.did === 30 || device.did === 31) { // humidity or temperature
var sensorData = toSensorData(device);
var message = createMessage(sensorData);
sendMessage(message);
}
};
var scan = function() {
fetchDevices(function(devices) {
devices.forEach(sendReport);
});
};
console.log('\nNinja Block bridge for The Thing System');
console.log('Scan Interval is ' + scanInterval/60.0/1000.0 + ' minutes\n');
scan();
setInterval(scan, scanInterval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment