001package jmri.jmrix.srcp;
002
003import jmri.JmriException;
004import jmri.PowerManager;
005import jmri.managers.AbstractPowerManager;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * PowerManager implementation for controlling layout power
012 *
013 * @author Bob Jacobsen Copyright (C) 2001, 2008
014 */
015public class SRCPPowerManager extends AbstractPowerManager<SRCPBusConnectionMemo> implements SRCPListener {
016
017    boolean waiting = false;
018    int onReply = UNKNOWN;
019    int _bus = 0;
020    SRCPTrafficController tc = null;
021
022    public SRCPPowerManager(SRCPBusConnectionMemo memo, int bus) {
023        super(memo);
024        // connect to the TrafficManager
025        tc = memo.getTrafficController();
026        tc.addSRCPListener(this);
027        _bus = bus;
028    }
029
030    @Override
031    public void setPower(int v) throws JmriException {
032        int old = power;
033        power = UNKNOWN; // while waiting for reply
034        checkTC();
035        if (v == ON) {
036            // configure to wait for reply
037            waiting = true;
038            onReply = PowerManager.ON;
039            // send "Enable main track"
040            SRCPMessage l = SRCPMessage.getEnableMain();
041            tc.sendSRCPMessage(l, this);
042        } else if (v == OFF) {
043            // configure to wait for reply
044            waiting = true;
045            onReply = PowerManager.OFF;
046            firePowerPropertyChange(old, power);
047            // send "Kill main track"
048            SRCPMessage l = SRCPMessage.getKillMain();
049            tc.sendSRCPMessage(l, this);
050        }
051        firePowerPropertyChange(old, power);
052    }
053
054    // to free resources when no longer used
055    @Override
056    public void dispose() throws JmriException {
057        tc.removeSRCPListener(this);
058        tc = null;
059    }
060
061    private void checkTC() throws JmriException {
062        if (tc == null) {
063            throw new JmriException("attempt to use SRCPPowerManager after dispose");
064        }
065    }
066
067    // to listen for status changes from SRCP system
068    @Override
069    public void reply(SRCPReply m) {
070        if (waiting) {
071            int old = power;
072            power = onReply;
073            firePowerPropertyChange(old, power);
074        }
075        waiting = false;
076    }
077
078    // to listen for status changes from SRCP system
079    @Override
080    public void reply(jmri.jmrix.srcp.parser.SimpleNode n) {
081        log.debug("reply called with simpleNode {}", n.jjtGetValue());
082        reply(new SRCPReply(n));
083    }
084
085    @Override
086    public void message(SRCPMessage m) {
087        if (m.isKillMain()) {
088            // configure to wait for reply
089            waiting = true;
090            onReply = PowerManager.OFF;
091        } else if (m.isEnableMain()) {
092            // configure to wait for reply
093            waiting = true;
094            onReply = PowerManager.ON;
095        }
096    }
097
098    private final static Logger log = LoggerFactory.getLogger(SRCPPowerManager.class);
099
100}
101
102
103