From 06e22b252c9e014565f981fe325b92f4fa50291b Mon Sep 17 00:00:00 2001 From: Liam Boddin Date: Thu, 31 Aug 2023 17:50:58 +0200 Subject: [PATCH 1/5] Added JSDoc to crypto.service.ts --- Server/src/services/crypto.service.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Server/src/services/crypto.service.ts b/Server/src/services/crypto.service.ts index 9657bfdf..1bbe1c07 100644 --- a/Server/src/services/crypto.service.ts +++ b/Server/src/services/crypto.service.ts @@ -1,6 +1,13 @@ import * as argon from "argon2" export default class CryptoService { + + /** + * Wrapper method for verifying a plain password against hashed password using argon2. + * + * @param hashedPassword The argon2-hashed password from the database to verify against. + * @param plainPassword The plain password the user entered. + */ public static async verify(hashedPassword: string, plainPassword: string): Promise { let isCorrectPassword: boolean try { @@ -10,10 +17,11 @@ export default class CryptoService { } return isCorrectPassword } + /** * Produces a hash using the argon hashing. * @param input The password, that needs to be hashed - * @returns Undefined, if the hashing is unsuccessful, a hash of the password otherwise. + * @returns undefined, if the hashing is unsuccessful, a hash of the password otherwise. */ public static async produceHash(input: string): Promise { try { From ab3a944e5d42f55c59cf69ea89aa0d24f4b5cf9a Mon Sep 17 00:00:00 2001 From: Liam Boddin Date: Thu, 31 Aug 2023 17:52:21 +0200 Subject: [PATCH 2/5] Remove unnecessary methods from poi service --- Server/src/routes/poi.route.ts | 8 +- Server/src/services/poi.service.ts | 218 ++++------------------------- 2 files changed, 28 insertions(+), 198 deletions(-) diff --git a/Server/src/routes/poi.route.ts b/Server/src/routes/poi.route.ts index 433643ef..a58198a0 100644 --- a/Server/src/routes/poi.route.ts +++ b/Server/src/routes/poi.route.ts @@ -142,7 +142,7 @@ export class PoiRoute { properties: null } - const type: POIType | null = await POIService.getPOITypeById(userData.typeId) + const type: POIType | null = await database.pois.getTypeById(userData.typeId) if (!type) { logger.error(`Could not find poi type with id ${userData.typeId}`) res.sendStatus(400) @@ -179,7 +179,7 @@ export class PoiRoute { return } - const poiToUpdate: POI | null = await POIService.getPOIById(poiId) + const poiToUpdate: POI | null = await database.pois.getById(poiId) if (!poiToUpdate) { logger.error(`Could not find poi with id ${userData.id}`) res.sendStatus(404) @@ -233,13 +233,13 @@ export class PoiRoute { } // Look out for the POI - const poi: POI | null = await POIService.getPOIById(poiId) + const poi: POI | null = await database.pois.getById(poiId) if (!poi) { logger.error(`Could not find poi with id ${poiId}`) res.sendStatus(500) return } - const success: boolean = await POIService.removePOI(poi) + const success: boolean = await database.pois.remove(poi.uid) if (!success) { logger.error(`Could not delete poi with id ${poiId}`) res.sendStatus(500) diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index 3ee5e423..baf331e7 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -3,8 +3,6 @@ import database from "./database.service" import TrackService from "./track.service" import VehicleService from "./vehicle.service" import GeoJSONUtils from "../utils/geojsonUtils" - -import distance from "@turf/distance" import { logger } from "../utils/logger" /** @@ -78,15 +76,6 @@ export default class POIService { return point } - /** - * - * @param id id of POI to search for - * @returns `POI` with `id` if it exists, `null` otherwise - */ - public static async getPOIById(id: number): Promise { - return database.pois.getById(id) - } - /** * Wrapper to get distance of poi in kilometers along the assigned track * @param poi `POI` to get the distance for @@ -167,6 +156,27 @@ export default class POIService { return (poiDistKm / trackLength) * 100 } + /** + * Search for POI's on a track + * @param track `Track` to search on for POI's + * @param type `POIType` to filter the returned POI's by + * @returns `POI[]` of all POI's along the given `track` + */ + public static async getAllPOIsForTrack(track: Track, type?: POIType): Promise { + // no type given, just return database query + if (type == null) { + return database.pois.getAll(track.uid) + } + + // filter by type + const trackPOIs = await database.pois.getAll(track.uid) + trackPOIs.filter(function(poi, _index, _poiList) { + return poi.typeId == type.uid + }) + return trackPOIs + } + +// THIS METHOD IS NOT USED ANYMORE BUT HAS SOME LOGICS IN IT /** * Search for nearby POI's either within a certain distance or by amount * @param point point to search nearby POI's from @@ -236,7 +246,7 @@ export default class POIService { return null } - allPOIsForTrack.filter(async function (poi, _index, _pois) { + allPOIsForTrack.filter(async function(poi, _index, _pois) { const poiTrackKm = await POIService.getPOITrackDistanceKm(poi) if (poiTrackKm == null) { return false @@ -247,7 +257,7 @@ export default class POIService { // filter pois by distance if given if (maxDistance != null) { - allPOIsForTrack.filter(async function (poi, _index, _pois) { + allPOIsForTrack.filter(async function(poi, _index, _pois) { const poiTrackKm = await POIService.getPOITrackDistanceKm(poi) if (poiTrackKm == null) { return false @@ -257,7 +267,7 @@ export default class POIService { }) } // sort POI's by distance to searched point - allPOIsForTrack = allPOIsForTrack.sort(function (poi0, poi1) { + allPOIsForTrack = allPOIsForTrack.sort(function(poi0, poi1) { // parse POI position const POIPos0 = GeoJSONUtils.parseGeoJSONFeaturePoint(poi0.position) const POIPos1 = GeoJSONUtils.parseGeoJSONFeaturePoint(poi1.position) @@ -292,184 +302,4 @@ export default class POIService { allPOIsForTrack.slice(0, count) return allPOIsForTrack } - - /** - * Search for POI's on a track - * @param track `Track` to search on for POI's - * @param type `POIType` to filter the returned POI's by - * @returns `POI[]` of all POI's along the given `track` - */ - public static async getAllPOIsForTrack(track: Track, type?: POIType): Promise { - // no type given, just return database query - if (type == null) { - return database.pois.getAll(track.uid) - } - - // filter by type - const trackPOIs = await database.pois.getAll(track.uid) - trackPOIs.filter(function (poi, _index, _poiList) { - return poi.typeId == type.uid - }) - return trackPOIs - } - - /** - * Set a new position for an existing POI - * @param poi `POI` to update - * @param position new position of `poi` - * @returns updated `POI` if successful, `null` otherwise - */ - public static async setPOIPosition(poi: POI, position: GeoJSON.Feature): Promise { - // enrich and update - const POITrack = await database.tracks.getById(poi.trackId) - if (POITrack == null) { - // TODO: this really should not happen, how to handle? delete POI? - return null - } - const enrichedPoint = await this.enrichPOIPosition(position, POITrack) - if (enrichedPoint == null) { - return null - } - return database.pois.update(poi.uid, undefined, undefined, undefined, undefined, enrichedPoint) - } - - /** - * Rename an existing POI - * @param poi `POI` to rename - * @param newName new name of `poi` - * @returns renamed `POI` if successful, `null` otherwise - */ - public static async renamePOI(poi: POI, newName: string): Promise { - return database.pois.update(poi.uid, newName) - } - - /** - * Update description for a given POI - * @param poi `POI` to update description for - * @param newDesc new description for `poi` - * @returns updated `POI` if successful, `null` otherwise - */ - public static async updateDescription(poi: POI, newDesc: string): Promise { - return database.pois.update(poi.uid, undefined, newDesc) - } - - /** - * Set new type of POI - * @param poi `POI` to update - * @param type new type of `poi` - * @returns updated `POI` if successful, `null` otherwise - */ - public static async setPOIType(poi: POI, type: POIType): Promise { - return database.pois.update(poi.uid, undefined, undefined, type.uid) - } - - /** - * Set track for POI - * @param poi `POI` to set track for - * @param track new `Track` for `poi` - * @returns updated `POI` if successful, `null` otherwise - */ - public static async setPOITrack(poi: POI, track: Track): Promise { - // update track kilometer value first - const poiPos = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position) - if (poiPos == null) { - // TODO: log this - return null - } - const updatedPOIPos = await this.enrichPOIPosition(poiPos, track) - if (updatedPOIPos == null) { - return null - } - - // update poi's track and track kilometer - return database.pois.update(poi.uid, undefined, undefined, undefined, track.uid, updatedPOIPos) - } - - /** - * Set if a POI is a turning point - * @param poi `POI` to update - * @param isTurningPoint indicator if `poi` is a turning point - * @returns updated `POI` if successful, `null` otherwise - */ - public static async setTurningPoint(poi: POI, isTurningPoint: boolean): Promise { - return database.pois.update(poi.uid, undefined, undefined, undefined, undefined, undefined, isTurningPoint) - } - - /** - * Delete existing POI - * @param poi `POI` to delete - * @returns `true`, if deletion was successful, `false` otherwise - */ - public static async removePOI(poi: POI): Promise { - return database.pois.remove(poi.uid) - } - - // --- POI-types --- - - /** - * Create new POI-type - * @param type name of new POI-type - * @param icon name of an icon associated to type - * @param desc optional description of new POI-type - * @returns created `POIType` if successful, `null` otherwise - */ - public static async createPOIType(type: string, icon: string, desc?: string): Promise { - return database.pois.saveType(type, icon, desc) - } - - /** - * - * @returns all existing `POIType`s - */ - public static async getAllPOITypes(): Promise { - return database.pois.getAllTypes() - } - - /** - * Search for POI type by a given id - * @param id id to search POI type by - * @returns `POIType` with id `id` if successful, `null` otherwise - */ - public static async getPOITypeById(id: number): Promise { - return database.pois.getTypeById(id) - } - - /** - * Change name of existing POI-type - * @param type `POIType` to change name of - * @param newType new name for `type` - * @returns renamed `POIType` if successful, `null` otherwise - */ - public static async renamePOIType(type: POIType, newType: string): Promise { - return database.pois.updateType(type.uid, newType) - } - - /** - * Update description of existing POI-type - * @param type `POIType` to change description of - * @param desc new description for `type` - * @returns updated `POIType` if successful, `null` otherwise - */ - public static async setPOITypeDescription(type: POIType, desc: string): Promise { - return database.pois.updateType(type.uid, undefined, undefined, desc) - } - - /** - * Change icon of POI type - * @param type `POIType` to change the icon of - * @param icon name of new icon to be associated with type - * @returns updated `POI` if successful, `null` otherwise - */ - public static async setPOITypeIcon(type: POIType, icon: string): Promise { - return database.pois.updateType(type.uid, undefined, icon) - } - - /** - * Delete existing POI-type - * @param type `POIType` to delete - * @returns `true` if deletion was successful, `false` otherwise - */ - public static async removePOIType(type: POIType): Promise { - return database.pois.removeType(type.uid) - } } From dd8ff11a1c11171d5cb9386e15bb058b30e74385 Mon Sep 17 00:00:00 2001 From: Liam Boddin Date: Thu, 31 Aug 2023 17:53:47 +0200 Subject: [PATCH 3/5] Remove unnecessary methods from track service --- Server/src/services/track.service.ts | 56 +--------------------------- 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/Server/src/services/track.service.ts b/Server/src/services/track.service.ts index 08eb0354..743c1e2b 100644 --- a/Server/src/services/track.service.ts +++ b/Server/src/services/track.service.ts @@ -57,7 +57,7 @@ export default class TrackService { track: GeoJSON.FeatureCollection ): GeoJSON.FeatureCollection { // iterate over all features - turfMeta.featureEach(track, function (feature, featureIndex) { + turfMeta.featureEach(track, function(feature, featureIndex) { // compute track kilometer for each point if (featureIndex > 0) { const prevFeature = track.features[featureIndex - 1] @@ -265,58 +265,4 @@ export default class TrackService { } return turfHelpers.lineString(turfMeta.coordAll(trackData)) } - - /** - * Search for all tracks that have a given location as start or end point - * @param location location to search for - * @returns all `Track[]`, which have `location` either as their starting location or as their destination, thus could be empty - */ - public static async searchTrackByLocation(location: string): Promise { - return database.tracks.getByLocation(location) - } - - /** - * Assign a new path of GeoJSON points to an existing track - * @param track existing track - * @param path new path for `track` - * @returns `Track` with updated path - */ - public static async updateTrackPath( - track: Track, - path: GeoJSON.FeatureCollection - ): Promise { - const enrichedTrack = this.enrichTrackData(path) - // typecast to any, because JSON is expected - // typecast to any, because JSON is expected - return database.tracks.update(track.uid, undefined, undefined, enrichedTrack as any) - } - - /** - * Update starting location of a track - * @param track `Track` to update - * @param newStart new starting location of `track` - * @returns updated `Track` if successful, `null` otherwise - */ - public static async setStart(track: Track, newStart: string): Promise { - return database.tracks.update(track.uid, newStart) - } - - /** - * Update destination of a track - * @param track `Track` to update - * @param newDest new destination of `track` - * @returns updated `Track` if successful, `null` otherwise - */ - public static async setDestination(track: Track, newDest: string): Promise { - return database.tracks.update(track.uid, undefined, newDest) - } - - /** - * Delete track - * @param track track to delete - * @returns `true` if deletion was successfull, `false` otherwise - */ - public static async removeTrack(track: Track): Promise { - return database.tracks.remove(track.uid) - } } From fd6c5d3d62a5da080fcbf668747d139640250fed Mon Sep 17 00:00:00 2001 From: Liam Boddin Date: Thu, 31 Aug 2023 17:55:22 +0200 Subject: [PATCH 4/5] Remove unnecessary methods from vehicle- and tracker service --- Server/src/routes/track.route.ts | 11 +- Server/src/routes/tracker.route.ts | 5 +- Server/src/routes/vehicle.route.ts | 6 +- Server/src/services/tracker.service.ts | 113 +---------------- Server/src/services/vehicle.service.ts | 161 ++----------------------- 5 files changed, 18 insertions(+), 278 deletions(-) diff --git a/Server/src/routes/track.route.ts b/Server/src/routes/track.route.ts index b6f1133a..d670083e 100644 --- a/Server/src/routes/track.route.ts +++ b/Server/src/routes/track.route.ts @@ -4,13 +4,12 @@ import TrackService from "../services/track.service" import { POI, Track, Vehicle } from "@prisma/client" import please_dont_crash from "../utils/please_dont_crash" import { logger } from "../utils/logger" -import { UpdateTrack, BareTrack, FullTrack, PointOfInterest, Position, Vehicle as APIVehicle } from "../models/api" +import { BareTrack, FullTrack, PointOfInterest, Position, UpdateTrack, Vehicle as APIVehicle } from "../models/api" import VehicleService from "../services/vehicle.service" import { Feature, GeoJsonProperties, LineString, Point } from "geojson" import POIService from "../services/poi.service" import GeoJSONUtils from "../utils/geojsonUtils" import database from "../services/database.service" -import TrackerService from "../services/tracker.service" /** * The router class for the routing of the track uploads from the website. @@ -221,9 +220,9 @@ export class TrackRoute { // If we know that, convert it in the API format. const pos: Position | undefined = geo_pos ? { - lat: GeoJSONUtils.getLatitude(geo_pos), - lng: GeoJSONUtils.getLongitude(geo_pos) - } + lat: GeoJSONUtils.getLatitude(geo_pos), + lng: GeoJSONUtils.getLongitude(geo_pos) + } : undefined // Also acquire the percentage position. It might happen that a percentage position is known, while the position is not. // This might not make much sense. @@ -234,7 +233,7 @@ export class TrackRoute { track: vehicle.trackId, name: vehicle.name ? vehicle.name : "Empty Name", type: vehicle.typeId, - trackerIds: (await TrackerService.getTrackerByVehicle(vehicle.uid)).map(y => y.uid), + trackerIds: (await database.trackers.getByVehicleId(vehicle.uid)).map(y => y.uid), pos, percentagePosition, heading diff --git a/Server/src/routes/tracker.route.ts b/Server/src/routes/tracker.route.ts index 727e0e89..544ab54a 100644 --- a/Server/src/routes/tracker.route.ts +++ b/Server/src/routes/tracker.route.ts @@ -5,7 +5,6 @@ import TrackerService from "../services/tracker.service" import { UplinkTracker } from "../models/api.tracker" import please_dont_crash from "../utils/please_dont_crash" import { Tracker, Vehicle } from "@prisma/client" -import VehicleService from "../services/vehicle.service" import database from "../services/database.service" import { Tracker as APITracker } from "../models/api" @@ -152,7 +151,7 @@ export class TrackerRoute { } const trackerId = trackerData.end_device_ids.device_id // load the tracker from the database - const tracker: Tracker | null = await TrackerService.getTrackerById(trackerId) + const tracker: Tracker | null = await database.trackers.getById(trackerId) if (!tracker) { logger.silly(`Tried to append log on unknown tracker with id ${trackerId}`) res.sendStatus(401) @@ -165,7 +164,7 @@ export class TrackerRoute { } // and get the vehicle the tracker is attached to. If it has no associated vehicle, do nothing. const associatedVehicle: Vehicle | null = tracker.vehicleId - ? await VehicleService.getVehicleById(tracker.vehicleId) + ? await database.vehicles.getById(tracker.vehicleId) : null if (!associatedVehicle) { logger.silly(`Got position from tracker ${trackerId} with no associated vehicle.`) diff --git a/Server/src/routes/vehicle.route.ts b/Server/src/routes/vehicle.route.ts index df599dc0..18859e28 100644 --- a/Server/src/routes/vehicle.route.ts +++ b/Server/src/routes/vehicle.route.ts @@ -47,11 +47,11 @@ export class VehicleRoute { /** * Get a list of vehicles with all the required properties for CRUD operations - * @param req A request containing a track id in the parameters + * @param _req A request containing a track id in the parameters * @param res A list of `VehicleListItemWebsite`. * @returns Nothing */ - private async getAllVehicles(req: Request, res: Response): Promise { + private async getAllVehicles(_req: Request, res: Response): Promise { const vehicles = await database.vehicles.getAll() const apiVehicles: APIVehicle[] = await Promise.all( vehicles.map(async vehicle => { @@ -320,7 +320,7 @@ export class VehicleRoute { return } - const userVehicle: Vehicle | null = await VehicleService.getVehicleById(userData.vehicleId) + const userVehicle: Vehicle | null = await database.vehicles.getById(userData.vehicleId) if (!userVehicle) { logger.error(`Could not find vehicle with id ${userData.vehicleId}`) res.sendStatus(404) diff --git a/Server/src/services/tracker.service.ts b/Server/src/services/tracker.service.ts index 9b11b129..6d3a77d4 100644 --- a/Server/src/services/tracker.service.ts +++ b/Server/src/services/tracker.service.ts @@ -1,64 +1,10 @@ -import { logger } from "../utils/logger" -import { Prisma, Log, Tracker, Vehicle } from "@prisma/client" -import VehicleService from "./vehicle.service" +import { Log, Prisma, Vehicle } from "@prisma/client" import database from "./database.service" /** * Service for tracker management. This includes registration of new trackers and writing logs. */ export default class TrackerService { - /** - * Register new trackers - * @param trackerId id of `Tracker` - * @param data data from tracker when sending hello-message - * @returns `Tracker` if registration was successful, `null` otherwise - */ - public static async registerTracker(trackerId: string, data?: unknown): Promise { - const tracker = await this.getTrackerById(trackerId) - if (tracker == null) { - return await database.trackers.save(trackerId, null, data) - } else { - return tracker - } - } - - /** - * Search for tracker by id - * @param id id of `Tracker` - * @returns `Tracker` if it exists, `null` otherwise - */ - public static async getTrackerById(id: string): Promise { - return database.trackers.getById(id) - } - - /** - * Get all trackers for a given vehicle - * @param vehicleId `Vehicle.uid`, the trackers are assigned to - * @returns `Tracker`[] assigned to `vehicle` - */ - public static async getTrackerByVehicle(vehicleId: number): Promise { - return await database.trackers.getByVehicleId(vehicleId) - } - - /** - * Assign tracker to a vehicle - * @param tracker `Tracker` to assign to a vehicle - * @param vehicle `Vehicle`, which gets assigned a tracker - * @returns `Tracker` that got assigned to a `Vehicle` if successful, `null` otherwise - */ - public static async setVehicle(tracker: Tracker, vehicle: Vehicle): Promise { - return database.trackers.update(tracker.uid, vehicle.uid) - } - - /** - * Deletes a tracker - * @param tracker `Tracker` to delete - * @returns `true` if deletion was successful, `false` otherwise - */ - public static async removeTracker(tracker: Tracker): Promise { - return database.trackers.remove(tracker.uid) - } - // --- Vehicle logs --- /** @@ -100,61 +46,4 @@ export default class TrackerService { trackerId }) } - - /** - * TODO: Define internal schema for data? Where? - * Log new data received by a tracker (wrapper to call from tracker endpoints, - * because they cannot "know" what vehicle they are on) - * @param trackerId id of the `TrackerĀ“ - * @param timestamp creation timestamp of the log - * @param position current position - * @param heading heading of the vehicle in degree (0-359) - * @param speed speed of the vehicle in kmph - * @param battery battery voltage of the tracker in V - * @param data data received by a tracker - * @returns a new entry `Log` if successful, `null` otherwise - */ - public static async appendTrackerLog( - trackerId: string, - timestamp: Date, - position: [number, number], - heading: number, - speed: number, - battery: number, - data: unknown - ): Promise { - logger.info("reached service") - logger.info(data) - - // check if tracker already exists and if not create it - let tracker = await this.getTrackerById(trackerId) - if (tracker == null) { - tracker = await this.registerTracker(trackerId) - } - - if (tracker == null || tracker.vehicleId == null) { - // TODO: log this, especially if tracker is still null - // (no vehicle id is not that critical as a tracker could exist without an assigned vehicle, - // but logging will not happen then and would not make sense) - return null - } - - const vehicle = await VehicleService.getVehicleById(tracker.vehicleId) - if (vehicle == null) { - // TODO: log this, a vehicle should exist if a tracker is assigned to it - return null - } - // actual wrapper - return this.appendLog(vehicle, timestamp, position, heading, speed, trackerId, battery, data) - } - - /** - * Get log entries for a given vehicle - * @param vehicle `Vehicle` to search the log entries by - * @param tracker (optional) `Tracker` to filter logs - * @returns `Log[]` of all log entries for `vehicle` or `null` if an error occured - */ - public static async getVehicleLogs(vehicle: Vehicle, tracker?: Tracker): Promise { - return database.logs.getAll(vehicle.uid, tracker?.uid) - } } diff --git a/Server/src/services/vehicle.service.ts b/Server/src/services/vehicle.service.ts index d9a60166..853af1ba 100644 --- a/Server/src/services/vehicle.service.ts +++ b/Server/src/services/vehicle.service.ts @@ -1,13 +1,10 @@ import { logger } from "../utils/logger" import database from "./database.service" -import { Vehicle, VehicleType, Track, Tracker, Log } from ".prisma/client" +import { Log, Track, Vehicle, VehicleType } from ".prisma/client" import TrackService from "./track.service" -import TrackerService from "./tracker.service" import GeoJSONUtils from "../utils/geojsonUtils" import along from "@turf/along" -import * as turfHelpers from "@turf/helpers" -import * as turfMeta from "@turf/meta" import nearestPointOnLine from "@turf/nearest-point-on-line" import { Position } from "../models/api" @@ -29,36 +26,6 @@ export default class VehicleService { }) } - /** - * Create a new vehicle - * @param type `VehicleType` of new vehicle - * @param name name for new vehicle (has to be unique for the track) - * @param track_uid `Track` - * @returns created `Vehicle` if successful, `null` otherwise - */ - public static async createVehicle(type: VehicleType, track_uid: number, name: string): Promise { - return database.vehicles.save(type.uid, track_uid, name) - } - - /** - * Search vehicle by id - * @param id id to search vehicle for - * @returns `Vehicle` with id `id` if it exists, `null` otherwise - */ - public static async getVehicleById(id: number): Promise { - return database.vehicles.getById(id) - } - - /** - * Search vehicle by name (this function should not be used mainly to identify a vehicle, but rather to get the vehicle id) - * @param name name to search the vehicle by (which should be unique on the given track) - * @param track `Track` the vehicle is assigned to - * @returns `Vehicle` with name `name` if it exists, `null` otherwise - */ - public static async getVehicleByName(name: string, track: Track): Promise { - return database.vehicles.getByName(name, track.uid) - } - /** * Search for nearby vehicles either within a certain distance or by amount and either from a given point or vehicle * @param point point to search nearby vehicles from, this could also be a vehicle @@ -128,7 +95,7 @@ export default class VehicleService { return null } - allVehiclesOnTrack.filter(async function (vehicle, index, vehicles) { + allVehiclesOnTrack.filter(async function(vehicle, index, vehicles) { const vehicleTrackKm = await VehicleService.getVehicleTrackDistanceKm(vehicle) if (vehicleTrackKm == null) { // TODO: log this @@ -140,7 +107,7 @@ export default class VehicleService { // filter vehicles by distance if given if (maxDistance != null) { - allVehiclesOnTrack.filter(async function (vehicle, index, vehicles) { + allVehiclesOnTrack.filter(async function(vehicle, index, vehicles) { const vehicleTrackKm = await VehicleService.getVehicleTrackDistanceKm(vehicle) if (vehicleTrackKm == null) { return false @@ -151,7 +118,7 @@ export default class VehicleService { // enrich vehicles with track distance for sorting let vehiclesWithDistances: [Vehicle, number][] = await Promise.all( - allVehiclesOnTrack.map(async function (vehicle) { + allVehiclesOnTrack.map(async function(vehicle) { let vehicleDistance = await VehicleService.getVehicleTrackDistanceKm(vehicle) vehicleDistance = vehicleDistance == null ? -1 : vehicleDistance // this should not happen return [vehicle, vehicleDistance] @@ -159,7 +126,7 @@ export default class VehicleService { ) // sort vehicles by distance to searched point - vehiclesWithDistances = vehiclesWithDistances.sort(function (v0, v1) { + vehiclesWithDistances = vehiclesWithDistances.sort(function(v0, v1) { // if this happens, we cannot sort the POI's if (v0[1] < 0 || v1[1] < 0) { // TODO: log this, maybe some other handling @@ -202,7 +169,7 @@ export default class VehicleService { // get all vehicles for track and filter by type const vehicles: Vehicle[] = await database.vehicles.getAll(track.uid) - const filteredVehicles = vehicles.filter(function (vehicle, index, vehicles) { + const filteredVehicles = vehicles.filter(function(vehicle, index, vehicles) { return vehicle.typeId == type.uid }) return filteredVehicles @@ -253,7 +220,7 @@ export default class VehicleService { // add predicted tracker positions for (let i = 0; i < trackers.length; i++) { // create list of positions of this specific tracker (also filtered for last ten minutes) - const trackerLogs = allLogs.filter(function (log) { + const trackerLogs = allLogs.filter(function(log) { return log.trackerId == trackers[i].uid }) @@ -617,118 +584,4 @@ export default class VehicleService { } return avgSpeed } - - /** - * Rename an existing vehicle - * @param vehicle `Vehicle` to rename - * @param newName new name for `vehicle` - * @returns renamed `Vehicle` if successful, `null` otherwise - */ - public static async renameVehicle(vehicle: Vehicle, newName: string): Promise { - return database.vehicles.update(vehicle.uid, undefined, undefined, newName) - } - - /** - * Update type of vehicle - * @param vehicle `Vehicle` to set new type for - * @param type new `VehicleType` of `vehicle` - * @returns updated `Vehicle` if successful, `null` otherwise - */ - public static async setVehicleType(vehicle: Vehicle, type: VehicleType): Promise { - return database.vehicles.update(vehicle.uid, type.uid) - } - - /** - * Assign a new tracker to a given vehicle (wrapper for TrackerService) - * @param vehicle `Vehicle` to assign `tracker` to - * @param tracker `Tracker` to be assigned to `vehicle` - * @returns updated `Tracker` with assigned `vehicle` if successful, `null` otherwise - */ - public static async assignTrackerToVehicle(tracker: Tracker, vehicle: Vehicle): Promise { - return TrackerService.setVehicle(tracker, vehicle) - } - - /** - * Delete existing vehicle - * @param vehicle `Vehicle` to delete - * @returns `true` if deletion was successful, `false` otherwise - */ - public static async removeVehicle(vehicle: Vehicle): Promise { - return database.vehicles.remove(vehicle.uid) - } - - // --- vehicle types --- - - /** - * Create a new vehicle type - * @param type description of new vehicle type - * @param icon name of an icon associated to type - * @param desc (optional) description for new vehicle type - * @returns created `VehicleType` if successful, `null` otherwise - */ - public static async createVehicleType(type: string, icon: string, desc?: string): Promise { - return database.vehicles.saveType(type, icon, desc) - } - - /** - * - * @returns all existing `VehicleType`s - */ - public static async getAllVehicleTypes(): Promise { - return database.vehicles.getAllTypes() - } - - /** - * Search vehicle type by a given id - * @param id id to search vehicle type for - * @returns `VehicleType` with id `id`, null if not successful - */ - public static async getVehicleTypeById(id: number): Promise { - return database.vehicles.getTypeById(id) - } - - /** - * Change name of existing vehicle type - * @param type `VehicleType` to change name of - * @param newType new name for `type` - * @returns updated `VehicleType` if successful, `null` otherwise - */ - public static async renameVehicleType(type: VehicleType, newType: string): Promise { - return database.vehicles.updateType(type.uid, newType) - } - - /** - * Change description of vehicle type - * @param type `VehicleType` to change the description of - * @param desc new description for `type` - * @returns updated `VehicleType` if successful, `null` otherwise - */ - public static async setVehicleTypeDescription(type: VehicleType, desc: string): Promise { - return database.vehicles.updateType(type.uid, undefined, undefined, desc) - } - - /** - * Change icon of vehicle type - * @param type `VehicleType` to change the icon of - * @param icon name of new icon to be associated with type - * @returns updated `VehicleType` if successful, `null` otherwise - */ - public static async setVehicleTypeIcon(type: VehicleType, icon: string): Promise { - return database.vehicles.updateType(type.uid, undefined, icon) - } - - /** - * Delete existing vehicle type - * @param type `VehicleType` to delete - * @returns `true` if deletion was successful, `false` otherwise - */ - public static async removeVehicleType(type: VehicleType): Promise { - return database.vehicles.remove(type.uid) - } - - static async getAllVehicles() { - const vehicles: Vehicle[] = await database.vehicles.getAll() - - return vehicles - } } From 6abdc3fc78a80d5b4b1447197e0ec1227dd377ea Mon Sep 17 00:00:00 2001 From: Liam Boddin Date: Sun, 3 Sep 2023 10:21:38 +0200 Subject: [PATCH 5/5] Cleaned eslint warnings --- Server/src/services/poi.service.ts | 130 +------------------------ Server/src/services/vehicle.service.ts | 8 +- 2 files changed, 5 insertions(+), 133 deletions(-) diff --git a/Server/src/services/poi.service.ts b/Server/src/services/poi.service.ts index baf331e7..d0fb256f 100644 --- a/Server/src/services/poi.service.ts +++ b/Server/src/services/poi.service.ts @@ -1,7 +1,6 @@ -import { POI, POIType, Track, Vehicle } from ".prisma/client" +import { POI, POIType, Track } from ".prisma/client" import database from "./database.service" import TrackService from "./track.service" -import VehicleService from "./vehicle.service" import GeoJSONUtils from "../utils/geojsonUtils" import { logger } from "../utils/logger" @@ -175,131 +174,4 @@ export default class POIService { }) return trackPOIs } - -// THIS METHOD IS NOT USED ANYMORE BUT HAS SOME LOGICS IN IT - /** - * Search for nearby POI's either within a certain distance or by amount - * @param point point to search nearby POI's from - * @param track `Track` to search on for POIs. If none is given, the closest will be used. - * @param count amount of points, that should be returned. If none given only one (i.e. the nearest) will be returned. - * @param heading could be either 1 or -1 to search for POI only towards the end and start of the track (seen from `point`) respectively - * @param maxDistance maximum distance in track-kilometers to the POI's - * @param type `POIType` to filter the returned POI's by - * @returns `POI[]`, either #`count` of nearest POI's or all POI's within `maxDistance` of track-kilometers, but at most #`count`. - * That is the array could be empty. - */ - public static async getNearbyPOIs( - point: GeoJSON.Feature | Vehicle, - track?: Track, - count?: number, - heading?: number, - maxDistance?: number, - type?: POIType - ): Promise { - // TODO: testing - // TODO: just copied from VehicleService, i.e. there is probably a better solution - // extract vehicle position if a vehicle is given instead of a point - if ((point).uid) { - // also use the assigned track if none is given - if (track == null) { - const tempTrack = await database.tracks.getById((point).trackId) - if (tempTrack == null) { - return null - } - track = tempTrack - } - - const vehiclePosition = await VehicleService.getVehiclePosition(point) - if (vehiclePosition == null) { - return null - } - point = vehiclePosition - } - - // now we can safely assume, that this is actually a point - const searchPoint = >point - // check if a track is given, else initialize it with the closest one - if (track == null) { - const tempTrack = await TrackService.getClosestTrack(searchPoint) - if (tempTrack == null) { - // TODO: log this - return null - } - track = tempTrack - } - - // compute distance of point mapped on track - const trackDistance = await TrackService.getPointTrackKm(searchPoint, track) - if (trackDistance == null) { - // TODO: log this - return null - } - - // search for all POIs on the track - let allPOIsForTrack = await this.getAllPOIsForTrack(track, type) - - // filter pois by heading if given - if (heading != null) { - // invalid heading - if (heading != 1 && heading != -1) { - // TODO: log this - return null - } - - allPOIsForTrack.filter(async function(poi, _index, _pois) { - const poiTrackKm = await POIService.getPOITrackDistanceKm(poi) - if (poiTrackKm == null) { - return false - } - return poiTrackKm - trackDistance * heading > 0 - }) - } - - // filter pois by distance if given - if (maxDistance != null) { - allPOIsForTrack.filter(async function(poi, _index, _pois) { - const poiTrackKm = await POIService.getPOITrackDistanceKm(poi) - if (poiTrackKm == null) { - return false - } - // consider both directions (heading would filter those out) - return Math.abs(poiTrackKm - trackDistance) < maxDistance - }) - } - // sort POI's by distance to searched point - allPOIsForTrack = allPOIsForTrack.sort(function(poi0, poi1) { - // parse POI position - const POIPos0 = GeoJSONUtils.parseGeoJSONFeaturePoint(poi0.position) - const POIPos1 = GeoJSONUtils.parseGeoJSONFeaturePoint(poi1.position) - if (POIPos0 == null || POIPos1 == null) { - // TODO: log this - return 0 - } - - // if this happens, we cannot sort the POI's - const POIPos0TrackKm = GeoJSONUtils.getTrackKm(POIPos0) - const POIPos1TrackKm = GeoJSONUtils.getTrackKm(POIPos1) - if (POIPos0TrackKm == null || POIPos1TrackKm == null) { - // TODO: log this, maybe some other handling - return 0 - } - - // compute distances to vehicle and compare - const distanceToVehicle0 = Math.abs(POIPos0TrackKm - trackDistance) - const distanceToVehicle1 = Math.abs(POIPos1TrackKm - trackDistance) - return distanceToVehicle0 - distanceToVehicle1 - }) - - // check if a certain amount is searched for - count = count == null ? 1 : count - - // if less POI's were found then we need to return, we return every POI that we have - if (count > allPOIsForTrack.length) { - return allPOIsForTrack - } - - // only return first #count of POI's - allPOIsForTrack.slice(0, count) - return allPOIsForTrack - } } diff --git a/Server/src/services/vehicle.service.ts b/Server/src/services/vehicle.service.ts index 853af1ba..5ee8da8b 100644 --- a/Server/src/services/vehicle.service.ts +++ b/Server/src/services/vehicle.service.ts @@ -31,6 +31,7 @@ export default class VehicleService { * @param point point to search nearby vehicles from, this could also be a vehicle * * @param track `Track` to search on for vehicles. If none is given and `point` is not a `Vehicle`, the closest will be used. * If none is given and `point` is a `Vehicle`, the assigned track will be used. + * @param track The track the vehicles should be on. * @param count amount of vehicles, that should be returned. If none given only one (i.e. the nearest) will be returned. * @param heading could be either 1 or -1 to search for vehicles only towards the end and start of the track (seen from `point`) respectively * @param maxDistance maximum distance in track-kilometers to the vehicles @@ -95,7 +96,7 @@ export default class VehicleService { return null } - allVehiclesOnTrack.filter(async function(vehicle, index, vehicles) { + allVehiclesOnTrack.filter(async function(vehicle) { const vehicleTrackKm = await VehicleService.getVehicleTrackDistanceKm(vehicle) if (vehicleTrackKm == null) { // TODO: log this @@ -107,7 +108,7 @@ export default class VehicleService { // filter vehicles by distance if given if (maxDistance != null) { - allVehiclesOnTrack.filter(async function(vehicle, index, vehicles) { + allVehiclesOnTrack.filter(async function(vehicle) { const vehicleTrackKm = await VehicleService.getVehicleTrackDistanceKm(vehicle) if (vehicleTrackKm == null) { return false @@ -169,10 +170,9 @@ export default class VehicleService { // get all vehicles for track and filter by type const vehicles: Vehicle[] = await database.vehicles.getAll(track.uid) - const filteredVehicles = vehicles.filter(function(vehicle, index, vehicles) { + return vehicles.filter(function(vehicle) { return vehicle.typeId == type.uid }) - return filteredVehicles } /**