001package jmri.jmrit.withrottle;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import jmri.InstanceManager;
006import jmri.JmriException;
007import jmri.PowerManager;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Handle two-way communications regarding track power.
013 *
014 * @author Brett Hoffman Copyright (C) 2010
015 */
016public class TrackPowerController extends AbstractController implements PropertyChangeListener {
017
018    private PowerManager pwrMgr = null;
019
020    public TrackPowerController() {
021        pwrMgr = InstanceManager.getNullableDefault(jmri.PowerManager.class);
022        if (pwrMgr == null) {
023            log.info("No power manager instance.");
024            isValid = false;
025        } else {
026            register();
027            isValid = true;
028        }
029    }
030
031    @Override
032    public boolean verifyCreation() {
033        return isValid;
034    }
035
036    @Override
037    public void handleMessage(String message, DeviceServer deviceServer) {
038        if (message.charAt(0) == 'A') {
039            switch (message.charAt(1)) {
040                case '1':
041                    setTrackPowerOn();
042                    break;
043                case '0':
044                    setTrackPowerOff();
045                    break;
046                default:
047                    log.warn("Unknown Track Power message from wi-fi device");
048                    break;
049            }
050        }
051    }
052
053    private void setTrackPowerOn() {
054        if (pwrMgr != null) {
055            try {
056                pwrMgr.setPower(PowerManager.ON);
057            } catch (JmriException e) {
058                log.error("Cannot turn power on.");
059            }
060        }
061    }
062
063    private void setTrackPowerOff() {
064        if (pwrMgr != null) {
065            try {
066                pwrMgr.setPower(PowerManager.OFF);
067            } catch (JmriException e) {
068                log.error("Cannot turn power off.");
069            }
070        }
071    }
072
073    public void sendCurrentState() {
074        if (listeners == null) {
075            return;
076        }
077        String message = null;
078        switch (pwrMgr.getPower()) {
079            case PowerManager.ON:
080                message = "PPA1";
081                break;
082            case PowerManager.OFF:
083                message = "PPA0";
084                break;
085            case PowerManager.UNKNOWN:
086                message = "PPA2";
087                break;
088            default:
089                log.error("Unexpected state value: +{}", pwrMgr.getPower());
090                break;
091        }
092
093        for (ControllerInterface listener : listeners) {
094            listener.sendPacketToDevice(message);
095        }
096
097    }
098
099    @Override
100    public void propertyChange(PropertyChangeEvent event) {
101        sendCurrentState();
102    }
103
104    @Override
105    public void register() {
106        pwrMgr.addPropertyChangeListener(this);
107    }
108
109    @Override
110    public void deregister() {
111        pwrMgr.removePropertyChangeListener(this);
112    }
113
114    private final static Logger log = LoggerFactory.getLogger(TrackPowerController.class);
115}