public HttpResponseMessage SetUnitLocation(UnitLocationInput locationInput) { var unit = _unitsService.GetUnitById(locationInput.Uid); if (unit == null) { throw HttpStatusCode.NotFound.AsException(); } if (unit.DepartmentId != DepartmentId) { throw HttpStatusCode.Unauthorized.AsException(); } if (this.ModelState.IsValid) { try { CqrsEvent locationEvent = new CqrsEvent(); UnitLocation location = new UnitLocation(); location.UnitId = locationInput.Uid; if (locationInput.Tms.HasValue) { location.Timestamp = locationInput.Tms.Value; } else { location.Timestamp = DateTime.UtcNow; } if (!String.IsNullOrWhiteSpace(locationInput.Lat) && locationInput.Lat != "NaN" && !String.IsNullOrWhiteSpace(locationInput.Lon) && locationInput.Lon != "NaN") { location.Latitude = decimal.Parse(locationInput.Lat); location.Longitude = decimal.Parse(locationInput.Lon); if (!String.IsNullOrWhiteSpace(locationInput.Acc) && locationInput.Acc != "NaN") { location.Accuracy = decimal.Parse(locationInput.Acc); } if (!String.IsNullOrWhiteSpace(locationInput.Alt) && locationInput.Alt != "NaN") { location.Altitude = decimal.Parse(locationInput.Alt); } if (!String.IsNullOrWhiteSpace(locationInput.Alc) && locationInput.Alc != "NaN") { location.AltitudeAccuracy = decimal.Parse(locationInput.Alc); } if (!String.IsNullOrWhiteSpace(locationInput.Spd) && locationInput.Spd != "NaN") { location.Speed = decimal.Parse(locationInput.Spd); } if (!String.IsNullOrWhiteSpace(locationInput.Hdn) && locationInput.Hdn != "NaN") { location.Heading = decimal.Parse(locationInput.Hdn); } locationEvent.Type = (int)CqrsEventTypes.UnitLocation; locationEvent.Data = ObjectSerialization.Serialize(location); _cqrsProvider.EnqueueCqrsEvent(locationEvent); return(Request.CreateResponse(HttpStatusCode.Created)); } else { return(Request.CreateResponse(HttpStatusCode.NotModified)); } } catch (Exception ex) { Logging.LogException(ex); throw HttpStatusCode.InternalServerError.AsException(); } } throw HttpStatusCode.BadRequest.AsException(); }
public async Task <ActionResult> SetUnitLocation(UnitLocationInput locationInput, CancellationToken cancellationToken) { var unit = await _unitsService.GetUnitByIdAsync(locationInput.Uid); if (unit == null) { return(NotFound()); } if (unit.DepartmentId != DepartmentId) { return(Unauthorized()); } if (this.ModelState.IsValid) { try { CqrsEvent locationEvent = new CqrsEvent(); UnitLocation location = new UnitLocation(); location.UnitId = locationInput.Uid; if (locationInput.Tms.HasValue) { location.Timestamp = locationInput.Tms.Value; } else { location.Timestamp = DateTime.UtcNow; } if (!String.IsNullOrWhiteSpace(locationInput.Lat) && locationInput.Lat != "NaN" && !String.IsNullOrWhiteSpace(locationInput.Lon) && locationInput.Lon != "NaN") { location.Latitude = decimal.Parse(locationInput.Lat); location.Longitude = decimal.Parse(locationInput.Lon); if (!String.IsNullOrWhiteSpace(locationInput.Acc) && locationInput.Acc != "NaN") { location.Accuracy = decimal.Parse(locationInput.Acc); } if (!String.IsNullOrWhiteSpace(locationInput.Alt) && locationInput.Alt != "NaN") { location.Altitude = decimal.Parse(locationInput.Alt); } if (!String.IsNullOrWhiteSpace(locationInput.Alc) && locationInput.Alc != "NaN") { location.AltitudeAccuracy = decimal.Parse(locationInput.Alc); } if (!String.IsNullOrWhiteSpace(locationInput.Spd) && locationInput.Spd != "NaN") { location.Speed = decimal.Parse(locationInput.Spd); } if (!String.IsNullOrWhiteSpace(locationInput.Hdn) && locationInput.Hdn != "NaN") { location.Heading = decimal.Parse(locationInput.Hdn); } locationEvent.Type = (int)CqrsEventTypes.UnitLocation; locationEvent.Data = ObjectSerialization.Serialize(location); await _cqrsProvider.EnqueueCqrsEventAsync(locationEvent); return(CreatedAtAction(nameof(SetUnitLocation), new { id = locationInput.Uid }, locationEvent)); } else { return(Ok()); } } catch (Exception ex) { Logging.LogException(ex); return(BadRequest()); } } return(BadRequest()); }