public class AWSIotDevice extends AbstractAwsIotDevice
AWSIotDevice
represents a device that is one-to-one mapped with the
AWS IoT device shadow. The linkage is created through the shadow name that is
passed into the constructor.
There are two typical ways of using AWSIotDevice
. One is to extend
AWSIotDevice
and provide device attributes that are to be
synchronized with the shadow and their accessor methods (getters and
setters). The other way is to use the get/update/delete methods provided in
this class to directly access the shadow document. The first approach is easy
to implement and should work for most of the use cases; the second approach
provides the user the ability of directly accessing the data (document)
stored on the device shadow, which is very flexible, however the user is
responsible for parsing the shadow document encoded in JSON, and providing
shadow-compatible document in update calls. It's also possible to use both
approaches in a same application.
To leverage the synchronization function provided by the library, one needs
to extend AWSIotDevice
. Device attributes that are to be kept in sync
with the shadow must be annotated with AWSIotDeviceProperty
. One
should also provide getter functions for these annotated attributes to be
reported to the shadow as well as setter functions to accept updates from the
shadow. A simplified example is like this
public class SomeDevice extends AWSIotDevice { @AWSIotDeviceProperty boolean switch; public boolean getSwitch() { // read from the device and return the value to be reported to the shadow return ...; } public void setSwitch(boolean requestedState) { // write to the device with the requested value from the shadow } }
To linked the above class with the shadow, one could do like so
AWSIotMqttClient client = new AWSIotMqttClient(...); SomeDevice someDevice = new SomeDevice(thingName); client.attach(someDevice); client.connect();
To access the shadow directly, one could do as the below. All the methods in this class are thread-safe, therefore can be called in different user threads.
AWSIotMqttClient client = new AWSIotMqttClient(...); AWSIotDevice awsIotDevice = new AWSIotDevice(thingName); client.attach(awsIotDevice); client.connect(); ... String jsonDocument = awsIotDevice.get(); ... client.update(jsonDocument); ...
The library contains sample applications that demonstrate how each of these two methods can be used.
deviceReportQos, enableVersioning, methodAckQos, methodQos, reportInterval, shadowUpdateQos, thingName
Constructor and Description |
---|
AWSIotDevice(String thingName)
Instantiates a new device instance.
|
Modifier and Type | Method and Description |
---|---|
void |
delete()
Deletes the content of a thing shadow.
|
void |
delete(AWSIotMessage message,
long timeout)
Deletes the content of a thing shadow.
|
void |
delete(long timeout)
Deletes the content of a thing shadow.
|
String |
get()
Retrieves the latest state stored in the thing shadow.
|
void |
get(AWSIotMessage message,
long timeout)
Retrieves the latest state stored in the thing shadow.
|
String |
get(long timeout)
Retrieves the latest state stored in the thing shadow.
|
AWSIotQos |
getDeviceReportQos()
Gets the MQTT QoS level for publishing the device report.
|
AWSIotQos |
getMethodAckQos()
Gets the MQTT QoS level for subscribing to acknowledgement messages of
shadow methods.
|
AWSIotQos |
getMethodQos()
Gets the MQTT QoS level for sending the shadow methods, namely Get,
Update, and Delete.
|
long |
getReportInterval()
Gets the device report interval.
|
AWSIotQos |
getShadowUpdateQos()
Gets the MQTT QoS level for subscribing to shadow updates.
|
boolean |
isEnableVersioning()
Checks if versioning is enabled for device updates.
|
String |
onDeviceReport()
This function handles collecting device data for reporting to the shadow.
|
void |
onShadowUpdate(String jsonState)
This function handles update messages received from the shadow.
|
void |
setDeviceReportQos(AWSIotQos deviceReportQos)
Sets the MQTT QoS level for publishing the device report.
|
void |
setEnableVersioning(boolean enableVersioning)
Sets the device update versioning to be enabled or disabled.
|
void |
setMethodAckQos(AWSIotQos methodAckQos)
Sets the MQTT QoS level for subscribing to acknowledgement messages of
shadow methods.
|
void |
setMethodQos(AWSIotQos methodQos)
Sets the MQTT QoS level for sending shadow methods.
|
void |
setReportInterval(long reportInterval)
Sets the device report interval in milliseconds.
|
void |
setShadowUpdateQos(AWSIotQos shadowUpdateQos)
Sets the MQTT QoS level for subscribing to shadow updates.
|
void |
update(AWSIotMessage message,
long timeout)
Updates the content of a thing shadow with the data provided in the
request.
|
void |
update(String jsonState)
Updates the content of a thing shadow with the data provided in the
request.
|
void |
update(String jsonState,
long timeout)
Updates the content of a thing shadow with the data provided in the
request.
|
activate, deactivate, getClient, getCommandManager, getDevice, getDeviceSubscriptions, getJsonObjectMapper, getLocalVersion, getReportedProperties, getSyncTask, getThingName, getUpdatableProperties, isCommandReady, isTopicReady, onCommandAck, onSubscriptionAck, setClient, setLocalVersion, setSyncTask, startSync, startVersionSync, stopSync
public AWSIotDevice(String thingName)
thingName
- the thing namepublic long getReportInterval()
getReportInterval
in class AbstractAwsIotDevice
public void setReportInterval(long reportInterval)
AWSIotMqttClient.attach(AWSIotDevice)
call. The default interval
is 3,000ms. Setting it to 0 will disable reporting.setReportInterval
in class AbstractAwsIotDevice
reportInterval
- the new report intervalpublic boolean isEnableVersioning()
isEnableVersioning
in class AbstractAwsIotDevice
public void setEnableVersioning(boolean enableVersioning)
AWSIotMqttClient.attach(AWSIotDevice)
call.setEnableVersioning
in class AbstractAwsIotDevice
enableVersioning
- true to enable device update versioning; false to disable.public AWSIotQos getDeviceReportQos()
getDeviceReportQos
in class AbstractAwsIotDevice
public void setDeviceReportQos(AWSIotQos deviceReportQos)
AWSIotMqttClient.attach(AWSIotDevice)
call.setDeviceReportQos
in class AbstractAwsIotDevice
deviceReportQos
- the new device report QoSpublic AWSIotQos getShadowUpdateQos()
getShadowUpdateQos
in class AbstractAwsIotDevice
public void setShadowUpdateQos(AWSIotQos shadowUpdateQos)
AWSIotMqttClient.attach(AWSIotDevice)
call.setShadowUpdateQos
in class AbstractAwsIotDevice
shadowUpdateQos
- the new shadow update QoSpublic AWSIotQos getMethodQos()
getMethodQos
in class AbstractAwsIotDevice
public void setMethodQos(AWSIotQos methodQos)
AWSIotMqttClient.attach(AWSIotDevice)
call.setMethodQos
in class AbstractAwsIotDevice
methodQos
- the new QoS level for sending shadow methods.public AWSIotQos getMethodAckQos()
getMethodAckQos
in class AbstractAwsIotDevice
public void setMethodAckQos(AWSIotQos methodAckQos)
AWSIotMqttClient.attach(AWSIotDevice)
call.setMethodAckQos
in class AbstractAwsIotDevice
methodAckQos
- the new QoS level for subscribing to acknowledgement messages.public String get() throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
get
in class AbstractAwsIotDevice
AWSIotException
- exception thrown if the operation failspublic String get(long timeout) throws AWSIotException, AWSIotTimeoutException
get
in class AbstractAwsIotDevice
timeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void get(AWSIotMessage message, long timeout) throws AWSIotException
AWSIotMessage.onSuccess()
,
AWSIotMessage.onFailure()
, and AWSIotMessage.onTimeout()
, one
of which will be invoked after the operation succeeded, failed, or timed
out respectively.get
in class AbstractAwsIotDevice
message
- the message object contains callback functions; if the call is
successful, the full JSON document of the device state will be
stored in the payload
field of message
.timeout
- the timeout in milliseconds for the operation to be considered
timed outAWSIotException
- exception thrown if the operation failspublic void update(String jsonState) throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
update
in class AbstractAwsIotDevice
jsonState
- the JSON document of the new device stateAWSIotException
- exception thrown if the operation failspublic void update(String jsonState, long timeout) throws AWSIotException, AWSIotTimeoutException
update
in class AbstractAwsIotDevice
jsonState
- the JSON document of the new device statetimeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void update(AWSIotMessage message, long timeout) throws AWSIotException
AWSIotMessage.onSuccess()
, AWSIotMessage.onFailure()
, and
AWSIotMessage.onTimeout()
, one of which will be invoked after the
operation succeeded, failed, or timed out respectively.update
in class AbstractAwsIotDevice
message
- the message object contains callback functionstimeout
- the timeout in milliseconds for the operation to be considered
timed outAWSIotException
- exception thrown if the operation failspublic void delete() throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
delete
in class AbstractAwsIotDevice
AWSIotException
- exception thrown if the operation failspublic void delete(long timeout) throws AWSIotException, AWSIotTimeoutException
delete
in class AbstractAwsIotDevice
timeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void delete(AWSIotMessage message, long timeout) throws AWSIotException
AWSIotMessage.onSuccess()
,
AWSIotMessage.onFailure()
, and AWSIotMessage.onTimeout()
, one
of which will be invoked after the operation succeeded, failed, or timed
out respectively.delete
in class AbstractAwsIotDevice
message
- the message object contains callback functionstimeout
- the timeout in milliseconds for the operation to be considered
timed outAWSIotException
- exception thrown if the operation failspublic void onShadowUpdate(String jsonState)
onShadowUpdate
in class AbstractAwsIotDevice
jsonState
- the JSON document containing the delta between 'desired' and
'reported' statespublic String onDeviceReport()
onDeviceReport
in class AbstractAwsIotDevice
Copyright © 2020. All rights reserved.