From 3e7027dd82cd4794ea0ed9c561865e5a009aa03c Mon Sep 17 00:00:00 2001 From: Level2 Pidor Date: Sun, 17 Jan 2016 19:45:44 +0100 Subject: [PATCH] implement real people counter hardware fixed IP for projector add readme.txt --- README.txt | 23 +++++++++++++ scripts/beamerdetect.sh | 33 +++++++++++++++--- scripts/dhcp2presency.sh | 17 +++++++++- .../{projector.sh => 01projector.sh} | 0 scripts/lockbutton.sh.d/changestatus | 4 +++ scripts/peoplecounter-realtime.sh | 34 +++++++++++++++++++ scripts/upd_status.sh | 1 + scripts/upd_status.sh.d/peoplecountercheck | 15 ++++++++ scripts/ws4beamer_status.py | 23 +++++++++++++ systemfiles/inittab | 3 ++ 10 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 README.txt rename scripts/lockbutton.sh.d/{projector.sh => 01projector.sh} (100%) create mode 100755 scripts/peoplecounter-realtime.sh create mode 100755 scripts/upd_status.sh.d/peoplecountercheck create mode 100755 scripts/ws4beamer_status.py diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..b447084 --- /dev/null +++ b/README.txt @@ -0,0 +1,23 @@ +Pidor is controlling the state of Level2 on the website, spaceapi and twitter + +This documentation is work in progress, sorry about that. + +Install +put this in /etc/inittab (see example in systemfiles/inittab) +# pidor +P0:2345:respawn:/root/pidor/scripts/lockbutton.sh +P1:2345:respawn:/root/pidor/scripts/beamerdetect.sh +P2:2345:respawn:/root/pidor/scripts/ws4beamer_status.py +P3:2345:respawn:/root/pidor/scripts/peoplecounter-realtime.sh + +put the beamer IP into beamerip.txt +put the peoplecounter ip into peoplecounterip.txt + +fill in the crontab +* * * * * /root/pidor/scripts/dhcp2presency.sh +* * * * * /root/pidor/scripts/upd_status.sh > /run/spacestatus.out 2>&1 + +needs an apache, for some silly historic reason, and for the lights commander +cd ~/pidor/www && ./intallwebsite.sh + + diff --git a/scripts/beamerdetect.sh b/scripts/beamerdetect.sh index f6bad37..bb29b5d 100755 --- a/scripts/beamerdetect.sh +++ b/scripts/beamerdetect.sh @@ -1,19 +1,34 @@ #!/bin/bash +BEAMERIP=$(cat $(dirname "$0")"/beamerip.txt") +function raisescreen() { + echo "Rolling projection screen up" + ssh pi@doorbuzz 'doorbuzz/projectionscreen.sh up' +} +function pingall() { + i=1 + while [ $i -lt 255 ] + do + ping -c 1 10.2.113.$i -q >/dev/null & + i=$((i+1)) + done + wait +} if [ "$1" = "off" ] then ( echo "called with parameter $1" - projip=$(arp -an|awk -F'[()]' '/00:50:41:79:d1:34/{print $2}') + projip="$BEAMERIP" if [ "$projip" = "" ] then echo "no projector IP found" - arp -a + arp -a + raisescreen exit fi signalsource="$(wget -qO - 'http://'"$projip"'/tgi/return.tgi?query=info' |awk -F'[<>]' '//{print substr($3,33,2)}')" if [ "$signalsource" = "00" ] || [ "$signalsource" = "15" ] || [ "$signalsource" = "" ] then - ssh pi@doorbuzz 'doorbuzz/projectionscreen.sh up' + raisescreen echo "wget http://$projip/tgi/return.tgi?command=2a3102fd0660 #projector off" wget -qO - 'http://'"$projip"'/tgi/return.tgi?command=2a3102fd0660' 2>&1 echo $? @@ -22,12 +37,20 @@ then fi ) | logger -t "$(basename $0) $$" exit +fi & +if [ "$1" = "off" ] +then + exit # because the if before is in background fi prevstatus="unknown" while true do - projip=$(arp -an|awk -F'[()]' '/00:50:41:79:d1:34/{print $2}') - # from the acer webpage we read that bytes 30-31 contain 00 if poer off and 01 if power on + if [ $(date +%H) -eq 23 ] + then + pingall + fi + projip="$BEAMERIP" + # from the acer webpage we read that bytes 30-31 contain 00 if power off and 01 if power on # we only test if 01, because if off, it can also give no response # but seems to be bytes 32-33 more accurate statusbyte="$(wget -qO - 'http://'"$projip"'/tgi/return.tgi?query=info'|awk -F'[<>]' '//{print substr($3,31,2)}')" diff --git a/scripts/dhcp2presency.sh b/scripts/dhcp2presency.sh index 17702e6..04c6fe3 100755 --- a/scripts/dhcp2presency.sh +++ b/scripts/dhcp2presency.sh @@ -1,4 +1,16 @@ #!/bin/bash +PEOPLECOUNTERIP=$(cat $(dirname "$0")"/peoplecounterip.txt") +# we now have a people counter +p="$( + wget -qO - "http://$PEOPLECOUNTERIP/output.cgi?t=$(date +%s)" | + sed 's/.*Occupancy://'| + awk '{print $2}')" +if [ "$p" != "" ] +then + echo "$p" > /run/presency +else + logger $0 cannot access people counter. fallback to dhcp value guessing +fi n=0 for i in $(ls /run/dhcp-leases/) do @@ -18,4 +30,7 @@ do fi fi done -echo "$n" > /run/presency +if [ "$p" = "" ] # write DHCP count if people counter offline +then + echo "$n" > /run/presency +fi diff --git a/scripts/lockbutton.sh.d/projector.sh b/scripts/lockbutton.sh.d/01projector.sh similarity index 100% rename from scripts/lockbutton.sh.d/projector.sh rename to scripts/lockbutton.sh.d/01projector.sh diff --git a/scripts/lockbutton.sh.d/changestatus b/scripts/lockbutton.sh.d/changestatus index b46c66d..b36389f 100755 --- a/scripts/lockbutton.sh.d/changestatus +++ b/scripts/lockbutton.sh.d/changestatus @@ -2,6 +2,10 @@ # changes level2 state by the state of the main door # i.e. people have to leave it open to keep status open sleep 1 # silly way to debounce +if [ ! -f /run/spacestatus ] # needed to init after boot +then + cp -p /root/var/spacestatus /run/spacestatus +fi if [ "$1" = "pushed" ] && [ "$(cat /run/spacestatus)" = "open" ] then diff --git a/scripts/peoplecounter-realtime.sh b/scripts/peoplecounter-realtime.sh new file mode 100755 index 0000000..fd51048 --- /dev/null +++ b/scripts/peoplecounter-realtime.sh @@ -0,0 +1,34 @@ +#!/bin/bash +#update peoplecounter number in realtime +#but: this is too intrusive. should only update when the count of +@ people has not changed for a certain time +PEOPLECOUNTERIP=$(cat $(dirname "$0")"/peoplecounterip.txt") +state="online" +while true +do + p="$( + wget -qO - "http://$PEOPLECOUNTERIP/output.cgi?t=$(date +%s)" | + sed 's/.*Occupancy://'| + awk '{print $2}')" + if [ "$p" != "" ] + then + oldp="$(cat /run/peoplecounter)" + echo "$p" > /run/peoplecounter + if [ "$p" != "$oldp" ] + then + logger $0 changed from $oldp to $p people + fi + if [ "$state" = "offline" ] + then + state="online" + logger $0 people counter online + fi + else + if [ "$state" = "online" ] + then + state="offline" + logger $0 people counter offline + fi + fi + sleep 10 +done diff --git a/scripts/upd_status.sh b/scripts/upd_status.sh index 0b508d7..422d07e 100755 --- a/scripts/upd_status.sh +++ b/scripts/upd_status.sh @@ -1,4 +1,5 @@ #!/bin/bash +sleep 5 IGNORE_DOORLOCKBUTTON="no" LockDir="/run/$(basename "$0").run" spaceapikey="$(cat "$(dirname $0)"/spaceapikey.txt)" diff --git a/scripts/upd_status.sh.d/peoplecountercheck b/scripts/upd_status.sh.d/peoplecountercheck new file mode 100755 index 0000000..6364921 --- /dev/null +++ b/scripts/upd_status.sh.d/peoplecountercheck @@ -0,0 +1,15 @@ +#!/bin/bash +# this script only makes sense if there is a good people counter installed +if [ "$1" = "closed" ] && [ "$2" = "open" ] +then + p="$(cat /run/presency)" + if [ "$p" != "0" ] + then + logger $0 There are $p people, closed door from inside waiting 1 min. + sleep 60 + p="$(cat /run/presency)" + logger $0 There are $p people, closed door from inside after 1 min. + else + logger $0 There are $p people, correctly closed door from outside. + fi & +fi diff --git a/scripts/ws4beamer_status.py b/scripts/ws4beamer_status.py new file mode 100755 index 0000000..3fcb4b6 --- /dev/null +++ b/scripts/ws4beamer_status.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python2.7 +import requests +import flask +import re + +app = flask.Flask(__name__) + +@app.route("/") +def status(): + try: + res = requests.get("http://10.2.113.7/tgi/return.tgi?query=info", timeout=1) + if "NG" in res.content: + return "0" + reg_res = re.findall(b"([^<]*)", res.content) + status = str(reg_res[0][30:32]) + if status == "01": + return "1" + except Exception as e: + print(e) + return "0" + +if __name__ == "__main__": + app.run("0.0.0.0", 5042) diff --git a/systemfiles/inittab b/systemfiles/inittab index 07985e1..33ce8df 100644 --- a/systemfiles/inittab +++ b/systemfiles/inittab @@ -74,3 +74,6 @@ T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 # pidor P0:2345:respawn:/root/pidor/scripts/lockbutton.sh P1:2345:respawn:/root/pidor/scripts/beamerdetect.sh +P2:2345:respawn:/root/pidor/scripts/ws4beamer_status.py +P3:2345:respawn:/root/pidor/scripts/peoplecounter-realtime.sh +