package org.apache.plc4x.java.api.connection;

import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.api.exceptions.PlcInvalidAddressException;
import org.apache.plc4x.java.api.model.Address;

import java.util.Optional;

/**
 * Interface defining the most basic methods a PLC4X connection should support.
 * This generally handles the connection establishment itself and the parsing of
 * address strings to the platform dependent Address instances.
 * <p>
 * The individual operations are then defined by other interfaces within this package.
 */
public interface PlcConnection extends AutoCloseable {

    /**
     * Established the connection to the remote PLC.
     *
     * @throws PlcConnectionException an exception if the connection attempt failed.
     */
    void connect() throws PlcConnectionException;

    /**
     * Returns true if the PlcConnection is connected to a remote PLC.
     *
     * @return true, if connected, false, if not.
     */
    boolean isConnected();

    /**
     * Closes the connection to the remote PLC.
     *
     * @throws Exception an exception if shutting down the connection failed.
     */
    @Override
    void close() throws Exception;

    /**
     * Parses a PLC/protocol dependent query string into a PlcQuery object.
     *
     * @param queryString String representation of a query for the current type of PLC/protocol.
     * @return PlcQuery object containing all query data.
     * @throws PlcInvalidQueryException an exception if there was a problem parsing the query string.
     */
    PlcQuery prepareQuery(String queryString) throws PlcInvalidQueryException;

    Optional<PlcReader> getReader();

    Optional<PlcWriter> getWriter();

    Optional<PlcSubscriber> getSubscriber();
}

Notes to above class:


package org.apache.plc4x.java.api.model;

/**
 * Base type for all query types.
 * Typically every driver provides an implementation of this interface in order
 * to be able to describe the query for a resource. As this is completely tied to
 * the implemented protocol, this base interface makes absolutely no assumption to
 * any information it should provide.
 *
 * In order to stay platform and protocol independent every driver connection implementation
 * provides a prepareQuery(String) method that is able to parse a string representation of
 * a query into it's individual query type. Manually constructing Query objects
 * manually makes the solution less independent from the protocol, but might be faster.
 */
public interface PlcQuery {


}
Notes to the above class:


package org.apache.plc4x.java.api.messages;

import org.apache.plc4x.java.api.messages.items.RequestItem;

import java.util.*;

/**
 * Base type for all messages sent from the plc4x system to a connected plc.
 * @param <REQUEST_ITEM> 
 */
public abstract class PlcRequest<REQUEST_ITEM extends RequestItem> implements PlcMessage {

    protected final Map<String, REQUEST_ITEM> requestItemMap;

    public PlcRequest() {
        this.requestItemMap = new HashMap<>();
    }

    public PlcRequest(List<REQUEST_ITEM> requestItems) {
        this();
        Objects.requireNonNull(requestItems, "Request items must not be null");
        // Add all the items to the internal map.
        requestItems.stream().map(item -> requestItemMap.put(item.getName()));
    }

    public void addItem(REQUEST_ITEM requestItem) {
        Objects.requireNonNull(requestItem, "Request item must not be null");
        requestItemMap.put(requestItem.getName(), requestItem);
    }
    
    public REQUEST_ITEM getItem(String name) {
        Objects.requireNonNull(name, "Name must not be null");
        return requestItemMap.get(name);
    }

    public Collection<REQUEST_ITEM> getRequestItems() {
        return requestItemMap.values();
    }

    public int getNumberOfItems() {
        return getRequestItems().size();
    }
    
    public boolean isEmpty() {
        return requestItemMap.isEmpty();
    }
    
}
Notes to the above class:


package org.apache.plc4x.java.api.messages.items;

import org.apache.plc4x.java.api.model.PlcQuery;

import java.util.Objects;

/**
 * Wrapper Object to bind a name to a {@link PlcQuery} .
 */
public abstract class PlcRequestItem {
    
    private final String name;
    private final PlcQuery query;

    public RequestItem(String name, PlcQuery query) {
        Objects.requireNonNull(name, "Name must not be null");
        Objects.requireNonNull(query, "Query type must not be null");
        this.name = name;
        this.query = query;
    }

    public String getName() {
        return null;
    }
    
    public PlcQuery getQuery() {
        return query;
    }

}


...


public class PlcReadRequestItem extends PlcRequestItem {

    public RequestItem(String name, PlcQuery query) {
		super(name, query);
	}

}
Notes to the above class:


package org.apache.plc4x.java.api.messages.items;

import org.apache.plc4x.java.api.types.ResponseCode;

import java.util.Objects;

public abstract class PlcResponseItem {

    private final PlcRequestItem requestItem;

    private final ResponseCode responseCode;

    public ResponseItem(PlcRequestItem requestItem, ResponseCode responseCode) {
        Objects.requireNonNull(requestItem,"Request item must not be null");
        Objects.requireNonNull(responseCode,"Response code must not be null");
        this.requestItem = requestItem;
        this.responseCode = responseCode;
    }

    public PlcRequestItem getRequestItem() {
        return requestItem;
    }

    public ResponseCode getResponseCode() {
        return responseCode;
    }

}
Notes to the above class:


Notes to the above class:


Notes to the above class:


Notes to the above class: