-
Notifications
You must be signed in to change notification settings - Fork 2
/
mirador.js
149 lines (131 loc) · 5.06 KB
/
mirador.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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const index = require(path.join(__dirname, 'routes/index'));
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', index);
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')));
module.exports = app;
const hostname = '0.0.0.0';
const port = 3000;
const server = app.listen(port, hostname, () => {
console.log(`Mirador is running on http://${hostname}:${port}/`);
})
let robots = {};
let strategic_points = [];
let pings = {}
const io = require('socket.io')(server);
const mirador = require('./public/js/miradorlib.js');
const { randomUUID } = require('crypto');
io.on("connection", (socket) => {
socket.on("login", (robot) => {
let old_robot_id = Object.keys(robots).find(id => (robots[id].address === robot.address));
if (old_robot_id === undefined) {
robots[socket.id] = robot;
} else {
delete robots[old_robot_id].countdown;
robots[socket.id] = robots[old_robot_id];
socket.emit("recover", robots[socket.id]);
delete robots[old_robot_id];
}
socket.join("robots"); // join Robots room
socket.join(robot.address); // join WebRTC room
console.log("New connection:", socket.id, robot);
console.log("Total:", Object.keys(robots).length);
socket.broadcast.to("robots").emit("newbie", robot);
socket.to(robot.address).emit("watcher", socket.id);
});
socket.on("robot", (position, job) => {
if (socket.id in robots) {
robots[socket.id].position = position;
robots[socket.id].job = job;
}
io.to("robots").emit("siblings", (robots));
});
socket.on("broadcaster", () => {
let address = socket.request.connection.remoteAddress;
socket.join(address);
console.log("New broadcaster:", socket.request.connection.remoteAddress);
for (let id in robots) {
if (robots[id].address === address) {
socket.emit("watcher", id)
}
}
});
socket.on("offer", (id, message) => {
socket.to(id).emit("offer", socket.id, message);
});
socket.on("answer", (id, message) => {
socket.to(id).emit("answer", socket.id, message);
});
socket.on("candidate", (id, message) => {
socket.to(id).emit("candidate", socket.id, message);
});
socket.on("changeVideoSource", () => {
socket.to(robots[socket.id].address).emit("changeVideoSource");
});
//PINGS
socket.emit("updatePings", pings);
socket.on("updatePings", newPings => {
pings = newPings;
socket.broadcast.emit("updatePings", pings);
})
//STRATEGIC POINTS
socket.emit("updateStrategicPoints", strategic_points);
socket.on("newStratPoints", robot_points => {
let i = 0;
//let new_points = robot_points.filter(x => !Object.values(strategic_points).some(y => sameContent(x, y)));
let new_points = robot_points
for (let new_point of new_points) {
new_point.id = strategic_points.length;
strategic_points.push(new_point);
console.log(`New strategic point at ${new_point.position.latitude},${new_point.position.longitude}`);
i++;
}
io.emit("updateStrategicPoints", strategic_points);
});
socket.on("stratPointStatus", robot_point => {
let sp = strategic_points.find(x => x.id == robot_point.id);
if (sp) {
sp.status = robot_point.status;
console.log("updating a status");
}
io.emit("updateStrategicPoints", strategic_points);
})
socket.on("disconnect", () => {
if (socket.id in robots) {
console.log("Logout :", robots);
// Send a logout message to other robots
socket.to("robots").emit("logout", robots[socket.id]);
socket.to(robots[socket.id].address).emit("disconnectWatcher", socket.id);
delete robots[socket.id];
/*
robots[socket.id].countdown = setTimeout(() => {
delete robots[socket.id]
}, 1000);
*/
console.log("Total:", Object.keys(robots).length);
//socket.to(robots[socket.id].address).emit("disconnectWatcher", socket.id);
}
});
});
const POSITION_MIN_DISTANCE = 0.0001;
function samePosition(p1, p2) {
return Math.abs(p1.longitude - p2.longitude) < POSITION_MIN_DISTANCE &&
Math.abs(p1.latitude - p2.latitude) < POSITION_MIN_DISTANCE/2;
}
function sameContent(p1, p2) {
//Check if the Content is different or if the trap is a POI
if((p1.message == p2.message) && (p1.message !== "Point d'interet")){
console.log("Same server");
return 1;
}else{
console.log("New server");
return 0;
}
}