Livebeat is now able to send, store and show beats
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
import * as amqp from 'amqplib';
|
||||
import { logger, RABBITMQ_URI } from '../app';
|
||||
import { Beat } from '../models/beat/beat.model.';
|
||||
import { Phone } from '../models/phone/phone.model';
|
||||
|
||||
interface IBeat {
|
||||
token: string,
|
||||
gpsLocation: Array<number>,
|
||||
battery: number,
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
export class RabbitMQ {
|
||||
connection: amqp.Connection | null = null;
|
||||
@@ -9,12 +18,32 @@ export class RabbitMQ {
|
||||
this.connection = await amqp.connect(RABBITMQ_URI);
|
||||
this.channel = await this.connection.createChannel();
|
||||
|
||||
this.channel.consume('Tracker', (msg) => {
|
||||
logger.debug("Received from broker: " + msg?.content.toString());
|
||||
}, { noAck: false });
|
||||
this.channel.consume('tracker', async (income) => {
|
||||
if (income === undefined || income === null) return;
|
||||
|
||||
const msg: IBeat = JSON.parse(income.content.toString()) as IBeat
|
||||
|
||||
// Get phone
|
||||
const phone = await Phone.findOne({ androidId: msg.token });
|
||||
if (phone == undefined) {
|
||||
logger.info(`Received beat from unknown device with id ${msg.token}`);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`New beat from ${phone.displayName} with ${msg.gpsLocation[2]} accuracy and ${msg.battery}% battery`)
|
||||
|
||||
Beat.create({
|
||||
phone: phone._id,
|
||||
coordinate: [msg.gpsLocation[0], msg.gpsLocation[1]],
|
||||
accuracy: msg.gpsLocation[2],
|
||||
speed: msg.gpsLocation[3],
|
||||
battery: msg.battery,
|
||||
createdAt: msg.timestamp
|
||||
});
|
||||
}, { noAck: true });
|
||||
}
|
||||
|
||||
async publish(queueName = 'Tracker', data: any) {
|
||||
async publish(queueName = 'tracker', data: any) {
|
||||
if (this.connection == undefined) await this.init()
|
||||
this.channel?.sendToQueue(queueName, Buffer.from(data));
|
||||
}
|
||||
|
||||
6
backend/lib/request.ts
Normal file
6
backend/lib/request.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Request } from "express";
|
||||
import { IUser } from "../models/user/user.interface";
|
||||
|
||||
export interface LivebeatRequest extends Request {
|
||||
user?: IUser
|
||||
}
|
||||
Reference in New Issue
Block a user