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

org.bidib.wizard.simulation.ReadyDmxLineSimulator Maven / Gradle / Ivy

There is a newer version: 2.0.34
Show newest version
package org.bidib.wizard.simulation;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.BidibPort;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.LcConfigX;
import org.bidib.jbidibc.messages.enums.LcOutputType;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.AccessoryGetMessage;
import org.bidib.jbidibc.messages.message.AccessoryParaGetMessage;
import org.bidib.jbidibc.messages.message.AccessoryParaResponse;
import org.bidib.jbidibc.messages.message.AccessoryParaSetMessage;
import org.bidib.jbidibc.messages.message.AccessorySetMessage;
import org.bidib.jbidibc.messages.message.AccessoryStateResponse;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.jbidibc.messages.message.LcConfigGetMessage;
import org.bidib.jbidibc.messages.message.LcConfigSetMessage;
import org.bidib.jbidibc.messages.message.LcConfigXGetAllMessage;
import org.bidib.jbidibc.messages.message.LcConfigXGetMessage;
import org.bidib.jbidibc.messages.message.LcConfigXResponse;
import org.bidib.jbidibc.messages.message.LcConfigXSetMessage;
import org.bidib.jbidibc.messages.message.LcKeyMessage;
import org.bidib.jbidibc.messages.message.LcNotAvailableResponse;
import org.bidib.jbidibc.messages.message.LcOutputMessage;
import org.bidib.jbidibc.messages.message.LcPortQueryAllMessage;
import org.bidib.jbidibc.messages.message.LcPortQueryMessage;
import org.bidib.jbidibc.messages.message.LcStatResponse;
import org.bidib.jbidibc.messages.port.PortConfigUtils;
import org.bidib.jbidibc.messages.port.PortConfigValue;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.simulation.SimulationBidibMessageProcessor;
import org.bidib.jbidibc.simulation.SwitchingFunctionsNode;
import org.bidib.jbidibc.simulation.annotation.BidibNodeSimulator;
import org.bidib.jbidibc.simulation.annotation.BidibNodeSimulators;
import org.bidib.jbidibc.simulation.nodes.DefaultNodeSimulator;
import org.bidib.jbidibc.simulation.nodes.FlatPortType;
import org.bidib.jbidibc.simulation.nodes.InputPortType;
import org.bidib.jbidibc.simulation.nodes.PortType;
import org.bidib.wizard.model.ports.InputPort;
import org.bidib.wizard.model.ports.Port;
import org.bidib.wizard.model.status.InputPortStatus;
import org.bidib.wizard.simulation.events.InputPortSetStatusEvent;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventSubscriber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@BidibNodeSimulators({ @BidibNodeSimulator(vid = "13", pid = "118"), @BidibNodeSimulator(vid = "251", pid = "113"),
    @BidibNodeSimulator(vid = "251", pid = "114") })
public class ReadyDmxLineSimulator extends DefaultNodeSimulator implements SwitchingFunctionsNode {

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

    protected final Map inputPorts = new HashMap();

    protected int inputPortCount;

    private byte totalAspects = 26;

    public ReadyDmxLineSimulator(byte[] nodeAddress, long uniqueId, boolean autoAddFeature,
        SimulationBidibMessageProcessor messageReceiver, final BidibRequestFactory bidibRequestFactory) {
        super(nodeAddress, uniqueId, autoAddFeature, messageReceiver, bidibRequestFactory);
    }

    @Override
    protected void prepareFeatures() {
        LOGGER.info("Prepare the features.");
        super.prepareFeatures();

        // features.add(new Feature(BidibLibrary.FEATURE_CTRL_MAC_COUNT, 0));
        // features.add(new Feature(BidibLibrary.FEATURE_CTRL_MAC_LEVEL, 0));

        if (inputPortCount > 0) {
            features.add(new Feature(BidibLibrary.FEATURE_CTRL_INPUT_COUNT, inputPortCount));
        }

        features.add(new Feature(BidibLibrary.FEATURE_ACCESSORY_COUNT, 8));
        // features.add(new Feature(BidibLibrary.FEATURE_ACCESSORY_MACROMAPPED, 0));
        features.add(new Feature(BidibLibrary.FEATURE_ACCESSORY_SURVEILLED, 0));

        features.add(new Feature(BidibLibrary.FEATURE_FW_UPDATE_MODE, 1));
    }

    protected void setupInputPorts() {
        if (CollectionUtils.isNotEmpty(inputPorts.values())) {
            LOGGER.info("InputPorts are set externally already.");
            return;
        }

        for (int id = 0; id < inputPortCount; id++) {
            InputPort port = new InputPort();

            port.setId(id);
            port.setStatus(id % 2 == 0 ? InputPortStatus.ON : InputPortStatus.OFF);
            inputPorts.put(id, port);
        }
    }

    @Override
    public void start() {
        LOGGER.info("Start the simulator for address: {}", getAddress());

        AnnotationProcessor.process(this);

        // prepare the input ports
        setupInputPorts();

        super.start();
    }

    @Override
    public void stop() {
        AnnotationProcessor.unprocess(this);
        super.stop();
    }

    @EventSubscriber(eventClass = InputPortSetStatusEvent.class)
    public void inputPortSetStatus(InputPortSetStatusEvent setStatusEvent) {
        LOGGER.info("The change of the input port was requested.");
        String nodeAddress = setStatusEvent.getNodeAddr();

        // check if the node is addressed
        if (!isAddressEqual(nodeAddress)) {
            LOGGER.trace("Another node is addressed.");
            return;
        }

        int portNum = setStatusEvent.getPortNum();

        changeInputPortStatus(portNum);
    }

    protected void changeInputPortStatus(int portNum) {

        InputPort port = inputPorts.get(portNum);
        if (port != null) {
            switch (port.getStatus()) {
                case OFF:
                    port.setStatus(InputPortStatus.ON);
                    break;
                default:
                    port.setStatus(InputPortStatus.OFF);
                    break;
            }

            // prepare the request
            final LcKeyMessage lcKeyMessage = new LcKeyMessage(portNum) {
                @Override
                public byte[] getAddr() {
                    return getNodeAddress();
                }
            };
            processRequest(lcKeyMessage);
        }
        else {
            LOGGER.warn("The requested light port is not available: {}", portNum);
        }
    }

    @Override
    protected byte[] prepareResponse(BidibMessageInterface bidibMessage) {

        byte[] response = null;
        switch (ByteUtils.getInt(bidibMessage.getType())) {
            case BidibLibrary.MSG_LC_OUTPUT:
                response = processLcOutputRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_CONFIG_GET:
                response = processLcConfigGetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_CONFIG_SET:
                response = processLcConfigSetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_PORT_QUERY:
                response = processLcPortQueryRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_PORT_QUERY_ALL:
                processLcPortQueryAllRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_CONFIGX_SET:
                response = processLcConfigXSetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_CONFIGX_GET:
                response = processLcConfigXGetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_LC_CONFIGX_GET_ALL:
                processLcConfigXGetAllRequest(bidibMessage);
                break;

            case BidibLibrary.MSG_ACCESSORY_SET:
                response = processAccessorySetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_ACCESSORY_GET:
                response = processAccessoryGetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_ACCESSORY_GETALL:
                response = processAccessoryGetAllRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_ACCESSORY_PARA_SET:
                response = processAccessoryParaSetRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_ACCESSORY_PARA_GET:
                response = processAccessoryParaGetRequest(bidibMessage);
                break;
            default:
                response = super.prepareResponse(bidibMessage);
                break;
        }
        return response;
    }

    protected byte[] processLcOutputRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the LcOutput request: {}", bidibMessage);
        byte[] response = null;
        try {
            LcOutputMessage lcOutputMessage = (LcOutputMessage) bidibMessage;
            LcOutputType outputType = lcOutputMessage.getOutputType(getPortModel());
            int outputNumber = lcOutputMessage.getOutputNumber(getPortModel());

            BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), outputType, outputNumber);

            LcNotAvailableResponse lcNotAvailableResponse =
                new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
            response = lcNotAvailableResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcStat response failed.", ex);
        }
        return response;
    }

    protected byte[] processLcConfigGetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the LcConfigGet request: {}", bidibMessage);
        byte[] response = null;

        try {
            LcConfigGetMessage lcConfigGetMessage = (LcConfigGetMessage) bidibMessage;

            LcOutputType outputType = lcConfigGetMessage.getPortType(getPortModel());
            int outputNumber = lcConfigGetMessage.getPortNumber(getPortModel());

            BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), outputType, outputNumber);

            LcNotAvailableResponse lcNotAvailableResponse =
                new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
            response = lcNotAvailableResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcConfig response failed.", ex);
        }
        return response;
    }

    protected byte[] processLcConfigSetRequest(BidibMessageInterface bidibMessage) {

        LOGGER.info("Process the LcConfigSet request: {}", bidibMessage);
        byte[] response = null;
        try {
            LcConfigSetMessage lcConfigSetMessage = (LcConfigSetMessage) bidibMessage;

            LcOutputType outputType = lcConfigSetMessage.getPortType(getPortModel());
            int outputNumber = lcConfigSetMessage.getPortNumber(getPortModel());

            BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), outputType, outputNumber);

            LOGGER.warn("No port assigned!");
            LcNotAvailableResponse magicResponse =
                new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
            response = magicResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcStat response failed.", ex);
        }
        return response;
    }

    protected byte[] processLcConfigXSetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the LcConfigXSet request: {}", bidibMessage);
        byte[] response = null;
        try {
            LcConfigXSetMessage lcConfigXSetMessage = (LcConfigXSetMessage) bidibMessage;
            int outputNumber = lcConfigXSetMessage.getPortNumber(getPortModel());

            LcOutputType outputType = lcConfigXSetMessage.getPortType(getPortModel());

            Port port = null;
            switch (outputType) {
                case INPUTPORT:
                    InputPort inputPort = inputPorts.get(Integer.valueOf(outputNumber));
                    if (inputPort != null) {
                        inputPort.setPortConfigX(lcConfigXSetMessage.getLcConfigX(messageLogger).getPortConfig());
                        port = inputPort;
                    }
                    else {
                        LOGGER.warn("Inputport not available, outputNumber: {}", outputNumber);
                    }
                    break;
                default:
                    LOGGER.warn("LcConfigSet request for unsupported port type detected: {}", outputType);
                    break;
            }

            BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), outputType, outputNumber);

            if (port != null) {
                LcConfigX lcConfigX =
                    new LcConfigX(bidibPort, lcConfigXSetMessage.getLcConfigX(messageLogger).getPortConfig());

                LcConfigXResponse lcConfigXResponse =
                    new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
                        LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
                response = lcConfigXResponse.getContent();
            }
            else {
                LOGGER.warn("No port assigned!");
                LcNotAvailableResponse magicResponse =
                    new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
                response = magicResponse.getContent();
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcConfigX response failed.", ex);
        }
        return response;
    }

    protected byte[] processLcConfigXGetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the LcConfigXGet request: {}", bidibMessage);
        byte[] response = null;

        try {
            LcConfigXGetMessage lcConfigXGetMessage = (LcConfigXGetMessage) bidibMessage;
            int outputNumber = lcConfigXGetMessage.getPortNumber(getPortModel());
            LcOutputType lcOutputType = lcConfigXGetMessage.getPortType(getPortModel());
            Port port = null;
            Map> values = new LinkedHashMap<>();
            switch (lcOutputType) {
                case INPUTPORT:
                    InputPort inputPort = inputPorts.get(Integer.valueOf(outputNumber));
                    if (inputPort != null) {

                        LOGGER.info("Return config of input port: {}", inputPort);
                        BidibPort bidibPort =
                            BidibPort.prepareBidibPort(getPortModel(), lcOutputType, inputPort.getId());
                        LcConfigX lcConfigX = new LcConfigX(bidibPort, values);

                        LcConfigXResponse lcConfigXResponse =
                            new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
                                LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
                        response = lcConfigXResponse.getContent();

                        LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));

                    }
                    else {
                        LOGGER.warn("Inputport not available, outputNumber: {}", outputNumber);
                    }
                    break;
                default:
                    LOGGER.warn("LcConfigGet request for unsupported port type detected: {}", lcOutputType);
                    break;
            }

            LOGGER.info("Return config of port: {}", port);

            if (response == null) {
                BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), lcOutputType, outputNumber);

                LOGGER.warn("No port assigned!");
                LcNotAvailableResponse magicResponse =
                    new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
                response = magicResponse.getContent();
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcConfigX response failed.", ex);
        }
        return response;
    }

    protected void processLcConfigXGetAllRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the LcConfigXGetAll request: {}", bidibMessage);
        byte[] response = null;

        try {
            LcConfigXGetAllMessage lcConfigXGetAllMessage = (LcConfigXGetAllMessage) bidibMessage;
            LcOutputType lcOutputType = lcConfigXGetAllMessage.getPortTypeFrom(getPortModel());

            // TODO evaluate port type/range to

            Map> values = new LinkedHashMap<>();

            if (lcOutputType != null) {
                LOGGER.info("Get all ports for output type: {}", lcOutputType);

                switch (lcOutputType) {
                    case INPUTPORT:
                        for (InputPort inputPort : inputPorts.values()) {
                            values.clear();

                            LOGGER.info("Return config of input port: {}", inputPort);
                            BidibPort bidibPort =
                                BidibPort.prepareBidibPort(getPortModel(), lcOutputType, inputPort.getId());
                            LcConfigX lcConfigX = new LcConfigX(bidibPort, values);

                            LcConfigXResponse lcConfigXResponse =
                                new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
                                    LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
                            response = lcConfigXResponse.getContent();

                            LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
                            sendSpontanousResponse(response);
                            response = null;
                        }
                        break;
                    default:
                        LOGGER.warn("Unsupported port type requested: {}", lcOutputType);
                        break;
                }
            }
            else {
                // deliver input ports
                for (InputPort inputPort : inputPorts.values()) {
                    values.clear();

                    LOGGER.info("Return config of input port: {}", inputPort);
                    BidibPort bidibPort =
                        BidibPort.prepareBidibPort(getPortModel(), LcOutputType.INPUTPORT, inputPort.getId());
                    LcConfigX lcConfigX = new LcConfigX(bidibPort, values);

                    LcConfigXResponse lcConfigXResponse =
                        new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
                            LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
                    response = lcConfigXResponse.getContent();

                    LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
                    sendSpontanousResponse(response);
                    response = null;
                }

            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create lcConfigXResponse response failed.", ex);
        }
    }

    protected byte[] processLcPortQueryRequest(BidibMessageInterface bidibMessage) {

        LOGGER.info("Process the LcOutputQuery request: {}", bidibMessage);
        byte[] response = null;

        byte portState = 0;

        try {
            final LcPortQueryMessage lcPortQueryMessage = (LcPortQueryMessage) bidibMessage;
            LcOutputType outputType = lcPortQueryMessage.getPortType(getPortModel());
            int outputNumber = lcPortQueryMessage.getPortNumber(getPortModel());
            LOGGER.info("Get port with portNumber: {}", outputNumber);

            switch (outputType) {
                case INPUTPORT:
                    portState = inputPorts.get(outputNumber).getStatus().getType().getType();
                    break;
                default:
                    LOGGER.warn("LcOutputQuery for unsupported port type detected: {}", outputType);
                    break;
            }
            BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), outputType, outputNumber);

            LcStatResponse lcStatResponse =
                new LcStatResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort, portState);
            response = lcStatResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcStat response failed.", ex);
        }
        return response;
    }

    protected byte[] processLcPortQueryAllRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the PortQueryAll request: {}", bidibMessage);
        byte[] response = null;
        try {
            LcPortQueryAllMessage portQueryAllMessage = (LcPortQueryAllMessage) bidibMessage;
            int portRangeFrom = portQueryAllMessage.getPortRangeFrom(getPortModel());
            int portRangeTo = portQueryAllMessage.getPortRangeTo(getPortModel());

            int portTypeMask = portQueryAllMessage.getPortTypeMask();

            LOGGER
                .info("Query all port states, portRangeFrom: {}, portRangeTo: {}, portModel: {}, portTypeMask: {}",
                    portRangeFrom, portRangeTo, getPortModel(), portTypeMask);

            if (PortConfigUtils.isSupportsInputPort(portTypeMask) && MapUtils.isNotEmpty(inputPorts)) {
                for (InputPort inputPort : inputPorts.values()) {

                    if (inputPort.getId() >= portRangeFrom && inputPort.getId() < portRangeTo) {
                        try {
                            InputPortStatus inputPortStatus = inputPort.getStatus();
                            byte portStatus = inputPortStatus.getType().getType();

                            publishPortState(bidibMessage.getAddr(), LcOutputType.INPUTPORT, inputPort.getId(),
                                portStatus);
                        }
                        catch (Exception ex) {
                            LOGGER.warn("Publish port state failed for port: {}", inputPort, ex);
                        }
                    }
                    else {
                        LOGGER.info("Skip input port that is out of port range: {}", inputPort);
                    }
                }
            }

            LOGGER.info("Send the terminating LC_NA message.");
            publishLcNaResponse(bidibMessage.getAddr());

        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create LcStat response failed.", ex);
        }
        return response;
    }

    protected void publishPortState(byte[] address, LcOutputType outputType, int outputNumber, byte portStatus)
        throws ProtocolException {

        BidibPort bidibPort = BidibPort.prepareBidibPort(getPortModel(), outputType, outputNumber);

        LcStatResponse lcStatResponse = new LcStatResponse(address, getNextSendNum(), bidibPort, portStatus);

        LOGGER.info("Prepared LcStatResponse: {}", lcStatResponse);

        byte[] response = lcStatResponse.getContent();
        sendSpontanousResponse(response);
    }

    protected void publishLcNaResponse(byte[] address) throws ProtocolException {

        BidibPort bidibPort = new BidibPort(new byte[] { ByteUtils.getLowByte(0xFF), ByteUtils.getLowByte(0xFF) });

        LcNotAvailableResponse lcNotAvailableResponse =
            new LcNotAvailableResponse(address, getNextSendNum(), bidibPort);

        LOGGER.info("Prepared LcNotAvailableResponse: {}", lcNotAvailableResponse);

        byte[] response = lcNotAvailableResponse.getContent();
        sendSpontanousResponse(response);
    }

    protected byte[] processAccessorySetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the AccessorySet request: {}", bidibMessage);
        byte[] response = null;

        try {
            AccessorySetMessage accessorySetMessage = (AccessorySetMessage) bidibMessage;
            int accessoryNumber = accessorySetMessage.getAccessoryNumber();
            int aspect = accessorySetMessage.getAspect();

            byte[] value = new byte[] { totalAspects, 0, 0 };

            AccessoryStateResponse accessoryStateResponse =
                new AccessoryStateResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) accessoryNumber,
                    (byte) aspect, value);
            response = accessoryStateResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create AccessoryState response failed.", ex);
        }
        return response;
    }

    protected byte[] processAccessoryGetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the AccessoryGet request: {}", bidibMessage);
        byte[] response = null;

        try {
            AccessoryGetMessage accessoryGetMessage = (AccessoryGetMessage) bidibMessage;
            int accessoryNumber = accessoryGetMessage.getAccessoryNumber();
            int aspect = 1;
            byte[] value = new byte[] { totalAspects, 0, 0 };

            AccessoryStateResponse accessoryStateResponse =
                new AccessoryStateResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) accessoryNumber,
                    (byte) aspect, value);
            response = accessoryStateResponse.getContent();

            LOGGER.info("Return accessoryStateResponse: {}", accessoryStateResponse.toExtendedString());
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create AccessoryState response failed.", ex);
        }
        return response;
    }

    protected byte[] processAccessoryGetAllRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the AccessoryGetAll request: {}", bidibMessage);
        byte[] response = null;

        try {

            Feature featureAccessoryCount = Feature.findFeature(features, BidibLibrary.FEATURE_ACCESSORY_COUNT);
            int accessoryCount = featureAccessoryCount.getValue();

            int aspect = 0;

            for (int accessoryNumber = 0; accessoryNumber < accessoryCount; accessoryNumber++) {

                byte[] value = new byte[] { 3 /* total */, 0 /* execute */, 0 /* wait */ };

                Feature featureAccessoryMacroMapped =
                    Feature.findFeature(features, BidibLibrary.FEATURE_ACCESSORY_MACROMAPPED);

                if (featureAccessoryMacroMapped != null && featureAccessoryMacroMapped.getValue() > 0) {
                    value[0] = 0; // no aspects by default if macro-mapped

                    switch (accessoryNumber) {
                        case 0: // auto start
                            value[0] = 2; // 2 non-macro mapped aspects for accessory number 0
                            aspect = 0; // aspect 0 is active
                            break;
                        case 1: // overlay
                            value[0] = 5; // 5 non-macro mapped aspects for accessory number 1
                            aspect = 0; // aspect 0 is active
                            break;
                        case 2: // fixed pattern
                            value[0] = 7; // 7 non-macro mapped aspects for accessory number 2
                            aspect = 0; // aspect 0 is active
                            break;
                        case 3: // extra pattern
                            value[0] = 4; // 4 non-macro mapped aspects for accessory number 3
                            aspect = 0; // aspect 0 is active
                            break;
                        default:
                            break;
                    }
                }

                LOGGER.info("Return data for accessory: {}, data: {}", accessoryNumber, ByteUtils.bytesToHex(value));

                AccessoryStateResponse accessoryStateResponse =
                    new AccessoryStateResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) accessoryNumber,
                        (byte) aspect, value);
                response = accessoryStateResponse.getContent();

                sendSpontanousResponse(response);
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create AccessoryState response failed.", ex);
        }
        return null;
    }

    protected byte[] processAccessoryParaSetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the AccessoryParaSet request: {}", bidibMessage);
        byte[] response = null;

        try {
            AccessoryParaSetMessage accessoryParaSetMessage = (AccessoryParaSetMessage) bidibMessage;
            int accessoryNumber = accessoryParaSetMessage.getAccessoryNumber();
            int paraNumber = accessoryParaSetMessage.getParaNumber();

            byte[] value = accessoryParaSetMessage.getValue();

            AccessoryParaResponse accessoryParaResponse =
                new AccessoryParaResponse(bidibMessage.getAddr(), getNextSendNum(),
                    ByteUtils.getLowByte(accessoryNumber), ByteUtils.getLowByte(paraNumber), value);
            response = accessoryParaResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create AccessoryPara response failed.", ex);
        }
        return response;
    }

    protected byte[] processAccessoryParaGetRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the AccessoryParaGet request: {}", bidibMessage);
        byte[] response = null;

        try {
            AccessoryParaGetMessage accessoryParaGetMessage = (AccessoryParaGetMessage) bidibMessage;
            int accessoryNumber = accessoryParaGetMessage.getAccessoryNumber();
            int paraNumber = accessoryParaGetMessage.getParaNumber();

            if (paraNumber == BidibLibrary.BIDIB_ACCESSORY_SWITCH_TIME) {

                LOGGER.info("The param BIDIB_ACCESSORY_SWITCH_TIME is known for accessory 0 and 1!");

                // TODO provide the correct data here ...
                byte[] value = null;

                switch (accessoryNumber) {
                    case 0:
                        value = new byte[] { ByteUtils.getLowByte(0x87) };
                        break;
                    case 1:
                        value = new byte[] { ByteUtils.getLowByte(0x77) };
                        break;
                    default:
                        value = new byte[] { ByteUtils.getLowByte(paraNumber) };
                        paraNumber = BidibLibrary.BIDIB_ACCESSORY_PARA_NOTEXIST;
                        break;
                }

                AccessoryParaResponse accessoryParaResponse =
                    new AccessoryParaResponse(bidibMessage.getAddr(), getNextSendNum(),
                        ByteUtils.getLowByte(accessoryNumber), ByteUtils.getLowByte(paraNumber), value);
                response = accessoryParaResponse.getContent();
            }
            else {
                // TODO provide the correct data here ...
                byte[] value = new byte[] { 0, 0, 0, 0 };

                AccessoryParaResponse accessoryParaResponse =
                    new AccessoryParaResponse(bidibMessage.getAddr(), getNextSendNum(),
                        ByteUtils.getLowByte(accessoryNumber), ByteUtils.getLowByte(paraNumber), value);
                response = accessoryParaResponse.getContent();
            }

        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create AccessoryPara response failed.", ex);
        }
        return response;
    }

    @Override
    public void setPortsConfig(FlatPortType portType) {
    }

    @Override
    public void setPortsConfig(PortType portType) {
        if (portType == null) {
            return;
        }

        if (portType instanceof InputPortType) {
            inputPortCount = portType.getCount();

            LOGGER.info("Configured number of input ports: {}", inputPortCount);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy