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

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

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

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

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

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

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

            portsProcessed = false;
        }

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

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

        LOGGER.debug("Prepare the cached servo ports.");
        for (GenericPort genericPort : genericPorts) {
            // fetch the values from the generic port
            if (genericPort.isSupportsServoPort()) {
                LOGGER.trace("The current port supports servo port: {}", genericPort);
                ServoPort servoPort = new ServoPort(genericPort);
                servoPort.setId(genericPort.getPortNumber());
                ports.add(servoPort);
            }
            else {
                LOGGER.trace("The current port does not support servo port: {}", genericPort);
            }
        }

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

    @Override
    public List getEnabledPorts() {

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

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

                        genericPort.setPortValue(portValue);

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

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

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

                    ServoPort servoPort = PortListUtils.findPortByPortNumber(ports, portNumber);

                    if (servoPort != null) {
                        servoPort.setValue(portValue);

                        LOGGER.info("ServoPort value has changed, port: {}, value: {}", servoPort, portValue);
                        if (servoPort.isInactive()) {
                            servoPort.setInactive(false);
                            portConfigChanged = true;
                        }
                        port = servoPort;
                    }
                    else {
                        LOGGER.warn("No servo port available for portNumber: {}", portNumber);
                    }
                }
                else {
                    LOGGER.warn("No servo ports or generic ports available.");
                }
            }
        }

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

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy