001package jmri.implementation;
002
003import jmri.ConsistListener;
004import jmri.DccLocoAddress;
005import jmri.InstanceManager;
006import jmri.CommandStation;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * This is the Default DCC consist manager installed on systems which support
012 * the command station interface. It uses the NMRA consist creation packet
013 * instead of Operations Mode programming to build a consist, but otherwise is
014 * derived from the DccConsist code.
015 *
016 * @author Paul Bender Copyright (C) 2011
017 */
018public class NmraConsist extends DccConsist {
019        
020    private CommandStation commandStation = null;
021
022// Initialize a consist for the specific address.
023    public NmraConsist(int address) {
024        super(address);
025        log.debug("Nmra Consist created for address: {}", address);
026    }
027
028    public NmraConsist(DccLocoAddress address) {
029        this(address,InstanceManager.getDefault(CommandStation.class));
030    }
031 
032    public NmraConsist(DccLocoAddress address,CommandStation cs){
033        super(address);
034        commandStation = cs;
035        log.debug("Nmra Consist created for address: {}", address.toString());
036    }
037
038    /*
039     *  Add a Locomotive to an Advanced Consist
040     *  @param address is the Locomotive address to add to the locomotive
041     *  @param directionNormal is True if the locomotive is traveling
042     *        the same direction as the consist, or false otherwise.
043     */
044    @Override
045    protected void addToAdvancedConsist(DccLocoAddress LocoAddress, boolean directionNormal) {
046        if (log.isDebugEnabled()) {
047            log.debug("Add Locomotive {} to advanced consist {} With Direction Normal {}.",
048                    LocoAddress.toString(),
049                    consistAddress.toString(),
050                    directionNormal);
051        }
052        // create the message and fill it,
053        byte[] contents = jmri.NmraPacket.consistControl(LocoAddress.getNumber(),
054                LocoAddress.isLongAddress(),
055                consistAddress.getNumber(),
056                directionNormal);
057        commandStation.sendPacket(contents, 4);
058        notifyConsistListeners(LocoAddress, ConsistListener.OPERATION_SUCCESS);
059
060    }
061
062    /*
063     *  Remove a Locomotive from an Advanced Consist
064     *  @param address is the Locomotive address to add to the locomotive
065     */
066    @Override
067    protected void removeFromAdvancedConsist(DccLocoAddress LocoAddress) {
068        if (log.isDebugEnabled()) {
069            log.debug("Remove Locomotive {} from advanced consist {}.",
070                    LocoAddress.toString(),
071                    consistAddress.toString());
072        }
073        // create the message and fill it,
074        byte[] contents = jmri.NmraPacket.consistControl(LocoAddress.getNumber(),
075                LocoAddress.isLongAddress(),
076                0, //set to 0 to remove
077                true);//always normal direction
078        commandStation.sendPacket(contents, 4);
079        notifyConsistListeners(LocoAddress, ConsistListener.OPERATION_SUCCESS);
080    }
081    private final static Logger log = LoggerFactory.getLogger(NmraConsist.class);
082}