From 6027f1327a9c2d75a8fb167d80d1f7a8e01f9f5c Mon Sep 17 00:00:00 2001 From: Mrkva Date: Sat, 22 Apr 2017 23:12:17 +0200 Subject: [PATCH] Configuration is no longer hardcoded, spaceapi support implemented --- brmdoor-rpi/brmdoor-rpi.sh | 89 +++++++++++++++++---------- brmdoor-rpi/brmdoor.conf.dist-biodoor | 62 +++++++++++++++++++ brmdoor-rpi/brmdoor.conf.dist-brmdoor | 65 +++++++++++++++++++ brmdoor-rpi/brmstatus-template.json | 47 ++++++++++++++ 4 files changed, 232 insertions(+), 31 deletions(-) create mode 100644 brmdoor-rpi/brmdoor.conf.dist-biodoor create mode 100644 brmdoor-rpi/brmdoor.conf.dist-brmdoor create mode 100644 brmdoor-rpi/brmstatus-template.json diff --git a/brmdoor-rpi/brmdoor-rpi.sh b/brmdoor-rpi/brmdoor-rpi.sh index 78f6ed0..3d3218b 100755 --- a/brmdoor-rpi/brmdoor-rpi.sh +++ b/brmdoor-rpi/brmdoor-rpi.sh @@ -1,20 +1,12 @@ #!/bin/bash - -GPIO_LOCK=24 -GPIO_LED=22 -GPIO_BEEP=23 - -GPIO_SWITCH=18 -GPIO_MAGSWITCH=17 - - -NFC_BINARY=/root/brmdoor/nfc-getcard -ALLOWED_LIST=/root/brmdoor/allowed.list - -IRSSIFIFO=/home/brmdoor/.irssi/remote-control -IRCCHANS=("#brmlab" "#brmbiolab" "#brmstatus") -IRSSITOPICS=/home/brmdoor/.irssi/topics/ - +WD=$(dirname $(readlink -f $0)) +if [ -e $WD/brmdoor.conf ]; then + echo "Loading config file $WD/brmdoor.conf..." + . $WD/brmdoor.conf +else + echo "ERROR: Config file not found. Please create brmdoor.conf in the same directory as brmdoor-rpi.sh." + exit 1 +fi # WARNING - OPEN/DOOR are stored as GPIO values. Since we have pullups and switches that grounds these pins, values are inverted (ie. 1 means "CLOSED") OPEN=1 @@ -27,10 +19,8 @@ IGNORE_ALARM_SET=50 IGNORE_ALARM=0 - export LD_LIBRARY_PATH=/usr/local/lib - clean_gpio() { for i in $GPIO_LOCK $GPIO_LED $GPIO_BEEP $GPIO_SWITCH $GPIO_MAGSWITCH; do echo in > /sys/class/gpio/gpio${i}/direction @@ -85,8 +75,12 @@ irc_status() { echo "TINFO" > "$IRSSIFIFO" sleep 5s for chan in ${IRCCHANS[*]}; do + echo "chan: $chan" T=`cat "${IRSSITOPICS}/${chan}"` - NT=`echo "$T"|sed "s/BRMBIOLAB OPEN\|BRMBIOLAB CLOSED/BRMBIOLAB $1/"` + NT=`echo "$T"|sed "s/$ROOM OPEN\|$ROOM CLOSED/$ROOM $1/"` + echo "t: $T" + echo "nt: $NT" + echo "sed: s/$ROOM OPEN\|$ROOM CLOSED/$ROOM $1/" if [ "$NT" = "$T" ]; then continue; fi @@ -99,6 +93,16 @@ log_message() { echo "`date "+%Y-%m-%d %T"` $1" >> ~/brmdoor.log } +updateSpaceApi() { + local open=$1 + local changestamp=$2 + if [ -z $SPACEAPI_DST ] || [ ! -f $SPACEAPI_TPL ]; then + return + fi + + cat "$SPACEAPI_TPL"| sed "s/##OPEN##/$open/g;s/##LASTCHANGE##/$changestamp/g" > $SPACEAPI_DST +} + trap clean_gpio EXIT @@ -106,6 +110,8 @@ for i in $GPIO_LOCK $GPIO_LED $GPIO_BEEP $GPIO_SWITCH $GPIO_MAGSWITCH; do echo $i > /sys/class/gpio/export done +sleep 1 # do not remove unless running as root... few ms after exporting the GPIO the file is owned by root:root + for i in $GPIO_LOCK $GPIO_LED $GPIO_BEEP; do echo "out" > /sys/class/gpio/gpio${i}/direction done @@ -119,21 +125,32 @@ LOOP=0 NFC_FAILED=1 +CURRENT_OPEN=`cat /sys/class/gpio/gpio${GPIO_SWITCH}/value` +LASTCHANGE=`date +%s` + +if [ $CURRENT_OPEN -eq 1 ]; then + irc_status "CLOSED" & + updateSpaceApi false $LASTCHANGE +else + irc_status "OPEN" & + updateSpaceApi true $LASTCHANGE +fi + while true; do CARD=`$NFC_BINARY` RET=$? if [ $RET -ne 0 ] && [ $NFC_FAILED -eq 1 ] ; then NFC_FAILED=0 log_message "NFC_FAILURE" - logger -p user.error "[biodoor] NFC failure" - irc_message "[biodoor] NFC error! Might be out of order!" + logger -p user.error "[$IDENTITY] NFC failure" +# irc_message "[$IDENTITY] NFC error! Might be out of order!" sleep 1s continue elif [ $RET -eq 0 ] && [ $NFC_FAILED -eq 0 ]; then NFC_FAILED=1 log_message "NFC_BACK" - logger -p user.error "[biodoor] NFC back" - irc_message "[biodoor] NFC communication is back!" + logger -p user.error "[$IDENTITY] NFC back" +# irc_message "[$IDENTITY] NFC communication is back!" fi if [ $IGNORE_ALARM -gt 0 ]; then @@ -144,13 +161,13 @@ while true; do NAME=`grep -i "^[0-9a-zA-Z_-]* ${CARD}$" "$ALLOWED_LIST"| cut -d ' ' -f 1` if [ -z "$NAME" ]; then log_message "UNKNOWN_CARD $CARD" & - logger "[biodoor] unauthorized access denied for card $CARD" & - irc_message "[biodoor] unauthorized access denied" & + logger "[$IDENTITY] unauthorized access denied for card $CARD" & + irc_message "[$IDENTITY] unauthorized access denied" & beep_invalid else log_message "DOOR_UNLOCKED $NAME $CARD" & - logger "[biodoor] unlocked by $NAME $CARD" & - irc_message "[biodoor] door unlocked" & + logger "[$IDENTITY] unlocked by $NAME $CARD" & + irc_message "[$IDENTITY] door unlocked" & echo 1 > /sys/class/gpio/gpio${GPIO_LOCK}/value beep_unlocked & sleep $LOCK_TIMEOUT @@ -163,15 +180,24 @@ while true; do CURRENT_OPEN=`cat /sys/class/gpio/gpio${GPIO_SWITCH}/value` if [ $CURRENT_OPEN -eq 1 -a $OPEN -eq 0 ]; then log_message "STATUS_CLOSED" & - irc_message "[biostatus] update: CLOSED" & + irc_message "[${STATUS}] update: CLOSED" & irc_status "CLOSED" & + LASTCHANGE=`date +%s` + updateSpaceApi false $LASTCHANGE IGNORE_ALARM=$IGNORE_ALARM_SET + if [ -n $IMAGE_DST ]; then + cp $IMAGE_CLOSED $IMAGE_DST + fi fi if [ $CURRENT_OPEN -eq 0 -a $OPEN -eq 1 ]; then log_message "STATUS_OPEN" & - irc_message "[biostatus] update: OPEN" & + irc_message "[${STATUS}] update: OPEN" & irc_status "OPEN" & - + LASTCHANGE=`date +%s` + updateSpaceApi true $LASTCHANGE + if [ -n $IMAGE_DST ]; then + cp $IMAGE_OPEN $IMAGE_DST + fi fi CURRENT_DOOR=`cat /sys/class/gpio/gpio${GPIO_MAGSWITCH}/value` @@ -180,7 +206,7 @@ while true; do if [ $CURRENT_DOOR -eq 1 ] && [ $DOOR -eq 0 ] && [ $OPEN -eq 1 ] && [ $IGNORE_ALARM -eq 0 ]; then log_message "DOOR_ALARM" & - irc_message "[biodoor] alarm! (status closed, door opened, not unlocked)" & + irc_message "[$IDENTITY] alarm! (status closed, door opened, not unlocked)" & beep_alarm fi @@ -206,4 +232,5 @@ while true; do fi let LOOP=$LOOP+1 + sleep 1 done diff --git a/brmdoor-rpi/brmdoor.conf.dist-biodoor b/brmdoor-rpi/brmdoor.conf.dist-biodoor new file mode 100644 index 0000000..8c10882 --- /dev/null +++ b/brmdoor-rpi/brmdoor.conf.dist-biodoor @@ -0,0 +1,62 @@ +#!/bin/bash + +# maglock GPIO pin +GPIO_LOCK=24 + +# status led GPIO pin +GPIO_LED=22 + +# buzzer GPIO pin +GPIO_BEEP=23 + +# status switch GPIO pin +GPIO_SWITCH=18 + +# magswitch GPIO pin (used as alarm trigger) +GPIO_MAGSWITCH=17 + +# path to nfc-getcard binary (from the brmdoor-pi repo) +NFC_BINARY=/home/brmdoor/brmdoor/brmdoor-rpi/nfc-getcard + +# list of allowed card IDs with names +# format: +# user1 cafebabe +# user2 b16b00b5 + +ALLOWED_LIST=/home/brmdoor/allowed.list + +# fifo for remote irssi control +IRSSIFIFO=/home/brmdoor/.irssi/remote-control + +# IRC chans where bot will idle +IRCCHANS=("#brmlab" "#brmbiolab" "#brmstatus") + +# directory with stored IRC chans topics +IRSSITOPICS=/home/brmdoor/.irssi/topics/ + +# image of closed brmlab +IMAGE_CLOSED=/home/brmdoor/brmdoor/brmd/status-closed.png + +# image of open brmlab +IMAGE_OPEN=/home/brmdoor/brmdoor/brmd/status-open.png + +# destination image +# do not set if you don't want image update on open/close +# IMAGE_DST=/var/www/html/brmd/brmstatus.png + +# spaceAPI template +# two tokens will be replaced: ##OPEN## with true/false if brmlab is open/closed and ##LASTCHANGE## with unix timestamp of last status change +SPACEAPI_TPL=/home/brmdoor/brmdoor/brmdoor-rpi/brmstatus-template.json + +# SpaceAPI destination file, do not set if you don't want updates +# SPACEAPI_DST=/var/www/html/brmd/brmstatus.json + + +# identity used in logs and IRC messages, used in messages like: "[$IDENTITY] door unlocked" +IDENTITY="brmbiodoor" + +# status identifier for logs and IRC, used in messages: "[${STATUS}] update: CLOSED" +STATUS="biostatus" + +# room name in IRC topic +ROOM="BRMBIOLAB" diff --git a/brmdoor-rpi/brmdoor.conf.dist-brmdoor b/brmdoor-rpi/brmdoor.conf.dist-brmdoor new file mode 100644 index 0000000..3b9a614 --- /dev/null +++ b/brmdoor-rpi/brmdoor.conf.dist-brmdoor @@ -0,0 +1,65 @@ +#!/bin/bash + +# maglock GPIO pin +GPIO_LOCK=24 + +# status led GPIO pin +GPIO_LED=22 + +# buzzer GPIO pin +GPIO_BEEP=23 + +# status switch GPIO pin +GPIO_SWITCH=18 + +# magswitch GPIO pin (used as alarm trigger) +GPIO_MAGSWITCH=17 + +# path to nfc-getcard binary (from the brmdoor-pi repo) +NFC_BINARY=/home/brmdoor/brmdoor/brmdoor-rpi/nfc-getcard + +# list of allowed card IDs with names +# format: +# user1 cafebabe +# user2 b16b00b5 + +ALLOWED_LIST=/home/brmdoor/allowed.list + +# fifo for remote irssi control +IRSSIFIFO=/home/brmdoor/.irssi/remote-control + +# IRC chans where bot will idle +IRCCHANS=("#brmlab" "#brmbiolab" "#brmstatus") + +# directory with stored IRC chans topics +IRSSITOPICS=/home/brmdoor/.irssi/topics/ + +# image of closed brmlab +IMAGE_CLOSED=/home/brmdoor/brmdoor/brmd/status-closed.png + +# image of open brmlab +IMAGE_OPEN=/home/brmdoor/brmdoor/brmd/status-open.png + +# destination image +# do not set if you don't want image update on open/close +IMAGE_DST=/var/www/html/brmd/brmstatus.png + +# spaceAPI template +# two tokens will be replaced: ##OPEN## with true/false if brmlab is open/closed and ##LASTCHANGE## with unix timestamp of last status change +SPACEAPI_TPL=/home/brmdoor/brmdoor/brmdoor-rpi/brmstatus-template.json + +# SpaceAPI destination file, do not set if you don't want updates +SPACEAPI_DST=/var/www/html/brmd/brmstatus.json + + + +# identity used in logs and IRC messages, used in messages like: "[$IDENTITY] door unlocked" +IDENTITY="brmdoor" + +# status identifier for logs and IRC, used in messages: "[${STATUS}] update: CLOSED" +STATUS="brmstatus" + +# room name in IRC topic +ROOM="BRMLAB" + + diff --git a/brmdoor-rpi/brmstatus-template.json b/brmdoor-rpi/brmstatus-template.json new file mode 100644 index 0000000..730c95e --- /dev/null +++ b/brmdoor-rpi/brmstatus-template.json @@ -0,0 +1,47 @@ +{ + "api": "0.13", + "space": "brmlab", + "logo": "https://status.brmlab.cz/_media/brmlab.png", + "url": "https://brmlab.cz", + "open": ##OPEN##, + "location": { + "address": "brmlab z.s., Bubenska 1477/1, 170 00 Prague 7, Czech republic", + "lon": 14.4369158, + "lat": 50.0984614 + }, + "contact": { + "phone": "+420608801582", + "sip": "910114321@sip.odorik.cz", + "irc": "irc://freenode/#brmlab", + "twitter": "@brmlab", + "facebook": "brmlab", + "foursquare": "4cbdc77cc7228cfa25052cce", + "ml": "brmlab@brmlab.cz", + "issue_mail": "cmFkYUBicm1sYWIuY3oK" + }, + "issue_report_channels": [ + "issue_mail" + ], + "state": { + "open": ##OPEN##, + "lastchange": ##LASTCHANGE##, + "icon": { + "open": "https://status.brmlab.cz/status-open-icon.png", + "closed": "https://status.brmlab.cz/status-closed-icon.png" + } + }, + "icon": { + "open": "https://status.brmlab.cz/status-open-icon.png", + "closed": "https://status.brmlab.cz/status-closed-icon.png" + }, + "stream": { + "mp4": "http://brmlab.cz/stream.flv" + }, + "projects": [ + "https://brmlab.cz/project/start", + "https://github.com/brmlab" + ], + "cache": { + "schedule": "m.02" + } +}