forked from teyeheimans/domoticz-bash-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphone-presence.sh
executable file
·195 lines (163 loc) · 4.62 KB
/
phone-presence.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
LOCKFILE="/tmp/synclock"
# Enter your domoticz account details here.
DOMOTICZ_USER="username"
DOMOTICZ_PASS="p@ssw0rd"
DOMOTICZ_PIN=12344
DOMOTICZ_URL="https://domoticz.url:8443";
# attempts before we assume it's offline
COOLDOWN_COUNTER=5;
type hping3 >/dev/null 2>&1 || { echo >&2 "I require hping3 but it's not installed. Aborting."; exit 1; }
type jq >/dev/null 2>&1 || { echo >&2 "I require JQ but it's not installed. Aborting."; exit 1; }
#
# Here is a list of devices we want to check.
# Each line has:
# '${idx} ${ip} ${mac-address}'
#
# Note! Mac address is case sensitive!
#
PHONE_DEVICES=(
'72 10.0.1.213 34:ee:fd:60:9a:11'
'71 10.0.1.212 e0:ff:45:9e:2c:22'
);
MYPATH=$(dirname $0);
VERBOSE=0;
if [[ "$#" -ge 1 ]] ;
then
if [[ $1 == "verbose" ]] || [[ $1 == "--verbose" ]] || [[ $1 == "-v" ]];
then
VERBOSE=1;
fi;
fi;
#
# Include variables file, if exists
#
if [ -f "${MYPATH}/phone-presence.conf" ];
then
source "${MYPATH}/phone-presence.conf"
fi
JQ=`which jq`
HPING=`which hping3`
# check if there is a lockfile
if [ -f $LOCKFILE ];
then
# Yes, there is a lockfile.
# Check if it is more than 60 minutes old
if test `find "$LOCKFILE" -mmin +60`
then
# Lockfile was too old, ignore it and remove it
rm $LOCKFILE
else
PID=`cat $LOCKFILE`
if ps -p $PID > /dev/null 2>/dev/null
then
# Lockfile was new enough, and process still exists. Do NOT run this cron!
if [[ $VERBOSE -eq 1 ]]
then
echo "Job was already running" >/dev/stderr
fi
exit
else
# Lockfile was new enough, however process does not exist anymore. Remove lockfile and continue.
rm $LOCKFILE
fi
fi
fi
#
# Display verbose output if wanted
#
function verbose {
if [[ $VERBOSE -eq 1 ]];
then
echo $1;
fi
}
#
# Function to check device
#
function check_device {
idx=$1
ip=$2
mac=$3
# Ping device to fetch ARP record
verbose "Executing hping command:"
verbose "$HPING -2 -c 10 -p 5353 -i u1 ${ip} -q"
$HPING -2 -c 10 -p 5353 -i u1 ${ip} -q >/dev/null 2>&1;
# Fetch all current devices
declare -a DEVICES
DEVICES=`/usr/sbin/arp -an | awk '{print $4}'`
verbose "Idx: ${idx}, ip: ${ip}, mac: ${mac}";
# Fetch known state
json=$(curl -u ${DOMOTICZ_USER}:${DOMOTICZ_PASS} --connect-timeout 10 -k -s "${DOMOTICZ_URL}/json.htm?type=devices&filter=all&used=true&order=Name")
status=`echo $json | ${JQ} -r '.status'`
verbose "Status of request: ${status}";
if [[ "${status}" == "OK" ]] ;
then
known_state=$(echo $json | ${JQ} -r ".result[] | select(.idx == \"${idx}\") | .Status")
verbose "Current device status: ${known_state}"
else
echo >&2 "${DOMOTICZ_URL}/json.htm?type=devices&filter=all&used=true&order=Name";
echo >&2 "Error, failed to fetch JSON response from domoticz";
exit 1;
fi
failfile="/tmp/${idx}-offline-check"
if [ -f ${failfile} ];
then
failcounter=`cat ${failfile}`
else
failcounter=1;
fi
verbose "Mac address: ${mac}"
#echo ${DEVICES[*]}
if [[ ${DEVICES[*]} =~ ${mac} ]]
then
verbose "State: ON, known state: ${known_state}"
if [ -f ${failfile} ];
then
rm "${failfile}";
fi
if [ "${known_state}" != "On" ];
then
verbose "UPDATE STATE TO ON!";
response=$(curl -u ${DOMOTICZ_USER}:${DOMOTICZ_PASS} --connect-timeout 10 -k -s "${DOMOTICZ_URL}/json.htm?type=command¶m=switchlight&idx=${idx}&switchcmd=On&level=0&passcode=${DOMOTICZ_PIN}")
verbose "$response"
fi
else
verbose "State: OFF, known sate: ${known_state}, attempt ${failcounter}/${COOLDOWN_COUNTER}";
if [ "${known_state}" != "Off" ];
then
((failcounter++));
# store max attempts
echo "${failcounter}" > "${failfile}";
if [[ ${failcounter} -le ${COOLDOWN_COUNTER} ]];
then
verbose "We are still in cooldown period. Do nothing...";
else
verbose "UPDATE STATE TO OFF!";
response=$(curl -u ${DOMOTICZ_USER}:${DOMOTICZ_PASS} --connect-timeout 10 -k -s "${DOMOTICZ_URL}/json.htm?type=command¶m=switchlight&idx=${idx}&switchcmd=Off&level=0&passcode=${DOMOTICZ_PIN}")
verbose "$response"
fi
else
verbose "Known state is already off. Do nothing...";
if [ -f ${failfile} ];
then
rm "${failfile}";
fi
fi
fi
verbose "";
}
#
# Walk all devices to check
#
i=0;
while [ "$i" -lt "${#PHONE_DEVICES[@]}" ];
do
DEVICE=(${PHONE_DEVICES[i]});
IDX=${DEVICE[0]};
IP=${DEVICE[1]};
MAC=${DEVICE[2]};
verbose "=== Starting scan for device with idx ${IDX} on ip ${IP} with Mac address ${MAC} === ";
check_device ${IDX} ${IP} ${MAC};
((i++));
done