public abstract class AbstractDriver<M> extends java.lang.Object implements Driver, DriverTagManager
Driver
that handles the complexities of maintaining a subscription model, a
request schedule, managing a request cycle, and the DriverState
.
Implementations then need only worry about how to optimize and create Request
's for reading, writing, and
browsing as well as handling calls to connect and disconnect.Modifier and Type | Field and Description |
---|---|
protected NodeBuilderFactory |
builderFactory |
protected DriverDiagnosticsImpl |
diagnosticsContext |
protected java.util.concurrent.ScheduledExecutorService |
executor |
protected LoggerEx |
log |
protected NodeManager |
nodeManager |
Constructor and Description |
---|
AbstractDriver(DriverContext driverContext) |
Modifier and Type | Method and Description |
---|---|
void |
addDriverTag(DriverTag tag)
Add a
DriverTag . |
void |
browse(BrowseOperation browseOp)
Asynchronous browse operation.
|
protected abstract void |
connect()
A chance for implementations to do anything that is needed before moving into
DriverState.Connected . |
protected abstract Request<M> |
createBrowseRequest(BrowseOperation browseOp)
Create a browse
Request for the given BrowseOperation . |
protected abstract Request<M> |
createReadRequest(java.util.List<? extends ReadItem> items)
Create a read
Request with the given items. |
protected Request<M> |
createSubscribeRequest(java.util.List<? extends ReadItem> items)
Create a read
Request with the given items. |
protected abstract Request<M> |
createWriteRequest(java.util.List<? extends WriteItem> items)
Create a write
Request with the given items. |
Guid |
currentGuid() |
protected abstract void |
disconnect()
A chance for implementations to do anything that is needed before moving into
DriverState.Disconnected . |
DriverTag |
findTag(java.lang.String address)
Find a
DriverTag with the given address, if it exists. |
int |
getConcurrentRequests() |
DriverDiagnostics |
getDiagnostics() |
DriverContext |
getDriverContext() |
java.util.Optional<DriverMetrics> |
getDriverMetrics()
If this driver uses a traditional polling model, a
DriverMetrics . |
DriverState |
getDriverState() |
java.lang.String |
getDriverStatus()
Returns the current status of the driver.
|
java.lang.String |
getDriverTagAddressPrefix() |
LoggerEx |
getLogger() |
java.lang.String |
getName() |
RequestDiagnostics |
getRequestDiagnostics(java.lang.String address) |
protected abstract java.lang.Object |
getRequestKey(M message)
Extract the request key from the given message.
|
protected abstract boolean |
isBrowsingSupported() |
protected abstract boolean |
isOfflineBrowsingSupported() |
protected void |
messageArrived(M message)
Notify
AbstractDriver that a messaged arrived. |
Guid |
nextGuid() |
void |
notifyConnectFailed(java.lang.Exception connectError)
Callback indicating that the result of
connect() was a failure. |
void |
notifyConnectionLost()
Notify
AbstractDriver that the connection has unintentionally been lost. |
void |
notifyConnectSucceeded()
Callback indicating that the result of
connect() was a success. |
void |
notifyDisconnectDone()
A callback to notify
AbstractDriver that a call to disconnect() is done. |
protected abstract java.util.List<java.util.List<? extends ReadItem>> |
optimizeRead(java.util.List<? extends ReadItem> items)
Optimize the given list of items into sub-lists, where each sub-list is expected to fit into a single
Request when createReadRequest(List) is called. |
protected java.util.List<java.util.List<? extends ReadItem>> |
optimizeSubscribe(java.util.List<? extends ReadItem> items)
Optimize the given list of items into sub-lists, where each sub-list is expected to fit into a single
Request when createSubscribeRequest(List) is called. |
protected java.util.List<java.util.List<? extends WriteItem>> |
optimizeWrite(java.util.List<? extends WriteItem> items)
Optimize the given list of items into sub-lists where each sub-list is expected to fit into a single
Request when
createWriteRequest(List) is called. |
void |
postRequest(Request<M> request,
int delay)
Post a
Request . |
void |
readItems(java.util.List<? extends ReadItem> items)
Asynchronous read call for a list of items.
|
void |
removeDriverTag(java.lang.String address)
Removes the
DriverTag at the given address. |
void |
reschedule()
Reschedule all the current
ScheduledRequest s. |
void |
setConcurrentRequests(int concurrentRequests)
Sets the allowed number of concurrent requests.
|
protected void |
setState(DriverState state) |
void |
shutdown()
Called when a driver should cease all activities and shutdown any open connections.
|
void |
writeItems(java.util.List<? extends WriteItem> items)
Asynchronous write call for a list of items.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
buildNode, getDriverStatusJSLibName, getDriverStatusJSUrl
protected DriverDiagnosticsImpl diagnosticsContext
protected LoggerEx log
protected java.util.concurrent.ScheduledExecutorService executor
protected NodeManager nodeManager
protected NodeBuilderFactory builderFactory
public AbstractDriver(DriverContext driverContext)
public void readItems(java.util.List<? extends ReadItem> items)
Driver
ReadItem.setValue(DataValue)
.public void writeItems(java.util.List<? extends WriteItem> items)
Driver
WriteItem.setWriteStatus(StatusCode)
.writeItems
in interface Driver
public void browse(BrowseOperation browseOp)
Driver
BrowseOperation.browseDone(StatusCode, List, Guid)
.public void shutdown()
Driver
public final DriverState getDriverState()
public java.lang.String getDriverStatus()
Driver
getDriverStatus
in interface Driver
public java.util.Optional<DriverMetrics> getDriverMetrics()
Driver
DriverMetrics
.getDriverMetrics
in interface Driver
DriverMetrics
if supported by this driver.protected abstract void connect()
DriverState.Connected
. When
done, notifyConnectSucceeded()
or notifyConnectFailed(Exception)
must be called.protected abstract void disconnect()
DriverState.Disconnected
.
When done, notifyDisconnectDone()
must be called.public void notifyConnectSucceeded()
connect()
was a success.
Clears the last connect error, sets DriverState.Connected, and starts any subscriptions.public void notifyConnectFailed(java.lang.Exception connectError)
connect()
was a failure.
The contents of connectError
will be displayed to the user on the device status page of the Ignition
gateway.connectError
- A user-facing error message that will be displayed on the device status page of
the Ignition gateway.public void notifyDisconnectDone()
AbstractDriver
that a call to disconnect()
is done.
This will move the driver into DriverState.Disconnected
and cancel any outstanding
ScheduledRequest
s.public void notifyConnectionLost()
AbstractDriver
that the connection has unintentionally been lost. It is assumed that because the
connection was lost, disconnect()
does not need to be called.
If DriverState
is not currently DriverState.Disconnected
then the following occurs:
DriverState
is set to DriverState.Disconnected
.ScheduledRequest
's are canceled.connect()
will be scheduled.
protected void setState(DriverState state)
public void postRequest(Request<M> request, int delay)
Request
.request
- The Request
to post.delay
- The delay before posting.public void reschedule()
ScheduledRequest
s. This implies that all requests will be
re-optimized.protected void messageArrived(M message)
AbstractDriver
that a messaged arrived. This cannot be called on the same
thread that Request.sendMessage()
was invoked on.message
- The message that has arrived.public DriverContext getDriverContext()
DriverContext
for this Driver
.public java.lang.String getName()
public LoggerEx getLogger()
Request
s.public void setConcurrentRequests(int concurrentRequests)
Request
objects that can be sent (
Request.sendMessage()
) at the same time without first receiving a response.concurrentRequests
- The allowed number of concurrent requests.public int getConcurrentRequests()
public Guid nextGuid()
public Guid currentGuid()
public DriverDiagnostics getDiagnostics()
public RequestDiagnostics getRequestDiagnostics(java.lang.String address)
public java.lang.String getDriverTagAddressPrefix()
DriverTag.getAddress()
that differentiates
DriverTag
addresses from actual device addresses.public void addDriverTag(DriverTag tag)
DriverTagManager
DriverTag
.addDriverTag
in interface DriverTagManager
tag
- The tag to add.public void removeDriverTag(java.lang.String address)
DriverTagManager
DriverTag
at the given address.
If the address points to a folder then the folder and all its children will be recursively
removed.removeDriverTag
in interface DriverTagManager
address
- The address to remove.public DriverTag findTag(java.lang.String address)
DriverTagManager
DriverTag
with the given address, if it exists.findTag
in interface DriverTagManager
address
- The address of the DriverTag
.DriverTag
for address
, or null
if none exists.protected abstract java.util.List<java.util.List<? extends ReadItem>> optimizeRead(java.util.List<? extends ReadItem> items)
Request
when createReadRequest(List)
is called.
Things to take into consideration when optimizing might be:
items
- The items to optimize.Request
.protected java.util.List<java.util.List<? extends ReadItem>> optimizeSubscribe(java.util.List<? extends ReadItem> items)
Request
when createSubscribeRequest(List)
is called.
By default, this implementation simply defers to optimizeRead(List)
. Override if you need to do anything
special when optimizing a request that will be used for a subscribe instead of a read.items
- The items to optimize.Request
.protected java.util.List<java.util.List<? extends WriteItem>> optimizeWrite(java.util.List<? extends WriteItem> items)
createWriteRequest(List)
is called.
Similar considerations as when optimizing reads should be taken.items
- The items to optimize into sub-lists.Request
.protected abstract Request<M> createReadRequest(java.util.List<? extends ReadItem> items)
Request
with the given items. These items are guaranteed to be a sub-list previously
generated by a call to optimizeRead(List)
and should therefore fit into single Request
.protected Request<M> createSubscribeRequest(java.util.List<? extends ReadItem> items)
Request
with the given items. These items are guaranteed to be a sub-list previously
generated by a call to optimizeSubscribe(List)
and should therefore fit into single Request
.
By default, this implementation simply defers to createReadRequest(List)
. Override if you need to do
anything special when creating a request that will be used for a subscribe instead of a read.protected abstract Request<M> createWriteRequest(java.util.List<? extends WriteItem> items)
Request
with the given items. These items are guaranteed to be a sub-list previously
generated by a call to optimizeWrite(List)
and should therefore fit into a single Request
.protected abstract Request<M> createBrowseRequest(BrowseOperation browseOp)
Create a browse Request
for the given BrowseOperation
.
If the implementation does not actually need to post a Request
to complete the browse then
BrowseOperation.browseDone(StatusCode, List, Guid)
can be called whenever browsing is complete and a
null value can be returned.
browseOp
- The BrowseOperation
for this Request
.Request
for the given BrowseOperation
, or null if
no Request
is necessary to complete the browse.protected abstract java.lang.Object getRequestKey(M message)
Extract the request key from the given message. This key is used to identify the Request
that this
message is a response to.
message
- The message to extract a key from.protected abstract boolean isBrowsingSupported()
protected abstract boolean isOfflineBrowsingSupported()