-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtesla-commands.sh
executable file
·223 lines (196 loc) · 6.37 KB
/
tesla-commands.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# shellcheck shell=dash
#
# tesla.sh
#
###
##
# teslaCtrlSendCommand
##
###
teslaCtrlSendCommand() {
# Process in case of nested call (autowake)
if [ $# -eq 4 ]; then
log_debug "teslaCtrlSendCommand; Nested call: in. Set callMode and copy internal variables"
vin_previous=$vin
command_previous=$command
commandDescription_previous=$commandDescription
callMode="nested"
else
log_debug "teslaCtrlSendCommand; Standard call: in."
callMode="standard"
fi
# Set internal variables
vin=$1
command=$2
commandDescription=$3
# Prepare for calling tesla-control
export TESLA_VIN=$vin
export TESLA_KEY_FILE=$KEYS_DIR/${vin}_private.pem
export TESLA_KEY_NAME=$KEYS_DIR/${vin}_private.pem
# shellcheck disable=SC2016
TESLA_CONTROL_CMD='/usr/bin/tesla-control -ble -command-timeout 20s $command 2>&1'
# Retry loop
max_retries=5
for sendCommandCount in $(seq $max_retries); do
log_notice "Attempt $sendCommandCount/${max_retries} sending $commandDescription to vin:$vin command:$command"
set +e
teslaCtrlOut=$(eval $TESLA_CONTROL_CMD)
EXIT_STATUS=$?
set -e
if [ $EXIT_STATUS -eq 0 ]; then
log_debug "teslaCtrlSendCommand; $teslaCtrlOut"
log_info "Command $command was successfully delivered to vin:$vin"
# Finalize in case of nested call (autowake)
if [[ "$callMode" == "nested" ]]; then
log_debug "teslaCtrlSendCommand; Nested call: out. Set callMode back to standard and revert internal variables"
vin=$vin_previous
command=$command_previous
commandDescription=$commandDescription_previous
callMode="standard"
else
log_debug "teslaCtrlSendCommand; Standard call: out."
fi
return 0
else
if [[ "$teslaCtrlOut" == *"car could not execute command"* ]]; then
log_warning "teslaCtrlSendCommand; $teslaCtrlOut"
log_warning "Skipping command $command to vin:$vin"
return 10
elif [[ "$teslaCtrlOut" == *"context deadline exceeded"* ]]; then
# TODO check that this situation appears only once (or few)
# to avoid getting into a loop if we cannot wake the car
# if this happens the "else" will never be triggered and the command will never exit
# it would be possible to parse the return code of teslaCtrlSendCommand to check that wake succeeded
log_debug "teslaCtrlSendCommand; txt deadline exc. - IN"
log_warning "teslaCtrlSendCommand; $teslaCtrlOut"
log_warning "Vehicle might be asleep"
log_notice "Trying to wake up car then launch the command again"
teslaCtrlSendCommand $vin "-domain vcsec wake" "Wake up vehicule" "internal"
log_debug "teslaCtrlSendCommand; txt deadline exc. - OUT"
else
log_error "tesla-control send command:$command to vin:$vin failed exit status $EXIT_STATUS."
log_error "teslaCtrlSendCommand; $teslaCtrlOut"
# Don't continue if we've reached max retries
[ $max_retries -eq $sendCommandCount ] &&
return 15
log_notice "teslaCtrlSendCommand; Retrying in $BLE_CMD_RETRY_DELAY seconds"
fi
sleep $BLE_CMD_RETRY_DELAY
fi
done
# Unreachable
return 99
}
###
##
#
##
###
teslaCtrlSendKey() {
vin=$1
export TESLA_VIN=$vin
# shellcheck disable=SC2016
TESLA_ADD_KEY_CMD='/usr/bin/tesla-control -ble -command-timeout 20s add-key-request $KEYS_DIR/${vin}_public.pem owner cloud_key 2>&1'
log_info "Trying to deploy the public key to vin:$vin"
max_retries=5
for sendKeyCount in $(seq $max_retries); do
log_notice "Attempt $sendKeyCount/${max_retries} to delivery the public key to vin $vin"
set +e
teslaCtrlOut=$(eval $TESLA_ADD_KEY_CMD)
EXIT_STATUS=$?
set -e
if [ $EXIT_STATUS -eq 0 ]; then
log_debug "teslaCtrlSendKey; $teslaCtrlOut"
log_warning "KEY DELIVERED; IN YOUR CAR, CHECK THE CAR's CENTRAL SCREEN AND ACCEPT THE KEY USING YOUR NFC CARD"
return 0
else
log_error "teslaCtrlSendKey; $teslaCtrlOut"
log_error "Could not send the key; Is the car awake and sufficiently close to the bluetooth adapter?"
# Don't continue if we've reached max retries
[ $max_retries -eq $sendKeyCount ] &&
return 15
log_notice "teslaCtrlSendKey; Retrying in $BLE_CMD_RETRY_DELAY seconds"
sleep $BLE_CMD_RETRY_DELAY
fi
done
return 1
}
###
##
# Is the car awake?
##
###
pingVehicle() {
vin=$1
log_debug "pingVehicle; entering vin:$vin"
if teslaCtrlSendCommand $vin ping "Ping vehicle"; then
log_debug "pingVehicle; ping vehicle succeeded vin:$vin"
ret=0
else
log_debug "pingVehicle; Failed to ping vehicle vin:$vin"
ret=2
fi
log_debug "pingVehicle; leaving vin:$vin return:$ret"
return $ret
}
###
##
# Loop for 5 minutes for key to be accepted
##
###
acceptKeyConfirmationLoop() {
vin=$1
log_debug "acceptKeyConfirmationLoop; entering vin:$vin"
acceptKeyConfirmationLoopSeconds=300
acceptKeyExpireTime=$(($(date +%s) + acceptKeyConfirmationLoopSeconds))
log_info "acceptKeyConfirmationLoop; check if key was accepted by sending a ping command vin:$vin"
# Retry loop
# shellcheck disable=SC1073
while [ "$(date +%s)" -lt $acceptKeyExpireTime ]; do
if pingVehicle $vin; then
log_info "acceptKeyConfirmationLoop; congratulation, the public key has been accepted vin:$vin"
log_debug "touch $KEYS_DIR/${vin}_pubkey_accepted"
touch $KEYS_DIR/${vin}_pubkey_accepted
log_debug "acceptKeyConfirmationLoop; leaving vin:$vin ret:0"
return 0
else
log_notice "acceptKeyConfirmationLoop; sleeping 5 seconds before retrying key vin:$vin"
sleep 5
fi
done
log_debug "acceptKeyConfirmationLoop; leaving vin:$vin ret:1"
return 1
}
###
##
# Send the key to the car then check if it was accepted
##
###
deployKeyMain() {
vin=$1
log_debug "deployKeyMain; calling teslaCtrlSendKey()"
if ! teslaCtrlSendKey $vin; then
log_debug "deployKeyMain; key was not delivered"
return 1
fi
log_debug "deployKeyMain; calling acceptKeyConfirmationLoop()"
if acceptKeyConfirmationLoop $vin; then
log_info "Setting up Home Assistant device's panel"
setupPanelMain $vin
else
log_debug "deployKeyMain; key was not accepted"
return 1
fi
}
###
##
# Tesla VIN to BLE Local Name
##
###
function vinToBLEln() {
vin=$1
ble_ln=""
# BLE Local Name
ble_ln="S$(echo -n ${vin} | sha1sum | cut -c 1-16)C"
echo $ble_ln
}