forked from romeokienzler/ibm-developer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iotGame.r
50 lines (40 loc) · 1.22 KB
/
iotGame.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
library(ibmdbR)
mycon <- idaConnect("BLUDB", "", "")
idaInit(mycon)
df <- as.data.frame(ida.data.frame('"DASH105608"."TEMPERATURE"')[ ,c('NAME', 'TEMPERATURE', 'TIMESTAMP')])
pattern="%Y-%m-%d %H:%M:%S.000000"
timestamp=as.numeric(strptime(df$TIMESTAMP, pattern))
maxTimestamp=max(timestamp)
cutOffTimestamp=maxTimestamp - 10
idx = which(timestamp>cutOffTimestamp)
#idx
workingSubset=df[idx,]
#workingSubset
#values=as.numeric(workingSubset$TEMPERATURE)
#globalMean=mean(values)
#globalSD=sd(values)
#globalMean
#globalSD
devideIds=unique(workingSubset$NAME)
#devideIds
scores = 1:length(devideIds)
for (i in 1:length(devideIds)) {
idx = which(workingSubset$NAME==devideIds[i])
df=workingSubset[idx,]
values=as.numeric(df$TEMPERATURE)
#scores[i]=(sqrt((globalMean- mean(values))^2) +sqrt((globalSD- sd(values))^2))/2
#scores[i]=abs(globalSD- sd(values))
scores[i]=sd(values)
}
scores
if (max(scores)>1) {
devideIdOutlier=devideIds[scores==max(scores)]
} else {
devideIdOutlier=0;
}
#devideIds
devideIdOutlier
#dfalert <- as.data.frame(ida.data.frame('"DASH110683"."alert"')[ ,c('sensor')])
#dfalert
#idaUpdate(mycon, dfrm=dfalert)
#idadf(mycon,paste('update DASH105608.alert set sensor=\'',devideIdOutlier,'\''))