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

org.bidib.wizard.common.node.SoundPortHandler 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.SoundPortEnum;
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.SoundPort;
import org.bidib.wizard.model.ports.event.PortListEvent;
import org.bidib.wizard.model.ports.event.PortStatusEvent;
import org.bidib.wizard.model.status.SoundPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

        synchronized (ports) {
            ports.clear();

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

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

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

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

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

    @Override
    public List getEnabledPorts() {

        // TODO make sure that the soundPorts are filled already

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

    public void setPortStatus(final int portNumber, int portState, final NodeChangePublisher nodeChangePublisher) {
        SoundPort 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.isSupportsSoundPort()) {
                        LOGGER.info("The current port supports sound port: {}", genericPort);

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

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

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

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

                    SoundPort soundPort = PortListUtils.findPortByPortNumber(ports, portNumber);

                    if (soundPort != null) {
                        SoundPortEnum type = SoundPortEnum.valueOf(ByteUtils.getLowByte(portState));
                        SoundPortStatus status = SoundPortStatus.valueOf(type);
                        LOGGER.debug("SoundPort status has changed, port: {}, status: {}", soundPort, status);
                        soundPort.setStatus(status);

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

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

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

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

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy