-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add car endpoint for uploading car position to sondehub
- Loading branch information
1 parent
9048dd7
commit 1b07a10
Showing
15 changed files
with
450 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}` | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Plugins { | ||
INFLUXDB = 'influxdb', | ||
SONDEHUB = 'sondehub', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}); |
Oops, something went wrong.