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)); }
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; } } }
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()); } } }
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()); } }
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()); }
public static async Task SendTrespassEvent(int uavId) { var uav = await db.UAVs.FindAsync(uavId); EventLog evt = new EventLog(); evt.uav_id = uav.Id; evt.uav_callsign = uav.Callsign; evt.criticality = "critical"; evt.message = uav.Callsign + " Trespassing"; evt.create_date = DateTime.Now; evt.modified_date = DateTime.Now; //wtf seriously? -- why is this in here twice... evt.UAVId = uav.Id; evt.operator_screen_name = "NEST"; eventHub.Clients.All.newEvent(evt); db.EventLogs.Add(evt); await db.SaveChangesAsync(); eventHub.Clients.All.newEvent(evt); }
public async Task <HttpResponseMessage> UAVAssignment(int id, int?userId) { try { UAV u = _db.UAVs.FirstOrDefault(x => x.Id == id); u.User_user_id = userId; _db.Entry(u).State = System.Data.Entity.EntityState.Modified; await _db.SaveChangesAsync(); } catch (DbUpdateException e) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task PushFlightStateUpdate(FlightStateDTO dto) { Clients.All.flightStateUpdate(dto); //await TrespassChecker.ReportTrespassIfNecesarry(dto.UAVId, dto.Latitude, dto.Longitude); var eventHub = GlobalHost.ConnectionManager.GetHubContext <EventLogHub>(); UAV uav = db.UAVs.FirstOrDefault(x => x.Id == dto.Id); double lat = Math.Round(dto.Latitude * 10000) / 10000; double lon = Math.Round(dto.Longitude * 10000) / 10000; var mis = from ms in db.Missions where ms.ScheduleId == uav.Id select ms; //look up the uav bool areaList = (areaWarning.Where(u => u.Id == uav.Id).Count()) > 0; bool batteryList = (batteryWarning.Where(u => u.Id == uav.Id).Count()) > 0; if (!areaList) { bool check = db.MapRestrictedSet.Where( u => u.SouthWestLatitude <mis.FirstOrDefault().Latitude&& u.NorthEastLatitude> mis.FirstOrDefault().Latitude&& u.SouthWestLongitude <mis.FirstOrDefault().Longitude&& u.NorthEastLongitude> mis.FirstOrDefault().Longitude ).Count() > 0; if (check) { areaWarning.Add(uav); EventLog evt = new EventLog(); evt.event_id = events; evt.uav_id = uav.Id; evt.uav_callsign = uav.Callsign; evt.criticality = "critical"; evt.message = "Delivery is in restricted area"; evt.create_date = DateTime.Now; evt.modified_date = DateTime.Now; //wtf seriously? -- why is this in here twice... evt.UAVId = uav.Id; evt.operator_screen_name = "jimbob"; eventHub.Clients.All.newEvent(evt); db.EventLogs.Add(evt); events++; } } //it's not in the battery list if (!batteryList) { if (dto.BatteryLevel < .2) { //add list -- stops from sending warning everytime batteryWarning.Add(uav); EventLog evt = new EventLog(); evt.event_id = events; evt.uav_id = uav.Id; evt.uav_callsign = uav.Callsign; evt.criticality = "critical"; evt.message = "Low Battery"; evt.create_date = DateTime.Now; evt.modified_date = DateTime.Now; //wtf seriously? -- why is this in here twice... evt.UAVId = uav.Id; evt.operator_screen_name = "jimbob"; eventHub.Clients.All.newEvent(evt); db.EventLogs.Add(evt); events++; } } try { db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } // flightstatedto entity is not the same as models in our db context. can not guarantee atomic. need to wipe out flightstatedto }