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

org.bidib.jbidibc.scm.ScmSerialBidib Maven / Gradle / Ivy

The newest version!
package org.bidib.jbidibc.scm;

import java.util.List;

import org.bidib.jbidibc.core.BidibInterface;
import org.bidib.jbidibc.messages.MessageReceiver;
import org.bidib.jbidibc.messages.base.AbstractBaseBidib;
import org.bidib.jbidibc.messages.helpers.Context;
import org.bidib.jbidibc.serial.AbstractSerialBidib;
import org.bidib.jbidibc.serial.LineStatusListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is the default bidib implementation. It creates and initializes the MessageReceiver and the NodeFactory that is
 * used in the system.
 * 
 */
public final class ScmSerialBidib extends AbstractSerialBidib {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScmSerialBidib.class);

    private ScmSerialConnector connector;

    private ScmSerialBidib() {
    }

    /**
     * Get a new initialized instance of ScmSerialBidib.
     *
     * @return the instance of ScmSerialBidib
     */
    public static BidibInterface createInstance(final Context context) {
        LOGGER.info("Create new instance of ScmSerialBidib.");

        ScmSerialBidib instance = new ScmSerialBidib();
        instance.initialize(context);

        return instance;
    }

    @Override
    public void initialize(final Context context) {
        LOGGER.info("Initialize. Create the connector.");
        super.initialize(context);

        connector = new ScmSerialConnector();

        final MessageReceiver serialMessageReceiver = getMessageReceiver();
        connector.setMessageReceiver(serialMessageReceiver);
        connector.setLineStatusListener(new LineStatusListener() {

            @Override
            public void notifyLineStatusChanged(boolean ready, boolean manualEvent) {
                fireCtsChanged(ready, manualEvent);
            }
        });

        initializeConnector(connector);
    }

    @Override
    public void close() {

        LOGGER.info("Close is called.");

        if (connector.close()) {
            super.close();

            cleanupAfterClose(getMessageReceiver());
        }
    }

    @Override
    public List getPortIdentifiers() {
        return connector.getPortIdentifiers();
    }

    @Override
    protected boolean isImplAvaiable() {
        return connector.isImplAvaiable();
    }

    @Override
    protected void internalOpen(String portName, final Context context) throws Exception {
        connector.internalOpen(portName, context);
    }

    @Override
    public boolean isOpened() {
        boolean isOpened = false;
        try {
            isOpened = connector.isOpened();
        }
        catch (Exception ex) {
            LOGGER.warn("The connector is not available.", ex);
        }
        return isOpened;
    }

    @Override
    public void send(byte[] data) {
        connector.send(data);
    }

    @Override
    protected AbstractBaseBidib getConnector() {
        return connector;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy