Exemplo n.º 1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            using (BleDBContext db = new BleDBContext())
            {
                Bitmap   bitmap = drawBaseImage();
                Graphics g      = Graphics.FromImage(bitmap);

                foreach (BleBeacon beacon in db.BleBeacons)
                {
                    List <BleLastLocation> locations = db.LastLocations.Where(l => l.BleBeaconsId == beacon.BleBeaconsId).ToList();
                    BleLastLocation        location  = locations.OrderByDescending(l => l.Date).FirstOrDefault();

                    if (location != null)
                    {
                        drawCircle(g, Pens.Black, Brushes.Black, (float)coordinateWithOffset(location.X), (float)coordinateWithOffset(location.Y), 3);
                        //g.DrawEllipse(Pens.Black, (float)((location.X * 100 + 24) - 3), (float)((location.Y * 100 + 24) - 3), 3 + 3, 3 + 3);
                        //g.FillEllipse(Brushes.Black, (float)((location.X * 100 + 24) - 3), (float)((location.Y * 100 + 24) - 3), 3 + 3, 3 + 3);

                        g.DrawString(beacon.Name, DefaultFont, Brushes.Black, (float)(location.X * metersToPixels + (extraBorder + 9)), (float)(location.Y * metersToPixels + (extraBorder - 11)));
                    }
                }
                pictureBox1.Image = bitmap;
            }
        }
Exemplo n.º 2
0
        private void locationCalculation()
        {
            while (!shouldStop)
            {
                Dictionary <string, Dictionary <BleNode, double> > copyOfDistances = CopyOfDistances;
                foreach (string mac in copyOfDistances.Keys)
                {
                    List <BleNode> nodeList = new List <BleNode>();
                    List <double>  distList = new List <double>();

                    /*
                     * PointF a = new PointF();
                     * PointF b = new PointF();
                     * PointF c = new PointF();
                     *
                     * float dA = 0;
                     * float dB = 0;
                     * float dC = 0;
                     */

                    lock (distances[mac])
                    {
                        int i = 0;
                        foreach (BleNode node in distances[mac].Keys)
                        {
                            if (i++ > 2)
                            {
                                break;
                            }

                            nodeList.Add(node);
                            distList.Add(distances[mac][node]);
                        }
                    }

                    if (nodeList.Count == 3 && distList.Count == 3)
                    {
                        /*
                         * a = new PointF((float)nodeList[0].X, (float)nodeList[0].Y);
                         * b = new PointF((float)nodeList[1].X, (float)nodeList[1].Y);
                         * c = new PointF((float)nodeList[2].X, (float)nodeList[2].Y);
                         *
                         * dA = (float)distList[0];
                         * dB = (float)distList[1];
                         * dC = (float)distList[2];
                         */

                        //Vector2 vector = GetLocation(nodeList, distList);
                        Vector2 vector = trilaterate2DLinear(nodeList, distList);
                        //PointF point = GetLocationWithCenterOfGravity(a, b, c, dA, dB, dC);

                        Location location = new Location();
                        location.X = vector.X;
                        location.Y = vector.Y;

                        //location.X = point.X;
                        //location.Y = point.Y;
                        location.Date = DateTime.Now;

                        BleBeacon beacon = beacons.Find(bea => bea.MacAddress == mac);

                        if (beacon != null)
                        {
                            location.BleBeaconsId = beacon.BleBeaconsId;

                            /*
                             * using (BleBeaconServerContext db = new BleBeaconServerContext())
                             * {
                             *  foreach (BleNode node in nodeList)
                             *  {
                             *      bool foundDistance = false;
                             *      foreach (BleDistance distance in db.Distances)
                             *      {
                             *          if (distance.BleBeaconsId == beacon.BleBeaconsId && distance.BleNodesId == node.BleNodesId)
                             *          {
                             *              distance.Distance = distList[nodeList.IndexOf(node)];
                             *              foundDistance = true;
                             *              break;
                             *          }
                             *      }
                             *
                             *      if (!foundDistance)
                             *      {
                             *          BleDistance distance = new BleDistance();
                             *          distance.BleBeaconsId = beacon.BleBeaconsId;
                             *          distance.BleNodesId = node.BleNodesId;
                             *          distance.Distance = distList[nodeList.IndexOf(node)];
                             *          db.Distances.Add(distance);
                             *      }
                             *  }
                             *
                             *  db.SaveChanges();
                             * }*/

                            using (BleBeaconServerContext db = new BleBeaconServerContext())
                            {
                                bool lastLocationFound = false;
                                foreach (BleLastLocation bleLastLocation in db.LastLocations)
                                {
                                    if (bleLastLocation.BleBeaconsId == beacon.BleBeaconsId)
                                    {
                                        bleLastLocation.Date = location.Date;
                                        bleLastLocation.X    = location.X;
                                        bleLastLocation.Y    = location.Y;
                                        lastLocationFound    = true;
                                        break;
                                    }
                                }

                                if (!lastLocationFound)
                                {
                                    BleLastLocation lastLocation = new BleLastLocation();
                                    lastLocation.BleBeaconsId = beacon.BleBeaconsId;
                                    lastLocation.Date         = location.Date;
                                    lastLocation.X            = location.X;
                                    lastLocation.Y            = location.Y;
                                    db.LastLocations.Add(lastLocation);
                                }
                                //Saving location history data to database
                                if (!lastLocations.ContainsKey(beacon) || lastLocations[beacon] <= DateTime.Now.AddHours(-1))
                                {
                                    if (!lastLocations.ContainsKey(beacon))
                                    {
                                        lastLocations.Add(beacon, location.Date);
                                    }
                                    else
                                    {
                                        lastLocations[beacon] = location.Date;
                                    }

                                    db.Locations.Add(location);
                                }
                                db.SaveChanges();
                            }


                            lock (locations)
                            {
                                if (locations.ContainsKey(mac))
                                {
                                    locations[mac] = location;
                                }
                                else
                                {
                                    locations.Add(mac, location);
                                }
                            }
                        }
                    }
                }

                Thread.Sleep(1000);
            }
        }