Skip to content

Commit

Permalink
free parked
Browse files Browse the repository at this point in the history
  • Loading branch information
JockeTS-BTH committed Jan 19, 2024
1 parent 66ce2b3 commit b07b0d6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
14 changes: 10 additions & 4 deletions simulation/SimulatedClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ class SimulatedClient {
async useScooter() {
console.log(`User ${this.user.FirstName} with ID ${this.user.UserID} using Scooter with ID ${this.scooter.ScooterID}!`);

this.trip = await publicHelper.getRandomMatchingTrip(this.scooter.Location.replace(/\s/g, ""));

if (this.scooter.Status != "Free Parking") {
this.trip = await publicHelper.getRandomMatchingTrip(this.scooter.Location.replace(/\s/g, ""));
} else {
this.trip = await publicHelper.generateFreeTrip(this.scooter.Location.replace(/\s/g, ""));
}

if (this.trip) {
this.intervalID = setInterval(() => this.moveScooter(), SimulatedClient.waypointInterval * 1000);
} else {
console.log("No trip available.");
}
}

Expand Down Expand Up @@ -85,7 +91,7 @@ class SimulatedClient {
publicHelper.updateScooter(this.scooter);
}

console.log("Moving scooter to: " + JSON.stringify(this.scooter.Location));
// console.log("Moving scooter to: " + JSON.stringify(this.scooter.Location));

this.tripIndex++;
}
Expand All @@ -94,7 +100,7 @@ class SimulatedClient {
// Return scooter
async returnScooter() {
SimulatedClient.numClients--;

console.log("Returning a scooter!");

await publicHelper.stopRent(this.rentalLogID, this.user, this.scooter);
Expand Down
11 changes: 5 additions & 6 deletions simulation/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ const publicHelper = require("./utils").publicHelper;
const SC = require("./SimulatedClient").SimulatedClient;

// Max number of clients at any given time
const maxClients = 1000;
const maxClients = 500;

// Time (in seconds) between new clients being activated
const clientInterval = 1;
const clientInterval = 3;

// Time (in seconds) it takes for a scooter to move from one waypoint to another
const waypointInterval = 1;

// Number of waypoints between a scooter's properties being updated in database
const updateInterval = 1;
const updateInterval = 3;

async function startSimulator() {

SC.waypointInterval = waypointInterval;
SC.updateInterval = updateInterval;

Expand All @@ -27,10 +26,10 @@ async function startSimulator() {

async function createNewClient() {
const user = await publicHelper.getRandomAvailableUser();
console.log(user);
// console.log(user);

const scooter = await publicHelper.getRandomAvailableScooter();
console.log(scooter);
// console.log(scooter);

new SC(user, scooter);
}
Expand Down
75 changes: 74 additions & 1 deletion simulation/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const rentHelper = {

const stationHelper = {
stations: [],
requestsMade: 0,

async initStations() {
if (this.stations.length === 0) {
Expand All @@ -107,6 +108,72 @@ const stationHelper = {
return station.StationID;
}
}
},

// Get a trip to a random station in same city as freely parked scooter
async getTripToStationInCity(location) {
await this.initStations();

if (this.requestsMade >= 40) {
return;
}

let index = publicHelper._getRndInteger(0, this.stations.length - 1);
let station = this.stations[index];

while (station.Location.charAt(1) != location.charAt(1)) {
index = publicHelper._getRndInteger(0, this.stations.length - 1);
station = this.stations[index];
}

const waypoints = await this.fetchWaypoints(location, station.Location);

const trip = {
Origin: location,
Destination: station.Location,
Waypoints: waypoints
};

return trip;
},

// Fetch waypoints between origin and destination from API
async fetchWaypoints(origin, destination) {
const url = "https://api.openrouteservice.org/v2/directions/cycling-electric"
const apiKey = "5b3ce3597851110001cf624820729cd7892e4b7488a775715f073283";

const originArr = origin.split(",");
const destinationArr = destination.split(",");

const response = await fetch(`${url}?api_key=${apiKey}&start=${originArr[1]},${originArr[0]}&end=${destinationArr[1]},${destinationArr[0]}`);

this.requestsMade++;

if (this.requestsMade === 40) {
const timeoutID = setTimeout(function() {
this.requestsMade = 0;
clearTimeout(timeoutID);
}.bind(this), 60 * 1000);
}

const obj = await response.json();

const waypoints = obj.features[0].geometry.coordinates;

this.switchLatLon(waypoints);

return waypoints;
},

// Swap the order of lat and lon in coordinates
switchLatLon(waypoints) {
for (const waypoint of waypoints) {
const lat = waypoint[1];
const lon = waypoint[0];

waypoint[0] = lat;
waypoint[1] = lon;
}
}
}

Expand Down Expand Up @@ -277,7 +344,7 @@ const publicHelper = {

let index = this._getRndInteger(0, scooters.length - 1);

while (scooters[index].Status != "Parked") {
while (scooters[index].Status === "In Use") {
index = this._getRndInteger(0, scooters.length - 1);
}

Expand Down Expand Up @@ -316,6 +383,12 @@ const publicHelper = {
return selectedTrip;
},

async generateFreeTrip(location) {
const trip = await stationHelper.getTripToStationInCity(location);

return trip;
},

async startRent(user, scooter) {
// Temporary?
stationHelper.initStations();
Expand Down

0 comments on commit b07b0d6

Please sign in to comment.