-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
249 lines (223 loc) · 7.66 KB
/
index.js
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
//
// Serves the ambient and the relay module on HTTP.
//
// TODO: Other RDF serialisations
// TODO: LDP headers and implementation streamlining
// Author: kaefer3000
//
// Import the interface to Tessel hardware
var tessel = require('tessel');
// Load the interface to the ambient sensor
var ambientlib = require('ambient-attx4');
// Load the interface to the relay
var relaylib = require('relay-mono');
// Load the web framework
var express = require('express');
// Load the logger for the web framework
var logger = require('morgan')
// Load some parsers for HTTP message bodys
var bodyParser = require('body-parser')
var relay = relaylib.use(tessel.port['A']);
var ambient = ambientlib.use(tessel.port['B']);
// The root app
var app = express();
// The two routers for the sensors/actuators
var ambientApp = express.Router({ 'strict' : true });
var relayApp = express.Router({ 'strict' : true });
// accept any content type
relayApp.use(bodyParser.json({ 'type' : '*/*' }));
app.use(function (req, res, next) {
res.header("Content-Type",'application/ld+json');
next();
});
// configuring the app
app.set('json spaces', 2);
app.set('case sensitive routing', true);
app.set('strict routing', true);
app.use(logger('dev'));
// defining a utility method that redirects (301) missing trailing slashes
var redirectMissingTrailingSlash = function(request, response, next) {
if (!request.originalUrl.endsWith('/'))
response.redirect(301, request.originalUrl + '/');
else
next();
};
// wiring the apps and routers
app.use("/ambient", ambientApp);
app.use("/relay", relayApp);
// LDP description of the root app
app.all('/', redirectMissingTrailingSlash);
app.get('/', function(request, response) {
response.json({
'@id' : '' ,
'@type' : 'http://www.w3.org/ns/ldp#BasicContainer' ,
'http://www.w3.org/ns/ldp#contains' : ['ambient/' , 'relay/' ]
});
});
// describing the light sensor
ambientApp.route("/light").get(function (request, response) {
ambient.getLightLevel(function(err, data) {
if (err) {
response.status(500);
response.send(err);
return;
}
response.json({
'@id' : '#value' ,
'@type' : [ 'http://www.w3.org/ns/ssn/SensorOutput' , 'http://purl.org/linked-data/cube#Observation' ] ,
'http://xmlns.com/foaf/0.1/isPrimaryTopicOf' : '' ,
'http://www.w3.org/ns/ssn/isValueOf' : { '@id' : '#sensorOutput' , 'http://www.w3.org/ns/ssn/isProducedBy' : '#sensor' } ,
'http://example.org/hasLightValue' : data
});
});
});
// describing the sound sensor
ambientApp.route('/sound').get(function (request, response) {
ambient.getSoundLevel(function(err, data) {
if (err) {
response.status(500);
response.send(err);
return;
}
response.json({
'@id' : '#value' ,
'@type' : [ 'http://www.w3.org/ns/ssn/SensorOutput' , 'http://purl.org/linked-data/cube#Observation' ] ,
'http://xmlns.com/foaf/0.1/isPrimaryTopicOf' : '' ,
'http://www.w3.org/ns/ssn/isValueOf' : {
'@id' : '#sensorOutput' ,
'http://www.w3.org/ns/ssn/isProducedBy' : '#sensor'
} ,
'http://example.org/hasSoundValue' : data
});
});
});
// LDP description of the sensors of the ambient module
ambientApp.route('/').all(redirectMissingTrailingSlash);
ambientApp.route('/').get(function(request, response) {
var ret = {
'@id' : '' ,
'@type' : 'http://www.w3.org/ns/ldp#IndirectContainer' ,
'http://www.w3.org/ns/ldp#hasMemberRelation' : 'http://example.org/hasSensorValue',
'http://www.w3.org/ns/ldp#insertedContentRelation' : 'http://xmlns.com/foaf/0.1/primaryTopic',
'http://www.w3.org/ns/ldp#contains' : [],
'http://example.org/hasSensorValue' : []
};
if (ambientApp.stack)
ambientApp.stack.forEach(function(blubb){
if (blubb.route.path)
if (blubb.route.path.startsWith('/') && blubb.route.path.length > 1) {
ret['http://www.w3.org/ns/ldp#contains'].push(blubb.route.path.substring(1));
ret['http://example.org/hasSensorValue'].push(blubb.route.path.substring(1) + '#value');
}
});
response.json(ret);
});
// LDP description of the the relay module
relayApp.route('/').all(redirectMissingTrailingSlash)
.get(function(request, response) {
response.json({
'@id' : '' ,
'@type' : 'http://www.w3.org/ns/ldp#BasicContainer' ,
'http://www.w3.org/ns/ldp#contains' : ['1' , '2' ]
});
});
// GETting the state of one switch
relayApp.route("/:id").get(function(request, response) {
if (request.params.id == 1 || request.params.id == 2) {
relay.getState(Number(request.params.id), function(err, state) {
if (err) {
response.status(500);
response.send(err);
return;
}
response.json({
'@id' : '#actuator',
'http://xmlns.com/foaf/0.1/isPrimaryTopicOf' : '',
'@type' : 'http://purl.oclc.org/NET/UNIS/fiware/iot-lite#ActuatingDevice',
'http://example.org/isSwitchedOn' : state
});
});
} else {
response.sendStatus(404);
};
});
// PUTting the state of one switch
relayApp.route("/:id").put(function(request, response) {
if (request.params.id == 1 || request.params.id == 2) {
relay.getState(Number(request.params.id), function(err, state) {
if (err) {
response.status(500);
response.send(err);
return;
}
var datatype = typeof request.body['http://example.org/isSwitchedOn'];
var targetState;
switch (datatype) {
case "boolean":
targetState = request.body['http://example.org/isSwitchedOn'];
break;
case "string":
targetState = request.body['http://example.org/isSwitchedOn'].toLowerCase() == "true";
if (!targetState && request.body['http://example.org/isSwitchedOn'].toLowerCase() !== "false") {
response.status(400);
response.send("Please supply something with a proper boolean value for the http://example.org/isSwitchedOn property");
return;
}
break;
case "undefined":
response.status(400);
response.send("Please supply something with http://example.org/isSwitchedOn property (and give it a boolean value)");
return;
default:
response.status(400);
response.send("Please supply something with a proper boolean value for the http://example.org/isSwitchedOn property");
return;
}
if (typeof targetState !== "boolean") {
response.sendStatus(500);
} else if (targetState !== state) {
relay.setState(Number(request.params.id), targetState, function(err) {
if (err) {
response.status(500);
response.send(err);
return;
}
});
response.sendStatus(204);
return;
}
response.sendStatus(204);
return;
});
} else {
response.sendStatus(404);
return;
}
});
// Startup the server
var port = 8080;
app.listen(port, function () {
console.log('Example app listening on port ' + port);
});
// For finding the server in the network, some handy output on the console
console.log(require('os').networkInterfaces());
// error output for the ambient module
ambient.on('error', function (err) {
console.log(err);
});
// check mediatype of a request for json or json-ld
var acceptJSONLDMediaType = function(req) {
var datatype = typeof req.headers['content-type'];
switch (datatype) {
case "string":
var mediatype = req.headers['content-type'].toLowerCase();
if (mediatype.startsWith("application/ld+json")
|| mediatype.startsWith("application/json"))
return true;
else
return false;
break;
default:
return false;
}
};