/// <summary> /// Get latest image for a given camera /// </summary> /// <param name="camera">Camera object</param> /// <returns>Status of API call <seealso cref="APIMASH.ApiResponseStatus"/>. This method will alway return success.</returns> public async Task <APIMASH.ApiResponseStatus> GetCameraImage(TomTomCameraViewModel camera) { BitmapImage cameraImage = null; // invoke the API (explicit deserializer provided because the image responses from TomTom don't include a Content-Type header var apiResponse = await Invoke <BitmapImage>( Deserializers <BitmapImage> .DeserializeImage, "https://api.tomtom.com/trafficcams/getfullcam/{0}.jpg?key={1}", camera.CameraId, this._apiKey); // if successful, grab image as deserialized response if (apiResponse.IsSuccessStatusCode) { cameraImage = apiResponse.DeserializedResponse; } // otherwise, use some stock image to reflect error condition else if (apiResponse.StatusCode == HttpStatusCode.NotFound) { cameraImage = new BitmapImage(new Uri("ms-appx:///APIMASH_APIs/Assets/camera404.png")); } else { cameraImage = new BitmapImage(new Uri("ms-appx:///APIMASH_APIs/Assets/cameraError.png")); } // populate the ViewModel with the image TomTomCamerasModel.PopulateViewModel(cameraImage, apiResponse.RawResponse, camera); // return a success status (there will always be an image returned) return(ApiResponseStatus.Default); }
/// <summary> /// Performs a query for traffic cameras within the given BoundingBox, <paramref name="b"/> /// </summary> /// <param name="b">Bounding box defining area for which to return traffic cams</param> /// <param name="maxResults">Maximum number of results to assign to view model (0 = assign all results)</param> /// <returns>Status of API call <seealso cref="APIMASH.ApiResponseStatus"/></returns> public async Task <APIMASH.ApiResponseStatus> GetCameras(BoundingBox b, Int32 maxResults = 0) { // clear the results lock (_resultsLock) { TimeLapseQueue.Clear(); TomTomViewModel.Results.Clear(); TomTomViewModel.ResultsTruncated = false; } // invoke the API var apiResponse = await Invoke <TomTomCamerasModel.cameras>( "http://api.tomtom.com/trafficcams/boxquery?top={0}&bottom={1}&left={2}&right={3}&format=xml&key={4}", b.North, b.South, b.West, b.East, this._apiKey); // if successful, copy relevant portions from model to the view model if (apiResponse.IsSuccessStatusCode) { lock (_resultsLock) { TomTomViewModel.ResultsTruncated = TomTomCamerasModel.PopulateViewModel( apiResponse.DeserializedResponse, TomTomViewModel.Results, new LatLong((b.North + b.South) / 2, (b.West + b.East) / 2), maxResults); } } else { switch (apiResponse.StatusCode) { case HttpStatusCode.Unauthorized: apiResponse.SetCustomStatus("Supplied API key is not valid for this request.", apiResponse.StatusCode); break; case HttpStatusCode.InternalServerError: case HttpStatusCode.ServiceUnavailable: apiResponse.SetCustomStatus("Problem appears to be at TomTom's site. Please retry later.", apiResponse.StatusCode); break; } } // return the status information return(apiResponse as APIMASH.ApiResponseStatus); }
/// <summary> /// Get latest image for a given camera /// </summary> /// <param name="camera">Camera object</param> /// <returns>Status of API call <seealso cref="APIMASH.ApiResponseStatus"/>. This method will alway return success.</returns> public async Task <APIMASH.ApiResponseStatus> GetCameraImage(TomTomCameraViewModel camera) { BitmapImage cameraImage = null; Byte[] imageBytes; // invoke the API (explicit deserializer provided because the image responses from TomTom don't include a Content-Type header var apiResponse = await Invoke <BitmapImage>( Deserializers <BitmapImage> .DeserializeImage, "https://api.tomtom.com/trafficcams/getfullcam/{0}.jpg?key={1}", camera.CameraId, this._apiKey); // if successful, grab image as deserialized response if (apiResponse.IsSuccessStatusCode) { cameraImage = apiResponse.DeserializedResponse; imageBytes = apiResponse.RawResponse; } // otherwise, use some stock image to reflect error condition else { var errorImage = (apiResponse.StatusCode == HttpStatusCode.NotFound) ? "ms-appx:///APIMASH_APIs/Assets/camera404.png" : "ms-appx:///APIMASH_APIs/Assets/cameraError.png"; cameraImage = new BitmapImage(new Uri(errorImage)); imageBytes = Encoding.UTF8.GetBytes(errorImage); } lock (_resultsLock) { // populate the ViewModel with the image and if image was added, determine if we're at capacity and remove oldest picture if (TomTomCamerasModel.PopulateViewModel(cameraImage, imageBytes, camera)) { UpdateTimeLapseQueue(camera.CameraId); } } // return a success status (there will always be an image returned) return(ApiResponseStatus.Default); }