Esempio n. 1
0
        private void gestionaPaqueteAutenticado(string myString, string clientIPAddress, string[] datosConexion)
        {
            Cliente myCliente = new Cliente(Formulario, "9050");
            string[] coordenadas;
            //string dirimconges;
            //int IDbitmap;
            //SError err;
            BD AccesoBD= new BD(Formulario);

            /*******************************
                Para agregacion
             *******************************/
            string msje, tipo,nombreVia,sentidoMarcha;
            double X = 0.0, Y = 0.0,Z=0.0;
            string[] info;

            switch (datosConexion[0])
            {
                case "D1"://el nodo ha perdido el cambio de beacon
                   string msj = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + ",00," + Server.myPseudonimo + ", Ek1(00:TimeStamp:newPseu)";
                   myCliente.respuesta("01", viejoPseu, msj, clientIPAddress);
                    break;
                case "T1"://paquete de tráfico, comprobamos validez
                    datosConexion[2] = Criptutiles.Decrypt(datosConexion[2], DButiles.recuperaSK(datosConexion[1]));
                    coordenadas = datosConexion[2].Split(';');

                    lock (this)
                    {

                        info = datosConexion[2].Split('_');
                        tipo = info[0];

                        //Si es un mensaje de aggregación
                        if (tipo.Equals("I"))
                        {
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "An information packet arrives" });

                            msje = info[1];
                            nombreVia = info[2];
                            sentidoMarcha = info[3];
                            X = double.Parse(info[4]);
                            Y = double.Parse(info[5]);
                            Z = double.Parse(info[6]);
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { msje + " in " + nombreVia + "direction " + sentidoMarcha + X + ", " + Y });
                            System.Globalization.CultureInfo myCIintl = new System.Globalization.CultureInfo("es-ES", false);
                            agregacion PaqAg = new agregacion(Formulario, nombreVia, sentidoMarcha, X, Y, Z, DateTime.ParseExact(info[7], "MM/dd/yyyy HH:mm:ss", myCIintl));
                            PaqAg.InformacionAggreacion(msje, clientIPAddress);
                        }
                        //Si es una respuesta de aggregación

                        //Formato del paquete|F-Atasco en la M40-ID-hash.ToString|
                        if (tipo.Equals("F"))
                        {
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "LLega un paquete F" });
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "Compruebo si estoy a la espera de firmas" });
                            if (AggregarFirmas.getEsperoFirmas())
                            {
                                //Formamos el paquete con la información que nos va llegando de los diferentes vehículos
                                //string IDPaquete=info[1];
                                msje = info[1];
                                nombreVia = info[2];
                                sentidoMarcha = info[3];
                                string IDVehículo = info[4];
                                string firma = info[5];
                                string data = "A signed packet arrives from " + IDVehículo.ToString();
                                Formulario.Invoke(Formulario.myDelegate, new Object[] { data });
                                X = double.Parse(info[6]);
                                Y = double.Parse(info[7]);
                                Z = double.Parse(info[8]);
                                DateTime myDateTime = DateTime.Now;
                                string posiblevento;

                                posiblevento = AccesoBD.ExistePosibleEvento("T1", nombreVia, sentidoMarcha, X, Y);
                                Formulario.Invoke(Formulario.myDelegate, new Object[] { "¿Existe el posible Evento?" });
                                if (!(posiblevento.Equals("") && posiblevento.Equals("error")))
                                {
                                    Formulario.Invoke(Formulario.myDelegate, new Object[] { "SI" });
                                    Formulario.Invoke(Formulario.myDelegate, new Object[] { "LO esperaba" });
                                    if (AggregarFirmas.getnumerofirmas() <= 0)
                                    {
                                        string IP = IPAddress.Broadcast.ToString();
                                        string firmas = AggregarFirmas.getfirmas();
                                        AggregarFirmas.agregafirmas(firmas + firma + "|" + IDVehículo);
                                        AggregarFirmas.incrementarfirmas();
                                        Formulario.Invoke(Formulario.myDelegate, new Object[] { "AgregoFirma con id VEhículo, Lo inserto en BD" });
                                        AccesoBD.insertarEvento("T1", nombreVia, sentidoMarcha, X, Y, Z, myDateTime, firma);
                                        //utiles.anadePoi("Atasco", "Atasco", (int)(X*100000), (int)(Y*100000), 100);
                                        utiles.anadePoi("Atasco", "Atasco", (int)(X), (int)(Y), 100);
                                        Cliente Client = new Cliente(Formulario, "9050");
                                        string prueba = "A_Traffic Jam." + AggregarFirmas.getfirmas() + "_" + nombreVia + "_" + sentidoMarcha + "_" + X.ToString() + "_" + Y.ToString() + "_" + Z.ToString() + "_" + myDateTime.ToString("MM/dd/yyyy HH:mm:ss");
                                        Client.respuesta("T1", myPseudonimo, Criptutiles.Encrypt(prueba, AccesoBD.recuperamySK()), IP);
                                        Formulario.Invoke(Formulario.myDelegate, new Object[] { "Send an aggregation Packet" });
                                        AggregarFirmas.resetEsperoFirmas();
                                    }
                                }
                            }
                            else
                            {
                                Formulario.Invoke(Formulario.myDelegate, new Object[] { "No espero paquete F, no hago nada" });
                            }
                        }//end IF F
                        if (tipo.Equals("A"))
                        {
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "An agregation packet arrives" });

                            msje = info[1];
                            nombreVia = info[2];
                            sentidoMarcha = info[3];
                            X = double.Parse(info[4]);
                            Y = double.Parse(info[5]);
                            Z = double.Parse(info[6]);
                            System.Globalization.CultureInfo myCIintl = new System.Globalization.CultureInfo("es-ES", false);
                            agregacion PaqAg = new agregacion(Formulario, nombreVia, sentidoMarcha, X, Y, Z, DateTime.ParseExact(info[7], "MM/dd/yyyy HH:mm:ss", myCIintl));
                            PaqAg.MensajeAgregado(msje);
                        }//end IF A
                    }//end lock

                    break;

                case "P1"://paquete de publicidad, oomprobamos validez e insertamos en el mapa en caso de ser válido
                    //COMPROBANDO FIRMA CIFRADA (CLAVE PUBLICA)
                    string datosDescifrados=Criptutiles.RSAverifica(datosConexion[1], datosConexion[2], true);
                    if (datosDescifrados.Equals(""))
                   // if (!RSA3.VerifyData(decryptedData, "SHA1", signedData))// (dataToEncrypt, "SHA1", signedData);
                        Formulario.Invoke(Formulario.myDelegate, new Object[] { "Advertisement Data not Correct" });
                    else
                    {
                        //Formulario.Invoke(Formulario.myDelegate, new Object[] { "Advertisement Data Correct" });
                        //datosConexion[2] = Criptutiles.Decrypt(datosConexion[2], DButiles.recuperaSK(datosConexion[1]));
                        coordenadas = datosDescifrados.Split(';');

                        Formulario.Invoke(Formulario.myDelegate, new Object[] { "Advertisement: " + coordenadas[0]+" in "+ coordenadas[2] + "," + coordenadas[3] });
                        DButiles.insertarEvento("P1", coordenadas[0], coordenadas[1], double.Parse(coordenadas[2]), double.Parse(coordenadas[3]), double.Parse(coordenadas[4]), DateTime.Parse(coordenadas[5]), coordenadas[coordenadas.Length-1]);
                        utiles.anadePoi("El Corte Ingles","Comercio", int.Parse(coordenadas[2]), int.Parse(coordenadas[3]), 100);
                       /* try
                        {
                            int res = CApplicationAPI.DeletePoiCategory(out err, coordenadas[0], "ESP", 1000);
                            string dirImagen = @coordenadas[0]+".bmp";
                            res = CApplicationAPI.AddPoiCategory(out err, coordenadas[3],dirImagen, "ESP", 1000);
                            LONGPOSITION position = new LONGPOSITION(int.Parse(coordenadas[2]), int.Parse(coordenadas[3]));//X, Y);
                            SPoi poi = new SPoi(position, "Comercio", coordenadas[0], 1000);
                            res = CApplicationAPI.AddPoi(out err, ref poi, 1000);
                        }
                        catch {}*/
                    }
                    break;

                case "P2"://paquete de aparcamiento
                    //COMPROBANDO FIRMA CIFRADA (CLAVE PUBLICA)
                    string datosDescifrados2=Criptutiles.RSAverifica(datosConexion[1], datosConexion[2],false);
                    if (datosDescifrados2.Equals(""))
                        // if (!RSA3.VerifyData(decryptedData, "SHA1", signedData))// (dataToEncrypt, "SHA1", signedData);
                        Formulario.Invoke(Formulario.myDelegate, new Object[] { "Possible Parking Data not Correct" });
                    else
                    {
                        // datosConexion[2] = Criptutiles.Decrypt(datosConexion[2], DButiles.recuperaSK(datosConexion[1]));
                        coordenadas = datosDescifrados2.Split(';');
                        Formulario.Invoke(Formulario.myDelegate, new Object[] { "Parking in: " + coordenadas[0] + " sense " + coordenadas[1] + "coordinates " + coordenadas[2] + "," + coordenadas[3] });
                        DButiles.insertarEvento("P2", coordenadas[0], coordenadas[1], double.Parse(coordenadas[2]), double.Parse(coordenadas[3]), double.Parse(coordenadas[4]), DateTime.Parse(coordenadas[5]), coordenadas[coordenadas.Length - 1]);
                        utiles.anadePoi("Posible Plaza", "Posible Plaza", int.Parse(coordenadas[2]), int.Parse(coordenadas[3]),100);
                       /* try
                        {
                            int res = CApplicationAPI.DeletePoiCategory(out err, "Posible Plaza", "ESP", 1000);
                            string dirImagen = @"parkingsinfondo.bmp";
                            res = CApplicationAPI.AddPoiCategory(out err, "Posible Plaza", dirImagen, "ESP", 1000);
                            LONGPOSITION position = new LONGPOSITION(int.Parse(coordenadas[2]), int.Parse(coordenadas[3]));//X, Y);
                            SPoi poi = new SPoi(position, "Posible Plaza", "Posible plaza", 1000);
                            res = CApplicationAPI.AddPoi(out err, ref poi, 1000);
                            //if (CApplicationAPI.AddBitmapToMap(out err, dirimconges, int.Parse(coordenadas[2]), int.Parse(coordenadas[3]), out IDbitmap, 1000) == 1)
                        }
                        catch
                        {}*/
                    }
                    break;

                default:
                    break;
            }
        }
        public void ManejaIncidente(string NombreVia, string sentidoMarcha, double X, double Y, double Z)
        {
            //OBTIENE HORA ACTUAL
            DateTime myDateTime = DateTime.Now;
            //ACCESO A LA BD
            BD AccesoBD = new BD(Formulario);
            string evento, evento2;
            //Formulario.Invoke(Formulario.myDelegate, new Object[] { "ATASCO!!" });
            //Formulario.Invoke(Formulario.myDelegate, new Object[] { "Traffic Jam in: " + NombreVia });

            //Se comprueba si ya existe el incidente en las coordenadas X,Y aprox.
            evento = AccesoBD.ExisteEvento("T1", NombreVia, sentidoMarcha, X, Y);
            //Formulario.Invoke(Formulario.myDelegate, new Object[] { "¿¿Existe Evento??" });
            if (!evento.Equals("error"))
            {
                string[] words = evento.Split('|');
                //Si no existe el evento se introduce en BD
                if (evento.Equals(""))
                {
                    // Formulario.Invoke(Formulario.myDelegate, new Object[] { "NO" });
                    evento2 = AccesoBD.ExistePosibleEvento("T1", NombreVia, sentidoMarcha, X, Y);
                    if (evento2.Equals(""))
                    {
                        if (AccesoBD.insertarPosibleEvento("T1", NombreVia, sentidoMarcha, X, Y, Z, myDateTime, ""))
                        {
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "Inserta en PosibleEvneto" });

                            //Se ha insertado correctamente
                            // Formulario.Invoke(Formulario.myDelegate, new Object[] { "The event has been inserted properly in DB" });

                            //Si se trata de un nuevo evento se envía un mensaje buscando agregación.
                            AggregarFirmas.setFirmas();
                            AggregarFirmas.setNumerofirmas();
                            AggregarFirmas.setEsperoFirmas();
                            AggregarFirmas.getEsperoFirmas();
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "Me quedo a la espera de firmas" });
                        }
                        else
                        {
                            Formulario.Invoke(Formulario.myDelegate, new Object[] { "IncidenteV1=>Error insertando posibleevento en BD " });
                        }
                    }

                    agregacion Agpaq = new agregacion(Formulario, NombreVia, sentidoMarcha, X, Y, Z, myDateTime);
                    Agpaq.EnviaNuevoPaqueteAgregacion();
                }
                else
                {
                    // Formulario.Invoke(Formulario.myDelegate, new Object[] { "SI" });
                    //Si existe el incidente no hacemos nada
                    Formulario.Invoke(Formulario.myDelegate, new Object[] { "Incident already dealt " });
                }
            }//error
        }
Esempio n. 3
0
        private void Atasco_Click(object sender, EventArgs e)
        {
            BD DButiles = new BD(this);
            int CoorX = -1632700;// 0899; //-1538956;//
            int CoorY = 2848779;//2309;//2786158;//
            int CoorZ = 0;
            SError err;
            SGpsPosition gps;
            try
            {
                if (CApplicationAPI.GetActualGpsPosition(out err, out gps, false, 1000) == 1)
                {
                    CoorX = gps.Longitude;
                    CoorY = gps.Latitude;
                }
            }
            catch { }

            string viaOnombre = "Atasco";
            string sentido = "sentido";
            DateTime fechaDato = DateTime.Now;
            string datoPublicidad = viaOnombre + ";" + sentido + ";" + CoorX + ";" + CoorY + ";" + CoorZ + ";" + fechaDato;//Estos datos los coge de un fichero

               // string firma = Convert.ToBase64String(signedData).ToString();//
            Utiles utiles = new Utiles();
            Cliente clientePubli = new Cliente(this, "9050");
            Cripto criputiles = new Cripto(this);
                //----------------------------------------------------------------------------------------------
                //ENVIAMOS ATASCO TB (PARA PRUEBA)
                //CoorX = CoorX + 200;// -1538956;
               // CoorY = 2786158;
                viaOnombre = "calle1";
                sentido = "sentido1";
                fechaDato = DateTime.Now;//.GetDateTimeFormats("MM/dd/yyyy HH:mm:ss");

            agregacion datoagregado=  new agregacion(this, viaOnombre, sentido, CoorX, CoorY, CoorZ, fechaDato);
            string firma = datoagregado.FirmarMsje("Traffic Jam", Server.myPseudonimo);
                string datoAtasco = viaOnombre + "_" + sentido + "_" + CoorX + "_" + CoorY + "_" + CoorZ + "_" + fechaDato.ToString("MM/dd/yyyy HH:mm:ss");//Estos datos los coge de un fichero
               // firma = criputiles.RSAfirma(datoAtasco);
                string mensaje = "A_Traffic Jam." + firma + '|'+Server.myPseudonimo + "_" + datoAtasco;
                Invoke(myDelegate, new Object[] { "Sending Possible Traffic Jam" });
                DButiles.insertarEvento("T1", viaOnombre, sentido, CoorX, CoorY, CoorZ, fechaDato, firma);
                clientePubli.respuesta("T1", Server.myPseudonimo, criputiles.Encrypt(mensaje, DButiles.recuperamySK()), IPAddress.Broadcast.ToString());//,+ ";" + firma
                utiles.anadePoi("Atasco", "Atasco", CoorX, CoorY, 100);
        }