WebCam als schlanke alternative zum appVideoControl
This commit is contained in:
50
src/snapshotService.js
Normal file
50
src/snapshotService.js
Normal file
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
|
||||
// Returns an Express router mounted at /api/snapshot
|
||||
// GET /api/snapshot → JSON listing of cameras
|
||||
// GET /api/snapshot/cam0 → latest JPEG from cam0
|
||||
// GET /api/snapshot/cam1 → latest JPEG from cam1
|
||||
function createSnapshotRouter(streams) {
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (_req, res) => {
|
||||
res.json({
|
||||
cameras: streams.map((s, i) => ({
|
||||
id: `cam${i}`,
|
||||
device: s.device,
|
||||
running: s.isRunning,
|
||||
clients: s.clientCount,
|
||||
hasFrame: s.latestFrame !== null,
|
||||
url: `/api/snapshot/cam${i}`,
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:id', (req, res) => {
|
||||
const idx = parseInt(req.params.id.replace('cam', ''), 10);
|
||||
if (isNaN(idx) || !streams[idx]) {
|
||||
return res.status(404).json({ error: 'camera not found' });
|
||||
}
|
||||
|
||||
const frame = streams[idx].latestFrame;
|
||||
if (!frame) {
|
||||
return res.status(503).json({ error: 'no frame available yet – stream may still be starting' });
|
||||
}
|
||||
|
||||
res.set({
|
||||
'Content-Type': 'image/jpeg',
|
||||
'Content-Length': frame.length,
|
||||
'Cache-Control': 'no-store',
|
||||
'X-Camera-Id': `cam${idx}`,
|
||||
'X-Camera-Device': streams[idx].device,
|
||||
'X-Timestamp': new Date().toISOString(),
|
||||
});
|
||||
res.end(frame);
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
module.exports = { createSnapshotRouter };
|
||||
Reference in New Issue
Block a user