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); } } } }
public async Task <List <Models.Module> > GetModules() { using (DataContext = new SimplySecureDataContext()) { var modules = await DataContext.Modules.ToListAsync(); return(modules); } }
public async Task CreateHeartbeat(Models.Heartbeat heartbeat) { using (DataContext = new SimplySecureDataContext()) { DataContext.Heartbeats.Add(heartbeat); await DataContext.SaveChangesAsync(); } }
public async Task CreateBootMessage(Models.BootMessage bootMessage) { using (DataContext = new SimplySecureDataContext()) { DataContext.BootMessages.Add(bootMessage); await DataContext.SaveChangesAsync(); } }
public async Task <List <Models.StateChange> > GetStateChanges() { using (DataContext = new SimplySecureDataContext()) { var statusChanges = await DataContext.StateChanges.ToListAsync(); return(statusChanges); } }
public async Task CreateStateChange(Models.StateChange stateChange) { using (DataContext = new SimplySecureDataContext()) { DataContext.StateChanges.Add(stateChange); await DataContext.SaveChangesAsync(); } }
public async Task CreateModuleEvent(Models.ModuleEvent moduleEvent) { using (DataContext = new SimplySecureDataContext()) { DataContext.ModuleEvents.Add(moduleEvent); await DataContext.SaveChangesAsync(); } }
public async Task CreateModule(Module module) { using (DataContext = new SimplySecureDataContext()) { DataContext.Modules.Add(module); await DataContext.SaveChangesAsync(); } }
public async Task <List <Models.ModuleEvent> > GetModuleEvents() { using (DataContext = new SimplySecureDataContext()) { var statusChanges = await DataContext.ModuleEvents.ToListAsync(); return(statusChanges); } }
public async Task SaveLocationActionEvent(LocationActionEvent locationActionEvent) { using (DataContext = new SimplySecureDataContext()) { DataContext.LocationActionEvents.Add(locationActionEvent); await DataContext.SaveChangesAsync(); } }
public async Task DeleteLocation(Location location) { using (DataContext = new SimplySecureDataContext()) { DataContext.Locations.Remove(location); await DataContext.SaveChangesAsync(); } }
public static async Task Seed() { using (var db = new SimplySecureDataContext()) { if (db.Database.GetPendingMigrations().Any()) { await db.Database.MigrateAsync(); } } }
public async Task RemovePushNotificationToken(PushNotificationToken token) { using (DataContext = new SimplySecureDataContext()) { DataContext.PushNotificationTokens .RemoveRange(DataContext.PushNotificationTokens .Where(t => t.Token == token.Token)); await DataContext.SaveChangesAsync(); } }
public async Task CreatePanic(Panic panic) { panic.Id = Guid.NewGuid(); using (DataContext = new SimplySecureDataContext()) { DataContext.Panics.Add(panic); await DataContext.SaveChangesAsync(); } }
public async Task TriggerLocation(Location location) { using (DataContext = new SimplySecureDataContext()) { location.Triggered = true; DataContext.Locations.Update(location); await DataContext.SaveChangesAsync(); } }
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); } }
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(); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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(); } }
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(); } }
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); } }