Welis Garagen-Anzeige
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
var Tinkerforge = require('../Tinkerforge')
|
||||
, Conf = require('./config.json')
|
||||
, Helper = require('../Adawim/helper')
|
||||
, Log = require('../Adawim/logging')
|
||||
, http = require('http')
|
||||
, WebSocket = require('ws')
|
||||
, hobu = require('./hobu');
|
||||
|
||||
|
||||
var item = {
|
||||
host: Conf.display.host,
|
||||
port: Conf.display.port,
|
||||
uid: Conf.display.uid
|
||||
}
|
||||
|
||||
connect(item)
|
||||
|
||||
|
||||
/* private */
|
||||
function reconnect(item) {
|
||||
Log.debug('Reconnect ' + item.host);
|
||||
setTimeout(function() { 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 oled = new Tinkerforge.BrickletOLED128x64(UID, ipcon); // Create device object
|
||||
|
||||
|
||||
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) {
|
||||
// Clear display
|
||||
oled.clearDisplay();
|
||||
|
||||
// Write "Hello World" starting from upper left corner of the screen
|
||||
oled.writeLine(0, 0, 'I love programming!');
|
||||
|
||||
hobu.init(oled)
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
process.on( 'SIGINT', function() {
|
||||
Log.log( "Gracefully disconnect " + HOST );
|
||||
ipcon.disconnect();
|
||||
process.exit( );
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"display" : {
|
||||
"host": "localhost",
|
||||
"port": "4223",
|
||||
"uid": "Bm2",
|
||||
"dimension": {
|
||||
"width": 128,
|
||||
"height": 64
|
||||
}
|
||||
},
|
||||
"hobu": {
|
||||
"host": "192.168.2.100",
|
||||
"port": "8000"
|
||||
},
|
||||
"keywords": {
|
||||
"garage": "Garage"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
var Conf = require('./config.json')
|
||||
, Helper = require('../Adawim/helper')
|
||||
, Log = require('../Adawim/logging')
|
||||
, WebSocket = require('ws')
|
||||
, GM = require('gm'); // FIXME: maybe use node-gd instead
|
||||
|
||||
|
||||
const WIDTH = Conf.display.dimension.width;
|
||||
const HEIGHT = Conf.display.dimension.height;
|
||||
const wsURL = 'ws://'+Conf.hobu.host+':'+Conf.hobu.port;
|
||||
var ws = null;
|
||||
var oled = null;
|
||||
|
||||
exports.init = function(oled_) {
|
||||
oled = oled_
|
||||
|
||||
ws = new WebSocket(wsURL, {
|
||||
perMessageDeflate: false
|
||||
})
|
||||
|
||||
ws.on('open', function open() {
|
||||
ws.send('{type:"version"}')
|
||||
ws.send('{type:"check_global"}')
|
||||
})
|
||||
|
||||
ws.on('message', function incoming(data) {
|
||||
if(Log.isTrace()) { Log.inspect('Incoming data', data) }
|
||||
|
||||
handleIncomeingData(JSON.parse(data))
|
||||
})
|
||||
}
|
||||
|
||||
function handleIncomeingData(data_) {
|
||||
const type = data_.data.type
|
||||
const data = data_.data.data
|
||||
|
||||
switch(type) {
|
||||
case 'version':
|
||||
Log.debug('HoBu Version: ' + data.value)
|
||||
break
|
||||
case 'check_global':
|
||||
handleGlobalCheck(data)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
function handleGlobalCheck(data) {
|
||||
const isOK = data.result
|
||||
const foundGarageEntry = filterGarageEntry(data).length > 0
|
||||
|
||||
if(foundGarageEntry) {
|
||||
oled.clearDisplay()
|
||||
oled.writeLine(0, 0, 'Garagentor offen!')
|
||||
} else {
|
||||
oled.clearDisplay()
|
||||
oled.writeLine(0, 0, 'Garagentor geschlossen!')
|
||||
}
|
||||
}
|
||||
|
||||
function filterGarageEntry(data) {
|
||||
const keyword = Conf.keywords.garage
|
||||
return data.whatswrong.filter(function(entry) { return entry.msgOnErr.indexOf(keyword) >= 0 })
|
||||
}
|
||||
Reference in New Issue
Block a user