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

org.bidib.wizard.common.node.BacklightPortHandler 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.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.BacklightPort;
import org.bidib.wizard.model.ports.GenericPort;
import org.bidib.wizard.model.ports.event.PortListEvent;
import org.bidib.wizard.model.ports.event.PortStatusEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

        synchronized (ports) {
            ports.clear();
            if (CollectionUtils.isNotEmpty(backlightPorts)) {
                ports.addAll(backlightPorts);

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

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

    @Override
    protected void cachePorts(List genericPorts) {

        LOGGER.debug("Prepare the backlight ports.");
        for (GenericPort genericPort : genericPorts) {
            // fetch the values from the generic port
            if (genericPort.isSupportsBacklightPort()) {
                LOGGER.trace("The current port supports backlight port: {}", genericPort);
                BacklightPort backlightPort = new BacklightPort(genericPort);
                backlightPort.setId(genericPort.getPortNumber());
                ports.add(backlightPort);
            }
            else {
                LOGGER.trace("The current port does not support backlight port: {}", genericPort);
            }
        }
        final NodeLabels nodeLabels = getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
        // set the port labels
        BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.backlightPort, ports);
    }

    @Override
    public List getEnabledPorts() {

        // TODO make sure that the lightPorts are filled already

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

    /**
     * Set the value of a backlight port.
     * 
     * @param portNumber
     *            the port number
     * @param portValue
     *            the new port value
     */
    public void setPortValue(final int portNumber, int portValue, final NodeChangePublisher nodeChangePublisher) {
        LOGGER.info("Set the backlight port value: {}, portNumber: {}", portValue, portNumber);
        BacklightPort 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.isSupportsBacklightPort()) {
                        LOGGER.info("The current port supports backlight port: {}", genericPort);

                        genericPort.setPortValue(portValue);

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

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

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

                    BacklightPort backlightPort = PortListUtils.findPortByPortNumber(ports, portNumber);

                    if (backlightPort != null) {
                        Integer oldValue = backlightPort.getValue();
                        if (oldValue == null || oldValue != portValue || (backlightPort.getExpectedTargetValue() != null && backlightPort.getExpectedTargetValue() > -1)) {
                            backlightPort.setValue(portValue, true);

                            LOGGER
                                .debug("BacklightPort value has changed, port: {}, value: {}", backlightPort,
                                    portValue);
                            port = backlightPort;
                        }
                    }
                    else {
                        LOGGER.warn("No backlight port available for portNumber: {}", portNumber);
                    }
                }
                else {
                    LOGGER.debug("No backlight ports or generic ports available.");
                }
            }
        }

        if (port != null) {
            nodeChangePublisher.firePortValueChanged(BacklightPort.class, port);

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy