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

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

There is a newer version: 2.0.29
Show newest version
package org.bidib.jibidibc.jserialcomm;

import java.util.List;

import org.bidib.jbidibc.core.BidibInterface;
import org.bidib.jbidibc.core.MessageReceiver;
import org.bidib.jbidibc.core.base.DataTransferStatusListener;
import org.bidib.jbidibc.core.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() {
        LOGGER.info("Create new instance of JSerialCommSerialBidib.");

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

        return instance;
    }

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

        connector = new JSerialCommSerialConnector();

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

            @Override
            public void notifyLineStatusChanged(boolean ready) {
                fireCtsChanged(ready);
            }
        });
        connector.setDataTransferStatusListener(new DataTransferStatusListener() {

            @Override
            public void notifySendStarted() {
                fireSendStarted();
            }

            @Override
            public void notifySendStopped() {
                fireSendStopped();
            }

            @Override
            public void notifyReceiveStarted() {
                fireReceiveStarted();
            }

            @Override
            public void notifyReceiveStopped() {
                fireReceiveStopped();
            }

        });
        connector.initialize();
    }

    @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()) {
            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);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy