R/config_helpers.R

Defines functions set_configuration reset_configuration

Documented in reset_configuration set_configuration

#' Configure Environment
#'
#' These functions are used specifically for their side effects -- namely to
#' set (or reset) authentication secrets and configuration details stored in
#' `options()`.
#'
#' @param hue_bridge_ip the IP address of your Hue Bridge
#' @param hue_username the username with access to your Hue Bridge
#' @param darksky_key Dark Sky secret key
#' @param lat latitude (in decimal degrees). Positive north; negative south.
#' @param lon longitude (in decimal degrees). Positive east; negative west.
#' @param hue_storage_path path to directory where Hue training data are stored
#' @param darksky_storage_path path to directory where Dark Sky weather data are
#'   stored
#'
#' @return Returns `TRUE` (invisibly) if options were successfully set or
#'   reset.
#'
#' @seealso [PhilipsHue::set_bridge_credentials()]
#' @seealso [DarkSky::set_dark_sky_credentials()]
#'
#' @export
set_configuration <- function(
    hue_bridge_ip, hue_username, darksky_key, lat, lon,
    hue_storage_path = 'data/state', darksky_storage_path = 'data/weather'
) {
    if (length(hue_storage_path) != 1L || !is.character(hue_storage_path)) {
        stop('Please specify `hue_storage_path` as a single character value')
    }

    if (length(darksky_storage_path) != 1L || !is.character(darksky_storage_path)) {
        stop('Please specify `darksky_storage_path` as a single character value')
    }

    if (!dir.exists(hue_storage_path)) {dir.create(hue_storage_path, recursive = TRUE)}
    if (!dir.exists(darksky_storage_path)) {dir.create(darksky_storage_path, recursive = TRUE)}

    PhilipsHue::set_bridge_credentials(hue_bridge_ip, hue_username)
    DarkSky::set_dark_sky_credentials(darksky_key, lat, lon)

    options(zeitgebeR = list(
        hue_storage_path = hue_storage_path,
        darksky_storage_path = darksky_storage_path
    ))

    return(invisible(TRUE))
}

#' @rdname set_configuration
#' @export
reset_configuration <- function() {
    PhilipsHue::reset_bridge_credentials()
    DarkSky::reset_dark_sky_credentials()
    options(zeitgebeR = list())
}
fascinatingfingers/zeitgebeR documentation built on Oct. 25, 2019, 4:50 p.m.