diff --git a/hobu_value_picker/app.js b/hobu_value_picker/app.js index 96be1c9..7b63051 100644 --- a/hobu_value_picker/app.js +++ b/hobu_value_picker/app.js @@ -25,8 +25,6 @@ function connect(item) { Log.log('Init ' + UID + ' @' + HOST + ' with period of ' + item.callback.period); var ipcon = new Tinkerforge.IPConnection(); // Create IP connection - var t = new Tinkerforge.BrickletTemperature(UID, ipcon); // Create device object - var h = new Tinkerforge.BrickletHumidity(UID, ipcon); // Create device object var counter = 0; var readyForNextNotification = true; @@ -56,25 +54,25 @@ function connect(item) { ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED, function (connectReason) { - t.setTemperatureCallbackPeriod(item.callback.period); - } - ); + var category = item.on.new.value.category; - // Register temperature callback - t.on(Tinkerforge.BrickletTemperature.CALLBACK_TEMPERATURE, - // Callback function for temperature callback (parameter has unit °C/100) - function (temperature) { - saveValue((temperature/100.0), item); - //Log.log('Temperature: ' + temperature/100.0 + ' °C'); - } - ); - - // Register humidity callback - h.on(Tinkerforge.BrickletHumidity.CALLBACK_HUMIDITY, - // Callback function for humidity callback (parameter has unit %RH/10) - function (humidity) { - saveValue((humidity/10.0), item); - //Log.log('Humidity: ' + humidity/10.0 + ' ' + item.on.new.value.category); + switch(connectReason) { + case Tinkerforge.IPConnection.CONNECT_REASON_REQUEST: + Log.log('Connect by request for '+UID+' @'+HOST); + break; + case Tinkerforge.IPConnection.CONNECT_REASON_AUTO_RECONNECT: + Log.log('Connect by auto-reconnect for '+UID+' @'+HOST); + break; + } + + switch(category) { + case 'TEMPERATURE': + connectCategoryTemperature(item, ipcon); + break; + case '%RH': + connectCategoryHR(item, ipcon); + break; + } } ); @@ -85,6 +83,38 @@ function connect(item) { }); } +/* private */ +function connectCategoryTemperature(item, ipcon) { + var UID = item.uid; + var t = new Tinkerforge.BrickletTemperature(UID, ipcon); // Create device object + t.setTemperatureCallbackPeriod(item.callback.period); + + // Register temperature callback + t.on(Tinkerforge.BrickletTemperature.CALLBACK_TEMPERATURE, + // Callback function for temperature callback (parameter has unit °C/100) + function (temperature) { + saveValue((temperature/100.0), item); + Log.log('Temperature: ' + temperature/100.0 + ' °C'); + } + ); +} + +/* private */ +function connectCategoryHR(item, ipcon) { + var UID = item.uid; + var h = new Tinkerforge.BrickletHumidity(UID, ipcon); // Create device object + h.setHumidityCallbackPeriod(item.callback.period); + + // Register humidity callback + h.on(Tinkerforge.BrickletHumidity.CALLBACK_HUMIDITY, + // Callback function for humidity callback (parameter has unit %RH/10) + function (humidity) { + saveValue((humidity/10.0), item); + Log.log('Humidity: ' + humidity/10.0 + ' ' + item.on.new.value.category); + } + ); +} + /* private */ function saveValue(value, item) { var room = item.on.new.value.room; diff --git a/hobu_value_picker/config.json b/hobu_value_picker/config.json index 878d915..1967c4a 100644 --- a/hobu_value_picker/config.json +++ b/hobu_value_picker/config.json @@ -24,7 +24,7 @@ "port": 4223, "uid": "xCt", "callback": { - "period": 60000 + "period": 120000 }, "on": { "new": {