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

📄 abstractdevicemonitor.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 2002 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.application.monitor;import java.util.Hashtable;import java.util.Vector;import java.util.Enumeration;import com.dalsemi.onewire.OneWireException;import com.dalsemi.onewire.adapter.OneWireIOException;import com.dalsemi.onewire.adapter.DSPortAdapter;import com.dalsemi.onewire.container.OneWireContainer;import com.dalsemi.onewire.utils.Address;import com.dalsemi.onewire.utils.OWPath;/** * <P>Abstract super-class for 1-Wire Monitors, a optionally-threadable * object for searching 1-Wire networks.  If this object is not run in it's own * thread, it is possible to perform single-step searches by calling the search * method directly {@see #search(Vector, Vector)}.  The monitor will generate * events for device arrivals, device departures, and exceptions from the * DSPortAdapter.</P> * * <P>In a touch-contact environment, it is not suitable to say that a * device has "departed" because it was missing for one cycle of searching. * In the time it takes to get an iButton into a blue-dot receptor, the * monitor could have generated a handful of arrival and departure events.  To * circumvent this problem, the device monitor keeps a "missing state count" for * each device on the network.  Each search cycle that passes where the device * is missing causes it's "missing state count" to be incremented.  Once the * device's "missing state count" is equal to the "max state count" * {@see #getMaxStateCount()}, a departure event is generated for the device. * If the 1-Wire Network is not in a touch environment, it may be unnecessary * to use this "missing state count".  In those instances, setting the state * count to 1 will disable the feature {@see #setMaxStateCount(int)}.</P> * * <P>Similarly, the reporting of exceptions could be spurious in a * touch-contact environment.  Instead of reporting the exception on each * failed search attempt, the monitor will default to retrying the search a * handful of times {@see #getMaxErrorCount()} before finally reporting the * exception.  To disable this feature, set the max error count to 1 * {@see #setMaxErrorCount(int)}.</P> * * <P>To receive events, an object must implement the * <code>DeviceMonitorEventListener</code> interface * {@see DeviceMonitorEventListener} and must be added to * the list of listeners {@see #addDeviceMonitorEventListener}.</P> * * @author SH * @version 1.00 */public abstract class AbstractDeviceMonitor   implements Runnable{   //--------   //-------- Constants   //--------   /** object used for synchronization */   protected final Object sync_flag = new Object();   /** Addresses of all current devices, mapped to their state count */   protected final Hashtable deviceAddressHash = new Hashtable();   /**    * hashtable for holding device containers, static to keep only a    * single instance of each OneWireContainer.    */   protected static final Hashtable deviceContainerHash = new Hashtable();   /**    * Listeners who receive notification of events generated by this    * device Monitor    */   protected final Vector listeners = new Vector();   //--------   //-------- Variables   //--------   /** Number of searches that a button should be "missing" before it is removed */   protected int max_state_count = 3;   /** Number of searches that an error occurs before a dialog is displayed */   protected int max_error_count = 6;   /** Flag for overall thread running state */   protected volatile boolean keepRunning = true, hasCompletelyStopped = false;   /** Flag to indicate thread has begin to run */   protected volatile boolean startRunning = true;   /** Flag to indicate thread is running now */   protected volatile boolean isRunning = false;   /** the adapter to search for devices */   protected DSPortAdapter adapter = null;   /**    * The device monitor will internally cache OneWireContainer objects for each    * 1-Wire device.  Use this method to clean up all stale container objects.    * A stale container object is a OneWireContainer object which references a    * 1-Wire device address which has not been seen by a recent search.    * This will be essential in a touch-contact environment which could run    * for some time and needs to conserve memory.    */   public void cleanUpStaleContainerReferences()   {      synchronized(deviceContainerHash)      {         Enumeration e = deviceContainerHash.keys();         while(e.hasMoreElements())         {            Object o = e.nextElement();            if(!deviceAddressHash.containsKey(o))               deviceContainerHash.remove(o);         }      }   }   /**    * The device monitor will internally cache OWPath objects for each    * 1-Wire device.  Use this method to clean up all stale OWPath objects.    * A stale path object is a OWPath which references a branching path to a    * 1-Wire device address which has not been seen by a recent search.    * This will be essential in a touch-contact environment which could run    * for some time and needs to conserve memory.    */   public void cleanUpStalePathReferences()   {      // no-op by default.  Only NetworkDeviceMonitor uses paths   }   /**    * Resets this device monitor.  All known devices will be marked as    * "departed" and departure events will be fired.    */   public void resetSearch()   {      synchronized (sync_flag)      {         // fire departures for all devices         if(deviceAddressHash.size()>0 && listeners.size()>0)         {            Vector v = new Vector(deviceAddressHash.size());            Enumeration e = deviceAddressHash.keys();            while(e.hasMoreElements())               v.addElement(e.nextElement());            fireDepartureEvent(adapter, v);         }         deviceAddressHash.clear();      }   }   /**    * The number of searches that a button should be "missing"    * before it is removed.    *    * @return The number of searches that a button should be "missing"    * before it is removed.    */   public int getMaxStateCount()   {      return this.max_state_count;   }   /**    * The number of searches that a button should be "missing"    * before it is removed    *    * @param stateCnt The number of searches that a button should be "missing"    * before it is removed.    */   public void setMaxStateCount(int stateCnt)   {      if(stateCnt<=0)         throw new IllegalArgumentException("State Count must be greater than 0");      this.max_state_count = stateCnt;   }   /**    * Number of searches that an error occurs before listener's are notified    *    * @return Number of searches that an error occurs before listener's    * are notified    */   public int getMaxErrorCount()   {      return this.max_error_count;   }   /**    * Number of searches that an error occurs before listener's are notified    *    * @param errorCnt Number of searches that an error occurs before listener's    * are notified    */   public void setMaxErrorCount(int errorCnt)   {      if(errorCnt<=0)         throw new IllegalArgumentException("Error Count must be greater than 0");      this.max_error_count = errorCnt;   }   /**    * Returns the DSPortAdapter this device is searching    *    * @param the DSPortAdapter this monitor is searching    */   public DSPortAdapter getAdapter()   {      return this.adapter;   }   /**    * Sets this monitor to search a new DSPortAdapter    *    * @param the DSPortAdapter this monitor should search    */   public abstract void setAdapter(DSPortAdapter adapter);   //--------   //-------- Monitor methods   //--------   /**    * Performs a search of the 1-Wire network    *    * @param arrivals A vector of Long objects, represent new arrival addresses.    * @param departures A vector of Long objects, represent departed addresses.    */   public abstract void search(Vector arrivals, Vector departures)      throws OneWireException, OneWireIOException;   /**    * Pause this monitor    *    * @param blocking if true, this method will block until the monitor is paused.    * @returns true if the monitor was successfully paused.    */   public boolean pauseMonitor (boolean blocking)   {      // clear the start flag      synchronized (sync_flag)      {         if (hasCompletelyStopped || (!startRunning && !isRunning))            return true;         startRunning = false;      }      // wait until it is paused or until timeout      int i = 0;      while (isRunning && (blocking || (i++)<100))      {         msSleep(10);      }      return !isRunning;   }   /**    * Resume this monitor    *    * @param blocking if true, this method will block until the monitor is resumed.    * @returns true if the monitor was successfully resumed.    */   public boolean resumeMonitor (boolean blocking)   {      // set the start flag      synchronized (sync_flag)      {         if (hasCompletelyStopped)            return false;         if (startRunning && isRunning)            return true;         startRunning = true;      }      // wait until it is running      int i = 0;      while (!isRunning && (blocking || (i++)<100))      {         msSleep(10);      }      return isRunning;   }   /**    * Check if this monitor is running.    *    * @return <CODE>true</CODE> if monitor is running    */   public boolean isMonitorRunning ()   {      return isRunning;   }   /**    * Kill this monitor.  Wait util this    * thread is no longer alive (with timeout).    */   public void killMonitor ()   {      // clear the running flags      synchronized (sync_flag)

⌨️ 快捷键说明

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