예제 #1
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);
             *          }
             *      }
             *  }*/

            //}
        }
예제 #2
0
        public void writeFile()
        {
            while (!shouldStop)
            {
                Dictionary <string, BeaconData> beaconDataCopy = new Dictionary <string, BeaconData>();
                lock (beaconDatas)
                {
                    foreach (string mac in beaconDatas.Keys)
                    {
                        beaconDataCopy.Add(mac, beaconDatas[mac]);
                    }
                }

                string tempFile = filename + ".temp";


                try
                {
                    using (System.IO.StreamWriter file =
                               new System.IO.StreamWriter(tempFile, false))
                    {
                        foreach (BeaconData data in beaconDataCopy.Values)
                        {
                            if (data.Date.AddMinutes(30) < DateTime.Now)
                            {
                                lock (beaconDatas)
                                {
                                    beaconDatas.Remove(data.Mac);
                                }
                                continue;
                            }
                            else
                            {
                                string name = data.Mac;
                                if (data.Beacon != null && data.Beacon.Name != null && data.Beacon.Name != "")
                                {
                                    name = data.Beacon.Name;
                                }

                                string node = "Unknown sender";
                                if (data.Node != null && data.Node.Sender != null && data.Node.Sender != "")
                                {
                                    node = data.Node.Sender;
                                }

                                if (data is RuuvitagData)
                                {
                                    RuuvitagData ruuviData = (RuuvitagData)data;

                                    int pressure = (int)ruuviData.Pressure / 100;
                                    file.WriteLine("tags{mac=\"" + ruuviData.Mac + "\",beaconname=\"" + name + "\",node=\"" + node + "\",type=\"ruuvitag\",valuetype=\"temp\"} " + ruuviData.Temp.ToString("D"));
                                    file.WriteLine("tags{mac=\"" + ruuviData.Mac + "\",beaconname=\"" + name + "\",node=\"" + node + "\",type=\"ruuvitag\",valuetype=\"humidity\"} " + ruuviData.Humidity.ToString("0.00", CultureInfo.CreateSpecificCulture("en-US")));
                                    file.WriteLine("tags{mac=\"" + ruuviData.Mac + "\",beaconname=\"" + name + "\",node=\"" + node + "\",type=\"ruuvitag\",valuetype=\"pressure\"} " + pressure.ToString("D"));
                                }
                                else if (data is APlantData)
                                {
                                    APlantData aplantData = (APlantData)data;

                                    file.WriteLine("tags{mac=\"" + data.Mac + "\",beaconname=\"" + name + "\",node=\"" + node + "\",type=\"aplant\",valuetype=\"temp\"} " + aplantData.Temp.ToString("D"));
                                    file.WriteLine("tags{mac=\"" + data.Mac + "\",beaconname=\"" + name + "\",node=\"" + node + "\",type=\"aplant\",valuetype=\"soilmoisture\"} " + aplantData.SoilMoisture.ToString("D"));
                                }
                            }
                        }
                    }

                    lock (beaconDatas)
                    {
                        Trace.WriteLine("[beacondatagrafanafilewriter] Copying grafana file from temp file to actual file. BeaconDatas count: " + beaconDatas.Count);
                    }
                    File.Copy(tempFile, filename, true);
                    File.Delete(tempFile);
                } catch (Exception ex)
                {
                    Trace.WriteLine("[beacondatagrafanafilewriter] Exception caugth: " + ex.Message);
                }
                for (int i = 0; i < 15; i++)
                {
                    if (!shouldStop)
                    {
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }