📄 pnptemperaturesensor.java
字号:
package net.sf.dz.device.sensor.impl;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import java.util.TreeMap;import java.util.TreeSet;import javax.net.ssl.SSLException;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.service.ActiveService;import org.freehold.jukebox.service.PassiveService;import org.freehold.jukebox.service.ServiceUnavailableException;import net.sf.dz.device.sensor.TemperatureSensor;import net.sf.dz.pnp.MulticastClient;import net.sf.dz.pnp.MulticastEvent;import net.sf.dz.pnp.MulticastEventListener;import net.sf.dz.pnp.custom.SimpleBroadcastClient;import net.sf.dz.daemon.tcp.client.ConnectorFactory;import net.sf.dz.daemon.tcp.client.DeviceConnector;import net.sf.dz.daemon.tcp.client.DeviceListener;/** * Autoconfigurable TCP temperature sensor. * * <p> * * Uses {@link MulticastClient MulticastClient} to determine available DAC * instances, then, on demand, figures out which instance provides the * requested sensor, connects to it over TCP and reads temperature values, * then distributes them to listeners. * * <p> * * In the current form, this was initally copy-n-paste from {@link * TcpTemperatureSensor TcpTemperatureSensor}, but since this is a Very Bad * Thing (TM), this class has to either extend * <code>TcpTemperatureSensor</code> or replace it altogether. * * @author Copyright © <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2004 * @version $Id: PnpTemperatureSensor.java,v 1.2 2004/06/28 20:35:48 vtt Exp $ */public class PnpTemperatureSensor extends AbstractTemperatureSensor implements DeviceListener { /** * Log channel to use. * * Properly initialized in {@link startup startup()}. */ private LogChannel CH_PNPTS = new LogChannel("TempSensor/PnP"); /** * Last known temperature. * * <p> * * The reason for it being an Object, not <code>java.lang.Double</code>, * is the {@link net.sf.dz.event.TemperatureSensorListener * TemperatureSensorListener} signature which requires an Object to be * able to accept error condition notifications. * * <p> * * Implementation will have two possible value classes for this object: * if everything is normal, then it is <code>java.lang.Double</code>, * otherwise <code>java.lang.String</code> containing the error * description. */ private Object lastKnownTemperature = (Object)"Not Available"; protected static MulticastClient multicastClient; protected static ConnectorFactory connectorFactory; private DeviceConnector connector; protected void configure() throws Throwable { super.configure(); String cfroot = getConfigurationRoot(); Configuration cf = getConfiguration(); } public final String getServiceSignature() { return "DZ DAC Sensors"; } protected void startup() throws Throwable { synchronized ( getClass() ) { if ( multicastClient == null ) { multicastClient = new SimpleBroadcastClient(5001); multicastClient.setLogger(getLogger()); if ( !multicastClient.start().waitFor() ) { throw new ServiceUnavailableException("Could not start multicast client, messages should have been provided"); } connectorFactory = new ConnectorFactory(getLogger()); connectorFactory.setLogger(getLogger()); connectorFactory.addMulticastClient(multicastClient); } } connector = connectorFactory.getConnector(getServiceSignature(), getAddress()); connector.addListener(this); } public synchronized double getSensorTemperature() throws IOException { checkStatus(); if ( !isActive() ) { complain(LOG_ALERT, CH_PNPTS, "getSensorTemperature() before started"); return 0; } if ( lastKnownTemperature == null ) { throw new IllegalStateException("lastKnownTemperature is null, this can't be happening"); } if ( lastKnownTemperature instanceof String ) { throw new IllegalStateException((String)lastKnownTemperature); } if ( lastKnownTemperature instanceof Double ) { return ((Double)lastKnownTemperature).doubleValue(); } throw new IllegalStateException("lastKnownTemperature is not null, not a String, not a Double - WTF?"); } public void currentTemperatureChanged(TemperatureSensor source, Object currentTemperature) { lastKnownTemperature = currentTemperature; } protected void shutdown(Throwable cause) throws Throwable { } public void deviceEvent(DeviceConnector connector, String data) { complain(LOG_DEBUG, CH_PNPTS, "Signal: " + data); try { // Let's try to parse the data currentTemperatureChanged(null, new Double(Double.parseDouble(data))); } catch ( Throwable t ) { // All right, it wasn't data currentTemperatureChanged(null, data); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -