-
Notifications
You must be signed in to change notification settings - Fork 1
/
wss.js
83 lines (70 loc) · 2.26 KB
/
wss.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
const WebSocket = require('ws');
let { BruinBot } = require('./models/bruinbot.model');
let { coordDistanceM } = require('./util/utils');
let { VICINITY } = require('./constants');
const { Path } = require('./models/map.model');
const wss = new WebSocket.Server({ noServer: true });
const clients = new Map();
const commandBot = () => {
clients.forEach((client, key) => {
// Determine if we want to send command, who to send command to, and what to send
console.log('Sending command to bots...');
if (key === 'Teddy Bear') {
client.send('Pinging from server...');
}
});
};
const updateLocationHandler = async function (botId, lat, lon) {
try {
let bot = await BruinBot.findById(botId);
if (!bot) return;
bot.location.latitude = lat;
bot.location.longitude = lon;
if (bot.status == 'InTransit') {
let pathEnd = bot.path[bot.path.length - 1];
if (
coordDistanceM(lat, lon, pathEnd.latitude, pathEnd.longitude) < VICINITY
) {
bot.path = [];
bot.status = 'Idle';
}
}
await bot.save();
console.log(`Successfully updated location of bot ${botId}`);
} catch (err) {
console.log('Error: ' + err);
}
};
const messageHandler = async (msg, ws) => {
const msgSplit = msg.split(' ');
console.log(`received: ${msg}`);
if (msg.startsWith('register')) {
const key = msg.split('register')[1].substring(1);
clients.set(key, ws);
return `Welcome to BruinBot! ${key} is registered.`;
} else if (msg.startsWith('path')) {
const paths = await Path.find().populate('nodeA').populate('nodeB');
return JSON.stringify(paths);
} else if (msgSplit[0] == 'location' && msgSplit.length == 4) {
updateLocationHandler(msgSplit[1], msgSplit[2], msgSplit[3]);
return 'Accepted and attempting location update request to database!';
} else if (msgSplit[0] == 'join') return 'Welcome to BruinBot!';
else return 'Error: WebSocket request not valid.';
};
wss.on('connection', (ws) => {
ws.on('message', async (msg) => {
const response = await messageHandler(msg, ws);
ws.send(response);
});
ws.on('close', () => {
clients.forEach((client, key) => {
if (ws === client) {
console.log(`${key} is disconnected.`);
clients.delete(key);
}
});
});
});
setInterval(commandBot, 10000);
module.exports.wss = wss;
module.exports.clients = clients;