From f24a0c708a732401eae6cfde4da2b78b0f035a34 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 1 Aug 2023 16:05:54 +0300 Subject: [PATCH] update markers --- server.js | 67 +++++++++++++++++++++++--------------- static/templates/live.html | 12 +++---- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/server.js b/server.js index 823900d..5981ea6 100644 --- a/server.js +++ b/server.js @@ -34,17 +34,17 @@ app.get("/devices/drivers", drivers); app.get("/devices/update", update); -// const DB_User = process.env.DB_USER; -// const DB_Password = process.env.DB_PASSWORD; -// const DB_Host = process.env.DB_HOST; -// const DB_Port = process.env.DB_PORT; -// const DB_Name = process.env.DB_NAME; +const DB_User = process.env.DB_USER; +const DB_Password = process.env.DB_PASSWORD; +const DB_Host = process.env.DB_HOST; +const DB_Port = process.env.DB_PORT; +const DB_Name = process.env.DB_NAME; -const DB_User = "postgres"; -const DB_Password = "password"; -const DB_Host = "postgres"; -const DB_Port = "5432"; -const DB_Name = "postgres"; +// const DB_User = "postgres"; +// const DB_Password = "password"; +// const DB_Host = "postgres"; +// const DB_Port = "5432"; +// const DB_Name = "postgres"; async function index(req, res) { var templateData = { @@ -161,10 +161,13 @@ app.post("/devices-geo", async (req, res) => { // console.log(selectedDevices); const placeholders = selectedDevices - .map((_, index) => `$${index + 1}`) - .join(","); + .map((_, index) => `$${index + 1}`) + .join(","); const subquery = ` - SELECT MAX(time) AS time, serial + SELECT g.serial, g.longitude, g.latitude, g.direction, g.speed, r.lastkeepalive + FROM geo g + INNER JOIN ( + SELECT serial, MAX(time) AS time FROM geo WHERE serial IN ( SELECT serial @@ -172,24 +175,34 @@ app.post("/devices-geo", async (req, res) => { WHERE id IN (${placeholders}) ) GROUP BY serial - `; - const query = ` - SELECT g.serial, g.longitude, g.latitude, g.direction, g.speed - FROM geo g - INNER JOIN (${subquery}) s - ON g.serial = s.serial AND g.time = s.time - `; + ) s ON g.serial = s.serial AND g.time = s.time + INNER JOIN registrars r ON g.serial = r.serial +`; + +pool.query(subquery, selectedDevices, (err, result) => { + if (err) { + console.error("Ошибка выполнения запроса:", err); + res.status(500).json({ error: "Ошибка сервера" }); + return; + } + + const minuteInMillis = 60000; + + // Process the result to include lastkeepalive information + const devicesData = result.rows.map((row) => ({ + serial: row.serial, + longitude: row.longitude, + latitude: row.latitude, + direction: row.direction, + speed: row.speed, + status: Date.now() - Date.parse(row.lastkeepalive) <= minuteInMillis, + })); + - pool.query(query, selectedDevices, (err, result) => { - if (err) { - console.error("Ошибка выполнения запроса:", err); - res.status(500).json({ error: "Ошибка сервера" }); - return; - } // console.log(result.rows); - const devicesData = result.rows; + res.json({ devicesData }); }); }); diff --git a/static/templates/live.html b/static/templates/live.html index df13023..f4883f6 100644 --- a/static/templates/live.html +++ b/static/templates/live.html @@ -517,7 +517,7 @@ const selectedDevices = Array.from(checkboxes) .then(data => { console.log(data.devicesData); data.devicesData.forEach(device => { - const { serial, longitude, latitude, direction, speed } = device; + const { serial, status, longitude, latitude, direction, speed } = device; var newDirection = direction + 90 console.log(newDirection); @@ -525,10 +525,10 @@ const selectedDevices = Array.from(checkboxes) const el = document.createElement('div'); el.className = 'marker'; // el.style.transform = `rotate(${direction / 100}deg)`; - el.innerHTML = ` + el.innerHTML = ` -
${speed} км/ч`; +
${speed} км/ч`; // Создание нового маркера на карте const marker = new mapboxgl.Marker(el) @@ -551,7 +551,7 @@ function updateMapMarkers(devicesData) { // Создание новых маркеров на карте на основе полученных данных devicesData.forEach(device => { - const { serial, longitude, latitude, direction, speed } = device; + const { serial, status, longitude, latitude, direction, speed } = device; var newDirection = direction + 90 console.log(newDirection); @@ -559,10 +559,10 @@ function updateMapMarkers(devicesData) { const el = document.createElement('div'); el.className = 'marker'; // el.style.transform = `rotate(${direction}deg)`; - el.innerHTML = ` + el.innerHTML = ` -
${speed} км/ч`; +
${speed} км/ч`; // Создание нового маркера на карте const marker = new mapboxgl.Marker(el)