with switch

This commit is contained in:
Gerrit Linnemann 2016-08-24 20:30:53 +02:00
parent 24fa81d9be
commit a012c757de
2 changed files with 51 additions and 21 deletions

View File

@ -25,8 +25,6 @@ function connect(item) {
Log.log('Init ' + UID + ' @' + HOST + ' with period of ' + item.callback.period); Log.log('Init ' + UID + ' @' + HOST + ' with period of ' + item.callback.period);
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection 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 counter = 0;
var readyForNextNotification = true; var readyForNextNotification = true;
@ -56,25 +54,25 @@ function connect(item) {
ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED, ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
function (connectReason) { function (connectReason) {
t.setTemperatureCallbackPeriod(item.callback.period); var category = item.on.new.value.category;
}
);
// Register temperature callback switch(connectReason) {
t.on(Tinkerforge.BrickletTemperature.CALLBACK_TEMPERATURE, case Tinkerforge.IPConnection.CONNECT_REASON_REQUEST:
// Callback function for temperature callback (parameter has unit °C/100) Log.log('Connect by request for '+UID+' @'+HOST);
function (temperature) { break;
saveValue((temperature/100.0), item); case Tinkerforge.IPConnection.CONNECT_REASON_AUTO_RECONNECT:
//Log.log('Temperature: ' + temperature/100.0 + ' °C'); Log.log('Connect by auto-reconnect for '+UID+' @'+HOST);
} break;
); }
// Register humidity callback switch(category) {
h.on(Tinkerforge.BrickletHumidity.CALLBACK_HUMIDITY, case 'TEMPERATURE':
// Callback function for humidity callback (parameter has unit %RH/10) connectCategoryTemperature(item, ipcon);
function (humidity) { break;
saveValue((humidity/10.0), item); case '%RH':
//Log.log('Humidity: ' + humidity/10.0 + ' ' + item.on.new.value.category); 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 */ /* private */
function saveValue(value, item) { function saveValue(value, item) {
var room = item.on.new.value.room; var room = item.on.new.value.room;

View File

@ -24,7 +24,7 @@
"port": 4223, "port": 4223,
"uid": "xCt", "uid": "xCt",
"callback": { "callback": {
"period": 60000 "period": 120000
}, },
"on": { "on": {
"new": { "new": {