Skip to content

Commit

Permalink
Provide an option to return device information to R environment
Browse files Browse the repository at this point in the history
  • Loading branch information
bhelsel committed Jul 14, 2023
1 parent 9e1a468 commit 00185de
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions R/device.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Copyright © 2022 University of Kansas. All rights reserved.
#
# Creative Commons Attribution NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)

#' @title Extract device information from the Fitbit API
#' @description Extract device, last sync time, and battery level from the Fitbit API.
#' @param token.pathname Path name to the Fitbit API access token.
#' @param toSQL Write the device information to a SQL database, Default: TRUE
#' @param returnDevice Return the device information to the users R environment, Default: FALSE
#' @return Writes the device information to a SQL database stored in the data folder.
#' @details Extract device, last sync time, and battery level from the Fitbit API.
#' @seealso
Expand All @@ -11,7 +17,7 @@
#' @rdname get_fitbit_device
#' @export

get_fitbit_device <- function(token.pathname){
get_fitbit_device <- function(token.pathname, toSQL = TRUE, returnDevice = FALSE){
directory <- dirname(dirname(token.pathname))
devicesData <- data.frame(matrix(ncol = 5, nrow = 0))
colnames(devicesData) <- c("user", "deviceVersion", "lastSyncTime", "battery", "batteryLevel")
Expand All @@ -20,11 +26,23 @@ get_fitbit_device <- function(token.pathname){
user <- token$credentials$user_id
url_devices <- paste0("https://api.fitbit.com/1/", "user/", user, "/", "devices.json")
device <- jsonlite::fromJSON(httr::content(httr::GET(url_devices, token), as = "text"))
data <- data.frame(cbind(deviceVersion=device$deviceVersion, lastSyncTime=device$lastSyncTime, battery=device$battery, batteryLevel=device$batteryLevel, type=device$type))
if(length(data) != 0){

if(length(device) != 0){
data <- data.frame(cbind(deviceVersion=device$deviceVersion, lastSyncTime=device$lastSyncTime, battery=device$battery, batteryLevel=device$batteryLevel, type=device$type))
} else{
data <- data.frame(cbind(deviceVersion=NA, lastSyncTime=NA, battery=NA, batteryLevel=NA, type=NA))
}

database <- grep(user, list.files(paste0(directory, "/data"), full.names = TRUE), value = TRUE)
con <- DBI::dbConnect(RSQLite::SQLite(), database)
DBI::dbWriteTable(con, "device", data, overwrite = TRUE)
DBI::dbDisconnect(con)

if(toSQL){
con <- DBI::dbConnect(RSQLite::SQLite(), database)
DBI::dbWriteTable(con, "device", data, overwrite = TRUE)
DBI::dbDisconnect(con)
}

if(returnDevice){
return(device)
}

}

0 comments on commit 00185de

Please sign in to comment.