-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.js
109 lines (88 loc) · 2.95 KB
/
app2.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
var fs = require('fs');
var Papa = require('papaparse');
var dotenv = require('dotenv').config()
// Const Vars
// Max number of drivers to simulate
const MAX_NO_TO_SIMULATE = 3;
// Start id for drivers
const START_ID = 1050
// Waypoints folder name
const WAYPOINTS_FOLDER_NAME = 'waypoints/';
// File type
const FILE_TYPE = '.csv';
simulator = (id) => {
var dirverQuery = 'name=admin' + id + '&type=deiver';
var socket = require('socket.io-client')(process.env.XTRACK_URL, {forceNew: true, query: dirverQuery});
// Lifecycle 1
socket.on('connect', function() {
// On connection open
console.log("Connection Established: " + id);
// Create driver object
socket.emit('driver:connection:join');
// Set Variables for this socket connection
var count = 0;
var exit_interval = false;
var data = {};
// Csv file reader streamer
var file_name = WAYPOINTS_FOLDER_NAME + id + FILE_TYPE;
var file = fs.createReadStream(file_name);
// Read waypoints
Papa.parse(file, {
//worker: true, // Don't bog down the main thread if its a big file
header: true,
dynamicTyping: true,
complete: function(results, file) {
data = results.data;
}
});
// Update location every 4 second
setInterval(() => {
// Check if exit_interval and exit
if(exit_interval) clearInterval(this);
// Check if the data is the last and if so exit the time interval
// Restart it
if(data.length == count) count = 0;
var coordinate = {
name: 'admin' + id,
longitude: data[count].longitude,
latitude: data[count].latitude
}
socket.emit('location:update', coordinate)
//console.log(" ID => " + id + " Coordinates => " + coordinate)
count++;
}, 400);
})
// Lifecycle 2
// after a given delay, the client tries to reconnect
socket.on('reconnect_attempt', () => {
console.log(id + " Connection Error, Retrying..." + process.env.XTRACK_URL)
});
// Lifecycle 3
// the first attempt fails
socket.on('reconnect_error', () => {
console.log(id + " Retrying Failed")
});
// Lifecycle 4
// the client won't try to reconnect anymore
socket.on('reconnect_failed', () => {
console.log(id + " Connection Failed, Closing connection")
});
// Lifecycle last
socket.on('disconnect', function() {
console.log("Connection closed For => " + id);
})
socket.on('error', function(error) {
console.log(error);
socket.close()
setTimeout(() => {
startSimulator();
}, 3000);
})
}
console.log("Simulator Started");
function startSimulator() {
for (var i = 2; i < MAX_NO_TO_SIMULATE; i++){
simulator(i);
}
}
startSimulator();