Example #1
0
 public TeamLogic(HtfDbContext dbContext, TeamMapper teamMapper, TeamRegistrationMapper teamRegistrationMapper)
 {
     _dbContext              = dbContext;
     _teamMapper             = teamMapper;
     _teamRegistrationMapper = teamRegistrationMapper;
     _dbContext.Database.EnsureCreated();
 }
Example #2
0
 public AndroidLogic(
     HtfDbContext dbContext,
     AndroidMapper androidMapper,
     DeploymentMapper deploymentMapper,
     RequestMapper requestMapper)
 {
     _dbContext        = dbContext;
     _androidMapper    = androidMapper;
     _deploymentMapper = deploymentMapper;
     _requestMapper    = requestMapper;
     _dbContext.Database.EnsureCreated();
 }
Example #3
0
        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();
                    }
                }
            }
        }
Example #4
0
        private static async Task HandlePendingRequests()
        {
            using (HtfDbContext dbContext = new HtfDbContext())
            {
                Console.WriteLine("[ HTF2017 - Getting pending sensory data requests... ]");
                List <SensoryDataRequest> dataRequests = await dbContext.SensoryDataRequests
                                                         .Where(x => !x.Fulfilled).OrderBy(x => x.TimeStamp).ToListAsync();

                Console.WriteLine($"[ HTF2017 - {(dataRequests.Count > 0 ? $"{dataRequests.Count}" : "no")} pending sensory data requests found. ]");

                foreach (SensoryDataRequest dataRequest in dataRequests)
                {
                    Android android = await dbContext.Androids.Include(x => x.Team).SingleOrDefaultAsync(x => x.Id == dataRequest.AndroidId);

                    if (android != null)
                    {
                        Location location = await dbContext.Locations.SingleOrDefaultAsync(x => x.Id == android.Team.LocationId);

                        Console.WriteLine($"[ HTF2017 - Processing datarequest for '{android.Team.Name}'. ]");

                        SensoryData previousCrowdSensoryData = await dbContext.SensoryData
                                                               .Where(x => x.AndroidId == dataRequest.AndroidId && x.Crowd.HasValue)
                                                               .OrderByDescending(o => o.TimeStamp).FirstOrDefaultAsync();

                        SensoryData previousMoodSensoryData = await dbContext.SensoryData
                                                              .Where(x => x.AndroidId == dataRequest.AndroidId && x.Mood.HasValue)
                                                              .OrderByDescending(o => o.TimeStamp).FirstOrDefaultAsync();

                        SensoryData previousRelationshipSensoryData = await dbContext.SensoryData
                                                                      .Where(x => x.AndroidId == dataRequest.AndroidId && x.Relationship.HasValue)
                                                                      .OrderByDescending(o => o.TimeStamp).FirstOrDefaultAsync();

                        SensoryData data = new SensoryData
                        {
                            AndroidId = dataRequest.AndroidId,
                            // https://www.movable-type.co.uk/scripts/latlong.html
                            Longitude           = dataRequest.Location ? location.Longitude : (Double?)null,
                            Lattitude           = dataRequest.Location ? location.Lattitude : (Double?)null,
                            Crowd               = dataRequest.Crowd ? GetCrowd(android, previousCrowdSensoryData) : null,
                            Mood                = dataRequest.Mood ? GetMood(android, previousMoodSensoryData) : null,
                            Relationship        = dataRequest.Relationship ? GetRelationship(android, previousRelationshipSensoryData) : null,
                            TimeStamp           = DateTime.UtcNow,
                            AutonomousRequested = dataRequest.AutonomousRequest
                        };

                        await dbContext.SensoryData.AddAsync(data);

                        dataRequest.Fulfilled = true;

                        Boolean isCompromised = IsAndroidCompromised(android.AutoPilot,
                                                                     android.LocationSensorAccuracy, android.CrowdSensorAccuracy,
                                                                     android.MoodSensorAccuracy, android.RelationshipSensorAccuracy);
                        if (isCompromised)
                        {
                            android.Compromised = true;
                            switch (android.AutoPilot)
                            {
                            case AutoPilot.Level1:
                                android.Team.Score -= 10;
                                break;

                            case AutoPilot.Level2:
                                android.Team.Score -= 100;
                                break;

                            case AutoPilot.Level3:
                                android.Team.Score -= 1000;
                                break;
                            }
                        }

                        await dbContext.SaveChangesAsync();

                        Console.WriteLine($"[ HTF2017 - datarequest for '{android.Team.Name}' processed and fulfilled. ]");
                    }
                    else
                    {
                        Console.WriteLine($"[ HTF2017 - PANIC - No team found for android '{dataRequest.AndroidId}'! ]");
                    }
                }
            }
        }
Example #5
0
 public FeedbackController(HtfDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Example #6
0
        private static async Task HandlePendingSensoryData()
        {
            using (HtfDbContext dbContext = new HtfDbContext())
            {
                Console.WriteLine("[ HTF2017 - Getting pending sensory data... ]");
                List <SensoryData> data = await dbContext.SensoryData
                                          .Where(x => !x.Sent).OrderBy(x => x.TimeStamp).ToListAsync();

                Console.WriteLine(
                    $"[ HTF2017 - {(data.Count > 0 ? $"{data.Count}" : "no")} pending sensory data found. ]");

                foreach (SensoryData d in data)
                {
                    Team team = await dbContext.Androids.Where(x => x.Id == d.AndroidId)
                                .Select(x => x.Team).SingleOrDefaultAsync();

                    if (team != null)
                    {
                        Console.WriteLine($"[ HTF2017 - Processing data for '{team.Name}'. ]");
                        Boolean received = false;

                        try
                        {
                            RestClient         client   = new RestClient(team.FeedbackEndpoint);
                            RestRequest        request  = new RestRequest(Method.POST);
                            FeedbackRequestDto feedback = new FeedbackRequestDto
                            {
                                AndroidId         = d.AndroidId,
                                Longitude         = d.Longitude,
                                Lattitude         = d.Lattitude,
                                Crowd             = d.Crowd,
                                Mood              = d.Mood,
                                Relationship      = d.Relationship,
                                TimeStamp         = d.TimeStamp,
                                AutonomousRequest = d.AutonomousRequested
                            };
                            request.AddJsonBody(feedback);
                            FeedbackResponseDto response = await client.PostTaskAsync <FeedbackResponseDto>(request);

                            if (response != null && response.TeamId == team.Id)
                            {
                                if (d.AutonomousRequested)
                                {
                                    team.Score += 10;
                                }
                                else
                                {
                                    team.Score += 20;
                                }
                                received = true;
                            }
                            else
                            {
                                team.Score--;
                            }
                        }
                        catch
                        {
                            team.Score--;
                        }

                        d.Sent     = true;
                        d.Received = received;
                        await dbContext.SaveChangesAsync();

                        Console.WriteLine($"[ HTF2017 - data for '{team.Name}' {(received ? "successfully sent" : "failed to send")}. ]");
                    }
                    else
                    {
                        Console.WriteLine($"[ HTF2017 - PANIC - No team found for android '{d.AndroidId}'! ]");
                    }
                    await Task.Delay(100);
                }
            }
        }
Example #7
0
 public LocationLogic(HtfDbContext dbContext, LocationMapper locationMapper)
 {
     _dbContext      = dbContext;
     _locationMapper = locationMapper;
     _dbContext.Database.EnsureCreated();
 }