public IEnumerable <Signal> Signals()
        {
            using (var context = new LightningContext())
            {
                var output = new List <Signal>();

                var results = (from d in context.Datapackets.OrderByDescending(x => x.Id)
                               join detector in context.Detectors on d.Detectoruid equals detector.Id

                               select new { d.Data, detectorName = detector.Name, d.Received, d.Id }).Take(30);

                foreach (var d in results.ToList())

                {
                    var o = new ushort[728];
                    Buffer.BlockCopy(d.Data, 0, o, 0, 1456);
                    output.Add(new Signal()
                    {
                        Data = o, Detector = d.detectorName, Received = d.Received, ReceivedString = FromUnixTime(d.Received).ToString(), Id = d.Id
                    }
                               );
                }
                ;
                return(output);
            }
        }
Beispiel #2
0
        public void StrikeTest()
        {
            using (var context = new LightningContext())
            {
                var t = context.Datapackets.Join(context.Datapackets, x => x.Received, y => y.Received, (x, y) => new { Left = x, Right = y });


                var s = t.Where(x => x.Left.Received == x.Right.Received).Where(x => x.Left.Detectoruid > x.Right.Detectoruid)
                        .Select(x =>
                                new
                {
                    lID   = x.Left.Detectoruid,
                    rID   = x.Right.Detectoruid,
                    lTime = x.Left.Received,
                    rTime = x.Right.Received
                }).ToArray();

                if (t.Any())
                {
                    var st = s.Select(x => new Strike()
                    {
                        StrikeTime = x.lTime
                    }).ToArray();
                }
            }
        }
        public IEnumerable <Detector> Detectors()
        {
            List <Detector> detectorList = new List <Detector>();

            using (var context = new LightningContext())
            {
                var detectorIDs = (from sp in context.Statuspackets
                                   join det in context.Detectors
                                   on sp.Detectoruid equals det.Id

                                   select new
                {
                    sp.Detectoruid,
                    sp.Gpslon,
                    sp.Gpslat,
                    sp.Received,
                    det.Name
                }).Where(x => x.Gpslon != 0 && x.Gpslat != 0).Distinct().GroupBy(x => x.Detectoruid).Select(x => x.Select(d => new Detector()
                {
                    Name           = d.Name,
                    Lat            = d.Gpslat,
                    Lon            = d.Gpslon,
                    Received       = d.Received,
                    ReceivedString = FromUnixTime(d.Received ?? 0).ToString()
                }));

                detectorList.AddRange(detectorIDs.SelectMany(x => x.OrderByDescending(y => y.Received).Take(1)));
            }
            return(detectorList);
        }
        public IEnumerable <Strike> Strikes()
        {
            using (var context = new LightningContext())
            {
                var t = context.Datapackets.Join(context.Datapackets, x => x.Received, y => y.Received, (x, y) => new { Left = x, Right = y })
                        .Where(x => x.Left.Detectoruid != x.Right.Detectoruid)
                        .Where(x => x.Left.Received == x.Right.Received)
                        .Select(x =>
                                new
                {
                    lID   = x.Left.Detectoruid,
                    rID   = x.Right.Detectoruid,
                    lTime = x.Left.Received,
                    rTime = x.Right.Received
                }).Take(10).ToArray();

                if (t.Any())
                {
                    return(t.Select(x => new Strike()
                    {
                        Received = x.lTime
                    }).ToArray());
                }
                return(null);
            }
        }
Beispiel #5
0
        public void Process(LightningContext context)
        {
            var closePackets = context.Datapackets.Where(x => x.Received > this.packet.Received - TOACorrelator.MAXDELAY);

            var fullInfo = (from data in closePackets
                            join
                            status in context.Statuspackets
                            on data.Batchid equals status.Batchid
                            select new
            {
                DetectionInstance = DetectionInstance.FromPacket(data),
                Status = status
            }).ToList();

            foreach (var x in fullInfo)
            {
                x.DetectionInstance.DetectorLat = x.Status.Gpslat.Value;
                x.DetectionInstance.DetectorLon = x.Status.Gpslon.Value;
            }

            Strike strike = TOACorrelator.Correlate(fullInfo.Select(x => x.DetectionInstance).ToList());

            if (strike != null)
            {
                context.Add(strike);
            }
        }
 public uint Register(string unique)
 {
     if (unique == null)
     {
         return(0);
     }
     // check db for existing DetectorID
     using (var context = new LightningContext())
     {
         var found = context.Detectors.Where(x => x.Devicecode == unique).Select(x => x.Id).FirstOrDefault();
         if (found != 0)
         {
             return(found);
         }
         else
         {
             var nextId = context.Detectors.Max(x => x.Id) + 1;
             context.Detectors.Add(new Detectors()
             {
                 Id = nextId, Devicecode = unique
             });
             context.SaveChanges();
             return(nextId);
         }
     }
 }
Beispiel #7
0
        public static void ServerThread()
        {
            //TODO: Provide datapacketbuffer access as a service (signal?) to provide realtime counts without hitting the DB

            var dataPacketBuffer = new List <DetectionDataPacket>();

            UdpClient udpClient = new UdpClient(5000);

            while (true)
            {
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                Models.IncomingRawUdpPacket potentialPacket = new Models.IncomingRawUdpPacket(udpClient.Receive(ref RemoteIpEndPoint));

                potentialPacket.PopulateFromIncomingPacket(RemoteIpEndPoint);
                Console.WriteLine($"{potentialPacket.GetPacketType().ToString()}");
                if (potentialPacket.GetPacketType() != Models.PacketType.Unknown)
                {
                    var newPacket = potentialPacket.Generate();
                    Console.WriteLine($"New packet incoming on thread {Thread.CurrentThread.ManagedThreadId} : {potentialPacket.IPAddress}:{potentialPacket.IPPort}");

                    // Throw in buffer
                    if (newPacket is DetectionDataPacket dPacket)
                    {
                        dataPacketBuffer.Add(dPacket);
                        //TODO: If coincedent, keep it, else wait till it ages
                        var data = FindStrike(dataPacketBuffer);
                        data.ForEach(x => dataPacketBuffer.Remove(x));
                        if (data != null)
                        {
                            Task.Run(() =>
                            {
                                using (var context = new LightningContext())
                                {
                                    foreach (var d in data)
                                    {
                                        d.StoreInDB(context);
                                    }
                                }
                            });
                        }

                        dataPacketBuffer.RemoveAll(x => x.GetPacket().Persisteddate < DateTime.Now.AddMilliseconds(-500).Ticks);
                    }
                    else
                    {
                        Task.Run(() =>
                        {
                            using (var context = new LightningContext())
                            {
                                newPacket.StoreInDB(context);
                            }
                        });
                    }
                }
                else
                {
                    Task.Run(() => Console.WriteLine($"Unknown Packet incoming on thread {Thread.CurrentThread.ManagedThreadId}"));
                }
            }
        }
 public InfoDump GetInfoDump()
 {
     using (var context = new LightningContext())
     {
         InfoDump dump = new InfoDump
         {
             StatusPacketCount = context.Statuspackets.Count(),
             DataPacketCount   = context.Datapackets.Count(),
             DetectorCount     = context.Detectors.Count()
         };
         return(dump);
     }
 }
Beispiel #9
0
        public void StoreInDB()
        {
            Rawpackets packet = new Rawpackets
            {
                Data    = RawBytes,
                Port    = IPPort.ToString(),
                Address = IPAddress.ToString(),
            };

            using (var context = new LightningContext())
            {
                context.Add(packet);
                context.SaveChanges();
            }
        }
 public long RealtimeStatusPacketCount()
 {
     try
     {
         using (var context = new LightningContext())
         {
             var n     = DateTime.Now;
             var u     = n.Ticks;
             var limit = n.AddSeconds(-5).Ticks;
             return(context.Statuspackets.Count(x => x.Persisteddate > limit && x.Persisteddate < u));
         }
     }
     catch
     {
     }
     finally
     {
     }
     return(0);
 }
Beispiel #11
0
 public void StoreInDB(LightningContext context)
 {
     if (!packet.IsReady())
     {
         throw new InvalidOperationException("Packet not constructed properly");
     }
     try
     {
         context.Add(packet);
         Process(context);
         context.SaveChanges();
     }
     catch (Exception ex)
     {
         Console.Write(ex.Message);
         if (ex.InnerException != null)
         {
             Console.Write(ex.InnerException.Message);
         }
     }
 }
Beispiel #12
0
        public static void CompileKernels()
        {
            context = new Context();

            lc_cuda = LightningContext.CreateCudaContext(context);
            lc_cpu  = LightningContext.CreateCPUContext(context, GroupSize);

            fibonacci_cuda_kernel = lc_cuda.LoadCachedKernel(typeof(Fibonacci).GetMethod(
                                                                 nameof(FibonacciKernel),
                                                                 BindingFlags.NonPublic | BindingFlags.Static)
                                                             );
            fibonacci_shared_cuda_kernel = lc_cuda.LoadCachedKernel(typeof(Fibonacci).GetMethod(
                                                                        nameof(FibonacciSharedKernel),
                                                                        BindingFlags.NonPublic | BindingFlags.Static)
                                                                    );
            System.IO.File.WriteAllBytes("Kernel.ptx", fibonacci_shared_cuda_kernel.CompiledKernel.GetBuffer());
            fibonacci_cpu_kernel = lc_cpu.LoadCachedKernel(typeof(Fibonacci).GetMethod(
                                                               nameof(FibonacciKernel),
                                                               BindingFlags.NonPublic | BindingFlags.Static)
                                                           );
        }
 public void StoreInDB(LightningContext context)
 {
     if (!packet.IsReady())
     {
         throw new InvalidDataException("Packet not constructed properly");
     }
     Console.WriteLine($"Status Packet storing on thread {Thread.CurrentThread.ManagedThreadId}");
     try
     {
         context.Add(packet);
         context.SaveChanges();
     }
     catch (Exception e)
     {
         if (e.InnerException != null)
         {
             Console.WriteLine($"{e.InnerException.Message}  : storing status");
         }
         else
         {
             Console.WriteLine($"{e.Message}  : storing status");
         }
     }
 }
 public void Process(LightningContext context)
 {
     throw new NotImplementedException();
 }