mirror of
https://github.com/brmlab/brmdoor.git
synced 2025-06-07 19:24:01 +02:00
Configuration is no longer hardcoded, spaceapi support implemented
This commit is contained in:
parent
70d0e3e936
commit
6027f1327a
4 changed files with 232 additions and 31 deletions
|
@ -1,20 +1,12 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
WD=$(dirname $(readlink -f $0))
|
||||||
GPIO_LOCK=24
|
if [ -e $WD/brmdoor.conf ]; then
|
||||||
GPIO_LED=22
|
echo "Loading config file $WD/brmdoor.conf..."
|
||||||
GPIO_BEEP=23
|
. $WD/brmdoor.conf
|
||||||
|
else
|
||||||
GPIO_SWITCH=18
|
echo "ERROR: Config file not found. Please create brmdoor.conf in the same directory as brmdoor-rpi.sh."
|
||||||
GPIO_MAGSWITCH=17
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
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/
|
|
||||||
|
|
||||||
|
|
||||||
# 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")
|
# 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
|
OPEN=1
|
||||||
|
@ -27,10 +19,8 @@ IGNORE_ALARM_SET=50
|
||||||
|
|
||||||
IGNORE_ALARM=0
|
IGNORE_ALARM=0
|
||||||
|
|
||||||
|
|
||||||
export LD_LIBRARY_PATH=/usr/local/lib
|
export LD_LIBRARY_PATH=/usr/local/lib
|
||||||
|
|
||||||
|
|
||||||
clean_gpio() {
|
clean_gpio() {
|
||||||
for i in $GPIO_LOCK $GPIO_LED $GPIO_BEEP $GPIO_SWITCH $GPIO_MAGSWITCH; do
|
for i in $GPIO_LOCK $GPIO_LED $GPIO_BEEP $GPIO_SWITCH $GPIO_MAGSWITCH; do
|
||||||
echo in > /sys/class/gpio/gpio${i}/direction
|
echo in > /sys/class/gpio/gpio${i}/direction
|
||||||
|
@ -85,8 +75,12 @@ irc_status() {
|
||||||
echo "TINFO" > "$IRSSIFIFO"
|
echo "TINFO" > "$IRSSIFIFO"
|
||||||
sleep 5s
|
sleep 5s
|
||||||
for chan in ${IRCCHANS[*]}; do
|
for chan in ${IRCCHANS[*]}; do
|
||||||
|
echo "chan: $chan"
|
||||||
T=`cat "${IRSSITOPICS}/${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
|
if [ "$NT" = "$T" ]; then
|
||||||
continue;
|
continue;
|
||||||
fi
|
fi
|
||||||
|
@ -99,6 +93,16 @@ log_message() {
|
||||||
echo "`date "+%Y-%m-%d %T"` $1" >> ~/brmdoor.log
|
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
|
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
|
echo $i > /sys/class/gpio/export
|
||||||
done
|
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
|
for i in $GPIO_LOCK $GPIO_LED $GPIO_BEEP; do
|
||||||
echo "out" > /sys/class/gpio/gpio${i}/direction
|
echo "out" > /sys/class/gpio/gpio${i}/direction
|
||||||
done
|
done
|
||||||
|
@ -119,21 +125,32 @@ LOOP=0
|
||||||
|
|
||||||
NFC_FAILED=1
|
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
|
while true; do
|
||||||
CARD=`$NFC_BINARY`
|
CARD=`$NFC_BINARY`
|
||||||
RET=$?
|
RET=$?
|
||||||
if [ $RET -ne 0 ] && [ $NFC_FAILED -eq 1 ] ; then
|
if [ $RET -ne 0 ] && [ $NFC_FAILED -eq 1 ] ; then
|
||||||
NFC_FAILED=0
|
NFC_FAILED=0
|
||||||
log_message "NFC_FAILURE"
|
log_message "NFC_FAILURE"
|
||||||
logger -p user.error "[biodoor] NFC failure"
|
logger -p user.error "[$IDENTITY] NFC failure"
|
||||||
irc_message "[biodoor] NFC error! Might be out of order!"
|
# irc_message "[$IDENTITY] NFC error! Might be out of order!"
|
||||||
sleep 1s
|
sleep 1s
|
||||||
continue
|
continue
|
||||||
elif [ $RET -eq 0 ] && [ $NFC_FAILED -eq 0 ]; then
|
elif [ $RET -eq 0 ] && [ $NFC_FAILED -eq 0 ]; then
|
||||||
NFC_FAILED=1
|
NFC_FAILED=1
|
||||||
log_message "NFC_BACK"
|
log_message "NFC_BACK"
|
||||||
logger -p user.error "[biodoor] NFC back"
|
logger -p user.error "[$IDENTITY] NFC back"
|
||||||
irc_message "[biodoor] NFC communication is back!"
|
# irc_message "[$IDENTITY] NFC communication is back!"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $IGNORE_ALARM -gt 0 ]; then
|
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`
|
NAME=`grep -i "^[0-9a-zA-Z_-]* ${CARD}$" "$ALLOWED_LIST"| cut -d ' ' -f 1`
|
||||||
if [ -z "$NAME" ]; then
|
if [ -z "$NAME" ]; then
|
||||||
log_message "UNKNOWN_CARD $CARD" &
|
log_message "UNKNOWN_CARD $CARD" &
|
||||||
logger "[biodoor] unauthorized access denied for card $CARD" &
|
logger "[$IDENTITY] unauthorized access denied for card $CARD" &
|
||||||
irc_message "[biodoor] unauthorized access denied" &
|
irc_message "[$IDENTITY] unauthorized access denied" &
|
||||||
beep_invalid
|
beep_invalid
|
||||||
else
|
else
|
||||||
log_message "DOOR_UNLOCKED $NAME $CARD" &
|
log_message "DOOR_UNLOCKED $NAME $CARD" &
|
||||||
logger "[biodoor] unlocked by $NAME $CARD" &
|
logger "[$IDENTITY] unlocked by $NAME $CARD" &
|
||||||
irc_message "[biodoor] door unlocked" &
|
irc_message "[$IDENTITY] door unlocked" &
|
||||||
echo 1 > /sys/class/gpio/gpio${GPIO_LOCK}/value
|
echo 1 > /sys/class/gpio/gpio${GPIO_LOCK}/value
|
||||||
beep_unlocked &
|
beep_unlocked &
|
||||||
sleep $LOCK_TIMEOUT
|
sleep $LOCK_TIMEOUT
|
||||||
|
@ -163,15 +180,24 @@ while true; do
|
||||||
CURRENT_OPEN=`cat /sys/class/gpio/gpio${GPIO_SWITCH}/value`
|
CURRENT_OPEN=`cat /sys/class/gpio/gpio${GPIO_SWITCH}/value`
|
||||||
if [ $CURRENT_OPEN -eq 1 -a $OPEN -eq 0 ]; then
|
if [ $CURRENT_OPEN -eq 1 -a $OPEN -eq 0 ]; then
|
||||||
log_message "STATUS_CLOSED" &
|
log_message "STATUS_CLOSED" &
|
||||||
irc_message "[biostatus] update: CLOSED" &
|
irc_message "[${STATUS}] update: CLOSED" &
|
||||||
irc_status "CLOSED" &
|
irc_status "CLOSED" &
|
||||||
|
LASTCHANGE=`date +%s`
|
||||||
|
updateSpaceApi false $LASTCHANGE
|
||||||
IGNORE_ALARM=$IGNORE_ALARM_SET
|
IGNORE_ALARM=$IGNORE_ALARM_SET
|
||||||
|
if [ -n $IMAGE_DST ]; then
|
||||||
|
cp $IMAGE_CLOSED $IMAGE_DST
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
if [ $CURRENT_OPEN -eq 0 -a $OPEN -eq 1 ]; then
|
if [ $CURRENT_OPEN -eq 0 -a $OPEN -eq 1 ]; then
|
||||||
log_message "STATUS_OPEN" &
|
log_message "STATUS_OPEN" &
|
||||||
irc_message "[biostatus] update: OPEN" &
|
irc_message "[${STATUS}] update: OPEN" &
|
||||||
irc_status "OPEN" &
|
irc_status "OPEN" &
|
||||||
|
LASTCHANGE=`date +%s`
|
||||||
|
updateSpaceApi true $LASTCHANGE
|
||||||
|
if [ -n $IMAGE_DST ]; then
|
||||||
|
cp $IMAGE_OPEN $IMAGE_DST
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CURRENT_DOOR=`cat /sys/class/gpio/gpio${GPIO_MAGSWITCH}/value`
|
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
|
if [ $CURRENT_DOOR -eq 1 ] && [ $DOOR -eq 0 ] && [ $OPEN -eq 1 ] && [ $IGNORE_ALARM -eq 0 ]; then
|
||||||
log_message "DOOR_ALARM" &
|
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
|
beep_alarm
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -206,4 +232,5 @@ while true; do
|
||||||
fi
|
fi
|
||||||
|
|
||||||
let LOOP=$LOOP+1
|
let LOOP=$LOOP+1
|
||||||
|
sleep 1
|
||||||
done
|
done
|
||||||
|
|
62
brmdoor-rpi/brmdoor.conf.dist-biodoor
Normal file
62
brmdoor-rpi/brmdoor.conf.dist-biodoor
Normal file
|
@ -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"
|
65
brmdoor-rpi/brmdoor.conf.dist-brmdoor
Normal file
65
brmdoor-rpi/brmdoor.conf.dist-brmdoor
Normal file
|
@ -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"
|
||||||
|
|
||||||
|
|
47
brmdoor-rpi/brmstatus-template.json
Normal file
47
brmdoor-rpi/brmstatus-template.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue