|
| 1 | +package io.mavsdk.example; |
| 2 | + |
| 3 | +import io.mavsdk.System; |
| 4 | +import io.mavsdk.camera.Camera; |
| 5 | +import io.reactivex.Completable; |
| 6 | +import io.reactivex.Single; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +public class ListPhotos { |
| 15 | + private static final Logger logger = LoggerFactory.getLogger(ListPhotos.class); |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + logger.debug("Starting example: taking and listing photos..."); |
| 19 | + |
| 20 | + System drone = new System(); |
| 21 | + CountDownLatch latch = new CountDownLatch(1); |
| 22 | + |
| 23 | + // Completable trying to take 50 pictures (stopping in case of error) |
| 24 | + Completable takePhotoCompletable |
| 25 | + = drone.getCamera().takePhoto() |
| 26 | + .doOnComplete(() -> logger.debug("Picture taken")) |
| 27 | + .doOnError(throwable -> logger.error("Error: " + throwable.getMessage())) |
| 28 | + .onErrorComplete() |
| 29 | + .delay(1, TimeUnit.SECONDS) |
| 30 | + .repeat(50); |
| 31 | + |
| 32 | + // Single getting a list of all the pictures available on the camera |
| 33 | + Single<List<Camera.CaptureInfo>> photosList |
| 34 | + = drone.getCamera().listPhotos(Camera.PhotosRange.ALL) |
| 35 | + .doOnSuccess(captureInfos -> logger.debug("Got " + captureInfos.size() + " photos")) |
| 36 | + .doOnError(throwable -> logger.error("Error: " + throwable.getMessage())); |
| 37 | + |
| 38 | + // Run them one after the other |
| 39 | + takePhotoCompletable.andThen(photosList) |
| 40 | + .subscribe((captureInfos, throwable) -> latch.countDown()); |
| 41 | + |
| 42 | + try { |
| 43 | + latch.await(); |
| 44 | + } catch (InterruptedException ignored) { |
| 45 | + // This is expected |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments