Value picker
This commit is contained in:
parent
d79e95dfeb
commit
203d770e98
112
hobu_value_picker/app.js
Normal file
112
hobu_value_picker/app.js
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
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 + ' 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 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) {
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
process.on( 'SIGINT', function() {
|
||||||
|
Log.log( "Gracefully disconnect " + HOST );
|
||||||
|
ipcon.disconnect();
|
||||||
|
process.exit( );
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* private */
|
||||||
|
function saveValue(value, item) {
|
||||||
|
var room = item.on.new.value.room;
|
||||||
|
var category = item.on.new.value.category;
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
host: item.on.new.value.host,
|
||||||
|
port: item.on.new.value.port,
|
||||||
|
path: '/persist/value/of/'+category+'/with/'+value+'/in/'+room+'/'
|
||||||
|
};
|
||||||
|
|
||||||
|
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) {
|
||||||
|
Log.error(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
}
|
||||||
23
hobu_value_picker/config.json
Normal file
23
hobu_value_picker/config.json
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"host": "192.168.2.126",
|
||||||
|
"port": 4223,
|
||||||
|
"uid": "8j1",
|
||||||
|
"callback": {
|
||||||
|
"period": 6000
|
||||||
|
},
|
||||||
|
"on": {
|
||||||
|
"new": {
|
||||||
|
"value": {
|
||||||
|
"type": "web",
|
||||||
|
"host": "hobu",
|
||||||
|
"port": "2999",
|
||||||
|
"category": "TEMPERATURE",
|
||||||
|
"room": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user