reconnect

This commit is contained in:
Gerrit Linnemann 2016-07-07 22:11:13 +02:00
parent 52344b9413
commit d79e95dfeb
2 changed files with 58 additions and 13 deletions

View File

@ -6,9 +6,23 @@ var Tinkerforge = require('../Tinkerforge')
Helper.each(Conf.items, function(item) {
connect(item);
});
/* private */
function reconnect(item) {
Log.debug('Reconnect ' + item.host);
setTimeout(connect(item), 10000);
}
/* private */
function connect(item) {
var HOST = item.host;
var PORT = item.port;
var UID = item.uid;
Log.log('Init ' + UID + ' @' + HOST);
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var md = new Tinkerforge.BrickletMotionDetector(UID, ipcon); // Create device object
@ -29,7 +43,12 @@ Helper.each(Conf.items, function(item) {
IPConnection.ERROR_FUNCTION_NOT_SUPPORTED = 42
IPConnection.ERROR_UNKNOWN_ERROR = 43
*/
Log.log('Error: ' + error);
Log.error('Error @'+HOST+': ' + error);
if(error === 13) {
reconnect(item);
}
}
); // Connect to brickd
// Don't use device before ipcon is connected
@ -55,8 +74,7 @@ Helper.each(Conf.items, function(item) {
ipcon.disconnect();
process.exit( );
});
});
}
/* private */
function doMotionCall(options) {
@ -70,7 +88,7 @@ function doMotionCall(options) {
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
Log.log(str);
Log.log(HOST + ': ' + str);
});
}
@ -79,7 +97,7 @@ function doMotionCall(options) {
});
req.on('error', function(err) {
Log.error(err);
Log.error(HOST + ': ' + err);
});
req.end();

View File

@ -5,12 +5,24 @@ var Tinkerforge = require('../Tinkerforge')
, http = require('http');
Helper.each(Conf.items, function(item) {
Helper.each(Conf.items, function(item) {
connect(item);
});
/* private */
function reconnect(item) {
Log.debug('Reconnect ' + item.host);
setTimeout(connect(item), 10000);
}
/* private */
function connect(item) {
var HOST = item.host;
var PORT = item.port;
var UID = item.uid;
Log.log('Init ' + UID);
Log.log('Init ' + UID + ' @' + HOST);
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var dus = new Tinkerforge.BrickletDistanceUS(UID, ipcon); // Create device object
@ -20,7 +32,23 @@ Helper.each(Conf.items, function(item) {
ipcon.connect(HOST, PORT,
function (error) {
Log.log('Error: ' + 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.error('Error @'+HOST+': ' + error);
if(error === 13) {
reconnect(item);
}
}
); // Connect to brickd
// Don't use device before ipcon is connected
@ -61,8 +89,7 @@ Helper.each(Conf.items, function(item) {
ipcon.disconnect();
process.exit( );
});
});
}
/* private */
function doHoBuDoorBellCall(options) {
@ -76,7 +103,7 @@ function doHoBuDoorBellCall(options) {
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
Log.log(str);
Log.log(HOST + ': ' + str);
});
}
@ -85,7 +112,7 @@ function doHoBuDoorBellCall(options) {
});
req.on('error', function(err) {
Log.error(err);
Log.error(HOST + ': ' + err);
});
req.end();