io4
This commit is contained in:
parent
53dee34ffc
commit
3f2e76cdcc
136
hobu_io4/app.js
Normal file
136
hobu_io4/app.js
Normal file
@ -0,0 +1,136 @@
|
||||
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 io = new Tinkerforge.BrickletIO4(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) {
|
||||
// Enable interrupt on pin 0
|
||||
io.setInterrupt(1 << 0);
|
||||
|
||||
// get current value as bitmask
|
||||
io.getValue(
|
||||
function (valueMask) {
|
||||
Log.log('Value Mask: ' + valueMask.toString(2));
|
||||
handleDependingOnValue(item, valueMask.toString(2));
|
||||
},
|
||||
function (error) {
|
||||
Log.error('Error: ' + error);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// Register interrupt callback
|
||||
io.on(Tinkerforge.BrickletIO4.CALLBACK_INTERRUPT,
|
||||
// Callback function for interrupt callback
|
||||
function (interruptMask, valueMask) {
|
||||
Log.log('Interrupt Mask: ' + interruptMask.toString(2));
|
||||
Log.log('Value Mask: ' + valueMask.toString(2));
|
||||
|
||||
handleDependingOnValue(item, valueMask.toString(2));
|
||||
}
|
||||
);
|
||||
|
||||
process.on('SIGINT', function() {
|
||||
Log.log( "Gracefully disconnect " + HOST );
|
||||
ipcon.disconnect();
|
||||
process.exit( );
|
||||
});
|
||||
}
|
||||
|
||||
/* private */
|
||||
function handleDependingOnValue(item, value2check) {
|
||||
switch(value2check) {
|
||||
case item.on.action.open.value:
|
||||
doDoorStateChangedCall(item.on.action.open);
|
||||
break;
|
||||
case item.on.action.closed.value:
|
||||
doDoorStateChangedCall(item.on.action.closed);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* private */
|
||||
function doDoorStateChangedCall(options) {
|
||||
Log.inspect('Call options', options);
|
||||
|
||||
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(HOST + ': ' + 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();
|
||||
}
|
||||
26
hobu_io4/config.json
Normal file
26
hobu_io4/config.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"host": "localhost",
|
||||
"port": 4223,
|
||||
"uid": "gXh",
|
||||
"on": {
|
||||
"action": {
|
||||
"timeout": 2000,
|
||||
"open": {
|
||||
"value": "1110",
|
||||
"host": "hobu",
|
||||
"port": "2999",
|
||||
"path": "/set/state/of/door/100/to/open/"
|
||||
},
|
||||
"closed": {
|
||||
"value": "1111",
|
||||
"host": "hobu",
|
||||
"port": "2999",
|
||||
"path": "/set/state/of/door/100/to/closed/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user