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

📄 onewireserver.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
字号:
package net.sf.dz.daemon.onewire.owfs;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FilenameFilter;import java.io.IOException;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import java.util.TreeSet;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.sem.RWLock;import org.freehold.jukebox.service.ActiveService;import net.sf.dz.daemon.Server;import net.sf.dz.daemon.ServerModule;import net.sf.dz.daemon.onewire.DeviceContainer;import net.sf.dz.daemon.onewire.OneWireContainerListener;/** * <a href="http://owfs.sourceforge.net/">OWFS</a> based 1-Wire&reg; server. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2004 * @version $Id: OneWireServer.java,v 1.2 2004/06/28 20:35:47 vtt Exp $ */public class OneWireServer extends ActiveService implements ServerModule, net.sf.dz.daemon.onewire.OneWireServer {    public static final LogChannel CH_OWFS = new LogChannel("OWFS 1-Wire");    /**     * The listener set.     */    private Set listenerSet = new HashSet();        /**     * The server this module is attached to.     */    private Server server;        private RWLock lock = new RWLock();        private File mountPoint;    public void attach(Server server) {            if ( this.server != null ) {                    throw new IllegalStateException("Already attached to the server");        }                this.server = server;    }    /**     * Configure the service.     *     * All the configuration problems have to be detected at this stage.     */    protected void configure() throws Throwable {            String cfroot = getConfigurationRoot();        Configuration cf = getConfiguration();                String mount = cf.getString(cfroot + ".owfs.mount_point");                complain(LOG_INFO, CH_OWFS, "OWFS mount point: " + mount);        mountPoint = new File(mount);    }        protected void startup() throws Throwable {            if ( !mountPoint.exists() || !mountPoint.canRead() || !mountPoint.isDirectory() ) {                    throw new IllegalArgumentException(mountPoint + ": doesn't exist, not readable or not a directory");        }            complain(LOG_NOTICE, CH_OWFS, "Started");    }    protected void execute() throws Throwable {            while ( isEnabled() ) {                    poll();        }    }        private void poll() {            try {                    File branches[] = mountPoint.listFiles(new FamilyNameFilter("1F"));                        // Handle the default branch first                        poll(mountPoint);                        // And then the rest                        for ( int idx = 0; idx < branches.length; idx++ ) {                            pollBranch(branches[idx]);            }                } catch ( Throwable t ) {                    complain(LOG_ERR, CH_OWFS, "Poll broken: ", t);        }    }        private void poll(File root) throws FileNotFoundException, IOException {            complain(LOG_INFO, CH_OWFS, "Polling " + root.getName());                // Check if this is a branch                if ( root.getName().startsWith("1F") ) {                    pollBranch(root);            return;        }            File tempSensors[] = root.listFiles(new FamilyNameFilter("10"));                if ( tempSensors.length == 0 ) {                    complain(LOG_NOTICE, CH_OWFS, "No sensors on " + root.getName() + "?");        }                for ( int idx = 0; idx < tempSensors.length; idx++ ) {                    readTemp(tempSensors[idx]);        }        File switches[] = root.listFiles(new FamilyNameFilter("12"));                if ( switches.length == 0 ) {                    complain(LOG_NOTICE, CH_OWFS, "No switches on " + root.getName() + "?");        }                for ( int idx = 0; idx < switches.length; idx++ ) {                    readSwitch(switches[idx]);        }        File branches[] = root.listFiles(new FamilyNameFilter("1F"));                for ( int idx = 0; idx < branches.length; idx++ ) {                    pollBranch(branches[idx]);        }    }        private void pollBranch(File root) throws FileNotFoundException, IOException {            // DS2409, in current implementation, has 'main' and 'aux' subdirectories.                // VT: FIXME: Don't know about others, though...                File main = new File(root, "main");                if ( main.exists() ) {                    poll(main);        }                File aux = new File(root, "aux");                if ( aux.exists() ) {                    poll(aux);        }    }        private void readTemp(File sensorDir) throws FileNotFoundException, IOException {            complain(LOG_DEBUG, CH_OWFS, "Sensor: " + sensorDir);                File tempFile = new File(sensorDir, "temperature");                BufferedReader br = new BufferedReader(new FileReader(tempFile));                String temp = br.readLine();                complain(LOG_INFO, CH_OWFS, "Temperature: " + sensorDir.getName() + ": " + temp);    }        private void readSwitch(File switchDir) {            complain(LOG_DEBUG, CH_OWFS, "Switch: " + switchDir);                            }        protected void shutdown(Throwable cause) throws Throwable {            // Nothing to do, really...        complain(LOG_NOTICE, CH_OWFS, "Shut down.");    }        public DeviceContainer getDeviceContainer(String address) {            throw new Error("Not Implemented");    }        public Iterator iterator() {            return new TreeSet().iterator();    }    public RWLock getLock() {            return lock;    }        /**     * Add the listener.     *     * @param listener The listener to add.     */    public synchronized void addListener(OneWireContainerListener listener) {            // VT: FIXME: Dump the set of arrival notifications on'em? Better be        // done from a different thread...                listenerSet.add(listener);    }        public void removeListener(OneWireContainerListener listener) {            listenerSet.remove(listener);    }        protected static class FamilyNameFilter implements FilenameFilter {            private String family;                public FamilyNameFilter(String family) {                    if ( family == null ) {                            throw new IllegalArgumentException("Family name can't be null");            }                        if ( family.length() != 2 ) {                            throw new IllegalArgumentException("Invalid family name '" + family + "' - must be two hex chars. I think.");            }                        this.family = family;        }            public boolean accept(File dir, String name) {                    if ( name == null ) {                            return false;            }                        if ( name.startsWith(family) ) {                            return true;            }                        return false;        }    }}

⌨️ 快捷键说明

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