Created
April 17, 2015 14:47
-
-
Save gocreating/cf8197d33e4fa1f641d9 to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env node | |
var globalInstanceId; | |
var qs = require('querystring'); | |
var http = require("http"); | |
var server = http.createServer(function (request, response) { | |
console.log(request.url, request.remoteAddress); | |
if (request.method == 'POST') { | |
var body = ''; | |
request.on('data', function (data) { | |
body += data; | |
// Too much POST data, kill the connection! | |
if (body.length > 1e6) | |
request.connection.destroy(); | |
}); | |
request.on('end', function () { | |
var post = qs.parse(body); | |
console.log(post); | |
//sendLines('blablabla', [post]); | |
dIaClient.requestIndication('UPDATE', null, | |
gscl.dia.uri + '/applications/SmartMeter/containers/blablabla/contentInstances/'+globalInstanceId, { | |
content: { | |
$t: post, | |
contentType: 'application/json' | |
} | |
}).on('STATUS_CREATED', function (data) { | |
logger.info("CONTENTINSTANCE UPDATED"); | |
}); | |
}); | |
} | |
response.end('fuck!!!!!!!!!!!'); | |
}); | |
server.listen(8888); | |
var argv = require('optimist') | |
.usage('Usage: $0 --delay [num] --id [num]') | |
.argv; | |
var openmtc = require('openmtc'); | |
var XIA = openmtc.interfaces.xIa; | |
// maps primitives to http | |
var HttpClient = openmtc.transport.http.client; | |
var config = require("openmtc-client").getConfig(); | |
// configuration file for NSCL | |
var nscl = require('../settings/config').Hostscl; | |
// configuration file for GSCL | |
var gscl = config.scl; | |
// uuid | |
var uuid = require('node-uuid'); | |
// fs | |
var fs = require('fs'); | |
// logging | |
var log4js = config.log4js; | |
var logger = log4js.getLogger('[app]'); | |
var appID = "SmartMeter"; | |
var lazy = require("lazy"); | |
// create client for dIa interface | |
// generic communication module | |
httpHandler = new HttpClient(); | |
// dIa interface | |
dIaClient = new XIA(gscl.dia.uri, httpHandler); | |
// data | |
var data = []; | |
var registered = false; | |
var dataDIR = __dirname + '/../data'; | |
var delay, dataID; | |
function main() { | |
delay = parseFloat(argv.delay) || 0; | |
dataID = argv.id; | |
logger.debug(delay ? "Delay set to " + delay + "s." : "No delay set."); | |
delay *= 1000; | |
createAPP(); | |
} | |
function createAPP() { | |
/* register at GSCL */ | |
// create APPLICATION | |
dIaClient.requestIndication("CREATE", null, gscl.dia.uri + '/applications', { | |
application: { | |
appId: appID, | |
aPoC: "http://localhost:1334", | |
aPoCPaths: { | |
aPoCPath: [ | |
{ | |
path: "(/public);(/restricted)" | |
} | |
] | |
}, | |
announceTo: { sclList: { reference: [ nscl.uri ]} }, | |
searchStrings: { | |
searchString: ["SmartMeter", "GA", "Home"] | |
} | |
} | |
}).on('STATUS_CREATED', function (data) { | |
'use strict'; | |
logger.info("APP CREATED"); | |
registered = true; | |
//readData(); | |
buildContainer('blablabla'); | |
}).on("ERROR", function(data) { | |
if (data == 409) { | |
logger.info("App already exists. Assuming that's fine.'"); | |
registered = true; | |
readData(); | |
} | |
else | |
logger.error("Error creating app: " + data); | |
}); | |
}; | |
function sendLines(containerId, lines) { | |
if (lines.length == 0) | |
return; | |
sendData(containerId, lines.shift(), function() { | |
sendLines(containerId, lines); | |
}); | |
} | |
function buildContainer(containerID) { | |
// create CONTAINER for SmartMeter | |
dIaClient.requestIndication("CREATE", null, | |
gscl.dia.uri + '/applications/' + appID + '/containers', { | |
container: { | |
id: containerID | |
} | |
}).on('STATUS_CREATED', function (data) { | |
sendLines('blablabla', [{haha: 'llalala'}]); | |
logger.info("CONTAINER CREATED"); | |
//var i = 0; | |
//new lazy(fs.createReadStream(dataDIR + "/" + containerID)).lines/*.take(5)*/.map(buildJSON).join(function(lines) { | |
// sendLines(containerID, lines); | |
// }); | |
}); | |
} | |
function buildJSON(line) { | |
// reference | |
var day1 = "2009-01-01"; | |
logger.trace("Got line: " + line.toString()); | |
var codes = line.toString().split(" "); | |
logger.trace("Codes: " + codes); | |
/** codes[0] --> Five digit code: | |
* Day code: digits 1-3 (day 1 = 1st January 2009) | |
* Time code: digits 4-5 (1-48 for each 30 minutes with 1= 00:00:00 – 00:29:59) | |
* codes [1] : Electricity consumed during 30 minute interval (in kW) | |
*/ | |
var timestamp = parseInt(codes[0], 10); | |
var consumed = parseFloat(codes[1], 10); | |
/* | |
var day_code = codes[0].substr(0, 3); | |
var time_code = codes[0].substr(3, 2); | |
var timestamp = new Date(day1); | |
console.log(timestamp); | |
console.log(day_code); | |
timestamp.setDate(timestamp.getDate() + parseInt(day_code)); | |
timestamp.setMinutes(timestamp.getMinutes() + (time_code * 30)); | |
console.log(timestamp); | |
console.log(consumed); | |
*/ | |
var json_object = { 'data': { 'timestamp': timestamp, 'consumed': consumed} }; | |
console.log(json_object); | |
return json_object; | |
}; | |
/** | |
* read data and build json objects | |
*/ | |
function readData() { | |
if (dataID) { | |
logger.info("Reading data from " + dataDIR + "/" + dataID); | |
buildContainer(dataID); | |
return; | |
} | |
console.log("reading all data from " + dataDIR); | |
'use strict'; | |
var numFiles = fs.readdirSync(dataDIR).length; | |
console.log(numFiles + " files found"); | |
var x = 0; | |
fs.readdirSync(dataDIR).forEach(function (filename) { | |
if (++x > 2) | |
return; | |
console.log("reading " + x + " - " + dataDIR + "/" + filename); | |
buildContainer(filename); | |
}); | |
} | |
/** | |
* | |
* @param containerID | |
* @param data | |
*/ | |
function sendData(containerID, data, onSuccess) { | |
logger.debug("SmartMeter with ID:" + containerID + " pushing data: "); | |
logger.debug(data); | |
dIaClient.requestIndication('CREATE', null, | |
gscl.dia.uri + '/applications/' + appID + '/containers/' + | |
containerID + '/contentInstances', { | |
contentInstance: { | |
content: { | |
$t: new Buffer(JSON.stringify(data)).toString('base64'), | |
contentType: 'application/json' | |
} | |
} | |
}).on('STATUS_CREATED', function (data) { | |
console.log('============================='); | |
var s = data.split('/'); | |
var instanceId = s[s.length - 1]; | |
console.log(instanceId); | |
globalInstanceId = instanceId; | |
console.log('============================='); | |
logger.info("CONTENTINSTANCE CREATED"); | |
if (delay) { | |
logger.debug("Sleeping " + delay + "ms."); | |
setTimeout(onSuccess, delay); | |
} else | |
onSuccess(); | |
}); // create contentInstance | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment