-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster-server.js
More file actions
95 lines (81 loc) · 2.49 KB
/
Copy pathmaster-server.js
File metadata and controls
95 lines (81 loc) · 2.49 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
var express = require('express');
var bodyParser = require('body-parser');
var Auth = require('./auth.js');
var auth = Auth.auth;
var services = require('./services.js');
var proxy_request = require('./proxy-request.js');
var zerorpc = require("zerorpc");
var util = require('util');
// Create server
var app = express();
// var client = new zerorpc.Client();
// client.connect("tcp://127.0.0.1:4242");
// app.use(function sendParams (params){
// client.invoke("getparams", params, function(error, res, more) {
// console.log("node says:", res);
// });
// })
var clientParams={
argument:"scurry",
brightness:"255",
saturation:"100",
hue:"26",
hue2: "128",
vecx: "0",
vecy: "0",
count:"67",
speed:"100",
offset: "50",
radius: "3"
};
var server = new zerorpc.Server({
hello: function(name, reply) {
reply(null, "services Hello, " + name);
},
broadcastParams: function(params,reply) {
//console.log("broadcast request:",clientParams)
//this talks from the node server to the Python script whenever python requests it
reply(null, clientParams);
}
// getParams: function(name, reply) {
// reply(null, )
// }
});
server.bind("tcp://0.0.0.0:4242");
// Configure middleware
app.use(bodyParser.json()); // for parsing appplication/json
app.use(function logRequest (req, res, next) {
//console.log('Received request: ' + req.url);
if (req.url=="/param")
{
clientParams=req.body.params;
}
if (req.url=="/brightness")
{
clientParams['brightness']=req.body.params['brightness'];
}
return next();
});
// Configure static files
app.use(express.static('app'));
// Login route
app.post('/login', Auth.login);
// Ping route to check auth
app.head('/ping', auth, function (req, res, next) {
return res.sendStatus(200);
});
// config route,s (unique to master pi)
app.get('/config', auth, services.getConfig);
app.put('/config', auth, services.updateConfig);
app.get('/brightness', auth, services.getConfig);
app.put('/brightness', auth, services.updateConfig);
app.get('/wtf',auth, services.wtf);
app.get('/param',auth, services.getParams);
// error handler
app.use(function errorHandler(err, req, res, next) {
//console.log(util.inspect(req));
//res.status(500).json({'error': 'master-server.js: An error occurred on:'+req+':' + err});
});
app.listen(80, function listen () {
console.log("Master Server listening at port 80.");
});