コード例 #1
0
        public void Ejecutar()
        {
            bool   verificado = false;
            string prueba;

            try
            {
                prueba = clienteSocket.Leer();
            }
            catch (NullReferenceException ex)
            {
                prueba = "n|n|n";
            }

            Console.WriteLine(prueba);
            string fechaString;
            int    medidorV;

            try
            {
                fechaString = (prueba.Split('|'))[0];
            }
            catch (Exception)
            {
                fechaString = "fechanovalida";
            }

            try
            {
                medidorV = Int32.Parse((prueba.Split('|'))[1]);
            }
            catch (Exception ex)
            {
                medidorV = 0;
            }
            string tipoV;

            try
            {
                tipoV = (prueba.Split('|'))[2];
            }
            catch (Exception)
            {
                tipoV = "cadena";
            }


            DateTime fecha;

            try
            {
                fecha = DateTime.ParseExact(fechaString, "yyyy-MM-dd-HH-mm-ss",
                                            System.Globalization.CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                fecha = new DateTime(2021, 01, 01, 00, 00, 00);
            }

            string   fecha2string;
            TimeSpan intervalo = DateTime.Now - fecha;

            if (tipoV == "consumo")
            {
                foreach (var medidor in dalConsumo.ObtenerMedidores())
                {
                    if (medidorV == medidor)
                    {
                        verificado = true;
                    }
                }
            }
            else if (tipoV == "trafico")
            {
                foreach (var medidor in dalTrafico.ObtenerMedidores())
                {
                    if (medidorV == medidor)
                    {
                        verificado = true;
                    }
                }
            }


            if (verificado == true && ((intervalo.TotalMinutes < 30) && (tipoV == "trafico" || tipoV == "consumo")))
            {
                clienteSocket.Escribir(DateTime.Now + "| WAIT");

                string input;
                try
                {
                    input = clienteSocket.Leer();
                }
                catch (Exception)
                {
                    input = "n|n|n|n|n|n";
                }

                string[] mensaje2;
                try
                {
                    mensaje2 = input.Split('|');
                }
                catch (Exception)
                {
                    input    = "n|n|n|n|n|n";
                    mensaje2 = input.Split('|');
                }


                if (mensaje2.Length == 6)
                {
                    int      medidor;
                    DateTime fecha2;
                    int      valor;
                    int      estado;


                    try
                    {
                        medidor = Int32.Parse((input.Split('|'))[0]);
                    }
                    catch (Exception)
                    {
                        medidor = 0;
                    }

                    fecha2string = (input.Split('|'))[1];

                    try
                    {
                        fecha2 = DateTime.ParseExact(fecha2string, "yyyy-MM-dd-HH-mm-ss",
                                                     System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (Exception ex)
                    {
                        fecha2 = new DateTime(2021, 01, 01, 00, 00, 00);
                    }

                    string tipo = (input.Split('|'))[2];

                    try
                    {
                        valor = Int32.Parse((input.Split('|'))[3]);
                    }
                    catch (Exception)
                    {
                        valor = -1;
                    }

                    try
                    {
                        estado = Int32.Parse((input.Split('|'))[4]);
                    }
                    catch (Exception)
                    {
                        estado = 7;
                    }

                    string confirmar = (input.Split('|'))[5];

                    if ((medidorV == medidor) && (fecha2 == fecha) && (tipo == "consumo" || tipo == "trafico") && (valor >= 0 && valor <= 1000) && (estado >= -1 && estado <= 2) && (confirmar == "UPDATE"))
                    {
                        if (tipo == "consumo")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fecha2,
                                Tipo     = tipo,
                                Valor    = valor,
                                Estado   = estado
                            };

                            lock (dal)
                            {
                                dal.RegistrarLecturaConsumo(l);
                                clienteSocket.Escribir(medidor + "|OK");
                                Console.WriteLine(medidor + "|OK");
                            }
                        }
                        else if (tipo == "trafico")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fecha2,
                                Tipo     = tipo,
                                Valor    = valor,
                                Estado   = estado
                            };

                            lock (dal)
                            {
                                clienteSocket.Escribir(medidor + "|OK");
                                dal.RegistrarLecturaTrafico(l);
                                Console.WriteLine(medidor + "|OK");
                            }
                        }
                    }
                    else
                    {
                        clienteSocket.Escribir(fecha2string + "|" + medidor + "| ERROR");
                        Console.WriteLine(fecha2string + "|" + medidor + "| ERROR");
                    }
                }
                else if (mensaje2.Length == 5)
                {
                    List <string> errores = new List <string>();
                    int           medidor;
                    DateTime      fecha2;
                    int           valor;

                    try
                    {
                        medidor = Int32.Parse((input.Split('|'))[0]);
                    }
                    catch (Exception)
                    {
                        medidor = 0;
                    }

                    fecha2string = (input.Split('|'))[1];

                    try
                    {
                        fecha2 = DateTime.ParseExact(fecha2string, "yyyy-MM-dd-HH-mm-ss",
                                                     System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (Exception ex)
                    {
                        fecha2 = new DateTime(2021, 01, 01, 00, 00, 00);
                    }

                    string tipo = (input.Split('|'))[2];

                    try
                    {
                        valor = Int32.Parse((input.Split('|'))[3]);
                    }
                    catch (Exception ex)
                    {
                        valor = -1;
                    }



                    string confirmar = (input.Split('|'))[4];

                    if ((medidorV == medidor) && (fecha2 == fecha) && (tipo == "consumo" || tipo == "trafico") && (valor >= 0 && valor <= 1000) && (confirmar == "UPDATE"))
                    {
                        if (tipo == "consumo")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fecha2,
                                Tipo     = tipo,
                                Valor    = valor,
                            };

                            lock (dal)
                            {
                                dal.RegistrarLecturaConsumo(l);
                                clienteSocket.Escribir(medidor + "|OK");
                                Console.WriteLine(medidor + "|OK");
                                //clienteSocket.CerrarConexion();
                            }
                        }
                        else
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fecha2,
                                Tipo     = tipo,
                                Valor    = valor,
                            };

                            lock (dal)
                            {
                                dal.RegistrarLecturaTrafico(l);
                                clienteSocket.Escribir(medidor + "|OK");
                                Console.WriteLine(medidor + "|OK");
                            }
                        }
                    }
                    else
                    {
                        clienteSocket.Escribir(fecha2string + "|" + medidor + "| ERROR");
                        Console.WriteLine(fecha2string + "|" + medidor + "| ERROR");
                    }
                }
                else
                {
                    clienteSocket.Escribir(fechaString + "|" + medidorV + "| ERROR");
                    Console.WriteLine(fechaString + "|" + medidorV + "| ERROR");
                    //Console.WriteLine("Conexion Rechazada");
                    clienteSocket.CerrarConexion();
                }
            }
            else
            {
                clienteSocket.Escribir("Error en solicitud");
                //Console.WriteLine("Conexion Rechazada");
                clienteSocket.CerrarConexion();
            }
        }
コード例 #2
0
        public void Ejecutar()
        {
            bool verificado = false;

            clienteSocket.Escribir("Ingresar: Fecha (aaaa-mm-dd-hh-mm-ss)|Numero Medidor (4 números)|Tipo (Consumo o Tráfico):");
            string prueba = clienteSocket.Leer();
            string fechaV;

            try
            {
                fechaV = (prueba.Split('|'))[0];
            }
            catch (Exception)
            {
                fechaV = "";
            }


            int medidorV;

            try
            {
                medidorV = Int32.Parse((prueba.Split('|'))[1]);
            }
            catch (Exception ex)
            {
                medidorV = 0;
            }

            string tipoV;

            try
            {
                tipoV = (prueba.Split('|'))[2];
            }
            catch (Exception)
            {
                tipoV = "";
            }


            DateTime fechaNew;

            try
            {
                fechaNew = DateTime.ParseExact(fechaV, "yyyy-MM-dd-HH-mm-ss",
                                               System.Globalization.CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                fechaNew = new DateTime(2021, 01, 01, 00, 00, 00);
            }

            TimeSpan intervalo = DateTime.Now - fechaNew;

            if (tipoV == "consumo")
            {
                foreach (var medidor in dalConsumo.ObtenerMedidores())
                {
                    if (medidorV == medidor)
                    {
                        verificado = true;
                    }
                }
            }
            else if (tipoV == "trafico")
            {
                foreach (var medidor in dalTrafico.ObtenerMedidores())
                {
                    if (medidorV == medidor)
                    {
                        verificado = true;
                    }
                }
            }
            if (verificado == true && ((intervalo.TotalMinutes < 30) && (tipoV == "trafico" || tipoV == "consumo")))
            {
                clienteSocket.Escribir(DateTime.Now + "| WAIT");
                string input;
                try
                {
                    input = clienteSocket.Leer();
                }
                catch (Exception)
                {
                    input = "a";
                }

                string[] frase;
                try
                {
                    frase = input.Split('|');
                }
                catch (Exception)
                {
                    input = "a|a|a|a|a|a";
                    frase = input.Split('|');
                }

                if (frase.Length == 6)
                {
                    int medidor;
                    try
                    {
                        medidor = Int32.Parse((input.Split('|'))[0]);
                    }
                    catch (Exception)
                    {
                        medidor = 0;
                    }
                    string fecha;
                    try
                    {
                        fecha = (input.Split('|'))[1];
                    }
                    catch (Exception)
                    {
                        fecha = "";
                    }

                    string tipo;
                    try
                    {
                        tipo = (input.Split('|'))[2];
                    }
                    catch (Exception)
                    {
                        tipo = "";
                    }

                    int valor;
                    try
                    {
                        valor = Int32.Parse((input.Split('|'))[3]);
                    }
                    catch (Exception)
                    {
                        valor = -1;
                    }
                    int estado;
                    try
                    {
                        estado = Int32.Parse((input.Split('|'))[4]);
                    }
                    catch (Exception)
                    {
                        estado = 6;
                    }

                    string confirmar;
                    try
                    {
                        confirmar = (input.Split('|'))[5];
                    }
                    catch (Exception)
                    {
                        confirmar = "";
                    }


                    if (confirmar != "UPDATE" || estado < -1 || estado > 2 || valor < 0 || valor > 1000 || fecha != fechaV || medidorV != medidor)
                    {
                        clienteSocket.Escribir(medidorV + "|ERROR");
                        clienteSocket.CerrarConexion();
                    }
                    else
                    {
                        if (tipo == "consumo")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fechaNew,
                                Tipo     = tipo,
                                Valor    = valor,
                                Estado   = estado
                            };

                            lock (dal)
                            {
                                clienteSocket.Escribir(medidorV + "|OK");
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(medidorV + "|OK");
                                Console.ResetColor();
                                dal.RegistrarLecturaConsumo(l);
                                clienteSocket.CerrarConexion();
                            }
                        }
                        else if (tipo == "trafico")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fechaNew,
                                Tipo     = tipo,
                                Valor    = valor,
                                Estado   = estado
                            };

                            lock (dal)
                            {
                                clienteSocket.Escribir(medidorV + "|OK");
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(medidorV + "|OK");
                                Console.ResetColor();
                                dal.RegistrarLecturaTrafico(l);
                                clienteSocket.CerrarConexion();
                            }
                        }
                    }
                }
                else if (frase.Length == 5)
                {
                    int medidor;
                    try
                    {
                        medidor = Int32.Parse((input.Split('|'))[0]);
                    }
                    catch (Exception)
                    {
                        medidor = 0;
                    }

                    string fecha = (input.Split('|'))[1];

                    string tipo = (input.Split('|'))[2];

                    int valor;
                    try
                    {
                        valor = Int32.Parse((input.Split('|'))[3]);
                    }
                    catch (Exception)
                    {
                        valor = -1;
                    }
                    string confirmar = (input.Split('|'))[4];

                    if (confirmar != "UPDATE" || valor < 0 || valor > 1000 || fecha != fechaV || medidorV != medidor)
                    {
                        clienteSocket.Escribir(medidorV + "|ERROR");
                        clienteSocket.CerrarConexion();
                    }
                    else
                    {
                        if (tipo == "consumo")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fechaNew,
                                Tipo     = tipo,
                                Valor    = valor
                            };
                            lock (dal)

                            {
                                clienteSocket.Escribir(medidorV + "|OK");
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(medidorV + "|OK");
                                Console.ResetColor();
                                dal.RegistrarLecturaConsumo(l);
                                clienteSocket.CerrarConexion();
                            }
                        }
                        else if (tipo == "trafico")
                        {
                            Lectura l = new Lectura()
                            {
                                NroSerie = medidor,
                                Fecha    = fechaNew,
                                Tipo     = tipo,
                                Valor    = valor
                            };

                            lock (dal)
                            {
                                clienteSocket.Escribir(medidorV + "|OK");
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(medidorV + "|OK");
                                Console.ResetColor();
                                dal.RegistrarLecturaTrafico(l);
                                clienteSocket.CerrarConexion();
                            }
                        }
                    }
                }
                else
                {
                    clienteSocket.Escribir(DateTime.Now + "|" + medidorV + "|" + "ERROR");
                    Console.WriteLine(DateTime.Now + "|" + medidorV + "|" + "ERROR");
                    clienteSocket.CerrarConexion();
                }
            }
            else
            {
                clienteSocket.Escribir("Error en solicitud. Cerrando Conexión");
                Console.WriteLine(DateTime.Now + "|" + medidorV + "|" + "ERROR");
                clienteSocket.CerrarConexion();
            }
        }