Skip to content

Commit

Permalink
Merge pull request #172 from kieler/160-add-timestamp
Browse files Browse the repository at this point in the history
160 add timestamp
  • Loading branch information
JulianGrabitzky authored Sep 19, 2023
2 parents 6283ab8 + ce19429 commit 488a208
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Server/src/models/api.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const UpdateRequestApp = z.object({
vehicleId: z.number().int().nonnegative(), // vehicle id of user
pos: Position.optional(), // the current position of user
speed: z.number().optional(), // Speed in km/h
heading: z.number().optional() // Heading of the vehicle between 0 and 359
heading: z.number().optional(), // Heading of the vehicle between 0 and 359
timestamp: z.number().optional() // Timestamp of GPS measurement in milliseconds since epoch
})

//================ new
Expand Down
6 changes: 4 additions & 2 deletions Server/src/routes/vehicle.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ export class VehicleRoute {
if (
vehiclePayload.pos !== undefined &&
vehiclePayload.heading !== undefined &&
vehiclePayload.speed !== undefined
vehiclePayload.speed !== undefined &&
vehiclePayload.timestamp !== undefined
) {
await VehicleService.appendLog(userVehicle.uid, vehiclePayload.pos, vehiclePayload.heading, vehiclePayload.speed)
await VehicleService.appendLog(userVehicle.uid, vehiclePayload.pos, vehiclePayload.heading, vehiclePayload.speed,
new Date(vehiclePayload.timestamp))
}

const userVehicleData = await VehicleService.getVehicleData(userVehicle)
Expand Down
46 changes: 24 additions & 22 deletions Server/src/services/vehicle.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,51 @@ export type VehicleData = {
/** Service for vehicle management. */
export default class VehicleService {
/**
* Search for vehicles on a track
* @param track `Track` to search on for vehicles
* @param type `VehicleType` to filter the returned vehicles by
* @returns `Vehicle[]` of all vehicles on the given `track`
*/
public static async getAllVehiclesForTrack(track: Track, type?: VehicleType): Promise<Vehicle[]> {
// if no type is given, this is a simple forward
if (type == null) {
return database.vehicles.getAll(track.uid)
}

// 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 vehicle.typeId == type.uid
})
return filteredVehicles
}

/**
* Append log for a given vehicle
* @param vehicleId vehicle id to append the log for
* @param position position of the vehicle
* @param heading heading of the vehicle
* @param speed speed of the vehicle
* @param timestamp timestamp of the gps position
* @returns appended log if successful
* @throws PrismaError, if appending log in the database was not possible
*/
public static async appendLog(
vehicleId: number,
position: z.infer<typeof Position>,
heading: number,
speed: number
speed: number,
timestamp: Date
): Promise<Log> {
return await database.logs.save({
timestamp: new Date(),
timestamp,
vehicleId,
position: [position.lng, position.lat],
heading,
speed
})
}

/**
* Search for vehicles on a track
* @param track `Track` to search on for vehicles
* @param type `VehicleType` to filter the returned vehicles by
* @returns `Vehicle[]` of all vehicles on the given `track`
*/
public static async getAllVehiclesForTrack(track: Track, type?: VehicleType): Promise<Vehicle[]> {
// if no type is given, this is a simple forward
if (type == null) {
return database.vehicles.getAll(track.uid)
}

// 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 vehicle.typeId == type.uid
})
return filteredVehicles
}

/**
* Compute all data (e.g. position, speed and heading) for a given vehicle
* @param vehicle `Vehicle` to compute data for
Expand Down

0 comments on commit 488a208

Please sign in to comment.