forked from Smiril/proHomeeStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_presence_scanner.sh
149 lines (132 loc) · 3.5 KB
/
bluetooth_presence_scanner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Thanks to https://github.com/mavnezz/proHomeeStatus
# Need: sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
#
# -------------------------
# Settings (edit here)
# -------------------------
# bluetooth tags and user names
tags=("E8:D1:x:xx:xx:x;User A" "EA:64:xx:xx:xx:x;User B")
# workingdir - consider using a ramdisk, as a lot of i/o is generated
tmpdir="/ramdisk"
tmpfile="scan.txt"
# iterations before "away" => away * scaninterval = time to advertise leave
away=30
# interval between scans in seconds
scaninterval=15
# send additional webhook if all are away
# for_all=true yes
# for_all=false no
for_all=true
# webhook message payload after user name
returnmsg="is at home"
leavemsg="has left"
allmsg="Nobody at home"
# Webhook function, adopt to your needs. Sends payload as POST data
webhookurl="http://192.168.0.10:1880/hook"
webhooktoken="!SECRET"
sendwebhook () {
curl -s \
--form-string "message=$1" \
--form-string "token=$2" \
$webhookurl > /dev/null 2>&1
}
# ----------------------
# do not edit below here
# ----------------------
# prevent multiple instances
script="${0##*/}"
for pid in $(pidof -x $script); do
if [ $pid != $$ ]; then
echo "[$(date)] : $script : Process is already running with PID $pid"
exit 1
fi
done
sudo hciconfig hci0 down
sleep 1
sudo hciconfig hci0 up
reset
echo "Tag Scanner with webhooks"
echo ""
# Define arrays for counting
declare -A AThome
declare -A AThome_counter
declare allaway_hook=false
# Whitelist clear
sudo hcitool lewlclr
# tags into Whitelist
echo "valid tags"
for i in ${tags[@]}; do
echo $i
sudo hcitool lewladd --random ${i:0:17}
AThome[${i:18}]=false
AThome_counter[${i:18}]=0
if [ $? -eq 1 ]; then
echo "Bluetooth error; not installed?"
exit
fi
done
echo ""
while true; do
sleep 1
sudo hcitool lescan --whitelist > $tmpdir/$tmpfile & sleep $scaninterval && sudo pkill --signal SIGINT hcito
reset
while read -r line; do
if [ "${line:0:7}" != "LE Scan" ]; then
for ((index=0; index<${#tags[@]}; index++)); do
if [ "${tags[$index]:0:17}" = ${line:0:17} ]; then
user="${tags[$index]:18}"
AThome_counter[$user]=0
if [ ${AThome[$user]} = false ]; then
AThome[$user]=true
allaway_hook=false
sendwebhook "$user $returnmsg" "$webhooktoken"
fi
fi
done
fi
done <$tmpdir/$tmpfile
# check if user is away
for i in "${!AThome_counter[@]}"; do
if [ ${AThome_counter[$i]} -eq "$away" ]; then
AThome[$i]=false
sendwebhook "$i $leavemsg" "$webhooktoken"
fi
done
# increase away counter by 1
for i in "${!AThome_counter[@]}"; do
if [ ${AThome[$i]} = false ]; then
status="away"
else
status="@home"
fi
echo ""
echo "User: $i"
echo "Status: $status"
echo "Counter: ${AThome_counter[$i]} (max. $away)"
AThome_counter["$i"]=$((${AThome_counter[$i]}+1))
done
# set away if everyone left
if [ "$for_all" = true ]; then
allaway=true
for i in "${!AThome[@]}"; do
if [ ${AThome[$i]} = true ]; then
allaway=false
fi
done
if [ "$allaway" = true ]; then
if [ "$allaway_hook" = false ]; then
allaway_hook=true
sendwebhook "$allmsg" "$webhooktoken"
fi
fi
if [ "$allaway" = true ]; then
status="away"
else
status="@home"
fi
echo ""
echo "User: all"
echo "Status: $status"
fi
done