-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.R
More file actions
253 lines (236 loc) · 9.55 KB
/
db.R
File metadata and controls
253 lines (236 loc) · 9.55 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#
# DB functions
#
library(mongolite)
library(jsonlite)
library(dplyr, quietly = TRUE, warn.conflicts = FALSE)
# CONSTANTS
constant.type = function (x) {
factor(x, levels = c(1, 2, 3, 4),
labels = c("int", "bool", "real", "string")
)
}
constant.command.category = function (x) {
factor(x, levels = c(1, 2, 3, 4, 5, 6, 7),
labels = c("toggle", "temperature", "fan", "lightswitch",
"acmode", "lightintensity", "lightcolor")
)
}
constant.data.category = function (x) {
factor(x, levels = c(1, 2, 3, 4, 5, 6, 7),
labels = c("temperature", "luminance", "presence",
"humidity", "pressure", "windspeed", "smoke")
)
}
#
# INTERNAL FUNCTIONS
#
db._select = function (conn, ..., sort = NULL) {
# Do select with sort
if (is.null(sort)) {
conn$find(toJSON(list(...), auto_unbox = TRUE))
}
else {
conn$find(toJSON(list(...), auto_unbox = TRUE),
sort = toJSON(sort, auto_unbox = TRUE))
}
}
db._getUniqueId = function(nodes, node) {
# Select unique id of node
if (!is.na(node$dataId)) {
select(filter(nodes, controllerId == node$controllerId,
nodeId == node$nodeId,
id == node$dataId), uniqueId)[[1]]
}
else {
select(filter(nodes, controllerId == node$controllerId,
nodeId == node$nodeId,
id == node$commandId), uniqueId)[[1]]
}
}
db.get.metadata = function (house.id) {
# Function that gets the house metadata, returning a list with house metadata,
# in other words, information about the house sensors and actuators that are
# accepted and active.
#
# Args:
# house.id: The id of the house
# Returns:
# A list with two elements: data and commands. Each one is a data.frame
# containing all data and command available in the house
conn <- mongo(db = "server-db",
collection = paste('node', house.id, sep = '_'))
nodes <- db._select(conn, accepted = 1, alive = 1)
sensors <- NULL
actuators <- NULL
uniqueId <- 1
if (nrow(nodes) > 0) {
for (i in 1:nrow(nodes)) {
node <- nodes[i,]
# Get node room (all rules will apply only to devices in the same room)
baseDataFrame = cbind(node[c("controllerId", "nodeId")], node["extra"][[1]]["room"])
if (!is.null(node$dataType) && !is.null(node$dataType[[1]])) {
types <- node$dataType[[1]]
if (is.data.frame(types) && nrow(types) > 0) {
for (j in 1:nrow(types)) {
type <- types[j,]
if (constant.type(type$type) == "bool") {
sensors <- rbind(sensors, cbind(baseDataFrame,
data.frame(
uniqueId = uniqueId,
id = type$id,
type = constant.type(type$type),
category = constant.data.category(type$dataCategory),
min = 0,
max = 1
)))
}
else {
sensors <- rbind(sensors, cbind(baseDataFrame,
data.frame(
uniqueId = uniqueId,
id = type$id,
type = constant.type(type$type),
category = constant.data.category(type$dataCategory),
min = type$range[[1]][1],
max = type$range[[1]][2]
)))
}
uniqueId <- uniqueId + 1
}
}
}
if (!is.null(node$commandType) && !is.null(node$commandType[[1]])) {
types <- node$commandType[[1]]
if (is.data.frame(types) && nrow(types) > 0) {
for (j in 1:nrow(types)) {
type <- types[j,]
if (constant.type(type$type) == "bool") {
actuators <- rbind(actuators, cbind(baseDataFrame,
data.frame(
uniqueId = uniqueId,
id = type$id,
type = constant.type(type$type),
category = constant.command.category(type$commandCategory),
min = 0,
max = 1
)))
}
else {
actuators <- rbind(actuators, cbind(baseDataFrame,
data.frame(
uniqueId = uniqueId,
id = type$id,
type = constant.type(type$type),
category = constant.command.category(type$commandCategory),
min = type$range[[1]][1],
max = type$range[[1]][2]
)))
}
uniqueId <- uniqueId + 1
}
}
}
}
}
list(data = sensors, command = actuators)
}
db.get.training.data = function (house.id, nodes,
timestamp.start = 0,
timestamp.step = 60) {
# Function that gets all the house data with columns representing sensor
# or actuator data and the timestamp. It repeats the data each timestamp.step
# seconds. All times are represented in seconds.
#
# Args:
# house.id: The house id
# node.ids: Nodes in the format returned by db.get.metadata
# timestamp.start: the starting timestamp that we want the data (default = 0)
# timestamp.step: this function will repeat rows at this time step,
# for example, if you have a row in 0s and a row in 121s,
# the return will have the first row 3 times, in the case
# you are using the default step (default = 60)
# Returns:
# A data.frame with all the data ordered by timestamp and with n+1
# columns, where n columns contain data or command and the remainig
# column contains the timestamp
conn <- mongo(db = "server-db",
collection = paste('all_states', house.id, sep = '_'))
node.ids <- rbind(select(nodes$data, controllerId, nodeId),
select(nodes$command, controllerId, nodeId))
allData <- db._select(conn, "$or" = node.ids,
timestamp = list("$gte" = timestamp.start),
sort = list(timestamp = 1))
# Contains current data of all nodes
currentData = data.frame(timestamp = timestamp.start)
# Fill current data with NA
for (i in 1:nrow(nodes$data)) {
uniqueId <- nodes$data[i, "uniqueId"]
currentData[1,paste("data", uniqueId, sep = "_")] <- NA
}
for (i in 1:nrow(nodes$command)) {
uniqueId <- nodes$command[i, "uniqueId"]
currentData[1,paste("data", uniqueId, sep = "_")] <- NA
}
#currentData[1, "repeat"] <- 1
output <- NULL
completeData <- FALSE
if (nrow(allData) > 0) {
for (i in 1:nrow(allData)) {
cat("\r")
cat(format(round(100*(i / nrow(allData)), 2), nsmall = 2))
cat("%")
measure <- allData[i,]
uniqueId <- db._getUniqueId(rbind(nodes$data, nodes$command),
measure)
# Timestamp change
if (measure$timestamp > currentData$timestamp & currentData$timestamp > timestamp.start) {
nTimes <- floor((measure$timestamp - currentData$timestamp) /
timestamp.step)
#if (nTimes >= 1) {
# currentData[1, "repeatTimes"] <- nTimes
# output <- rbind(output, currentData)
#}
for (i in 1:nTimes) {
output <- rbind(output, currentData)
currentData$timestamp <- currentData$timestamp + timestamp.step
}
}
#Update current data with value and timestamp
currentData[1,paste("data", uniqueId, sep = "_")] <- measure$value
currentData$timestamp <- measure$timestamp
}
cat("\n")
}
output
}
db.put.rules = function (house.id, rules) {
# Function that save the generated rules in the DB
#
# Args:
# house.id: The id of the house
# rules:
# Returns:
#
#
#{
# command: {
# nodeId: NODE_ID,
# commandId: COMMAND_ID
# value: VALUE
# }
# controllerId: CONTROLLER_ID
# accepted: 0
# clauses: [
# [{
# "lhs": "NODE_ID.DATA_ID"
# "operator: "OPERATOR" (can be ==, <=, >=, !=)
# "rhs": "NODE_ID.DATA_ID" | VALUE
# }]
# ]
#}
conn <- mongo(db = "server-db",
collection = paste('rules', house.id, sep = '_'))
inserted <- conn$insert(rules)$nInserted
conn$find(fields = "{\"_id\": 1 }", sort = "{\"$natural\": -1 }", limit = inserted)[,"_id"]
}