diff --git a/hobu_doorbell/app.js b/hobu_analog_in_V2/app.js similarity index 100% rename from hobu_doorbell/app.js rename to hobu_analog_in_V2/app.js diff --git a/hobu_doorbell/config.json b/hobu_analog_in_V2/config.json similarity index 100% rename from hobu_doorbell/config.json rename to hobu_analog_in_V2/config.json diff --git a/hobu_sound_intensity/app.js b/hobu_sound_intensity/app.js new file mode 100644 index 0000000..ed1214a --- /dev/null +++ b/hobu_sound_intensity/app.js @@ -0,0 +1,118 @@ +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 si = new Tinkerforge.BrickletSoundIntensity(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) { + // Get threshold callbacks with a debounce time of 1 second (1000ms) + si.setDebouncePeriod(item.on.action.period); + + // Configure threshold for intensity "greater than e.g. 2000" + si.setIntensityCallbackThreshold('>', item.on.action.ring.marker, 0); + } + ); + + // Register intensity callback + si.on(Tinkerforge.BrickletSoundIntensity.CALLBACK_INTENSITY_REACHED, + // Callback function for intensity callback + function (intensity) { + Log.log('Intensity: ' + intensity); + doHoBuDoorBellCall(item); + } + ); + + process.on('SIGINT', function() { + Log.log( "Gracefully disconnect " + HOST ); + ipcon.disconnect(); + process.exit( ); + }); +} + + +/* private */ +function doHoBuDoorBellCall(item) { + var options = { + host: item.on.action.ring.host, + port: item.on.action.ring.port, + path: item.on.action.ring.path + }; + + 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); + }); + } + + 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_sound_intensity/config.json b/hobu_sound_intensity/config.json new file mode 100644 index 0000000..7a58b68 --- /dev/null +++ b/hobu_sound_intensity/config.json @@ -0,0 +1,20 @@ +{ + "items": [ + { + "host": "localhost", + "port": 4223, + "uid": "vpm", + "on": { + "action": { + "period": 1000, + "ring": { + "marker": 2300, + "host": "hobu", + "port": "2999", + "path": "/doorbell/ring/Wohnungstür/" + } + } + } + } + ] +}