diff --git a/index.js b/index.js index 31657f54f21a1ca20b975daf935d55daf123281a..a20f0d30efad5a00c89054726eed456a8616ce64 100644 --- a/index.js +++ b/index.js @@ -3,33 +3,46 @@ /** * LIBRARIES */ - var fliclib = require( './vendor/fliclib-linux-hci/clientlib/nodejs/fliclibNodeJs' ); var FlicClient = fliclib.FlicClient; var FlicConnectionChannel = fliclib.FlicConnectionChannel; -var FlicScanner = fliclib.FlicScanner; var request = require( 'request' ); /** * CONFIGURATION & SETUP */ - -var config = require( './config.json' ); - -var client = new FlicClient( config.flicd_ip, config.flicd_port ); +var config = require( './config.json' ); +var homeAssistantApiBase = config.home_assistant_proto + '://' + config.home_assistant_host + ':' + config.home_assistant_port + '/api/'; +var client = new FlicClient( config.flicd_ip, config.flicd_port ); /** * BUTTON BINDING */ -Object.keys( config.buttons ).forEach( function( index ) { - var buttonConfig = config.buttons[index]; +client.once( 'ready', function() { + client.getInfo( function( info ) { + info.bdAddrOfVerifiedButtons.forEach( function( bdAddr ) { + listenToButton( bdAddr ); + } ); + } ); +} ); -// var button = dashButton( buttonConfig.mac, config.arp_interface ); +client.on( 'newVerifiedButton', function( bdAddr ) { + listenToButton( bdAddr ); +}); -// button.on( 'detected', buttonActivated ); -} ); +/** + * BUTTON REACTIONS + */ +function listenToButton( bdAddr ) { + var cc = new FlicConnectionChannel( bdAddr ); + client.addConnectionChannel( cc ); -var homeAssistantApiBase = config.home_assistant_proto + '://' + config.home_assistant_host + ':' + config.home_assistant_port + '/api/'; + cc.on( 'buttonUpOrDown', function( clickType, wasQueued, timeDiff ) { + if ( 'ButtonDown' === clickType && false === wasQueued ) { + buttonActivated( bdAddr ); + } + } ); +} /** * BUTTON FUNCTIONALITY