-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
701 lines (610 loc) · 25.1 KB
/
Copy pathProgram.cs
File metadata and controls
701 lines (610 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
using System;
using System.Threading;
using System.Collections.Generic;
namespace SistemaContraIncendios
{
// ESTRUCTURA PARA ALMACENAR DATOS DEL SENSOR
public struct DatosSensor
{
public int piso;
public int habitacion;
public double temperatura;
public bool hayHumo;
public DateTime fechaHora;
public string estado;
}
// ESTRUCTURA PARA ALMACENAR HISTORIAL
public struct RegistroHistorial
{
public DateTime fechaHora;
public int piso;
public int habitacion;
public double temperatura;
public bool hayHumo;
public string estado;
}
class Program
{
// ARRAYS PARA ALMACENAR DATOS
static DatosSensor[,] sensores = new DatosSensor[3, 4]; // 3 pisos, 4 habitaciones
static RegistroHistorial[] historial = new RegistroHistorial[1000];
static int contadorHistorial = 0;
static bool sistemaActivo = false;
static bool alertaActiva = false;
static bool enMenuPrincipal = true;
static Random random = new Random();
static void Main(string[] args)
{
// INICIALIZAR SENSORES
InicializarSensores();
// LOGIN BÁSICO
if (Login())
{
Console.Clear();
Console.WriteLine("¡Bienvenido al Sistema Contra Incendios!");
Thread.Sleep(2000);
// MOSTRAR MENÚ PRINCIPAL
MostrarMenuPrincipal();
}
else
{
Console.WriteLine("Acceso denegado. Presione cualquier tecla para salir...");
Console.ReadKey();
}
}
// FUNCIÓN DE LOGIN
static bool Login()
{
int intentos = 0;
while (intentos < 3)
{
Console.Clear();
Console.WriteLine("=== SISTEMA CONTRA INCENDIOS ===");
Console.WriteLine("Ingrese sus credenciales:");
Console.Write("Usuario: ");
string usuario = Console.ReadLine();
Console.Write("Contraseña: ");
string contraseña = Console.ReadLine();
if (usuario == "admin" && contraseña == "1234")
{
return true;
}
else
{
intentos++;
Console.WriteLine($"Credenciales incorrectas. Intentos restantes: {3 - intentos}");
Console.WriteLine("Presione cualquier tecla para continuar...");
Console.ReadKey();
}
}
return false;
}
// FUNCIÓN PARA INICIALIZAR SENSORES
static void InicializarSensores()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
sensores[i, j].piso = i + 1;
sensores[i, j].habitacion = j + 1;
sensores[i, j].temperatura = 20.0 + random.NextDouble() * 5; // 20-25°C
sensores[i, j].hayHumo = false;
sensores[i, j].fechaHora = DateTime.Now;
sensores[i, j].estado = "NORMAL";
}
}
}
// FUNCIÓN PARA MOSTRAR MENÚ PRINCIPAL
static void MostrarMenuPrincipal()
{
enMenuPrincipal = true;
sistemaActivo = true;
// Hilo para actualizar sensores en segundo plano
Thread hiloActualizacion = new Thread(() =>
{
while (enMenuPrincipal)
{
ActualizarSensores();
Thread.Sleep(2000); // Actualizar cada 2 segundos
}
});
hiloActualizacion.IsBackground = true;
hiloActualizacion.Start();
int opcion = 0;
do
{
// Verificar alerta antes de mostrar el menú
if (VerificarAlerta() && !alertaActiva)
{
alertaActiva = true;
enMenuPrincipal = false;
MostrarAlertaIncendio();
enMenuPrincipal = true;
}
if (enMenuPrincipal && !alertaActiva)
{
Console.Clear();
Console.WriteLine("=== MENÚ PRINCIPAL ===");
Console.WriteLine("1. Ver historial de temperaturas");
Console.WriteLine("2. Probar sensor único");
Console.WriteLine("3. Ver estado de sensores");
Console.WriteLine("4. Salir");
Console.WriteLine();
// MOSTRAR MAQUETA DEL EDIFICIO CON DATOS ACTUALIZADOS
MostrarMaquetaEdificio();
// Mostrar estado actual
MostrarEstadoActual();
Console.Write("Seleccione una opción: ");
// Verificar si hay entrada disponible
if (Console.KeyAvailable)
{
string input = Console.ReadLine();
if (int.TryParse(input, out opcion))
{
switch (opcion)
{
case 1:
enMenuPrincipal = false;
VerHistorialTemperaturas();
enMenuPrincipal = true;
break;
case 2:
enMenuPrincipal = false;
ProbarSensorUnico();
enMenuPrincipal = true;
break;
case 3:
enMenuPrincipal = false;
VerEstadoSensores();
enMenuPrincipal = true;
break;
case 4:
Console.WriteLine("Saliendo del sistema...");
sistemaActivo = false;
enMenuPrincipal = false;
break;
default:
Console.WriteLine("Opción inválida. Presione cualquier tecla...");
Console.ReadKey();
break;
}
}
else
{
Console.WriteLine("Entrada inválida. Presione cualquier tecla...");
Console.ReadKey();
}
}
else
{
// Si no hay entrada, actualizar la pantalla cada segundo
Thread.Sleep(1000);
}
}
} while (opcion != 4 && enMenuPrincipal);
}
// FUNCIÓN PARA MOSTRAR MAQUETA DEL EDIFICIO
static void MostrarMaquetaEdificio()
{
Console.WriteLine("\n=== MAQUETA DEL EDIFICIO ===");
Console.WriteLine($"Última actualización: {DateTime.Now:HH:mm:ss}");
for (int piso = 2; piso >= 0; piso--) // Mostrar de arriba hacia abajo
{
Console.Write($"Piso {piso + 1}: ");
for (int habitacion = 0; habitacion < 4; habitacion++)
{
string color = ObtenerColorSensor(piso, habitacion);
// ESTRUCTURAS CONDICIONALES MÚLTIPLES
if (color == "VERDE")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("[OK] ");
}
else if (color == "AMARILLO")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[⚠] ");
}
else if (color == "ROJO")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[🔥] ");
}
Console.ResetColor();
}
Console.WriteLine();
}
Console.WriteLine();
}
// FUNCIÓN PARA OBTENER COLOR DEL SENSOR
static string ObtenerColorSensor(int piso, int habitacion)
{
double temp = sensores[piso, habitacion].temperatura;
bool humo = sensores[piso, habitacion].hayHumo;
// ESTRUCTURAS CONDICIONALES ANIDADAS
if (temp > 30.0) // Temperatura inestable
{
if (humo)
{
return "ROJO"; // Humo + temperatura alta
}
else
{
return "AMARILLO"; // Solo temperatura alta
}
}
else // Temperatura estable
{
if (humo)
{
return "AMARILLO"; // Solo humo
}
else
{
return "VERDE"; // Todo normal
}
}
}
// FUNCIÓN PARA VER ESTADO DE SENSORES
static void VerEstadoSensores()
{
bool monitoreoActivo = true;
alertaActiva = false;
Console.Clear();
Console.WriteLine("=== MONITOREO EN TIEMPO REAL ===");
Console.WriteLine("Presione ESC para volver al menú principal\n");
while (monitoreoActivo)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
monitoreoActivo = false;
break;
}
}
// Actualizar sensores cada 2 segundos
ActualizarSensores();
// Verificar si hay alerta
if (VerificarAlerta() && !alertaActiva)
{
alertaActiva = true;
MostrarAlertaIncendioEnMonitoreo();
alertaActiva = false;
}
if (!alertaActiva)
{
Console.Clear();
Console.WriteLine("=== MONITOREO EN TIEMPO REAL ===");
Console.WriteLine("Presione ESC para volver al menú principal\n");
// Mostrar estado actual
MostrarEstadoActual();
MostrarMaquetaEdificio();
// Sonido de actualización
Console.Beep(800, 100);
Thread.Sleep(2000);
}
}
}
// FUNCIÓN PARA ACTUALIZAR SENSORES
static void ActualizarSensores()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
// Generar temperatura aleatoria
double nuevaTemp = 18.0 + random.NextDouble() * 20; // 18-38°C
// Generar humo aleatorio (10% probabilidad)
bool nuevoHumo = random.NextDouble() < 0.1;
sensores[i, j].temperatura = nuevaTemp;
sensores[i, j].hayHumo = nuevoHumo;
sensores[i, j].fechaHora = DateTime.Now;
// Actualizar estado
ActualizarEstadoSensor(i, j);
// Guardar en historial
GuardarEnHistorial(i, j);
}
}
}
// FUNCIÓN PARA ACTUALIZAR ESTADO DEL SENSOR
static void ActualizarEstadoSensor(int piso, int habitacion)
{
double temp = sensores[piso, habitacion].temperatura;
bool humo = sensores[piso, habitacion].hayHumo;
if (temp > 30.0 && humo)
{
sensores[piso, habitacion].estado = "EMERGENCIA";
}
else if (temp > 30.0 || humo)
{
sensores[piso, habitacion].estado = "ALERTA";
}
else
{
sensores[piso, habitacion].estado = "NORMAL";
}
}
// FUNCIÓN PARA GUARDAR EN HISTORIAL
static void GuardarEnHistorial(int piso, int habitacion)
{
if (contadorHistorial < 1000)
{
historial[contadorHistorial].fechaHora = sensores[piso, habitacion].fechaHora;
historial[contadorHistorial].piso = piso + 1;
historial[contadorHistorial].habitacion = habitacion + 1;
historial[contadorHistorial].temperatura = sensores[piso, habitacion].temperatura;
historial[contadorHistorial].hayHumo = sensores[piso, habitacion].hayHumo;
historial[contadorHistorial].estado = sensores[piso, habitacion].estado;
contadorHistorial++;
}
}
// FUNCIÓN PARA VERIFICAR ALERTA
static bool VerificarAlerta()
{
// ESTRUCTURA REPETITIVA PARA (FOR) ANIDADA
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (sensores[i, j].temperatura > 30.0 && sensores[i, j].hayHumo)
{
return true;
}
}
}
return false;
}
// FUNCIÓN PARA MOSTRAR ALERTA DE INCENDIO DESDE MENÚ PRINCIPAL
static void MostrarAlertaIncendio()
{
bool alertaEnCurso = true;
while (alertaEnCurso)
{
Console.Clear();
// Mostrar alerta en rojo
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨");
Console.WriteLine("🔥 SEÑALES DE INCENDIO DETECTADAS 🔥");
Console.WriteLine("🚨 CONTACTANDO CON LOS BOMBEROS 🚨");
Console.WriteLine("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨");
Console.ResetColor();
Console.WriteLine("\nPresione 1 para simular agua y extintor");
Console.WriteLine("Presione ESC para volver al menú");
// Mostrar sensores en emergencia
MostrarSensoresEmergencia();
// Sonido de alerta
Console.Beep(1000, 300);
Console.Beep(800, 300);
DateTime inicio = DateTime.Now;
bool entradaDetectada = false;
while ((DateTime.Now - inicio).TotalMilliseconds < 1000 && !entradaDetectada)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
entradaDetectada = true;
if (key.Key == ConsoleKey.D1)
{
ExtinguirIncendio();
alertaEnCurso = false;
alertaActiva = false;
}
else if (key.Key == ConsoleKey.Escape)
{
alertaEnCurso = false;
alertaActiva = false;
}
}
Thread.Sleep(50);
}
// Verificar si la alerta sigue activa
if (!VerificarAlerta())
{
alertaEnCurso = false;
alertaActiva = false;
}
}
}
// FUNCIÓN PARA MOSTRAR ALERTA DE INCENDIO EN MONITOREO
static void MostrarAlertaIncendioEnMonitoreo()
{
bool alertaEnCurso = true;
while (alertaEnCurso)
{
Console.Clear();
// Mostrar alerta en rojo
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨");
Console.WriteLine("🔥 SEÑALES DE INCENDIO DETECTADAS 🔥");
Console.WriteLine("🚨 CONTACTANDO CON LOS BOMBEROS 🚨");
Console.WriteLine("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨");
Console.ResetColor();
Console.WriteLine("\nPresione 1 para simular agua y extintor");
Console.WriteLine("Presione cualquier otra tecla para continuar");
// Mostrar sensores en emergencia
MostrarSensoresEmergencia();
// Sonido de alerta
Console.Beep(1000, 300);
Console.Beep(800, 300);
// Esperar por entrada del usuario con timeout
DateTime inicio = DateTime.Now;
bool entradaDetectada = false;
while ((DateTime.Now - inicio).TotalMilliseconds < 1000 && !entradaDetectada)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
entradaDetectada = true;
if (key.Key == ConsoleKey.D1)
{
ExtinguirIncendio();
alertaEnCurso = false;
}
else
{
alertaEnCurso = false;
}
}
Thread.Sleep(50);
}
// Verificar si la alerta sigue activa
if (!VerificarAlerta())
{
alertaEnCurso = false;
}
}
}
// FUNCIÓN PARA MOSTRAR SENSORES EN EMERGENCIA
static void MostrarSensoresEmergencia()
{
Console.WriteLine("\n=== SENSORES EN EMERGENCIA ===");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (sensores[i, j].estado == "EMERGENCIA")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"🔥 Piso {i + 1}, Habitación {j + 1}: {sensores[i, j].temperatura:F1}°C - HUMO DETECTADO");
Console.ResetColor();
}
}
}
}
// FUNCIÓN PARA EXTINGUIR INCENDIO
static void ExtinguirIncendio()
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("💧 ACTIVANDO SISTEMA DE EXTINCIÓN 💧");
Console.WriteLine("🧯 ROCIADORES ACTIVADOS 🧯");
Console.ResetColor();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (sensores[i, j].estado == "EMERGENCIA")
{
sensores[i, j].temperatura = 20.0 + random.NextDouble() * 5;
sensores[i, j].hayHumo = false;
sensores[i, j].estado = "NORMAL";
sensores[i, j].fechaHora = DateTime.Now;
GuardarEnHistorial(i, j);
}
}
}
Console.WriteLine("\n✅ INCENDIO EXTINGUIDO EXITOSAMENTE");
Console.WriteLine("🔄 SISTEMA REGULARIZADO");
Console.WriteLine("\nPresione cualquier tecla para continuar...");
Console.ReadKey();
}
// FUNCIÓN PARA MOSTRAR ESTADO ACTUAL
static void MostrarEstadoActual()
{
Console.WriteLine($"Última actualización: {DateTime.Now:HH:mm:ss}");
Console.WriteLine("Estado de sensores por piso:");
// ESTRUCTURA REPETITIVA PARA (FOR) ANIDADA
for (int i = 0; i < 3; i++)
{
Console.Write($"Piso {i + 1}: ");
for (int j = 0; j < 4; j++)
{
Console.Write($"H{j + 1}({sensores[i, j].temperatura:F1}°C");
if (sensores[i, j].hayHumo) Console.Write("💨");
Console.Write(") ");
}
Console.WriteLine();
}
Console.WriteLine();
}
// FUNCIÓN PARA PROBAR SENSOR ÚNICO
static void ProbarSensorUnico()
{
Console.Clear();
Console.WriteLine("=== PRUEBA DE SENSOR ÚNICO ===");
Console.Write("Ingrese el piso (1-3): ");
int piso = int.Parse(Console.ReadLine()) - 1;
Console.Write("Ingrese la habitación (1-4): ");
int habitacion = int.Parse(Console.ReadLine()) - 1;
// ESTRUCTURAS CONDICIONALES ANIDADAS
if (piso >= 0 && piso < 3 && habitacion >= 0 && habitacion < 4)
{
Console.WriteLine($"\nProbando sensor Piso {piso + 1}, Habitación {habitacion + 1}...");
// ESTRUCTURA REPETITIVA PARA (FOR)
for (int i = 0; i < 10; i++)
{
// Generar datos aleatorios para la prueba
double tempPrueba = 15.0 + random.NextDouble() * 25;
bool humoPrueba = random.NextDouble() < 0.2;
Console.Clear();
Console.WriteLine("=== PRUEBA DE SENSOR ÚNICO ===");
Console.WriteLine($"Sensor: Piso {piso + 1}, Habitación {habitacion + 1}");
Console.WriteLine($"Lectura {i + 1}/10");
Console.WriteLine($"Temperatura: {tempPrueba:F1}°C");
Console.WriteLine($"Humo: {(humoPrueba ? "DETECTADO" : "NO DETECTADO")}");
// Determinar estado
string estadoPrueba = "NORMAL";
ConsoleColor color = ConsoleColor.Green;
if (tempPrueba > 30.0 && humoPrueba)
{
estadoPrueba = "EMERGENCIA";
color = ConsoleColor.Red;
}
else if (tempPrueba > 30.0 || humoPrueba)
{
estadoPrueba = "ALERTA";
color = ConsoleColor.Yellow;
}
Console.ForegroundColor = color;
Console.WriteLine($"Estado: {estadoPrueba}");
Console.ResetColor();
Console.Beep(600, 100);
Thread.Sleep(1000);
}
Console.WriteLine("\nPrueba completada. Presione cualquier tecla...");
Console.ReadKey();
}
else
{
Console.WriteLine("Ubicación inválida. Presione cualquier tecla...");
Console.ReadKey();
}
}
// FUNCIÓN PARA VER HISTORIAL DE TEMPERATURAS
static void VerHistorialTemperaturas()
{
Console.Clear();
Console.WriteLine("=== HISTORIAL DE TEMPERATURAS ===");
if (contadorHistorial == 0)
{
Console.WriteLine("No hay registros en el historial.");
}
else
{
Console.WriteLine($"Mostrando {contadorHistorial} registros:\n");
Console.WriteLine("Fecha/Hora\t\tPiso\tHab\tTemp\tHumo\tEstado");
Console.WriteLine("================================================================");
// ESTRUCTURA REPETITIVA PARA (FOR)
for (int i = 0; i < contadorHistorial; i++)
{
Console.WriteLine($"{historial[i].fechaHora:MM/dd HH:mm:ss}\t{historial[i].piso}\t{historial[i].habitacion}\t{historial[i].temperatura:F1}°C\t{(historial[i].hayHumo ? "SÍ" : "NO")}\t{historial[i].estado}");
// Mostrar de 20 en 20
if ((i + 1) % 20 == 0)
{
Console.WriteLine("\nPresione cualquier tecla para continuar...");
Console.ReadKey();
Console.Clear();
Console.WriteLine("=== HISTORIAL DE TEMPERATURAS (continuación) ===");
Console.WriteLine("Fecha/Hora\t\tPiso\tHab\tTemp\tHumo\tEstado");
Console.WriteLine("================================================================");
}
}
}
Console.WriteLine("\nPresione cualquier tecla para volver al menú...");
Console.ReadKey();
}
}
}