Exemple #1
0
        public static BeaconData ParseValues(byte[] data)
        {
            BeaconData beaconData = null;

            if (data != null)
            {
                if (data.Length > 13)
                {
                    beaconData = new BeaconData();
                    //beaconData.mac = string.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", data[12], data[11], data[10], data[9], data[8], data[7]);
                    beaconData.mac = data[12].ToString("X2") + ":" + data[11].ToString("X2") + ":" + data[10].ToString("X2") + ":" + data[9].ToString("X2") + ":" + data[8].ToString("X2") + ":" + data[7].ToString("X2");

                    byte rssiByte = data[data.Length - 1];
                    if (rssiByte > 0x80)
                    {
                        beaconData.rssi = -(rssiByte & 0x7F);
                    }
                    else
                    {
                        beaconData.rssi = rssiByte;
                    }

                    //beaconData.rssi = Convert.ToInt32(data[data.Length - 1]);
                    //aconData.rssi = rssiValue;
                }
            }
            return(beaconData);
        }
Exemple #2
0
 public PebblebeeData(BeaconData data)
 {
     this.Mac    = data.Mac;
     this.Rssi   = data.Rssi;
     this.Type   = data.Type;
     this.Date   = data.Date;
     this.Beacon = data.Beacon;
 }
Exemple #3
0
        private void AddToNodeMap(BleNode node, BeaconData data)
        {
            string mac  = data.Mac;
            double rssi = data.Rssi;

            //Dictionary<BleNode, RollingList<int>> nodeMap;
            Dictionary <BleNode, FilterContainer> nodeMap;

            if (mac != null && rssi != 0)
            {
                if (beaconMap.ContainsKey(mac))
                {
                    nodeMap = beaconMap[mac];
                }
                else
                {
                    //nodeMap = new Dictionary<BleNode, RollingList<int>>();
                    lock (beaconMap)
                    {
                        nodeMap = new Dictionary <BleNode, FilterContainer>();
                        beaconMap.Add(mac, nodeMap);
                    }
                }

                if (nodeMap != null)
                {
                    if (nodeMap.ContainsKey(node))
                    {
                        lock (nodeMap[node])
                        {
                            //nodeMap[node].Add(packet.rssi);

                            nodeMap[node].Filter.Update(new[] { (double)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)rssi });
                            container.LastEstimate = container.Filter.getState()[0];
                        }
                        //nodeMap.Add(node, list);
                        lock (nodeMap)
                        {
                            nodeMap.Add(node, container);
                        }
                    }
                }
            }
        }
 public RuuvitagData(BeaconData data)
 {
     this.Rssi   = data.Rssi;
     this.Mac    = data.Mac;
     this.Type   = data.Type;
     this.Date   = data.Date;
     this.Beacon = data.Beacon;
     this.Node   = data.Node;
 }
Exemple #5
0
 public void AddData(BeaconData data)
 {
     lock (beaconDatas)
     {
         if (beaconDatas.ContainsKey(data.Mac))
         {
             beaconDatas[data.Mac] = data;
         }
         else
         {
             beaconDatas.Add(data.Mac, data);
         }
     }
 }
Exemple #6
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);
             *          }
             *      }
             *  }*/

            //}
        }