pidor/scripts/upd_status.sh

78 lines
2.9 KiB
Bash
Raw Normal View History

2014-10-01 16:31:55 +02:00
#!/bin/bash
IGNORE_DOORLOCKBUTTON="no"
LockDir="/run/$(basename "$0").run"
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)
if [ "$LockDirStamp" != "" ] && [ "$LockDirStamp" -lt $(date --date "300 seconds ago" +%s) ]
2014-10-01 16:31:55 +02:00
then
rmdir "$LockDir"
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
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:
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
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
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
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"state":{"open":true,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
#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
if [ -x "$0".d/"$plugin" ]
then
"$0".d/"$plugin" "$status" "$oldstatus"
logger -t $(basename $0) "$$ called $plugin '$status' '$oldstatus'. ret=$?"
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
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"state":{"open":false,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
#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
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
/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