Exemplo n.º 1
0
        public async Task <IHttpActionResult> UpdatePhase(int id, string phase)
        {
            var mis = await db.Missions.FindAsync(id);

            if (mis != null && phase != null)
            {
                mis.Phase = phase;
                db.Entry(mis);
                try
                {
                    await db.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    //Just catch the exception and emit out, that's all we care about
                }

                var hub = GlobalHost.ConnectionManager.GetHubContext <VehicleHub>();
                hub.Clients.All.newMissionPhase(id, phase);
                return(Ok());
            }
            else
            {
                return(Conflict());
            }
        }
Exemplo n.º 2
0
        public async Task BroadcastAcceptedCommand(CMD_ACK ack)
        {
            NestContainer db = new NestContainer();
            {
                switch (ack.CommandType)
                {
                case "CMD_NAV_Target":
                    CMD_NAV_Target target = await db.CMD_NAV_Target.FindAsync(ack.CommandId);

                    if (target != null && ack.Accepted == true)
                    {
                        target.Acked = true;
                        Clients.All.targetCommandAccepted(target);
                        db.Entry(target).State = EntityState.Modified;
                        await db.SaveChangesAsync();
                    }
                    break;

                case "CMD_NAV_Hold":
                    CMD_NAV_Hold hold = await db.CMD_NAV_Hold.FindAsync(ack.CommandId);

                    if (hold != null && ack.Accepted == true)
                    {
                        hold.Acked = true;
                        Clients.All.targetCommandAccepted(hold);
                        db.Entry(hold).State = EntityState.Modified;
                        await db.SaveChangesAsync();
                    }
                    break;

                case "CMD_NAV_Return":
                    CMD_NAV_Return goBack = await db.CMD_NAV_Return.FindAsync(ack.CommandId);

                    if (goBack != null && ack.Accepted == true)
                    {
                        goBack.Acked = true;
                        Clients.All.targetCommandAccepted(goBack);
                        db.Entry(goBack).State = EntityState.Modified;
                        await db.SaveChangesAsync();
                    }
                    break;

                case "CMD_NAV_Land":
                    CMD_NAV_Land land = await db.CMD_NAV_Land.FindAsync(ack.CommandId);

                    if (land != null && ack.Accepted == true)
                    {
                        land.Acked = true;
                        Clients.All.targetCommandAccepted(land);
                        db.Entry(land).State = EntityState.Modified;
                        await db.SaveChangesAsync();
                    }
                    break;

                default:
                    break;
                }
            }
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> InsertWaypoints(int id, Waypoint newWp)
        {
            //Use a transaction because we have to make possibly 2 commits to the database. If
            //either fails, we need to roll back
            using (var trans = db.Database.BeginTransaction())
            {
                try
                {
                    Mission mission = await db.Missions.FindAsync(id);

                    //If the mission doesn't exist, or the input parameter doesn't match the stored mission id, then return bad request
                    //The auto-generated WebApi2
                    if (mission == null && newWp.MissionId != id)
                    {
                        return(BadRequest("The Mission was not found and the waypoint was null"));
                    }

                    newWp = db.Waypoints.Add(newWp);
                    await db.SaveChangesAsync();

                    var wps = mission.Waypoints;
                    //Try to get the nextWaypoint id
                    var nextPointId = newWp.NextWaypointId;
                    //If nextPointId is not null, fix the reference of the former previous waypoint
                    if (nextPointId != null)
                    {
                        //Find previous waypoint
                        Waypoint prevWp = (from wp in mission.Waypoints
                                           //Make sure we don't accidentally get the wp we just inserted
                                           where wp.NextWaypointId == nextPointId && wp.Id != newWp.Id && wp.IsActive
                                           select wp).FirstOrDefault();
                        if (prevWp != null) //If this new WP is not at the beginning of the list
                        {
                            prevWp.NextWaypointId = newWp.Id;
                            //This waypoint is now modified. Let the context know.
                            db.Entry(prevWp).State = System.Data.Entity.EntityState.Modified;
                            await db.SaveChangesAsync();
                        }
                    }
                    //We finished with the transactions, make sure to commit them.
                    trans.Commit();
                    var hub = GlobalHost.ConnectionManager.GetHubContext <VehicleHub>();
                    hub.Clients.All.WaypointInserted(id);

                    return(Ok(newWp));
                }
                catch (DbUpdateException e)
                {
                    //Something went wrong. Rollback any changes, if any.
                    trans.Rollback();
                    return(BadRequest());
                }
            }
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> PutFlightState(int id, FlightStateDTO flightStateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != flightStateDto.Id)
            {
                return(BadRequest());
            }
            FlightState flightState = Mapper.Map <FlightState>(flightStateDto);

            db.Entry(flightState).State = System.Data.Entity.EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlightStateExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 5
0
        public IHttpActionResult PutMapRestricted(int id, MapRestricted mapRestricted)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != mapRestricted.Id)
            {
                return(BadRequest());
            }

            db.Entry(mapRestricted).State = System.Data.Entity.EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MapRestrictedExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> setCurrentMission(int id, int missionId)
        {
            var sched = await db.Schedules.FindAsync(id);

            var mission = await db.Missions.FindAsync(missionId);

            mission.ScheduleId      = sched.Id;
            sched.CurrentMission    = mission.id;
            db.Entry(mission).State = System.Data.Entity.EntityState.Modified;
            db.Entry(sched).State   = System.Data.Entity.EntityState.Modified;
            await db.SaveChangesAsync();

            var hub = GlobalHost.ConnectionManager.GetHubContext <VehicleHub>();

            hub.Clients.All.vehicleHasNewMission(sched.UAVId, id, missionId);

            return(Ok());
        }
Exemplo n.º 7
0
        public IHttpActionResult assignMultipleUAVs(int user_id, int amt)
        {
            IEnumerable <UAV> uavs = db.UAVs.Where(u => u.User == null && u.isActive == true).Take(amt);

            NEST_App.Models.User user = db.Users.Find(user_id);
            foreach (UAV uav in uavs)
            {
                user.UAVs.Add(uav);
            }
            db.Entry(user).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch
            {
                return(StatusCode(HttpStatusCode.Conflict));
            }

            return(Ok(user));
        }
Exemplo n.º 8
0
        public DateTime GetEta(int id)
        {
            var uav            = _db.UAVs.Find(id);
            var firstOrDefault = uav.Schedules.FirstOrDefault();

            if (firstOrDefault == null)
            {
                return(new DateTime());
            }
            if (firstOrDefault.CurrentMission == null)
            {
                return(new DateTime());
            }
            var currentMissionId = (int)firstOrDefault.CurrentMission;
            var currentMission   = _db.Missions.Find(currentMissionId);

            var orDefault = uav.FlightStates.FirstOrDefault();

            if (orDefault == null)
            {
                return(new DateTime());
            }
            var radianVelocity = GeoCodeCalc.CalcPythagorean(orDefault.VelocityX, orDefault.VelocityY);

            if (currentMission.TimeAssigned == null)
            {
                return(new DateTime());
            }
            var flightState = uav.FlightStates.FirstOrDefault();

            if (flightState == null)
            {
                return(new DateTime());
            }
            currentMission.EstimatedCompletionTime = ((DateTime)currentMission.TimeAssigned).AddMinutes(GeoCodeCalc.CalcDistance(flightState.Latitude,
                                                                                                                                 flightState.Longitude, currentMission.Latitude, currentMission.Longitude) / radianVelocity);
            _db.Entry(currentMission).State = System.Data.Entity.EntityState.Modified;
            _db.SaveChanges();
            return((DateTime)currentMission.EstimatedCompletionTime);
        }