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

org.bidib.wizard.server.jsontuils.JsonPortMapping Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.server.jsontuils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.bidib.api.json.types.NodeAddress;
import org.bidib.api.json.types.node.PortCountType.PortType;
import org.bidib.api.json.types.occupancy.AbstractPort;
import org.bidib.api.json.types.occupancy.PortIdentifier;
import org.bidib.api.json.types.switching.PcfgType;
import org.bidib.api.json.types.switching.PcfgType.PcfgKeyType;
import org.bidib.api.json.types.switching.PortStatusResponse;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.enums.LcOutputType;
import org.bidib.jbidibc.messages.port.BytePortConfigValue;
import org.bidib.jbidibc.messages.port.Int16PortConfigValue;
import org.bidib.jbidibc.messages.port.PortConfigValue;
import org.bidib.jbidibc.messages.port.RgbPortConfigValue;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.wizard.model.ports.AnalogPort;
import org.bidib.wizard.model.ports.BacklightPort;
import org.bidib.wizard.model.ports.FeedbackPort;
import org.bidib.wizard.model.ports.InputPort;
import org.bidib.wizard.model.ports.LightPort;
import org.bidib.wizard.model.ports.MotorPort;
import org.bidib.wizard.model.ports.Port;
import org.bidib.wizard.model.ports.ServoPort;
import org.bidib.wizard.model.ports.SoundPort;
import org.bidib.wizard.model.ports.StatusPort;
import org.bidib.wizard.model.ports.SwitchPairPort;
import org.bidib.wizard.model.ports.SwitchPort;
import org.bidib.wizard.model.ports.ValuePort;
import org.bidib.wizard.model.status.FeedbackPortStatus;
import org.bidib.wizard.model.status.InputPortStatus;
import org.bidib.wizard.model.status.LightPortStatus;
import org.bidib.wizard.model.status.SoundPortStatus;
import org.bidib.wizard.model.status.SwitchPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class JsonPortMapping {

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonPortMapping.class);

    private final Map> portTypeMapping = new HashMap<>();

    public static final String PORT_PACKAGE_NAME = "org.bidib.api.json.types.switching.";

    @PostConstruct
    public void initialize() {

        portTypeMapping.put(PortType.ANALOGPORT, AnalogPort.class);
        portTypeMapping.put(PortType.BACKLIGHTPORT, BacklightPort.class);
        portTypeMapping.put(PortType.INPUTPORT, InputPort.class);
        portTypeMapping.put(PortType.LIGHTPORT, LightPort.class);
        portTypeMapping.put(PortType.MOTORPORT, MotorPort.class);
        portTypeMapping.put(PortType.SERVOPORT, ServoPort.class);
        portTypeMapping.put(PortType.SOUNDPORT, SoundPort.class);
        portTypeMapping.put(PortType.SWITCHPORT, SwitchPort.class);
        portTypeMapping.put(PortType.SWITCHPAIRPORT, SwitchPairPort.class);
    }

    /**
     * Lookup the name of the json port class.
     * 
     * @param portClazz
     *            the port class
     * @return the json port class
     */
    public String lookupJsonPortClassName(Class portClazz) {

        Entry> entry =
            portTypeMapping
                .entrySet().stream().filter(e -> e.getValue() == portClazz).findFirst()
                .orElseThrow(() -> new RuntimeException("No mapping available for portClazz: " + portClazz));

        return PORT_PACKAGE_NAME + entry.getValue().getSimpleName();
    }

    /**
     * Create a port instance from the provided port identifier.
     * 
     * @param jsonPortIdentifier
     *            the json port identifier
     * @return the port instance
     */
    public > T toPort(PortIdentifier jsonPortIdentifier) {
        T port = null;

        PortType portType = jsonPortIdentifier.getPortType();

        Class portClazz = portTypeMapping.get(portType);
        if (portClazz != null) {
            try {
                port = (T) portClazz.getDeclaredConstructor().newInstance();

                port.setId(jsonPortIdentifier.getId());
            }
            catch (Exception ex) {
                LOGGER.warn("Create new instance of port class failed.", ex);

                throw new RuntimeException("Create new instance of port class failed: " + portClazz.getName());
            }
        }
        else {
            LOGGER.warn("Unknown port type is not processed: {}", jsonPortIdentifier);
        }
        return port;
    }

    public static AbstractPort toJsonPort(Port port) {

        return toJsonPort(port, true, true);
    }

    public static AbstractPort toJsonPort(Port port, boolean withStatus, boolean withConfig) {

        AbstractPort jsonPort = null;

        switch (Port.getPortType(port)) {
            case SWITCHPORT:
                org.bidib.api.json.types.switching.SwitchPort jsonSwitchPort =
                    new org.bidib.api.json.types.switching.SwitchPort();
                SwitchPort switchPort = (SwitchPort) port;
                jsonSwitchPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonSwitchPort.withPortType(PortType.SWITCHPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfg = new HashSet<>();
                    jsonSwitchPort.withPcfg(pcfg);
                    if (switchPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_TICKS)) {
                        pcfg.add(new PcfgType(PcfgKeyType.TICKS, switchPort.getSwitchOffTime()));
                    }

                    // add the ioBehaviour
                    if (switchPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_SWITCH_CTRL)) {
                        pcfg
                            .add(new PcfgType(PcfgKeyType.SWITCH_CTRL,
                                ByteUtils.getInt(switchPort.getOutputBehaviour().getType())));
                    }

                    // add the load type
                    if (switchPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_LOAD_TYPE)) {
                        pcfg
                            .add(new PcfgType(PcfgKeyType.LOAD_TYPE,
                                ByteUtils.getInt(switchPort.getLoadType().getType())));
                    }
                }

                if (withStatus) {
                    // status
                    SwitchPortStatus switchPortStatus = switchPort.getStatus();
                    if (switchPortStatus != null) {
                        jsonSwitchPort
                            .withPortStatus(org.bidib.api.json.types.switching.SwitchPairPort.SwitchPortStatus
                                .fromValue(switchPortStatus.name()));
                    }
                }

                jsonPort = jsonSwitchPort;
                break;
            case SWITCHPAIRPORT:
                org.bidib.api.json.types.switching.SwitchPairPort jsonSwitchPairPort =
                    new org.bidib.api.json.types.switching.SwitchPairPort();
                SwitchPairPort switchPairPort = (SwitchPairPort) port;
                jsonSwitchPairPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonSwitchPairPort.withPortType(PortType.SWITCHPAIRPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfgSwitchPair = new HashSet<>();
                    jsonSwitchPairPort.withPcfg(pcfgSwitchPair);
                    if (switchPairPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_TICKS)) {
                        pcfgSwitchPair.add(new PcfgType(PcfgKeyType.TICKS, switchPairPort.getSwitchOffTime()));
                    }

                    // add the load type
                    if (switchPairPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_LOAD_TYPE)) {
                        pcfgSwitchPair
                            .add(new PcfgType(PcfgKeyType.LOAD_TYPE,
                                ByteUtils.getInt(switchPairPort.getLoadType().getType())));
                    }
                }

                if (withStatus) {
                    SwitchPortStatus switchPairPortStatus = switchPairPort.getStatus();
                    if (switchPairPortStatus != null) {
                        jsonSwitchPairPort
                            .withPortStatus(org.bidib.api.json.types.switching.SwitchPairPort.SwitchPortStatus
                                .fromValue(switchPairPortStatus.name()));
                    }
                }
                jsonPort = jsonSwitchPairPort;
                break;
            case SERVOPORT:
                org.bidib.api.json.types.switching.ServoPort jsonServoPort =
                    new org.bidib.api.json.types.switching.ServoPort();
                ServoPort servoPort = (ServoPort) port;
                jsonServoPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonServoPort.withPortType(PortType.SERVOPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfgServo = new HashSet<>();
                    pcfgServo.add(new PcfgType(PcfgKeyType.SERVO_ADJ_L, servoPort.getTrimDown()));
                    pcfgServo.add(new PcfgType(PcfgKeyType.SERVO_ADJ_H, servoPort.getTrimUp()));
                    pcfgServo.add(new PcfgType(PcfgKeyType.SERVO_SPEED, servoPort.getSpeed()));
                    jsonServoPort.withPcfg(pcfgServo);
                }

                if (withStatus) {
                    Integer servoPosition = servoPort.getValue();
                    jsonServoPort.withPosition(servoPosition);
                }

                jsonPort = jsonServoPort;
                break;
            case INPUTPORT:
                org.bidib.api.json.types.switching.InputPort jsonInputPort =
                    new org.bidib.api.json.types.switching.InputPort();
                InputPort inputPort = (InputPort) port;
                jsonInputPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonInputPort.withPortType(PortType.INPUTPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfgInput = new HashSet<>();
                    jsonInputPort.withPcfg(pcfgInput);
                    if (inputPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_TICKS)) {
                        pcfgInput.add(new PcfgType(PcfgKeyType.TICKS, inputPort.getSwitchOffTime()));
                    }
                    // add the ioBehaviour
                    if (inputPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_INPUT_CTRL)) {
                        pcfgInput
                            .add(new PcfgType(PcfgKeyType.INPUT_CTRL,
                                ByteUtils.getInt(inputPort.getInputBehaviour().getType())));
                    }
                }

                if (withStatus) {
                    InputPortStatus inputPortStatus = inputPort.getStatus();
                    if (inputPortStatus != null) {
                        jsonInputPort
                            .withPortStatus(org.bidib.api.json.types.switching.InputPort.InputPortStatus
                                .fromValue(inputPortStatus.name()));
                    }
                }
                jsonPort = jsonInputPort;
                break;
            case LIGHTPORT:
                org.bidib.api.json.types.switching.LightPort jsonLightPort =
                    new org.bidib.api.json.types.switching.LightPort();
                LightPort lightPort = (LightPort) port;
                jsonLightPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonLightPort.withPortType(PortType.LIGHTPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfgLight = new HashSet<>();
                    pcfgLight.add(new PcfgType(PcfgKeyType.LEVEL_PORT_OFF, lightPort.getPwmMin()));
                    pcfgLight.add(new PcfgType(PcfgKeyType.LEVEL_PORT_ON, lightPort.getPwmMax()));
                    pcfgLight.add(new PcfgType(PcfgKeyType.DIMM_DOWN, lightPort.getDimMin()));
                    pcfgLight.add(new PcfgType(PcfgKeyType.DIMM_UP, lightPort.getDimMax()));
                    jsonLightPort.withPcfg(pcfgLight);

                    // add RGB
                    if (lightPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_RGB)) {
                        pcfgLight.add(new PcfgType(PcfgKeyType.RGB, lightPort.getRgbValue()));
                    }

                    // add transition time
                    if (lightPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_TRANSITION_TIME)) {
                        pcfgLight.add(new PcfgType(PcfgKeyType.TRANSITION_TIME, lightPort.getTransitionTime()));
                    }
                }

                if (withStatus) {
                    LightPortStatus lightPortStatus = lightPort.getStatus();
                    if (lightPortStatus != null) {
                        jsonLightPort
                            .withPortStatus(org.bidib.api.json.types.switching.LightPort.LightPortStatus
                                .valueOf(lightPortStatus.name()));
                    }
                }
                jsonPort = jsonLightPort;
                break;
            case BACKLIGHTPORT:
                org.bidib.api.json.types.switching.BacklightPort jsonBacklightPort =
                    new org.bidib.api.json.types.switching.BacklightPort();
                BacklightPort backlightPort = (BacklightPort) port;
                jsonBacklightPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonBacklightPort.withPortType(PortType.BACKLIGHTPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfgBacklight = new HashSet<>();
                    jsonBacklightPort.withPcfg(pcfgBacklight);
                    pcfgBacklight.add(new PcfgType(PcfgKeyType.DIMM_DOWN, backlightPort.getDimSlopeDown()));
                    pcfgBacklight.add(new PcfgType(PcfgKeyType.DIMM_UP, backlightPort.getDimSlopeUp()));
                    pcfgBacklight
                        .add(new PcfgType(PcfgKeyType.OUTPUT_MAP, backlightPort.getDmxMapping() - DMX_MAPPING_OFFSET));
                }

                if (withStatus) {
                    jsonBacklightPort.setValue(backlightPort.getValue());
                }
                jsonPort = jsonBacklightPort;
                break;

            case FEEDBACKPORT:
                org.bidib.api.json.types.occupancy.FeedbackPort jsonFeedbackPort =
                    new org.bidib.api.json.types.occupancy.FeedbackPort();
                FeedbackPort feedbackPort = (FeedbackPort) port;
                jsonFeedbackPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonFeedbackPort.withPortType(PortType.FEEDBACKPORT);

                FeedbackPortStatus feedbackPortStatus = feedbackPort.getStatus();
                if (feedbackPortStatus != null) {
                    jsonFeedbackPort
                        .withPortStatus(org.bidib.api.json.types.occupancy.FeedbackPort.FeedbackPortStatus
                            .fromValue(feedbackPortStatus.name()));
                }
                jsonPort = jsonFeedbackPort;
                break;

            case MOTORPORT:
                org.bidib.api.json.types.switching.MotorPort jsonMotorPort =
                    new org.bidib.api.json.types.switching.MotorPort();
                MotorPort motorPort = (MotorPort) port;
                jsonMotorPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonMotorPort.withPortType(PortType.MOTORPORT);

                if (withStatus) {
                    int motorSpeed = motorPort.getValue();
                    jsonMotorPort.withSpeed(motorSpeed);
                }
                jsonPort = jsonMotorPort;
                break;

            case SOUNDPORT:
                org.bidib.api.json.types.switching.SoundPort jsonSoundPort =
                    new org.bidib.api.json.types.switching.SoundPort();
                SoundPort soundPort = (SoundPort) port;
                jsonSoundPort.withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive());
                jsonSoundPort.withPortType(PortType.SOUNDPORT);

                if (withConfig) {
                    // prepare the PCFG params
                    Set pcfgSound = new HashSet<>();
                    jsonSoundPort.withPcfg(pcfgSound);
                    if (soundPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_TICKS)) {
                        pcfgSound.add(new PcfgType(PcfgKeyType.TICKS, soundPort.getPulseTime()));
                    }
                }

                if (withStatus) {
                    SoundPortStatus soundPortStatus = soundPort.getStatus();
                    if (soundPortStatus != null) {

                        // special handling for sound port status
                        String statusValue = null;
                        switch (soundPortStatus) {
                            case PLAY:
                                statusValue = "TURN_ON";
                                break;
                            case STOP:
                                statusValue = "TURN_OFF";
                                break;
                            case TEST:
                            default:
                                statusValue = soundPortStatus.name();
                                break;
                        }
                        jsonSoundPort
                            .withPortStatus(
                                org.bidib.api.json.types.switching.SoundPort.SoundPortStatus.fromValue(statusValue));
                    }
                }
                jsonPort = jsonSoundPort;
                break;

            default:
                LOGGER.warn("Unsupported port type: {}", port);
                break;
        }

        if (jsonPort != null) {
            jsonPort.withPortIdentifier(port.getPortIdentifier());
            jsonPort.withLabel(port.getLabel());
        }

        LOGGER.info("Prepared jsonPort: {}", jsonPort);
        return jsonPort;
    }

    private static final int DMX_MAPPING_OFFSET = 1;

    /**
     * Add the port config to the provided port.
     * 
     * @param port
     *            the port
     * @param pcfgs
     *            the port config
     */
    public static void addPortConfigX(Port port, Set pcfgs) {

        Map> portConfigX = port.getPortConfigX();

        for (PcfgType pcfg : pcfgs) {

            PcfgKeyType key = pcfg.getKey();
            Integer value = pcfg.getValue();
            LOGGER.info("Current key: {}, value: {}", key, value);

            switch (key) {
                case SERVO_SPEED:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_SERVO_SPEED, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case SERVO_ADJ_L:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_SERVO_ADJ_L, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case SERVO_ADJ_H:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_SERVO_ADJ_H, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case TICKS:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_TICKS, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case INPUT_CTRL:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_INPUT_CTRL, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case SWITCH_CTRL:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_SWITCH_CTRL, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case LOAD_TYPE:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_LOAD_TYPE, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case RGB:
                    portConfigX.put(BidibLibrary.BIDIB_PCFG_RGB, new RgbPortConfigValue(value));
                    break;
                case LEVEL_PORT_ON:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_LEVEL_PORT_ON,
                            new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case LEVEL_PORT_OFF:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_LEVEL_PORT_OFF,
                            new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case DIMM_DOWN:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_DIMM_DOWN, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case DIMM_UP:
                    portConfigX
                        .put(BidibLibrary.BIDIB_PCFG_DIMM_UP, new BytePortConfigValue(ByteUtils.getLowByte(value)));
                    break;
                case DIMM_DOWN_8_8:
                    portConfigX.put(BidibLibrary.BIDIB_PCFG_DIMM_DOWN_8_8, new Int16PortConfigValue(value));
                    break;
                case DIMM_UP_8_8:
                    portConfigX.put(BidibLibrary.BIDIB_PCFG_DIMM_UP_8_8, new Int16PortConfigValue(value));
                    break;
                default:
                    LOGGER.warn("Unknown port configX key is not processed: {}", key);
                    break;
            }
        }

        port.setPortConfigX(portConfigX);
    }

    /**
     * Create a port with the provided status or the provided value.
     * 
     * @param jsonPortIdentifier
     *            the port identifier
     * @param status
     *            the status
     * @param value
     *            the value
     * @return the port instance
     */
    public Port toPortWithStatus(PortIdentifier jsonPortIdentifier, String status, Integer value) {

        Port port = toPort(jsonPortIdentifier);
        if (port instanceof ValuePort) {

            ValuePort valuePort = (ValuePort) port;
            valuePort.setValue(value);
            // valuePort.setValue(Integer.parseInt(status));
        }
        else if (port instanceof StatusPort) {
            StatusPort statusPort = (StatusPort) port;

            if (statusPort instanceof SwitchPort || statusPort instanceof SwitchPairPort) {
                SwitchPortStatus portStatus = SwitchPortStatus.valueOf(status);
                ((StatusPort) statusPort).setStatus(portStatus);
            }
            else if (statusPort instanceof LightPort) {
                org.bidib.api.json.types.switching.LightPort.LightPortStatus portStatus =
                    org.bidib.api.json.types.switching.LightPort.LightPortStatus.fromValue(status);
                LightPortStatus lightPortStatus = LightPortStatus.valueOf(portStatus.name());
                ((LightPort) statusPort).setStatus(lightPortStatus);
            }
            else if (statusPort instanceof SoundPort) {
                org.bidib.api.json.types.switching.SoundPort.SoundPortStatus portStatus =
                    org.bidib.api.json.types.switching.SoundPort.SoundPortStatus.fromValue(status);
                SoundPortStatus soundPortStatus = SoundPortStatus.valueOf(portStatus.name());
                ((SoundPort) statusPort).setStatus(soundPortStatus);
            }
            else {
                LOGGER.warn("Unhandled port type to set the status: {}, status: {}", statusPort, status);
            }
        }
        else {
            LOGGER.warn("The current port is not a ValuePort and not a StatusPort: {}", port);
        }

        return port;
    }

    /**
     * Convert the feedback port to a JSON feedback port.
     * 
     * @param port
     *            the feedback port
     * @return the JSON feedback port
     */
    public static org.bidib.api.json.types.occupancy.FeedbackPort toJsonOccupancyPort(Port port) {

        org.bidib.api.json.types.occupancy.FeedbackPort jsonFeedbackPort = null;

        if (port instanceof FeedbackPort) {
            FeedbackPort feedbackPort = (FeedbackPort) port;
            jsonFeedbackPort =
                new org.bidib.api.json.types.occupancy.FeedbackPort()
                    .withId(port.getId()).withEnabled(port.isEnabled()).withInactive(port.isInactive())
                    .withLabel(port.getLabel())
                    .withPortStatus(org.bidib.api.json.types.occupancy.FeedbackPort.FeedbackPortStatus
                        .fromValue(feedbackPort.getStatus().name()));
            jsonFeedbackPort.withPortType(PortType.FEEDBACKPORT);
        }
        else {
            LOGGER.warn("Unhandled port type to convert: {}", port);
        }

        return jsonFeedbackPort;
    }

    /**
     * Create the {@code PortStatusResponse} from the values of the port.
     * 
     * @param connectionId
     *            the connection id
     * @param nodeAddress
     *            the node address
     * @param port
     *            the port
     * @return the json instance
     */
    public static PortStatusResponse toJsonPortStatusResponse(
        String connectionId, NodeAddress nodeAddress, Port port) {

        LcOutputType portType = port.getPortType();
        PortType jsonPortType = PortType.valueOf(portType.name());

        final PortStatusResponse portStatusResponse =
            new PortStatusResponse()
                .withConnectionId(connectionId).withNode(nodeAddress)
                .withPort(new PortIdentifier(port.getPortNumber(), jsonPortType));

        if (port instanceof ValuePort) {
            Integer value = ((ValuePort) port).getValue();
            portStatusResponse.withValue(value);
        }
        else if (port instanceof StatusPort) {
            StatusPort statusPort = (StatusPort) port;

            if (statusPort instanceof SwitchPort || statusPort instanceof SwitchPairPort) {
                SwitchPortStatus portStatus = ((StatusPort) statusPort).getStatus();
                if (portStatus != null) {
                    portStatusResponse
                        .withStatus(org.bidib.api.json.types.switching.SwitchPairPort.SwitchPortStatus
                            .fromValue(portStatus.name()).name());
                }
            }
            else if (statusPort instanceof LightPort) {
                LightPortStatus lightPortStatus = ((LightPort) statusPort).getStatus();
                if (lightPortStatus != null) {
                    portStatusResponse
                        .withStatus(org.bidib.api.json.types.switching.LightPort.LightPortStatus
                            .valueOf(lightPortStatus.name()).value());
                }
            }
            else if (statusPort instanceof SoundPort) {
                SoundPortStatus soundPortStatus = ((SoundPort) statusPort).getStatus();
                if (soundPortStatus != null) {
                    portStatusResponse
                        .withStatus(org.bidib.api.json.types.switching.SoundPort.SoundPortStatus
                            .valueOf(soundPortStatus.name()).name());
                }
            }
            else {
                LOGGER
                    .warn("Unhandled port type to set the status: {}, status: {}", statusPort, statusPort.getStatus());
            }
        }
        else if (port instanceof InputPort) {
            InputPort inputPort = (InputPort) port;
            if (inputPort.getStatus() != null) {
                portStatusResponse
                    .withStatus(org.bidib.api.json.types.switching.InputPort.InputPortStatus
                        .fromValue(inputPort.getStatus().name()).name());
            }
        }
        else {
            LOGGER.warn("The current port is not a ValuePort and not a StatusPort: {}", port);
        }

        return portStatusResponse;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy