R/device.R

Defines functions delete_alarm update_alarm add_alarm get_alarms get_devices

Documented in add_alarm delete_alarm get_alarms get_devices update_alarm

#' @title Get Devices
#'
#' @description Returns a list of the Fitbit devices connected to a user's account.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#'
#' @export
get_devices <- function(token)
{
  url <- paste0(url_api, "devices.json")
  get(url, token)
}

#' Get Alarms
#'
#' Returns a list of the set alarms connected to a user's account.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param tracker_id	The ID of the tracker for which data is returned. The tracker-id value is found via the Get Devices endpoint.
#'
#' @export
get_alarms <- function(token, tracker_id)
{
  url <- paste0(url_api, sprintf("devices/tracker/%s/alarms.json", tracker_id))
  response <- get(url, token)
  result <- response$trackerAlarms
  if(length(result) == 0){
    data.frame(
      alarmId=integer(),
      deleted=logical(),
      enabled=logical(),
      recurring=logical(),
      snoozeCount=integer(),
      snoozeLength=integer(),
      syncedToDevice=logical(),
      time=integer(),
      vibe=character(),
      weekDays=character()
    )
  } else{
    result
  }
}

#' Add alarm
#'
#' Adds the alarm settings to a given ID for a given device.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param tracker_id	The ID of the tracker for which data is returned. The tracker-id value is found by get_devices().
#' @param time	Time of day that the alarm vibrates with a UTC timezone offset, e.g. 07:15-08:00
#' @param weekday	Comma separated list of days of the week on which the alarm vibrates, e.g. MONDAY,TUESDAY
#' @param enabled	TRUE or FALSE. If FALSE, alarm does not vibrate until enabled is set to TRUE.
#' @param recurring	TRUE or FALSE. If FALSE, the alarm is a single event.
#' @export
add_alarm <- function(token, tracker_id, time, weekday, enabled=TRUE, recurring=FALSE)
{
  url <- paste0(url_api, sprintf("devices/tracker/%s/alarms.json", tracker_id))
  body <- list(time=time, enabled=enabled, recurring=recurring, weekDays=weekday)
  response <- post(url, token, body=body)
  data <- response$trackerAlarm
  data$weekDays <- if(length(data$weekDays) == 0){""}else{data$weekDays}
  as.data.frame(data)
}


#' Update Alarm
#'
#' Updates the alarm entry with a given ID for a given device. It also gets a response in the format requested.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param tracker_id	The ID of the tracker whose alarms is managed. The tracker-id value is found via the Get Devices endpoint.
#' @param alarm_id	The ID of the alarm to be updated. The alarm-id value is found in the response of the Get Alarms endpoint.
#' @param time Time of day that the alarm vibrates with a UTC timezone offset, e.g. 07:15-08:00
#' @param weekday	Comma separated list of days of the week on which the alarm vibrates, e.g. MONDAY,TUESDAY
#' @param enabled	TRUE or FALSE. If FALSE, alarm does not vibrate until enabled is set to TRUE.
#' @param recurring	TRUE or FALSE. If FALSE, the alarm is a single event.
#' @param snooze_length Minutes between alarms; integer value.
#' @param snooze_count	Maximum snooze count; integer value.
#' @param label Label for the alarm; string value.
#' @param vibe Vibe pattern; only one value for now - DEFAULT.
#' @export
update_alarm <- function(token, tracker_id, alarm_id, time, weekday, enabled=TRUE, recurring=FALSE, snooze_length=3, snooze_count=9, label="", vibe="DEFAULT")
{
  url <- paste0(url_api, sprintf("devices/tracker/%s/alarms/%s.json", tracker_id, alarm_id))
  body <- list(time=time, enabled=enabled, recurring=recurring, weekDays=weekday, snoozeLength=snooze_length, snoozeCount=snooze_count, label=label, vibe=vibe)
  response <- post(url, token, body=body)
  data <- response$trackerAlarm
  data$weekDays <- if(length(data$weekDays) == 0){""}else{data$weekDays}
  as.data.frame(data)
}

#' Delete Alarm
#'
#' Deletes the user's device alarm entry with the given ID for a given device.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param tracker_id	The ID of the tracker whose alarms is managed. The tracker-id value is found via the Get Devices endpoint.
#' @param alarm_id	The ID of the alarm to be updated. The alarm-id value is found in the response of the Get Alarms endpoint.
#' @export
delete_alarm <- function(token, tracker_id, alarm_id)
{
  url <- paste0(url_api, sprintf("devices/tracker/%s/alarms/%s.json", tracker_id, alarm_id))
  delete(url, token)
}
teramonagi/fitbitr documentation built on Jan. 21, 2021, 8:35 p.m.