-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.sh
166 lines (132 loc) · 4.39 KB
/
command.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
#!/bin/bash
### RECEIVE COMMAND FROM THE API ###
### DO NOT MODIFY THIS FILE ###
# Configuration
source config.env
source params.env
# Common code
source common.sh
# Set API endpoint
COMMAND_ENDPOINT=${BASE_API}/commands
# Remember the script startup time
START_TIME=$(date +%s)
TIME_TO_RUN="${1:-60}"
execute_command() {
local command_name="$1"
local argument="$2"
# Debug print
if [ "$LOCAL_DEBUG" = 1 ]; then
echo "command: ${command_name}, argument: ${argument}"
function request() { echo "$@"; }
function set_dv() { echo "$@"; }
else
function request() { curl "http://localhost:7654/$@"; }
function set_dv() { sdv $1 $2; }
fi
# Send updated value of specific metric to the server
function update() { sleep 1; bash send.sh $1; }
# Prepare the arguments and execute the command
case "${command_name}" in
"door_lock")
local ARG="2"
[[ "$argument" == "1" ]] && ARG="1" || ARG="2"
set_dv GUI_lockRequest $ARG
set_dv GUI_headLightFlashRequest 1
update VAPI_isLocked
;;
"sentry")
request "set_sentry_mode?on=${argument}"
update GUI_sentryModeState
;;
"auto_conditioning")
local CMD="stop"
[[ "$argument" == "1" ]] && CMD="start" || CMD="stop"
request "auto_conditioning_${CMD}"
update HVAC_aconStatus
;;
"charging_limit")
request "set_charge_limit?percent=${argument}"
update GUI_chargeLimitRequest
;;
"charging_amps")
set_dv GUI_chargeCurrentRequest $argument
update GUI_chargeCurrentRequest
;;
"charge_port")
set_dv GUI_chargePortDoorRequest true
update VAPI_chargePortDoor
;;
"charge")
local CMD="stop"
[[ "$argument" == "1" ]] && CMD="start" || CMD="stop"
request "charge_${CMD}"
update VAPI_isCharging
;;
"trunk")
set_dv GUI_rearTrunkRequest 1
update DOOR_rearTrunkLatch
;;
"frunk")
set_dv GUI_frontTrunkRequest 1
update DOOR_frontTrunkLatch
;;
# Set the value of a specific metric
# argument format is "dv_name/dv_value"
"set_dv")
IFS='/' read -ra parts <<< "${argument}"
local dv="${parts[0]}"
local val="${parts[1]}"
set_dv $dv $val
update $dv
;;
# Forced OTA update
"update")
bash update.sh -f
;;
*)
echo "Unknown command: ${command_name}"
;;
esac
}
# Function to calculate the remaining time to run
remaining_run_time() {
END=$(date +%s)
DIFF=$(( $TIME_TO_RUN - $END + $START_TIME ))
echo $DIFF
}
# Check for new commands until the time to run is over
while (( $(remaining_run_time) > 0 )); do
command=$(curl $CURL_OPTS \
-s --max-time "$(remaining_run_time)" \
-H "Authorization: Basic $BASIC_AUTH" \
"${COMMAND_ENDPOINT}")
# Save the exit code of curl
status_code=$?
# exit code 28 means we're timed out
# exit not equals to 0 means something went wrong
if (( status_code == 28 )); then
break
# Non-empty response received, parse it
elif (( status_code == 0 )) && [[ -n "$command" ]]; then
IFS=',' read -ra cmd_parts <<< "${command}"
uuid="${cmd_parts[0]}"
command_name="${cmd_parts[1]}"
argument="${cmd_parts[2]}"
received_hash="${cmd_parts[3]}"
# Calculate the expected security hash
expected_hash=$(echo -n "${uuid}${command_name}${argument}${PASSWORD}" | ${SHA256CMD} | awk '{ print $1 }')
# Authorize the command request by comparing hashesh
if [ "${received_hash}" != "${expected_hash}" ]; then
continue
fi
# Execute the command and send a POST request
# to confirm tha command was accepted
if execute_command "${command_name}" "${argument}"; then
curl -s -X POST \
-H "Authorization: Basic $BASIC_AUTH" \
"${COMMAND_ENDPOINT}/${uuid}"
fi
fi
# Sleep for 1 second before checking for new commands
sleep 1
done