Switch from RabbitMQ to server-sent events
(not fully working yet)
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import { Response } from "express";
|
||||
import { eventManager, logger } from "../app";
|
||||
import { LivebeatRequest } from "../lib/request";
|
||||
import { IBeat } from "../models/beat/beat.interface";
|
||||
import { Beat } from "../models/beat/beat.model.";
|
||||
import { ISeverity } from "../models/notifications/notification.interface";
|
||||
import { Phone } from "../models/phone/phone.model";
|
||||
|
||||
const timeouts: Map<string, NodeJS.Timeout> = new Map<string, NodeJS.Timeout>();
|
||||
|
||||
export async function GetBeatStats(req: LivebeatRequest, res: Response) {
|
||||
const phones = await Phone.find({ user: req.user?._id });
|
||||
const perPhone: any = {};
|
||||
@@ -20,27 +24,88 @@ export async function GetBeatStats(req: LivebeatRequest, res: Response) {
|
||||
}
|
||||
|
||||
export async function GetBeat(req: LivebeatRequest, res: Response) {
|
||||
const from: number = Number(req.query.from);
|
||||
const to: number = Number(req.query.to);
|
||||
const from: number = Number(req.query.from || 0);
|
||||
const to: number = Number(req.query.to || Date.now() / 1000);
|
||||
const limit: number = Number(req.query.limit || 10000);
|
||||
const sort: number = Number(req.query.sort || 1); // Either -1 or 1
|
||||
const phoneId = req.query.phoneId;
|
||||
|
||||
// Grab default phone if non was provided.
|
||||
const phone = req.query.phone === undefined ? await Phone.findOne({ user: req.user?._id }) : await Phone.findOne({ _id: phoneId, user: req.user?._id });
|
||||
let beats: IBeat[] = []
|
||||
let beats: IBeat[] = [];
|
||||
|
||||
//console.log(from, to);
|
||||
//console.log(`Search from ${new Date(from).toString()} to ${new Date(to * 1000).toString()}`);
|
||||
|
||||
if (phone !== null) {
|
||||
beats = await Beat.find(
|
||||
{
|
||||
phone: phone._id,
|
||||
createdAt: {
|
||||
$gte: new Date((from | 0) * 1000),
|
||||
$lte: new Date((to | Date.now() /1000) * 1000)
|
||||
$gte: new Date((from)),
|
||||
$lte: new Date(to * 1000)
|
||||
}
|
||||
}).sort({ _id: sort }).limit(limit);
|
||||
res.status(200).send(beats);
|
||||
} else {
|
||||
res.status(404).send({ message: 'Phone not found' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function AddBeat(req: LivebeatRequest, res: Response) {
|
||||
const beat = req.body as IBeat;
|
||||
const androidId = req.headers.deviceId as string;
|
||||
|
||||
if (androidId === undefined) {
|
||||
res.status(401).send({ message: 'Device id is missing' });
|
||||
}
|
||||
|
||||
// Get phone
|
||||
const phone = await Phone.findOne({ androidId });
|
||||
if (phone == undefined) {
|
||||
logger.warning(`Received beat from unknown device with id ${androidId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let newBeat;
|
||||
if (beat.coordinate !== undefined && beat.accuracy !== undefined) {
|
||||
logger.info(`New beat from ${phone.displayName} => ${beat.coordinate[0]}, ${beat.coordinate[1]} | Height: ${beat.coordinate[3]}m | Speed: ${beat.coordinate[4]} | Accuracy: ${beat.accuracy}% | Battery: ${beat.battery}%`);
|
||||
|
||||
newBeat = await Beat.create({
|
||||
phone: phone._id,
|
||||
// [latitude, longitude, altitude]
|
||||
coordinate: [beat.coordinate[0], beat.coordinate[1], beat.coordinate[2]],
|
||||
accuracy: beat.coordinate[3],
|
||||
speed: beat.coordinate[4],
|
||||
battery: beat.battery,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
newBeat = await Beat.create({
|
||||
phone: phone._id,
|
||||
battery: beat.battery,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
|
||||
// Broadcast if device became active
|
||||
if (timeouts.has(phone.id)) {
|
||||
clearTimeout(timeouts.get(phone.id)!!);
|
||||
} else {
|
||||
phone.active = true;
|
||||
await phone.save();
|
||||
|
||||
eventManager.push('phone_alive', phone.toJSON(), phone.user);
|
||||
}
|
||||
|
||||
const timeoutTimer = setTimeout(async () => {
|
||||
eventManager.push('phone_dead', phone.toJSON(), phone.user, ISeverity.WARN);
|
||||
timeouts.delete(phone.id);
|
||||
phone.active = false;
|
||||
await phone.save();
|
||||
}, 60_000);
|
||||
timeouts.set(phone.id, timeoutTimer);
|
||||
|
||||
eventManager.push('beat', newBeat.toJSON(), phone.user);
|
||||
}
|
||||
Reference in New Issue
Block a user