From 53dee34ffc2e5e69b24d1830db0b1d5eacd57534 Mon Sep 17 00:00:00 2001 From: Gerrit Linnemann Date: Thu, 10 Nov 2016 21:18:58 +0100 Subject: [PATCH] distance ir --- hobu_distance_ir_io/app.js | 117 ++++++++++++++++++++++++++++++++ hobu_distance_ir_io/config.json | 25 +++++++ 2 files changed, 142 insertions(+) create mode 100644 hobu_distance_ir_io/app.js create mode 100644 hobu_distance_ir_io/config.json diff --git a/hobu_distance_ir_io/app.js b/hobu_distance_ir_io/app.js new file mode 100644 index 0000000..93c1124 --- /dev/null +++ b/hobu_distance_ir_io/app.js @@ -0,0 +1,117 @@ +var Tinkerforge = require('../Tinkerforge') +, Conf = require('./config.json') +, Helper = require('../Adawim/helper') +, Log = require('../Adawim/logging') +, http = require('http'); + + +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 dus = new Tinkerforge.BrickletDistanceIR(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.error('Error @'+HOST+': ' + error); + + if(error === 13) { + reconnect(item); + } + } + ); // Connect to brickd + // Don't use device before ipcon is connected + + ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED, + function (connectReason) { + // Set period for distance value callback to 0.2s (200ms) + // Note: The distance value callback is only called every 0.2 seconds + // if the distance value has changed since the last call! + dus.setDistanceCallbackPeriod(5000); + } + ); + + // Register distance value callback + dus.on(Tinkerforge.BrickletDistanceIR.CALLBACK_DISTANCE, + // Callback function for distance value callback + function (distance) { + Log.log('Distance Value: ' + distance); + + if(distance < item.value) { + doDoorStateChangedCall(item.on.action.open); + } else { + doDoorStateChangedCall(item.on.action.closed); + } + } + ); + + process.on( 'SIGINT', function() { + Log.log( "Gracefully disconnect " + HOST ); + ipcon.disconnect(); + process.exit( ); + }); +} + +/* private */ +function doDoorStateChangedCall(options) { + Log.inspect('Call options', 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(HOST + ': ' + str); + }); + } + + var req = http.request(options, function(res) { + //Log.inspect('Result', res); + }); + + req.on('error', function(err) { + if(HOST) { + Log.error(HOST + ': ' + err); + } else { + Log.error(err); + } + }); + + req.end(); +} \ No newline at end of file diff --git a/hobu_distance_ir_io/config.json b/hobu_distance_ir_io/config.json new file mode 100644 index 0000000..f7a9ea9 --- /dev/null +++ b/hobu_distance_ir_io/config.json @@ -0,0 +1,25 @@ +{ + "items": [ + { + "host": "localhost", + "port": 4223, + "uid": "xuk", + "value": 500, + "on": { + "action": { + "timeout": 2000, + "open": { + "host": "hobu", + "port": "2999", + "path": "/set/state/of/door/100/to/open/" + }, + "closed": { + "host": "hobu", + "port": "2999", + "path": "/set/state/of/door/100/to/closed/" + } + } + } + } + ] +}