Пример #1
0
        public List <DatiDispositivo> PhonesInRange(int min, int max, int threshold = 0)
        {
            List <DatiDispositivo> list = new List <DatiDispositivo>();
            string query = "SELECT MAC, timestamp, x, y " +
                           "FROM posizioni WHERE timestamp < " + max + " AND timestamp > " + min + " ORDER BY timestamp";

            using (MySqlConnection connessione = new MySqlConnection("Database=" + Database + ";" + "Server=" + Server + ";" + "Port=3306;" + "UID=" + Uid + ";" + "Password="******";"))
                using (MySqlCommand cmd = connessione.CreateCommand())
                {
                    try
                    {
                        connessione.Open();
                        cmd.CommandText = query;
                        using (var dataReader = cmd.ExecuteReader())
                        {
                            while (dataReader.Read())
                            {
                                DatiDispositivo pi = new DatiDispositivo(dataReader.GetString(0), dataReader.GetInt32(1), dataReader.GetDouble(2), dataReader.GetDouble(3));
                                list.Add(pi);
                            }
                            Connesso = true;
                            return(list);
                        }
                    }
                    catch (MySqlException e)
                    {
                        System.Diagnostics.Debug.WriteLine("MySqlException catched" + e.ToString());
                        Connesso = false;
                        return(null);
                    }
                }
        }
Пример #2
0
        /// <summary>
        /// per il mac passato controllo se ci sono dispositivi a lui correlati, in base alla soglia passata, all'interno della lista dei dispositivi (se stesso è correlato a lui)
        /// </summary>
        /// <param name="device">dispositivo da controllare</param>
        /// <param name="distThreshold">distanza massima</param>
        /// <param name="hiddenDevices">dispositivi totali</param>
        /// <returns></returns>
        private List <String> CountCorrelatedPhones(DatiDispositivo device, Double distThreshold, List <DatiDispositivo> hiddenDevices)
        {
            List <String> correlatedMacs = new List <string>();

            foreach (DatiDispositivo dev in hiddenDevices)
            {
                Double distX           = Math.Abs(dev.Posizione.Ascissa - device.Posizione.Ascissa);
                Double disty           = Math.Abs(dev.Posizione.Ascissa - device.Posizione.Ascissa);
                Double devicesDistance = Math.Sqrt((distX * distX) + (disty * disty));
                if (devicesDistance < distThreshold)
                {
                    correlatedMacs.Add(dev.MAC_Address);
                }
            }
            return(correlatedMacs);
        }
Пример #3
0
 public void RemoveSearch()
 {
     if (selectedPhonePos.Count != 0)
     {
         String          cleanSelectedMAC = selectedMAC.Replace(":", "");
         DatiDispositivo p = selectedPhonePos.First <DatiDispositivo>();
         selectedPhonePos.Remove(p);
         if (Pacchetto.CntrlGlobal(selectedMAC))
         {
             //hiddenPhone
             hiddenPhonePos.Add(p);
         }
         else
         {
             //visiblePhone
             phonePos.Add(p);
         }
     }
 }
Пример #4
0
        public List <DatiDispositivo> MostFrequentPhones(int n, int min, int max, int threshold = 0)
        {
            List <DatiDispositivo> MACList = new List <DatiDispositivo>();
            string query = "SELECT DISTINCT MAC, timestamp " +
                           "FROM posizioni AS p1" +
                           " WHERE p1.MAC IN (SELECT * " +
                           "FROM (SELECT MAC " +
                           "FROM posizioni AS p2 " +
                           "WHERE p2.timestamp < " + max + " AND p2.timestamp > " + min +
                           " GROUP BY p2.MAC" +
                           " ORDER BY COUNT(*) DESC" +
                           " LIMIT " + n + ")" +
                           " AS p3)" +
                           " AND p1.timestamp < " + max + " AND p1.timestamp > " + min + ";";

            using (MySqlConnection connessione = new MySqlConnection("Database=" + Database + ";" + "Server=" + Server + ";" + "Port=3306;" + "UID=" + Uid + ";" + "Password="******";"))
                using (MySqlCommand cmd = connessione.CreateCommand())
                {
                    try
                    {
                        connessione.Open();
                        cmd.CommandText = query;
                        using (var dataReader = cmd.ExecuteReader())
                        {
                            while (dataReader.Read())
                            {
                                DatiDispositivo pi = new DatiDispositivo(dataReader.GetString(0), dataReader.GetInt32(1), 0, 0);
                                MACList.Add(pi);
                            }
                            Connesso = true;
                            return(MACList);
                        }
                    }
                    catch (MySqlException e)
                    {
                        System.Diagnostics.Debug.WriteLine("MySqlException catched" + e.ToString());
                        Connesso = false;
                        return(null);
                    }
                }
        }
Пример #5
0
        public List <string> CountHiddenPhones(DatiDispositivo p, double threshold)
        {
            int           time = EspServer.getUnixEpoch() - 60;
            List <string> list = new List <string>();

            string query = "SELECT MAC " +
                           "FROM posizioni WHERE global = 1 AND timestamp > " + time +
                           " AND ABS(x - " + p.Posizione.Ascissa.ToString(CultureInfo.InvariantCulture) + ") < " + threshold.ToString(CultureInfo.InvariantCulture) +
                           " AND ABS(y - " + p.Posizione.Ordinata.ToString(CultureInfo.InvariantCulture) + ") < " + threshold.ToString(CultureInfo.InvariantCulture) +
                           " AND MAC <> '" + p.MAC_Address + "'";

            using (MySqlConnection connessione = new MySqlConnection("Database=" + Database + ";" + "Server=" + Server + ";" + "Port=3306;" + "UID=" + Uid + ";" + "Password="******";"))
                using (MySqlCommand cmd = connessione.CreateCommand())
                {
                    try
                    {
                        connessione.Open();
                        cmd.CommandText = query;
                        using (var dataReader = cmd.ExecuteReader())
                        {
                            while (dataReader.Read())
                            {
                                list.Add(dataReader.GetString(0));
                            }
                            Connesso = true;
                            return(list);
                        }
                    }
                    catch (MySqlException e)
                    {
                        System.Diagnostics.Debug.WriteLine("MySqlException catched." + e.ToString());
                        Connesso = false;
                        return(null);
                    }
                }
        }
Пример #6
0
        /// <summary>
        /// se abbiamo almeno 2 schede, ricerca nel database la tupla(Distinct) MAC,timestamp,global,hash, ID scheda i,RSSI scheda i
        /// all'interno di pacchetti in cui prendendo in considerazione dati di schede diverse riguardanti lo stesso MAC (i-esimo)
        /// la cui differenza di timestamp (tra dati diversi) sia entro una certa soglia (si riferisca alla stessa lettura) e il cui
        /// timestamp sia entro il minuto di ricerca , prese queste tuple calcola i punti di intersezione tramite i metodi dei cerchi
        /// ed infine li aggiunge ad una lista
        /// </summary>
        /// <param name="nBoards">numero di schede presenti</param>
        /// <param name="threshold">soglia del timestamp tra dati diversi</param>
        /// <returns>lista di punti di intersezione</returns>
        public List <DatiDispositivo> GetLastMinuteData(int nBoards, int threshold = 0)
        {
            try
            {
                if (nBoards < 2)
                {
                    return(null);
                }

                int time = EspServer.getUnixEpoch() - 60;

                //Create a list to store the result
                List <DatiDispositivo> list = new List <DatiDispositivo>();

                using (MySqlConnection connessione = new MySqlConnection("Database=" + Database + ";" + "Server=" + Server + ";" + "Port=3306;" + "UID=" + Uid + ";" + "Password="******";"))
                    using (MySqlCommand cmd = connessione.CreateCommand())
                    {
                        connessione.Open();
                        //Create Query
                        StringBuilder builder = new StringBuilder();
                        builder.Append("SELECT DISTINCT P1.MAC, P1.timestamp, P1.global, P1.hash");

                        for (int i = 0; i < nBoards; i++)
                        {
                            builder.Append(", P").Append(i + 1).Append(".ID_scheda, P").Append(i + 1).Append(".RSSI");
                        }

                        builder.Append(VarQuery(nBoards, time, threshold));

                        //Create Command
                        cmd.CommandText = builder.ToString();
                        cmd.ExecuteNonQuery();
                        using (MySqlDataReader dataReader = cmd.ExecuteReader())
                        {
                            while (dataReader.Read())
                            {
                                string mac       = dataReader.GetString(0);
                                int    timestamp = dataReader.GetInt32(1);
                                bool   global    = dataReader.GetBoolean(2);

                                List <Cerchio> cerchi = new List <Cerchio>();
                                for (int i = 0; i < nBoards; i++)
                                {
                                    int id   = dataReader.GetInt32(4 + i * 2);
                                    int rssi = dataReader.GetInt32(5 + i * 2);

                                    cerchi.Add(new Cerchio(GetScheda(id).Punto, rssi));
                                }
                                Punto point = Cerchio.Intersezione(cerchi);
                                if (!(Double.IsNaN(point.Ascissa) || Double.IsNaN(point.Ordinata)))
                                {
                                    if (point.isInside(schede))
                                    {
                                        DatiDispositivo p = new DatiDispositivo(mac, timestamp, point, global);
                                        list.Add(p);
                                    }
                                    else
                                    {
                                        System.Diagnostics.Debug.WriteLine("Geofence discarded: (" + point.Ascissa + "; " + point.Ordinata + ")");
                                    }
                                }
                            }
                        }

                        InserisciPosizioni(list, connessione);
                    }
                Connesso = true;
                return(list);
            }
            catch (KeyNotFoundException e)
            {
                System.Diagnostics.Debug.WriteLine("idBoard not found..." + e.ToString());
                throw e;
            }
            catch (MySqlException e)
            {
                System.Diagnostics.Debug.WriteLine("MySqlException catched." + e.ToString());
                Connesso = false;
                return(null);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Caught exception: " + e.ToString());
                return(null);
            }
        }