/// <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})."); } }
private PiBroker(ICameraClient cameraClient, IBrowserClient browserClients) { Camera = cameraClient; Browsers = browserClients; ServerSettings = new ServerSettings {JpegCompression= 90}; PiSettings = new PiSettings {TransmitImagePeriod = TimeSpan.FromMilliseconds(200)}; }
/// <summary> /// Takes picture and downloads image to specified local path. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <param name="path">The local path to download to.</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> public static async Task TakePicture(this ICameraClient client, string path, bool useLocalFileUri = false) { using (var targetStream = client.CreateFile(path)) { await client.TakePicture(targetStream, useLocalFileUri); } }
/// <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}")); }
/// <summary> /// Takes picture and downloads image to specified stream. /// </summary> /// <param name="client">The <see cref="ICameraClient"/> method extends.</param> /// <param name="targetStream">The target stream image is downloaded to.</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> public static async Task TakePicture(this ICameraClient client, Stream targetStream, bool useLocalFileUri = false) { var uri = await client.TakePicture(useLocalFileUri); using (var sourceStream = await client.GetHttpClient().GetStreamAsync(uri)) { await sourceStream.CopyToAsync(targetStream); } }
private PiBroker(ICameraClient cameraClient, IBrowserClient browserClients) { Camera = cameraClient; Browsers = browserClients; ServerSettings = new ServerSettings { JpegCompression = 90 }; PiSettings = new PiSettings { TransmitImagePeriod = TimeSpan.FromMilliseconds(200) }; }
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); }
private static async Task <TResult> PostAsJson <TResult>(this ICameraClient client, object value, Uri uri = null) { var response = await client.GetHttpClient().PostAsJsonAsync( uri ?? ExecuteUri, value ); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsAsync <TResult>(); return(result); }
protected override void OnClientDisconnected(ICameraClient client) { _connector.Disconnect(_camera.AudioChannel, _client.AudioChannel); _connector.Disconnect(_camera.VideoChannel, _client.VideoChannel); _connector.Dispose(); var handler = ClientCountChange; if (handler != null) { handler(null, new EventArgs()); } base.OnClientDisconnected(client); }
/// <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); }
public CameraController(ICameraClient cameraClient, IMemoryCache cache) { this.cameraClient = cameraClient; this.cache = cache; }