⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstracttemperaturesensor.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
字号:
package net.sf.dz.device.sensor.impl;import java.io.IOException;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TreeSet;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.logger.Logger;import org.freehold.jukebox.conf.Configurable;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.service.ActiveService;import net.sf.dz.device.sensor.TemperatureSensor;import net.sf.dz.event.TemperatureSensorListener;/** * An abstract temperature sensor. * * <p> * * Supports the common configuration and listener notification features. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001-2002 * @version $Id: AbstractTemperatureSensor.java,v 1.6 2004/06/28 20:35:48 vtt Exp $ */abstract public class AbstractTemperatureSensor extends ActiveService implements TemperatureSensor {    /**     * Log channel to use.     */    public static final LogChannel CH_ATS = new LogChannel("TS/Abstract");        private static final Set errorMessages = new TreeSet();        {        errorMessages.add("1-Wire network short circuit");        errorMessages.add("Connection Lost");        errorMessages.add("Not Available");        errorMessages.add("Sensor Departed");    }        /**     * The current temperature.     *     * <p>     *     * This value has to be updated by {@link #execute execute()} and used     * by {@link #getCurrentTemperature getCurrentTemperature()} in order to     * provide a fast response.     *     * <p>     *     * If the value is <code>null</code>, it means that no temperature     * readings are available yet.     */    private Object currentTemperature = "Not Available";        private Set listenerSet = new HashSet();        /**     * The poll interval.     *     * <p>     *     * Sleep this many milliseconds between measuring the temperature and     * possibly reporting it.     *     * <p>     *     * This variable initialized to -1 in order to make sure that we do     * invoke the {@link #configure super.configure()} from the subclass. If     * we don't, then {@link #execute execute()} is going to blow up when it     * tries to sleep for the negative amount of time.     */    private long poll_interval = -1;        /**     * Hardware address of the sensor on the remote end to watch for.     */    private String address;        public AbstractTemperatureSensor() {        }    /**     * Configure the poll interval and sensor address.     */    protected void configure() throws Throwable {            String cfroot = getConfigurationRoot();        Configuration cf = getConfiguration();            poll_interval = cf.getLong(cfroot + ".poll_interval", 5) * 1000;        address = cf.getString(cfroot + ".address");    }        public String getAddress() {            // Make sure we're configured                getConfiguration();                return address;    }    public synchronized final double getCurrentTemperature() {            if ( currentTemperature instanceof Double ) {                    return ((Double)currentTemperature).doubleValue();                } else {                    throw new IllegalStateException(currentTemperature.toString());        }    }        /**     * We need this to avoid notifying the guys if the temperature hasn't     * really changed <strong>for them</strong> (remember, we dumb down the     * readings).     */    private double lastTemperature = 0;        protected void execute() {            if ( poll_interval < 0 ) {                    throw new IllegalArgumentException("You have to call super.configure() from configure()");        }                if ( poll_interval < 1000 ) {                    complain(LOG_WARNING, CH_ATS, "Unreasonably short poll interval");        }                try {                    while ( isEnabled() ) {                            try {                                    currentTemperature = new Double(getSensorTemperature());                                                            //complain(LOG_ALERT, CH_ATS, "Current temperature: " + currentTemperature);                                        // VT: NOTE: We will notify the listeners even if the                    // temperature hasn't changed - their processing logic may                    // suck and just get stuck if they don't get frequent                    // notifications                                        for ( Iterator i = listenerSet.iterator(); i.hasNext(); ) {                                            TemperatureSensorListener l = (TemperatureSensorListener)i.next();                                                l.currentTemperatureChanged(this, currentTemperature);                    }                                        lastTemperature = ((Double)currentTemperature).doubleValue();                                    } catch ( IllegalStateException isex ) {                                    String message = isex.getMessage();                                        currentTemperature = message;                                    // Most probably, the readings are not yet available                                        if ( isKnownErrorMessage(message) ) {                                            complain(LOG_WARNING, CH_ATS, "Sensor failure[" + getAddress() + "]: " + message);                                                // Notify the consumers anyway, the sensor may be                        // permanently inoperable                                            for ( Iterator i = listenerSet.iterator(); i.hasNext(); ) {                                                    TemperatureSensorListener l = (TemperatureSensorListener)i.next();                                                        // VT: FIXME: The literal string will be                            // compared against, it would be a good idea to                            // expose it as a constant somewhere                                                        l.currentTemperatureChanged(this, message);                        }                                            } else {                                            // The problem is different, so let's handle it                        // where we know how                                                throw isex;                    }                }                            //complain(LOG_ERR, CH_ATS, "sleeping for " + poll_interval + "ms...");                                    Thread.sleep(poll_interval);            }                } catch ( Throwable t ) {                    complain(LOG_ERR, CH_ATS, "Unexpected problem, shutting down:", t);        }    }        protected final boolean isKnownErrorMessage(String message) {            return errorMessages.contains(message);    }        public final void addListener(TemperatureSensorListener l) {            //complain(LOG_DEBUG, CH_ATS, "Added listener: " + l);            listenerSet.add(l);    }        public final void removeListener(TemperatureSensorListener l) {            listenerSet.remove(l);    }        /**     * Get the actual device temperature.     *     * @return The sensor temperature.     *     * @exception IOException if there was a problem communicating with the     * hardware sensor.     */    abstract protected double getSensorTemperature() throws IOException;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -