stella.ow
Class ServerClient

java.lang.Object
  extended by util.PropertyContainer
      extended by util.PropertyResources
          extended by util.PropertyBundles
              extended by io.AbstractDriver
                  extended by io.AbstractSocketDriver
                      extended by stella.ow.ServerClient
All Implemented Interfaces:
Driver, Resettable, StreamIODriver, Cloneable, OneWireDriver, ExitCleaning, Initializable, LocalizedSupplying, PropertySupplying, ResourceSupplying

public class ServerClient
extends AbstractSocketDriver
implements OneWireDriver, Resettable

Java class for accessing OWFS through owserver daemon over IP.

OWFS is an open source project developed by Paul Alfille and hosted at http://www.owfs.org

Based on the original OWNet code from Paul Alfille. This is a one-wire driver that allow switching and reading sensors.


Nested Class Summary
static class ServerClient.Sensor
          Tries to read a single temperature sensor.
static class ServerClient.Swi
          Tries to set a single switch.
static class ServerClient.Switch
          Tries to set a single switch.
 
Nested classes/interfaces inherited from class util.PropertyResources
PropertyResources.URLResource
 
Field Summary
private static Map<String,String> cache
          A cache for diverse path's.
private static Map<String,Long> cachetime
          The attached time cache.
private static long DEFBUSCOOLDOWN
          Default retries for write.
private static long DEFCACHETIME
          Default retries for write.
private static int DEFDATALENGTH
          Datalength.
private static int DEFFORMATFLAGS
          Formatting flags.
private static int DEFMAXNULLREAD
          Maximum number of allowed null payload reads before throwing IOExc.
private static boolean DEFPERSISTENT
          Datalength.
private static int DEFREADRETRY
          Default retries for write.
private static long DEFRESETCADENCE
          Default timeout allowed for the reset.
private static long DEFRESETTIMEOUT
          Default timeout allowed for the reset.
private static long DEFRETRYCOOLDOWN
          Default retries for write.
private static int DEFSOCKETPORT
          Port of the server.
private static int DEFSOCKETTIMEOUT
          Timeout.
private static int DEFWRITERETRY
          Default retries for write.
private  DataInputStream indata
          The input stream of the socket is wrapped into a data input stream.
static String KEY_BUSCOOLDOWN
          Before and after any bus operation cooldown.
static String KEY_CACHETIME
          Cache-valid for A/D convert operations.
static String KEY_DATALENGTH
          Datalength.
static String KEY_FORMATFLAGS
          Formatting flags.
static String KEY_MAXNULLREAD
          Problems with hanging servers always reporting OK, but never end.
static String KEY_OWRESET
          Points to the path of an executable script that resets the server.
static String KEY_PERSISTENT
          Datalength.
static String KEY_READRETRY
          Read retries on I/O Errors.
static String KEY_RESETCADENCE
          The time-out in ms allowed for successive resets.
static String KEY_RESETTIMEOUT
          The time-out in ms allowed for the reset.
static String KEY_RETRYCOOLDOWN
          I/O Errors retry cooldown.
static String KEY_WRITERETRY
          Write retries on I/O Errors.
private  Date lastfail
          Last read failure, cleared on success.
private  Date lastreset
          Last successful reset.
private  Object LOCK
          As a very last resort, a global lock.
private  DataOutputStream outdata
          The output stream of the socket is wrapped into a data output stream.
static int OWNET_FLAG_D_F_I
          format as f.i (10.67C6697351FF)
static int OWNET_FLAG_D_F_I_C
          format as f.i.c (10.67C6697351FF.8D)
static int OWNET_FLAG_D_F_IC
          format as f.ic (10.67C6697351FF8D)
static int OWNET_FLAG_D_FI
          format as fi (1067C6697351FF)
static int OWNET_FLAG_D_FI_C
          format as fi.c (1067C6697351FF.8D)
static int OWNET_FLAG_D_FIC
          format as fic (1067C6697351FF8D)
private static int OWNET_FLAG_PERSIST
           
static int OWNET_FLAG_T_C
          Temperature format: (C) Centigrade
static int OWNET_FLAG_T_F
          Temperature format: (F) Fahrenheit
static int OWNET_FLAG_T_K
          Temperature format: (K) Kelvin
