001package jmri.jmris;
002
003import java.beans.PropertyChangeListener;
004import java.io.IOException;
005import jmri.InstanceManager;
006import jmri.JmriException;
007import jmri.PowerManager;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Abstract interface between the JMRI power manager and a network connection
013 *
014 * @author Paul Bender Copyright (C) 2010
015 */
016abstract public class AbstractPowerServer implements PropertyChangeListener {
017
018    public AbstractPowerServer() {
019
020        // Check to see if the Power Manger has a current status
021/*        if(mgrOK()) {
022         try {
023         sendStatus(p.getPower());
024         } catch (JmriException ex) {
025         try {
026         sendErrorStatus();
027         } catch (IOException ie) {
028         } catch (java.lang.NullPointerException je) {
029         }
030         } catch(IOException ie2) {
031         } catch (java.lang.NullPointerException je2) {
032         }
033         }*/
034    }
035
036    protected boolean mgrOK() {
037        if (p == null) {
038            p = InstanceManager.getNullableDefault(PowerManager.class);
039            if (p == null) {
040                log.error("No power manager instance found");
041                try {
042                    sendErrorStatus();
043                } catch (IOException ie) {
044                }
045                return false;
046            } else {
047                p.addPropertyChangeListener(PowerManager.POWER, this);
048            }
049        }
050        return true;
051    }
052
053    public void setOnStatus() {
054        if (mgrOK()) {
055            try {
056                p.setPower(PowerManager.ON);
057            } catch (JmriException e) {
058                log.error("Exception trying to turn power on", e);
059                try {
060                    sendErrorStatus();
061                } catch (IOException ie) {
062                }
063            }
064        }
065    }
066
067    public void setOffStatus() {
068        if (mgrOK()) {
069            try {
070                p.setPower(PowerManager.OFF);
071            } catch (JmriException e) {
072                log.error("Exception trying to turn power off", e);
073                try {
074                    sendErrorStatus();
075                } catch (IOException ie) {
076                }
077            }
078        }
079    }
080
081    @Override
082    public void propertyChange(java.beans.PropertyChangeEvent ev) {
083        try {
084            sendStatus(p.getPower());
085        } catch (IOException ie) {
086            // silently ignore
087        }
088    }
089
090    public void dispose() {
091        if (p != null) {
092            p.removePropertyChangeListener(this);
093        }
094    }
095
096    protected PowerManager p = null;
097
098    /*
099     * Protocol Specific Abstract Functions
100     */
101    abstract public void sendStatus(int Status) throws IOException;
102
103    abstract public void sendErrorStatus() throws IOException;
104
105    abstract public void parseStatus(String statusString) throws JmriException, IOException;
106
107    private final static Logger log = LoggerFactory.getLogger(AbstractPowerServer.class);
108
109}