feat: count people around

This commit is contained in:
Jo 2025-09-11 11:53:44 +02:00
parent 74e1b5f10b
commit 1f449e9f0c
Signed by: devthejo
GPG key ID: 00CCA7A92B1D5351
2 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,46 @@
const { ctx } = require("@modjo/core")
const { reqCtx } = require("@modjo/express/ctx")
const RADAR_DISTANCE_KM = 25000 // 25km in meters
module.exports = function () {
const redis = ctx.require("redisHotGeodata")
async function doRadarPeopleCount(_req) {
const { deviceId } = reqCtx.get("session")
if (!deviceId) {
throw new Error("No device found in session")
}
// Get current device location from Redis
const devicePosition = await redis.geopos("device", deviceId)
if (
!devicePosition ||
!devicePosition[0] ||
devicePosition[0].length !== 2
) {
throw new Error("Device location not available in Redis")
}
const [longitude, latitude] = devicePosition[0]
// Use Redis GEORADIUS to get all devices within 25km
const devicesData = await redis.georadius(
"device",
longitude,
latitude,
RADAR_DISTANCE_KM,
"m",
"WITHDIST"
)
const count = devicesData.filter(
([id]) => parseInt(id, 10) !== deviceId
).length
return { count }
}
return [doRadarPeopleCount]
}

View file

@ -0,0 +1,35 @@
# description: Get count of people within 25km radius of authenticated user's device location
x-security:
- auth: ["user"]
responses:
200:
# description: People count within 25km radius
content:
application/json:
schema:
type: object
properties:
count:
type: integer
minimum: 0
description: Number of people within 25km radius
required:
- count
400:
# description: Invalid coordinates provided
content:
application/json:
schema:
type: object
properties:
error:
type: string
500:
# description: Internal server error
content:
application/json:
schema:
type: object
properties:
error:
type: string