Exemplo n.º 1
0
        private void dequeuePackets()
        {
            UdpReceiveResult result;
            int count = 0;

            while (!shouldStop)
            {
                try
                {
                    //byte[] data = ss.ReceiveFrameBytes();

                    lock (receivedUdpPackets)
                    {
                        count = receivedUdpPackets.Count;
                        if (count > 0)
                        {
                            result = receivedUdpPackets.Dequeue();
                        }
                    }

                    if (count == 0)
                    {
                        Thread.Sleep(10);
                        continue;
                    }
                    else
                    {
                        if (result != null && result.Buffer.Length > 0)
                        {
                            string data = System.Text.Encoding.UTF8.GetString(result.Buffer);

                            //byte[] data = (byte[])result.Buffer.Clone();

                            //Console.WriteLine(data);

                            if (result.Buffer.Length >= 2)
                            {
                                /*
                                 * int index = 0;
                                 * BeaconPacket packet = new BeaconPacket();
                                 * packet.SenderLength = BitConverter.ToUInt16(result.Buffer, 0);
                                 * index += 2;
                                 *
                                 * if(result.Buffer.Length < 2 + index)
                                 * {
                                 *  Console.WriteLine("[udplistener] Error at parsing packet: Buffer length too short!");
                                 * }
                                 *
                                 * packet.Sender = System.Text.Encoding.UTF8.GetString(result.Buffer, index, packet.SenderLength);
                                 * index += packet.SenderLength;
                                 * if(packet.Sender == null || packet.Sender == "" || packet.Sender.Length != 17)
                                 * {
                                 *  Console.WriteLine("[udplistener] Error at parsing packet: Sender not valid!");
                                 *  return;
                                 * }
                                 *
                                 * packet.DataLength = BitConverter.ToUInt16(result.Buffer, index);
                                 * index += 2;
                                 *
                                 * packet.Data = new byte[packet.DataLength];
                                 *
                                 *
                                 * if(packet.Data.Length >= result.Buffer.Length - index)
                                 * {
                                 *  for(int i = 0; i < packet.DataLength; i++)
                                 *  {
                                 *      packet.Data[i] = result.Buffer[index++];
                                 *  }
                                 * }
                                 */
                                //result.Buffer. CopyTo(packet.Data, index);

                                BeaconPacket packet = JsonConvert.DeserializeObject <BeaconPacket>(data);

                                if (BeaconPacketReceived != null)
                                {
                                    BeaconPacketReceived(packet);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Program.DebugWriter.WriteLine("[nflistener] Catched exception in loop dequeuePackets " + ex.Message + "\r\n\r\n" + ex.StackTrace);
                    Console.WriteLine("[udplistener] Catched exception in loop dequeuePackets " + ex.Message + "\r\n\r\n" + ex.StackTrace);
                }
            }
        }
Exemplo n.º 2
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);
             *          }
             *      }
             *  }*/

            //}
        }