Backend Routes: Webhook - OtagoPolytechnic/Air-Quality-Monitoring-System GitHub Wiki
About
This webhook is designed to handle and process data sent from CO2 devices.
When data is uploaded from a device to The Things Network, the webhook controller parses the incoming data. It then performs the following steps:
- Device Lookup:
The webhook checks if the device already exists in the database. - Device Handling:
If the device does not exist, it is created in the database.
If the device does exist, the webhook updates the device's model with the latest sensor data.
TTN login details are available on gitlabs
Webhook Route
The webhook operates at the endpoint root/api/v1/integrations/webhook.
If the route for the webhook is modified in app.js, it is crucial to update the corresponding endpoint in The Things Network Webhook to match the new route.
Test example
describe('POST webhook', () => {
afterAll(() => {
server.close();
})
it('should respond with 400 Bad request when payload is empty', async () => {
const response = await request(app)
.post(webhookPath)
.send({});
expect(response.status).toBe(400);
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe("Empty payload received");
});
Payload Example
end_device_ids: {
device_id: 'eui-XXXXXXX',
application_ids: { application_id: 'co2-test' },
dev_eui: 'XXXXXXX',
dev_addr: 'XXXXXXX'
},
correlation_ids: [ 'gs:uplink:01HRX4B0G0SP6N9T9K1DZT3R8V' ],
received_at: '2024-03-14T00:03:37.808080299Z',
uplink_message: {
f_port: 1,
f_cnt: 91,
frm_payload: 'MTI0NToyMwA=',
decoded_payload: { receivedString: '1245:23\x00' },
rx_metadata: [ [Object] ],
settings: {
data_rate: [Object],
frequency: '917200000',
timestamp: 1033421052,
time: '2024-03-13T23:15:04.922843Z'
},
received_at: '2024-03-14T00:03:37.601844323Z',
consumed_airtime: '0.102912s',
network_ids: {
net_id: 'XXXX',
ns_id: 'XXXXXXX',
tenant_id: 'ttn',
cluster_id: 'au1',
cluster_address: 'TTN-NETWORK-ADDRESS'
}
}
The data decoded_payload: { receivedString: '1245:23\x00' },
is where the co2 level is stored, receivedString: 'co2-level:temperature'