feat: conectar alarmas, mensajes y accesos a BD real
- MonitoreoAlarmaPage: alarmas activas/resueltas desde AlarmasDao, alarma más crítica en tiempo real, log completo con zona y estado, pull-to-refresh, estado vacío - MensajeriaPage: mensajes desde MensajesDao, envío guarda en BD, botones pánico y soporte también persisten en BD, estado vacío - ControlAccesoPage: accesos desde AccesosDao con JOIN a usuarios y puertas, método de acceso, autorizado/denegado, pull-to-refresh, estado vacío - AlarmasDao: contarResueltasUltimas24h, getActivasConZona, getAllConZona - AccesosDao: getRecientesConDetalle (JOIN usuarios + puertas) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
49302dfd23
commit
f566139e0a
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../theme.dart';
|
||||
import '../db/dao/alarmas_dao.dart';
|
||||
|
||||
class MonitoreoAlarmaPage extends StatefulWidget {
|
||||
const MonitoreoAlarmaPage({Key? key}) : super(key: key);
|
||||
@@ -18,9 +19,17 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
late final Animation<double> _pingScale;
|
||||
late final Animation<double> _pingOpacity;
|
||||
|
||||
final _dao = AlarmasDao();
|
||||
int _contActivas = 0;
|
||||
int _contResueltas24h = 0;
|
||||
List<Map<String, dynamic>> _activas = [];
|
||||
List<Map<String, dynamic>> _log = [];
|
||||
bool _cargando = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_cargar();
|
||||
|
||||
_pulseCtrl = AnimationController(vsync: this,
|
||||
duration: const Duration(seconds: 2))..repeat();
|
||||
@@ -41,6 +50,24 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
.animate(CurvedAnimation(parent: _pingCtrl, curve: Curves.easeOut));
|
||||
}
|
||||
|
||||
Future<void> _cargar() async {
|
||||
setState(() => _cargando = true);
|
||||
try {
|
||||
final activas = await _dao.getActivasConZona();
|
||||
final log = await _dao.getAllConZona(limit: 20);
|
||||
final contActivas = await _dao.contarActivas();
|
||||
final contResueltas = await _dao.contarResueltasUltimas24h();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_activas = activas;
|
||||
_log = log;
|
||||
_contActivas = contActivas;
|
||||
_contResueltas24h = contResueltas;
|
||||
});
|
||||
} catch (_) {}
|
||||
if (mounted) setState(() => _cargando = false);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pulseCtrl.dispose();
|
||||
@@ -48,22 +75,82 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// ── Helpers ─────────────────────────────────────────────────────────────────
|
||||
String _tipoLabel(String tipo) {
|
||||
switch (tipo) {
|
||||
case 'intrusion': return 'INTRUSIÓN';
|
||||
case 'humo': return 'HUMO';
|
||||
case 'movimiento':return 'MOVIMIENTO';
|
||||
case 'acceso': return 'ACCESO';
|
||||
default: return tipo.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
Color _severidadColor(int sev) {
|
||||
if (sev >= 5) return AppColors.error;
|
||||
if (sev >= 4) return AppColors.tertiary;
|
||||
if (sev >= 3) return AppColors.secondary;
|
||||
return AppColors.onSurfaceVariant;
|
||||
}
|
||||
|
||||
IconData _tipoIcon(String tipo) {
|
||||
switch (tipo) {
|
||||
case 'intrusion': return Icons.warning_amber;
|
||||
case 'humo': return Icons.local_fire_department;
|
||||
case 'movimiento':return Icons.directions_run;
|
||||
case 'acceso': return Icons.lock_open;
|
||||
default: return Icons.notifications;
|
||||
}
|
||||
}
|
||||
|
||||
String _horaCorta(String iso) {
|
||||
try {
|
||||
final dt = DateTime.parse(iso).toLocal();
|
||||
final h = dt.hour.toString().padLeft(2, '0');
|
||||
final m = dt.minute.toString().padLeft(2, '0');
|
||||
return '$h:$m';
|
||||
} catch (_) { return ''; }
|
||||
}
|
||||
|
||||
String _badgeEstado(String estado) {
|
||||
switch (estado) {
|
||||
case 'activa': return 'ACTIVA';
|
||||
case 'resuelta': return 'RESUELTO';
|
||||
case 'silenciada':return 'SILENCIADA';
|
||||
default: return estado.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
Color _badgeColor(String estado) {
|
||||
switch (estado) {
|
||||
case 'activa': return AppColors.error;
|
||||
case 'resuelta': return AppColors.primary;
|
||||
case 'silenciada':return AppColors.onSurfaceVariant;
|
||||
default: return AppColors.onSurfaceVariant;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
primary: false,
|
||||
padding: const EdgeInsets.fromLTRB(24, 24, 24, 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_statusBento(),
|
||||
const SizedBox(height: 32),
|
||||
_criticalEvents(),
|
||||
const SizedBox(height: 32),
|
||||
_eventLog(),
|
||||
const SizedBox(height: 32),
|
||||
_tacticalView(),
|
||||
],
|
||||
return RefreshIndicator(
|
||||
onRefresh: _cargar,
|
||||
color: AppColors.primary,
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
primary: false,
|
||||
padding: const EdgeInsets.fromLTRB(24, 24, 24, 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_statusBento(),
|
||||
const SizedBox(height: 32),
|
||||
_criticalEvents(),
|
||||
const SizedBox(height: 32),
|
||||
_eventLog(),
|
||||
const SizedBox(height: 32),
|
||||
_tacticalView(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -74,10 +161,14 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
const SizedBox(height: 16),
|
||||
Row(children: [
|
||||
Expanded(child: _miniStatCard(
|
||||
'ALARMAS ACTIVAS', '03', Icons.warning, AppColors.error)),
|
||||
'ALARMAS ACTIVAS',
|
||||
_cargando ? '—' : '$_contActivas',
|
||||
Icons.warning, AppColors.error)),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(child: _miniStatCard(
|
||||
'RESUELTAS 24H', '142', Icons.check_circle, AppColors.primary)),
|
||||
'RESUELTAS 24H',
|
||||
_cargando ? '—' : '$_contResueltas24h',
|
||||
Icons.check_circle, AppColors.primary)),
|
||||
]),
|
||||
]);
|
||||
|
||||
@@ -147,23 +238,46 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
]),
|
||||
);
|
||||
|
||||
// ── Gráfico de frecuencia ───────────────────────────────────────────────────
|
||||
// ── Eventos críticos ────────────────────────────────────────────────────────
|
||||
Widget _criticalEvents() => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(children: [
|
||||
const Icon(Icons.priority_high, color: AppColors.error, size: 16),
|
||||
const SizedBox(width: 8),
|
||||
Text('EVENTOS CRÍTICOS',
|
||||
style: appHeadline(size: 13, color: AppColors.error, spacing: 1.5)),
|
||||
]),
|
||||
const SizedBox(height: 16),
|
||||
// ── Alarma más crítica ───────────────────────────────────────────────────────
|
||||
Widget _criticalEvents() {
|
||||
final alarma = _activas.isNotEmpty ? _activas.first : null;
|
||||
final color = alarma != null
|
||||
? _severidadColor(alarma['severidad'] as int)
|
||||
: AppColors.onSurfaceVariant;
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(children: [
|
||||
Icon(Icons.priority_high, color: color, size: 16),
|
||||
const SizedBox(width: 8),
|
||||
Text('ALARMA MÁS CRÍTICA',
|
||||
style: appHeadline(size: 13, color: color, spacing: 1.5)),
|
||||
]),
|
||||
const SizedBox(height: 16),
|
||||
if (alarma == null)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(color: AppColors.surfaceHigh,
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
child: Column(children: [
|
||||
Icon(Icons.shield_outlined, color: AppColors.primary, size: 32),
|
||||
const SizedBox(height: 10),
|
||||
Text('Sin alarmas activas',
|
||||
style: appHeadline(size: 14, color: AppColors.primary)),
|
||||
const SizedBox(height: 4),
|
||||
Text('Todos los sectores en estado normal',
|
||||
style: appLabel(size: 12, color: AppColors.onSurfaceVariant)),
|
||||
]),
|
||||
)
|
||||
else
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: IntrinsicHeight(
|
||||
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||
Container(width: 6, color: AppColors.error),
|
||||
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Container(width: 6, color: color),
|
||||
Expanded(
|
||||
child: Container(
|
||||
color: AppColors.surfaceHigh,
|
||||
@@ -174,18 +288,23 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
Expanded(child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('INTRUSIÓN NIVEL 5',
|
||||
style: appLabel(size: 10, weight: FontWeight.bold,
|
||||
color: AppColors.error, spacing: 1.5)),
|
||||
Text(
|
||||
'${_tipoLabel(alarma['tipo'] as String)} · NIVEL ${alarma['severidad']}',
|
||||
style: appLabel(size: 10, weight: FontWeight.bold,
|
||||
color: color, spacing: 1.5),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text('ALMACÉN PRINCIPAL G-4',
|
||||
style: appHeadline(size: 18)),
|
||||
Text(
|
||||
(alarma['zona_nombre'] as String? ?? 'Zona desconocida').toUpperCase(),
|
||||
style: appHeadline(size: 18),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(children: [
|
||||
const Icon(Icons.schedule, size: 14,
|
||||
color: AppColors.onSurfaceVariant),
|
||||
const SizedBox(width: 6),
|
||||
Text('02:14:45', style: appLabel(size: 12)),
|
||||
Text(_horaCorta(alarma['timestamp'] as String),
|
||||
style: appLabel(size: 12)),
|
||||
]),
|
||||
],
|
||||
)),
|
||||
@@ -193,8 +312,8 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
ElevatedButton(
|
||||
onPressed: () {},
|
||||
style: ElevatedButton.styleFrom(
|
||||
primary: AppColors.error,
|
||||
onPrimary: AppColors.onError,
|
||||
primary: color,
|
||||
onPrimary: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 10),
|
||||
shape: RoundedRectangleBorder(
|
||||
@@ -203,7 +322,7 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
),
|
||||
child: Text('RESPONDER',
|
||||
style: appLabel(size: 12, weight: FontWeight.bold,
|
||||
color: AppColors.onError)),
|
||||
color: Colors.white)),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -212,56 +331,85 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// ── Registro de eventos ─────────────────────────────────────────────────────
|
||||
// ── Registro de alarmas ─────────────────────────────────────────────────────
|
||||
Widget _eventLog() => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
|
||||
Text('REGISTRO DE EVENTOS',
|
||||
style: appHeadline(size: 13, spacing: 1.5)),
|
||||
Text('Ver Historial',
|
||||
style: appLabel(size: 12, weight: FontWeight.bold,
|
||||
color: AppColors.primary)),
|
||||
]),
|
||||
const SizedBox(height: 24),
|
||||
_logEntry(icon: Icons.lock_open, iconBg: AppColors.surfaceHighest,
|
||||
iconColor: AppColors.secondary,
|
||||
title: 'Acceso Autorizado', badge: 'SEGURO',
|
||||
badgeTextColor: AppColors.onSurfaceVariant, badgeBg: AppColors.surfaceLow,
|
||||
description: 'Entrada: Muelle Trasero - Usuario: #0824',
|
||||
time: '10:45:12', showLine: true),
|
||||
_logEntry(icon: Icons.local_fire_department,
|
||||
iconBg: AppColors.tertiaryContainer, iconColor: AppColors.tertiary,
|
||||
title: 'Humo Detectado', badge: 'URGENTE',
|
||||
badgeTextColor: AppColors.tertiary, badgeBg: AppColors.tertiaryContainer,
|
||||
description: 'Sensor #402 - Sala de Servidores A',
|
||||
time: '09:12:05', showLine: true),
|
||||
_logEntry(icon: Icons.videocam, iconBg: AppColors.surfaceHighest,
|
||||
iconColor: AppColors.primary,
|
||||
title: 'Detección de Movimiento', badge: 'RESUELTO',
|
||||
badgeTextColor: AppColors.onSurfaceVariant, badgeBg: AppColors.surfaceLow,
|
||||
description: 'Cámara 12 - Cerca Perimetral Sur',
|
||||
time: '08:55:30', showLine: false),
|
||||
Text('REGISTRO DE ALARMAS',
|
||||
style: appHeadline(size: 13, spacing: 1.5)),
|
||||
const SizedBox(height: 16),
|
||||
if (_cargando)
|
||||
const Center(child: Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 24),
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: AppColors.primary),
|
||||
))
|
||||
else if (_log.isEmpty)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(color: AppColors.surfaceLow,
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
child: Column(children: [
|
||||
const Icon(Icons.history, color: AppColors.outlineVariant, size: 28),
|
||||
const SizedBox(height: 8),
|
||||
Text('Sin registros',
|
||||
style: appLabel(size: 13, color: AppColors.outlineVariant)),
|
||||
Text('Carga datos de demo o sincroniza',
|
||||
style: appLabel(size: 11, color: AppColors.outlineVariant)),
|
||||
]),
|
||||
)
|
||||
else
|
||||
..._log.asMap().entries.map((e) {
|
||||
final i = e.key;
|
||||
final a = e.value;
|
||||
final color = _severidadColor(a['severidad'] as int);
|
||||
final estado = a['estado'] as String;
|
||||
final badge = _badgeEstado(estado);
|
||||
final badgeC = _badgeColor(estado);
|
||||
return _logEntry(
|
||||
icon: _tipoIcon(a['tipo'] as String),
|
||||
iconColor: color,
|
||||
iconBg: color.withOpacity(0.12),
|
||||
title: _tipoLabel(a['tipo'] as String),
|
||||
badge: badge,
|
||||
badgeColor: badgeC,
|
||||
zona: a['zona_nombre'] as String? ?? '—',
|
||||
time: _horaCorta(a['timestamp'] as String),
|
||||
showLine: i < _log.length - 1,
|
||||
);
|
||||
}).toList(),
|
||||
],
|
||||
);
|
||||
|
||||
Widget _logEntry({
|
||||
required IconData icon, required Color iconBg, required Color iconColor,
|
||||
required String title, required String badge, required Color badgeTextColor,
|
||||
required Color badgeBg, required String description, required String time,
|
||||
required IconData icon,
|
||||
required Color iconBg,
|
||||
required Color iconColor,
|
||||
required String title,
|
||||
required String badge,
|
||||
required Color badgeColor,
|
||||
required String zona,
|
||||
required String time,
|
||||
required bool showLine,
|
||||
}) =>
|
||||
Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
Column(children: [
|
||||
Container(width: 32, height: 32,
|
||||
decoration: BoxDecoration(color: iconBg, shape: BoxShape.circle),
|
||||
child: Icon(icon, size: 16, color: iconColor)),
|
||||
Container(
|
||||
width: 32, height: 32,
|
||||
decoration: BoxDecoration(color: iconBg, shape: BoxShape.circle),
|
||||
child: Icon(icon, size: 16, color: iconColor),
|
||||
),
|
||||
if (showLine)
|
||||
Container(width: 1, height: 64, margin: const EdgeInsets.only(top: 4),
|
||||
color: AppColors.outlineVariant.withOpacity(0.3)),
|
||||
Container(
|
||||
width: 1, height: 56,
|
||||
margin: const EdgeInsets.only(top: 4),
|
||||
color: AppColors.outlineVariant.withOpacity(0.3),
|
||||
),
|
||||
]),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(child: Padding(
|
||||
@@ -272,14 +420,16 @@ class _MonitoreoAlarmaPageState extends State<MonitoreoAlarmaPage>
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
decoration: BoxDecoration(color: badgeBg,
|
||||
borderRadius: BorderRadius.circular(4)),
|
||||
decoration: BoxDecoration(
|
||||
color: badgeColor.withOpacity(0.12),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(badge, style: appLabel(size: 10,
|
||||
weight: FontWeight.bold, color: badgeTextColor)),
|
||||
weight: FontWeight.bold, color: badgeColor)),
|
||||
),
|
||||
]),
|
||||
const SizedBox(height: 4),
|
||||
Text(description, style: appLabel(size: 12)),
|
||||
Text(zona, style: appLabel(size: 12)),
|
||||
const SizedBox(height: 4),
|
||||
Text(time, style: appLabel(size: 10, color: AppColors.outline)),
|
||||
]),
|
||||
|
||||
Reference in New Issue
Block a user