Skip to content

Commit

Permalink
feat: add car endpoint for uploading car position to sondehub
Browse files Browse the repository at this point in the history
  • Loading branch information
raska-vilem committed Jan 25, 2025
1 parent 9048dd7 commit 1b07a10
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 134 deletions.
49 changes: 49 additions & 0 deletions GAPP/apps/gapp-server/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import sensible from '@fastify/sensible';
import influxDbPlugin from './plugins/influxdb';
import swagger from '@fastify/swagger';
import swaggerUi from '@fastify/swagger-ui';
import { carsController } from './controllers/cars.controller';
import sondehubPlugin from './plugins/sondehub';

interface AppOptions extends FastifyPluginOptions {
influxDbToken: string;
influxDbHost: string;
influxDbOrg: string;
}

export const app = async (fastify: FastifyInstance, opts: AppOptions) => {
// LIBRARIES
fastify.register(sensible);

// PLUGINS
await fastify.register(influxDbPlugin, {
host: opts.influxDbHost,
token: opts.influxDbToken,
org: opts.influxDbOrg,
});
await fastify.register(sondehubPlugin, { dev: true });

await fastify.register(swagger);
await fastify.register(swaggerUi, {
routePrefix: '/docs',
});

// ROUTES
fastify.register(carsController, { prefix: '/cars' });

fastify.get(
'/ping',
{
schema: {
description: 'Ping route',
response: {
200: {
type: 'string',
},
},
},
},
() => `pong\n\n${new Date().toString()}`
);
};
20 changes: 0 additions & 20 deletions GAPP/apps/gapp-server/src/app/app.spec.ts

This file was deleted.

20 changes: 0 additions & 20 deletions GAPP/apps/gapp-server/src/app/app.ts

This file was deleted.

12 changes: 0 additions & 12 deletions GAPP/apps/gapp-server/src/app/plugins/influxdb.ts

This file was deleted.

12 changes: 0 additions & 12 deletions GAPP/apps/gapp-server/src/app/plugins/sensible.ts

This file was deleted.

7 changes: 0 additions & 7 deletions GAPP/apps/gapp-server/src/app/routes/root.ts

This file was deleted.

1 change: 1 addition & 0 deletions GAPP/apps/gapp-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const envSchema = {
desc: 'Influx db host with port',
devDefault: 'http://localhost:8086',
}),
INFLUXDB_ORG: str({ desc: 'Influx db organization', default: 'fik' }),
};

export const getConfig = (configObject: object) => {
Expand Down
27 changes: 27 additions & 0 deletions GAPP/apps/gapp-server/src/controllers/cars.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { B_CarStatus, Q_Callsign } from '../schemas';
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';

export const carsController: FastifyPluginAsyncTypebox = async (fastify) => {
fastify.post(
'/status',
{
schema: {
summary: 'Uploads car position to sondehub amateur.',
querystring: Q_Callsign,
body: B_CarStatus,
},
},
async (req, rep) => {
const { callsign } = req.query;
const { latitude, longitude, altitude } = req.body;

await req.server.sondehub.uploadStationPosition({
uploader_callsign: callsign,
uploader_position: [latitude, longitude, altitude],
mobile: true,
});

rep.code(201).send();
}
);
};
9 changes: 4 additions & 5 deletions GAPP/apps/gapp-server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import Fastify from 'fastify';
import { app } from './app/app';
import { app } from './app';
import { getConfig } from './config';
import pino from 'pino';
import { Uploader } from '@gapp/sondehub';

console.log(Uploader);
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';

const config = getConfig(process.env);
const logger = pino();
const server = Fastify({ loggerInstance: logger });
const server = Fastify({ loggerInstance: logger, disableRequestLogging: true }).withTypeProvider<TypeBoxTypeProvider>();

server.register(app, {
influxDbToken: config.INFLUXDB_TOKEN,
influxDbHost: config.INFLUXDB_HOST,
influxDbOrg: config.INFLUXDB_ORG,
});

server.listen({ port: config.PORT, host: '0.0.0.0' }, (err) => {
Expand Down
39 changes: 39 additions & 0 deletions GAPP/apps/gapp-server/src/plugins/influxdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FastifyPluginAsync, FastifyPluginOptions } from 'fastify';
import fp from 'fastify-plugin';
import { InfluxDB, QueryApi, WriteApi } from '@influxdata/influxdb-client';
import { Plugins } from './plugins';

interface InfluxdbPluginOptions extends FastifyPluginOptions {
host: string;
token: string;
org: string;
}

declare module 'fastify' {
interface FastifyInstance {
influxWriteApi: WriteApi;
influxQueryApi: QueryApi;
}
}

const influxDbPlugin: FastifyPluginAsync<InfluxdbPluginOptions> = async (fastify, options) => {
const influxClient = new InfluxDB({
token: options.token,
url: options.host,
});

const writeApi = influxClient.getWriteApi(options.org, 'fik');
const queryApi = influxClient.getQueryApi(options.org);

fastify.decorate('influxWriteApi', writeApi);
fastify.decorate('influxQueryApi', queryApi);
fastify.addHook('onClose', async () => {
fastify.log.info('Closing influxdb write api...');
await writeApi.close();
fastify.log.info('Influxdb write api closed');
});
};

export default fp(influxDbPlugin, {
name: Plugins.INFLUXDB,
});
4 changes: 4 additions & 0 deletions GAPP/apps/gapp-server/src/plugins/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Plugins {
INFLUXDB = 'influxdb',
SONDEHUB = 'sondehub',
}
35 changes: 35 additions & 0 deletions GAPP/apps/gapp-server/src/plugins/sondehub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { FastifyPluginAsync, FastifyPluginOptions } from 'fastify';
import fp from 'fastify-plugin';
import { Plugins } from './plugins';
import { Uploader } from '@gapp/sondehub';

interface SondehubPluginOptions extends FastifyPluginOptions {
dev: boolean;
}

declare module 'fastify' {
interface FastifyInstance {
sondehub: Uploader;
}
}

const sondehubPlugin: FastifyPluginAsync<SondehubPluginOptions> = async (fastify, options) => {
const uploader = new Uploader({
uploader_callsign: 'gapp-default',
dev: options.dev,
software_name: 'gapp-server',
software_version: '0.0.1',
});

fastify.decorate('sondehub', uploader);

fastify.addHook('onClose', async () => {
fastify.log.info('Deinitializing sondehub uploader...');
await uploader.deinit();
fastify.log.info('Sondehub uploader deinitialized');
});
};

export default fp(sondehubPlugin, {
name: Plugins.SONDEHUB,
});
13 changes: 13 additions & 0 deletions GAPP/apps/gapp-server/src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Type as T } from '@sinclair/typebox';

export const Q_Callsign = T.Object({
callsign: T.String(),
});

export const B_CarStatus = T.Object({
car_id: T.Optional(T.String()),
car_heartbeat_value: T.Optional(T.String({ format: 'date-time' })),
latitude: T.Number(),
longitude: T.Number(),
altitude: T.Number(),
});
Loading

0 comments on commit 1b07a10

Please sign in to comment.