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 - } }