2014-10-01 16:31:55 +02:00
|
|
|
#!/bin/bash
|
|
|
|
IGNORE_DOORLOCKBUTTON="no"
|
|
|
|
LockDir="/run/$(basename "$0").run"
|
2015-01-01 22:24:58 +01:00
|
|
|
spaceapikey="$(cat "$(dirname $0)"/spaceapikey.txt)"
|
2014-10-01 16:31:55 +02:00
|
|
|
P() {
|
|
|
|
while ! mkdir "$LockDir" 2>/dev/null
|
|
|
|
do
|
|
|
|
LockDirStamp=$(stat -c %Y "$LockDir" 2>/dev/null)
|
2015-07-19 19:48:02 +02:00
|
|
|
if [ "$LockDirStamp" != "" ] && [ "$LockDirStamp" -lt $(date --date "300 seconds ago" +%s) ]
|
2014-10-01 16:31:55 +02:00
|
|
|
then
|
|
|
|
rmdir "$LockDir"
|
2015-07-19 19:48:02 +02:00
|
|
|
logger -t $(basename $0) "$$ deleting stale semaphore dir $LockDir"
|
2014-10-01 16:31:55 +02:00
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|
|
|
|
V() {
|
|
|
|
rmdir "$LockDir" 2>/dev/null
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
2015-07-19 19:48:02 +02:00
|
|
|
logger -t $(basename $0) "$$ semaphore dir $LockDir disappeared while running"
|
2014-10-01 16:31:55 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
P
|
|
|
|
if [ ! -f /run/spacestatus ] # self initilizing on new install or boot
|
|
|
|
then
|
|
|
|
if [ -f /root/var/spacestatus ]
|
|
|
|
then # we could also get it from spaceapi, so that could should go here:
|
2015-07-19 19:48:02 +02:00
|
|
|
logger -t $(basename $0) "$$ boot detected, restoring spacestatus to $(cat /root/var/spacestatus)"
|
2014-10-01 16:31:55 +02:00
|
|
|
cp -p /root/var/spacestatus /run/spacestatus # restore from backup
|
|
|
|
else
|
2015-07-19 19:48:02 +02:00
|
|
|
logger -t $(basename $0) "$$ never run before (new install?) setting spacestatus to closed"
|
2014-10-01 16:31:55 +02:00
|
|
|
echo "closed" > /run/spacestatus
|
|
|
|
fi
|
|
|
|
chown www-data /run/spacestatus
|
|
|
|
fi
|
|
|
|
|
2015-01-01 22:24:58 +01:00
|
|
|
status="$(cat /run/spacestatus)"
|
|
|
|
oldstatus="$(cat /root/var/spacestatus)"
|
|
|
|
doorlockbutton="$(cat /run/doorlockbutton)"
|
2014-10-01 16:31:55 +02:00
|
|
|
nai=$(stat -c "%Y" /run/spacestatus) # get mtime as status change time
|
|
|
|
if [ "$status" = "open" ]
|
|
|
|
then
|
2015-01-01 22:24:58 +01:00
|
|
|
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"state":{"open":true,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
|
2015-07-19 19:48:02 +02:00
|
|
|
#logger -t $(basename $0) "$$ sending status $status to spacapi ret=$?"
|
2014-10-01 16:31:55 +02:00
|
|
|
fi
|
|
|
|
for plugin in $(ls "$0".d)
|
|
|
|
do
|
2015-01-01 22:24:58 +01:00
|
|
|
if [ -x "$0".d/"$plugin" ]
|
|
|
|
then
|
|
|
|
"$0".d/"$plugin" "$status" "$oldstatus"
|
2015-07-19 19:48:02 +02:00
|
|
|
logger -t $(basename $0) "$$ called $plugin '$status' '$oldstatus'. ret=$?"
|
2015-01-01 22:24:58 +01:00
|
|
|
fi
|
2014-10-01 16:31:55 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$status" = "closed" ] && (
|
|
|
|
[ "$IGNORE_DOORLOCKBUTTON" = "yes" ] || [ "$doorlockbutton" = "pushed" ] )
|
|
|
|
then
|
|
|
|
# problem: if closing state but not actually shuting door for a longer time, the status in spaceapi
|
|
|
|
# will be the time of closing but not that of actually shutting the door
|
|
|
|
# but the status will only be updated once the door is shut
|
2015-01-01 22:24:58 +01:00
|
|
|
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"state":{"open":false,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
|
2015-07-19 19:48:02 +02:00
|
|
|
#logger -t $(basename $0) "$$ sending status $status to spacapi ret=$?"
|
2014-10-01 16:31:55 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $nai -ne $(stat -c "%Y" /root/var/spacestatus) ] # backup file in case it changed
|
|
|
|
then
|
|
|
|
cp -p /run/spacestatus /root/var/spacestatus
|
2015-07-19 19:48:02 +02:00
|
|
|
logger -t $(basename $0) "$$ spacestatus changed, saving to SD. ret=$?"
|
2014-10-01 16:31:55 +02:00
|
|
|
fi
|
|
|
|
presency=$(cat /run/presency)
|
|
|
|
if [ "$status" = "closed" ]
|
|
|
|
then
|
|
|
|
presency=0
|
|
|
|
fi
|
2015-01-01 22:24:58 +01:00
|
|
|
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"sensors":{"people_now_present":[{"value":'"$presency"'}]}}' http://spaceapi.syn2cat.lu/sensor/set
|
2014-10-01 16:31:55 +02:00
|
|
|
V
|