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

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

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

import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.enums.FirmwareUpdateOperation;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.jbidibc.messages.message.FwUpdateOpMessage;
import org.bidib.jbidibc.messages.message.FwUpdateStatResponse;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.simulation.InterfaceNode;
import org.bidib.jbidibc.simulation.SimulationBidibMessageProcessor;
import org.bidib.jbidibc.simulation.SimulatorNode;
import org.bidib.jbidibc.simulation.SimulatorRegistry;
import org.bidib.jbidibc.simulation.events.NodeLostEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FirmwareErrorSimulator extends LightControlSimulator implements InterfaceNode {
    private static final Logger LOGGER = LoggerFactory.getLogger(FirmwareErrorSimulator.class);

    private SimulatorRegistry simulatorRegistry;

    private int dataBlocksReceived;

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

    @Override
    public void setSimulatorRegistry(final SimulatorRegistry simulatorRegistry) {
        this.simulatorRegistry = simulatorRegistry;
    }

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

        byte[] response = null;
        try {
            FwUpdateOpMessage fwUpdateOpMessage = (FwUpdateOpMessage) bidibMessage;
            FirmwareUpdateOperation operation = fwUpdateOpMessage.getOperation();

            LOGGER.info("processFwUpdateOpRequest, operation: {}", operation);

            int timeout = 0;
            int status = BidibLibrary.BIDIB_MSG_FW_UPDATE_STAT_ERROR;
            switch (operation) {
                case ENTER:
                    status = BidibLibrary.BIDIB_MSG_FW_UPDATE_STAT_READY;
                    LOGGER.warn("Send no answer on ENTER request. Send NODE_LOST instead.");
                    // get the master node
                    // SimulatorNode master = simulatorRegistry.getSimulator("0");

                    // if (master instanceof GBMboostMasterSimulator) {
                    // NodeLostEvent nodeLostEvent = new NodeLostEvent(getNodeAddress(), getUniqueId());
                    // ((GBMboostMasterSimulator) master).nodeLostEvent(nodeLostEvent);
                    // return response;
                    // }
                    break;
                case DATA:
                    status = BidibLibrary.BIDIB_MSG_FW_UPDATE_STAT_DATA;
                    dataBlocksReceived++;

                    if (dataBlocksReceived > 0 && dataBlocksReceived < 200 && dataBlocksReceived % 15 == 0) {
                        LOGGER.warn("Return a timeout > 0");
                        timeout = 30;
                    }
                    break;
                case SETDEST:
                    status = BidibLibrary.BIDIB_MSG_FW_UPDATE_STAT_DATA;
                    break;
                case EXIT:
                    status = BidibLibrary.BIDIB_MSG_FW_UPDATE_STAT_EXIT;
                    LOGGER.warn("Send no answer on EXIT request.");
                    // return response;
                    break;
                default:
                    break;
            }
            FwUpdateStatResponse fwUpdateStatResponse =
                new FwUpdateStatResponse(bidibMessage.getAddr(), getNextSendNum(), ByteUtils.getLowByte(status),
                    ByteUtils.getLowByte(timeout));
            response = fwUpdateStatResponse.getContent();

            if (operation == FirmwareUpdateOperation.EXIT) {
                // get the master node
                SimulatorNode master = simulatorRegistry.getSimulator("0");

                if (master instanceof GBMboostMasterSimulator) {
                    NodeLostEvent nodeLostEvent = new NodeLostEvent(getNodeAddress(), getUniqueId());
                    ((GBMboostMasterSimulator) master).nodeLostEvent(nodeLostEvent);
                    return response;
                }
            }

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

    @Override
    public void addSubNode(SimulatorNode simulator) {
        // not implemented

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy