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