コード例 #1
0
        public async Task ProcessOfflineModules(
            ILocationRepository locationRepository,
            IMessagingService messagingService,
            ILocationActionEventsRepository locationActionEventsRepository)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var modules
                    = await DataContext.Modules
                      .Where(m => m.Offline == false && (m.LastHeartbeat < DateTime.Now.AddMinutes(-5)))
                      .Include(m => m.Location)
                      .ToListAsync();

                foreach (var module in modules)
                {
                    module.Offline = true;

                    DataContext.Modules.Update(module);

                    await DataContext.SaveChangesAsync();

                    if (module.Location.Armed)
                    {
                        await LocationTriggeringService.DetermineIfTriggering(locationRepository, locationActionEventsRepository, messagingService, module);
                    }
                    else
                    {
                        await messagingService.SendModuleOfflineMessage(module);
                    }
                }
            }
        }
コード例 #2
0
        public async Task <List <Models.Module> > GetModules()
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var modules = await DataContext.Modules.ToListAsync();

                return(modules);
            }
        }
コード例 #3
0
        public async Task CreateHeartbeat(Models.Heartbeat heartbeat)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.Heartbeats.Add(heartbeat);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #4
0
        public async Task CreateBootMessage(Models.BootMessage bootMessage)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.BootMessages.Add(bootMessage);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #5
0
        public async Task <List <Models.StateChange> > GetStateChanges()
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var statusChanges = await DataContext.StateChanges.ToListAsync();

                return(statusChanges);
            }
        }
コード例 #6
0
        public async Task CreateStateChange(Models.StateChange stateChange)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.StateChanges.Add(stateChange);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #7
0
        public async Task CreateModuleEvent(Models.ModuleEvent moduleEvent)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.ModuleEvents.Add(moduleEvent);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #8
0
        public async Task CreateModule(Module module)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.Modules.Add(module);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #9
0
        public async Task <List <Models.ModuleEvent> > GetModuleEvents()
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var statusChanges = await DataContext.ModuleEvents.ToListAsync();

                return(statusChanges);
            }
        }
コード例 #10
0
        public async Task SaveLocationActionEvent(LocationActionEvent locationActionEvent)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.LocationActionEvents.Add(locationActionEvent);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #11
0
        public async Task DeleteLocation(Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.Locations.Remove(location);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #12
0
 public static async Task Seed()
 {
     using (var db = new SimplySecureDataContext())
     {
         if (db.Database.GetPendingMigrations().Any())
         {
             await db.Database.MigrateAsync();
         }
     }
 }
コード例 #13
0
        public async Task RemovePushNotificationToken(PushNotificationToken token)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.PushNotificationTokens
                .RemoveRange(DataContext.PushNotificationTokens
                             .Where(t => t.Token == token.Token));

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #14
0
        public async Task CreatePanic(Panic panic)
        {
            panic.Id = Guid.NewGuid();

            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.Panics.Add(panic);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #15
0
        public async Task TriggerLocation(Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                location.Triggered = true;

                DataContext.Locations.Update(location);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #16
0
        public async Task <Module> FindModule(Guid moduleId)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var module
                    = await DataContext.Modules
                      .Where(m => m.Id == moduleId)
                      .Include(m => m.Location)
                      .FirstOrDefaultAsync();

                return(module);
            }
        }
コード例 #17
0
        public async Task CreateLocationUser(LocationUser locationUser)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.LocationUsers.RemoveRange
                    (DataContext.LocationUsers.Where(u => u.ApplicationUserId == locationUser.ApplicationUserId &&
                                                     u.LocationId == locationUser.LocationId));

                DataContext.LocationUsers.Add(locationUser);

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #18
0
        public async Task <List <LocationActionEvent> > GetLocationActionEventsByLocation(Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var actionEvents
                    = await DataContext.LocationActionEvents
                      .Where(a => a.LocationId == location.Id)
                      .Include(e => e.ApplicationUser)
                      .OrderByDescending(l => l.DateCreated)
                      .ToListAsync();

                return(actionEvents);
            }
        }
コード例 #19
0
        public async Task <LocationUser> FindLocationUser(Guid id)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var locationUser
                    = await DataContext.LocationUsers
                      .Where(u => u.Id == id)
                      .Include(u => u.Location)
                      .Include(u => u.ApplicationUser)
                      .FirstOrDefaultAsync();

                return(locationUser);
            }
        }
コード例 #20
0
        public async Task <List <PushNotificationToken> > GetLocationsPushNotificationTokens(List <ApplicationUser> users)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var userIds = users.Select(c => c.Id).Distinct().ToList();

                var tokens
                    = await DataContext.PushNotificationTokens
                      .Where(n => userIds.Contains(n.ApplicationUserId))
                      .ToListAsync();

                return(tokens);
            }
        }
コード例 #21
0
        public async Task <List <Location> > GetLocationsForUser(ApplicationUser user)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var locations =
                    await DataContext.LocationUsers
                    .Where(m => m.ApplicationUserId == user.Id)
                    .Select(m => m.Location)
                    .OrderBy(m => m.Name)
                    .ToListAsync();

                return(locations);
            }
        }
コード例 #22
0
        public async Task <Location> GetLocationById(Guid id)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var location = await DataContext.Locations.FindAsync(id);

                if (location == null)
                {
                    throw new NullReferenceException(ErrorMessageResponses.UnableToFindLocation);
                }

                return(location);
            }
        }
コード例 #23
0
        public async Task <List <LocationUser> > GetLocationUsers(Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var locationUsers =
                    await DataContext.LocationUsers
                    .Where(l => l.LocationId == location.Id)
                    .Include(l => l.Location)
                    .Include(l => l.ApplicationUser)
                    .OrderBy(m => m.Name)
                    .ToListAsync();

                return(locationUsers);
            }
        }
コード例 #24
0
        public async Task <List <Module> > GetModulesByLocation(Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var modules
                    = await DataContext
                      .Modules
                      .Where(b => b.LocationId == location.Id)
                      .Include(m => m.Location)
                      .OrderBy(m => m.Name)
                      .ToListAsync();

                return(modules);
            }
        }
コード例 #25
0
        public async Task <List <ModuleEvent> > GetModuleEventsByLocation(Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var moduleEvents
                    = await DataContext.ModuleEvents
                      .Where(m => m.Module.LocationId == location.Id)
                      .Include(m => m.Module)
                      .OrderByDescending(m => m.DateCreated)
                      .Take(200)
                      .ToListAsync();

                return(moduleEvents);
            }
        }
コード例 #26
0
        public async Task CreateLocation(ILocationUsersRepository locationUsersRepository, Location location)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                DataContext.Locations.Add(location);

                await DataContext.SaveChangesAsync();

                var locationUser = new LocationUser
                {
                    LocationId = location.Id,

                    ApplicationUserId = location.ApplicationUserId
                };

                await locationUsersRepository.CreateLocationUser(locationUser);
            }
        }
コード例 #27
0
        public async Task UpdateModuleHeartbeats(
            List <ModuleViewModel> modulesViewModels,
            ILocationRepository locationRepository,
            IMessagingService messagingService,
            ILocationActionEventsRepository locationActionEventsRepository)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var modules
                    = await DataContext.Modules.Where
                          (module => modulesViewModels.Any(vm => vm.Id == module.Id))
                      .Include(m => m.Location)
                      .ToListAsync();

                foreach (var module in modules)
                {
                    var moduleViewModel
                        = modulesViewModels.Single
                              (m => m.Id == module.Id);

                    if (moduleViewModel.LastHeartbeat >= module.LastHeartbeat.AddSeconds(10))
                    {
                        module.LastHeartbeat = moduleViewModel.LastHeartbeat;
                        module.Offline       = false;
                    }

                    DataContext.Modules.Update(module);

                    if (module.Location.Armed && module.State != moduleViewModel.State)
                    {
                        //this should never happen but it is here for completeness
                        await LocationTriggeringService.DetermineIfTriggering(locationRepository, locationActionEventsRepository, messagingService, module);
                    }
                }

                await DataContext.SaveChangesAsync();
            }
        }
コード例 #28
0
        public async Task UpdateModuleHeartbeat(Models.Module module)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                var dbModule
                    = await DataContext.Modules.Where
                          (m => m.Id == module.Id).SingleOrDefaultAsync();

                if (dbModule == null)
                {
                    DataContext.Modules.Add(module);
                    await DataContext.SaveChangesAsync();

                    return;
                }

                dbModule.LastHeartbeat = DateTime.UtcNow;
                dbModule.State         = module.State;

                DataContext.Modules.Update(dbModule);
                await DataContext.SaveChangesAsync();
            }
        }
コード例 #29
0
        public async Task DisarmLocation(Location location, ApplicationUser user, ILocationActionEventsRepository locationActionEventsRepository)
        {
            using (DataContext = new SimplySecureDataContext())
            {
                location.Armed = false;

                location.Triggered = false;

                DataContext.Locations.Update(location);

                await DataContext.SaveChangesAsync();

                var locationAction = new LocationActionEvent
                {
                    ApplicationUserId = user.Id,

                    Action = LocationActionEnum.Disarmed,

                    LocationId = location.Id
                };

                await locationActionEventsRepository.SaveLocationActionEvent(locationAction);
            }
        }