001package jmri.jmrit.withrottle;
002
003import java.util.ArrayList;
004import jmri.Consist;
005import jmri.LocoAddress;
006import jmri.DccLocoAddress;
007import jmri.implementation.NmraConsistManager;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * @author Brett Hoffman Copyright (C) 2010, 2011
013 * 
014 */
015public class WiFiConsistManager extends NmraConsistManager {
016
017    ArrayList<ControllerInterface> listeners = null;
018    boolean isValid = false;
019
020    public WiFiConsistManager() {
021        super(jmri.InstanceManager.getDefault(jmri.CommandStation.class));
022        log.debug("New WiFiConsistManager");
023        isValid = true;
024    }
025
026    /**
027     * Check to see if an address will try to broadcast (0) a programming
028     * message.
029     *
030     * @param addr The address to check
031     * @return true if address is no good, otherwise false
032     */
033    public boolean checkForBroadcastAddress(DccLocoAddress addr) {
034        if (addr.getNumber() < 1) {
035            log.warn("Trying to use broadcast address!");
036            return true;
037        }
038        return false;
039    }
040
041    @Override
042    public Consist addConsist(LocoAddress address) {
043        if (! (address instanceof DccLocoAddress)) { 
044            throw new IllegalArgumentException("address is not a DccLocoAddress object");
045        }
046        WiFiConsist consist;
047        consist = new WiFiConsist((DccLocoAddress) address);
048        consistTable.put(address, consist);
049        notifyConsistListChanged();
050        return consist;
051    }
052
053    /**
054     * Add a listener to handle: listener.sendPacketToDevice(message);
055     *
056     * @param listener the listener to add.
057     */
058    public void addControllerListener(ControllerInterface listener) {
059        if (listeners == null) {
060            listeners = new ArrayList<ControllerInterface>(1);
061        }
062        if (!listeners.contains(listener)) {
063            listeners.add(listener);
064        }
065    }
066
067    public void removeControllerListener(ControllerInterface listener) {
068        if (listeners == null) {
069            return;
070        }
071        if (listeners.contains(listener)) {
072            listeners.remove(listener);
073        }
074    }
075
076    private final static Logger log = LoggerFactory.getLogger(WiFiConsistManager.class);
077
078}