37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import path from 'path'
|
|
import fs from 'fs'
|
|
import { broadcast } from './ws.js';
|
|
|
|
const CSV_PATH = path.join(process.cwd(), 'db/logs.csv')
|
|
|
|
function appendToCSV(row) {
|
|
const exists = fs.existsSync(CSV_PATH)
|
|
|
|
const header = 'name,latitude,longitude,timestamp\n'
|
|
const line = `${row.name},${row.latitude},${row.longitude},${row.timestamp}\n`
|
|
|
|
if (!exists) {
|
|
fs.appendFileSync(CSV_PATH, header + line, 'utf8')
|
|
} else {
|
|
fs.appendFileSync(CSV_PATH, line, 'utf8')
|
|
}
|
|
}
|
|
|
|
const handlers = {
|
|
updateLocation(req, res) {
|
|
const { name, latitude, longitude, timestamp } = req.body;
|
|
console.log(`Received location: (${name}, ${latitude}, ${longitude}) at ${timestamp}`);
|
|
broadcast("update-location", { name, latitude, longitude, timestamp });
|
|
appendToCSV({ name, latitude, longitude, timestamp })
|
|
res.json({ success: true });
|
|
},
|
|
|
|
async uploadCSV(req, res) {
|
|
const csvString = req.body;
|
|
console.log(csvString);
|
|
await global.db.uploadCSV(csvString)
|
|
res.json({ ok: true });
|
|
}
|
|
}
|
|
|
|
export default handlers; |