Gerrit Linnemann 971495b393 HCBASE-93
Error code overview:

IPConnection.ERROR_ALREADY_CONNECTED = 11
IPConnection.ERROR_NOT_CONNECTED = 12
IPConnection.ERROR_CONNECT_FAILED = 13
IPConnection.ERROR_INVALID_FUNCTION_ID = 21
IPConnection.ERROR_TIMEOUT = 31
IPConnection.ERROR_INVALID_PARAMETER = 41
IPConnection.ERROR_FUNCTION_NOT_SUPPORTED = 42
IPConnection.ERROR_UNKNOWN_ERROR = 43

http://www.tinkerforge.com/de/doc/Software/Bricklets/MotionDetector_Bricklet_JavaScript.html
https://www.npmjs.com/package/nodejs-websocket
https://devcenter.heroku.com/articles/node-websockets
2016-06-08 18:08:00 +02:00

97 lines
2.4 KiB
JavaScript

var Tinkerforge = require('../Tinkerforge')
, Conf = require('./config.json')
, Helper = require('../Adawim/helper')
, Log = require('../Adawim/logging')
, http = require('http')
, ws = require("nodejs-websocket");
Helper.each(Conf.items, function(item) {
var HOST = item.host;
var PORT = item.port;
var UID = item.uid;
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var md = new Tinkerforge.BrickletMotionDetector(UID, ipcon); // Create device object
var counter = 0;
var readyForNextNotification = true;
ipcon.connect(HOST, PORT,
function (error) {
/*
IPConnection.ERROR_ALREADY_CONNECTED = 11
IPConnection.ERROR_NOT_CONNECTED = 12
IPConnection.ERROR_CONNECT_FAILED = 13
IPConnection.ERROR_INVALID_FUNCTION_ID = 21
IPConnection.ERROR_TIMEOUT = 31
IPConnection.ERROR_INVALID_PARAMETER = 41
IPConnection.ERROR_FUNCTION_NOT_SUPPORTED = 42
IPConnection.ERROR_UNKNOWN_ERROR = 43
*/
Log.log('Error: ' + error);
}
); // Connect to brickd
// Don't use device before ipcon is connected
ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
function (connectReason) {
}
);
// Register motion detected callback
md.on(Tinkerforge.BrickletMotionDetector.CALLBACK_MOTION_DETECTED,
// Callback function for motion detected callback
function () {
Log.log('Motion detected');
sendToHoBu("ping");
}
);
process.on( 'SIGINT', function() {
Log.log( "Gracefully disconnect " + HOST );
ipcon.disconnect();
process.exit( );
});
});
/* private */
function doHoBuDoorBellCall(options) {
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
Log.log(str);
});
}
http.request(options, callback).end();
}
/* private */
function sendToHoBu(data) {
Log.debug('Send data to ' + Conf.hobu.webservice);
try {
ws.connect(Conf.hobu.webservice, null, function(conn) {
if(Helper.isDefinedAndNotNull(conn)) {
conn.send(data, null);
} else {
Log.error('Connection is NULL.');
}
//TODO: disconnect!!
});
} catch(err) {
Log.error(err.message);
}
}