Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
derGraph committed Sep 1, 2024
1 parent a7c8fe1 commit 7d7c2f8
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 114 deletions.
6 changes: 3 additions & 3 deletions src/lib/Leaflet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
switch (propulsion) {
case 0:
// anchoring
return "#4682B4";
return '#4682B4';
case 1:
// motoring
return '#FF6600';
Expand Down Expand Up @@ -128,8 +128,8 @@
function onLineChange(lines2D: L.Polyline[][]) {
if (lines2D.length != 0) {
var maxBounds = lines2D[0][0].getBounds();
lines2D.forEach(trackLines => {
trackLines.forEach(line => {
lines2D.forEach((trackLines) => {
trackLines.forEach((line) => {
line.remove();
line.addTo(map!);
maxBounds.extend(line.getBounds());
Expand Down
4 changes: 2 additions & 2 deletions src/lib/errorStore.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { writable } from "svelte/store";
import { writable } from 'svelte/store';

export default writable(new Response());
export default writable(new Response());
111 changes: 55 additions & 56 deletions src/lib/server/simplifyGps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,61 @@ import { prisma } from '$lib/server/prisma';
import type { Decimal } from '@prisma/client/runtime/library';
import { getDistanceFromLine } from 'geolib';

export async function simplifyGps(trip: string, amount: number){
let inputData = await prisma.datapoint.findMany({
where: {
tripId: trip,
optimized: 0,
},
take: amount
});

let lastPoint:Datapoint = inputData[0];
for(let i = 1; i < inputData.length-1; i++){
let crosstrackError = getDistanceFromLine( {lat: Number(inputData[i].lat), lng: Number(inputData[i].long)},
{lat: Number(lastPoint.lat), lng: Number(lastPoint.long)},
{lat: Number(inputData[i+1].lat), lng: Number(inputData[i+1].long)});

let turnRate = 0;
if(inputData[i].heading != null, inputData[i-1].heading != null){
turnRate = Number(inputData[i-1].heading) - Number(inputData[i].heading);
}



if(crosstrackError < 10 && Math.abs(turnRate) < 30){
// Delete Datapoint
await prisma.datapoint.update({
where: {id: inputData[i].id},
data: {
optimized: 1
}
});
}else{
// Change Datapoint to optimized
lastPoint = inputData[i];
await prisma.datapoint.update({
where: {id: inputData[i].id},
data: {
optimized: 2
}
});
}
}
export async function simplifyGps(trip: string, amount: number) {
let inputData = await prisma.datapoint.findMany({
where: {
tripId: trip,
optimized: 0
},
take: amount
});

let lastPoint: Datapoint = inputData[0];

for (let i = 1; i < inputData.length - 1; i++) {
let crosstrackError = getDistanceFromLine(
{ lat: Number(inputData[i].lat), lng: Number(inputData[i].long) },
{ lat: Number(lastPoint.lat), lng: Number(lastPoint.long) },
{ lat: Number(inputData[i + 1].lat), lng: Number(inputData[i + 1].long) }
);

let turnRate = 0;
if ((inputData[i].heading != null, inputData[i - 1].heading != null)) {
turnRate = Number(inputData[i - 1].heading) - Number(inputData[i].heading);
}

if (crosstrackError < 10 && Math.abs(turnRate) < 30) {
// Delete Datapoint
await prisma.datapoint.update({
where: { id: inputData[i].id },
data: {
optimized: 1
}
});
} else {
// Change Datapoint to optimized
lastPoint = inputData[i];
await prisma.datapoint.update({
where: { id: inputData[i].id },
data: {
optimized: 2
}
});
}
}
}


interface Datapoint{
id: string;
tripId: string;
time: Date;
lat: Decimal;
long: Decimal;
speed: Decimal | null;
heading: Decimal | null;
depth: Decimal | null;
h_accuracy: Decimal | null;
v_accuracy: Decimal | null;
propulsion: number | null;
optimized: number;
interface Datapoint {
id: string;
tripId: string;
time: Date;
lat: Decimal;
long: Decimal;
speed: Decimal | null;
heading: Decimal | null;
depth: Decimal | null;
h_accuracy: Decimal | null;
v_accuracy: Decimal | null;
propulsion: number | null;
optimized: number;
}
8 changes: 4 additions & 4 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import errorStore from '$lib/errorStore.js';
export let data;
let tracks: String[]|null = null;
let tracks: String[] | null = null;
const initialView: LatLngExpression = [43.95, 14.79];
Expand All @@ -24,13 +24,13 @@
propulsion?: number;
}
onMount(()=>{
if(data.user){
onMount(() => {
if (data.user) {
tracks = [data.user?.activeTripId];
}
});
</script>

<div class="md:container md:mx-auto py-3 h-full rounded">
<Leaflet view={initialView} zoom={8} tracks={tracks}/>
<Leaflet view={initialView} zoom={8} {tracks} />
</div>
73 changes: 41 additions & 32 deletions src/routes/api/Datapoints/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from '$lib/server/prisma';
import { error, json } from '@sveltejs/kit';
import '$lib/server/simplifyGps.js';
import '$lib/server/simplifyGps.js';
import { simplifyGps } from '$lib/server/simplifyGps.js';

interface Datapoint {
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function POST(event: {
error(400, result.name + ': ' + result.message);
}

if(Object.keys(input).length > 500){
if (Object.keys(input).length > 500) {
error(413, 'Payload to large: Maximum of 500 Datapoints per request are allowed!');
}

Expand Down Expand Up @@ -58,13 +58,13 @@ export async function POST(event: {
for (const k of Object.keys(input)) {
try {
input[k].id = k;
if(k == ""){
if (k == '') {
input[k].id = undefined;
}
let datapoint = checkDatapoint(input[k]);
datapoint.tripId = activeUser.activeTripId;
let existingPoint = null;
if(datapoint.id){
if (datapoint.id) {
existingPoint = await prisma.datapoint.findFirst({
where: { id: datapoint.id }
});
Expand Down Expand Up @@ -277,7 +277,11 @@ function compareObjects(
}

export async function GET(event) {
locals: { user: { username: String } };
locals: {
user: {
username: String;
}
}

let requestedTrip = event.url.searchParams.get('tripId');
let unparsed_start = event.url.searchParams.get('start');
Expand All @@ -289,16 +293,16 @@ export async function GET(event) {
error(400, {
message: 'No tripId requested!'
});
}else{
} else {
const cuidRegex = /^c[a-z0-9]{24}$/;
if (requestedTrip != null) {
if (!cuidRegex.test(requestedTrip)) {
error(400, 'Invalid Id!');
}
}
}
}

try{
try {
if (unparsed_start != null) {
start = new Date(parseInt(unparsed_start));
} else {
Expand All @@ -308,7 +312,7 @@ export async function GET(event) {
error(400, { message: 'Invalid start Timestamp!' });
}

try{
try {
if (unparsed_end != null) {
end = new Date(parseInt(unparsed_end));
} else {
Expand All @@ -317,30 +321,32 @@ export async function GET(event) {
} catch (error_message) {
error(400, { message: 'Invalid end Timestamp!' });
}

try {
if(event.locals.user?.username){
if (event.locals.user?.username) {
let tripData = await prisma.trip.findFirstOrThrow({
where: {
OR: [{
id: requestedTrip,
visibility: 1
},
{
id: requestedTrip,
visibility: 2
},
{
id: requestedTrip,
crew: {
some: {
username: event.locals.user?.username
OR: [
{
id: requestedTrip,
visibility: 1
},
{
id: requestedTrip,
visibility: 2
},
{
id: requestedTrip,
crew: {
some: {
username: event.locals.user?.username
}
}
}
}]
]
}
});
}else{
} else {
let tripData = await prisma.trip.findFirstOrThrow({
where: {
id: requestedTrip,
Expand All @@ -353,13 +359,16 @@ export async function GET(event) {
tripId: requestedTrip,
time: {
lte: end,
gte: start,
gte: start
},
OR: [{
optimized: 0
},{
optimized: 2
}]
OR: [
{
optimized: 0
},
{
optimized: 2
}
]
}
});
let responseData: { [k: string]: any } = {};
Expand Down
17 changes: 10 additions & 7 deletions src/routes/sign_in/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import errorStore from '$lib/errorStore.js';
export let form;
$: if(form?.error){
$errorStore = new Response(JSON.stringify({
message: form.error
}), {
statusText: "Bad request!",
status: 400
});
$: if (form?.error) {
$errorStore = new Response(
JSON.stringify({
message: form.error
}),
{
statusText: 'Bad request!',
status: 400
}
);
}
</script>

Expand Down
11 changes: 8 additions & 3 deletions src/routes/sign_up/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,22 @@ export const actions: Actions = {
},
activeTrip: {
create: {
name: 'newTrip',
name: 'newTrip'
}
}
}
});
await prisma.user.update({
where: {
username: username,
username: username
},
data: {
crewedTrips: {connect: {id: (await prisma.user.findFirstOrThrow({where: {username: username}})).activeTripId}}
crewedTrips: {
connect: {
id: (await prisma.user.findFirstOrThrow({ where: { username: username } }))
.activeTripId
}
}
}
});
} catch (error) {
Expand Down
17 changes: 10 additions & 7 deletions src/routes/sign_up/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
export let form;
$: if(form?.error){
$errorStore = new Response(JSON.stringify({
message: form.error
}), {
statusText: "Bad request!",
status: 400
});
$: if (form?.error) {
$errorStore = new Response(
JSON.stringify({
message: form.error
}),
{
statusText: 'Bad request!',
status: 400
}
);
}
</script>

Expand Down

0 comments on commit 7d7c2f8

Please sign in to comment.