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