1
|
|
|
/** |
2
|
|
|
* A node application to start a X amount of scooters. |
3
|
|
|
* Used to simulate a system of X scooters. |
4
|
|
|
* scooter.js will load .env file |
5
|
|
|
* This will later be removed, since docker will handle loading environment variables |
6
|
|
|
* Environment variables: |
7
|
|
|
* DBURI |
8
|
|
|
* NUMBER_OF_SCOOTERS |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
const { Scooter } = require("./scooter"); |
12
|
|
|
const db = require("./modules/sparkdb"); |
13
|
|
|
const scooter = require("./scooter"); |
14
|
|
|
|
15
|
|
|
const numberOfScooters = process.env.NUMBER_OF_SCOOTERS; |
16
|
|
|
let scooters = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Loads x number of scooters and returns them |
20
|
|
|
* @return list of scooter |
21
|
|
|
*/ |
22
|
|
|
async function loadNewScooters() { |
23
|
|
|
const scooterArray = []; |
24
|
|
|
for (let i = 0; i < numberOfScooters; i++) { |
25
|
|
|
const scooter = new Scooter(); |
26
|
|
|
await scooter.load(); // Load up new scooter, can pass id if custom generation is wanted |
27
|
|
|
scooterArray.push(scooter); |
28
|
|
|
} |
29
|
|
|
return scooterArray; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Starts update function on scooter |
34
|
|
|
* @param mixed scooter |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
async function startUpdate(scooter) { |
39
|
|
|
try { |
40
|
|
|
scooter.update(); |
41
|
|
|
} catch (e) { |
42
|
|
|
console.log(e); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed scooterArray |
48
|
|
|
* |
49
|
|
|
* @return [type] |
50
|
|
|
*/ |
51
|
|
|
async function startUpdateScooters() { |
52
|
|
|
scooters.forEach(scooter => { |
53
|
|
|
startUpdate(scooter); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Prints useful information about the current state of the scooters |
59
|
|
|
* @param mixed scooters |
60
|
|
|
* @param bool repeat |
61
|
|
|
* |
62
|
|
|
* @return [type] |
63
|
|
|
*/ |
64
|
|
|
async function printScooters(repeat) { |
65
|
|
|
const totalScooters = scooters.length; |
66
|
|
|
const scootersStateCount = {}; |
67
|
|
|
for (let i = 0; i < totalScooters; i++) { |
68
|
|
|
const scooter = scooters[i]; |
69
|
|
|
// console.log(scooter); |
70
|
|
|
scootersStateCount[scooter.status] = scootersStateCount[scooter.status] ? scootersStateCount[scooter.status] + 1 : 1; |
71
|
|
|
} |
72
|
|
|
for (const state in scootersStateCount) { |
73
|
|
|
console.log(state, scootersStateCount[state]); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
console.log("============================"); |
76
|
|
|
if (repeat) { |
77
|
|
|
setTimeout(() => printScooters(repeat), 5000); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string id |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
function removeScooter(id) { |
87
|
|
|
let index = -1; |
88
|
|
|
for (let i = 0; i < scooters.length; i++) { |
89
|
|
|
if (id === scooters[i]._id.toString()) { |
90
|
|
|
index = i; |
91
|
|
|
break; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
if (index !== -1) { |
95
|
|
|
console.log(`${scooters[index].name}: removed from database`); |
|
|
|
|
96
|
|
|
scooters.splice(index, 1); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return [type] |
102
|
|
|
*/ |
103
|
|
|
async function dropCallback() { |
104
|
|
|
console.log("Loading up new scooters:", numberOfScooters); |
|
|
|
|
105
|
|
|
const existingScooters = await db.getAllScooters(); |
106
|
|
|
const oldScooters = []; |
107
|
|
|
for (let i = 0; i < existingScooters.length; i++) { |
108
|
|
|
const scooter = new Scooter(errorCallback=removeScooter); |
|
|
|
|
109
|
|
|
await scooter.load(existingScooters[i]._id.toString()); |
110
|
|
|
oldScooters.push(scooter); |
111
|
|
|
} |
112
|
|
|
scooters = await loadNewScooters(); |
113
|
|
|
scooters = scooters.concat(oldScooters); |
114
|
|
|
console.log("Starting scooters") |
115
|
|
|
startUpdateScooters(scooters); |
|
|
|
|
116
|
|
|
printScooters(true); |
117
|
|
|
//TODO: Add watch on mongoDB database |
118
|
|
|
//IF NEW SCOOTER, ADD IT |
119
|
|
|
//IF SCOOTER IS REMOVED, REMOVES IT FROM ARRAY |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
async function main() { |
123
|
|
|
db.setMongoURI(process.env.DBURI); |
124
|
|
|
db.connect(); |
125
|
|
|
// console.log("Dropping scooter collection..."); |
126
|
|
|
// db.dropScooters(dropCallback); |
127
|
|
|
dropCallback(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (require.main === module) { |
131
|
|
|
main(); |
132
|
|
|
} |
133
|
|
|
|