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

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

package org.bidib.wizard.simulation;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.VendorData;
import org.bidib.jbidibc.messages.enums.BoosterState;
import org.bidib.jbidibc.messages.enums.CommandStationState;
import org.bidib.jbidibc.messages.enums.MessageClassEnum;
import org.bidib.jbidibc.messages.enums.TargetModeEnum;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.BidibCommand;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.jbidibc.messages.message.GuestRequestSendMessage;
import org.bidib.jbidibc.messages.message.GuestRequestSubscribeMessage;
import org.bidib.jbidibc.messages.message.VendorGetMessage;
import org.bidib.jbidibc.messages.message.VendorResponse;
import org.bidib.jbidibc.messages.message.VendorSetMessage;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.simulation.SimulationBidibMessageProcessor;
import org.bidib.jbidibc.simulation.annotation.BidibNodeSimulator;
import org.bidib.jbidibc.simulation.events.GuestRequestSendAccessoryAspectEvent;
import org.bidib.jbidibc.simulation.events.GuestRequestSendBoosterSetStateEvent;
import org.bidib.jbidibc.simulation.events.GuestRequestSendCsSetStateEvent;
import org.bidib.jbidibc.simulation.events.GuestRequestSendNodeTabGetAllEvent;
import org.bidib.jbidibc.simulation.events.GuestRequestSendNodeTabGetNextEvent;
import org.bidib.jbidibc.simulation.events.GuestRequestSendSysGetMagicEvent;
import org.bidib.jbidibc.simulation.events.GuestSubscribeEvent;
import org.bidib.jbidibc.simulation.nodes.DefaultNodeSimulator;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventSubscriber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@BidibNodeSimulator(vid = "251", pid = "305")
public class ReadyXPSimulator extends DefaultNodeSimulator {

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

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

    protected final ScheduledExecutorService simulationWorker = Executors.newScheduledThreadPool(1);

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

        // set the guest magic
        setNodeMagic(0xC0DE);
    }

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

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

    }

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

        AnnotationProcessor.process(this);

        super.start();
    }

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

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

        byte[] response = null;
        switch (ByteUtils.getInt(bidibMessage.getType())) {
            case BidibLibrary.MSG_GUEST_RESP_SENT:
                response = processGuestRespSentRequest(bidibMessage);
                break;
            case BidibLibrary.MSG_GUEST_RESP_NOTIFY:
                response = processGuestRespNotifyRequest(bidibMessage);
                break;
            default:
                response = super.prepareResponse(bidibMessage);
                break;
        }
        return response;
    }

    protected byte[] processGuestRespSentRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Received GuestRespSent message: {}", bidibMessage);

        return null;
    }

    protected byte[] processGuestRespNotifyRequest(BidibMessageInterface bidibMessage) {
        LOGGER.info("Received GuestRespNotify message: {}", bidibMessage);

        return null;
    }

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

        byte[] response = null;
        try {
            VendorSetMessage vendorSetMessage = (VendorSetMessage) bidibMessage;
            VendorData vendorData = vendorSetMessage.getVendorData();
            LOGGER.info("Set the vendor data: {}", vendorData);
            String vendorDataName = vendorData.getName();
            String vendorDataValue = vendorData.getValue();

            if (vendorDataName.toLowerCase().startsWith("ame ")) {
                // handle the aspect mapping
                LOGGER.info("Handle the aspect mapping, name: {}, value: {}", vendorDataName, vendorDataValue);

                String result = vendorData.getValue();
                if ("".equals(result) && "AME 0d7f002200 18 2 12 1".equals(vendorDataName)) {
                    // delete operation
                    LOGGER.warn("Force test invalid result for delete of cv name: {}", vendorDataName);
                    result = "1";
                }
                else if ("1".equals(result) && "AME 0d7f002200 17 2 12 1".equals(vendorDataName)) {
                    // write operation
                    LOGGER.warn("Force test invalid result for write of cv name: {}", vendorDataName);
                    result = "";
                }

                VendorResponse vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorData.getName(), result);
                response = vendorResponse.getContent();
            }
            else {
                // store the vendor data ...
                configurationVariables.put(vendorData.getName(), vendorData.getValue());

                VendorResponse vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorData.getName(),
                        vendorData.getValue());
                response = vendorResponse.getContent();
            }

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

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

        byte[] response = null;
        try {
            VendorGetMessage vendorGetMessage = (VendorGetMessage) bidibMessage;
            String vendorDataName = vendorGetMessage.getVendorDataName();
            LOGGER.info("Get the vendor data with name: {}", vendorDataName);

            if (vendorDataName.toLowerCase().equals("amr *")) {
                // handle the aspect mapping
                LOGGER.info("Handle the aspect mapping.");

                String vendorDataValue = "4"; // number of entries

                // return number of entries
                VendorResponse vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
                response = vendorResponse.getContent();
                sendSpontanousResponse(response);

                // first entry
                vendorDataName = "AME 0D7F002200 18 1 12 0";
                vendorDataValue = "1";
                vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
                response = vendorResponse.getContent();
                sendSpontanousResponse(response);

                // second entry
                vendorDataName = "AME 0D7F002200 18 2 12 1";
                vendorDataValue = "1";
                vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
                response = vendorResponse.getContent();
                sendSpontanousResponse(response);

                // 3rd entry
                vendorDataName = "AME 0D7F002200 17 1 11 1";
                vendorDataValue = "1";
                vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
                response = vendorResponse.getContent();
                sendSpontanousResponse(response);

                // 4th entry
                vendorDataName = "AME 0D7F002200 17 2 11 0";
                vendorDataValue = "1";
                vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
                response = vendorResponse.getContent();
                sendSpontanousResponse(response);

                response = null;
            }
            else {
                // fetch the value from the stored data ...
                String vendorDataValue = configurationVariables.get(vendorDataName);
                if (StringUtils.isBlank(vendorDataValue)) {
                    vendorDataValue = "";
                }

                VendorResponse vendorResponse =
                    new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
                response = vendorResponse.getContent();
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create vendor response failed.", ex);
        }
        return response;
    }

    @EventSubscriber(eventClass = GuestSubscribeEvent.class)
    public void guestSubscribeEvent(GuestSubscribeEvent guestSubscribeEvent) {
        LOGGER.info("Received the guestSubscribeEvent: {}", guestSubscribeEvent);

        byte[] response = null;
        try {

            int classValue = MessageClassEnum.classValue(guestSubscribeEvent.getMessageClass());
            // use same value for up- and downstream
            int subscription = ByteUtils.getInt(ByteUtils.getLowByte(classValue), ByteUtils.getLowByte(classValue));

            TargetModeEnum targetMode = TargetModeEnum.BIDIB_TARGET_MODE_TOP;

            switch (guestSubscribeEvent.getMessageClass()) {
                case BIDIB_MESSAGE_CLASS_COMMANDSTATION:
                    targetMode = TargetModeEnum.BIDIB_TARGET_MODE_DCCGEN;
                    break;
                case BIDIB_MESSAGE_CLASS_ACCESSORY:
                    targetMode = TargetModeEnum.BIDIB_TARGET_MODE_ACCESSORY;
                    break;
                case BIDIB_MESSAGE_CLASS_BOOSTER:
                    targetMode = TargetModeEnum.BIDIB_TARGET_MODE_BOOSTER;
                    break;
                default:
                    break;
            }

            GuestRequestSubscribeMessage subscribeMessage = new GuestRequestSubscribeMessage(targetMode, subscription);
            subscribeMessage.setAddr(nodeAddress);
            response = subscribeMessage.getContent();
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create GuestRequestSubscribeMessage response failed.", ex);
        }
        sendSpontanousResponse(response);
    }

    @EventSubscriber(eventClass = GuestRequestSendCsSetStateEvent.class)
    public void guestSendEvent(GuestRequestSendCsSetStateEvent guestRequestSendEvent) {
        LOGGER.info("Received the guestRequestSendEvent: {}", guestRequestSendEvent);

        byte[] response = null;
        try {

            // final byte[] targetNodeAddress = guestRequestSendEvent.getTargetNodeAddr();
            final CommandStationState csState = guestRequestSendEvent.getState();

            BidibRequestFactory requestFactory = new BidibRequestFactory();
            BidibCommand csDriveMessage = requestFactory.createCommandStationSetState(csState);

            GuestRequestSendMessage guestRequestSendMessage =
                new GuestRequestSendMessage(TargetModeEnum.BIDIB_TARGET_MODE_DCCGEN,
                    csDriveMessage.getMessageContent());
            guestRequestSendMessage.setAddr(nodeAddress);
            response = guestRequestSendMessage.getContent();

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

    @EventSubscriber(eventClass = GuestRequestSendBoosterSetStateEvent.class)
    public void guestSendEvent(GuestRequestSendBoosterSetStateEvent guestRequestSendEvent) {
        LOGGER.info("Received the guestRequestSendEvent: {}", guestRequestSendEvent);

        byte[] response = null;
        try {

            final BoosterState csState = guestRequestSendEvent.getState();

            BidibRequestFactory requestFactory = new BidibRequestFactory();
            BidibCommand csDriveMessage = requestFactory.createBoosterSetState(csState, false);

            GuestRequestSendMessage guestRequestSendMessage =
                new GuestRequestSendMessage(TargetModeEnum.BIDIB_TARGET_MODE_BOOSTER,
                    csDriveMessage.getMessageContent());
            guestRequestSendMessage.setAddr(nodeAddress);
            response = guestRequestSendMessage.getContent();

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

    @EventSubscriber(eventClass = GuestRequestSendAccessoryAspectEvent.class)
    public void guestSendEvent(GuestRequestSendAccessoryAspectEvent guestRequestSendEvent) {
        LOGGER.info("Received the guestRequestSendEvent: {}", guestRequestSendEvent);

        byte[] response = null;
        try {

            // final byte[] targetNodeAddress = guestRequestSendEvent.getTargetNodeAddr();
            final int accessoryNumber = guestRequestSendEvent.getAccessoryNumber();
            final int aspectNumber = guestRequestSendEvent.getAspectNumber();

            BidibRequestFactory requestFactory = new BidibRequestFactory();
            BidibCommand accessorySetMessage = requestFactory.createAccessorySet(accessoryNumber, aspectNumber);

            // TODO change the target node address
            GuestRequestSendMessage guestRequestSendMessage =
                new GuestRequestSendMessage(TargetModeEnum.BIDIB_TARGET_MODE_ACCESSORY,
                    accessorySetMessage.getMessageContent());
            guestRequestSendMessage.setAddr(nodeAddress);
            response = guestRequestSendMessage.getContent();

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

    @EventSubscriber(eventClass = GuestRequestSendSysGetMagicEvent.class)
    public void guestSendEvent(GuestRequestSendSysGetMagicEvent guestRequestSendEvent) {
        LOGGER.info("Received the guestRequestSendEvent: {}", guestRequestSendEvent);

        byte[] response = null;
        try {

            final byte[] targetNodeAddress = guestRequestSendEvent.getTargetNodeAddr();

            BidibRequestFactory requestFactory = new BidibRequestFactory();
            BidibCommand sysGetMagicMessage = requestFactory.createSysGetMagic();

            GuestRequestSendMessage guestRequestSendMessage =
                new GuestRequestSendMessage(TargetModeEnum.BIDIB_TARGET_MODE_TOP /* 0x0F */,
                    sysGetMagicMessage.getMessageContent());
            guestRequestSendMessage.setAddr(nodeAddress);
            response = guestRequestSendMessage.getContent();

            LOGGER.info("Prepared response content: {}", ByteUtils.bytesToHex(response));
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Create GuestRequestSendMessage response failed.", ex);
        }
        sendSpontanousResponse(response);
    }

    @EventSubscriber(eventClass = GuestRequestSendNodeTabGetAllEvent.class)
    public void guestSendEvent(GuestRequestSendNodeTabGetAllEvent guestRequestSendEvent) {
        LOGGER.info("Received the guestRequestSendEvent: {}", guestRequestSendEvent);

        byte[] response = null;
        try {

            // final byte[] targetNodeAddress = guestRequestSendEvent.getTargetNodeAddr();

            BidibRequestFactory requestFactory = new BidibRequestFactory();
            BidibCommand nodeTabGetAllMessage = requestFactory.createNodeTabGetAll();

            GuestRequestSendMessage guestRequestSendMessage =
                new GuestRequestSendMessage(TargetModeEnum.BIDIB_TARGET_MODE_TOP,
                    nodeTabGetAllMessage.getMessageContent());
            guestRequestSendMessage.setAddr(nodeAddress);
            response = guestRequestSendMessage.getContent();

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

    @EventSubscriber(eventClass = GuestRequestSendNodeTabGetNextEvent.class)
    public void guestSendEvent(GuestRequestSendNodeTabGetNextEvent guestRequestSendEvent) {
        LOGGER.info("Received the guestRequestSendEvent: {}", guestRequestSendEvent);

        byte[] response = null;
        try {

            // final byte[] targetNodeAddress = guestRequestSendEvent.getTargetNodeAddr();

            BidibRequestFactory requestFactory = new BidibRequestFactory();
            BidibCommand nodeTabGetAllMessage = requestFactory.createNodeTabGetNext();

            GuestRequestSendMessage guestRequestSendMessage =
                new GuestRequestSendMessage(TargetModeEnum.BIDIB_TARGET_MODE_TOP,
                    nodeTabGetAllMessage.getMessageContent());
            guestRequestSendMessage.setAddr(nodeAddress);
            response = guestRequestSendMessage.getContent();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy