Skip to content

Commit

Permalink
feat: reading chase cars status
Browse files Browse the repository at this point in the history
  • Loading branch information
VilemRaska committed Jan 27, 2025
1 parent 8d91e7e commit e260c46
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
16 changes: 14 additions & 2 deletions GAPP/apps/gapp-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ export const app = async (fastify: FastifyInstance, opts: AppOptions) => {
await fastify.register(carsServicePlugin);
await fastify.register(telemetryServicePlugin);

await fastify.register(swagger);
await fastify.register(swaggerUi, { routePrefix: '/docs' });
await fastify.register(swagger, {
openapi: {
info: {
title: 'GAPP API',
version: '0.0.1',
description: 'API Docs for ground app',
},
tags: [
{ name: 'cars', description: 'Chase cars API' },
{ name: 'sondes', description: 'Sondes telemetry APi' },
],
},
});
await fastify.register(swaggerUi, { routePrefix: '/docs', uiConfig: {} });

// ROUTES
fastify.register(carsController, { prefix: '/cars' });
Expand Down
12 changes: 9 additions & 3 deletions GAPP/apps/gapp-server/src/controllers/cars.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { B_CarStatus, Q_Callsign } from '../schemas';
import { B_CarStatus, Q_Callsign, Q_OptionalCallsign, R_CarsStatus } from '../schemas';
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';

export const carsController: FastifyPluginAsyncTypebox = async (fastify) => {
Expand All @@ -9,11 +9,17 @@ export const carsController: FastifyPluginAsyncTypebox = async (fastify) => {
summary: 'Get cars positions',
description: 'Get latest location for all registered cars.',
tags: ['cars'],
querystring: Q_OptionalCallsign,
response: {
200: R_CarsStatus,
},
},
},
async (req, rep) => {
req.server.carsService.getCarsStatus(['cesilko-test']);
rep.status(200).send();
const callsigns = req.query.callsign?.split(',') || [];

const carsStatus = await req.server.carsService.getCarsStatus(callsigns);
rep.status(200).send(carsStatus);
}
);

Expand Down
19 changes: 19 additions & 0 deletions GAPP/apps/gapp-server/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export const Q_Callsign = T.Object({
callsign: T.String(),
});

export const Q_OptionalCallsign = T.Object(
{
callsign: T.Optional(T.String()),
},
{ nullable: true }
);

export const B_CarStatus = T.Object({
car_id: T.Optional(T.String()),
car_heartbeat_value: T.Optional(T.String({ format: 'date-time' })),
Expand Down Expand Up @@ -78,3 +85,15 @@ export const B_SondeTtnTelemetry = T.Object({
}),
}),
});

export const R_CarsStatus = T.Array(
T.Object({
_time: T.String(),
altitude: T.Number(),
longitude: T.Number(),
latitude: T.Number(),
callsign: T.String(),
})
);

export type CarsStatus = Static<typeof R_CarsStatus>;
11 changes: 4 additions & 7 deletions GAPP/apps/gapp-server/src/services/cars.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InfluxDB, Point, QueryApi, WriteApi } from '@influxdata/influxdb-client';
import { InfluxDbServiceBase } from '../utils/influxdb-service-base';
import { Organization } from '@influxdata/influxdb-client-apis';
import { CarStatus } from '../schemas';
import { CarsStatus, CarStatus } from '../schemas';
import { arrayAsString } from '../utils/array-as-atring';

export class CarsService extends InfluxDbServiceBase {
Expand Down Expand Up @@ -33,17 +33,14 @@ export class CarsService extends InfluxDbServiceBase {
this.writeAPi.writePoint(point);
}

public async getCarsStatus(callsigns: string[]) {
console.log(callsigns);
public async getCarsStatus(callsigns: string[]): Promise<CarsStatus> {
const query = `from(bucket: "cars")
|> range(start: -24h)
|> filter(fn: (r) => contains(value: r.callsign, set: ${arrayAsString(callsigns)}))
${callsigns.length ? `|> filter(fn: (r) => contains(value: r.callsign, set: ${arrayAsString(callsigns)}))` : undefined}
|> last()
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> keep(columns: ["_time", "altitude", "longitude", "latitude", "callsign"])`;

const data = await this.queryAPi.collectRows(query);

console.log(data);
return await this.queryAPi.collectRows(query);
}
}

0 comments on commit e260c46

Please sign in to comment.