Files
Central_monitoreo/lib/db/database_helper.dart
T
PablinuxandClaude Sonnet 4.6 21d2c20525 feat: commit inicial — Central Monitoreo v1.0.0
- OperadorLoginPage con auth WebControl 2 pasos (empresas → login)
- ShellPage: sidebar 220px + IndexedStack + header con estado API
- DashboardPage: stat cards, feed alarmas, clientes, incidentes (timer 30s)
- ApiAuthService: flujo real con email + paswd + dba → token + apiKey
- DB SQLite v1: 7 tablas + 7 DAOs completos
- theme.dart: AppColors, appHeadline (Manrope), appLabel (Inter)
- Logo app: escudo radar neón (SVG + PNG 512px) en lib/recursos/
- desplegar.sh: build release Linux + upload a siax-system.net
- agents.md: estado completo + objetivos futuros documentados

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 22:11:12 -05:00

127 lines
3.7 KiB
Dart

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
class DatabaseHelper {
static const _dbName = 'central_monitoreo.db';
static const _dbVersion = 1;
DatabaseHelper._();
static final DatabaseHelper instance = DatabaseHelper._();
Database? _db;
Future<Database> get db async {
_db ??= await _initDb();
return _db!;
}
Future<Database> _initDb() async {
if (!kIsWeb && (Platform.isLinux || Platform.isWindows)) {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
}
final path = await _dbPath();
return openDatabase(path, version: _dbVersion, onCreate: _onCreate);
}
Future<String> _dbPath() async {
if (!kIsWeb && Platform.isLinux) {
final home = Platform.environment['HOME'] ?? '';
final dir = Directory('$home/.local/share/central_monitoreo');
if (!dir.existsSync()) dir.createSync(recursive: true);
return join(dir.path, _dbName);
}
final base = await getDatabasesPath();
return join(base, _dbName);
}
Future<void> _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE configuracion (
clave TEXT PRIMARY KEY,
valor TEXT
)
''');
await db.execute('''
CREATE TABLE sesiones_operador (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT NOT NULL,
usuario TEXT NOT NULL,
inicio TEXT NOT NULL,
fin TEXT,
activa INTEGER NOT NULL DEFAULT 1
)
''');
await db.execute('''
CREATE TABLE cache_clientes (
id INTEGER PRIMARY KEY,
nombre TEXT NOT NULL,
estado TEXT NOT NULL DEFAULT 'normal',
alarmas_activas INTEGER NOT NULL DEFAULT 0,
alarmas_criticas INTEGER NOT NULL DEFAULT 0,
ultima_actualizacion TEXT
)
''');
await db.execute('''
CREATE TABLE cache_alarmas (
id INTEGER PRIMARY KEY,
cliente_id INTEGER NOT NULL,
cliente_nombre TEXT,
tipo TEXT NOT NULL,
severidad INTEGER NOT NULL DEFAULT 1,
estado TEXT NOT NULL DEFAULT 'activa',
zona_nombre TEXT,
timestamp TEXT NOT NULL,
notas TEXT
)
''');
await db.execute('''
CREATE TABLE incidentes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
alarma_id INTEGER,
cliente_id INTEGER,
cliente_nombre TEXT,
tipo TEXT NOT NULL,
operador TEXT NOT NULL,
notas TEXT,
timestamp TEXT NOT NULL,
resuelto INTEGER NOT NULL DEFAULT 0,
sincronizado INTEGER NOT NULL DEFAULT 0
)
''');
await db.execute('''
CREATE TABLE operadores (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT NOT NULL,
iniciales TEXT NOT NULL,
nivel INTEGER NOT NULL DEFAULT 1,
activo INTEGER NOT NULL DEFAULT 1,
en_turno INTEGER NOT NULL DEFAULT 0
)
''');
await db.execute('''
CREATE TABLE sync_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tipo TEXT NOT NULL,
estado TEXT NOT NULL,
detalle TEXT,
timestamp TEXT NOT NULL
)
''');
// Configuración por defecto
await db.insert('configuracion', {'clave': 'poll_interval_seg', 'valor': '30'});
await db.insert('configuracion', {'clave': 'sonido_alerta', 'valor': '1'});
}
}