diff --git a/api/package-lock.json b/api/package-lock.json
index 6bc1a97..496e131 100644
--- a/api/package-lock.json
+++ b/api/package-lock.json
@@ -10,6 +10,7 @@
"dependencies": {
"@influxdata/influxdb-client": "^1.35.0",
"@influxdata/influxdb-client-apis": "^1.35.0",
+ "@msgpack/msgpack": "^3.1.3",
"cookie-parser": "^1.4.7",
"dotenv": "^17.3.1",
"express": "^5.2.1",
@@ -40,6 +41,15 @@
"integrity": "sha512-JH8ZL/ywcJyR9MmJ5BNqZllXNZQqQbnVZOqpPQqE1vHiFgAw4NHbvE0FOduNU8IX9babitBT46571OnPTT0Zcw==",
"license": "MIT"
},
+ "node_modules/@msgpack/msgpack": {
+ "version": "3.1.3",
+ "resolved": "https://registry.npmjs.org/@msgpack/msgpack/-/msgpack-3.1.3.tgz",
+ "integrity": "sha512-47XIizs9XZXvuJgoaJUIE2lFoID8ugvc0jzSHP+Ptfk8nTbnR8g788wv48N03Kx0UkAv559HWRQ3yzOgzlRNUA==",
+ "license": "ISC",
+ "engines": {
+ "node": ">= 18"
+ }
+ },
"node_modules/accepts": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
diff --git a/api/package.json b/api/package.json
index 465261d..daca388 100644
--- a/api/package.json
+++ b/api/package.json
@@ -11,6 +11,7 @@
"dependencies": {
"@influxdata/influxdb-client": "^1.35.0",
"@influxdata/influxdb-client-apis": "^1.35.0",
+ "@msgpack/msgpack": "^3.1.3",
"cookie-parser": "^1.4.7",
"dotenv": "^17.3.1",
"express": "^5.2.1",
@@ -19,4 +20,4 @@
"minio": "^8.0.7",
"pg": "^8.20.0"
}
-}
+}
\ No newline at end of file
diff --git a/api/src/index.js b/api/src/index.js
index 36aad80..7b20121 100644
--- a/api/src/index.js
+++ b/api/src/index.js
@@ -81,7 +81,10 @@ app.use('/params', paramsRoutes)
const settingsRoutes = require('./routes/settings')
app.use('/settings', settingsRoutes)
-// Avvio del server
+const rulesRoutes = require('./routes/rules')
+app.use('/rules', rulesRoutes)
+
+
app.listen(PORT, '0.0.0.0', () => {
console.log(`Started on port ${PORT}`);
});
diff --git a/api/src/routes/rules.js b/api/src/routes/rules.js
new file mode 100644
index 0000000..5b17cf6
--- /dev/null
+++ b/api/src/routes/rules.js
@@ -0,0 +1,497 @@
+const router = require('express').Router();
+const db = require('../storage/postgres');
+const { encode } = require('@msgpack/msgpack');
+
+// Mapping tipo → tabelle
+const TYPE_MAP = {
+ weather: { rules: 'weather', items: 'weatheritems' },
+ data: { rules: 'dataread', items: 'datareaditems' },
+ logs: { rules: 'logs', items: 'logitems' }
+};
+
+const VALID_TYPES = Object.keys(TYPE_MAP);
+
+/**
+ * GET /rules — Lista tutte le rules (id, version, active) per ogni tipo
+ */
+router.get('/', async (req, res) => {
+ try {
+ const result = {};
+ for (const [type, tables] of Object.entries(TYPE_MAP)) {
+ const { rows } = await db.query(
+ `SELECT * FROM ${tables.rules} ORDER BY created_at DESC`,
+ [], 'rules'
+ );
+ result[type] = rows;
+ }
+ res.json(result);
+ } catch (err) {
+ console.error('Error fetching rules', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * GET /rules/versions — Ritorna solo id+version delle 3 rules attive (check rapido)
+ */
+router.get('/versions', async (req, res) => {
+ try {
+ const versions = {};
+ for (const [type, tables] of Object.entries(TYPE_MAP)) {
+ const { rows } = await db.query(
+ `SELECT id, version FROM ${tables.rules} WHERE active = true AND archived = false LIMIT 1`,
+ [], 'rules'
+ );
+ versions[type] = rows[0] || null;
+ }
+ res.json(versions);
+ } catch (err) {
+ console.error('Error fetching versions', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * GET /rules/active?type=weather — Ritorna la rule attiva completa con items
+ * Supporta Accept: application/msgpack per formato compatto
+ */
+router.get('/active', async (req, res) => {
+ const { type } = req.query;
+ if (!type || !VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: `invalid type, must be one of: ${VALID_TYPES.join(', ')}` });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rows: ruleRows } = await db.query(
+ `SELECT * FROM ${tables.rules} WHERE active = true AND archived = false LIMIT 1`,
+ [], 'rules'
+ );
+
+ if (ruleRows.length === 0) {
+ return res.status(404).json({ error: `no active ${type} rule found` });
+ }
+
+ const rule = ruleRows[0];
+
+ const { rows: items } = await db.query(
+ `SELECT * FROM ${tables.items} WHERE rule_id = $1 AND enabled = true`,
+ [rule.id], 'rules'
+ );
+
+ const payload = {
+ id: rule.id,
+ version: rule.version,
+ description: rule.description,
+ tags: rule.tags,
+ items
+ };
+
+ // Se il client accetta msgpack, rispondi in binario
+ if (req.accepts('application/msgpack')) {
+ res.set('Content-Type', 'application/msgpack');
+ return res.send(Buffer.from(encode(payload)));
+ }
+
+ res.json(payload);
+ } catch (err) {
+ console.error('Error fetching active rule', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * GET /rules/:type/:id — Dettaglio di una rule specifica con items
+ */
+router.get('/:type/:id', async (req, res) => {
+ const { type, id } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rows: ruleRows } = await db.query(
+ `SELECT * FROM ${tables.rules} WHERE id = $1`,
+ [id], 'rules'
+ );
+
+ if (ruleRows.length === 0) {
+ return res.status(404).json({ error: 'rule not found' });
+ }
+
+ const rule = ruleRows[0];
+ const { rows: items } = await db.query(
+ `SELECT * FROM ${tables.items} WHERE rule_id = $1`,
+ [rule.id], 'rules'
+ );
+
+ res.json({ ...rule, items });
+ } catch (err) {
+ console.error('Error fetching rule', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * PATCH /rules/update?type=weather&from=1.0.0
+ * Ritorna le rules con versione > from (per aggiornamenti incrementali)
+ */
+router.patch('/update', async (req, res) => {
+ const { type, from } = req.query;
+
+ if (!type || !VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+ if (!from) {
+ return res.status(400).json({ error: 'missing from parameter' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rows } = await db.query(
+ `SELECT * FROM ${tables.rules} WHERE version > $1 AND archived = false ORDER BY version ASC`,
+ [from], 'rules'
+ );
+
+ if (rows.length === 0) {
+ return res.status(404).json({ error: 'no rules found with version greater than specified' });
+ }
+
+ // Per ogni rule, allegare gli items
+ const results = [];
+ for (const rule of rows) {
+ const { rows: items } = await db.query(
+ `SELECT * FROM ${tables.items} WHERE rule_id = $1`,
+ [rule.id], 'rules'
+ );
+ results.push({ ...rule, items });
+ }
+
+ if (req.accepts('application/msgpack')) {
+ res.set('Content-Type', 'application/msgpack');
+ return res.send(Buffer.from(encode(results)));
+ }
+
+ res.json(results);
+ } catch (err) {
+ console.error('Error fetching rules update', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+// --- ID Generation ---
+const TYPE_PREFIX = { weather: 'w', data: 'd', logs: 'l' };
+
+function generateId(type) {
+ const prefix = TYPE_PREFIX[type] || 'x';
+ const num = Math.floor(1000 + Math.random() * 9000);
+ // Pad to 8 chars with trailing zeros
+ return (prefix + num).padEnd(8, '0');
+}
+
+// --- ITEM FIELD DEFINITIONS per tipo ---
+const ITEM_FIELDS = {
+ weather: ['group_name', 'ref', 'name', 'unit', 'enabled'],
+ data: ['category', 'path', 'unit', 'enabled'],
+ logs: ['path', 'ref', 'unit', 'measurement', 'enabled']
+};
+
+// Campi rule aggiornabili
+const RULE_UPDATE_FIELDS = {
+ weather: ['version', 'tags', 'description'],
+ data: ['version', 'tags'],
+ logs: ['version', 'tags', 'description', 'browser_rule_id']
+};
+
+/**
+ * POST /rules/:type — Crea una nuova rule
+ */
+router.post('/:type', async (req, res) => {
+ const { type } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+ const id = generateId(type);
+ const { version, tags, description, browser_rule_id } = req.body;
+
+ if (!version) {
+ return res.status(400).json({ error: 'version is required' });
+ }
+
+ try {
+ let sql, params;
+ if (type === 'weather') {
+ sql = `INSERT INTO ${tables.rules} (id, version, tags, description) VALUES ($1, $2, $3, $4) RETURNING *`;
+ params = [id, version, tags || [], description || null];
+ } else if (type === 'logs') {
+ sql = `INSERT INTO ${tables.rules} (id, version, tags, description, browser_rule_id) VALUES ($1, $2, $3, $4, $5) RETURNING *`;
+ params = [id, version, tags || [], description || null, browser_rule_id || null];
+ } else {
+ sql = `INSERT INTO ${tables.rules} (id, version, tags) VALUES ($1, $2, $3) RETURNING *`;
+ params = [id, version, tags || []];
+ }
+
+ const { rows } = await db.query(sql, params, 'rules');
+ res.status(201).json(rows[0]);
+ } catch (err) {
+ console.error('Error creating rule', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * PUT /rules/:type/:id — Aggiorna una rule
+ */
+router.put('/:type/:id', async (req, res) => {
+ const { type, id } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+ const allowed = RULE_UPDATE_FIELDS[type];
+ const sets = [];
+ const params = [];
+ let idx = 1;
+
+ for (const field of allowed) {
+ if (req.body[field] !== undefined) {
+ sets.push(`${field} = $${idx}`);
+ params.push(req.body[field]);
+ idx++;
+ }
+ }
+
+ if (sets.length === 0) {
+ return res.status(400).json({ error: 'no valid fields to update' });
+ }
+
+ sets.push(`updated_at = NOW()`);
+ params.push(id);
+
+ try {
+ const sql = `UPDATE ${tables.rules} SET ${sets.join(', ')} WHERE id = $${idx} RETURNING *`;
+ const { rows } = await db.query(sql, params, 'rules');
+ if (rows.length === 0) return res.status(404).json({ error: 'rule not found' });
+ res.json(rows[0]);
+ } catch (err) {
+ console.error('Error updating rule', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * DELETE /rules/:type/:id — Elimina una rule (cascade items)
+ */
+router.delete('/:type/:id', async (req, res) => {
+ const { type, id } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rowCount } = await db.query(
+ `DELETE FROM ${tables.rules} WHERE id = $1`, [id], 'rules'
+ );
+ if (rowCount === 0) return res.status(404).json({ error: 'rule not found' });
+ res.json({ status: 'ok' });
+ } catch (err) {
+ console.error('Error deleting rule', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * PATCH /rules/:type/:id/active — Toggle active
+ * Disattiva tutte le altre, poi attiva questa (o disattiva se già attiva)
+ */
+router.patch('/:type/:id/active', async (req, res) => {
+ const { type, id } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ // Check current state
+ const { rows: current } = await db.query(
+ `SELECT active FROM ${tables.rules} WHERE id = $1`, [id], 'rules'
+ );
+ if (current.length === 0) return res.status(404).json({ error: 'rule not found' });
+
+ const isActive = current[0].active;
+
+ if (isActive) {
+ // Disattiva
+ await db.query(`UPDATE ${tables.rules} SET active = false, updated_at = NOW() WHERE id = $1`, [id], 'rules');
+ } else {
+ // Disattiva tutte, poi attiva questa
+ await db.query(`UPDATE ${tables.rules} SET active = false, updated_at = NOW() WHERE active = true`, [], 'rules');
+ await db.query(`UPDATE ${tables.rules} SET active = true, updated_at = NOW() WHERE id = $1`, [id], 'rules');
+ }
+
+ res.json({ status: 'ok', active: !isActive });
+ } catch (err) {
+ console.error('Error toggling active', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * PATCH /rules/:type/:id/archive — Toggle archived
+ */
+router.patch('/:type/:id/archive', async (req, res) => {
+ const { type, id } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rows } = await db.query(
+ `UPDATE ${tables.rules} SET archived = NOT archived, updated_at = NOW() WHERE id = $1 RETURNING archived`,
+ [id], 'rules'
+ );
+ if (rows.length === 0) return res.status(404).json({ error: 'rule not found' });
+ res.json({ status: 'ok', archived: rows[0].archived });
+ } catch (err) {
+ console.error('Error toggling archive', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * POST /rules/:type/:id/items — Aggiungi item
+ */
+router.post('/:type/:id/items', async (req, res) => {
+ const { type, id } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+ const fields = ITEM_FIELDS[type].filter(f => f !== 'enabled');
+ const values = fields.map(f => req.body[f]);
+
+ // Validate required fields present
+ for (let i = 0; i < fields.length; i++) {
+ if (values[i] === undefined || values[i] === null || values[i] === '') {
+ return res.status(400).json({ error: `${fields[i]} is required` });
+ }
+ }
+
+ const allFields = ['rule_id', ...fields];
+ const allValues = [id, ...values];
+ const placeholders = allValues.map((_, i) => `$${i + 1}`).join(', ');
+
+ try {
+ const sql = `INSERT INTO ${tables.items} (${allFields.join(', ')}) VALUES (${placeholders}) RETURNING *`;
+ const { rows } = await db.query(sql, allValues, 'rules');
+ res.status(201).json(rows[0]);
+ } catch (err) {
+ console.error('Error creating item', err);
+ if (err.code === '23505') {
+ return res.status(409).json({ error: 'duplicate item (unique constraint)' });
+ }
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * PUT /rules/:type/:id/items/:itemId — Aggiorna item
+ */
+router.put('/:type/:id/items/:itemId', async (req, res) => {
+ const { type, id, itemId } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+ const allowed = ITEM_FIELDS[type];
+ const sets = [];
+ const params = [];
+ let idx = 1;
+
+ for (const field of allowed) {
+ if (req.body[field] !== undefined) {
+ sets.push(`${field} = $${idx}`);
+ params.push(req.body[field]);
+ idx++;
+ }
+ }
+
+ if (sets.length === 0) {
+ return res.status(400).json({ error: 'no valid fields to update' });
+ }
+
+ params.push(itemId, id);
+
+ try {
+ const sql = `UPDATE ${tables.items} SET ${sets.join(', ')} WHERE id = $${idx} AND rule_id = $${idx + 1} RETURNING *`;
+ const { rows } = await db.query(sql, params, 'rules');
+ if (rows.length === 0) return res.status(404).json({ error: 'item not found' });
+ res.json(rows[0]);
+ } catch (err) {
+ console.error('Error updating item', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * DELETE /rules/:type/:id/items/:itemId — Elimina item
+ */
+router.delete('/:type/:id/items/:itemId', async (req, res) => {
+ const { type, id, itemId } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rowCount } = await db.query(
+ `DELETE FROM ${tables.items} WHERE id = $1 AND rule_id = $2`, [itemId, id], 'rules'
+ );
+ if (rowCount === 0) return res.status(404).json({ error: 'item not found' });
+ res.json({ status: 'ok' });
+ } catch (err) {
+ console.error('Error deleting item', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+/**
+ * PATCH /rules/:type/:id/items/:itemId/toggle — Toggle enabled
+ */
+router.patch('/:type/:id/items/:itemId/toggle', async (req, res) => {
+ const { type, id, itemId } = req.params;
+ if (!VALID_TYPES.includes(type)) {
+ return res.status(400).json({ error: 'invalid rule type' });
+ }
+
+ const tables = TYPE_MAP[type];
+
+ try {
+ const { rows } = await db.query(
+ `UPDATE ${tables.items} SET enabled = NOT enabled WHERE id = $1 AND rule_id = $2 RETURNING enabled`,
+ [itemId, id], 'rules'
+ );
+ if (rows.length === 0) return res.status(404).json({ error: 'item not found' });
+ res.json({ status: 'ok', enabled: rows[0].enabled });
+ } catch (err) {
+ console.error('Error toggling item', err);
+ res.status(500).json({ error: 'internal server error' });
+ }
+});
+
+module.exports = router;
diff --git a/api/src/storage/postgres.js b/api/src/storage/postgres.js
index 832e344..55b4124 100644
--- a/api/src/storage/postgres.js
+++ b/api/src/storage/postgres.js
@@ -13,7 +13,8 @@ const config = {
const pools = {
data: new Pool({ ...config, database: process.env.DATA_DB }),
users: new Pool({ ...config, database: process.env.USERS_DB }),
- sensors: new Pool({ ...config, database: process.env.SENSOR_DB || 'users' })
+ sensors: new Pool({ ...config, database: process.env.SENSOR_DB || 'users' }),
+ rules: new Pool({ ...config, database: process.env.RULES_DB || 'rules' }),
}
Object.entries(pools).forEach(([name, pool]) => {
diff --git a/console/src/index.js b/console/src/index.js
index 7e3a79d..b112a4e 100644
--- a/console/src/index.js
+++ b/console/src/index.js
@@ -87,6 +87,10 @@ app.get('/live', renderPage('live', {
realtimeWsUrl: process.env.REALTIME_WS_URL || 'ws://localhost:3002'
}));
+app.get('/rulesets', renderPage('rulesets', {
+ apiUrl: process.env.API_URL || 'http://localhost:3003'
+}));
+
app.listen(PORT, '0.0.0.0', () => {
console.log(`Started on port ${PORT}`);
});
diff --git a/console/src/pages/dashboard.html b/console/src/pages/dashboard.html
index 249530b..eb04d05 100644
--- a/console/src/pages/dashboard.html
+++ b/console/src/pages/dashboard.html
@@ -31,10 +31,10 @@
-
+
-
Previsioni
-
Visualizza le condizioni meteo attuali e le previsioni future.
+
Rulesets
+
Gestisci i template di configurazione per weather, data e logs.
diff --git a/console/src/pages/live.html b/console/src/pages/live.html
index e4eac83..f072500 100644
--- a/console/src/pages/live.html
+++ b/console/src/pages/live.html
@@ -28,6 +28,20 @@
+
+
+
+
+
@@ -299,6 +317,7 @@ function selectSession(sId, meta) {
document.getElementById('bottomBar').style.display = '';
document.getElementById('sensorName').textContent = meta.name || sId;
document.getElementById('sessionInfoTitle').textContent = `Sensore: ${meta.name || sId}`;
+ document.getElementById('currentSessionLabel').textContent = meta.sessionLabel || meta.session || sId;
liveData = {};
Object.values(miniCharts).forEach(c => c.destroy());
miniCharts = {};
@@ -704,4 +723,33 @@ function showToast(msg) {
}, 4500);
}
+// --- Session Label Popup ---
+document.getElementById('sessionLabelBtn').onclick = () => {
+ document.getElementById('sessionLabelInput').value = '';
+ document.getElementById('sessionLabelOverlay').style.display = 'flex';
+};
+document.getElementById('cancelSessionLabelBtn').onclick = () => {
+ document.getElementById('sessionLabelOverlay').style.display = 'none';
+};
+document.getElementById('saveSessionLabelBtn').onclick = async () => {
+ const label = document.getElementById('sessionLabelInput').value.trim();
+ if (!label || !currentSensorId) return;
+ try {
+ const res = await fetch(`${REALTIME_URL}/sessions/${currentSensorId}/label`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ label })
+ });
+ if (res.ok) {
+ document.getElementById('currentSessionLabel').textContent = label;
+ showToast(`Sessione rinominata: ${label}`);
+ } else {
+ showToast('Errore nel salvataggio');
+ }
+ } catch (err) {
+ showToast('Errore di connessione');
+ }
+ document.getElementById('sessionLabelOverlay').style.display = 'none';
+};
+
diff --git a/console/src/pages/rulesets.html b/console/src/pages/rulesets.html
new file mode 100644
index 0000000..97115be
--- /dev/null
+++ b/console/src/pages/rulesets.html
@@ -0,0 +1,591 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Conferma
+
Sei sicuro?
+
+
+
+
+
+
+
+
+
diff --git a/console/src/static/styles/rulesets.css b/console/src/static/styles/rulesets.css
new file mode 100644
index 0000000..0b3b55a
--- /dev/null
+++ b/console/src/static/styles/rulesets.css
@@ -0,0 +1,771 @@
+/* --- Rulesets Page --- */
+
+.rs-page {
+ max-width: 1200px;
+ margin: 0 auto;
+ padding: 0 24px 120px;
+}
+
+/* Header */
+.rs-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ padding: 24px 0 16px;
+ position: sticky;
+ top: 0;
+ z-index: 10;
+ background: var(--header-bg);
+ backdrop-filter: blur(12px);
+ -webkit-backdrop-filter: blur(12px);
+ border-bottom: 1px solid var(--header-border);
+ margin-bottom: 20px;
+}
+
+.rs-header h1 {
+ font-size: 1.5rem;
+ font-weight: 700;
+ color: var(--text-primary);
+}
+
+.rs-header-left {
+ display: flex;
+ align-items: center;
+ gap: 16px;
+}
+
+.rs-header-right {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+}
+
+/* Type Picker */
+.rs-type-picker {
+ display: flex;
+ gap: 4px;
+ background: rgba(241, 245, 249, 0.8);
+ border-radius: 12px;
+ padding: 4px;
+}
+
+.rs-type-picker button {
+ padding: 6px 16px;
+ border: none;
+ border-radius: 8px;
+ background: transparent;
+ color: var(--text-secondary);
+ font-size: 0.85rem;
+ font-weight: 600;
+ cursor: pointer;
+ transition: all 0.2s ease;
+ font-family: inherit;
+}
+
+.rs-type-picker button.active {
+ background: white;
+ color: var(--accent-color);
+ box-shadow: var(--shadow-sm);
+}
+
+.rs-type-picker button:hover:not(.active) {
+ color: var(--text-primary);
+}
+
+/* Toolbar */
+.rs-toolbar {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+ margin-bottom: 20px;
+ flex-wrap: wrap;
+}
+
+.rs-toolbar-left {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+}
+
+.rs-toolbar-right {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+}
+
+.rs-filter-btn {
+ padding: 6px 14px;
+ border: 1px solid var(--header-border);
+ border-radius: 8px;
+ background: white;
+ color: var(--text-secondary);
+ font-size: 0.8rem;
+ font-weight: 500;
+ cursor: pointer;
+ transition: all 0.2s ease;
+ font-family: inherit;
+}
+
+.rs-filter-btn.active {
+ background: var(--accent-light);
+ color: var(--accent-color);
+ border-color: var(--accent-border);
+}
+
+.rs-filter-btn:hover:not(.active) {
+ border-color: var(--accent-border);
+ color: var(--text-primary);
+}
+
+.rs-sort-select {
+ padding: 6px 12px;
+ border: 1px solid var(--header-border);
+ border-radius: 8px;
+ background: white;
+ color: var(--text-secondary);
+ font-size: 0.8rem;
+ font-family: inherit;
+ cursor: pointer;
+ appearance: none;
+ -webkit-appearance: none;
+ padding-right: 28px;
+ background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%2394a3b8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
+ background-repeat: no-repeat;
+ background-position: right 10px center;
+}
+
+.rs-new-btn {
+ padding: 8px 20px;
+ border: none;
+ border-radius: 10px;
+ background: var(--accent-color);
+ color: white;
+ font-size: 0.85rem;
+ font-weight: 600;
+ cursor: pointer;
+ transition: all 0.2s ease;
+ font-family: inherit;
+ box-shadow: 0 2px 8px rgba(37, 99, 235, 0.25);
+}
+
+.rs-new-btn:hover {
+ background: var(--accent-hover);
+ transform: translateY(-1px);
+ box-shadow: 0 4px 12px rgba(37, 99, 235, 0.35);
+}
+
+/* Rules Grid */
+.rs-grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
+ gap: 16px;
+}
+
+.rs-empty {
+ grid-column: 1 / -1;
+ text-align: center;
+ padding: 60px 20px;
+ color: var(--text-secondary);
+ font-size: 0.95rem;
+}
+
+/* Rule Card */
+.rs-card {
+ background: white;
+ border: 1px solid rgba(226, 232, 240, 0.6);
+ border-radius: 20px;
+ padding: 20px;
+ cursor: pointer;
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+}
+
+.rs-card:hover {
+ border-color: var(--accent-border);
+ box-shadow: 0 8px 30px rgba(191, 219, 254, 0.3);
+ transform: translateY(-2px);
+}
+
+.rs-card-header {
+ display: flex;
+ align-items: flex-start;
+ justify-content: space-between;
+ margin-bottom: 12px;
+}
+
+.rs-card-id {
+ font-family: 'SF Mono', 'Fira Code', monospace;
+ font-size: 0.8rem;
+ color: var(--text-tertiary);
+ background: var(--surface);
+ padding: 2px 8px;
+ border-radius: 6px;
+}
+
+.rs-card-version {
+ font-size: 1.1rem;
+ font-weight: 700;
+ color: var(--text-primary);
+ margin-bottom: 4px;
+}
+
+.rs-card-desc {
+ font-size: 0.8rem;
+ color: var(--text-secondary);
+ margin-bottom: 12px;
+ line-height: 1.4;
+ display: -webkit-box;
+ -webkit-line-clamp: 2;
+ -webkit-box-orient: vertical;
+ overflow: hidden;
+}
+
+.rs-card-badges {
+ display: flex;
+ gap: 6px;
+ flex-wrap: wrap;
+ align-items: center;
+}
+
+.rs-badge {
+ padding: 3px 10px;
+ border-radius: 20px;
+ font-size: 0.7rem;
+ font-weight: 600;
+ text-transform: uppercase;
+ letter-spacing: 0.5px;
+}
+
+.rs-badge.active {
+ background: #dcfce7;
+ color: #16a34a;
+}
+
+.rs-badge.archived {
+ background: #fef3c7;
+ color: #d97706;
+}
+
+.rs-badge.inactive {
+ background: var(--surface);
+ color: var(--text-tertiary);
+}
+
+.rs-card-footer {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ margin-top: 12px;
+ padding-top: 12px;
+ border-top: 1px solid rgba(226, 232, 240, 0.4);
+}
+
+.rs-card-items-count {
+ font-size: 0.75rem;
+ color: var(--text-tertiary);
+}
+
+.rs-card-date {
+ font-size: 0.75rem;
+ color: var(--text-tertiary);
+}
+
+.rs-card-tags {
+ display: flex;
+ gap: 4px;
+ flex-wrap: wrap;
+ margin-bottom: 8px;
+}
+
+.rs-tag {
+ padding: 2px 8px;
+ border-radius: 6px;
+ font-size: 0.7rem;
+ background: rgba(241, 245, 249, 0.8);
+ color: var(--text-secondary);
+}
+
+/* ===== POPUP OVERLAY ===== */
+.rs-overlay {
+ position: fixed;
+ inset: 0;
+ background: rgba(15, 23, 42, 0.4);
+ backdrop-filter: blur(8px);
+ -webkit-backdrop-filter: blur(8px);
+ z-index: 2000;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ padding: 24px;
+}
+
+.rs-popup {
+ background: white;
+ border-radius: 24px;
+ width: 700px;
+ max-width: 95vw;
+ max-height: 85vh;
+ overflow-y: auto;
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
+ border: 1px solid var(--header-border);
+}
+
+.rs-popup-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ padding: 24px 28px 16px;
+ border-bottom: 1px solid var(--header-border);
+ position: sticky;
+ top: 0;
+ background: white;
+ border-radius: 24px 24px 0 0;
+ z-index: 1;
+}
+
+.rs-popup-header-left {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+}
+
+.rs-popup-close {
+ width: 32px;
+ height: 32px;
+ border: none;
+ background: var(--surface);
+ border-radius: 8px;
+ cursor: pointer;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ color: var(--text-secondary);
+ font-size: 1.2rem;
+ transition: all 0.2s ease;
+}
+
+.rs-popup-close:hover {
+ background: #fee2e2;
+ color: #ef4444;
+}
+
+.rs-popup-body {
+ padding: 20px 28px 28px;
+}
+
+/* Popup sections */
+.rs-section {
+ margin-bottom: 20px;
+}
+
+.rs-section-title {
+ font-size: 0.75rem;
+ font-weight: 600;
+ text-transform: uppercase;
+ letter-spacing: 0.8px;
+ color: var(--text-tertiary);
+ margin-bottom: 8px;
+}
+
+/* Inline editable fields */
+.rs-field-row {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ margin-bottom: 8px;
+}
+
+.rs-field-label {
+ font-size: 0.8rem;
+ color: var(--text-secondary);
+ min-width: 80px;
+ font-weight: 500;
+}
+
+.rs-field-input {
+ flex: 1;
+ padding: 8px 12px;
+ border: 1px solid var(--header-border);
+ border-radius: 8px;
+ font-size: 0.85rem;
+ font-family: inherit;
+ color: var(--text-primary);
+ transition: border-color 0.2s ease;
+ background: white;
+}
+
+.rs-field-input:focus {
+ outline: none;
+ border-color: var(--accent-color);
+ box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
+}
+
+.rs-field-textarea {
+ width: 100%;
+ padding: 10px 12px;
+ border: 1px solid var(--header-border);
+ border-radius: 8px;
+ font-size: 0.85rem;
+ font-family: inherit;
+ color: var(--text-primary);
+ resize: vertical;
+ min-height: 60px;
+ transition: border-color 0.2s ease;
+}
+
+.rs-field-textarea:focus {
+ outline: none;
+ border-color: var(--accent-color);
+ box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
+}
+
+/* Action buttons row */
+.rs-actions {
+ display: flex;
+ gap: 8px;
+ flex-wrap: wrap;
+}
+
+.rs-action-btn {
+ padding: 6px 14px;
+ border: 1px solid var(--header-border);
+ border-radius: 8px;
+ font-size: 0.8rem;
+ font-weight: 500;
+ cursor: pointer;
+ transition: all 0.2s ease;
+ font-family: inherit;
+ background: white;
+ color: var(--text-secondary);
+}
+
+.rs-action-btn:hover {
+ border-color: var(--accent-border);
+ color: var(--text-primary);
+}
+
+.rs-action-btn.active-toggle {
+ background: #dcfce7;
+ color: #16a34a;
+ border-color: #bbf7d0;
+}
+
+.rs-action-btn.active-toggle.off {
+ background: white;
+ color: var(--text-secondary);
+ border-color: var(--header-border);
+}
+
+.rs-action-btn.archive-toggle.on {
+ background: #fef3c7;
+ color: #d97706;
+ border-color: #fde68a;
+}
+
+.rs-action-btn.danger {
+ color: #ef4444;
+ border-color: #fecaca;
+}
+
+.rs-action-btn.danger:hover {
+ background: #fef2f2;
+ border-color: #fca5a5;
+}
+
+/* Items section */
+.rs-items-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ margin-bottom: 8px;
+}
+
+.rs-add-item-btn {
+ padding: 4px 12px;
+ border: 1px dashed var(--accent-border);
+ border-radius: 8px;
+ background: var(--accent-light);
+ color: var(--accent-color);
+ font-size: 0.8rem;
+ font-weight: 600;
+ cursor: pointer;
+ transition: all 0.2s ease;
+ font-family: inherit;
+}
+
+.rs-add-item-btn:hover {
+ background: var(--accent-color);
+ color: white;
+ border-style: solid;
+}
+
+/* Item row */
+.rs-item {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 10px 12px;
+ background: var(--surface);
+ border-radius: 10px;
+ margin-bottom: 6px;
+ transition: all 0.2s ease;
+}
+
+.rs-item:hover {
+ background: rgba(241, 245, 249, 1);
+}
+
+.rs-item-field {
+ padding: 5px 8px;
+ border: 1px solid transparent;
+ border-radius: 6px;
+ font-size: 0.8rem;
+ font-family: inherit;
+ color: var(--text-primary);
+ background: transparent;
+ transition: all 0.2s ease;
+ min-width: 0;
+}
+
+.rs-item-field:focus {
+ border-color: var(--accent-color);
+ background: white;
+ outline: none;
+}
+
+.rs-item-field.narrow {
+ width: 60px;
+ flex-shrink: 0;
+}
+
+.rs-item-field.medium {
+ width: 100px;
+ flex-shrink: 0;
+}
+
+.rs-item-field.wide {
+ flex: 1;
+ min-width: 80px;
+}
+
+/* Toggle switch for item enabled */
+.rs-toggle {
+ width: 36px;
+ height: 20px;
+ background: #cbd5e1;
+ border-radius: 10px;
+ position: relative;
+ cursor: pointer;
+ transition: background 0.2s ease;
+ flex-shrink: 0;
+}
+
+.rs-toggle.on {
+ background: #22c55e;
+}
+
+.rs-toggle::after {
+ content: '';
+ position: absolute;
+ width: 16px;
+ height: 16px;
+ background: white;
+ border-radius: 50%;
+ top: 2px;
+ left: 2px;
+ transition: transform 0.2s ease;
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
+}
+
+.rs-toggle.on::after {
+ transform: translateX(16px);
+}
+
+.rs-item-delete {
+ width: 24px;
+ height: 24px;
+ border: none;
+ background: transparent;
+ color: var(--text-tertiary);
+ cursor: pointer;
+ border-radius: 6px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ font-size: 1rem;
+ flex-shrink: 0;
+ transition: all 0.2s ease;
+}
+
+.rs-item-delete:hover {
+ background: #fef2f2;
+ color: #ef4444;
+}
+
+/* Tags input */
+.rs-tags-wrap {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 6px;
+ align-items: center;
+ padding: 6px;
+ border: 1px solid var(--header-border);
+ border-radius: 8px;
+ min-height: 38px;
+ background: white;
+}
+
+.rs-tags-wrap:focus-within {
+ border-color: var(--accent-color);
+ box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
+}
+
+.rs-tag-chip {
+ display: flex;
+ align-items: center;
+ gap: 4px;
+ padding: 3px 8px;
+ background: var(--accent-light);
+ color: var(--accent-color);
+ border-radius: 6px;
+ font-size: 0.75rem;
+ font-weight: 500;
+}
+
+.rs-tag-chip button {
+ border: none;
+ background: none;
+ color: inherit;
+ cursor: pointer;
+ padding: 0;
+ font-size: 0.85rem;
+ opacity: 0.6;
+ line-height: 1;
+}
+
+.rs-tag-chip button:hover {
+ opacity: 1;
+}
+
+.rs-tag-input {
+ border: none;
+ outline: none;
+ font-size: 0.8rem;
+ font-family: inherit;
+ flex: 1;
+ min-width: 80px;
+ padding: 2px 4px;
+ background: transparent;
+ color: var(--text-primary);
+}
+
+/* Saving indicator */
+.rs-saving {
+ font-size: 0.75rem;
+ color: var(--accent-color);
+ opacity: 0;
+ transition: opacity 0.2s ease;
+}
+
+.rs-saving.visible {
+ opacity: 1;
+}
+
+/* Item field labels header */
+.rs-item-labels {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 0 12px 4px;
+ font-size: 0.7rem;
+ color: var(--text-tertiary);
+ text-transform: uppercase;
+ letter-spacing: 0.5px;
+ font-weight: 600;
+}
+
+.rs-item-labels span.narrow { width: 60px; flex-shrink: 0; }
+.rs-item-labels span.medium { width: 100px; flex-shrink: 0; }
+.rs-item-labels span.wide { flex: 1; min-width: 80px; }
+.rs-item-labels span.toggle-space { width: 36px; flex-shrink: 0; }
+.rs-item-labels span.delete-space { width: 24px; flex-shrink: 0; }
+
+/* Confirm dialog */
+.rs-confirm-overlay {
+ position: fixed;
+ inset: 0;
+ background: rgba(15, 23, 42, 0.5);
+ backdrop-filter: blur(4px);
+ z-index: 3000;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+}
+
+.rs-confirm-box {
+ background: white;
+ border-radius: 16px;
+ padding: 24px;
+ width: 360px;
+ max-width: 90vw;
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
+ text-align: center;
+}
+
+.rs-confirm-box h3 {
+ margin-bottom: 8px;
+ font-size: 1rem;
+ color: var(--text-primary);
+}
+
+.rs-confirm-box p {
+ font-size: 0.85rem;
+ color: var(--text-secondary);
+ margin-bottom: 20px;
+}
+
+.rs-confirm-actions {
+ display: flex;
+ gap: 8px;
+ justify-content: center;
+}
+
+.rs-confirm-actions button {
+ padding: 8px 20px;
+ border-radius: 8px;
+ font-size: 0.85rem;
+ font-weight: 600;
+ cursor: pointer;
+ font-family: inherit;
+ border: 1px solid var(--header-border);
+ background: white;
+ color: var(--text-secondary);
+ transition: all 0.2s ease;
+}
+
+.rs-confirm-actions button.confirm-danger {
+ background: #ef4444;
+ color: white;
+ border-color: #ef4444;
+}
+
+.rs-confirm-actions button.confirm-danger:hover {
+ background: #dc2626;
+}
+
+/* Back link */
+.rs-back {
+ display: inline-flex;
+ align-items: center;
+ gap: 6px;
+ color: var(--text-secondary);
+ text-decoration: none;
+ font-size: 0.85rem;
+ font-weight: 500;
+ transition: color 0.2s ease;
+}
+
+.rs-back:hover {
+ color: var(--accent-color);
+}
diff --git a/realtime/.env.example b/realtime/.env.example
index eabfd71..149e057 100644
--- a/realtime/.env.example
+++ b/realtime/.env.example
@@ -5,3 +5,4 @@ VERSION_STATE=beta
INFLX_URL=
INFLX_TOKEN=
INFLX_ORG=
+INFLX_BUCKET=
\ No newline at end of file
diff --git a/realtime/src/routes/sessions.js b/realtime/src/routes/sessions.js
index 9298488..0eb27b7 100644
--- a/realtime/src/routes/sessions.js
+++ b/realtime/src/routes/sessions.js
@@ -1,9 +1,9 @@
const router = require('express').Router();
-const { queryAll, query } = require('../store/redis');
+const { queryAll, query, hset } = require('../store/redis');
+const { connectedSensors } = require('../ws/handler');
/**
- * GET /sessions — Lista tutte le sessioni attive dei sensori.
- * Legge da Redis le chiavi sensors:* (scritte da handler.js alla connessione)
+ * GET /sessions — Lista tutte le sessioni dei sensori con metadata e rules versions
*/
router.get('/', async (req, res) => {
try {
@@ -16,7 +16,13 @@ router.get('/', async (req, res) => {
name,
connectedAt: info.timestamp || null,
session: info.session || null,
+ sessionLabel: info.sessionLabel || info.session || null,
status: info.status || 'unknown',
+ rules: {
+ weather: info.rules_weather || null,
+ data: info.rules_data || null,
+ logs: info.rules_logs || null,
+ }
};
}
res.json(sessions);
@@ -27,8 +33,7 @@ router.get('/', async (req, res) => {
});
/**
- * GET /sessions/pending — Lista token di connessione pendenti.
- * Legge da Redis le chiavi sensors_pending:* (create da createConnectionToken)
+ * GET /sessions/pending — Lista token di connessione pendenti
*/
router.get('/pending', async (req, res) => {
try {
@@ -41,8 +46,7 @@ router.get('/pending', async (req, res) => {
});
/**
- * GET /sessions/connected — Lista sensori attualmente connessi.
- * Legge da Redis le chiavi sensor:* (scritte da appendAsConnection in handler.js)
+ * GET /sessions/connected — Lista sensori attualmente connessi
*/
router.get('/connected', async (req, res) => {
try {
@@ -63,7 +67,7 @@ router.get('/connected', async (req, res) => {
});
/**
- * GET /sessions/connected/:id — Verifica se un sensore specifico è connesso.
+ * GET /sessions/connected/:id — Verifica se un sensore specifico è connesso
*/
router.get('/connected/:id', async (req, res) => {
const { id } = req.params;
@@ -79,4 +83,38 @@ router.get('/connected/:id', async (req, res) => {
}
});
+/**
+ * POST /sessions/:id/label — Cambia il label della sessione per un sensore connesso.
+ * Non interrompe il flusso dati. I nuovi punti InfluxDB avranno il nuovo tag.
+ */
+router.post('/:id/label', async (req, res) => {
+ const { id } = req.params;
+ const { label } = req.body;
+
+ if (!label || typeof label !== 'string' || label.trim().length === 0) {
+ return res.status(400).json({ error: 'label is required' });
+ }
+
+ const trimmedLabel = label.trim();
+
+ // Trova il WS client connesso
+ const ws = connectedSensors.get(id);
+ if (!ws) {
+ return res.status(404).json({ error: 'sensor not connected' });
+ }
+
+ // Aggiorna in memoria (effetto immediato sui prossimi punti InfluxDB)
+ ws.sessionLabel = trimmedLabel;
+
+ // Aggiorna in Redis per persistenza
+ try {
+ await hset(`sensors:${id}`, 'sessionLabel', trimmedLabel);
+ } catch (err) {
+ console.error('Error updating session label in Redis', err);
+ }
+
+ console.log(`[${id}] Session label changed to: ${trimmedLabel}`);
+ res.json({ status: 'ok', label: trimmedLabel });
+});
+
module.exports = router;
diff --git a/realtime/src/ws/handler.js b/realtime/src/ws/handler.js
index 364e2d7..2f644d2 100644
--- a/realtime/src/ws/handler.js
+++ b/realtime/src/ws/handler.js
@@ -3,8 +3,9 @@ const { decode } = require('@msgpack/msgpack');
const { consumeConnectionToken, appendAsConnection, query, hset, del } = require('../store/redis');
const { writeSensorData, queryHistory } = require('../store/influx');
-// In-memory map: sensorName → Set
-const sensorWatchers = new Map();
+// In-memory registries
+const sensorWatchers = new Map(); // sensorName → Set (watchers)
+const connectedSensors = new Map(); // sensorName → WebSocket (sensor clients)
function generateSessionId() {
const num = Math.floor(1000 + Math.random() * 9000);
@@ -71,13 +72,13 @@ function setup(server) {
wss.handleUpgrade(req, socket, head, (ws) => {
ws.sensorName = sensor;
ws.sessionId = generateSessionId();
+ ws.sessionLabel = ws.sessionId; // default label = sessionId
ws.connectedAt = new Date().toISOString();
+ ws.rulesVersions = null; // populated by _t:init message
handleSensorConnection(ws);
});
} else if (path === '/live') {
- // Accept upgrade without requiring query params.
- // The console sends { action: 'watch', sensorId } after connecting.
wss.handleUpgrade(req, socket, head, (ws) => {
handleWatcherConnection(ws);
});
@@ -90,11 +91,14 @@ function setup(server) {
}
function handleSensorConnection(ws) {
- const { sensorName, sessionId, connectedAt } = ws;
+ const { sensorName, sessionId, sessionLabel, connectedAt } = ws;
console.log(`Sensor connected: ${sensorName} (session: ${sessionId})`);
+ // Register in global registry
+ connectedSensors.set(sensorName, ws);
+
appendAsConnection(sensorName, 'connected', connectedAt);
- hset(`sensors:${sensorName}`, 'session', sessionId);
+ hset(`sensors:${sensorName}`, 'session', sessionId, 'sessionLabel', sessionLabel);
const pingInterval = setInterval(() => {
if (ws.readyState === ws.OPEN) ws.ping();
@@ -103,11 +107,28 @@ function handleSensorConnection(ws) {
ws.on('message', (data) => {
try {
const packet = decode(data);
+
+ // Messaggio di inizializzazione con versioni rulesets
+ if (packet._t === 'init') {
+ ws.rulesVersions = packet.rules || {};
+ console.log(`[${sensorName}] Rules versions:`, ws.rulesVersions);
+ // Salva in Redis
+ const rulesFields = [];
+ for (const [type, ver] of Object.entries(ws.rulesVersions)) {
+ rulesFields.push(`rules_${type}`, ver);
+ }
+ if (rulesFields.length > 0) {
+ hset(`sensors:${sensorName}`, ...rulesFields);
+ }
+ return; // non scrivere su InfluxDB
+ }
+
const { ts, _m, ...fields } = packet;
- writeSensorData(fields, sensorName, sessionId, ts);
+ // Usa sessionLabel (puo' cambiare a runtime dalla console)
+ writeSensorData(fields, sensorName, ws.sessionLabel, ts);
- // Broadcast to watchers as JSON messages grouped by measurement
+ // Broadcast to watchers
const watchers = sensorWatchers.get(sensorName);
if (watchers && watchers.size > 0) {
const messages = transformPacket(packet);
@@ -128,6 +149,7 @@ function handleSensorConnection(ws) {
ws.on('close', () => {
console.log(`Sensor disconnected: ${sensorName}`);
clearInterval(pingInterval);
+ connectedSensors.delete(sensorName);
appendAsConnection(sensorName, 'disconnected', new Date().toISOString());
del(`sensors:${sensorName}`);
});
@@ -145,7 +167,6 @@ function handleWatcherConnection(ws) {
const msg = JSON.parse(data.toString());
if (msg.action === 'watch' && msg.sensorId) {
- // Unwatch previous sensor if any
if (ws.sensorName) {
sensorWatchers.get(ws.sensorName)?.delete(ws);
if (sensorWatchers.get(ws.sensorName)?.size === 0) {
@@ -155,7 +176,6 @@ function handleWatcherConnection(ws) {
ws.sensorName = msg.sensorId;
- // Register as watcher
if (!sensorWatchers.has(msg.sensorId)) {
sensorWatchers.set(msg.sensorId, new Set());
}
@@ -163,14 +183,12 @@ function handleWatcherConnection(ws) {
console.log(`Watcher now watching sensor: ${msg.sensorId}`);
- // Send history since sensor connected
try {
const sensorInfo = await query(msg.sensorId, 'sensors');
if (sensorInfo && sensorInfo.timestamp && sensorInfo.session) {
const history = await queryHistory(msg.sensorId, sensorInfo.session, sensorInfo.timestamp);
for (const row of history) {
const ts = new Date(row._time).getTime();
- // Send each historical row as individual messages grouped by measurement
const rebuilt = { ts };
for (const [short, { key }] of Object.entries(fieldMapping)) {
const influxField = { t: 'temperature', h: 'humidity', spd: 'speed', cog: 'cog', sog: 'sog', hdg: 'headingTrue', lat: 'latitude', lon: 'longitude' }[short];
@@ -217,4 +235,4 @@ function handleWatcherConnection(ws) {
});
}
-module.exports = { setup };
+module.exports = { setup, connectedSensors };