-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscooter.js
266 lines (253 loc) · 7.94 KB
/
scooter.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* A node application to start a single scooter brain.
* Supposed to be used inside of an electric scooter.
* For simulation purposes you can run them on a pc.
*
* Environment variables:
* DBURI
*
* Status values:
* Available
* In use
* Maintenance
* Off
* Unavailable
* Charging
* Needs charging
*/
require("dotenv").config();
const { ObjectId } = require("mongodb");
const db = require("./modules/sparkdb");
const { GPSComponent } = require("./modules/gps");
const updateFrequencyMilliseconds = process.env.UPDATE_FREQUENCY_MILLISECONDS;
const batteryDepletionRate = process.env.BATTERY_DEPLETION_RATE;
const lowBatteryWarning = 10;
const namingPrefix = "Spark-Rentals#";
/**
* Loads scooter from database using id
* Returns a db representation of a scooter or null
* Will force an exit if not found
* @param string id
*
* @return db-scooter | null | exit(1)
*/
async function LoadScooter(id) {
const scooter = await db.findScooter(id);
if (!scooter) {
throw "Scooter removed";
}
return scooter;
}
/**
* @return db-scooter
*/
async function NewScooter(status, owner, coordinates, battery) {
const gps = new GPSComponent();
const id = new ObjectId();
const scooter = {
_id: id,
status: status ? status : "Available",
name: namingPrefix + id.toString().slice(id.toString().length - 4, id.toString().length),
battery: battery ? battery : Math.max(20, Math.random() * 100),
owner: owner ? owner : "Karlskrona",
trip: null,
log: [],
speed: gps.speed,
coordinates: coordinates ? coordinates : gps.coordinates
};
const result = await db.pushScooter(scooter);
if (!result) {
console.log("Error pushing scooter to database...");
process.exit(1);
}
return scooter;
}
/**
* Clears the last line in the console
* @return void
*/
function clearLastLine() {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine(1);
}
/**
* Prepares for logging to console by clearing a few lines
* @return void
*/
function prepareWindowForPrint() {
for (let index = 0; index < 6; index++) {
clearLastLine();
}
}
function printScooter(scooter) {
console.log("Connected to database:", db.getMongoURI());
console.log("Update frequency:", updateFrequencyMilliseconds, "ms");
console.log("status:", scooter.status);
console.log("battery:", scooter.battery);
console.log("coordinates:", "{ " + scooter.gpsComponent.coordinates.latitude + ", " + scooter.gpsComponent.coordinates.longitude + " }");
console.log("speed:", scooter.gpsComponent.speed, "km/h");
}
/**
* @param mixed c1
* @param mixed c2
*
* @return boolean
*/
function coordinateEquals(c1, c2) {
return c1.latitude === c2.latitude && c1.longitude === c2.longitude;
}
/**
* A single scooter simulating a real electric scooter.
* Pass null to _id for new scooter
*
* functions:
* load()
* update()
* set()
* dbData()
*
* @return void
*/
function Scooter(errorCallback)
{
this.status = "Off";
this.owner = null;
this.gpsComponent = null;
this.trip = {};
this.log = [];
this._id = null;
this.errorCallback = errorCallback;
this.set = data => {
this._id = data._id;
this.name = data.name;
this.status = data.status;
this.owner = data.owner;
this.battery = data.battery;
this.gpsComponent = new GPSComponent(data.coordinates);
this.trip = data.trip;
this.log = data.log;
};
this.dbData = () => {
return {
_id: this._id,
name: this.name,
status: this.status,
battery: this.battery,
owner: this.owner,
trip: this.trip,
log: this.log,
speed: this.gpsComponent.speed,
coordinates: this.gpsComponent.coordinates
};
};
this.load = async (id, print) => {
if (id) {
const result = await LoadScooter(id);
this.set(result);
} else {
const result = await NewScooter();
this.set(result);
}
if (print) {
console.log("Starting scooter:", this.name);
}
if (!this._id) {
console.log("Something bad happened, error with id");
process.exit(1);
}
// await this.gpsComponent.loadRoute();
if (print) {
console.log("Scooter is running");
printScooter(this);
}
};
this.update = async (print) => {
let result = null;
try {
result = await LoadScooter(this._id);
} catch (e) {
this.errorCallback(this._id.toString());
return;
}
//If scooter is turned of it wont do anything
if (this.status === "Off" && result.status === "Off") {
setTimeout(() => this.update(print), updateFrequencyMilliseconds)
return;
}
if (this.status !== "Charging") {
this.battery -= batteryDepletionRate * (this.gpsComponent.speed + 1);
} else {
this.battery += batteryDepletionRate * 10;
if (this.battery > 100) {
this.battery = 100;
}
}
if (result.status !== this.status) {
if (result.status === "In use") {
// Start the new trip
// Rest api have created initialized trip, sync state only
console.log(`${this.name}: Starting trip`)
this.gpsComponent.loadRoute();
this.trip = result.trip;
this.status = result.status;
} else if (result.status === "Available" && this.status === "In use") {
// Stop the current trip
console.log(`${this.name}: Ending trip`)
const newLogEntry = {
...this.trip,
endPosition: { ...this.gpsComponent.coordinates },
distance: this.gpsComponent.route.traveledKilometers
};
this.gpsComponent.stopRoute();
db.pushLog(this._id, newLogEntry);
this.currentTrip = null;
} else if (result.status === "Off") {
console.log(`${this.name}: Remote shutdown..`);
} else if (this.status === "Off" && result.status !== "Off") {
// REMOVE ACTIVATIOM
console.log(`${this.name}: Remote activation..`);
}
this.status = result.status;
}
if (this.battery < lowBatteryWarning) {
this.status = "Unavailable";
db.updateStatus(this._id, this.status);
} else if (this.battery <= 0) {
console.log(`${this.name}: Battery depleted`);
this.battery = 0;
this.status === "Off";
}
if (!coordinateEquals(result.coordinates, this.gpsComponent.coordinates)) {
// new coordinates manually set
// If this happens when a trip is in progress, what to do?
console.log(`${this.name}: Coordinate changed`)
this.gpsComponent.coordinates = result.coordinates;
}
this.gpsComponent.update(updateFrequencyMilliseconds);
db.updateScooterStates(this._id, this.gpsComponent.coordinates, this.gpsComponent.speed, this.battery);
if (this.canUpdateTrip()) {
db.updateScooterTrip(this._id, this.gpsComponent.route.traveledKilometers);
}
if (print) {
prepareWindowForPrint();
printScooter(this);
}
setTimeout(() => this.update(print), updateFrequencyMilliseconds)
};
this.canUpdateTrip = () => {
return this.trip && this.gpsComponent.route;
}
}
async function main() {
const scooter = new Scooter();
await scooter.load(false, true);
scooter.update(true);
}
if (require.main === module) {
db.setMongoURI(process.env.DBURI);
db.connect();
main();
}
module.exports = {
Scooter: Scooter
}