R/registro_consignas_temperatura.R

Defines functions registro_consignas_temperatura

Documented in registro_consignas_temperatura

#' @title Asignación consignas temperatura según rite y época (invierno/verano)
#'
#' @description Asignación consignas temperatura según rite y época (invierno/verano). Se lanza todas las noches para actualizar la consigna de cada estancia del edificio.
#'
#' @param consigna
#'
#' @return json
#'
#' @examples  prueba(1)
#'
#' @import httr
#' jsonlite
#' rjson
#' RCurl
#' dplyr
#' lubridate
#' timeDate
#'
#' @export

registro_consignas_temperatura <- function(consigna = 23){


  id_aulas <- c("f4d0d070-24dd-11eb-b605-01af9c6dd825","fa1cb5d0-24dd-11eb-b605-01af9c6dd825","feccf770-24dd-11eb-b605-01af9c6dd825","03179970-24de-11eb-b605-01af9c6dd825","083542e0-24de-11eb-b605-01af9c6dd825","0e3d9a70-24de-11eb-b605-01af9c6dd825","02d28360-2da0-11eb-b605-01af9c6dd825","0b615ec0-2da0-11eb-b605-01af9c6dd825")
  id_empresas <- c("aa6cf240-24d6-11eb-b605-01af9c6dd825","af8e9350-24d6-11eb-b605-01af9c6dd825","7f2c2bf0-276a-11eb-b605-01af9c6dd825","b5fc6370-24d6-11eb-b605-01af9c6dd825","caaf8810-276a-11eb-b605-01af9c6dd825","c38cfbd0-24d6-11eb-b605-01af9c6dd825","cba93f40-24d6-11eb-b605-01af9c6dd825","cf2ac4e0-24d6-11eb-b605-01af9c6dd825","e7d0e380-24d6-11eb-b605-01af9c6dd825","ef5309d0-24d6-11eb-b605-01af9c6dd825","79a16980-2da4-11eb-b605-01af9c6dd825","f9bf09a0-24d6-11eb-b605-01af9c6dd825","00f6b970-24d7-11eb-b605-01af9c6dd825","04c79790-24d7-11eb-b605-01af9c6dd825","08af5910-24d7-11eb-b605-01af9c6dd825","575a6340-2da4-11eb-b605-01af9c6dd825")
  id_despachos_pb <- c("1e6fea50-24df-11eb-b605-01af9c6dd825","229e9fe0-24df-11eb-b605-01af9c6dd825","27a84c20-24df-11eb-b605-01af9c6dd825","2c6781e0-24df-11eb-b605-01af9c6dd825","30d7fb60-24df-11eb-b605-01af9c6dd825","350fffc0-24df-11eb-b605-01af9c6dd825","39802b20-24df-11eb-b605-01af9c6dd825","43d0b3b0-24df-11eb-b605-01af9c6dd825","47b05ee0-24df-11eb-b605-01af9c6dd825","4c4e7810-24df-11eb-b605-01af9c6dd825","92a2d790-24dd-11eb-b605-01af9c6dd825")
  id_seminarios <- c("a14e2ec0-24dd-11eb-b605-01af9c6dd825","b97ea830-24dd-11eb-b605-01af9c6dd825","b4f30590-24dd-11eb-b605-01af9c6dd825","a14e2ec0-24dd-11eb-b605-01af9c6dd825","cabeb400-24dd-11eb-b605-01af9c6dd825","d89f8f40-24dd-11eb-b605-01af9c6dd825","e2ca4050-24dd-11eb-b605-01af9c6dd825")
  id_despachos_p1 <- c("5321b630-24de-11eb-b605-01af9c6dd825","59d9d5c0-24de-11eb-b605-01af9c6dd825","5eaeb660-24de-11eb-b605-01af9c6dd825","6311c260-24de-11eb-b605-01af9c6dd825","67f54900-24de-11eb-b605-01af9c6dd825","6d17ad60-24de-11eb-b605-01af9c6dd825","72dda4c0-24de-11eb-b605-01af9c6dd825","7d24b770-24de-11eb-b605-01af9c6dd825","817d8a40-24de-11eb-b605-01af9c6dd825","860200f0-24de-11eb-b605-01af9c6dd825","8a7fe7f0-24de-11eb-b605-01af9c6dd825","900c7fd0-24de-11eb-b605-01af9c6dd825","9fef93b0-24de-11eb-b605-01af9c6dd825")

  id_dispositivos <- c(id_aulas, id_empresas, id_despachos_pb, id_seminarios, id_despachos_p1, "244e4960-418c-11ec-b26c-5339f6f4e88b") # El último dispositivos es el de los fancoils, para definir la consigna de las zonas comunes.


  # ==============================================================================
  # PETICIÓN TOKEN THB
  # ==============================================================================

  cuerpo <- toJSON(list(username="kepa@techfriendly.es",password='kepa_tech'))
  post <- httr::POST(url = "http://116.202.100.157:8080/api/auth/login",
                     add_headers("Content-Type"="application/json","Accept"="application/json"),
                     body = cuerpo,
                     encode = "json",verbose()
  )

  resultado_peticion_token <- httr::content(post)
  auth_thb <- paste("Bearer",resultado_peticion_token$token)


  # Llamada API tiempo
  url <- "https://www.el-tiempo.net/api/json/v2/provincias/26/municipios/26089"
  get_tiempo <- httr::GET(url = url, add_headers("Content-Type"="application/json","Accept"="application/json"))
  tiempo <- jsonlite::fromJSON(rawToChar(get_tiempo$content))

  tmax_temperatura_ext <- tiempo$temperaturas$max
  temperaturas_hoy <- tiempo$pronostico$hoy$temperatura
  posicion_max <- length(temperaturas_hoy) - (23 - 15)  # 15 es la hora umbral de la jornada de trabajo
  max_temperatura_horas_objetivo <- max(temperaturas_hoy[1:posicion_max])


  # Detección invierno verano
  fecha <- Sys.Date()
  fecha_ref_1 <- as.Date("2021-05-14")
  year(fecha_ref_1) <- year(fecha)
  fecha_ref_2 <- as.Date("2021-09-22")
  year(fecha_ref_2) <- year(fecha)
  if(fecha >= fecha_ref_1 & fecha <= fecha_ref_2 & max_temperatura_horas_objetivo >= 18){
    consigna <- 27  # Verano
  }else{
    consigna <- 19  # Invierno
  }

  # Registro consignas
  for(i in 1:length(id_dispositivos)){

    # GET token del dispositivo
    url <- paste("http://116.202.100.157:8080/api/device/",id_dispositivos[i],"/credentials",sep = "")
    get_token <- httr::GET(url = url, add_headers("Content-Type"="application/json","Accept"="application/json","X-Authorization"=auth_thb))

    token <- jsonlite::fromJSON(rawToChar(get_token$content))
    token <- token$credentialsId

    url <- paste("http://116.202.100.157:8080/api/v1/",token,"/attributes",sep = "")

    # 1) Actualizacióna atributo Alumbrado OFF/ON
    json_envio_plataforma <- paste('{"Consigna temperatura":', consigna,'}',sep = "")
    post <- httr::POST(url = url,
                       add_headers("Content-Type"="application/json","Accept"="application/json","X-Authorization"=auth_thb),
                       body = json_envio_plataforma,
                       verify= FALSE,
                       encode = "json",verbose()
    )
  }

}
KepaAmigoTECHFriendly/prediccionesPresencia documentation built on Sept. 14, 2022, 4:09 p.m.