Example #1
0
        public async Task<Beacon> Ping(Guid uuid, int major, int minor, int proximity = 0)
        {
            var beaconProvider = TypeResolver.IBeaconProvider;

            var ret = beaconProvider.FindBeacon(uuid, major, minor);

            using (var db = new Context())
            {
                var client = await db.Clients.FindAsync(ClientID);
                db.Beacons.Attach(ret);

                var ping = new BeaconPing
                {
                    Client = client,
                    Date = DateTime.Now,
                    Beacon = ret,
                    Proximity = proximity
                };

                db.BeaconPings.Add(ping);
                await db.SaveChangesAsync();

                await TypeResolver.IScreenManager.UpdateClient(ping);

            }

            return ret;
        }
 public Beacon FindExactBeacon(Guid uuid, int major, int minor)
 {
     using (var db = new Context())
     {
         return db.Beacons.Find(uuid, major, minor);
     }
 }
Example #3
0
        public object GetClient(Guid clientid, int? major = null)
        {
            using (var db = new Context())
            {
                db.Configuration.LazyLoadingEnabled = false;
                db.Configuration.ProxyCreationEnabled = false;

                var client = db.Clients.Find(clientid);

                if (client == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var subq = db
                    .Entry(client)
                    .Collection(c => c.BeaconPings)
                    .Query()
                    .Include(c => c.Beacon)
                    .Where(c => c.Date >= ThirtyMinutesAgo);

                if (major.HasValue)
                {
                    subq = subq.Where(c => c.Beacon.Major == major.Value);
                }

                subq = subq
                    .OrderBy(c => c.Date);

                subq.Load();

                return client;
            }
        }
Example #4
0
        public object GetAllClients(int? major = null)
        {
            using (var db = new Context())
            {
                db.Configuration.LazyLoadingEnabled = false;

                var ret = db.Clients.AsQueryable();

                if (major.HasValue)
                {
                    ret = ret.Where(c => c.BeaconPings.Any(q => q.Date >= ThirtyMinutesAgo && q.Beacon.Major == major.Value));
                }
                else
                {
                    ret = ret.Where(c => c.BeaconPings.Any(q => q.Date >= ThirtyMinutesAgo));
                }

                ret = ret.OrderBy(c => c.Name);

               /* foreach (var item in ret)
                {
                    db
                        .Entry(item)
                        .Collection(i => i.BeaconPings)
                        .Query()
                        .Include(q => q.Beacon)
                        .Where(q => q.Date >= thirtyMinutesAgo)
                        .Load();

                }*/

                return ret.ToList();
            }
        }
 public IDictionary<int, Beacon> GetBeaconsByUuidAndMajor(Guid uuid, int major)
 {
     using (var db = new Context())
     {
         var ret = db.Beacons.Where(b => b.UUID == uuid && b.Major == major).ToDictionary(d => d.Minor, d => d);
         return ret;
     }
 }
 public IDictionary<int, Dictionary<int, Beacon>> GetBeaconsByUuid(Guid uuid)
 {
     using (var db = new Context())
     {
         var ret = db.Beacons.Where(b => b.UUID == uuid).GroupBy(c => c.Major).ToDictionary(c => c.Key, c => c.ToDictionary(d => d.Minor, d => d));
         return ret;
     }
 }
Example #7
0
 public ActionResult Clear()
 {
     using (var db = new Context())
     {
         db.Database.ExecuteSqlCommand("DELETE FROM BeaconPings");
     }
     return Content("OK", "text/plain");
 }
Example #8
0
        public async Task Clear()
        {
            using (var db = new Context())
            {
                /*db.BeaconPings.Update(
                    b => b.Client.ClientID == ClientID && !b.Cleared,
                    b => new BeaconPing { Cleared = true }
                );*/

                await db.Database.ExecuteSqlCommandAsync(@"UPDATE [BeaconPings] SET Cleared = 1 WHERE Client_ClientID = @p0 AND Cleared = 0", ClientID);
            }
        }
        public Beacon FindBeacon(Guid uuid, int major, int minor)
        {
            using (var db = new Context())
            {
                var ret = db.Beacons.Find(uuid, major, minor);

                if(ret == null) ret = db.Beacons.Find(uuid, major, 0);
                if(ret == null) ret = db.Beacons.Find(uuid, 0, 0);
                if(ret == null) ret = db.Beacons.Find(Guid.Empty, 0, 0);

                return ret;
            }
        }
Example #10
0
        public IEnumerable<object> GetState()
        {
            var beacons = TypeResolver.IBeaconProvider;

            using (var db = new Context())
            {
                return db.BeaconPings
                    .Include("Beacon")
                .Where(b => b.Client.ClientID == ClientID && !b.Cleared)
                .OrderByDescending(c => c.Date)
                .Take(10)
                .Where(q => q.Beacon.MaxProximity == 0 || q.Proximity < q.Beacon.MaxProximity)
                
                .ToList();
            }
        }
        public IDictionary<Guid, Dictionary<int, Dictionary<int, Beacon>>> GetAllBeacons()
        {
            using (var db = new Context())
            {
                var ret = db.Beacons
                    .GroupBy(b => b.UUID)
                    .ToDictionary(b => b.Key, b => b
                        .GroupBy(c => c.Major)
                        .ToDictionary(c => c.Key, c => c
                            .ToDictionary(d => d.Minor, d => d)
                         )
                    )
                ;

                return ret;
            }
        }
        internal static void Logic(HttpContext context, IEnsureClientController controller)
        {
            Guid clientid = Guid.Empty;
            var cookie = context.Request.Cookies["ClientID"];

            if (cookie != null)
            {
                Guid.TryParse(cookie.Value, out clientid);
            }

            if (clientid == Guid.Empty)
            {
                clientid = Guid.NewGuid();
            }

            cookie = new HttpCookie("ClientID", clientid.ToString());
            cookie.Domain = context.Request.Url.Host;
            cookie.Expires = DateTime.Now.AddYears(1);
            cookie.HttpOnly = true;
            context.Response.SetCookie(cookie);

            using (var db = new Context())
            {
                var client = db.Clients.Find(clientid);

                if (client == null)
                {
                    client = new Client
                    {
                        ClientID = clientid,
                        Name = "Unnamed"
                    };

                    db.Clients.Add(client);

                    db.SaveChanges();
                }
            }

            if (controller != null)
            {
                controller.ClientID = clientid;
            }
        }
Example #13
0
        public void AddScreen(Guid identifier, WebSocket socket)
        {
            lock (lockObject)
            {
                Sockets.Add(identifier, socket);
            }

            //now, let's send updates for existing clients
            using (var db = new Context())
            {
                var ret = db.Clients.AsQueryable()
                    .Where(c => c.BeaconPings.Any(q => q.Date >= ClientController.ThirtyMinutesAgo))
                    .OrderBy(c => c.Name);

                foreach (var client in ret)
                {
                    //get their latest beacon
                    var beaconping = client.BeaconPings.OrderByDescending(b => b.Date).First();
                    UpdateClient(beaconping);
                }
            }
        }
        public Beacon StoreBeacon(Beacon beacon)
        {
            using (var db = new Context())
            {
                var oldBeacon = db.Beacons.Find(beacon.UUID, beacon.Major, beacon.Minor);

                if (oldBeacon != null)
                {

                    ((IObjectContextAdapter)db).ObjectContext.Detach(oldBeacon);
                    db.Beacons.Attach(beacon);
                    db.Entry(beacon).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.Beacons.Add(beacon);
                }

                db.SaveChanges();

                return oldBeacon;
            }
        }
Example #15
0
 public void PutName(SetNameModel model)
 {
     using (var db = new Context())
     {
         var me = db.Clients.Find(ClientID);
         me.Name = model.Name;
         db.SaveChanges();
     }
 }
Example #16
0
 public string GetName()
 {
     using (var db = new Context())
     {
         var me = db.Clients.Find(ClientID);
         return me.Name;
     }
 }