public void EstimateNotHiddenPositions()
        {
            Int32 timestamp_from            = (Int32)Form_Home.DateTimeToUnixTimestamp(DateTime.Now);
            List <DevicePosition> positions = null;

            using (var db = new LiteDatabase(@"MyData.db"))
            {
                var col = db.GetCollection <Packet>("packets");

                //estraggo gli hash dei pacchetti validi ovvero faccio il controllo che siano pacchetti del timeslot corretto
                // che i pacchetti siano stati ricevuti da tutti e tre le schedine (una schedina potrebbe aver catturato lo stesso pacchetto più volte!!)

                /*var validHashes = col.Find(packet => packet.timestamp >= timestamp_from - 60 && packet.timestamp <= timestamp_from && packet.mac != "hidden")
                 *                   .GroupBy(x => new { x.mac, x.hash, x.boardId }, (key, packet) => new { key = key, Count = packet.Count() })
                 *                   .GroupBy(x => new { x.key.hash, x.key.mac }, (key, packet) => new { key = key, Count = packet.Count() })
                 *                   .Where(grp => grp.Count == BoardLoader.Instance.NumberOfBoards)
                 *                   .Select(p => p.key.hash);*/

                var validHashes = col.Find(packet => packet.timestamp >= timestamp_from - 60 && packet.timestamp <= timestamp_from && packet.mac != "hidden")
                                  .GroupBy(x => new { x.mac, hash_t = x.mac + "_" + x.timestamp.ToString().Substring(0, x.timestamp.ToString().Length - 1), x.boardId }, (key, packet) => new { key = key, Count = packet.Count() })
                                  .GroupBy(x => new { x.key.hash_t, x.key.mac }, (key, packet) => new { key = key, Count = packet.Count() })
                                  .Where(grp => grp.Count == BoardLoader.Instance.NumberOfBoards)
                                  .Select(p => p.key.hash_t);


                Debug.WriteLine("Dispositivi Triangolati (query_1):" + validHashes.Count());

                //Non faccio il filter per timestamp incluso nella generazione dell'hash
                //seleziono MAC,ID,AVGRSSI

                /*var deviceInfoList = col.FindAll()
                 *                      .Where(p => validHashes.Contains(p.hash))
                 *                      .GroupBy(x => new { x.mac, x.boardId },
                 *                                   (key, values) => new
                 *                                   {
                 *                                       key = key,
                 *                                       AVGRSSI = values.Average(x => x.rssi)
                 *
                 *                                   }).ToList();*/

                var deviceInfoList = col.FindAll()
                                     .Where(p => validHashes.Contains(p.mac + "_" + p.timestamp.ToString().Substring(0, p.timestamp.ToString().Length - 1)))
                                     .GroupBy(x => new { x.mac, x.boardId },
                                              (key, values) => new
                {
                    key     = key,
                    AVGRSSI = values.Average(x => x.rssi)
                }).ToList();

                //Debug.WriteLine("MAC+ID+AVG_RSSI trovati (query_2):"+deviceInfoList.Count());

                //mappa che contiene per ogni MAC una lista di stringhe del tipo 'ID'_'RSSI'
                Dictionary <string, List <string> > deviceInfo = new Dictionary <string, List <string> >();


                foreach (var info in deviceInfoList)
                {
                    string key   = info.key.mac;
                    string value = info.key.boardId + "_" + info.AVGRSSI.ToString();

                    if (!deviceInfo.ContainsKey(key))
                    {
                        List <string> id_rssi_list = new List <string> {
                            value
                        };

                        deviceInfo.Add(key, id_rssi_list);
                    }
                    else
                    {
                        deviceInfo[key].Add(value);
                    }
                }

                //MAC,X,Y
                positions = Trilateration(deviceInfo);
                var colPositions = db.GetCollection <DevicePosition>("positions");
                foreach (DevicePosition position in positions)
                {
                    colPositions.Insert(position);
                }
            }
        }
        public void  EstimateHiddenPositions()
        {
            double threshold = Form_Home.getThreshold();

            Debug.WriteLine(threshold);

            Int32 timestamp_from = (Int32)Form_Home.DateTimeToUnixTimestamp(DateTime.Now);
            List <DevicePosition> hiddenPositions = null;

            using (var db = new LiteDatabase(@"MyData.db"))
            {
                var col = db.GetCollection <Packet>("packets");
                //prendo gli hash di quei pacchetti che:
                //- sono stati ricevuti nello slot temporale corretto
                //- sono stati ricevuti da tutte e tre le schedine
                //N.B rispetto alla funzione standard qui lavoriamo sul sequenceNO e non sul mac dato che è casuale
                var validHiddenHashes = col.Find(packet => packet.timestamp >= timestamp_from && packet.timestamp <= timestamp_from + 60 && packet.mac == "hidden")
                                        .GroupBy(x => new { x.sequenceNumber, x.hash, x.boardId }, (key, packet) => new { key = key, Count = packet.Count() })
                                        .GroupBy(x => new { x.key.hash, x.key.sequenceNumber }, (key, packet) => new { key = key, Count = packet.Count() })
                                        .Where(grp => grp.Count == BoardLoader.Instance.NumberOfBoards)
                                        .Select(p => p.key.hash);

                // nel groupBy la presenza del mac sarà ininfluente nel raggruppamento ma servirà successivamente per assegnare differenziarli dai pacchetti standard
                var hiddenDeviceList = col.FindAll()
                                       .Where(p => validHiddenHashes.Contains(p.hash))
                                       .GroupBy(x => new { x.mac, x.hash, x.boardId },
                                                (key, values) => new
                {
                    key     = key,
                    AVGRSSI = values.Average(x => x.rssi),
                }).ToList();

                Dictionary <string, List <string> > hiddenDeviceInfo = new Dictionary <string, List <string> >();

                foreach (var device in hiddenDeviceList)
                {
                    string key   = device.key.hash.Substring(28) + "_hidden";
                    string value = device.key.boardId + "_" + device.AVGRSSI.ToString();

                    if (!hiddenDeviceInfo.ContainsKey(key))
                    {
                        List <string> id_rssi_list = new List <string> {
                            value
                        };
                        hiddenDeviceInfo.Add(key, id_rssi_list);
                    }
                    else
                    {
                        hiddenDeviceInfo[device.key.hash].Add(value);
                    }
                }

                hiddenPositions = Trilateration(hiddenDeviceInfo);
                List <DevicePosition> hiddenDevicePositions = null;

                foreach (var position1 in hiddenPositions)
                {
                    hiddenDevicePositions.Add(position1);
                    hiddenPositions.Remove(position1);

                    foreach (var position2 in hiddenPositions)
                    {
                        if (EuclideanDistance(position1.X, position1.Y, position2.X, position2.Y, threshold))
                        {
                            hiddenPositions.Remove(position2);
                        }
                    }
                }

                if (hiddenDevicePositions != null && hiddenDevicePositions.Count() != 0)
                {
                    var positionCollection = db.GetCollection <DevicePosition>("positions");

                    foreach (var position in hiddenDevicePositions)
                    {
                        positionCollection.Insert(position);
                    }
                }
            }
        }