Skip to content

Commit

Permalink
Remove unnecessary methods from vehicle- and tracker service
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Boddin committed Aug 31, 2023
1 parent dd8ff11 commit fd6c5d3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 278 deletions.
11 changes: 5 additions & 6 deletions Server/src/routes/track.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Server/src/routes/tracker.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand All @@ -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.`)
Expand Down
6 changes: 3 additions & 3 deletions Server/src/routes/vehicle.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
private async getAllVehicles(_req: Request, res: Response): Promise<void> {
const vehicles = await database.vehicles.getAll()
const apiVehicles: APIVehicle[] = await Promise.all(
vehicles.map(async vehicle => {
Expand Down Expand Up @@ -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)
Expand Down
113 changes: 1 addition & 112 deletions Server/src/services/tracker.service.ts
Original file line number Diff line number Diff line change
@@ -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<Tracker | null> {
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<Tracker | null> {
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<Tracker[]> {
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<Tracker | null> {
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<boolean> {
return database.trackers.remove(tracker.uid)
}

// --- Vehicle logs ---

/**
Expand Down Expand Up @@ -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<Log | null> {
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<Log[] | null> {
return database.logs.getAll(vehicle.uid, tracker?.uid)
}
}
Loading

0 comments on commit fd6c5d3

Please sign in to comment.