s2chttp
Version:
HTTP Library for pushing data to the cloud using s2c.io plaform for the internet of things
247 lines (233 loc) • 7.71 kB
JavaScript
/*
Jan 2nd, 2015
S2C.io HTTP Library for Beaglebone Black
*/
var fs = require('fs');
var http = require('http');
var os = require('os');
var ver = 20;
var version = '1.0.'+ver;
var gateway = new gateway('mybbb','','0'); // global variable gateway
// startup
console.log('----------------------------------')
console.log('- S2C.io HTTP v '+version+' -')
console.log('----------------------------------')
function Create(name)
{
gateway.name=name;
return gateway;
}
// Name Pin# Pin# Name
// - - 32 Vdd_ADC
// AIN4 33 34 GND_ADC
// AIN6 35 36 AIN5
// AIN2 37 38 AIN3
// AIN0 39 40 AIN1
// callback functions
function gateway(name,macaddr,uptime)
{
this.macaddr = macaddr;
this.name = name;
this.uptime = uptime;
this.localIP = '0.0.0.0';
this.API_KEY = 'ABDCD'
this.version = version;
this.Throttle = 30;
this.Tolerance = 50; // %
this.Tick = 5000;
this.t0 = (new Date()).valueOf();
this.Timer = 0;
this.connect = false;
this.sensorList = []; // array that contains the sensors management
this.SensorDataList = []; // array that contains the sensors data ONLY no management
this.sensorsNames = [];
this.ValueList = [];
this.InternalClk = (new Date()).valueOf();
this.DeltaClk = 1000000;
this.SetChannel = function(sensorname,channelname,value,time,error)
{
var s = this.sensorsNames.indexOf(sensorname);
var c = this.sensorList[s].channelNames.indexOf(channelname);
if(s != -1 && c != -1)
{
this.sensorList[s].channels[c].value = value;
this.sensorList[s].channels[c].time = time;
//this.sensorList[s].channels[c].error = error;
this.SensorDataList[s].channels[c].value = value;
this.SensorDataList[s].channels[c].time = time;
//console.log('Set Channel /'+sensorname+'/'+channelname+' to value = '+value+' at time = '+time);
}
}
this.publish = function() // send the data to the cloud
{
var gatpost = {}
var push = false;
var macaddr = this.macaddr;
var API_KEY = this.API_KEY;
this.Timer++;
this.uptime = RND(((new Date()).valueOf() - this.t0)/60000,1); // in minutes
gatpost.ip = this.localIP;
gatpost.uptime = this.uptime;
gatpost.version = this.version
gatpost.macaddr = macaddr;
gatpost.sensorList = this.SensorDataList; // need only the data for push
var data = JSON.stringify(gatpost);
var cnt = 0;
var PerC = 0;
for (var s=0; s<this.sensorList.length; s++)
{
for(var c=0;c<this.sensorList[s].channels.length;c++)
{
var nval = this.sensorList[s].channels[c].value;
if(cnt>=this.ValueList.length) { this.ValueList.push(nval); } // handle the case when ValueList is empty
else {
var oldval = this.ValueList[cnt];
this.ValueList[cnt] = nval;
if (oldval != 0) { PerC = Math.round(Math.abs(100*(nval-oldval)/oldval)); }
if (PerC>this.Tolerance)
{
console.log('old val = '+oldval+' val = '+nval+' PerC = '+PerC);
push = true;
}
}
cnt++;
}
}
if(push && (this.DeltaClk>=this.Throttle*100))
{
console.log('PUSH '+this.ValueList);
console.log('DeltaClk = '+this.DeltaClk);
this.InternalClk = (new Date()).valueOf();
console.log(JSON.stringify(gatpost));
POST(data,macaddr,API_KEY,post_callback);
}
else if(this.DeltaClk>=this.Throttle*1000)
{
this.InternalClk = (new Date()).valueOf();
console.log('DeltaClk = '+this.DeltaClk);
console.log('PERIODIC = '+this.ValueList);
console.log(JSON.stringify(gatpost));
POST(data,macaddr,API_KEY,post_callback);
}
this.DeltaClk = (new Date()).valueOf() - this.InternalClk;
}
this.AddSensor = function(sensor)
{
// 1. make sure the name is unique
for (var jk=0; jk<this.sensorList.length; jk++) { this.sensorsNames.push(this.sensorList[jk].name)}
var index = this.sensorsNames.indexOf(sensor.name);
if(index == -1) { this.sensorList.push(sensor) }
else
{
throw("Error: make sure the sensor name '"+sensor.name+"' is unique");
}
// 2. sort the channels by name
this.sensorsNames = [];
for (var jk=0; jk<this.sensorList.length; jk++) { this.sensorsNames.push(this.sensorList[jk].name)}
this.sensorsNames.sort();
console.log(this.sensorsNames)
// 3. Rearrange channels
var temp = [];
for (var jk=0; jk<this.sensorList.length; jk++) { temp.push(this.sensorList[jk])}
this.sensorList = [];
this.SensorDataList = [];
//console.log('temp = '+JSON.stringify(temp));
for (item in this.sensorsNames)
{
for(var jk=0; jk<temp.length; jk++)
{
if(temp[jk].name==this.sensorsNames[item])
{
this.sensorList.push(temp[jk])
var sensdata = new sensorData(temp[jk].name,temp[jk].type,temp[jk].id,temp[jk].channels);
this.SensorDataList.push(sensdata);
}
}
}
}
}
// Template Sensor Class
function sensorData(name,type,id,channels)
{
this.name = name;
this.type = type;
this.id = id;
this.channels = channels;
}
function sensor(name,type,id)
{
this.name = name;
this.type = type;
this.id = id;
this.channels = [];
this.channelNames = []; // array that contains only the channel names
this.AddChannel = function(name,value,unit)
{
// 1. make sure the name is unique
var index = this.channelNames.indexOf(name);
if(index == -1)
{
var nsensor = new channel(name,0,unit,0,'');
this.channels.push(nsensor);
this.channelNames.push(name);
console.log('New Channel Created '+name);
}
else
{
throw("Error: make sure the channel name '"+name+"' is unique");
}
// 2. sort the channels by name
this.channelNames.sort();
//console.log(this.channelNames);
// 3. Rearrange channels
var temp = [];
for (var jk=0; jk<this.channels.length; jk++) { temp.push(this.channels[jk])}
this.channels = [];
//console.log('temp = '+JSON.stringify(temp));
for (item in this.channelNames) {for (var jk=0; jk<temp.length; jk++){if (temp[jk].name == this.channelNames[item]){this.channels.push(temp[jk])}}}
}
console.log('New Sensor Created '+this.name);
}
// Channel Class
function channel(name,value,unit)
{
this.name = name;
this.value = value;
this.unit = unit;
this.time = 0;
//this.error = '';
}
function POST(data,macaddr,API_KEY,post_callback)
{
var http = require("http");
var options =
{
hostname: 'developer.sensors2cloud.com',
port: 80,
path: '/arduino',
method: 'POST',
headers: {'Content-Type': 'application/json','Content-Length': data.length,'macaddr':macaddr,'securekey':API_KEY}
};
var req = http.request(options, function(res)
{
res.setEncoding('utf8');
res.on('data', function (body) { post_callback(body); });
});
req.on('error', function(e) { post_callback(e.message); });
// write data to request body
req.write(data);
req.end();
}
function post_callback(resp)
{
console.log('POST response '+resp);
}
// Useful functions
function RND(x,d) {return Math.round(x*Math.pow(10,d))/Math.pow(10,d);}
// modules exports here
module.exports = {
Create: Create,
RND: RND,
sensor:sensor,
channel:channel,
}