📄 abstractdevicemonitor.java
字号:
{ keepRunning = false; startRunning = false; } // wait until the thread is no longer running, with a timeout int i=0; while (!hasCompletelyStopped && i<100) { msSleep(20); } } /** * Monitor run method that performs a periodic search * of the entire 1-Wire network. Listeners * that have registered are notified when changes in the network * are detected. */ public void run () { synchronized (sync_flag) { hasCompletelyStopped = false; } Vector arrivals = new Vector(), departures = new Vector(); while (keepRunning) { if (startRunning) { // set is now running synchronized (sync_flag) { isRunning = true; } // erase previous arrivals and departures arrivals.setSize(0); departures.setSize(0); // number of times an error occurred during 1-Wire search int errorCount = 0; boolean done = false; while(!done) { try { // search for new devices, remove stale device entries search(arrivals, departures); done = true; } catch(Exception e) { if(++errorCount==max_error_count) { fireException(adapter, e); done = true; } } } // sleep to give other threads a chance at this network msSleep(200); } else { // not running so clear flag synchronized (sync_flag) { isRunning = false; } msSleep(200); } } // not running so clear flag synchronized (sync_flag) { isRunning = false; hasCompletelyStopped = true; } } //-------- //-------- Utility methods //-------- /** * Sleep for the specified number of milliseconds * * @param msTime number of milliseconds to sleep */ protected void msSleep (long msTime) { Thread.yield(); try { Thread.sleep(msTime); } catch (InterruptedException e) { ; } } //-------- //-------- Event methods //-------- /** * Add a listener, to be notified of arrivals, departures, and exceptions. * * @param dmel Listener of monitor events. */ public void addDeviceMonitorEventListener(DeviceMonitorEventListener dmel) { if(dmel!=null) this.listeners.addElement(dmel); } /** * Notify the listeners of the arrival event * * @param address Vector of Long objects representing the address of new * arrivals. */ protected void fireArrivalEvent(DSPortAdapter adapter, Vector address) { DeviceMonitorEvent dme = new DeviceMonitorEvent(DeviceMonitorEvent.ARRIVAL, this, adapter, (Vector)address.clone()); for (int i = 0; i < listeners.size(); i++) { DeviceMonitorEventListener listener = (DeviceMonitorEventListener)listeners.elementAt(i); listener.deviceArrival(dme); } } /** * Notify the listeners of the departure event * * @param address Vector of Long objects representing the address of * departed devices. */ protected void fireDepartureEvent(DSPortAdapter adapter, Vector address) { DeviceMonitorEvent dme = new DeviceMonitorEvent(DeviceMonitorEvent.DEPARTURE, this, adapter, (Vector)address.clone()); for (int i = 0; i < listeners.size(); i++) { DeviceMonitorEventListener listener = (DeviceMonitorEventListener)listeners.elementAt(i); listener.deviceDeparture(dme); } } /** * Notify the listeners of the exception * * @param ex The exception that occurred. */ private void fireException (DSPortAdapter adapter, Exception ex) { for (int i = 0; i < listeners.size(); i++) { ((DeviceMonitorEventListener) listeners.elementAt( i)).networkException( new DeviceMonitorException(this, adapter, ex)); } } /** * Returns the OWPath of the device with the given address. * * @param address a byte array representing the address of the device * @return The OWPath representing the network path to the device. */ public OWPath getDevicePath(byte[] address) { return getDevicePath(Address.toLong(address)); } /** * Returns the OWPath of the device with the given address. * * @param address a string representing the address of the device * @return The OWPath representing the network path to the device. */ public OWPath getDevicePath(String address) { return getDevicePath(Address.toLong(address)); } /** * Returns the OWPath of the device with the given address. * * @param address a long representing the address of the device * @return The OWPath representing the network path to the device. */ public OWPath getDevicePath(long address) { return getDevicePath(new Long(address)); } /** * Returns the OWPath of the device with the given address. * * @param address a Long object representing the address of the device * @return The OWPath representing the network path to the device. */ public abstract OWPath getDevicePath(Long address); /** * Returns all addresses known by this monitor as an Enumeration of Long * objects. * * @return Enumeration of Long objects */ public Enumeration getAllAddresses() { return deviceAddressHash.keys(); } //-------- //-------- Static methods //-------- /** * Returns the OneWireContainer object of the device with the given address. * * @param adapter The DSPortAdapter that the device is connected to. * @param address a byte array representing the address of the device * @return The specific OneWireContainer object of the device */ public static OneWireContainer getDeviceContainer(DSPortAdapter adapter, byte[] address) { return getDeviceContainer(adapter, Address.toLong(address)); } /** * Returns the OneWireContainer object of the device with the given address. * * @param adapter The DSPortAdapter that the device is connected to. * @param address a String representing the address of the device * @return The specific OneWireContainer object of the device */ public static OneWireContainer getDeviceContainer(DSPortAdapter adapter, String address) { return getDeviceContainer(adapter, Address.toLong(address)); } /** * Returns the OneWireContainer object of the device with the given address. * * @param adapter The DSPortAdapter that the device is connected to. * @param address a long representing the address of the device * @return The specific OneWireContainer object of the device */ public static OneWireContainer getDeviceContainer(DSPortAdapter adapter, long address) { return getDeviceContainer(adapter, new Long(address)); } /** * Returns the OneWireContainer object of the device with the given address. * * @param adapter The DSPortAdapter that the device is connected to. * @param address a Long object representing the address of the device * @return The specific OneWireContainer object of the device */ public static OneWireContainer getDeviceContainer(DSPortAdapter adapter, Long longAddress) { synchronized(deviceContainerHash) { Object o = deviceContainerHash.get(longAddress); if(o==null) { o = adapter.getDeviceContainer(longAddress.longValue()); putDeviceContainer(longAddress, (OneWireContainer)o); } return (OneWireContainer)o; } } /** * Sets the OneWireContainer object of the device with the given address. * * @param address a byte array object representing the address of the device * @param owc The specific OneWireContainer object of the device */ public static void putDeviceContainer(byte[] address, OneWireContainer owc) { putDeviceContainer(Address.toLong(address),owc); } /** * Sets the OneWireContainer object of the device with the given address. * * @param address a String object representing the address of the device * @param owc The specific OneWireContainer object of the device */ public static void putDeviceContainer(String address, OneWireContainer owc) { putDeviceContainer(Address.toLong(address),owc); } /** * Sets the OneWireContainer object of the device with the given address. * * @param address a long object representing the address of the device * @param owc The specific OneWireContainer object of the device */ public static void putDeviceContainer(long address, OneWireContainer owc) { putDeviceContainer(new Long(address),owc); } /** * Sets the OneWireContainer object of the device with the given address. * * @param address a Long object representing the address of the device * @param owc The specific OneWireContainer object of the device */ public static void putDeviceContainer(Long longAddress, OneWireContainer owc) { synchronized(deviceContainerHash) { deviceContainerHash.put(longAddress, owc); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -