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);
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');
}
);
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;
}
// 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(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;

View File

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