From dfc8c52d6aa291363354f953e45039637954bb2f Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 1 Aug 2023 15:35:10 +0300 Subject: [PATCH] device status upd, markers upd --- server.js | 59 ++++++++++++++------------------------ static/styles/main.css | 4 +++ static/templates/live.html | 35 ++++++++++++++++------ 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/server.js b/server.js index dfd890a..823900d 100644 --- a/server.js +++ b/server.js @@ -115,14 +115,17 @@ async function live(req, res) { }); const client = await pool.connect(); + const minuteInMillis = 60 * 1000; + const query = ` - SELECT id, serial FROM registrars ORDER BY id ASC + SELECT id, serial, lastkeepalive FROM registrars ORDER BY id ASC `; const registrars = await client.query(query); templateData.Registrars = registrars.rows.map((row) => ({ id: row.id, serial: row.serial, + status: Date.now() - Date.parse(row.lastkeepalive) <= minuteInMillis, })); console.log(templateData); @@ -481,28 +484,24 @@ async function devices(req, res) { port: DB_Port, }); const client = await pool.connect(); - - // Выполняем два запроса и получаем результаты - const queryConnected = ` - SELECT id, serial, connected, name, "group", plate, sim, ip, port + + const minuteInMillis = 60 * 1000; + + // Выполняем запрос, чтобы получить все данные из таблицы registrars + const queryRegistrars = ` + SELECT id, serial, lastkeepalive, name, "group", plate, sim, ip, port FROM registrars - WHERE connected = true ORDER BY id `; - const queryDisconnected = ` - SELECT id, serial, connected, name, "group", plate, sim, ip, port - FROM registrars - WHERE connected = false - ORDER BY id - `; - const connectedRegistrars = await client.query(queryConnected); - const disconnectedRegistrars = await client.query(queryDisconnected); - - templateData.Registrars = [ - ...connectedRegistrars.rows.map((registrar) => ({ + const registrarsResult = await client.query(queryRegistrars); + const allRegistrars = registrarsResult.rows; + + // Определяем статус connected на основе lastkeepalive + const templateData = { + Registrars: allRegistrars.map((registrar) => ({ id: registrar.id, serial: registrar.serial, - status: registrar.connected, + status: Date.now() - Date.parse(registrar.lastkeepalive) <= minuteInMillis, name: registrar.name, group: registrar.group, plate: registrar.plate, @@ -510,29 +509,15 @@ async function devices(req, res) { ip: registrar.ip, port: registrar.port, })), - ...disconnectedRegistrars.rows.map((registrar) => ({ - id: registrar.id, - serial: registrar.serial, - status: registrar.connected, - name: registrar.name, - group: registrar.group, - plate: registrar.plate, - sim: registrar.sim, - ip: registrar.ip, - port: registrar.port, - })), - ]; - + }; + console.log(templateData); - - const source = fs.readFileSync( - "static/templates/devices/index.html", - "utf8" - ); + + const source = fs.readFileSync("static/templates/devices/index.html", "utf8"); const template = handlebars.compile(source); const resultT = template(templateData); res.send(resultT); - + client.release(); } catch (error) { console.error(error); diff --git a/static/styles/main.css b/static/styles/main.css index fa91e28..af7609d 100644 --- a/static/styles/main.css +++ b/static/styles/main.css @@ -420,6 +420,10 @@ header h2 span { position: relative; } +.active-false { + opacity: 50%; +} + .device img { margin-left: -22px; padding-right: 7px; diff --git a/static/templates/live.html b/static/templates/live.html index 70d5b20..df13023 100644 --- a/static/templates/live.html +++ b/static/templates/live.html @@ -84,11 +84,11 @@
  • -
  • @@ -305,7 +305,7 @@ - --> @@ -508,7 +517,7 @@ const selectedDevices = Array.from(checkboxes) .then(data => { console.log(data.devicesData); data.devicesData.forEach(device => { - const { longitude, latitude, direction, speed } = device; + const { serial, longitude, latitude, direction, speed } = device; var newDirection = direction + 90 console.log(newDirection); @@ -516,7 +525,7 @@ 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} км/ч`; @@ -542,7 +551,7 @@ function updateMapMarkers(devicesData) { // Создание новых маркеров на карте на основе полученных данных devicesData.forEach(device => { - const { longitude, latitude, direction, speed } = device; + const { serial, longitude, latitude, direction, speed } = device; var newDirection = direction + 90 console.log(newDirection); @@ -550,7 +559,7 @@ function updateMapMarkers(devicesData) { const el = document.createElement('div'); el.className = 'marker'; // el.style.transform = `rotate(${direction}deg)`; - el.innerHTML = ` + el.innerHTML = `
    ${speed} км/ч`; @@ -563,6 +572,16 @@ function updateMapMarkers(devicesData) { // Добавление маркера в массив markers markers.push(marker); }); + serial = $("input[name=camera-serial]:checked").val() + + var svgId = "marker-" + serial; + var svgElement = document.getElementById(svgId); + if (svgElement) { + svgElement.innerHTML = ` + + + `; + } } setInterval(() => {