From 8289e66903bb5c33ab3fd9afe7609e8268241495 Mon Sep 17 00:00:00 2001 From: Erick Hitter <services@ethitter.com> Date: Fri, 16 Dec 2016 20:08:19 -0800 Subject: [PATCH] Convert button presses (down only) to trigger events registered in the config --- index.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 31657f5..a20f0d3 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 -- GitLab