-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (75 loc) · 2.31 KB
/
server.js
File metadata and controls
85 lines (75 loc) · 2.31 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
const PythonShell = require('python-shell');
const express = require('express');
const app = express();
const port = 8080;
const fs = require('fs');
const lame = require('lame');
const Speaker = require('speaker');
const WebSocketServer = require("ws").Server
const wss = new WebSocketServer({port:3434});
const path = require('path');
let options = {
mode: 'text',
pythonPath: 'path/to/python',
pythonOptions: ['-u'],
scriptPath: 'scripts/',
args: ['value1', 'value2', 'value3']
};
app.get('/servoClock', async (req, res) => {
PythonShell.run(path.join(__dirname,'scripts/servo2.py'), function (err, results) {
if (err) {
console.log(err);
}
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
res.sendStatus(200);
});
});
app.get('/servoAntiClock', async (req, res) => {
PythonShell.run(path.join(__dirname,'scripts/servo.py'), function (err, results) {
if (err) {
console.log(err);
}
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
res.sendStatus(200);
});
});
app.get('/alarm', async (req, res) => {
//sudo apt-get install libasound2-dev (for speaker library to work)
fs.createReadStream(path.join(__dirname,'sounds/alarm.mp3'))
.pipe(new lame.Decoder())
.on('format', function (format) {
this.pipe(new Speaker(format));
});
res.sendStatus(200);
});
app.get('/elevator', async (req, res) => {
//sudo apt-get install libasound2-dev (for speaker library to work)
fs.createReadStream(path.join(__dirname,'sounds/elevator.mp3'))
.pipe(new lame.Decoder())
.on('format', function (format) {
this.pipe(new Speaker(format));
});
res.sendStatus(200);
});
app.get('/screen', async (req, res) => {
//sudo apt-get install libasound2-dev (for speaker library to work)
wss.broadcast('change', 'screen')
res.sendStatus(200);
});
app.use(express.static(path.join(__dirname,'static')));
app.listen(port, (err) => {
if (err) {
console.error('error', err);
} else {
console.log(`app listening on port ${port}`);
}
});
wss.broadcast = function broadcast(message, room){
wss.clients.forEach(function(client){
if(room == client.protocol){
client.send(JSON.stringify([{message: message}]))
}
});
};