-
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 TTN webhook endpoint and make it work
- Loading branch information
1 parent
1b07a10
commit 40ced4b
Showing
9 changed files
with
449 additions
and
16 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
23 changes: 23 additions & 0 deletions
23
GAPP/apps/gapp-server/src/controllers/sondes.controller.ts
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,23 @@ | ||
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'; | ||
import { B_SondeTelemetry } from '../schemas'; | ||
import { ttnPacketDto } from '../utils/ttn-packet-dto'; | ||
|
||
export const sondesController: FastifyPluginAsyncTypebox = async (fastify) => { | ||
fastify.post( | ||
'/telemetry', | ||
{ | ||
schema: { | ||
summary: 'TTN webhook', | ||
description: 'Endpoint for receiving telemetry data from TTN', | ||
body: B_SondeTelemetry, | ||
}, | ||
}, | ||
async (req, rep) => { | ||
const telemetryPacket = ttnPacketDto(req.body); | ||
|
||
req.server.sondehub.addTelemetry(telemetryPacket); | ||
|
||
rep.code(200).send('OK'); | ||
} | ||
); | ||
}; |
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 { FastifyPluginCallback, FastifyPluginOptions } from 'fastify'; | ||
import mqtt from 'mqtt'; | ||
import fp from 'fastify-plugin'; | ||
import { Plugins } from './plugins'; | ||
|
||
interface MqttPluginOptions extends FastifyPluginOptions { | ||
clientId: string; | ||
host: string; | ||
username: string; | ||
password: string; | ||
} | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
mqtt: mqtt.MqttClient; | ||
} | ||
} | ||
|
||
const mqttPlugin: FastifyPluginCallback<MqttPluginOptions> = (fastify, options, done) => { | ||
const client = mqtt.connect(options.host, { | ||
clientId: options.clientId, | ||
username: options.username, | ||
password: options.password, | ||
}); | ||
|
||
fastify.decorate('mqttClient', client); | ||
fastify.addHook( | ||
'onClose', | ||
async () => | ||
new Promise<void>((resolve) => { | ||
fastify.log.info('Closing MQTT client...'); | ||
client.end(() => { | ||
fastify.log.info('MQTT client closed'); | ||
resolve(); | ||
}); | ||
}) | ||
); | ||
|
||
client.on('error', (error: Error) => { | ||
fastify.log.error(error); | ||
}); | ||
|
||
client.on('connect', () => { | ||
fastify.log.info('Connected to MQTT broker'); | ||
done(); | ||
}); | ||
}; | ||
|
||
export default fp(mqttPlugin, { name: Plugins.MQTT }); |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum Plugins { | ||
INFLUXDB = 'influxdb', | ||
SONDEHUB = 'sondehub', | ||
MQTT = 'mqtt', | ||
} |
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,17 @@ | ||
import { Static } from '@sinclair/typebox'; | ||
import { B_SondeTelemetry } from '../schemas'; | ||
import { TelemetryPacket } from '@gapp/sondehub'; | ||
|
||
export const ttnPacketDto = (ttnPayload: Static<typeof B_SondeTelemetry>): TelemetryPacket => { | ||
return { | ||
time_received: ttnPayload.uplink_message.received_at, | ||
payload_callsign: ttnPayload.end_device_ids.device_id, | ||
datetime: new Date().toISOString(), | ||
lat: ttnPayload.uplink_message.decoded_payload.lat, | ||
lon: ttnPayload.uplink_message.decoded_payload.lon, | ||
alt: ttnPayload.uplink_message.decoded_payload.alt_m, | ||
heading: ttnPayload.uplink_message.decoded_payload.course, | ||
modulation: 'LoRa', | ||
uploader_callsign: 'GAPP-Server', | ||
}; | ||
}; |
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
Oops, something went wrong.