Пример #1
0
        public async Task <IActionResult> Post([FromBody] BaseComponentViewModel bootViewModel)
        {
            try
            {
                var module
                    = await _moduleRepository.FindModule
                          (Guid.Parse(bootViewModel.ModuleId));

                if (module == null)
                {
                    return(BadRequest(new Exception(ErrorMessageResponses.InvalidModuleId)));
                }

                await _bootRepository
                .SaveBootMessage
                    (new BootMessage(module.Id, bootViewModel.State));

                var moduleResponse
                    = await LocationTriggeringService
                      .DetermineIfTriggering(_locationRepository, _locationActionEventsRepository, _messagingService, module);

                module.State = bootViewModel.State;

                await _moduleRepository.UpdateModuleLastBoot(module);

                return(Ok(moduleResponse));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);

                return(BadRequest(new ErrorResponse(ex)));
            }
        }
        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 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();
            }
        }