001package jmri.jmrix.mrc;
002
003import java.util.Date;
004import jmri.JmriException;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * PowerManager implementation for controlling layout power
010 * <p>
011 * Some of the message formats used in this class are Copyright MRC, Inc. and
012 * used with permission as part of the JMRI project. That permission does not
013 * extend to uses in other software products. If you wish to use this code,
014 * algorithm or these message formats outside of JMRI, please contact Mrc Inc
015 * for separate permission.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001
018 * 
019 */
020public class MrcPowerManager
021        extends jmri.managers.AbstractPowerManager<MrcSystemConnectionMemo>
022        implements MrcTrafficListener {
023
024    public MrcPowerManager(MrcSystemConnectionMemo memo) {
025        super(memo);
026        // standard Mrc - connect
027        if (memo.getMrcTrafficController() == null) {
028            log.error("PowerManager Created, yet there is no Traffic Controller");
029            return;
030        }
031        this.tc = memo.getMrcTrafficController();
032        tc.addTrafficListener(MrcInterface.POWER, this);
033
034    }
035
036    @Override
037    public void setPower(int v) throws JmriException {
038        //power = UNKNOWN;
039        int old = power;
040
041        checkTC();
042        if (v == ON) {
043            MrcMessage l = MrcMessage.setPowerOn();
044            tc.sendMrcMessage(l);
045        } else if (v == OFF) {
046            MrcMessage l = MrcMessage.setPowerOff();
047            tc.sendMrcMessage(l);
048        }
049        power = v;
050        firePowerPropertyChange(old, power);
051    }
052
053    // these next three public methods have been added so that other classes
054    // do not need to reference the static final values "ON", "OFF", and "UKNOWN".
055    public boolean isPowerOn() {
056        return (power == ON);
057    }
058
059    public boolean isPowerOff() {
060        return (power == OFF);
061    }
062
063    public boolean isPowerUnknown() {
064        return (power == UNKNOWN);
065    }
066
067    // to free resources when no longer used
068    @Override
069    public void dispose() {
070        if (tc != null) {
071            tc.removeTrafficListener(MrcInterface.POWER, this);
072        }
073        tc = null;
074    }
075
076    MrcTrafficController tc = null;
077
078    private void checkTC() throws JmriException {
079        if (tc == null) {
080            throw new JmriException("Use power manager after dispose"); // NOI18N
081        }
082    }
083
084    @Override
085    public void notifyRcv(Date timestamp, MrcMessage m) { /*message(m);*/ }
086
087    @Override
088    public void notifyXmit(Date timestamp, MrcMessage m) {/* message(m);*/ }
089
090    @Override
091    public void notifyFailedXmit(Date timestamp, MrcMessage m) { /*message(m);*/ }
092
093    private final static Logger log = LoggerFactory.getLogger(MrcPowerManager.class);
094}
095
096