001package jmri.jmrix.can.adapters.gridconnect.canrs;
002
003import jmri.jmrix.AbstractMRMessage;
004import jmri.jmrix.AbstractMRReply;
005import jmri.jmrix.can.CanMessage;
006import jmri.jmrix.can.CanReply;
007import jmri.jmrix.can.adapters.gridconnect.GcTrafficController;
008import jmri.jmrix.can.cbus.CbusConstants;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Traffic controller for the MERG variant of the GridConnect protocol.
014 * <p>
015 * MERG CAN-RS/CAN-USB uses messages transmitted as an ASCII string of up to 24
016 * characters of the form: :ShhhhNd0d1d2d3d4d5d6d7; The S indicates a standard
017 * CAN frame hhhh is the two byte header (11 useful bits), left justified on
018 * send to adapter N or R indicates a normal or remote frame d0 - d7 are the (up
019 * to) 8 data bytes
020 *
021 * @author Andrew Crosland Copyright (C) 2008
022 */
023public class MergTrafficController extends GcTrafficController {
024
025    public MergTrafficController() {
026        super();
027        super.setCanId(CbusConstants.DEFAULT_STANDARD_ID);
028    }
029
030    // New message for hardware protocol
031    @Override
032    protected AbstractMRMessage newMessage() {
033        log.debug("New MergMessage created");
034        return new MergMessage();
035    }
036
037    /**
038     * Make a CanReply from a MergReply reply.
039     * {@inheritDoc}
040     */
041    @Override
042    public CanReply decodeFromHardware(AbstractMRReply m) {
043        log.debug("Decoding from hardware");
044        MergReply gc = new MergReply();
045        try {
046            gc = (MergReply) m;
047        } catch(java.lang.ClassCastException cce){
048            log.error("{} is not a MergReply",m);
049        }
050        CanReply ret = gc.createReply();
051        log.debug("CanReply created {} from MergReply {}", ret, gc);
052        return ret;
053    }
054
055    /**
056     * Encode a CanMessage for the hardware.
057     * {@inheritDoc}
058     */
059    @Override
060    public AbstractMRMessage encodeForHardware(CanMessage m) {
061        //log.debug("Encoding for hardware");
062        return new MergMessage(m);
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    protected AbstractMRReply newReply() {
070        log.debug("New MergReply created");
071        return new MergReply();
072    }
073
074    private final static Logger log = LoggerFactory.getLogger(MergTrafficController.class);
075
076}