/// <summary> /// Sets camera capture mode. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <param name="captureMode">Supported capture modes <see cref="CaptureMode.image"/> and <see cref="CaptureMode.video"/>.</param> /// <returns></returns> public static async Task SetCaptureMode(this ICameraClient client, CaptureMode captureMode) { switch (captureMode) { case CaptureMode.image: case CaptureMode.video: break; // ReSharper disable once RedundantCaseLabel case CaptureMode._other: // ReSharper disable once RedundantCaseLabel case CaptureMode.unknown: default: throw new ArgumentOutOfRangeException(nameof(captureMode), captureMode, null); } var result = await client.PostAsJson <Result>(new { name = "camera.setOptions", parameters = new { options = new { captureMode = captureMode.ToString("F") } } }); if (result?.error != null) { throw new Exception($"Failed to set options (code: {result.error.code}, message: {result.error.message})."); } }
/// <summary> /// Takes picture and returns uri where it can be downloaded. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <param name="useLocalFileUri">Optional flag for if absolute uri returned from camera should be used or absolute uri created from <see cref="ICameraClient.EndPoint"/> and relative uri, useful if called through a proxy. Default value <c>false</c></param> /// <returns>Absolute uri to image.</returns> public static async Task <Uri> TakePicture(this ICameraClient client, bool useLocalFileUri = false) { await client.SetCaptureMode(CaptureMode.image); await client.SetDateTime(DateTimeOffset.Now); var result = await client.PostAsJson <Model.TakePicture.Result>(new { name = "camera.takePicture" }); if (string.IsNullOrWhiteSpace(result.id)) { throw new Exception("No id returned from take picture."); } Model.TakePicture.Status.Result status; do { await Task.Delay(500); status = await client.GetStatus <Model.TakePicture.Status.Result>(result.id); } while (status?.state == "inProgress"); Uri uri; return ((useLocalFileUri && Uri.TryCreate(client.EndPoint, status?.results?._localFileUrl, out uri)) || Uri.TryCreate(status?.results?.fileUrl, UriKind.Absolute, out uri) ? uri : throw new Exception($"Failed to fetch status / uri for {result.id}")); }
private static async Task <TStatusResult> GetStatus <TStatusResult>(this ICameraClient client, string id) { var result = await client.PostAsJson <TStatusResult>( uri : ExecuteStatusUri, value : new { id }); return(result); }
/// <summary> /// Gets camera current capture mode. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <returns>Current <see cref="CaptureMode"/>.</returns> public static async Task <CaptureMode> GetCaptureMode(this ICameraClient client) { var result = await client.PostAsJson <Model.GetOptions.Result <Model.GetCaptureMode.Options> >(new { name = "camera.getOptions", parameters = new { optionNames = new[] { "captureMode" } } }); return(Enum.TryParse(result?.results?.options?.captureMode, true, out CaptureMode mode) ? mode : CaptureMode.unknown); }
/// <summary> /// Sets camera current date and time. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <param name="dateTime">The <see cref="DateTimeOffset"/> to set.</param> public static async Task SetDateTime(this ICameraClient client, DateTimeOffset dateTime) { var result = await client.PostAsJson <Result>(new { name = "camera.setOptions", parameters = new { options = new { dateTimeZone = dateTime.ToString("yyyy:MM:dd HH:mm:sszzz") } } }); if (result?.error != null) { throw new Exception($"Failed to set options (code: {result.error.code}, message: {result.error.message})."); } }
/// <summary> /// Gets camera current date and time. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <returns>Camera current <see cref="DateTimeOffset"/>, <c>null</c> if failed to parse date.</returns> public static async Task <DateTimeOffset?> GetDateTime(this ICameraClient client) { var result = await client.PostAsJson <Model.GetOptions.Result <Model.GetDateTime.Options> >(new { name = "camera.getOptions", parameters = new { optionNames = new[] { "dateTimeZone" } } }); return(DateTimeOffset.TryParseExact( result?.results?.options?.dateTimeZone, "yyyy:MM:dd HH:mm:sszzz", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var dateTimeZone) ? dateTimeZone as DateTimeOffset? : null); }