mirror of
https://github.com/sensebox/opensensmapr
synced 2025-02-20 11:53:57 +01:00
refactor api handler, manually parse measure csv
This commit is contained in:
parent
72c1227a46
commit
ed987a32f3
4 changed files with 53 additions and 27 deletions
43
R/api.R
43
R/api.R
|
@ -5,9 +5,7 @@
|
|||
# ==============================================================================
|
||||
|
||||
get_boxes_ = function (..., endpoint) {
|
||||
response = httr::GET(endpoint, path = c('boxes'), query = list(...)) %>%
|
||||
httr::content() %>%
|
||||
osem_remote_error()
|
||||
response = osem_request_(endpoint, path = c('boxes'), ...)
|
||||
|
||||
if (length(response) == 0) {
|
||||
warning('no boxes found for this query')
|
||||
|
@ -23,27 +21,44 @@ get_boxes_ = function (..., endpoint) {
|
|||
df
|
||||
}
|
||||
|
||||
get_box_ = function (..., endpoint) {
|
||||
httr::GET(endpoint, path = c('boxes', ...)) %>%
|
||||
httr::content() %>%
|
||||
osem_remote_error() %>%
|
||||
get_box_ = function (boxId, endpoint) {
|
||||
osem_request_(endpoint, path = c('boxes', boxId)) %>%
|
||||
parse_senseboxdata()
|
||||
}
|
||||
|
||||
get_measurements_ = function (..., endpoint) {
|
||||
result = httr::GET(endpoint, path = c('boxes', 'data'), query = list(...)) %>%
|
||||
httr::content(encoding = 'UTF-8') %>%
|
||||
osem_remote_error()
|
||||
result = osem_request_(endpoint, c('boxes', 'data'), ..., type = 'text')
|
||||
|
||||
# parse the CSV response manually & mute readr
|
||||
suppressWarnings({
|
||||
result = readr::read_csv(result, col_types = readr::cols(
|
||||
.default = readr::col_factor(NULL),
|
||||
createdAt = readr::col_datetime(),
|
||||
value = readr::col_double(),
|
||||
lat = readr::col_double(),
|
||||
lon = readr::col_double(),
|
||||
height = readr::col_double()
|
||||
))
|
||||
})
|
||||
|
||||
class(result) = c('osem_measurements', class(result))
|
||||
result
|
||||
}
|
||||
|
||||
get_stats_ = function (endpoint) {
|
||||
result = httr::GET(endpoint, path = c('stats')) %>%
|
||||
httr::content() %>%
|
||||
osem_remote_error()
|
||||
|
||||
result = osem_request_(endpoint, path = c('stats'))
|
||||
names(result) = c('boxes', 'measurements', 'measurements_per_minute')
|
||||
result
|
||||
}
|
||||
|
||||
osem_request_ = function (host, path, ..., type = 'parsed') {
|
||||
res = httr::GET(host, httr::progress(), path = path, query = list(...))
|
||||
#print(res$url)
|
||||
|
||||
if (httr::http_error(res)) {
|
||||
content = httr::content(res, 'parsed', encoding = 'UTF-8')
|
||||
stop(if ('message' %in% names(content)) content$message else httr::status_code(res))
|
||||
}
|
||||
|
||||
content = httr::content(res, type, encoding = 'UTF-8')
|
||||
}
|
||||
|
|
|
@ -126,3 +126,25 @@ parse_get_measurements_params = function (params) {
|
|||
|
||||
query
|
||||
}
|
||||
|
||||
paged_measurements_req = function (query) {
|
||||
if (is.na(query$from) && is.na(query$to))
|
||||
return(do.call(get_measurements_, query))
|
||||
|
||||
# auto paging: make a request for one 31day interval each.
|
||||
from = query$from
|
||||
to = query$to
|
||||
|
||||
dates = data.frame()
|
||||
while (from < to) {
|
||||
in31days = from + as.difftime(31, units = 'days')
|
||||
# TODO: how to do append to a vector / list instead?
|
||||
dates = rbind(dates, data.frame(from = from, to = min(in31days, to)))
|
||||
from = in31days + as.difftime(1, units = 'secs')
|
||||
}
|
||||
|
||||
print(dates)
|
||||
|
||||
# TODO: iterate over all those dates and call get_measurements_
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#' @export
|
||||
plot.osem_measurements = function (x, ...) {
|
||||
# TODO: group/color by sensor_id
|
||||
plot(value~createdAt, x, col = factor(x$sensorId), ...)
|
||||
plot(value~createdAt, x, col = x$sensorId, ...)
|
||||
invisible(x)
|
||||
}
|
||||
|
|
13
R/utils.R
13
R/utils.R
|
@ -11,22 +11,11 @@ osem_as_sf = function (x, ...) {
|
|||
sf::st_as_sf(x, ..., coords = c('lon', 'lat'), crs = 4326)
|
||||
}
|
||||
|
||||
osem_remote_error = function (response) {
|
||||
suppressWarnings({
|
||||
hasCode = !is.null(response$code)
|
||||
})
|
||||
|
||||
if (hasCode) stop(response$message)
|
||||
invisible(response)
|
||||
}
|
||||
|
||||
# parses from/to params for get_measurements_ and get_boxes_
|
||||
parse_dateparams = function (from, to) {
|
||||
from = utc_date(from)
|
||||
to = utc_date(to)
|
||||
if (from - to > 0)
|
||||
stop('"from" must be earlier than "to"')
|
||||
|
||||
if (from - to > 0) stop('"from" must be earlier than "to"')
|
||||
c(date_as_isostring(from), date_as_isostring(to))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue