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

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

The newest version!
package org.bidib.wizard.simulation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.collections4.CollectionUtils;
import org.bidib.jbidibc.messages.AddressData;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.FeedbackAddressData;
import org.bidib.jbidibc.messages.enums.AddressTypeEnum;
import org.bidib.jbidibc.messages.enums.EnrailmentDirectionEnum;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.BidibMessage;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.jbidibc.messages.message.FeedbackAddressResponse;
import org.bidib.jbidibc.messages.message.FeedbackConfidenceResponse;
import org.bidib.jbidibc.messages.message.FeedbackFreeResponse;
import org.bidib.jbidibc.messages.message.FeedbackGetRangeMessage;
import org.bidib.jbidibc.messages.message.FeedbackMultipleResponse;
import org.bidib.jbidibc.messages.message.FeedbackOccupiedResponse;
import org.bidib.jbidibc.messages.message.FeedbackSpeedResponse;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.messages.utils.NodeUtils;
import org.bidib.jbidibc.messages.utils.ThreadFactoryBuilder;
import org.bidib.jbidibc.simulation.SimulationBidibMessageProcessor;
import org.bidib.jbidibc.simulation.annotation.BidibNodeSimulator;
import org.bidib.jbidibc.simulation.nodes.DefaultNodeSimulator;
import org.bidib.wizard.model.ports.FeedbackPort;
import org.bidib.wizard.model.ports.Port;
import org.bidib.wizard.model.status.FeedbackPortStatus;
import org.bidib.wizard.simulation.events.FeedbackConfidenceSetEvent;
import org.bidib.wizard.simulation.events.FeedbackConfidenceStatusEvent;
import org.bidib.wizard.simulation.events.FeedbackPortSetStatusEvent;
import org.bidib.wizard.simulation.events.FeedbackPortStatusEvent;
import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventSubscriber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@BidibNodeSimulator(vid = "13", pid = "134")
public class GBM16TSSimulator extends DefaultNodeSimulator {
    private static final Logger LOGGER = LoggerFactory.getLogger(GBM16TSSimulator.class);

    private static final String SIMULATION_PANEL_CLASS =
        "org.bidib.wizard.simulation.client.view.panel.GBMboostMasterPanel";

    private final Map feedbackPorts = new HashMap();

    private final AtomicBoolean statusFreeze = new AtomicBoolean();

    private final AtomicBoolean statusValid = new AtomicBoolean();

    private final AtomicBoolean statusSignal = new AtomicBoolean();

    protected static final int MAX_NUM_OF_FEEDBACK_PORTS = 16;

    private boolean startFeedbackWorker = true;

    protected final ScheduledExecutorService feedbackAddrWorker;

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

        // c2000d8600d3ea --> 40000d8600d3ea
        this.uniqueId = (uniqueId & 0x00FFFFFFFFFFFFL) | (0x40L << (12 * 4));
        LOGGER.info("Provided uniqueId: {}", this.uniqueId);

        final ThreadFactory namedThreadFactory =
            new ThreadFactoryBuilder().setNameFormat("feedbackAddrWorkers-thread-%d").build();
        feedbackAddrWorker = Executors.newScheduledThreadPool(1, namedThreadFactory);
    }

    @Override
    protected void prepareFeatures() {
        super.prepareFeatures();

        features.add(new Feature(BidibLibrary.FEATURE_BM_SIZE, MAX_NUM_OF_FEEDBACK_PORTS));
        features.add(new Feature(BidibLibrary.FEATURE_BM_ON, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_SECACK_AVAILABLE, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_SECACK_ON, 100));
        features.add(new Feature(BidibLibrary.FEATURE_BM_ADDR_DETECT_AVAILABLE, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_ADDR_DETECT_ON, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_ADDR_AND_DIR, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_ISTSPEED_AVAILABLE, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_ISTSPEED_INTERVAL, 100));
        features.add(new Feature(BidibLibrary.FEATURE_BM_CV_AVAILABLE, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_CV_ON, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_DYN_STATE_INTERVAL, 5));
        features.add(new Feature(BidibLibrary.FEATURE_BM_EXT_AVAILABLE, 1));
        features.add(new Feature(BidibLibrary.FEATURE_BM_TIMESTAMP_ON, 1));

        features.add(new Feature(BidibLibrary.FEATURE_STRING_SIZE, 24));

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

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

    @Override
    protected void prepareCVs() {
        super.prepareCVs();

        configurationVariables.put("1", "1");
        configurationVariables.put("7", "27");
        configurationVariables.put("9", "128");

        configurationVariables.put("36", "1");
        configurationVariables.put("37", "5");
        configurationVariables.put("38", "5");
        configurationVariables.put("39", "2");
        configurationVariables.put("40", "16");
        configurationVariables.put("42", "5");
        configurationVariables.put("43", "15");
        configurationVariables.put("44", "16");
        configurationVariables.put("45", "8");
        configurationVariables.put("46", "3");
        configurationVariables.put("47", "1");
        configurationVariables.put("48", "0");
        configurationVariables.put("49", "2");
        configurationVariables.put("50", "0");
        configurationVariables.put("51", "3");
        configurationVariables.put("52", "87");
        configurationVariables.put("53", "232");
        configurationVariables.put("54", "3");
        configurationVariables.put("55", "2");
        configurationVariables.put("56", "0");
        configurationVariables.put("57", "4");
        configurationVariables.put("58", "0");
        configurationVariables.put("59", "255");
        configurationVariables.put("60", "255");
        configurationVariables.put("61", "0");
        configurationVariables.put("62", "255");
        configurationVariables.put("63", "255");
        configurationVariables.put("64", "0");
        configurationVariables.put("65", "0");
        configurationVariables.put("66", "0");
        configurationVariables.put("70", "85");
        configurationVariables.put("104", "1");
        configurationVariables.put("105", "13");
        configurationVariables.put("106", "100");
        configurationVariables.put("107", "0");
        configurationVariables.put("108", "2");
        configurationVariables.put("109", "7");
        configurationVariables.put("110", "2");
        configurationVariables.put("136", "0");
        configurationVariables.put("137", "8");
        configurationVariables.put("138", "4");

    }

    @Override
    public String getSimulationPanelClass() {
        return SIMULATION_PANEL_CLASS;
    }

    private int interval = 3000;

    private int currentLocoPosition = -1;

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

        AnnotationProcessor.process(this);

        // prepare the feedback ports
        setupFeedbackPorts();

        super.start();

        if (isFeedbackChangesEnabled() && startFeedbackWorker) {
            feedbackAddrWorker.scheduleWithFixedDelay(() -> {
                try {

                    int portNum = currentLocoPosition;
                    LOGGER.info("Trigger feedback address, portNum: {}", portNum);

                    FeedbackPortSetStatusEvent evtFree = null;
                    if (portNum > -1) {
                        portNum = currentLocoPosition;
                        if (portNum < 0) {
                            portNum = MAX_NUM_OF_FEEDBACK_PORTS - 1;
                        }
                        // make the active position occupied
                        evtFree = new FeedbackPortSetStatusEvent(getAddress(), portNum, FeedbackPortStatus.FREE);
                    }

                    currentLocoPosition++;
                    if (currentLocoPosition >= MAX_NUM_OF_FEEDBACK_PORTS) {
                        currentLocoPosition = 0;
                    }
                    FeedbackPortSetStatusEvent evtOcc =
                        new FeedbackPortSetStatusEvent(getAddress(), currentLocoPosition, FeedbackPortStatus.OCCUPIED);

                    // TODO
                    setFeedbackPortStatus(evtOcc);
                    triggerFeedbackAddressResponse(currentLocoPosition);

                    Thread.sleep(500);

                    if (evtFree != null) {
                        // send the free event
                        setFeedbackPortStatus(evtFree);
                        triggerFeedbackAddressResponse(evtFree.getPortNum());
                    }

                    int delayValue = ThreadLocalRandom.current().nextInt(300, 4000);
                    LOGGER.info("Wait for next execution, delayValue: {}", delayValue);
                    Thread.sleep(delayValue);

                }
                catch (Exception ex) {
                    LOGGER.warn("Trigger the feedback address failed.", ex);
                }
                LOGGER.info("Trigger feedback address has finished.");
            }, 5000, interval, TimeUnit.MILLISECONDS);
        }
        else {
            LOGGER.info("The feedback address publisher is not started.");
        }
    }

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

        if (feedbackAddrWorker != null) {
            LOGGER.info("Stop the booster diag worker.");
            feedbackAddrWorker.shutdownNow();
        }

        super.stop();
    }

    private void setupFeedbackPorts() {
        for (int id = 0; id < MAX_NUM_OF_FEEDBACK_PORTS; id++) {
            FeedbackPort port = new FeedbackPort();

            port.setId(id);
            // port.setStatus(id % 3 == 0 ? FeedbackPortStatus.FREE : FeedbackPortStatus.OCCUPIED);
            port.setStatus(FeedbackPortStatus.FREE);
            feedbackPorts.put(id, port);
        }
    }

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

        byte[] response = null;
        switch (ByteUtils.getInt(bidibMessage.getType())) {
            case BidibLibrary.MSG_BM_GET_RANGE:
                response = processBmGetRangeRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_BM_MIRROR_MULTIPLE:
                processBmMirrorMultipleRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_BM_MIRROR_OCC:
                processBmMirrorOccupiedRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_BM_MIRROR_FREE:
                processBmMirrorFreeRequest(bidibMessage);
                break;

            case BidibLibrary.MSG_BM_ADDR_GET_RANGE:
                processBmAddrGetRangeRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_BM_GET_CONFIDENCE:
                response = processBmGetConfidenceRequest(bidibMessage);
                break;
            default:
                response = super.prepareResponse(bidibMessage);
                break;
        }
        return response;
    }

    protected byte[] processBmGetRangeRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the FeedbackGetRangeMessage: {}", bidibMessage);

        byte[] response = null;
        try {
            FeedbackGetRangeMessage feedbackGetRangeMessage = (FeedbackGetRangeMessage) bidibMessage;
            int baseAddress = feedbackGetRangeMessage.getBeginRange();
            int end = feedbackGetRangeMessage.getEndRange();
            int feedbackSize = feedbackGetRangeMessage.getEndRange() - feedbackGetRangeMessage.getBeginRange();

            byte value = 0x00;
            int index = 0;

            int feedbackByteSize = feedbackSize / 8 + (feedbackSize % 8 > 0 ? 1 : 0);

            byte[] feedbackMultiple = new byte[feedbackByteSize];
            int position = feedbackMultiple.length;

            for (int portNum = end; portNum > baseAddress; portNum--) {
                value = (byte) ((value & 0xFF) << 1);
                FeedbackPort fbp = feedbackPorts.get(portNum - 1);
                byte status = 0;
                if (fbp != null) {
                    status = ByteUtils.getLowByte(fbp.getStatus().getType().getType(), 0x01);
                }
                value |= status;
                feedbackMultiple[position - 1] = value;

                index++;
                if (index > 7) {
                    value = 0;
                    index = 0;
                    position--;
                }
            }

            LOGGER.info("Prepared feedback multiple: {}", ByteUtils.bytesToHex(feedbackMultiple));

            FeedbackMultipleResponse feedbackMultipleResponse =
                new FeedbackMultipleResponse(bidibMessage.getAddr(), getNextSendNum(),
                    ByteUtils.getLowByte(baseAddress), ByteUtils.getLowByte(feedbackSize), feedbackMultiple);
            response = feedbackMultipleResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create feedbackMultiple response failed.", ex);
        }
        return response;
    }

    protected void processBmMirrorMultipleRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the FeedbackMirrorMultipleMessage: {}, do nothing ...", bidibMessage);
    }

    protected void processBmMirrorOccupiedRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the processBmMirrorOccupiedRequest: {}, do nothing ...", bidibMessage);
    }

    protected void processBmMirrorFreeRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Process the processBmMirrorFreeRequest: {}, do nothing ...", bidibMessage);
    }

    protected void processBmAddrGetRangeRequest(BidibMessageInterface bidibMessage) {

        try {
            for (FeedbackPort port : feedbackPorts.values()) {

                int detectorNumber = port.getId();
                List bidibAddresses = new ArrayList<>();
                List addresses = port.getAddresses();
                if (CollectionUtils.isNotEmpty(addresses)) {
                    for (FeedbackAddressData addressData : addresses) {
                        final EnrailmentDirectionEnum enrailmentDirection = addressData.getType();
                        AddressTypeEnum addressType = null;
                        switch (enrailmentDirection) {
                            case LOCOMOTIVE_LEFT:
                            case LOCOMOTIVE_RIGHT:
                                addressType = AddressTypeEnum.LOCOMOTIVE_FORWARD;
                                break;
                            case BASIC_ACCESSORY:
                                addressType = AddressTypeEnum.ACCESSORY;
                                break;
                            case EXTENDED_ACCESSORY:
                                addressType = AddressTypeEnum.EXTENDED_ACCESSORY;
                                break;
                            default:
                                break;
                        }
                        AddressData bidibAddress = new AddressData(addressData.getAddress(), addressType);
                        bidibAddresses.add(bidibAddress);
                    }
                }
                FeedbackAddressResponse feedbackAddressResponse =
                    new FeedbackAddressResponse(bidibMessage.getAddr(), getNextSendNum(), detectorNumber,
                        bidibAddresses);
                byte[] response = feedbackAddressResponse.getContent();
                LOGGER.info("Prepare feedbackAddressResponse: {}", ByteUtils.bytesToHex(response));
                sendSpontanousResponse(response);
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create feedbackAddress response failed.", ex);
        }
    }

    protected byte[] processBmGetConfidenceRequest(BidibMessageInterface bidibMessage) {
        byte[] response = null;
        try {

            byte valid = (byte) (statusValid.get() ? 1 : 0);
            byte freeze = (byte) (statusFreeze.get() ? 1 : 0);
            byte signal = (byte) (statusSignal.get() ? 1 : 0);

            // TODO if more than a single GBM16T is attached we must set more bits? See 4.7.4. Uplink: Nachrichten für
            // Belegtmelder --> MSG_BM_CONFIDENCE
            // Test with real system: See MainMessageListener.confidence()

            FeedbackConfidenceResponse feedbackConfidenceResponse =
                new FeedbackConfidenceResponse(bidibMessage.getAddr(), getNextSendNum(), valid, freeze, signal);
            response = feedbackConfidenceResponse.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create feedbackConfidence response failed.", ex);
        }
        return response;
    }

    private void publishFeedbackPortChange(Port port) {
        FeedbackPort feedbackPort = (FeedbackPort) port;
        FeedbackPortStatus status = feedbackPort.getStatus();

        LOGGER.info("The feedbackport status has changed, notify the listeners, nodeAddress: {}", nodeAddress);
        EventBus.publish(new FeedbackPortStatusEvent(NodeUtils.formatAddress(nodeAddress), port.getId(), status));
    }

    @Override
    public void queryStatus(Class portClass) {
        if (FeedbackPort.class.equals(portClass)) {
            for (FeedbackPort feedbackPort : feedbackPorts.values()) {
                publishFeedbackPortChange(feedbackPort);
            }

            // publish the confidence
            publishFeedbackConfidenceStatusEvent(statusValid.get(), statusFreeze.get(), statusSignal.get());
        }
    }

    private void triggerFeedbackAddressResponse(int portNum) {
        LOGGER.info("Trigger the feedback address repsonse, portNum: {}", portNum);
        byte[] response = null;

        List addresses = new LinkedList();

        byte lowByte = 3;
        byte highByte = ByteUtils.getLowByte(0x80);
        int address = ByteUtils.getWord(lowByte, (byte) (highByte & 0x3F));

        AddressData addressDataIn = new AddressData(address, AddressTypeEnum.valueOf((byte) ((highByte & 0xC0) >> 6)));
        addresses.add(new FeedbackAddressData(addressDataIn.getAddress(), EnrailmentDirectionEnum.LOCOMOTIVE_RIGHT));
        // }

        try {
            FeedbackPort feedbackPort = feedbackPorts.get(portNum);
            int detectorNumber = feedbackPort.getId();
            List bidibAddresses = new ArrayList<>();

            if (FeedbackPortStatus.OCCUPIED == feedbackPort.getStatus()) {
                // port is occupied -> add the addresses
                if (CollectionUtils.isNotEmpty(addresses)) {
                    for (FeedbackAddressData addressData : addresses) {
                        final EnrailmentDirectionEnum enrailmentDirection = addressData.getType();
                        AddressTypeEnum addressType = null;
                        switch (enrailmentDirection) {
                            case LOCOMOTIVE_LEFT:
                            case LOCOMOTIVE_RIGHT:
                                addressType = AddressTypeEnum.LOCOMOTIVE_FORWARD;
                                break;
                            case BASIC_ACCESSORY:
                                addressType = AddressTypeEnum.ACCESSORY;
                                break;
                            case EXTENDED_ACCESSORY:
                                addressType = AddressTypeEnum.EXTENDED_ACCESSORY;
                                break;
                            default:
                                break;
                        }
                        AddressData bidibAddress = new AddressData(addressData.getAddress(), addressType);
                        bidibAddresses.add(bidibAddress);
                    }
                }
            }
            FeedbackAddressResponse feedbackAddressResponse =
                new FeedbackAddressResponse(nodeAddress, getNextSendNum(), detectorNumber, bidibAddresses);

            response = feedbackAddressResponse.getContent();
            sendSpontanousResponse(response);

            if (FeedbackPortStatus.OCCUPIED == feedbackPort.getStatus()) {
                int speedValue = ThreadLocalRandom.current().nextInt(2, 100);

                FeedbackSpeedResponse feedbackSpeedResponse =
                    new FeedbackSpeedResponse(nodeAddress, getNextSendNum(), address, speedValue);
                sendSpontanousResponse(feedbackSpeedResponse.getContent());
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create FeedbackAddressResponse failed.", ex);
        }
    }

    @EventSubscriber(eventClass = FeedbackConfidenceSetEvent.class)
    public void feedbackConfidenceSetEvent(FeedbackConfidenceSetEvent feedbackConfidenceEvent) {
        String nodeAddress = feedbackConfidenceEvent.getNodeAddr();
        LOGGER.info("The change of the feedback confidence was requested, nodeAddress: {}", nodeAddress);

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

        statusValid.set(feedbackConfidenceEvent.getValid());
        statusFreeze.set(feedbackConfidenceEvent.getFreeze());
        statusSignal.set(feedbackConfidenceEvent.getSignal());

        byte valid = (byte) (statusValid.get() ? 1 : 0);
        byte freeze = (byte) (statusFreeze.get() ? 1 : 0);
        byte signal = (byte) (statusSignal.get() ? 1 : 0);

        try {
            FeedbackConfidenceResponse feedbackConfidenceResponse =
                new FeedbackConfidenceResponse(this.nodeAddress, getNextSendNum(), valid, freeze, signal);
            LOGGER.info("Prepared feedbackConfidenceResponse: {}", feedbackConfidenceResponse);
            sendSpontanousResponse(feedbackConfidenceResponse.getContent());
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Send feedbackConfidenceResponse failed.", ex);
        }

        publishFeedbackConfidenceStatusEvent(statusValid.get(), statusFreeze.get(), statusSignal.get());
    }

    private void publishFeedbackConfidenceStatusEvent(boolean valid, boolean freeze, boolean signal) {

        LOGGER
            .info("The feedbackport confidence status has changed, notify the listeners, nodeAddress: {}", nodeAddress);
        EventBus
            .publish(new FeedbackConfidenceStatusEvent(NodeUtils.formatAddress(nodeAddress), valid, freeze, signal));
    }

    @EventSubscriber(eventClass = FeedbackPortSetStatusEvent.class)
    public void feedbackPortSetStatus(FeedbackPortSetStatusEvent setStatusEvent) {
        LOGGER.info("The change of the feedback 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();
        try {
            invertFeedbackPortStatus(portNum);
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Publish feedback status failed.", ex);
        }
    }

    private int timestamp;

    private int getTimestamp() {
        timestamp += 10;
        if (timestamp > 65000) {
            timestamp = 0;
        }
        return timestamp;
    }

    private boolean hasTimestampFeature() {
        Feature feature = Feature.findFeature(features, BidibLibrary.FEATURE_BM_TIMESTAMP_ON);

        return (feature != null && feature.getValue() > 0);
    }

    private int decoderAddress = 10;

    private int getDecoderAddress() {
        decoderAddress++;

        if (decoderAddress > 15) {
            decoderAddress = 10;
        }

        return decoderAddress;
    }

    private int locationAddress = 120;

    private int getLocationAddress() {
        locationAddress++;

        if (locationAddress > 150) {
            locationAddress = 120;
        }

        return locationAddress;
    }

    protected void invertFeedbackPortStatus(int portNum) throws ProtocolException {

        FeedbackPort port = feedbackPorts.get(portNum);
        if (port != null) {
            BidibMessage response = null;

            switch (port.getStatus()) {
                case FREE:
                    port.setStatus(FeedbackPortStatus.OCCUPIED);
                    if (hasTimestampFeature()) {
                        response =
                            new FeedbackOccupiedResponse(getNodeAddress(), getNextSendNum(), portNum, getTimestamp());
                    }
                    else {
                        response = new FeedbackOccupiedResponse(getNodeAddress(), getNextSendNum(), portNum);
                    }
                    break;
                default:
                    port.setStatus(FeedbackPortStatus.FREE);

                    response = new FeedbackFreeResponse(getNodeAddress(), getNextSendNum(), portNum);
                    break;
            }

            LOGGER.info("Prepared the FeedbackFreeResponse: {}", response);
            sendSpontanousResponse(response.getContent());

            publishFeedbackPortChange(port);
        }
        else {
            LOGGER.warn("The requested feedback port is not available: {}", portNum);
        }
    }

    protected void setFeedbackPortStatus(FeedbackPortSetStatusEvent statusEvent) throws ProtocolException {

        int portNum = statusEvent.getPortNum();
        FeedbackPort port = feedbackPorts.get(portNum);
        if (port != null) {
            BidibMessage response = null;

            switch (statusEvent.getStatus()) {
                case OCCUPIED:
                    port.setStatus(FeedbackPortStatus.OCCUPIED);
                    if (hasTimestampFeature()) {
                        response =
                            new FeedbackOccupiedResponse(getNodeAddress(), getNextSendNum(), portNum, getTimestamp());
                    }
                    else {
                        response = new FeedbackOccupiedResponse(getNodeAddress(), getNextSendNum(), portNum);
                    }

                    break;
                default:
                    port.setStatus(FeedbackPortStatus.FREE);

                    response = new FeedbackFreeResponse(getNodeAddress(), getNextSendNum(), portNum);
                    break;
            }

            LOGGER.info("Prepared the FeedbackFreeResponse: {}", response);
            sendSpontanousResponse(response.getContent());

            publishFeedbackPortChange(port);
        }
        else {
            LOGGER.warn("The requested feedback port is not available: {}", portNum);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy