feat: 增加机器位置手动编辑功能及公开查看界面
This commit is contained in:
+14
-10
@@ -163,27 +163,31 @@ router.get('/check-update', (req, res) => {
|
||||
res.json({ update_counter: globalUpdateCounter });
|
||||
});
|
||||
|
||||
router.post('/machine-location', (req, res) => {
|
||||
router.post('/machine-location', async (req, res) => {
|
||||
const data = req.body || {};
|
||||
if (!data.device_id) return res.status(400).json({ ok: false, error: 'device_id required' });
|
||||
if (!data.station && data.station_code) {
|
||||
data.station = resolveStationName(data.station_code);
|
||||
}
|
||||
DataService.updateMachineLocation(data.device_id, data);
|
||||
await DataService.updateMachineLocation(data.device_id, data);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
router.get('/machine-locations', (req, res) => {
|
||||
const locs = DataService.getMachineLocations() || {};
|
||||
// clean up old locations (> 5 minutes)
|
||||
const now = Date.now();
|
||||
const active = {};
|
||||
for (const [id, data] of Object.entries(locs)) {
|
||||
if (now - data.last_update_ts < 5 * 60 * 1000) {
|
||||
active[id] = data;
|
||||
}
|
||||
res.json({ ok: true, data: locs });
|
||||
});
|
||||
|
||||
router.put('/machine-location/:id', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const data = req.body || {};
|
||||
const current = DataService.getMachineLocations()[id];
|
||||
if (!current) return res.status(404).json({ ok: false, error: 'machine not found' });
|
||||
if (!data.station && data.station_code) {
|
||||
data.station = resolveStationName(data.station_code);
|
||||
}
|
||||
res.json({ ok: true, data: active });
|
||||
await DataService.updateMachineLocation(id, data);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Config
|
||||
|
||||
@@ -224,6 +224,11 @@ router.get('/fares/query', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/machine-locations', (req, res) => {
|
||||
const locs = DataService.getMachineLocations() || {};
|
||||
res.json({ ok: true, data: locs });
|
||||
});
|
||||
|
||||
router.get('/config', (req, res) => {
|
||||
const cfg = DataService.getConfig();
|
||||
res.json({
|
||||
|
||||
@@ -95,6 +95,9 @@ const DataService = {
|
||||
if (configs.length > 0) cache.config = normalizeConfig(configs[0].v);
|
||||
else await conn.query('INSERT INTO kv_store (k, v) VALUES (?, ?)', ['config', JSON.stringify(cache.config)]);
|
||||
|
||||
const [machLocs] = await conn.query('SELECT v FROM kv_store WHERE k = ?', ['machine_locations']);
|
||||
if (machLocs.length > 0) cache.machineLocations = machLocs[0].v || {};
|
||||
|
||||
const [stations] = await conn.query('SELECT data FROM stations');
|
||||
cache.stations = stations.map(r => r.data);
|
||||
|
||||
@@ -526,11 +529,16 @@ const DataService = {
|
||||
},
|
||||
|
||||
getMachineLocations: () => cache.machineLocations,
|
||||
updateMachineLocation: (deviceId, data) => {
|
||||
updateMachineLocation: async (deviceId, data) => {
|
||||
cache.machineLocations[deviceId] = {
|
||||
...cache.machineLocations[deviceId],
|
||||
...data,
|
||||
last_update_ts: Date.now()
|
||||
};
|
||||
try {
|
||||
await pool.query('INSERT INTO kv_store (k, v) VALUES (?, ?) ON DUPLICATE KEY UPDATE v = VALUES(v)',
|
||||
['machine_locations', JSON.stringify(cache.machineLocations)]);
|
||||
} catch(e) { console.error('updateMachineLocation error', e); }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user