-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.r
438 lines (404 loc) · 11.1 KB
/
functions.r
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#' Title
#' @details A new token is needed every hour, so might as well run it every time
#' @param api_key Key from Ecobee, need to sign up as developer
#' @param refresh_token Gotten from Ecobee, good for a year
#'
#' @return A character token
#' @export
#'
#' @examples
#' access_token <- get_token(api_key=Sys.getenv('ECOBEE_API_KEY'), refresh_token=Sys.getenv('ECOBEE_REFRESH_TOKEN'))
get_token <- function(
api_key=Sys.getenv('ECOBEE_API_KEY'),
refresh_token=Sys.getenv('ECOBEE_REFRESH_TOKEN'),
refresh_url='https://api.ecobee.com/token'
)
{
token_request <- httr::POST(
url=glue::glue("{refresh_url}?grant_type=refresh_token&code={refresh_token}&client_id={api_key}"),
encode='json'
)
return(httr::content(token_request)$access_token)
}
#' Title
#' @details Calls the Ecobee API to get a report about the thermostat and sensors
#' @param startDate
#' @param endDate
#' @param thermostats
#' @param access_token
#'
#' @return
#' @export
#'
#' @examples
#' the_report <- get_report(thermostats='id1,id2', access_token=access_token)
get_report <- function(
startDate=as.character(Sys.Date()), endDate=as.character(Sys.Date()),
startInterval=0, endInterval=287,
thermostats,
access_token
)
{
httr::GET(
glue::glue(
'https://api.ecobee.com/1/runtimeReport?format=json&body={{"startDate":"{startDate}","endDate":"{endDate}","startInterval":{startInterval},"endInterval":{endInterval},"columns":"zoneAveTemp,hvacMode,fan,outdoorTemp,outdoorHumidity,sky,wind,zoneClimate,zoneCoolTemp,zoneHeatTemp,zoneHvacMode,zoneOccupancy","selection":{{"selectionType":"thermostats","selectionMatch":"{thermostats}"}},"includeSensors":true}}'
)
, httr::add_headers(Authorization=glue::glue('Bearer {access_token}'))
, encode='json'
) %>%
httr::content()
}
#' Title
#' @details The column names are kept separate from the data so this is used to tie things together
#' @param sensors
#'
#' @return
#' @export
#'
#' @examples
#' relate_sensor_id_to_name(report$sensorList[[1]]$sensors)
relate_sensor_id_to_name <- function(sensors)
{
purrr::map_df(
sensors,
~tibble::tibble(ID=.x$sensorId, name=glue::glue('{.x$sensorName}_{.x$sensorType}'))
)
}
#' Title
#' @details The column names are kept separate from the data so this is used to tie things together
#' @param sensorInfo
#'
#' @return
#' @export
#'
#' @examples
#' make_sensor_column_names(report$sensorList[[1]])
make_sensor_column_names <- function(sensorInfo)
{
sensorInfo$columns %>%
unlist() %>%
tibble::enframe(name='index', value='id') %>%
dplyr::left_join(relate_sensor_id_to_name(sensorInfo$sensors), by=c('id'='ID')) %>%
dplyr::mutate(name=as.character(name)) %>%
dplyr::mutate(name=dplyr::if_else(is.na(name), id, name))
}
#' Title
#' @details Extracts all the readings from each set of physical sensors
#' @param sensor
#'
#' @return
#' @export
#'
#' @examples
#' extract_one_sensor_info(report$sensorList[[1]])
extract_one_sensor_info <- function(sensor)
{
sensor_col_names <- make_sensor_column_names(sensor)$name
sensor$data %>%
unlist() %>%
readr::read_csv(col_names=sensor_col_names) %>%
# make it longer so we can easy remove rows based on a condition
tidyr::pivot_longer(cols=c(-date, -time), names_to='Sensor', values_to='Reading') %>%
# we use slice because grep() returns a vector of indices, not TRUE/FALSE
dplyr::slice(grep(pattern='_(temperature)|(occupancy)$', x=Sensor, ignore.case=FALSE)) %>%
# split apart the sensor name from what it's measuring
tidyr::separate(col=Sensor, into=c('Sensor', 'Measure'), sep='_', remove=TRUE) %>%
# rename the actual thermostats to say thermostat
dplyr::mutate(Sensor=sub(pattern='Thermostat .+$', replacement='Thermostat', x=Sensor)) %>%
# back into wide format so each sensor is its own column
tidyr::pivot_wider(names_from=Measure, values_from=Reading)
}
#' Title
#' @details Gets the readings for all the sensors for al lthe thermostats
#' @param report
#'
#' @return
#' @export
#'
#' @examples
#' extract_sensor_info(report)
extract_sensor_info <- function(report)
{
names(report$sensorList) <- purrr::map_chr(report$reportList, 'thermostatIdentifier')
purrr::map_df(report$sensorList, extract_one_sensor_info, .id='Thermostat')
}
#' Title
#' @details Gets all the readings data from a central thermostat
#' @param thermostat
#' @param colnames Comma-separated character of column names as specified in the Ecobee API
#'
#' @return
#' @export
#'
#' @examples
#' extract_one_thermostat_info(report$reportList[[1]], strsplit(x=report$columns, split=',')[[1]])
extract_one_thermostat_info <- function(thermostat, colnames)
{
thermostat$rowList %>%
unlist() %>%
readr::read_csv(col_names=c('date', 'time', colnames))
}
#' Title
#' @details Gets all the readings data from multiple central thermostats
#' @param report
#'
#' @return
#' @export
#'
#' @examples
#' extract_thermostat_info(report)
extract_thermostat_info <- function(report)
{
names(report$reportList) <- purrr::map_chr(report$reportList, 'thermostatIdentifier')
purrr::map_df(
report$reportList,
extract_one_thermostat_info, colnames=strsplit(x=report$columns, split=',')[[1]],
.id='Thermostat'
)
}
#' Title
#' @details Gets information about multiple thermostats
#' @param access_token
#'
#' @return
#' @export
#'
#' @examples
#' get_thermostat_info(access_token)
get_thermostat_info <- function(access_token)
{
httr::GET(
'https://api.ecobee.com/1/thermostat?format=json&body={"selection":{"selectionType":"registered","selectionMatch":"","includeRuntime":false,"includeSettings":false,"includeSensors":false,"includeWeather":false,"includeLocation":true}}'
, httr::add_headers(Authorization=glue::glue('Bearer {access_token}'))
, encode='json'
) %>%
httr::content()
}
#' Title
#'
#' @param thermostat_info
#'
#' @return Character of comma-separated thermostat IDs
#' @export
#'
#' @examples
get_thermostat_ids <- function(thermostat_info)
{
thermostat_info$thermostatList %>%
purrr::map_chr('identifier') %>%
paste(collapse=',')
}
#' Title
#' @details Gets the user-created names of thermostats rather than their identifiers
#' @param thermostat_object
#'
#' @return
#' @export
#'
#' @examples
#' thermo_info <- get_thermostat_info(access_token)
#' get_thermostat_names(thermo_info)
get_thermostat_names <- function(thermostat_object)
{
tibble::tibble(
ID=purrr::map_chr(thermostat_object$thermostatList, 'identifier')
, Name=purrr::map_chr(thermostat_object$thermostatList, 'name')
)
}
#' Title
#'
#' @param thermostat_level
#' @param sensor_level
#' @param thermostat_names
#'
#' @return
#' @export
#'
#' @examples
#' access_token <- get_token(api_key=Sys.getenv('ECOBEE_API_KEY'), refresh_token=Sys.getenv('ECOBEE_REFRESH_TOKEN'))
#' get_report(startDate=start_date, endDate=end_date, thermostats=thermostats, access_token=access_token)
#' thermo_names <- get_thermostat_names(thermo_info)
#' central_info <- extract_thermostat_info(report)
#' ind_info <- extract_sensor_info(report)
#' all_info <- combine_thermostat_sensors(central_info, ind_info, thermo_names)
combine_thermostat_sensors <- function(thermostat_level, sensor_level, thermostat_names)
{
dplyr::inner_join(x=thermostat_level, y=sensor_level, by=c('Thermostat', 'date', 'time')) %>%
dplyr::left_join(thermostat_names, by=c('Thermostat'='ID')) %>%
dplyr::mutate(Sensor=dplyr::if_else(Sensor=='Thermostat', glue::glue('{Name} Thermostat'), Sensor)) %>%
dplyr::relocate(Name, Sensor, date, time, temperature, occupancy)
}
#' Title
#' @details Figuring out what UTC date to use to run the report. Assumes all thermostats are in the same place.
#' @param thermostat_info
#'
#' @return
#' @export
#'
#' @examples
get_utc_date <- function(thermostat_info)
{
as.character(as.Date(thermostat_info$thermostatList[[1]]$utcTime))
}
#' Title
#'
#' @param thermostat_info
#' @param override
#'
#' @return
#' @export
#'
#' @examples
#' the_info <- get_thermostat_info(access_token)
#' get_local_date(the_info)
get_local_date <- function(thermostat_info, override=NULL)
{
if(!is.null(override))
{
return(override)
}
as.Date(thermostat_info$thermostatList[[1]]$thermostatTime)
}
#' Title
#'
#' @param thermostat_info
#'
#' @return
#' @export
#'
#' @examples
#' the_info <- get_thermostat_info(access_token)
#' get_time_offset(the_info)
get_time_offset <- function(thermostat_info)
{
thermostat_info$thermostatList[[1]]$location$timeZoneOffsetMinutes
}
#' Title
#'
#' @param thermostat_info
#' @param override
#'
#' @return
#' @export
#'
#' @examples
#' the_info <- get_thermostat_info(access_token)
#' compute_start_date(the_info)
compute_start_date <- function(thermostat_info, override=NULL)
{
if(!is.null(override))
{
first_part <- override
} else
{
first_part <- get_local_date(thermostat_info)
}
first_part + (get_time_offset(thermostat_info) > 0)
}
#' Title
#'
#' @param thermostat_info
#' @param override
#'
#' @return
#' @export
#'
#' @examples
#' the_info <- get_thermostat_info(access_token)
#' compute_end_date(the_info)
compute_end_date <- function(thermostat_info, override=NULL)
{
if(!is.null(override))
{
first_part <- override
} else
{
first_part <- get_local_date(thermostat_info)
}
first_part + (get_time_offset(thermostat_info) < 0)
}
#' Title
#'
#' @param thermostat_info
#'
#' @return
#' @export
#'
#' @examples
#' the_info <- get_thermostat_info(access_token)
#' compute_start_interval(the_info)
compute_start_interval <- function(thermostat_info)
{
# intervals go from 0 to 287
# if west of UTC we need to move minutes/5 forward
# if east of UTC we need to move minutes/5 backward
287*(get_time_offset(thermostat_info) > 0) - get_time_offset(thermostat_info)/5
}
#' Title
#'
#' @param thermostat_info
#'
#' @return
#' @export
#'
#' @examples
#' the_info <- get_thermostat_info(access_token)
#' the_start <- compute_start_interval(the_info)
#' compute_end_interval(the_start)
compute_end_interval <- function(start_interval)
{
# needs to end one interval before the start
start_interval - 1
}
#' Title
#'
#' @param data
#' @param file
#'
#' @return
#' @export
#'
#' @examples
write_file <- function(data, file)
{
readr::write_csv(x=data, file=file)
return(file)
}
#' Title
#'
#' @param file
#'
#' @return
#' @export
#'
#' @examples
delete_file_if_exists <- function(filename)
{
if(file.exists(filename))
file.remove(filename)
}
#' Title
#'
#' @param file
#' @param folder_name
#' @param filename
#' @param bucket_name
#'
#' @return
#' @export
#'
#' @examples
write_to_bucket <- function(file, folder_name, filename, bucket_name)
{
op <- aws.s3::put_object(
file=file, object=sprintf('%s/%s', folder_name, filename), bucket=bucket_name
)
# if successfully written
if(op)
{
return(Sys.time())
} else
{
return(FALSE)
}
}