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

org.bidib.jbidibc.jserialcomm.JSerialCommSerialBidib Maven / Gradle / Ivy

There is a newer version: 2.0.28
Show newest version
package org.bidib.jbidibc.jserialcomm;

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;

public class JSerialCommSerialBidib extends AbstractSerialBidib {
    private static final Logger LOGGER = LoggerFactory.getLogger(JSerialCommSerialBidib.class);

    private JSerialCommSerialConnector connector;

    private JSerialCommSerialBidib() {
    }

    /**
     * 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 JSerialCommSerialBidib.");

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

        return instance;
    }

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

        connector = new JSerialCommSerialConnector();

        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 List getPortIdentifiers() {
        return connector.getPortIdentifiers();
    }

    @Override
    protected void internalOpen(String portName, Context context) throws Exception {

        connector.internalOpen(portName, context);
    }

    @Override
    public void close() {

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

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

            cleanupAfterClose(getMessageReceiver());
        }
    }

    @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
    protected boolean isImplAvaiable() {
        return connector.isImplAvaiable();
    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy