Exemple #1
0
        public BeaconPacketHandler()
        {
            using (BleBeaconServerContext db = new BleBeaconServerContext())
            {
                if (db.BleNodes != null)
                {
                    foreach (BleNode node in db.BleNodes)
                    {
                        nodes.Add(node);
                    }
                }
                if (db.BleBeacons != null)
                {
                    foreach (BleBeacon beacon in db.BleBeacons)
                    {
                        beacons.Add(beacon);
                    }
                }
                if (db.Maps != null)
                {
                    foreach (Map map in db.Maps)
                    {
                        this.map = map;
                        break;
                    }
                    if (this.map == null)
                    {
                        map        = new Map();
                        map.Height = 0;
                        map.Width  = 0;
                        db.Maps.Add(map);
                        db.SaveChanges();
                    }
                }
            }

            distanceCalcThd = new Thread(distanceCalculation);
            distanceCalcThd.Start();

            locationCalcThd = new Thread(locationCalculation);
            locationCalcThd.Start();
        }
Exemple #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);
            }
        }
Exemple #3
0
        public void AddPacket(BeaconPacket packet)
        {
            BleNode node = nodes.Find(n => n.Sender == packet.Sender);

            if (node == null)
            {
                node        = new BleNode();
                node.Sender = packet.Sender;
                node.MapsId = map.MapsId;
                nodes.Add(node);

                using (BleBeaconServerContext db = new BleBeaconServerContext())
                {
                    db.BleNodes.Add(node);
                    db.SaveChanges();
                }
            }

            Types type = BeaconData.GetType(packet.ByteData);

            BeaconData beaconData = BeaconData.ParseValues(packet.ByteData);

            if (beaconData != null && type != Types.Unknown)
            {
                if (node != null)
                {
                    beaconData.Node = node;
                }
                if (beaconData.Mac != null && beaconData.Mac != "")
                {
                    BleBeacon beacon = beacons.Find(n => n.MacAddress == beaconData.Mac);
                    if (beacon == null)
                    {
                        beacon            = new BleBeacon();
                        beacon.MacAddress = beaconData.Mac;
                        beacons.Add(beacon);

                        using (BleBeaconServerContext db = new BleBeaconServerContext())
                        {
                            db.BleBeacons.Add(beacon);
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        beaconData.Beacon = beacon;
                    }
                }
            }

            if (type == Types.Ruuvitag)
            {
                RuuvitagParser parser    = new RuuvitagParser(beaconData);
                RuuvitagData   ruuviData = parser.ReadData(packet.ByteData);

                if (ruuviData != null)
                {
                    if (BeaconDataReceived != null)
                    {
                        BeaconDataReceived(ruuviData);
                    }

                    AddToNodeMap(node, ruuviData);
                }
            }
            else if (type == Types.APlant)
            {
                APlantParser parser     = new APlantParser(beaconData);
                APlantData   aplantData = parser.ReadData(packet.ByteData);

                if (aplantData != null)
                {
                    if (BeaconDataReceived != null)
                    {
                        BeaconDataReceived(aplantData);
                    }

                    AddToNodeMap(node, aplantData);
                }
            }
            else if (type == Types.PebbleBee)
            {
                PebblebeeParser parser     = new PebblebeeParser(beaconData);
                PebblebeeData   pebbleData = parser.ReadData(packet.ByteData);

                if (pebbleData != null)
                {
                    if (BeaconDataReceived != null)
                    {
                        BeaconDataReceived(pebbleData);
                    }

                    AddToNodeMap(node, pebbleData);
                }
            }

            /*if(packet.mac_address != null && packet.mac_address != "")
             * {
             *  BleBeacon beacon = beacons.Find(n => n.MacAddress == packet.mac_address);
             *  if(beacon == null)
             *  {
             *      beacon = new BleBeacon();
             *      beacon.MacAddress = packet.mac_address;
             *      beacons.Add(beacon);
             *
             *      using (BleBeaconServerContext db = new BleBeaconServerContext())
             *      {
             *          db.BleBeacons.Add(beacon);
             *          db.SaveChanges();
             *      }
             *  }
             *  //Dictionary<BleNode, RollingList<int>> nodeMap;
             *  Dictionary<BleNode, FilterContainer> nodeMap;
             *  if (beaconMap.ContainsKey(packet.mac_address))
             *  {
             *      nodeMap = beaconMap[packet.mac_address];
             *  } else
             *  {
             *      //nodeMap = new Dictionary<BleNode, RollingList<int>>();
             *      lock (beaconMap)
             *      {
             *          nodeMap = new Dictionary<BleNode, FilterContainer>();
             *          beaconMap.Add(packet.mac_address, nodeMap);
             *      }
             *  }
             *
             *  if(nodeMap != null)
             *  {
             *      if(nodeMap.ContainsKey(node))
             *      {
             *          lock (nodeMap[node])
             *          {
             *              //nodeMap[node].Add(packet.rssi);
             *
             *              nodeMap[node].Filter.Update(new[] { (double)packet.rssi });
             *              nodeMap[node].LastEstimate = nodeMap[node].Filter.getState()[0];
             *          }
             *      } else
             *      {
             *          //RollingList<int> list = new RollingList<int>(100);
             *          FilterContainer container = new FilterContainer();
             *          container.Filter = new UKF();
             *          //UKF filter = new UKF();
             *          //list.Add(packet.rssi);
             *          lock (container)
             *          {
             *              container.Filter.Update(new[] { (double)packet.rssi });
             *              container.LastEstimate = container.Filter.getState()[0];
             *          }
             *          //nodeMap.Add(node, list);
             *          lock (nodeMap)
             *          {
             *              nodeMap.Add(node, container);
             *          }
             *      }
             *  }*/

            //}
        }
Exemple #4
0
        private void distanceCalculation()
        {
            while (!shouldStop)
            {
                lock (beaconMap)
                {
                    foreach (string mac in beaconMap.Keys)
                    {
                        foreach (BleNode node in beaconMap[mac].Keys)
                        {
                            double rssiFiltered = 0;
                            lock (beaconMap[mac][node])
                            {
                                rssiFiltered = beaconMap[mac][node].LastEstimate;
                            }

                            double distance = CalculateDistance(txPower, rssiFiltered);

                            using (BleBeaconServerContext db = new BleBeaconServerContext())
                            {
                                BleBeacon beacon = beacons.Find(b => b.MacAddress == mac);
                                if (beacon != null)
                                {
                                    bool foundDistance = false;
                                    foreach (BleDistance bleDistance in db.Distances)
                                    {
                                        if (bleDistance.BleBeaconsId == beacon.BleBeaconsId && bleDistance.BleNodesId == node.BleNodesId)
                                        {
                                            bleDistance.Distance = distance;
                                            foundDistance        = true;
                                            break;
                                        }
                                    }

                                    if (!foundDistance)
                                    {
                                        BleDistance bleDistance = new BleDistance();
                                        bleDistance.BleBeaconsId = beacon.BleBeaconsId;
                                        bleDistance.BleNodesId   = node.BleNodesId;
                                        bleDistance.Distance     = distance;
                                        db.Distances.Add(bleDistance);
                                    }

                                    db.SaveChanges();
                                }
                            }

                            if (!distances.ContainsKey(mac))
                            {
                                lock (distances)
                                    distances.Add(mac, new Dictionary <BleNode, double>());
                            }

                            lock (distances[mac])
                            {
                                if (!distances[mac].ContainsKey(node))
                                {
                                    distances[mac].Add(node, distance);
                                }
                                else
                                {
                                    distances[mac][node] = distance;
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(1000);
            }
        }