forked from MrRonfo/openhab-smarther
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smarther-api.sh
383 lines (329 loc) · 17.4 KB
/
smarther-api.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
#-------------------------------------------------------------------------------
# BEGIN -- Configuration Section
#--------------------------------
# API subscription key
subscription_key="yourSubscriptionKey"
# Application credentials
client_id="yourClientId"
client_secret="yourClientSecret"
redirect_uri="https://yourWebServerPublicIP:yourWebServerPort/smarther/smarther-auth.php"
# Chronothermostat topology
plant_id="thePlantIdOfYourHome"
module_id="theModuleIdOfYourThermostat"
# Cloud2Cloud notification callback URL
notify_url="https://yourWebServerPublicIP:yourWebServerPort/smarther/smarther-c2c.php"
# Log level
log_level="INFO"
#--------------------------------
# END -- Configuration Section
#-------------------------------------------------------------------------------
oauth2_url="https://partners-login.eliotbylegrand.com/authorize"
token_url="https://partners-login.eliotbylegrand.com/token"
devapi_url="https://api.developer.legrand.com/smarther/v2.0"
thermo_url="$devapi_url/chronothermostat/thermoregulation/addressLocation"
data_dir=$OPENHAB_CONF/scripts/smarther/data
#-------------------------------------------------------------------------------
# Send an http GET request
# ($1 = log command, $2 = destination url)
function sendHttpGetRequest() {
log DEBUG "Command: $1 Url: $2"
local __rsp_data=$(curl -w "\n%{http_code}" -s -X GET "$2" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: $subscription_key" -H "Authorization: $ocm")
log INFO "Command: $1 Received: $__rsp_data"
echo "$__rsp_data"
}
#-------------------------------------------------------------------------------
# Send an http POST request
# ($1 = log command, $2 = destination url, $3 = request body)
function sendHttpPostRequest() {
log DEBUG "Command: $1 Url: $2 Body: $3"
local __rsp_data=$(curl -w "\n%{http_code}" -s -X POST "$2" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: $subscription_key" -H "Authorization: $ocm" --data-ascii "$3")
log INFO "Command: $1 Received: $__rsp_data"
echo "$__rsp_data"
}
#-------------------------------------------------------------------------------
# Send an http DELETE request
# ($1 = log command, $2 = destination url)
function sendHttpDeleteRequest() {
log DEBUG "Command: $1 Url: $2"
local __rsp_data=$(curl -w "\n%{http_code}" -s -X DELETE "$2" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: $subscription_key" -H "Authorization: $ocm")
log INFO "Command: $1 Received: $__rsp_data"
echo "$__rsp_data"
}
#-------------------------------------------------------------------------------
# Extract the http response code from response data
# ($1 = response data)
function getHttpRspCode() {
echo "${1##*$'\n'}"
}
#-------------------------------------------------------------------------------
# Extract the http response body from response data
# ($1 = response data)
function getHttpRspBody() {
echo "$1" | sed '$ d'
}
#-------------------------------------------------------------------------------
# Construct an error message from http response data
# ($1 = log command, $2 = response data)
function getHttpErrorMsg() {
local __rsp_errm=$(echo "$2" | sed "$ d" | jq -r '.message')
echo "\"errormsg\":\"($1) $__rsp_errm\""
}
#-------------------------------------------------------------------------------
# Construct the OAuth2 end-point Url to be called from browser
function getOAuth2CallUrl() {
local __oauth2_state=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
echo "$oauth2_url?client_id=$client_id&response_type=code&redirect_uri=$redirect_uri&state=$__oauth2_state"
}
#-------------------------------------------------------------------------------
# Write a message in the application log
# ($1 = log command, $2 = log message)
function log() {
if [ "$log_level" = "DEBUG" ] || [ "$1" = "INFO" ]; then
echo $(printf "[%-5s][%(%d/%m/%Y %H:%M:%S)T]" $1) $2 >> $OPENHAB_CONF/scripts/smarther/log/smarther.log
fi
}
timestamp_now=$(date +%s)
# Access token is valid for 1 hour
access_token=""
# Getting access token expiry string from refresh token file
access_token_expiry_string=""
if [ -f $data_dir/refresh.json ]; then
access_token_expiry_string=$(less $data_dir/refresh.json | jq -r .expires_on)
fi
if [ -z "$access_token_expiry_string" ]; then
#---------------------------------------------------------------------------
# Refresh token file is missing, restart the authorization process
#---------------------------------------------------------------------------
log DEBUG "Access token expiry tstamp is missing or invalid"
# Getting OAuth2 code from authorization file
oauth2_code=""
if [ -f $data_dir/authorization.json ]; then
oauth2_code=$(less $data_dir/authorization.json | jq -r .oauth2_code)
fi
if [ -z "$oauth2_code" ]; then
# OAuth2 code is missing or invalid: quit
rsp_errm="OAuth2 code is missing or invalid. To restore the authentication flow call the following Url from a browser: $(getOAuth2CallUrl)"
log DEBUG "Authorization.json: $rsp_errm"
echo "{\"rsptype\":\"$1\",\"rspcode\":500,\"errormsg\":\"$rsp_errm\"}"
exit 1
fi
log DEBUG "OAuth2 code: $oauth2_code"
# Calling the token end-point to get a new access token
log DEBUG "Sending request to get new access token"
rsp_data=$(curl -w "\n%{http_code}" -s -X POST -d "client_id=$client_id&client_secret=$client_secret&grant_type=authorization_code&code=$oauth2_code" $token_url)
rsp_code=$(getHttpRspCode "$rsp_data")
rsp_body=$(getHttpRspBody "$rsp_data")
if [ $rsp_code -ne 200 ]; then
# Call failed: http response has errors, quit
rsp_errm="Error getting new access token from server"
if [ $(echo $rsp_body | jq 'has("error")') ]; then
if [ $(echo $rsp_body | jq '.error_description | contains ("AADB2C90080")') ]; then
rsp_errm="OAuth2 code has expired. To restore the authentication flow call the following Url from a browser: $(getOAuth2CallUrl)"
else
rsp_errm=$(echo $rsp_body | jq -r '.error_description | gsub("\\r";"") | gsub("\\n";" ")')
fi
fi
log DEBUG "Request failed with error: $rsp_errm"
echo "{\"rsptype\":\"$1\",\"rspcode\":$rsp_code,\"errormsg\":\"$rsp_errm\"}"
exit 1
fi
# Copying response into formatted refresh token file
echo $rsp_body | jq '.' | more > $data_dir/refresh.json
access_token=$(less $data_dir/refresh.json | jq -r .access_token)
# Getting user:group:permissions information on this script
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"
user_group=$(stat -c "%U:%G" $script_dir/smarther-api.sh)
# Making sure the refresh token file is owned by same user:group as this script
sudo chown $user_group $data_dir/refresh.json
# Making sure that write permission on refresh token file is granted to group members as well
sudo chmod 664 $data_dir/refresh.json
# Logging access token expiry timestamp
access_token_expiry_string=$(less $data_dir/refresh.json | jq -r .expires_on)
access_token_expiry_tstamp=$(date -d "@$access_token_expiry_string")
log DEBUG "Done. Access Token new expiry date = $access_token_expiry_tstamp"
elif [ $access_token_expiry_string -le $timestamp_now ]; then
#---------------------------------------------------------------------------
# Refresh token file is present, access token is present but has expired
#---------------------------------------------------------------------------
access_token_expiry_tstamp=$(date -d "@$access_token_expiry_string")
log DEBUG "Access token has expired on $access_token_expiry_tstamp"
# Getting refresh token from refresh token file
refresh_token=$(less $data_dir/refresh.json | jq -r .refresh_token)
# Calling the refresh token flow to get a new access token
log DEBUG "Sending request to refresh the access token"
rsp_data=$(curl -w "\n%{http_code}" -s -X POST -d "client_id=$client_id&client_secret=$client_secret&grant_type=refresh_token&refresh_token=$refresh_token" $token_url)
rsp_code=$(getHttpRspCode "$rsp_data")
rsp_body=$(getHttpRspBody "$rsp_data")
if [ $rsp_code -ne 200 ]; then
# Call failed: http response has errors, quit
rsp_errm="Error refreshing the access token from server"
if [ $(echo $rsp_body | jq 'has("error")') ]; then
rsp_errm=$(echo $rsp_body | jq -r '.error_description | gsub("\\r";"") | gsub("\\n";" ")')
fi
log DEBUG "Request failed with error: $rsp_errm"
echo "{\"rsptype\":\"$1\",\"rspcode\":$rsp_code,\"errormsg\":\"$rsp_errm\"}"
exit 1
fi
# Copying response into formatted refresh token file
echo $rsp_body | jq '.' | more > $data_dir/refresh.json
access_token=$(less $data_dir/refresh.json | jq -r .access_token)
# Logging access token expiry timestamp
access_token_expiry_string=$(less $data_dir/refresh.json | jq -r .expires_on)
access_token_expiry_tstamp=$(date -d "@$access_token_expiry_string")
log DEBUG "Done. Access token new expiry date = $access_token_expiry_tstamp"
else
#---------------------------------------------------------------------------
# Refresh token file is present, access token is present and is still valid
#---------------------------------------------------------------------------
access_token_expiry_tstamp=$(date -d "@$access_token_expiry_string")
log DEBUG "Access token is still valid (will expire on: $access_token_expiry_tstamp)"
# Getting access token from refresh token file
access_token=$(less $data_dir/refresh.json | jq -r .access_token)
fi
ocm="Bearer $access_token"
rsp_code=0
rsp_json=""
case $1 in
get_measures)
# Operation used to retrieve the measured temperature and humidity detected by a chronothermostat
rsp_data=$(sendHttpGetRequest $1 "$thermo_url/plants/$plant_id/modules/parameter/id/value/$module_id/measures")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
rsp_body=$(getHttpRspBody "$rsp_data")
rsp_temp=$(echo "$rsp_body" | jq '.thermometer.measures[0].value | tonumber')
rsp_humi=$(echo "$rsp_body" | jq '.hygrometer.measures[0].value | tonumber')
rsp_json="\"temperature\":$rsp_temp,\"humidity\":$rsp_humi"
else
rsp_json=$(getHttpErrorMsg $1 "$rsp_data")
fi
;;
get_status)
# Operation used to retrieve the complete status of a chronothermostat
rsp_data=$(sendHttpGetRequest $1 "$thermo_url/plants/$plant_id/modules/parameter/id/value/$module_id")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
rsp_body=$(getHttpRspBody "$rsp_data")
rsp_func=$(echo "$rsp_body" | jq '.chronothermostats[0].function')
rsp_mode=$(echo "$rsp_body" | jq '.chronothermostats[0].mode')
rsp_setp=$(echo "$rsp_body" | jq '.chronothermostats[0].setPoint.value | tonumber')
rsp_prog=$(echo "$rsp_body" | jq '.chronothermostats[0].programs[0].number')
rsp_time=$(echo "$rsp_body" | jq 'if .chronothermostats[0].activationTime != null then .chronothermostats[0].activationTime else "forever" end')
rsp_frmt=$(echo "$rsp_body" | jq '.chronothermostats[0].temperatureFormat')
rsp_load=$(echo "$rsp_body" | jq '.chronothermostats[0].loadState')
rsp_temp=$(echo "$rsp_body" | jq '.chronothermostats[0].thermometer.measures[0].value | tonumber')
rsp_humi=$(echo "$rsp_body" | jq '.chronothermostats[0].hygrometer.measures[0].value | tonumber')
rsp_json="\"function\":$rsp_func,\"mode\":$rsp_mode,\"setpoint\":$rsp_setp,\"program\":$rsp_prog,\"time\":$rsp_time,\"tempformat\":$rsp_frmt,\"status\":$rsp_load,\"temperature\":$rsp_temp,\"humidity\":$rsp_humi"
else
rsp_json=$(getHttpErrorMsg $1 "$rsp_data")
fi
;;
get_programs)
# Operation used to retrieve the list of programs managed by a chronothermostat
rsp_data=$(sendHttpGetRequest $1 "$thermo_url/plants/$plant_id/modules/parameter/id/value/$module_id/programlist")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
rsp_body=$(getHttpRspBody "$rsp_data")
rsp_prog=$(echo "$rsp_body" | jq -c '.chronothermostats[0].programs')
rsp_json="\"programs\":$rsp_prog"
else
rsp_json=$(getHttpErrorMsg $1 "$rsp_data")
fi
;;
get_subscriptions)
# Operation used to get subscriptions of a user to get Cloud2Cloud notifications of a plant
rsp_data=$(sendHttpGetRequest $1 "$devapi_url/subscription")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
rsp_body=$(getHttpRspBody "$rsp_data")
rsp_json="\"subscriptions\":$rsp_body"
elif [ $rsp_code -eq 204 ]; then
rsp_json="\"errormsg\":\"($1) No subscription associated with this user\""
else
rsp_json=$(getHttpErrorMsg $1 "$rsp_data")
fi
;;
set_thermo)
# Operation used to set the status of a chronothermostat.
# Input arguments: $2 = Mode, $3 = Program, $4 = SetPoint, $5 = Timer
data_ascii=""
case $2 in
automatic)
data_ascii="{\"function\":\"heating\",\"mode\":\"automatic\",\"programs\":[{\"number\":$3}]}"
;;
manual)
if [ "$5" = "forever" ]; then
data_ascii="{\"function\":\"heating\",\"mode\":\"manual\",\"setPoint\":{\"value\":$4,\"unit\":\"C\"}}"
else
data_ascii="{\"function\":\"heating\",\"mode\":\"manual\",\"setPoint\":{\"value\":$4,\"unit\":\"C\"},\"activationTime\":\"$5\"}"
fi
;;
protection)
data_ascii="{\"function\":\"heating\",\"mode\":\"protection\"}"
;;
boost)
data_ascii="{\"function\":\"heating\",\"mode\":\"boost\",\"activationTime\":\"$5\"}"
;;
off)
data_ascii="{\"function\":\"heating\",\"mode\":\"off\"}"
;;
esac
if [ -n "$data_ascii" ]; then
rsp_data=$(sendHttpPostRequest $1 "$thermo_url/plants/$plant_id/modules/parameter/id/value/$module_id" "$data_ascii")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
# Retrieving chronothermostat updated status
rsp_data=$(sendHttpGetRequest $1 "$thermo_url/plants/$plant_id/modules/parameter/id/value/$module_id")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
rsp_body=$(getHttpRspBody "$rsp_data")
rsp_func=$(echo "$rsp_body" | jq '.chronothermostats[0].function')
rsp_mode=$(echo "$rsp_body" | jq '.chronothermostats[0].mode')
rsp_setp=$(echo "$rsp_body" | jq '.chronothermostats[0].setPoint.value | tonumber')
rsp_prog=$(echo "$rsp_body" | jq '.chronothermostats[0].programs[0].number')
rsp_time=$(echo "$rsp_body" | jq 'if .chronothermostats[0].activationTime != null then .chronothermostats[0].activationTime else "forever" end')
rsp_frmt=$(echo "$rsp_body" | jq '.chronothermostats[0].temperatureFormat')
rsp_load=$(echo "$rsp_body" | jq '.chronothermostats[0].loadState')
rsp_temp=$(echo "$rsp_body" | jq '.chronothermostats[0].thermometer.measures[0].value | tonumber')
rsp_humi=$(echo "$rsp_body" | jq '.chronothermostats[0].hygrometer.measures[0].value | tonumber')
rsp_json="\"function\":$rsp_func,\"mode\":$rsp_mode,\"setpoint\":$rsp_setp,\"program\":$rsp_prog,\"time\":$rsp_time,\"tempformat\":$rsp_frmt,\"status\":$rsp_load,\"temperature\":$rsp_temp,\"humidity\":$rsp_humi"
else
rsp_json=$(getHttpErrorMsg "$1, status" "$rsp_data")
fi
else
rsp_json=$(getHttpErrorMsg "$1, thermo" "$rsp_data")
fi
else
rsp_code=400
rsp_json="\"errormsg\":\"Mode ($2) is not a valid mode\""
fi
;;
set_subscription)
# Operation used to subscribe a user to get Cloud2Cloud notifications of a plant
rsp_data=$(sendHttpPostRequest $1 "$devapi_url/plants/$plant_id/subscription" "{\"EndPointUrl\":\"$notify_url\"}")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 201 ]; then
rsp_body=$(getHttpRspBody "$rsp_data")
rsp_json="\"subscriptions\":$rsp_body"
else
rsp_json=$(getHttpErrorMsg $1 "$rsp_data")
fi
;;
delete_subscription)
# Operation used to delete the subscription of a user to get Cloud2Cloud notifications of a plant
rsp_data=$(sendHttpDeleteRequest $1 "$devapi_url/plants/$plant_id/subscription/$2")
rsp_code=$(getHttpRspCode "$rsp_data")
if [ $rsp_code -eq 200 ]; then
rsp_json="\"done\":\"OK\""
else
rsp_json=$(getHttpErrorMsg $1 "$rsp_data")
fi
;;
*)
rsp_code=400
rsp_json="\"errormsg\":\"Command ($1) is not a valid command\""
;;
esac
rsp_json="{\"rsptype\":\"$1\",\"rspcode\":$rsp_code,$rsp_json}"
log INFO "Command: $1 Response: $rsp_json"
echo "$rsp_json"