-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
164 lines (156 loc) · 5.6 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
library(shiny)
library(httr)
library(shinydashboard)
library(jsonlite)
library(RCurl)
# Make reactive flags
values <- reactiveValues(flag = FALSE, sick_flag = FALSE,
prev_time = NULL, cur_time = Sys.time(),
process_time = NULL, image_ok=NULL)
result <- fromJSON("http://100.102.5.8:8000/nws-rest-api/last-weld")
temp <- as.data.frame(result$current)
names(temp) <- "current"
temp$voltage <- result$voltage
temp$resistance <- temp$voltage/temp$current
df <- temp
ip_addr = 'http://192.168.1.1/port0.jsn'
ftp_url = "ftp://192.168.1.2"
beat_df <- NULL
ui <- dashboardPage(skin = "black", # UI ####
dashboardHeader(title = tags$a(href='https://github.com/chronoclast/boschebol-gehaktbol',
tags$img(src="logo.png", style="width:100px;height:auto;"))),
dashboardSidebar(disable = TRUE),
dashboardBody(
fluidRow(
box(solidHeader = TRUE, status = "danger", title = div(icon("bolt","fa-fw"),"Last Welding"),
uiOutput("infoboxes"),
plotOutput("weld", height = "440px")),
div(box(solidHeader = TRUE, status = "danger", title = div(icon("image","fa-fw"),"SickScan"),
uiOutput("pic"))),
box(solidHeader = TRUE, status = "danger", title = div(icon("heartbeat","fa-fw"),"Part passing through..."),
plotOutput("heartbeat", height = "241px")
)
)
)
)
server <- function(input, output, session){
output$heartbeat <- renderPlot({ #heartbeat plot ####
df <- iolink()
plot(x=df$time, y=df$value, type="l",col="limegreen", ylim=c(0,1), lwd=2, xlab="Time")
})
iolink <- reactive({ #iolink reactive ####
invalidateLater(250)
balluf_result <- fromJSON(ip_addr)
dist <- strtoi(tolower(gsub("\\ ","",substr(balluf_result[[2]]$processInputs,1,5))),16)/10
r <- as.data.frame(Sys.time())
names(r) <- "time"
r$value <- ifelse(dist < 6000,1,0)
if(r$value == 1){
values$prev_time <- values$cur_time
values$flag <- TRUE
values$sick_flag <- TRUE
values$cur_time <- Sys.time()
values$process_time <- paste0(round(values$cur_time - values$prev_time,2),"sec")
}
beat_df <<- rbind(beat_df, r)
if (nrow(beat_df) > 30){
beat_df <<- beat_df[-1,]
}
return(beat_df)
})
apiCall <- reactive({ #api call reactive ####
result <- fromJSON("http://100.102.5.8:8000/nws-rest-api/last-weld")
temp <- as.data.frame(result$current)
names(temp) <- "current"
temp$voltage <- result$voltage
temp$resistance <- temp$voltage/temp$current
temp$spatter <- result$spatterTime
temp$error <- result$errorCode
df <<- temp
return(df)
})
output$infoboxes <- renderUI({ #infoboxes ####
if(values$flag==TRUE){
df <- apiCall()
values$flag <- FALSE
}
fluidRow(
infoBox(title = "Spatter",icon = icon("tint"), color = "olive",
value = ifelse(df$spatter[1]!=0,"Yes","No")),
infoBox(title = "Error",icon = icon("exclamation-circle"), color = "lime",
value = ifelse(df$error[1]!=0,"Yes","No")),
infoBox(title = "Time",icon = icon("recycle"), color = "orange",
value = values$process_time)
)
})
output$weld <- renderPlot({ ## OUTPUT WELD #####
if(values$flag==TRUE){
df <- apiCall()
values$flag <- FALSE
}
par(mfrow=c(2,1))
plot(x=row.names(df),y=df$current, type="l",col="limegreen", main="Current", ylab="Amps", xlab="")
plot(x=row.names(df),y=df$resistance,col="red", type ="l", main = "Resistance", ylab="Ohms", xlab="")
})
sickImage <- reactive({
invalidateLater(100)
print("executing...")
userpwd <- "weld:done"
filenames <- getURL(ftp_url, userpwd = userpwd,
ftp.use.epsv = FALSE,dirlistonly = TRUE)
destnames <- filenames <- strsplit(filenames, "\r*\n")[[1]] # destfiles = origin file names
#destnames <- filenames <- filenames[grepl("\\.png",filenames)]
filenames <- paste0(ftp_url,"/",filenames)
con <- getCurlHandle(ftp.use.epsv = FALSE, userpwd="weld:done")
mapply(function(x,y) writeBin(getBinaryURL(x, curl = con, dirlistonly = FALSE),
y), x = filenames, y = paste("D:/boschebol_hackathon/boschebol-gehaktbol/www/",destnames, sep = "")) #writing all zipped files in one directory
print("Image retrieved!")
return("image.png")
})
output$pic <- renderUI({ #SICK pic ####
print(paste0("SICK: ",values$sick_flag))
if(values$sick_flag == TRUE){
file <- sickImage()
values$sick_flag <- FALSE
area <- readLines("www/area.txt")
if (area == "YES"){
values$image_ok <- TRUE
} else {
values$image_ok <- FALSE
}
if(values$image_ok==TRUE){
feedback = "thumbs_up.png"
}
if(values$image_ok==FALSE){
feedback = "thumbs_down.png"
}
print(feedback)
div(
fluidRow(
img(src=feedback, width = 150, height = 150),
img(src=file, width = 220, height = 220)),
style="text-align:center;")
} else {
if(!is.null(values$image_ok)){
if(values$image_ok==TRUE){
feedback = "thumbs_up.png"
}
if(values$image_ok==FALSE){
feedback = "thumbs_down.png"
}
div(
fluidRow(
img(src=feedback, width = 150, height = 150),
img(src="image.png", width = 220, height = 220)),
style="text-align:center;")
} else {
div(
fluidRow(
img(src="image.png", width = 220, height = 220),
style="text-align:center;")
)
}
}
})
}
shinyApp(ui, server)