reconnect
This commit is contained in:
parent
52344b9413
commit
d79e95dfeb
@ -6,9 +6,23 @@ var Tinkerforge = require('../Tinkerforge')
|
|||||||
|
|
||||||
|
|
||||||
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 HOST = item.host;
|
||||||
var PORT = item.port;
|
var PORT = item.port;
|
||||||
var UID = item.uid;
|
var UID = item.uid;
|
||||||
|
|
||||||
|
Log.log('Init ' + UID + ' @' + HOST);
|
||||||
|
|
||||||
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
|
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
|
||||||
var md = new Tinkerforge.BrickletMotionDetector(UID, ipcon); // Create device object
|
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_FUNCTION_NOT_SUPPORTED = 42
|
||||||
IPConnection.ERROR_UNKNOWN_ERROR = 43
|
IPConnection.ERROR_UNKNOWN_ERROR = 43
|
||||||
*/
|
*/
|
||||||
Log.log('Error: ' + error);
|
|
||||||
|
Log.error('Error @'+HOST+': ' + error);
|
||||||
|
|
||||||
|
if(error === 13) {
|
||||||
|
reconnect(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
); // Connect to brickd
|
); // Connect to brickd
|
||||||
// Don't use device before ipcon is connected
|
// Don't use device before ipcon is connected
|
||||||
@ -55,8 +74,7 @@ Helper.each(Conf.items, function(item) {
|
|||||||
ipcon.disconnect();
|
ipcon.disconnect();
|
||||||
process.exit( );
|
process.exit( );
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
/* private */
|
/* private */
|
||||||
function doMotionCall(options) {
|
function doMotionCall(options) {
|
||||||
@ -70,7 +88,7 @@ function doMotionCall(options) {
|
|||||||
|
|
||||||
//the whole response has been recieved, so we just print it out here
|
//the whole response has been recieved, so we just print it out here
|
||||||
response.on('end', function () {
|
response.on('end', function () {
|
||||||
Log.log(str);
|
Log.log(HOST + ': ' + str);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +97,7 @@ function doMotionCall(options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', function(err) {
|
req.on('error', function(err) {
|
||||||
Log.error(err);
|
Log.error(HOST + ': ' + err);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
|
|||||||
@ -5,12 +5,24 @@ var Tinkerforge = require('../Tinkerforge')
|
|||||||
, http = require('http');
|
, 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 HOST = item.host;
|
||||||
var PORT = item.port;
|
var PORT = item.port;
|
||||||
var UID = item.uid;
|
var UID = item.uid;
|
||||||
|
|
||||||
Log.log('Init ' + UID);
|
Log.log('Init ' + UID + ' @' + HOST);
|
||||||
|
|
||||||
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
|
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
|
||||||
var dus = new Tinkerforge.BrickletDistanceUS(UID, ipcon); // Create device object
|
var dus = new Tinkerforge.BrickletDistanceUS(UID, ipcon); // Create device object
|
||||||
@ -20,7 +32,23 @@ Helper.each(Conf.items, function(item) {
|
|||||||
|
|
||||||
ipcon.connect(HOST, PORT,
|
ipcon.connect(HOST, PORT,
|
||||||
function (error) {
|
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
|
); // Connect to brickd
|
||||||
// Don't use device before ipcon is connected
|
// Don't use device before ipcon is connected
|
||||||
@ -61,8 +89,7 @@ Helper.each(Conf.items, function(item) {
|
|||||||
ipcon.disconnect();
|
ipcon.disconnect();
|
||||||
process.exit( );
|
process.exit( );
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
/* private */
|
/* private */
|
||||||
function doHoBuDoorBellCall(options) {
|
function doHoBuDoorBellCall(options) {
|
||||||
@ -76,7 +103,7 @@ function doHoBuDoorBellCall(options) {
|
|||||||
|
|
||||||
//the whole response has been recieved, so we just print it out here
|
//the whole response has been recieved, so we just print it out here
|
||||||
response.on('end', function () {
|
response.on('end', function () {
|
||||||
Log.log(str);
|
Log.log(HOST + ': ' + str);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +112,7 @@ function doHoBuDoorBellCall(options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', function(err) {
|
req.on('error', function(err) {
|
||||||
Log.error(err);
|
Log.error(HOST + ': ' + err);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user