63 lines
1.5 KiB
JavaScript

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 })
}