44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const websPath = path.join(__dirname, "..", "public");
|
|
|
|
/**
|
|
* Registers map-related routes.
|
|
* @param {Object} router - Route wrapper with get/post methods
|
|
* @param {Object} app - SignalK app instance
|
|
* @param {Object} settings - Plugin settings
|
|
*/
|
|
function registerMapRoutes(router, app, settings) {
|
|
// Serve interactive map with Mapbox token injected
|
|
router.get('/map', (req, res) => {
|
|
const filePath = path.join(websPath, "map.html");
|
|
fs.readFile(filePath, "utf8", (err, html) => {
|
|
if (err) {
|
|
res.status(500).send("Error loading map");
|
|
return;
|
|
}
|
|
const token = settings?.mapboxKey ?? "";
|
|
const finalHtml = html.replace("{{MAPBOX_KEY}}", token);
|
|
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
res.send(finalHtml);
|
|
});
|
|
});
|
|
|
|
// Stream boat position and serve latest via API
|
|
let lastPosition = null;
|
|
|
|
app.streambundle.getSelfStream("navigation.position").onValue(pos => {
|
|
lastPosition = pos;
|
|
});
|
|
|
|
router.get('/map/boat', (req, res) => {
|
|
if (!lastPosition) {
|
|
return res.json({ error: "No position data available" });
|
|
}
|
|
res.json(lastPosition);
|
|
});
|
|
}
|
|
|
|
module.exports = registerMapRoutes;
|