public IHttpActionResult AddMobileDeviceLocations([FromBody] IList <MobileDeviceLocationModel> models, string deviceID) { using (var dbContext = new DataContext()) { var mobileDevice = dbContext.MobileDevices.FirstOrDefault(f => f.DeviceID == deviceID); if (mobileDevice == null) { return(this.BadRequestEx(Error.MobileDeviceDoesNotExist)); } foreach (var mobileDeviceLocationModel in models) { var mobileDeviceLocation = new MobileDeviceLocation(); mobileDeviceLocation.GpsLongitude = mobileDeviceLocationModel.GpsLongitude; mobileDeviceLocation.GpsLatitude = mobileDeviceLocationModel.GpsLatitude; mobileDeviceLocation.LocationTimestamp = mobileDeviceLocationModel.LocationTimestamp; mobileDeviceLocation.MobileDeviceID = mobileDevice.ID; dbContext.MobileDeviceLocations.Add(mobileDeviceLocation); } dbContext.SaveChanges(); return(Ok()); } }
public HttpResponseMessage PostLocation(Guid id, MobileDeviceLocation value) { if (id == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Não foram enviados dados.")); } var mobileDevices = _database.GetCollection <MobileDevice>("MobileDevices"); MobileDevice device = mobileDevices.FindOneById(id); var accessPoints = _database.GetCollection <AccessPoint>("AccessPoints"); //Para cada AP que o dispositivo enxerga foreach (AccessPointSignal aps in value.APs) { //Se AP não existir, adiciona if (aps.AP._id.Equals(Guid.Empty)) { accessPoints.Insert(aps.AP); } //Calcula distancia aps.Distance = Math.Pow(10, ((-32.44 - 20 * Math.Log10(aps.AP.Frequency) - aps.Signal) / 20)); } //Para cada AP que o dispositivo enxerga List <AccessPointSignal> lista = value.APs.ToList(); List <AccessPointIntersection> intersecs = new List <AccessPointIntersection>(); var next = 0; for (int i = 0; i < lista.Count; i++) { next += 1; if (next == lista.Count) { next = 0; } var inter = new AccessPointIntersection(lista[i], lista[next]); if (inter.hasIntersection()) { //Se existe intersecção intersecs.Add(inter); } } //Se temos pelo menos 3 intersecções if (intersecs.Count >= 3) { double deltaX = 0, deltaY = 0; foreach (AccessPointIntersection intersec in intersecs) { deltaX += intersec.Center._x; deltaY += intersec.Center._y; } device.LastSeen = new Location() { _x = deltaX / intersecs.Count, _y = intersecs.Count, }; mobileDevices.Save(device); } return(Request.CreateResponse(HttpStatusCode.Created, device)); }