Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions clientlib/python/mqtt_bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

import fliclib
import paho.mqtt.client as mqtt

mqclient = mqtt.Client(clean_session=True)
mqclient.connect("test.mosquitto.org", 1883, 60)
mqclient.loop_start()
mqclient.publish("flic/gateway", "Start")

client = fliclib.FlicClient("localhost")


def button_up_or_down(channel, click_type, was_queued, time_diff):
print(channel.bd_addr)
mqclient.publish("flic/%s" % channel.bd_addr, str(click_type))


def status_changed(channel, connection_status, disconnect_reason):
print(channel.bd_addr)
print(connection_status)
print(disconnect_reason)
mqclient.publish("flic/%s" % channel.bd_addr, str(connection_status))


def got_button(bd_addr):
cc = fliclib.ButtonConnectionChannel(bd_addr)
cc.on_button_up_or_down = button_up_or_down
cc.on_connection_status_changed = status_changed
client.add_connection_channel(cc)


def got_info(items):
for bd_addr in items["bd_addr_of_verified_buttons"]:
got_button(bd_addr)


client.get_info(got_info)
client.on_new_verified_button = got_button
client.handle_events()
96 changes: 96 additions & 0 deletions flicd.initd
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/sh
### BEGIN INIT INFO
# Provides: flicd
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: flic daemon
### END INIT INFO

SCRIPT="/usr/sbin/flicd -f /var/lib/flic/flic.sqlite3 -s 0.0.0.0 -d"
RUNAS=root

PIDFILE=/var/run/flicd.pid
LOGFILE=/var/log/flicd.log

start() {
if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
# Try with this command line instead of above if not workable
# su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"

sleep 2
PID=$(cat $PIDFILE)
if pgrep -u $RUNAS -f $NAME > /dev/null
then
echo "$NAME is now running, the PID is $PID"
else
echo ''
echo "Error! Could not start $NAME!"
fi
}

stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}

uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file was not removed: $LOGFILE" >&2
update-rc.d -f $NAME remove
rm -fv "$0"
fi
}

status() {
printf "%-50s" "Checking <NAME>..."
if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}


case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac
19 changes: 19 additions & 0 deletions install-raspi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

# Download the latest flic binary
curl https://raw.githubusercontent.com/50ButtonsEach/fliclib-linux-hci/master/bin/armv6l/flicd > /usr/sbin/flicd
chmod a+x /usr/sbin/flicd

# Download the latest init script
curl https://raw.githubusercontent.com/50ButtonsEach/fliclib-linux-hci/master/flicd.initd > /etc/init.d/flicd
chmod a+x /etc/init.d/flicd

# Disable old bluetooth daemon
sudo systemctl disable bluetooth
sudo systemctl stop bluetooth

# Enable flicd
mkdir /var/lib/flic
sudo systemctl enable flicd
sudo systemctl start flicd