static int OWNET_FLAG_T_R
          Temperature format: (R) Rankine
private static int OWNET_MSG_DIR
           
private static int OWNET_MSG_DIRALL
           
private static int OWNET_MSG_ERROR
           
private static int OWNET_MSG_GET
           
private static int OWNET_MSG_NOP
           
private static int OWNET_MSG_PRESENCE
           
private static int OWNET_MSG_READ
           
private static int OWNET_MSG_READ_ANY
           
private static int OWNET_MSG_SIZE
           
private static int OWNET_MSG_WRITE
           
private static int OWNET_PROT_DATALEN
           
private static int OWNET_PROT_FLAGS
           
private static int OWNET_PROT_FUNCTION
           
private static int OWNET_PROT_OFFSET
           
private static int OWNET_PROT_PAYLOAD
           
private static int OWNET_PROT_RETVALUE
           
private static int OWNET_PROT_STRUCT_SIZE
           
private static int OWNET_PROT_VERSION
           
private  boolean persistent
          If true, connection is configured to be persistent.
 
Fields inherited from class io.AbstractSocketDriver
KEY_CLOSETIMEOUT, KEY_SOCKETPORT, KEY_SOCKETSERVER, KEY_SOCKETTIMEOUT
 
Fields inherited from class io.AbstractDriver
KEY_DRIVERNAME
 
Fields inherited from class util.PropertyBundles
KEY_LOCALECOUNTRY, KEY_LOCALELANGUAGE, KEY_RESOURCEBUNDLES
 
Fields inherited from class util.PropertyResources
KEY_NOINITONCREATE, localurl, locate, POSTFIX_DIR, POSTFIX_EXT, POSTFIX_FILE, POSTFIX_LIST, POSTFIX_URL, urlset
 
Fields inherited from class util.PropertyContainer
KEY_LISTSEPARATOR, KEY_MAPKEYVALUECHAR, KEY_MAPSEPARATOR
 
Fields inherited from interface stella.ow.OneWireDriver
INVALID, OK, VALID
 
Fields inherited from interface util.ResourceSupplying
KEY_URLRESOURCES, KEY_URLUSECONFIG, KEY_URLUSECURRENT, KEY_URLUSEHOME
 
Fields inherited from interface util.PropertySupplying
CONFIG, KEY_CLASS
 
Constructor Summary
ServerClient(Map<String,String> prop)
          Create a new instance of OWNet OWNet autoconnects to remote server when requested.
 
Method Summary
protected  boolean authenticate()
          On authenticate, we wrap our in/output stream.
private  int[] getPacket()
          Get a ownet message packet
private  String getPacketData(int[] packetHeader)
          Get a data packet from remote server
 boolean isOpen()
          Returns true if we are not persistent, otherwise we return the super value.
 int issue(String path, String value)
          Issuses a command.
 boolean open()
          Connect to remote host.
private  List owDir(String path, boolean dirall)
          Internal method to get directory list from server (multipacket mode)
private  boolean owPresence(String path)
          Internal method to get presence state from server
private  String owRead(String path)
          Internal method to read from server, using default excpected datalen
private  String owRead(String path, int expectedDataLen)
          Internal method to read from server
private  boolean owWrite(String path, String value)
          Internal method to write to server
 boolean reset()
          Resets the owserver by issuing a script.
 String retrieve(String path)
          The interface method for retrieving a sensor reading.
private  String retrieveCacheless(String path)
          Returns path without cache lookup.
private  void sendCString(String str, boolean termzero)
          Transmit a Java String as a C-String (null terminated)
private  void sendPacket(int function, int payloadlen, int datalen)
          Send a ownet message packet
 
Methods inherited from class io.AbstractSocketDriver
close, exit, getInputStream, getOutputStream, getSocket, init, setTimeout
 
Methods inherited from class io.AbstractDriver
createDriver, equals, getDriverName, hashCode
 
Methods inherited from class util.PropertyBundles
clone, getLocalized, getLocalized, getLocalizedString, getLocalizedString, loadResource
 
Methods inherited from class util.PropertyResources
createFrom, createFrom, createFrom, getApplet, getAsResources, getLocalClassLoader, getPropertiesToKey, getPropertiesToKey, getResource, getResourceAsStream, getResourceFromKey, getResources, keyCreate, keyCreate, reload, setApplet
 
Methods inherited from class util.PropertyContainer
augment, augment, augment, defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsEnums, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, isNew, parseObject, reload, removeProperty, rescanned, setObject, setProperties, setProperty, stringProperties, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface io.Driver
close, getDriverName
 
Methods inherited from interface util.ResourceSupplying
getResource, getResourceAsStream, getResources
 
Methods inherited from interface util.PropertySupplying
defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, parseObject, removeProperty, setObject, setProperty, stringProperties
 

Field Detail

KEY_OWRESET

public static final String KEY_OWRESET
Points to the path of an executable script that resets the server.

See Also:
Constant Field Values

KEY_RESETTIMEOUT

public static final String KEY_RESETTIMEOUT
The time-out in ms allowed for the reset.

See Also:
Constant Field Values

KEY_RESETCADENCE

public static final String KEY_RESETCADENCE
The time-out in ms allowed for successive resets.

See Also:
Constant Field Values

KEY_FORMATFLAGS

public static final String KEY_FORMATFLAGS
Formatting flags.

See Also:
Constant Field Values

KEY_DATALENGTH

public static final String KEY_DATALENGTH
Datalength.

See Also:
Constant Field Values

KEY_PERSISTENT

public static final String KEY_PERSISTENT
Datalength.

See Also:
Constant Field Values

KEY_WRITERETRY

public static final String KEY_WRITERETRY
Write retries on I/O Errors.

See Also:
Constant Field Values

KEY_READRETRY

public static final String KEY_READRETRY
Read retries on I/O Errors.

See Also:
Constant Field Values

KEY_RETRYCOOLDOWN

public static final String KEY_RETRYCOOLDOWN
I/O Errors retry cooldown.

See Also:
Constant Field Values

KEY_BUSCOOLDOWN

public static final String KEY_BUSCOOLDOWN
Before and after any bus operation cooldown.

See Also:
Constant Field Values

KEY_CACHETIME

public static final String KEY_CACHETIME
Cache-valid for A/D convert operations.

See Also:
Constant Field Values

KEY_MAXNULLREAD

public static final String KEY_MAXNULLREAD
Problems with hanging servers always reporting OK, but never end.

See Also:
Constant Field Values

DEFSOCKETPORT

private static final int DEFSOCKETPORT
Port of the server.

See Also:
Constant Field Values

DEFFORMATFLAGS

private static final int DEFFORMATFLAGS
Formatting flags.

See Also:
Constant Field Values

DEFSOCKETTIMEOUT

private static final int DEFSOCKETTIMEOUT
Timeout.

See Also:
Constant Field Values

DEFDATALENGTH

private static final int DEFDATALENGTH
Datalength.

See Also:
Constant Field Values

DEFPERSISTENT

private static final boolean DEFPERSISTENT
Datalength.

See Also:
Constant Field Values

DEFWRITERETRY

private static final int DEFWRITERETRY
Default retries for write.

See Also:
Constant Field Values

DEFREADRETRY

private static final int DEFREADRETRY
Default retries for write.

See Also:
Constant Field Values

DEFRETRYCOOLDOWN

private static final long DEFRETRYCOOLDOWN
Default retries for write.

See Also:
Constant Field Values

DEFBUSCOOLDOWN

private static final long DEFBUSCOOLDOWN
Default retries for write.

See Also:
Constant Field Values

DEFCACHETIME

private static final long DEFCACHETIME
Default retries for write.

See Also:
Constant Field Values

DEFMAXNULLREAD

private static final int DEFMAXNULLREAD
Maximum number of allowed null payload reads before throwing IOExc.

See Also:
Constant Field Values

DEFRESETTIMEOUT

private static final long DEFRESETTIMEOUT
Default timeout allowed for the reset.

See Also:
Constant Field Values

DEFRESETCADENCE

private static final long DEFRESETCADENCE
Default timeout allowed for the reset.

See Also:
Constant Field Values

OWNET_MSG_ERROR

private static final int OWNET_MSG_ERROR
See Also:
Constant Field Values

OWNET_MSG_NOP

private static final int OWNET_MSG_NOP
See Also:
Constant Field Values

OWNET_MSG_READ

private static final int OWNET_MSG_READ
See Also:
Constant Field Values

OWNET_MSG_WRITE

private static final int OWNET_MSG_WRITE
See Also:
Constant Field Values

OWNET_MSG_DIR

private static final int OWNET_MSG_DIR
See Also:
Constant Field Values

OWNET_MSG_SIZE

private static final int OWNET_MSG_SIZE
See Also:
Constant Field Values

OWNET_MSG_PRESENCE

private static final int OWNET_MSG_PRESENCE
See Also:
Constant Field Values

OWNET_MSG_DIRALL

private static final int OWNET_MSG_DIRALL
See Also:
Constant Field Values

OWNET_MSG_GET

private static final int OWNET_MSG_GET
See Also:
Constant Field Values

OWNET_MSG_READ_ANY

private static final int OWNET_MSG_READ_ANY
See Also:
Constant Field Values

OWNET_PROT_STRUCT_SIZE

private static final int OWNET_PROT_STRUCT_SIZE
See Also:
Constant Field Values

OWNET_PROT_VERSION

private static final int OWNET_PROT_VERSION
See Also:
Constant Field Values

OWNET_PROT_PAYLOAD

private static final int OWNET_PROT_PAYLOAD
See Also:
Constant Field Values

OWNET_PROT_FUNCTION

private static final int OWNET_PROT_FUNCTION
See Also:
Constant Field Values

OWNET_PROT_RETVALUE

private static final int OWNET_PROT_RETVALUE
See Also:
Constant Field Values

OWNET_PROT_FLAGS

private static final int OWNET_PROT_FLAGS
See Also:
Constant Field Values

OWNET_PROT_DATALEN

private static final int OWNET_PROT_DATALEN
See Also:
Constant Field Values

OWNET_PROT_OFFSET

private static final int OWNET_PROT_OFFSET
See Also:
Constant Field Values

OWNET_FLAG_D_F_I

public static final int OWNET_FLAG_D_F_I
format as f.i (10.67C6697351FF)

See Also:
Constant Field Values

OWNET_FLAG_D_FI

public static final int OWNET_FLAG_D_FI
format as fi (1067C6697351FF)

See Also:
Constant Field Values

OWNET_FLAG_D_F_I_C

public static final int OWNET_FLAG_D_F_I_C
format as f.i.c (10.67C6697351FF.8D)

See Also:
Constant Field Values

OWNET_FLAG_D_F_IC

public static final int OWNET_FLAG_D_F_IC
format as f.ic (10.67C6697351FF8D)

See Also:
Constant Field Values

OWNET_FLAG_D_FI_C

public static final int OWNET_FLAG_D_FI_C
format as fi.c (1067C6697351FF.8D)

See Also:
Constant Field Values

OWNET_FLAG_D_FIC

public static final int OWNET_FLAG_D_FIC
format as fic (1067C6697351FF8D)

See Also:
Constant Field Values

OWNET_FLAG_T_C

public static final int OWNET_FLAG_T_C
Temperature format: (C) Centigrade

See Also:
Constant Field Values

OWNET_FLAG_T_F

public static final int OWNET_FLAG_T_F
Temperature format: (F) Fahrenheit

See Also:
Constant Field Values

OWNET_FLAG_T_K

public static final int OWNET_FLAG_T_K
Temperature format: (K) Kelvin

See Also:
Constant Field Values

OWNET_FLAG_T_R

public static final int OWNET_FLAG_T_R
Temperature format: (R) Rankine

See Also:
Constant Field Values

OWNET_FLAG_PERSIST

private static final int OWNET_FLAG_PERSIST
See Also:
Constant Field Values

indata

private DataInputStream indata
The input stream of the socket is wrapped into a data input stream.


outdata

private DataOutputStream outdata
The output stream of the socket is wrapped into a data output stream.


persistent

private boolean persistent
If true, connection is configured to be persistent.


LOCK

private final Object LOCK
As a very last resort, a global lock.


cache

private static transient Map<String,String> cache
A cache for diverse path's.


cachetime

private static transient Map<String,Long> cachetime
The attached time cache.


lastfail

private Date lastfail
Last read failure, cleared on success.


lastreset

private Date lastreset
Last successful reset.

Constructor Detail

ServerClient

public ServerClient(Map<String,String> prop)
Create a new instance of OWNet OWNet autoconnects to remote server when requested. Default server:port are localhost:4304 (127.0.0.1:4304). If others than default are requested, set them using the setters setRemoteServer/setRemotePort, before performing any read/write opertions to OW.

Method Detail

reset

public boolean reset()
              throws IOException
Resets the owserver by issuing a script. Only possible if owserver is running on the same host as this client. Calls a system process indicated by KEY_OWRESET

Specified by:
reset in interface Resettable
Throws:
IOException

open

public boolean open()
             throws IOException
Connect to remote host. If we have already connected in persistent mode, reconnection is discarded unless the remote has close the connection. If no previous connection has been made, or previous connection was not persistent, we follow a disconnect/connect sequence.

Specified by:
open in interface Driver
Overrides:
open in class AbstractSocketDriver
Returns:
True if initialization was successful.
Throws:
IOException - Exception to throw on connection error

isOpen

public boolean isOpen()
Returns true if we are not persistent, otherwise we return the super value.

Specified by:
isOpen in interface Driver
Overrides:
isOpen in class AbstractSocketDriver
Returns:
True, if a previous initialization was successful

authenticate

protected boolean authenticate()
                        throws IOException
On authenticate, we wrap our in/output stream.

Overrides:
authenticate in class AbstractSocketDriver
Returns:
True if authentication was successful.
Throws:
IOException

retrieve

public String retrieve(String path)
The interface method for retrieving a sensor reading.

Specified by:
retrieve in interface OneWireDriver
Parameters:
path - The path to the one-wire sensor.

retrieveCacheless

private String retrieveCacheless(String path)
Returns path without cache lookup.


issue

public int issue(String path,
                 String value)
Issuses a command.

Specified by:
issue in interface OneWireDriver
Parameters:
path - The path to the one-wire sensor.
value - The command to send to the one-wire sensor.

sendPacket

private void sendPacket(int function,
                        int payloadlen,
                        int datalen)
                 throws IOException
Send a ownet message packet

Parameters:
function - ownet's header field
payloadlen - ownet's header field
datalen - ownet's header field
Throws:
IOException - Exception to throw, when something is wrong

getPacket

private int[] getPacket()
                 throws IOException
Get a ownet message packet

Returns:
the ownet's received header packet from server
Throws:
IOException - Exception to throw, when something is wrong

getPacketData

private String getPacketData(int[] packetHeader)
                      throws IOException
Get a data packet from remote server

Parameters:
packetHeader - ownet's header msg packet to use for receiving auxilary data
Returns:
data received from remote server
Throws:
IOException - Exception to throw, when something is wrong

sendCString

private void sendCString(String str,
                         boolean termzero)
                  throws IOException
Transmit a Java String as a C-String (null terminated)

Parameters:
str - string to transmit
Throws:
IOException - Exception to throw, when something is wrong

owRead

private String owRead(String path,
                      int expectedDataLen)
               throws IOException
Internal method to read from server

Parameters:
path - attribute's path
expectedDataLen - expected length of data to receive
Returns:
attribute's value
Throws:
IOException - Exception to throw, when something is wrong

owRead

private String owRead(String path)
               throws IOException
Internal method to read from server, using default excpected datalen

Parameters:
path - attribute's path
Returns:
attribute's value
Throws:
IOException - Exception to throw, when something is wrong

owWrite

private boolean owWrite(String path,
                        String value)
                 throws IOException
Internal method to write to server

Parameters:
path - attribute's path
value - value to set
Returns:
sucess state (most likely to be true or exception)
Throws:
IOException - Exception to throw, when something is wrong

owPresence

private boolean owPresence(String path)
                    throws IOException
Internal method to get presence state from server

Parameters:
path - element to check
Returns:
presence
Throws:
IOException - Exception to throw, when something is wrong

owDir

private List owDir(String path,
                   boolean dirall)
            throws IOException
Internal method to get directory list from server (multipacket mode)

Parameters:
path - directory to list
Returns:
list of elements found
Throws:
IOException - Exception to throw, when something is wrong