-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockitron.sh
executable file
·131 lines (119 loc) · 3.4 KB
/
lockitron.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
#!/bin/bash
if [[ -z "$CONFIG" ]]
then
CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}/lockitron
fi
API_URL=https://api.lockitron.com/v2
source $CONFIG
getdevices() {
curl -s "$API_URL/locks?access_token=$ACCESS_TOKEN" | jq "[.[] | {name,id,hardware_id}]"
}
getlockid() {
getdevices | jq ".[] | select(.name==\"$1\")" | jq -r ".id"
}
getlockhwid() {
getdevices | jq ".[] | select(.name==\"$1\")" | jq -r ".hardware_id" | grep -o -E "[0-9a-fA-F]{8}$"
}
findlockbleaddr() {
DEVS=$(hcitool lescan & (sleep 2 ; pkill --signal SIGINT hcitool))
REGEX="([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"
DEV=$(echo $DEVS | grep -o -E "$REGEX $1" | grep -o -E "$REGEX")
echo $DEV
}
signalble() {
hcitool lecc $1 > /dev/null 2>&1 &
}
case $1 in
list)
all_devices=$(getdevices)
echo $all_devices | jq -r ".[].name"
;;
status)
if [[ -z "$2" ]]
then
echo "Specify a unit to retrieve the status"
exit -1
else
lock_id=$(getlockid "$2")
status=$(curl -s "$API_URL/locks/$lock_id?access_token=$ACCESS_TOKEN" | jq '{state,avr_update_progress,ble_update_progress,updated_at,next_wake,pending_activity,serial_number,hardware_id}')
lock_state=$(echo $status | jq -r '.state')
next_wake_utc=$(echo $status | jq -r '.next_wake')
if [[ "$next_wake_utc" == "null" ]] ; then
next_wake="NA"
else
next_wake=$(date -d "$next_wake_utc")
fi
last_update_utc=$(echo $status | jq -r '.updated_at')
last_update=$(date -d "$last_update_utc")
avr_progress=$(echo $status | jq -r '.avr_update_progress')
if [[ "$avr_progress" == "null" ]] ; then
avr_progress="NA"
fi
ble_progress=$(echo $status | jq -r '.ble_update_progress')
if [[ "$ble_progress" == "null" ]] ; then
ble_progress="NA"
fi
pending_id=$(echo $status | jq -r '.pending_activity.id')
serial_num=$(echo $status | jq -r '.serial_number')
hardware_id=$(echo $status | jq -r '.hardware_id')
echo "State of lock \"$2\"
State: $lock_state
Last Updated: $last_update
Next Update: $next_wake"
if [[ "$avr_progress" -ne "100" ]]
then
if [[ "$avr_progress" != "NA" ]] ; then
avr_progress="$avr_progress%"
fi
echo "AVR Progress: $avr_progress"
fi
if [[ "$ble_progress" -ne "100" ]]
then
if [[ "$ble_progress" != "NA" ]] ; then
ble_progress="$ble_progress%"
fi
echo "BLE Progress: $ble_progress"
fi
if [[ "$pending_id" == "null" ]] ; then
pending_id="NA"
fi
echo "Pending: $pending_id"
echo "Serial#: $serial_num"
if [[ "$hardware_id" == "null" ]] ; then
hardware_id="NA"
fi
echo "Hardware ID: $hardware_id"
fi
;;
lock|unlock)
if [[ -z "$2" ]]
then
echo "Specify a unit to $1"
exit -1
else
if [[ "$3" == "immed" ]] ; then
if (( EUID != 0 )) ; then
echo "You must be root to $1 immediately." 1>&2
exit 100
fi
fi
lock_id=$(getlockid "$2")
if [[ "$3" == "immed" ]] ; then
lock_hwid=$(getlockhwid "$2")
lock_bleaddr=$(findlockbleaddr $lock_hwid)
signalble "$lock_bleaddr"
fi
curl -X PUT -s "$API_URL/locks/$lock_id?access_token=$ACCESS_TOKEN&state=$1"
fi
;;
firmware)
if [[ -z "$2" ]]
then
echo "Specify a unit to update firmware on"
exit -1
else
lock_id=$(getlockid "$2")
curl -X PUT -s "$API_URL/locks/$lock_id?access_token=$ACCESS_TOKEN&update_avr_firmware=true&update_ble_firmware=true"
fi
;;
esac