001package jmri.jmrit.withrottle;
002
003import jmri.DccLocoAddress;
004import jmri.implementation.NmraConsist;
005
006/**
007 * @author Brett Hoffman Copyright (C) 2010, 2011
008 * 
009 */
010public class WiFiConsist extends NmraConsist {
011
012    public WiFiConsist(DccLocoAddress address) {
013        super(address);
014    }
015
016    @Override
017    public void add(DccLocoAddress loco, boolean dirNorm) {
018        restore(loco, dirNorm);
019        sendConsistCommand(loco, dirNorm, this);
020        //set the value in the roster entry for CV19
021        setRosterEntryCVValue(loco);
022    }
023
024    @Override
025    public void remove(DccLocoAddress loco) {
026        //reset the value in the roster entry for CV19
027        resetRosterEntryCVValue(loco);
028        // then remove the address from all the internal lists.
029        consistDir.remove(loco);
030        consistList.remove(loco);
031        consistPosition.remove(loco);
032        consistRoster.remove(loco);
033        sendConsistCommand(loco, true, null);
034    }
035
036    /**
037     * Send an NMRA consisting command to add or remove a loco from a consist
038     *
039     * @param loco    The loco to add or remove
040     * @param dirNorm true for normal, false for reverse
041     * @param consist The short consist address for a loco, null to remove
042     */
043    public void sendConsistCommand(DccLocoAddress loco, boolean dirNorm, WiFiConsist consist) {
044        int conAddr = 0;
045        if (consist != null) {
046            conAddr = getConsistAddress().getNumber();
047        }
048        //  Use NMRA consist command to set consist address
049        byte[] packet = jmri.NmraPacket.consistControl(loco.getNumber(),
050                loco.isLongAddress(),
051                conAddr,
052                dirNorm);
053        if (packet != null) {
054            if (log.isDebugEnabled()) {
055                log.debug("Sending packet: {}", java.util.Arrays.toString(packet));
056            }
057            jmri.InstanceManager.getDefault(jmri.CommandStation.class).sendPacket(packet, 1);
058        }
059    }
060
061    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WiFiConsist.class);
062
063}