library("shiny") library("rjson") if(!exists("access_code")){ code<-"XXXXXX" info_data<-data.frame(Humidity=integer(),Current_Temperature=integer(),Target_Temperature=integer(),Times=character()) access_code<-system(sprintf('curl -X POST "https://api.home.nest.com/oauth2/access_token?client_id=XXX=%s&client_secret=XXX&grant_type=authoriz..."',code),intern=T) document<-fromJSON(access_code) access<-document$access_token devices<-paste("https://developer-api.nest.com/devices?auth=",access,sep="") all_devices<-system(sprintf('curl -L -X GET -H "Accept: application/json" "%s"',devices),intern=T) device<-gsub("thermostats|:|{|\"|humidity.*","",all_devices,perl=T) themostat_code<-paste("https://developer-api.nest.com/devices/thermostats/",device,"?auth=",access,sep="") } get_nest_info = function(info_data){ thermostat<-system(sprintf('curl -L -X GET -H "Accept: application/json" "%s"',themostat_code),intern=T) info<-fromJSON(thermostat) timeframe<-Sys.time() timeframe<-gsub("\\d+-\\d+-\\d+\\s","",timeframe) new_data<-data.frame(Humidity=info$humidity,Current_Temperature=info$ambient_temperature_f,Target_Temperature=info$target_temperature_f,Times=timeframe,stringsAsFactors = F) if(nrow(info_data)==10){ info_data<-info_data[-1,] } info_data<-rbind(info_data,new_data) return(info_data) } runApp(list( ui = pageWithSidebar( headerPanel("Nest Dashboard"), sidebarPanel( ), mainPanel( plotOutput("nestPlot") ) ), server =function(input, output, session) { autoInvalidate <- reactiveTimer(10000, session) output$nestPlot <- renderPlot({ autoInvalidate() info_data<<-get_nest_info(info_data) plot(info_data$Current_Temperature,type="n",axes=F,ann=F) lines(info_data$Current_Temperature,col="blue") lines(info_data$Target_Temperature,col="red") points(info_data$Current_Temperature, pch=21, bg="lightcyan", cex=1.25) points(info_data$Target_Temperature, pch=21, bg="lightcyan", cex=1.25) box() xy<-length(info_data$Times) axis(2, col.axis="blue", las=1) axis(1, at=1:xy, lab=info_data$Times, col.axis="purple") legend("topright",c("Current Temp","Target Temp"),lty=c(1,1),col=c("blue","red"),bty="n") }) } )) |