Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit c6407b5

Browse files
author
pidor
committed
send email when peoplecounter is offline
1 parent f78adee commit c6407b5

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ spaceapikey.txt
33
pychromecast
44
beamerip.txt
55
peoplecounterip.txt
6+
mailconfig.py

README.txt

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ tzselect
2525
comment out the last 4 lines of rsyslogd.conf (for xconsole)
2626
like explained here https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=122601
2727

28+
echo "pidor" > /etc/hostname
29+
sed -i 's/^127.0.1.1.*/127.0.1.1 pidor/' /etc/hosts
30+
31+
2832
cp -p systemfiles/sudoers.d/* /etc/sudoers.d/
2933

3034
mkdir /root/var # kinda important

scripts/mkmail.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/python
2+
import smtplib
3+
import sys
4+
import mailconfig as cnf
5+
server = smtplib.SMTP(cnf.mailserver, 25)
6+
if (len(sys.argv) < 3):
7+
print "usage: "+sys.argv[0]+" 'subject' 'body text'"
8+
sys.exit()
9+
msg = "Subject: " + sys.argv[1] + "\n\n" + sys.argv[2]
10+
server.sendmail(cnf.mailsource, cnf.maildestination, msg)
11+
server.quit()

scripts/peoplecounter-realtime.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ else
1010
fi
1111
STATSFILE="/run/peoplecounter$DEV"
1212
SAMPLES=20 # how many records to keep in file
13-
INTERVAL=0 # how long to wait between polls
13+
INTERVAL=1 # how long to wait between polls
1414
INTERVALSKIP=20 # poll with INTERVAL but only consider every INTERVALSKIP's for SAMPLES
1515
MAXFILE="/var/cache/peoplecountermax$DEV"
1616
PRESENCYRT="/run/presencyrt$DEV"
@@ -59,7 +59,7 @@ while true
5959
do
6060
# scrape new value
6161
p="$(
62-
wget -qO - "http://$PEOPLECOUNTERIP/output.cgi?t=$(date +%s)" |
62+
wget --tries=3 --timeout=2 -qO - "http://$PEOPLECOUNTERIP/output.cgi?t=$(date +%s)" |
6363
sed 's/.*Occupancy://'|
6464
awk '{print $2}')"
6565
# echo "p=$p" # debug
@@ -113,12 +113,17 @@ do
113113
then
114114
state="online"
115115
logger $(basename $0) people counter online
116+
$(dirname "$0")"/mkmail.py 'peoplecounter level2 online' 'peoplecounter is now reachable'
117+
#python -c 'import smtplib ; server = smtplib.SMTP("mail.syn2cat.lu", 25) ; msg = "Subject: peoplecounter level2 onfline\n\npeoplecounter is now reachable" ; server.sendmail("[email protected]", "[email protected]", msg) ; server.quit()'
116118
fi
117119
else
118120
if [ "$state" = "online" ]
119121
then
120122
state="offline"
121123
logger $(basename $0) people counter offline
124+
$(dirname "$0")"/mkmail.py 'peoplecounter level2 offline' 'peoplecounter was not reachable'
125+
#python -c 'import smtplib ; server = smtplib.SMTP("mail.syn2cat.lu", 25) ; msg = "Subject: peoplecounter level2 offline\n\npeoplecounter was not reachable" ; server.sendmail("[email protected]", "[email protected]", msg) ; server.quit()'
126+
122127
fi
123128
fi
124129
sleep "$INTERVAL"

scripts/upd_status.sh.d/peoplecountercheck

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
if [ "$1" = "closed" ] && [ "$2" = "open" ]
44
then
55
p="$(tail -1 /run/peoplecounter)"
6-
if [ "$p" != "0" ]
6+
pp="$(tac /run/peoplecounter | awk -v p="$p" 'p!=$1 {print;exit}')" # get previous count. this may fail if people exit and after long time close door
7+
if [ "$pp" = "" ]
78
then
8-
logger $(basename $0) There are $p people, closed door from inside. Waiting 1 min.
9+
pp=0 # if no variation found, don not do anything
10+
fi
11+
if [ "$p" != "0" ] && # there are people present
12+
[ "$p" -ge "$pp" ] # nobody left i.e. current count is greater or equal to previous count
13+
then
14+
logger $(basename $0) There are $p people, previusly $pp, closed door from inside. Waiting 1 min.
915
sleep 60
10-
p="$(tail -1 /run/peoplecounter)"
11-
if [ "$p" = "0" ]
16+
np="$(tail -1 /run/peoplecounter)"
17+
if [ "$np" -lt "$p" ] # new count is lower than previous
1218
then
13-
logger $(basename $0) There are $p people, after 1 min wait. All OK.
19+
logger $(basename $0) There are now $np people, after 1 min wait. All OK.
1420
else
15-
logger $(basename $0) There are $p people, after 1 min. Maybe ALARM
21+
logger $(basename $0) There are $np people, after 1 min. Maybe ALARM
22+
python -c 'import smtplib ; server = smtplib.SMTP("mail.syn2cat.lu", 25) ; msg = "Subject: ALARM level2 intruder\n\ndoor closed with people inside" ; server.sendmail("[email protected]", "[email protected]", msg) ; server.quit()'
23+
1624
fi
1725
else
1826
logger $(basename $0) There are $p people, correctly closed door from outside. All OK.

0 commit comments

Comments
 (0)