-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (100 loc) · 3.58 KB
/
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
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
const
http = require("http"),
express = require("express"),
socketio = require("socket.io");
const {
exec
} = require("child_process");
const https = require('https');
const fs = require("fs");
const voiture = require("./addon/build/Release/teslawish.node")
const { StreamCamera, Codec, Flip, SensorMode,StillCamera } = require('pi-camera-connect');
voiture.init();
// const streamCamera = new StreamCamera({
// codec: Codec.MJPEG,
// flip: Flip.Both,
// sensorMode: SensorMode.Mode6
// });
// async function cameraStartCapture() {
// await streamCamera.startCapture();
// }
const avancerVoiture = (millis)=>{
voiture.avancer(20);
setTimeout(voiture.arreter, millis);
}
const reculerVoiture = (millis)=>{
voiture.reculer(millis);
setTimeout(voiture.arreter, millis);
}
const directionVoiture = (angle)=>{
voiture.direction(parseInt(angle));
}
const stopVoiture = ()=>{
voiture.arreter();
}
const SERVER_PORT = 3000;
// create a new express app
const app = express();
// create http server and wrap the express app
let credentials = {
key: fs.readFileSync("./credentials/key.pem", "utf-8"),
cert: fs.readFileSync("./credentials/cert.pem", "utf-8")
};
const server = https.createServer(credentials, app);
const io = socketio(server);
let nextVisitorNumber = 1;
const onlineClients = new Set();
function onNewWebsocketConnection(socket) {
console.info(`Socket ${socket.id} has connected.`);
onlineClients.add(socket.id);
socket.on("disconnect", () => {
onlineClients.delete(socket.id);
console.info(`Socket ${socket.id} has disconnected.`);
});
socket.on("hello", helloMsg => console.info(`Socket ${socket.id} says: "${helloMsg}"`));
// will send a message only to this socket (different than using `io.emit()`, which would broadcast it)
socket.emit("welcome", `Vous êtes le pilote numéro ${nextVisitorNumber++}`);
socket.on("avancer", msg => {
avancerVoiture(msg);
io.emit("etat", `La voiture avance pendant ${parseInt(msg)/1000} secondes`);
});
socket.on("reculer", msg => {
reculerVoiture(msg);
io.emit("etat", `La voiture recule pendant ${parseInt(msg)/1000} secondes`);
});
socket.on("direction", msg => {
directionVoiture(msg);
io.emit("etat", `La voiture oriente ses roues à ${parseInt(msg)} degrès`);
io.emit("direction", msg);
});
socket.on("stop",()=>{
stopVoiture();
io.emit("etat","La voiture s'arrete");
});
// streamCamera.on('frame', (data) => {
// io.emit('pi-video-stream', "data:image/jpeg;base64," + data.toString("base64"));
// });
}
function startServer() {
// example on how to serve static files from a given folder
app.use(express.static("public"));
// will fire for every new websocket connection
io.on("connection", onNewWebsocketConnection);
// important! must listen from `server`, not `app`, otherwise socket.io won't function correctly
server.listen(SERVER_PORT, () => console.info(`Listening on port ${SERVER_PORT}.`));
// will send one message per second to all its clients
let secondsSinceServerStarted = 0;
// cameraStartCapture().then(() => {
// console.log('Camera is now capturing');
// });
// setInterval(()=>{
// streamCamera.takeImage().then((photo)=>{
// io.emit('pi-video-stream', "data:image/jpeg;base64," + photo.toString("base64"));
// });
// },60);
setInterval(() => {
secondsSinceServerStarted++;
io.emit("online", onlineClients.size);
}, 1000);
}
startServer();