All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zaber.motion.ascii.StreamBuffer Maven / Gradle / Ivy

Go to download

A library that aims to provide easy-to-use API for communication with Zaber devices using Zaber ASCII Protocol.

There is a newer version: 6.7.0
Show newest version
// ===== THIS FILE IS GENERATED FROM A TEMPLATE ===== //
// ============== DO NOT EDIT DIRECTLY ============== //

package zaber.motion.ascii;

import zaber.motion.protobufs.Main;
import zaber.motion.gateway.Call;
import zaber.motion.exceptions.MotionLibException;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Represents a stream buffer with this ID on a device.
 * A stream buffer is a place to store a queue of stream actions.
 */
public class StreamBuffer {
    private Device device;

    /**
     * @return The Device this buffer exists on.
     */
    public Device getDevice() {
        return this.device;
    }

    private int bufferId;

    /**
     * @return The number identifying the buffer on the device.
     */
    public int getBufferId() {
        return this.bufferId;
    }

    public StreamBuffer(
        Device device, int bufferId) {
        this.device = device;
        this.bufferId = bufferId;
    }

    /**
     * Get the buffer contents as an array of strings.
     * @return A CompletableFuture that can be completed to get the result:
     * A string array containing all the stream commands stored in the buffer.
     */
    public CompletableFuture getContentAsync() {
        Main.StreamBufferGetContentRequest.Builder builder = Main.StreamBufferGetContentRequest.newBuilder();
        builder = builder.setInterfaceId(getDevice().getConnection().getInterfaceId());
        builder = builder.setDevice(getDevice().getDeviceAddress());
        builder = builder.setBufferId(getBufferId());

        CompletableFuture response = Call.callAsync(
            "device/stream_buffer_get_content",
            builder.build(),
            Main.StreamBufferGetContentResponse.parser());
        return response
            .thenApply(r -> r.getBufferLinesList().stream().toArray(String[]::new));
    }

    /**
     * Get the buffer contents as an array of strings.
     * @return A string array containing all the stream commands stored in the buffer.
     */
    public String[] getContent() {
        try {
            return getContentAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Erase the contents of the buffer.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture eraseAsync() {
        Main.StreamBufferEraseRequest.Builder builder = Main.StreamBufferEraseRequest.newBuilder();
        builder = builder.setInterfaceId(getDevice().getConnection().getInterfaceId());
        builder = builder.setDevice(getDevice().getDeviceAddress());
        builder = builder.setBufferId(getBufferId());

        return Call.callAsync("device/stream_buffer_erase", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Erase the contents of the buffer.
     */
    public void erase() {
        try {
            eraseAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy