mirror of
https://github.com/sensebox/opensensmapr
synced 2025-06-09 11:36:07 +02: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) {
|
get_boxes_ = function (..., endpoint) {
|
||||||
response = httr::GET(endpoint, path = c('boxes'), query = list(...)) %>%
|
response = osem_request_(endpoint, path = c('boxes'), ...)
|
||||||
httr::content() %>%
|
|
||||||
osem_remote_error()
|
|
||||||
|
|
||||||
if (length(response) == 0) {
|
if (length(response) == 0) {
|
||||||
warning('no boxes found for this query')
|
warning('no boxes found for this query')
|
||||||
|
@ -23,27 +21,44 @@ get_boxes_ = function (..., endpoint) {
|
||||||
df
|
df
|
||||||
}
|
}
|
||||||
|
|
||||||
get_box_ = function (..., endpoint) {
|
get_box_ = function (boxId, endpoint) {
|
||||||
httr::GET(endpoint, path = c('boxes', ...)) %>%
|
osem_request_(endpoint, path = c('boxes', boxId)) %>%
|
||||||
httr::content() %>%
|
|
||||||
osem_remote_error() %>%
|
|
||||||
parse_senseboxdata()
|
parse_senseboxdata()
|
||||||
}
|
}
|
||||||
|
|
||||||
get_measurements_ = function (..., endpoint) {
|
get_measurements_ = function (..., endpoint) {
|
||||||
result = httr::GET(endpoint, path = c('boxes', 'data'), query = list(...)) %>%
|
result = osem_request_(endpoint, c('boxes', 'data'), ..., type = 'text')
|
||||||
httr::content(encoding = 'UTF-8') %>%
|
|
||||||
osem_remote_error()
|
# 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))
|
class(result) = c('osem_measurements', class(result))
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
get_stats_ = function (endpoint) {
|
get_stats_ = function (endpoint) {
|
||||||
result = httr::GET(endpoint, path = c('stats')) %>%
|
result = osem_request_(endpoint, path = c('stats'))
|
||||||
httr::content() %>%
|
|
||||||
osem_remote_error()
|
|
||||||
|
|
||||||
names(result) = c('boxes', 'measurements', 'measurements_per_minute')
|
names(result) = c('boxes', 'measurements', 'measurements_per_minute')
|
||||||
result
|
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
|
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
|
#' @export
|
||||||
plot.osem_measurements = function (x, ...) {
|
plot.osem_measurements = function (x, ...) {
|
||||||
# TODO: group/color by sensor_id
|
# TODO: group/color by sensor_id
|
||||||
plot(value~createdAt, x, col = factor(x$sensorId), ...)
|
plot(value~createdAt, x, col = x$sensorId, ...)
|
||||||
invisible(x)
|
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)
|
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_
|
# parses from/to params for get_measurements_ and get_boxes_
|
||||||
parse_dateparams = function (from, to) {
|
parse_dateparams = function (from, to) {
|
||||||
from = utc_date(from)
|
from = utc_date(from)
|
||||||
to = utc_date(to)
|
to = utc_date(to)
|
||||||
if (from - to > 0)
|
if (from - to > 0) stop('"from" must be earlier than "to"')
|
||||||
stop('"from" must be earlier than "to"')
|
|
||||||
|
|
||||||
c(date_as_isostring(from), date_as_isostring(to))
|
c(date_as_isostring(from), date_as_isostring(to))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue