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

org.bidib.wizard.common.node.LightPortHandler 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.enums.LightPortEnum;
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.LightPort;
import org.bidib.wizard.model.ports.event.PortListEvent;
import org.bidib.wizard.model.ports.event.PortStatusEvent;
import org.bidib.wizard.model.status.LightPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

        synchronized (ports) {
            ports.clear();

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

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

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

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

        LOGGER.info("Prepare the light ports.");
        for (GenericPort genericPort : genericPorts) {
            // fetch the values from the generic port
            if (genericPort.isSupportsLightPort()) {
                LOGGER.trace("The current port supports light port: {}", genericPort);
                LightPort lightPort = new LightPort(genericPort);
                lightPort.setId(genericPort.getPortNumber());

                LOGGER.trace("Cache the light port: {}", lightPort);
                ports.add(lightPort);
            }
            else {
                LOGGER.trace("The current port does not support light port: {}", genericPort);
            }
        }

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

    @Override
    public List getEnabledPorts() {

        // TODO make sure that the lightPorts are filled already

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

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

        LOGGER.debug("Set the light port status, port number: {}, portState: {}", portNumber, portState);

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

                    for (GenericPort genericPort : node.getGenericPorts()) {
                        // fetch the values from the generic port
                        if (genericPort.isSupportsLightPort() && genericPort.getPortNumber() == portNumber) {
                            LOGGER.debug("The current port supports light port: {}", genericPort);

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

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

                            port = new LightPort(genericPort);
                            break;
                        }
                        else {
                            LOGGER.trace("The current port does not support light port: {}", genericPort);
                        }
                    }
                }
            }

            if (port == null) {
                // make sure the light ports are available
                if (CollectionUtils.isNotEmpty(ports)) {
                    LOGGER.debug("Update the port status in the light ports.");
                    LightPort lightPort = PortListUtils.findPortByPortNumber(ports, portNumber);

                    if (lightPort != null) {
                        LightPortEnum type = LightPortEnum.valueOf(ByteUtils.getLowByte(portState));
                        LightPortStatus status = LightPortStatus.valueOf(type);
                        LOGGER.trace("LightPort status has changed, port: {}, status: {}", lightPort, status);
                        lightPort.setStatus(status);

                        if (lightPort.isInactive()) {
                            lightPort.setInactive(false);
                            portConfigChanged = true;
                        }
                        port = lightPort;
                    }
                    else {
                        LOGGER.warn("No light port available for portNumber: {}", portNumber);
                    }
                }
                else {
                    LOGGER.debug("No light ports or generic ports available.");
                }
            }
        }

        if (port != null) {
            LOGGER.debug("The status of the light port has changed: {}", port);
            nodeChangePublisher.firePortStatusChanged(LightPort.class, port);

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

            final PortStatusEvent portStatusEvent =
                new PortStatusEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), port);
            LOGGER.debug("Publish the port status event: {}", portStatusEvent);
            getPublisher().getSubjectPortEvents().onNext(portStatusEvent);

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy