From b07b0d60d8e475e9172c02a256ef918cfaf279b9 Mon Sep 17 00:00:00 2001 From: JockeTS Date: Fri, 19 Jan 2024 11:44:23 +0100 Subject: [PATCH] free parked --- simulation/SimulatedClient.js | 14 +++++-- simulation/simulator.js | 11 +++-- simulation/utils.js | 75 ++++++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 11 deletions(-) diff --git a/simulation/SimulatedClient.js b/simulation/SimulatedClient.js index 418d5bb..8adc2ea 100644 --- a/simulation/SimulatedClient.js +++ b/simulation/SimulatedClient.js @@ -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."); } } @@ -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++; } @@ -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); diff --git a/simulation/simulator.js b/simulation/simulator.js index 9fbe6c8..a42abac 100644 --- a/simulation/simulator.js +++ b/simulation/simulator.js @@ -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; @@ -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); } diff --git a/simulation/utils.js b/simulation/utils.js index 05ab6ee..95ad94a 100644 --- a/simulation/utils.js +++ b/simulation/utils.js @@ -84,6 +84,7 @@ const rentHelper = { const stationHelper = { stations: [], + requestsMade: 0, async initStations() { if (this.stations.length === 0) { @@ -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; + } } } @@ -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); } @@ -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();