-
Notifications
You must be signed in to change notification settings - Fork 3
/
snap-server.js
89 lines (71 loc) · 3.19 KB
/
snap-server.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
var static = require('node-static');
var http = require('http');
var ev3 = require('./ev3dev.js');
var fileServer = new static.Server("./snap", { cache: 0 });
var sensors = {}, motors = {};
//Create a server
http.createServer(function (req, res) {
var reqPath = require('url').parse(req.url, true);
//If there's no specific path request, just serve the default
if(reqPath.pathname == '/') {
console.log('Serving default Snap! page');
fileServer.serveFile('/snap.html', 200, {}, req, res);
}
//If the request is looking for an ev3dev function, process it
else if (reqPath.pathname.indexOf('/ev3dev') == 0) {
var pathParts = reqPath.pathname.split('/');
pathParts.shift();
console.log('Request for ev3 function: ' + pathParts);
var endPath = pathParts[pathParts.length - 1];
var value = evaluateFunctionEndpoint(endPath, reqPath.query);
res.writeHead(200, {
'Cache-Control': 'no-cache'
});
res.end(value.toString());
}
//Otherwise, assume it's looking for Snap! files
else {
console.log('Serving "' + reqPath.pathname + '"');
fileServer.serve(req, res);
}
}).listen(80);
console.log('Server listening on port 80');
function evaluateFunctionEndpoint(endpointName, query) {
var portName = query['portName'],
valueIndex = query['index'],
propertyName = query['property'],
propertyValue = query['value'];
//Process the request's endpoint name
switch (endpointName) {
case 'sensor':
//If we haven't already initialized a sensor for this port, do it now
if (sensors[portName] == undefined)
sensors[portName] = new ev3.Sensor(portName);
//If they are requesting a sensor value,
// use the helper function to adjust the value according to dp
if (valueIndex != undefined)
return sensors[portName].getFloatValue(valueIndex);
//If they have not specified a new value, they are trying to read
if (propertyValue == undefined)
return sensors[portName].getString(propertyName);
//If we are still executing and haven't returned yet,
// we assume that they are trying to set a value
sensors[portName].setString(propertyName, propertyValue);
//Return something
// TODO: return a status code instead of a blank string
return '';
case 'motor':
//If we haven't already initialized a motor for this port, do it now
if (motors[portName] == undefined)
motors[portName] = new ev3.Motor(portName);
//If they have not specified a new value, they are trying to read
if(propertyValue == undefined)
return motors[portName].getString(propertyName);
//If we are still executing and haven't returned yet,
// we assume that they are trying to set a value
motors[portName].setString(propertyName, propertyValue);
//Return something
// TODO: return a status code instead of a blank string
return '';
}
}