-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathathome.sh
More file actions
executable file
·138 lines (125 loc) · 2.79 KB
/
athome.sh
File metadata and controls
executable file
·138 lines (125 loc) · 2.79 KB
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
#!/usr/bin/env bash
verbose=false
logfile=false
while getopts :hvli: flag; do
case "${flag}" in
h)
echo -e "athome"
echo -e "USAGE: athome [-h] [-v] [-i <kLux>]"
echo -e "OPTIONS:"
echo -e "-h\t\tShow this message"
echo -e "-v\t\tVerbose output"
echo -e "-i <kLux>\tSet the illumination threshold"
exit 0
;;
v)
verbose=true
;;
l)
logfile=true
;;
i)
threshold=${OPTARG}
;;
*)
echo -e "ERROR: Unknown option ${flag}" >&2
exit 1
;;
esac
done
logger(){
if $verbose; then
echo "$(date): $@"
fi
if $logfile; then
echo "$(date): $@" >> ./athome.log
fi
}
get_ipstate(){
ipstate=$(sshpass -p Auber2112ine? ssh duckwilliam@duckwilliam.asuscomm.com -p 8022 ipcheck | grep -qi -e "iphone.*online" -e "online.*iphone" && echo $?)
if [[ $ipstate -eq 0 ]]; then
ip_found=1
else
ip_found=3
fi
echo "$ip_found"
}
get_light_set(){
if ! $threshold; then
threshold=400
#logger "No threshold set, using default value of $threshold"
fi
#logger "Threshold set to $threshold"
illumination=$(python -m illumipy)
if [[ $illumination -lt $threshold ]]; then
lights_set=0
else
lights_set=1
fi
echo "$lights_set"
}
get_light_state() {
hueBridge='duckwilliam.asuscomm.com' # Enter the IP address of your Philips Hue Bridge
huePort='8999' # Dont change unless you know what you're doing
hueApiHash='BojwEitzypKM4XP-BFNC3dtlukO-W8lmI-a1qMdh' # Enter your Hue API hash (s.a.)
hueBaseUrl="http://$hueBridge:$huePort/api/$hueApiHash" # Don't change
hueTimeOut='5' # Dont change unless you know what youre doing
light_state="$(curl --silent --max-time $hueTimeOut --request GET ${hueBaseUrl}/groups/6/ | grep -qi 'any_on..true'; echo "${PIPESTATUS[1]}")"
if [[ $light_state -eq 0 ]]; then
lights_on=1
else
lights_on=10
fi
echo "$lights_on"
}
get_hash_val(){
lights_set=$1
lights_on=$2
ip_found=$3
hash_val=$(( (lights_set + lights_on) * ip_found ))
if (( $hash_val %3 == 0 )); then
ishome=1 # is not home
asleep=2 # is not asleep
else
if [[ $hash_val -ge 10 ]]; then
asleep=20 # is asleep
else
asleep=2 # is not asleep
fi
ishome=10 # is home
fi
hash_val2=$(( (ishome + asleep) / 3 ))
echo "$hash_val2"
}
printer(){
case $1 in
1)
echo -e "away\n$2"
;;
4)
echo -e "home\nawake"
;;
10)
echo -e "home\nasleep"
;;
esac
}
main(){
lights_on=$(get_light_state)
lights_set=$(get_light_set)
ip_found=$(get_ipstate)
hash=$(get_hash_val $lights_set $lights_on $ip_found)
logger "lights on: $lights_on"
logger "lights should be on: $lights_set"
logger "ip found: $ip_found"
logger "hash: $hash"
if [[ $lights_on -eq 0 ]]; then
lighttext="lights=on"
else
lighttext="lights=off"
fi
result=$(printer $hash $lighttext)
echo $result
}
main
#echo $(lighttest)