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

org.bidib.wizard.common.node.AnalogPortHandler 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.wizard.api.model.NodeChangePublisher;
import org.bidib.wizard.api.model.NodeInterface;
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.AnalogPort;
import org.bidib.wizard.model.ports.GenericPort;
import org.bidib.wizard.model.ports.event.PortListEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

        synchronized (ports) {
            ports.clear();

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

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

        nodeChangePublisher.firePortListChanged(AnalogPort.class);
        // TODO change to use the subject
        getPublisher()
            .getSubjectPortEvents()
            .onNext(new PortListEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), ports));
    }

    /**
     * Get the analog ports.
     * 
     * @return the analog ports
     * @param nodeChangePublisher
     *            the node change publisher
     */
    @Override
    protected void cachePorts(List genericPorts) {

        LOGGER.debug("Prepare the analog ports.");
        for (GenericPort genericPort : genericPorts) {
            // fetch the values from the generic port
            if (genericPort.isSupportsAnalogPort()) {
                LOGGER.trace("The current port supports analog port: {}", genericPort);
                AnalogPort analogPort = new AnalogPort(genericPort);
                analogPort.setId(genericPort.getPortNumber());
                ports.add(analogPort);
            }
            else {
                LOGGER.trace("The current port does not support analog port: {}", genericPort);
            }
        }
        final NodeLabels nodeLabels = getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());

        // set the port labels
        BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.analogPort, ports);
    }

    @Override
    public List getEnabledPorts() {

        // TODO make sure that the lightPorts are filled already

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy