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 LocationActionEventsController(UserManager <ApplicationUser> userManager,
                                       ILocationActionEventsRepository locationActionEventsRepository,
                                       ILocationRepository locationRepository,
                                       ILogger <LocationActionEventsController> logger)
     : base(userManager, logger)
 {
     _locationRepository             = locationRepository;
     _locationActionEventsRepository = locationActionEventsRepository;
 }
示例#3
0
 public ServicesController(
     UserManager <ApplicationUser> userManager,
     IModuleRepository moduleRepository,
     ILocationRepository locationRepository,
     IMessagingService messagingService,
     ILocationActionEventsRepository locationActionEventsRepository,
     ILogger <ServicesController> logger)
     : base(userManager, logger)
 {
     _moduleRepository               = moduleRepository;
     _locationRepository             = locationRepository;
     _locationActionEventsRepository = locationActionEventsRepository;
     _messagingService               = messagingService;
 }
示例#4
0
 public PanicController(
     UserManager <ApplicationUser> userManager,
     ILocationRepository locationRepository,
     IPanicRepository panicRepository,
     IMessagingService messagingService,
     ILocationActionEventsRepository locationActionEventsRepository,
     ILogger <PanicController> logger)
     : base(userManager, logger)
 {
     _locationRepository             = locationRepository;
     _panicRepository                = panicRepository;
     _locationActionEventsRepository = locationActionEventsRepository;
     _messagingService               = messagingService;
 }
        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 static async Task <ModuleResponse> DetermineIfTriggering(
            ILocationRepository locationRepository,
            ILocationActionEventsRepository locationActionEventsRepository,
            IMessagingService messagingService,
            Module module)
        {
            var location = module.Location;

            var triggeredFlag = false;

            if (location.Armed && location.IsNotTriggered)
            {
                await locationRepository.TriggerLocation(location);

                await messagingService.SendModuleTriggeredMessage(module);

                var locationActionEvent = new LocationActionEvent
                {
                    LocationId = location.Id,

                    Action = LocationActionEnum.Triggered
                };

                await locationActionEventsRepository.SaveLocationActionEvent(locationActionEvent);

                triggeredFlag = true;
            }

            var moduleResponse = new ModuleResponse
            {
                Armed = location.Armed,

                Triggered = triggeredFlag && !location.IsSilentAlarm
            };

            return(moduleResponse);
        }
        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);
            }
        }
示例#8
0
        public static async Task SendPanicAlarm(
            ILocationActionEventsRepository locationActionEventsRepository,
            IMessagingService messagingService,
            ILocationRepository locationRepository,
            ApplicationUser user)
        {
            var locations = await locationRepository.GetLocationsForUser(user);

            foreach (var location in locations)
            {
                var locationActionEvent = new LocationActionEvent
                {
                    LocationId = location.Id,

                    Action = LocationActionEnum.Panic,

                    ApplicationUserId = user.Id
                };

                await locationActionEventsRepository.SaveLocationActionEvent(locationActionEvent);

                await messagingService.SendPanicAlarm(location, user);
            }
        }