private static async Task HandleAutoFeedback() { using (HtfDbContext dbContext = new HtfDbContext()) { List <Android> androidsToAutonomouslySendData = await dbContext.Androids .Where(x => !x.Compromised && x.AutoPilot != AutoPilot.Level3).ToListAsync(); foreach (Android android in androidsToAutonomouslySendData) { SensoryDataRequest lastAutonomousDataRequest = await dbContext.SensoryDataRequests .Where(x => x.AndroidId == android.Id && x.AutonomousRequest) .OrderByDescending(o => o.TimeStamp).FirstOrDefaultAsync(); TimeSpan timeSinceLastAutonomousUpdate = DateTime.UtcNow - (lastAutonomousDataRequest?.TimeStamp ?? DateTime.MinValue); TimeSpan updateThreshold = TimeSpan.MaxValue; switch (android.AutoPilot) { case AutoPilot.Level1: updateThreshold = TimeSpan.FromMinutes(15); break; case AutoPilot.Level2: updateThreshold = TimeSpan.FromMinutes(5); break; } if (timeSinceLastAutonomousUpdate > updateThreshold) { SensoryDataRequest request = new SensoryDataRequest { AndroidId = android.Id, AutonomousRequest = true, Location = true, Crowd = true, Mood = true, Relationship = true, TimeStamp = DateTime.UtcNow }; await dbContext.SensoryDataRequests.AddAsync(request); await dbContext.SaveChangesAsync(); } } } }
/// <summary> /// Deploys a new android in the field. /// </summary> /// <param name="teamId">The unique identifier of the registered team that is sending this request.</param> /// <param name="androidId">The unique identifier of the deployed android this reuest is meant for.</param> /// <param name="request">The actual request.</param> /// <returns>The android the request is for.</returns> public async Task <AndroidDto> SendRequest(Guid teamId, Guid androidId, AndroidRequestDto request) { Team teamToCheck = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Id == teamId); if (teamToCheck == null) { throw new HtfValidationException("The specified team is unknown!"); } if (!Crypt.EnhancedVerify(request.Password, teamToCheck.Password)) { throw new HtfValidationException("The specified password is not correct!"); } Android androidToRequest = await _dbContext.Androids.SingleOrDefaultAsync(x => x.Id == androidId); if (androidToRequest == null) { throw new HtfValidationException("The specified android is unknown!"); } if (androidToRequest.Compromised) { throw new HtfValidationException("The specified android is compromised!"); } if (androidToRequest.AutoPilot == AutoPilot.Level1) { throw new HtfValidationException("The specified level-1 android does not support manual requests!"); } SensoryDataRequest requestToCreate = _requestMapper.Map(request); requestToCreate.AndroidId = androidId; requestToCreate.TimeStamp = DateTime.UtcNow; await _dbContext.SensoryDataRequests.AddAsync(requestToCreate); await _dbContext.SaveChangesAsync(); return(_androidMapper.Map(androidToRequest)); }
public AndroidRequestDto Map(SensoryDataRequest request) { return(_mapper.Map <AndroidRequestDto>(request)); }