Package org.openwebnet4j
Class OpenGateway
- java.lang.Object
-
- org.openwebnet4j.OpenGateway
-
- All Implemented Interfaces:
ConnectorListener
- Direct Known Subclasses:
BUSGateway
,USBGateway
public abstract class OpenGateway extends Object implements ConnectorListener
Abstract OpenGateway to connect, send commands and receive events from a OpenWebNet gateway- Author:
- M. Valla - Initial contribution
-
-
Field Summary
Fields Modifier and Type Field Description protected OpenConnector
connector
protected boolean
isConnected
protected boolean
isDiscovering
protected ArrayList<GatewayListener>
listeners
protected byte[]
macAddr
-
Constructor Summary
Constructors Constructor Description OpenGateway()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
closeConnection()
Closes connection to the gateway and releases resourcesvoid
connect()
Connect to the OpenWebNet gateway.void
discoverDevices()
Start a device discovery session and notifyGatewayListener
s for each new device discovered calling methodGatewayListener.onNewDevice(org.openwebnet4j.message.Where, org.openwebnet4j.OpenDeviceType, org.openwebnet4j.message.BaseOpenMessage)
protected abstract void
discoverDevicesInternal()
String
getFirmwareVersion()
Returns the firmware version of the gateway (e.g.String
getMACAddr()
Returns the MAC address of the gateway as human readable Stringprotected abstract void
initConnector()
Init the connector for this OpenGateway.abstract boolean
isCmdConnectionReady()
Returns true if CMD connection is ready to send messages (connector must be connected and in case of BUS connection checks if a CMD was sent recently < 120sec)boolean
isConnected()
Returns true if this OpenGateway is connected.boolean
isDiscovering()
Check if the gateway has started a device discovery sessionprotected void
notifyListeners(Consumer<? super GatewayListener> method)
Generic method to notify registered OpenListener about 'method' event.void
onMessage(OpenMessage message)
This method is called when a newOpenMessage
is received by the OpenConnector MONITOR connectionvoid
onMonDisconnected(OWNException e)
This method is called when the MONITOR connection gets disconnected for some errorvoid
reconnect()
Tries to reconnect to the OpenWebNet gateway, waiting increasing time intervals.Response
send(OpenMessage msg)
Send a command message, and returns the response messagesResponse
sendHighPriority(OpenMessage msg)
Send a command message with high priority, and returns the response messagesprotected Response
sendInternal(OpenMessage msg)
void
subscribe(GatewayListener listener)
Add a listener for events from this OpenGateway.void
unsubscribe(GatewayListener listener)
Remove a listener for events from this OpenGateway.
-
-
-
Field Detail
-
isConnected
protected boolean isConnected
-
isDiscovering
protected boolean isDiscovering
-
listeners
protected final ArrayList<GatewayListener> listeners
-
connector
protected OpenConnector connector
-
macAddr
protected byte[] macAddr
-
-
Method Detail
-
initConnector
protected abstract void initConnector()
Init the connector for this OpenGateway.
-
connect
public void connect() throws OWNException
Connect to the OpenWebNet gateway.- Throws:
OWNException
- in case of error during connection
-
isConnected
public boolean isConnected()
Returns true if this OpenGateway is connected.- Returns:
- true if this OpenGateway is connected
-
reconnect
public void reconnect() throws OWNAuthException
Tries to reconnect to the OpenWebNet gateway, waiting increasing time intervals.GatewayListener.onConnectionError(org.openwebnet4j.communication.OWNException)
is called each time a connection is tried and failsGatewayListener.onReconnected()
is called when reconnection is successful. To stop trying, callcloseConnection()
.- Throws:
OWNAuthException
- in case of auth error (reconnect is stopped)
-
send
public Response send(OpenMessage msg) throws OWNException
Send a command message, and returns the response messages- Parameters:
msg
- theOpenMessage
to be sent- Returns:
- the
Response
messages received as response - Throws:
OWNException
- on send/response reading error
-
sendHighPriority
public Response sendHighPriority(OpenMessage msg) throws OWNException
Send a command message with high priority, and returns the response messages- Parameters:
msg
- theOpenMessage
to be sent- Returns:
- the
Response
messages received as response - Throws:
OWNException
- on send/response reading error
-
sendInternal
protected Response sendInternal(OpenMessage msg) throws OWNException
- Throws:
OWNException
-
isCmdConnectionReady
public abstract boolean isCmdConnectionReady()
Returns true if CMD connection is ready to send messages (connector must be connected and in case of BUS connection checks if a CMD was sent recently < 120sec)- Returns:
- boolean
-
onMessage
public void onMessage(OpenMessage message)
Description copied from interface:ConnectorListener
This method is called when a newOpenMessage
is received by the OpenConnector MONITOR connection- Specified by:
onMessage
in interfaceConnectorListener
- Parameters:
message
- theOpenMessage
received
-
onMonDisconnected
public void onMonDisconnected(OWNException e)
Description copied from interface:ConnectorListener
This method is called when the MONITOR connection gets disconnected for some error- Specified by:
onMonDisconnected
in interfaceConnectorListener
- Parameters:
e
- the Exception that caused disconnection
-
subscribe
public void subscribe(GatewayListener listener)
Add a listener for events from this OpenGateway.- Parameters:
listener
- theGatewayListener
to add
-
unsubscribe
public void unsubscribe(GatewayListener listener)
Remove a listener for events from this OpenGateway.- Parameters:
listener
- theGatewayListener
to remove.
-
notifyListeners
protected void notifyListeners(Consumer<? super GatewayListener> method)
Generic method to notify registered OpenListener about 'method' event. Thread safe. A (single) notification thread is used.- Parameters:
method
- the method to be notified
-
discoverDevices
public void discoverDevices() throws OWNException
Start a device discovery session and notifyGatewayListener
s for each new device discovered calling methodGatewayListener.onNewDevice(org.openwebnet4j.message.Where, org.openwebnet4j.OpenDeviceType, org.openwebnet4j.message.BaseOpenMessage)
- Throws:
OWNException
- in case of error while discovering
-
isDiscovering
public boolean isDiscovering()
Check if the gateway has started a device discovery session- Returns:
- true if the gateway has started a device discovery session
-
discoverDevicesInternal
protected abstract void discoverDevicesInternal() throws OWNException
- Throws:
OWNException
-
getFirmwareVersion
public String getFirmwareVersion()
Returns the firmware version of the gateway (e.g. 1.2.3)- Returns:
- String containing gateway firmware version, null if unknown
-
getMACAddr
public String getMACAddr()
Returns the MAC address of the gateway as human readable String- Returns:
- gateway MAC address as String (XX:YY:ZZ:...), or null if unknown
-
closeConnection
public void closeConnection()
Closes connection to the gateway and releases resources
-
-