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

org.bidib.wizard.common.node.InputPortHandler Maven / Gradle / Ivy

package org.bidib.wizard.common.node;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabels;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.enums.InputPortEnum;
import org.bidib.jbidibc.messages.enums.PortConfigStatus;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.wizard.api.model.NodeChangePublisher;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.utils.PortListUtils;
import org.bidib.wizard.common.labels.BidibLabelUtils;
import org.bidib.wizard.common.labels.WizardLabelFactory;
import org.bidib.wizard.common.labels.WizardLabelWrapper;
import org.bidib.wizard.model.ports.GenericPort;
import org.bidib.wizard.model.ports.InputPort;
import org.bidib.wizard.model.ports.event.PortListEvent;
import org.bidib.wizard.model.ports.event.PortStatusEvent;
import org.bidib.wizard.model.status.InputPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

    public InputPortHandler(final NodeInterface node, final ConnectionNodeAwarePublisher publisher,
        final WizardLabelWrapper wizardLabelWrapper) {
        super(node, publisher, wizardLabelWrapper);
    }

    @Override
    public void setPorts(List inputPorts, final NodeChangePublisher nodeChangePublisher) {
        LOGGER.info("Set the input ports on the node: {}", inputPorts);

        synchronized (ports) {
            ports.clear();

            if (CollectionUtils.isNotEmpty(inputPorts)) {
                ports.addAll(inputPorts);

                final NodeLabels nodeLabels =
                    getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
                // set the port labels
                BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.inputPort, ports);
            }
            portsProcessed = false;
        }

        nodeChangePublisher.firePortListChanged(InputPort.class);
        // TODO change to use the subject

        final ConnectionNodeAwarePublisher publisher = getPublisher();

        publisher
            .getSubjectPortEvents()
            .onNext(new PortListEvent(publisher.getConnectionId(), publisher.getUniqueId(), ports));
    }

    @Override
    protected void cachePorts(List genericPorts) {

        LOGGER.info("Prepare the input ports.");
        for (GenericPort genericPort : genericPorts) {
            // fetch the values from the generic port
            if (genericPort.isSupportsInputPort()) {
                LOGGER.trace("The current port supports input port: {}", genericPort);
                InputPort inputPort = new InputPort(genericPort);
                inputPort.setId(genericPort.getPortNumber());
                ports.add(inputPort);
            }
            else {
                LOGGER.trace("The current port does not support input port: {}", genericPort);
            }
        }

        final NodeLabels nodeLabels = getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
        // set the port labels
        BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.inputPort, ports);
    }

    @Override
    public List getEnabledPorts() {

        // TODO make sure that the inputPorts are filled already

        synchronized (ports) {
            List enabledPorts = new LinkedList<>();
            for (InputPort port : ports) {
                if (port.isEnabled()) {
                    enabledPorts.add(port);
                }
            }
            return Collections.unmodifiableList(enabledPorts);
        }
    }

    public void setPortStatus(final int portNumber, int portState, final NodeChangePublisher nodeChangePublisher) {
        InputPort port = null;
        boolean portConfigChanged = false;

        synchronized (ports) {
            // support the flat port model
            if (node.getNode().isPortFlatModelAvailable()) {
                if (CollectionUtils.isNotEmpty(node.getGenericPorts())) {

                    GenericPort genericPort =
                        PortListUtils.findGenericPortByPortNumber(node.getGenericPorts(), portNumber);

                    if (genericPort != null && genericPort.isSupportsInputPort()) {
                        LOGGER.info("The current port supports input port: {}", genericPort);

                        genericPort.setPortStatus(ByteUtils.getLowByte(portState));

                        if (genericPort.isInactive()) {
                            genericPort.setInactive(false);
                            portConfigChanged = true;
                        }

                        port = new InputPort(genericPort);
                    }
                    else {
                        LOGGER
                            .info(
                                "No generic port found or the current generic port does not support input port, port number: {}, port: {}",
                                portNumber, genericPort);
                    }
                }
            }

            if (port == null) {
                // make sure the input ports are available
                if (CollectionUtils.isNotEmpty(ports)) {

                    InputPort inputPort = PortListUtils.findPortByPortNumber(ports, portNumber);

                    if (inputPort != null) {
                        try {
                            InputPortEnum type = InputPortEnum.valueOf(ByteUtils.getLowByte(portState));
                            InputPortStatus status = InputPortStatus.valueOf(type);
                            LOGGER.info("InputPort status has changed, port: {}, status: {}", inputPort, status);
                            inputPort.setStatus(status);

                            if (inputPort.isInactive()) {
                                inputPort.setInactive(false);
                                portConfigChanged = true;
                            }
                        }
                        catch (IllegalArgumentException ex) {
                            LOGGER
                                .warn("Set the port status failed, port: {}, portState: {}, message: {}", inputPort,
                                    portState, ex.getMessage());

                            inputPort.setPortConfigErrorCode(ByteUtils.getInt(BidibLibrary.BIDIB_ERR_LC_PORT_GENERAL));
                            inputPort.setConfigStatus(PortConfigStatus.CONFIG_ERROR);
                            portConfigChanged = true;
                        }
                        port = inputPort;
                    }
                    else {
                        LOGGER.warn("No input port available for portNumber: {}", portNumber);
                    }
                }
                else {
                    LOGGER.debug("No input ports or generic ports available.");
                }
            }
        }

        if (port != null) {
            nodeChangePublisher.firePortStatusChanged(InputPort.class, port);

            if (portConfigChanged) {
                nodeChangePublisher.firePortConfigChanged(port);
            }

            getPublisher()
                .getSubjectPortEvents()
                .onNext(new PortStatusEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), port));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy