Created
April 19, 2019 10:43
-
-
Save NeelBhatt/e35e5410b546359e593b1cae8ab71033 to your computer and use it in GitHub Desktop.
PiMonitRHub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PiMonitRHub : Hub | |
{ | |
internal static bool _isStreamRunning = false; | |
private readonly PiCameraService _piCameraService; | |
public PiMonitRHub(PiCameraService piCameraService) | |
{ | |
_piCameraService = piCameraService; | |
} | |
public ChannelReader<object> StartStream(CancellationToken cancellationToken) | |
{ | |
var channel = Channel.CreateUnbounded<object>(); | |
_isStreamRunning = true; | |
_ = WriteItemsAsync(channel.Writer, cancellationToken); | |
return channel.Reader; | |
} | |
private async Task WriteItemsAsync(ChannelWriter<object> writer, CancellationToken cancellationToken) | |
{ | |
try | |
{ | |
while (_isStreamRunning) | |
{ | |
cancellationToken.ThrowIfCancellationRequested(); | |
await writer.WriteAsync(await _piCameraService.CapturePictureAsByteArray()); | |
await Task.Delay(100, cancellationToken); | |
} | |
} | |
catch (Exception ex) | |
{ | |
writer.TryComplete(ex); | |
} | |
writer.TryComplete(); | |
} | |
public void StopStream() | |
{ | |
_isStreamRunning = false; | |
Clients.All.SendAsync("StopStream"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment