/
Alcatraz
/
Wagomon
Обзор
Документация
Войти
/
Alcatraz
/
Wagomon
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/lib/server/currentSnapshot.js
123 строки
4 KB
saitgalineu
vkr
26 май 2026, 10:08
26 май 2026, 10:08
f9b2919
Код
Авторство
О чём код?
import { db } from './db.js'; import { getSystemSettings } from './systemSettings.js'; import { fromCelsiusToUnit, isTemperatureMeasureName } from './temperature.js'; export async function getCurrentSnapshot() { const result = {}; const { temperature_unit } = await getSystemSettings(); const controllersResult = await db.query( 'SELECT * FROM wago WHERE (analog = TRUE OR "binary" = TRUE) ORDER BY sort_order ASC NULLS LAST, id ASC' ); const controllers = controllersResult.rows || []; if (!controllers.length) { return result; } const controllerIds = controllers.map((c) => c.id); const [analogRes, binaryRes] = await Promise.all([ db.query( `SELECT sa.wago_id, sa.id, sa.name, sa.sort_order, sa.connection_id, sa.crit_min, sa.crit_max, sa.min_value, sa.max_value, sa.description, mt.name AS measure_type_name, latest.value AS last_value FROM sensors_analog sa LEFT JOIN measure_types mt ON sa.measure_type_id = mt.id LEFT JOIN LATERAL ( SELECT value FROM sensor_data_analog sda WHERE sda.sensor_id = sa.id ORDER BY time DESC LIMIT 1 ) AS latest ON TRUE WHERE sa.wago_id = ANY($1::int[]) ORDER BY sa.wago_id ASC, sa.sort_order ASC NULLS LAST, sa.connection_id ASC NULLS LAST, sa.id ASC`, [controllerIds] ), db.query( `SELECT sb.wago_id, sb.id, sb.name, sb.sort_order, sb.connection_id, sb.description, latest.value AS last_value FROM sensors_binary sb LEFT JOIN LATERAL ( SELECT value FROM sensor_data_binary sdb WHERE sdb.sensor_id = sb.id ORDER BY time DESC LIMIT 1 ) AS latest ON TRUE WHERE sb.wago_id = ANY($1::int[]) ORDER BY sb.wago_id ASC, sb.sort_order ASC NULLS LAST, sb.connection_id ASC NULLS LAST, sb.id ASC`, [controllerIds] ) ]); const analogByController = new Map(); for (const row of analogRes.rows || []) { const ctrlKey = `controller_${row.wago_id}`; const list = analogByController.get(ctrlKey) ?? []; const isTemp = isTemperatureMeasureName(row.measure_type_name); const baseCritMin = Number(row.crit_min ?? row.min_value ?? null); const baseCritMax = Number(row.crit_max ?? row.max_value ?? null); const value = isTemp ? fromCelsiusToUnit(row.last_value ?? null, temperature_unit) : row.last_value ?? null; const crit_min = isTemp ? fromCelsiusToUnit(baseCritMin, temperature_unit) : baseCritMin; const crit_max = isTemp ? fromCelsiusToUnit(baseCritMax, temperature_unit) : baseCritMax; list.push({ id: row.id, name: row.name, sort_order: row.sort_order ?? null, connection_id: row.connection_id, value, crit_min, crit_max, description: row.description || null }); analogByController.set(ctrlKey, list); } const binaryByController = new Map(); for (const row of binaryRes.rows || []) { const ctrlKey = `controller_${row.wago_id}`; const list = binaryByController.get(ctrlKey) ?? []; list.push({ id: row.id, name: row.name, sort_order: row.sort_order ?? null, connection_id: row.connection_id, value: row.last_value ?? null, description: row.description || null }); binaryByController.set(ctrlKey, list); } for (const ctrl of controllers) { const ctrlKey = `controller_${ctrl.id}`; const analog = ctrl.analog ? (analogByController.get(ctrlKey) || undefined) : undefined; const binary = ctrl.binary ? (binaryByController.get(ctrlKey) || undefined) : undefined; result[ctrlKey] = { analog, binary }; } return result; }