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

zaber.motion.ascii.Transport 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;

/**
 * Connection transport backend allowing to carry Zaber ASCII protocol over arbitrary protocols.
 * Can only be used with a single connection.
 */
public class Transport implements AutoCloseable {
    private int transportId;

    /**
     * @return The transport ID identifies this transport instance with the underlying library.
     */
    public int getTransportId() {
        return this.transportId;
    }

    public Transport(
        int transportId) {
        this.transportId = transportId;
    }

    /**
     * Creates new instance allowing to read/write messages from/to a single connection.
     * @return New instance of transport.
     */
    public static Transport open() {
        Main.EmptyRequest.Builder builder = Main.EmptyRequest.newBuilder();

        Main.CustomInterfaceOpenResponse response = Call.callSync(
            "custom/interface/open",
            builder.build(),
            Main.CustomInterfaceOpenResponse.parser());
        return new Transport(response.getTransportId());
    }


    /**
     * Closes the transport.
     * Also closes the connection using the transport.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture closeAsync() {
        Main.CustomInterfaceCloseRequest.Builder builder = Main.CustomInterfaceCloseRequest.newBuilder();
        builder = builder.setTransportId(getTransportId());

        return Call.callAsync("custom/interface/close", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Closes the transport.
     * Also closes the connection using the transport.
     */
    public void close() {
        try {
            closeAsync().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);
        }
    }

    /**
     * Closes the transport with error.
     * Also closes the connection using the transport propagating the error.
     * @param errorMessage Error to be propagated.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture closeWithErrorAsync(
        String errorMessage) {
        Main.CustomInterfaceCloseRequest.Builder builder = Main.CustomInterfaceCloseRequest.newBuilder();
        builder = builder.setTransportId(getTransportId());

        builder = builder.setErrorMessage(errorMessage);
        return Call.callAsync("custom/interface/close", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Closes the transport with error.
     * Also closes the connection using the transport propagating the error.
     * @param errorMessage Error to be propagated.
     */
    public void closeWithError(
        String errorMessage) {
        try {
            closeWithErrorAsync(errorMessage).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);
        }
    }

    /**
     * Writes a single message to the connection.
     * The message will be processed as a reply from the device.
     * @param message Single message of Zaber ASCII protocol.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture writeAsync(
        String message) {
        Main.CustomInterfaceWriteRequest.Builder builder = Main.CustomInterfaceWriteRequest.newBuilder();
        builder = builder.setTransportId(getTransportId());

        builder = builder.setMessage(message);
        return Call.callAsync("custom/interface/write", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Writes a single message to the connection.
     * The message will be processed as a reply from the device.
     * @param message Single message of Zaber ASCII protocol.
     */
    public void write(
        String message) {
        try {
            writeAsync(message).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);
        }
    }

    /**
     * Reads a single message generated by the connection.
     * The message is a request for the device.
     * Read should be called continuously in a loop to ensure all generated messages are processed.
     * Subsequent read call confirms that previous message was delivered to the device.
     * @return A CompletableFuture that can be completed to get the result:
     * Message generated by the connection.
     */
    public CompletableFuture readAsync() {
        Main.CustomInterfaceReadRequest.Builder builder = Main.CustomInterfaceReadRequest.newBuilder();
        builder = builder.setTransportId(getTransportId());

        CompletableFuture response = Call.callAsync(
            "custom/interface/read",
            builder.build(),
            Main.CustomInterfaceReadResponse.parser());
        return response
            .thenApply(r -> r.getMessage());
    }

    /**
     * Reads a single message generated by the connection.
     * The message is a request for the device.
     * Read should be called continuously in a loop to ensure all generated messages are processed.
     * Subsequent read call confirms that previous message was delivered to the device.
     * @return Message generated by the connection.
     */
    public String read() {
        try {
            return readAsync().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