public class ManagedDevice extends DeviceClient implements org.eclipse.paho.client.mqttv3.IMqttMessageListener, Runnable
The device management feature enhances the Watson IoT Platform service with new capabilities for managing devices.
What does Device Management add?
This is a derived class from DeviceClient and can be used by embedded devices to perform both Device operations and Device Management operations, i.e, the devices can use this class to do the following,
commandCallbacks, messageCodecs, messageCodecsByFormatCLIENT_ID_DELIMITER, config, DEFAULT_ACTION_TIMEOUT, DEFAULT_DISCONNECTED_BUFFER_SIZE, DEFAULT_MAX_CONNECT_ATTEMPTS, DEFAULT_MAX_INFLIGHT_MESSAGES, DEFAULT_MESSAGING_QOS, disconnectRequested, gson, messageCount, mqttAsyncClient, mqttCallback, mqttClient, mqttClientOptions, persistence| Constructor and Description |
|---|
ManagedDevice(DeviceConfig config,
DeviceData deviceData)
Constructor that creates a ManagedDevice object, but does not connect to
IBM Watson IoT Platform connect yet
|
| Modifier and Type | Method and Description |
|---|---|
void |
addCustomActionHandler(CustomActionHandler actionHandler)
Adds a device action handler which is of type
CustomActionHandler |
void |
addDeviceActionHandler(DeviceActionHandler actionHandler)
Adds a device action handler which is of type
DeviceActionHandler |
int |
addErrorCode(int errorCode)
Adds the current errorcode to IBM Watson IoT Platform.
|
void |
addFirmwareHandler(DeviceFirmwareHandler fwHandler)
Adds a firmware handler for this device,
that is of type
DeviceFirmwareHandler |
int |
addLog(String message,
Date timestamp,
LogSeverity severity)
Appends a Log message to the Watson IoT Platform.
|
int |
addLog(String message,
Date timestamp,
LogSeverity severity,
String data)
The Log message that needs to be added to the Watson IoT Platform.
|
int |
clearErrorCodes()
Clear the Error Codes from IBM Watson IoT Platform for this device
|
int |
clearLogs()
Clear the Logs from IBM Watson IoT Platform for this device
|
void |
connect()
This method just connects to the IBM Watson IoT Platform,
Device needs to make a call to manage() to participate in Device
Management activities.
|
void |
disconnect()
Disconnects from IBM Watson IoT Platform
|
DeviceData |
getDeviceData() |
void |
messageArrived(String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message)
The Device client does not currently support subscriptions.
|
void |
publish(String topic,
com.google.gson.JsonObject payload) |
void |
publish(String topic,
com.google.gson.JsonObject payload,
int qos)
Publish the Device management response to IBm Watson IoT Platform
|
protected org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message) |
void |
run() |
com.google.gson.JsonObject |
sendAndWait(String topic,
com.google.gson.JsonObject jsonPayload,
long timeout)
Send the message and waits for the response from IBM Watson IoT Platform
|
boolean |
sendManageRequest(long lifetime,
boolean supportFirmwareActions,
boolean supportDeviceActions)
Send a device manage request to Watson IoT Platform
|
boolean |
sendManageRequest(long lifetime,
boolean supportFirmwareActions,
boolean supportDeviceActions,
List<String> bundleIds)
Send a device manage request to Watson IoT Platform
|
boolean |
sendManageRequest(long lifetime,
boolean supportFirmwareActions,
boolean supportDeviceActions,
String bundleId)
Send a device manage request to Watson IoT Platform
|
boolean |
sendUnmanageRequest()
Moves the device from managed state to unmanaged state
A device uses this request when it no longer needs to be managed.
|
void |
subscribe(String[] topics,
int[] qos,
org.eclipse.paho.client.mqttv3.IMqttMessageListener[] listeners)
Subscribe the given listeners to the given topics
|
void |
subscribe(String topic,
int qos,
org.eclipse.paho.client.mqttv3.IMqttMessageListener listener)
Subscribe the given listener to the given topic
|
void |
unsubscribe(String topic)
UnSubscribe the library from the given topic
|
void |
unsubscribe(String[] topics)
UnSubscribe the library from the given topics
|
int |
updateLocation(Double latitude,
Double longitude,
Double elevation)
Update the location.
|
int |
updateLocation(Double latitude,
Double longitude,
Double elevation,
Date measuredDateTime)
Update the location of the device.
|
int |
updateLocation(Double latitude,
Double longitude,
Double elevation,
Date measuredDateTime,
Double accuracy)
Update the location of the device.
|
connectComplete, connectionLost, deliveryComplete, publishEvent, publishEvent, reconnect, registerCodec, registerCommandCallbackclose, configureMqttClient, getConfig, isConnected, toStringpublic ManagedDevice(DeviceConfig config, DeviceData deviceData) throws Exception
config - Configuration for the devicedeviceData - The Device ModelException - If the essential parameters are not setpublic DeviceData getDeviceData()
public void connect()
throws org.eclipse.paho.client.mqttv3.MqttException,
KeyManagementException,
NoSuchAlgorithmException
This method just connects to the IBM Watson IoT Platform, Device needs to make a call to manage() to participate in Device Management activities.
This method does nothing if the device is already connected. Also, this method does not retry when the following exceptions occur.
connect in class DeviceClientorg.eclipse.paho.client.mqttv3.MqttException - see aboveNoSuchAlgorithmException - TLS issuesKeyManagementException - TLS issuespublic boolean sendManageRequest(long lifetime,
boolean supportFirmwareActions,
boolean supportDeviceActions,
List<String> bundleIds)
throws org.eclipse.paho.client.mqttv3.MqttException
Send a device manage request to Watson IoT Platform
A Device uses this request to become a managed device. It should be the first device management request sent by the Device after connecting to the IBM Watson IoT Platform. It would be usual for a device management agent to send this whenever is starts or restarts.
This method connects the device to Watson IoT Platform connect if its not connected already
lifetime - The length of time in seconds within
which the device must send another Manage device request.
if set to 0, the managed device will not become dormant.
When set, the minimum supported setting is 3600 (1 hour).supportFirmwareActions - Tells whether the device supports firmware actions or not.
The device must add a firmware handler to handle the firmware requests.supportDeviceActions - Tells whether the device supports Device actions or not.
The device must add a Device action handler to handle the reboot and factory reset requests.bundleIds - List of Device Management Extension bundleIdsorg.eclipse.paho.client.mqttv3.MqttException - When there is a failurepublic boolean sendManageRequest(long lifetime,
boolean supportFirmwareActions,
boolean supportDeviceActions,
String bundleId)
throws org.eclipse.paho.client.mqttv3.MqttException
Send a device manage request to Watson IoT Platform
A Device uses this request to become a managed device. It should be the first device management request sent by the Device after connecting to the IBM Watson IoT Platform. It would be usual for a device management agent to send this whenever is starts or restarts.
This method connects the device to Watson IoT Platform connect if its not connected already
lifetime - The length of time in seconds within
which the device must send another Manage device request.
if set to 0, the managed device will not become dormant.
When set, the minimum supported setting is 3600 (1 hour).supportFirmwareActions - Tells whether the device supports firmware actions or not.
The device must add a firmware handler to handle the firmware requests.supportDeviceActions - Tells whether the device supports Device actions or not.
The device must add a Device action handler to handle the reboot and factory reset requests.bundleId - Unique identifier for a device management extensionorg.eclipse.paho.client.mqttv3.MqttException - When there is a failurepublic boolean sendManageRequest(long lifetime,
boolean supportFirmwareActions,
boolean supportDeviceActions)
throws org.eclipse.paho.client.mqttv3.MqttException
Send a device manage request to Watson IoT Platform
A Device uses this request to become a managed device. It should be the first device management request sent by the Device after connecting to the IBM Watson IoT Platform. It would be usual for a device management agent to send this whenever is starts or restarts.
This method connects the device to Watson IoT Platform connect if its not connected already
lifetime - The length of time in seconds within
which the device must send another Manage device request.
if set to 0, the managed device will not become dormant.
When set, the minimum supported setting is 3600 (1 hour).supportFirmwareActions - Tells whether the device supports firmware actions or not.
The device must add a firmware handler to handle the firmware requests.supportDeviceActions - Tells whether the device supports Device actions or not.
The device must add a Device action handler to handle the reboot and factory reset requests.org.eclipse.paho.client.mqttv3.MqttException - When there is a failurepublic int updateLocation(Double latitude, Double longitude, Double elevation)
latitude - Latitude in decimal degrees using WGS84longitude - Longitude in decimal degrees using WGS84elevation - Elevation in meters using WGS84public int updateLocation(Double latitude, Double longitude, Double elevation, Date measuredDateTime)
latitude - Latitude in decimal degrees using WGS84longitude - Longitude in decimal degrees using WGS84elevation - Elevation in meters using WGS84measuredDateTime - When the location information is retrievedpublic int updateLocation(Double latitude, Double longitude, Double elevation, Date measuredDateTime, Double accuracy)
latitude - Latitude in decimal degrees using WGS84longitude - Longitude in decimal degrees using WGS84elevation - Elevation in meters using WGS84measuredDateTime - When the location information is retrievedaccuracy - Accuracy of the position in meterspublic int clearErrorCodes()
public int clearLogs()
public int addErrorCode(int errorCode)
errorCode - The "errorCode" is a current device error code that
needs to be added to the Watson IoT Platform.public int addLog(String message, Date timestamp, LogSeverity severity)
message - The Log message that needs to be added to the Watson IoT Platform.timestamp - The Log timestampseverity - the Log severitypublic int addLog(String message, Date timestamp, LogSeverity severity, String data)
message - The Log message that needs to be added to the Watson IoT Platform.timestamp - The Log timestampseverity - The Log severitydata - The optional diagnostic string data -
The library will encode the data in base64 format as required by the Platformpublic boolean sendUnmanageRequest()
throws org.eclipse.paho.client.mqttv3.MqttException
org.eclipse.paho.client.mqttv3.MqttException - When failurepublic void subscribe(String topic, int qos, org.eclipse.paho.client.mqttv3.IMqttMessageListener listener) throws org.eclipse.paho.client.mqttv3.MqttException
Subscribe the given listener to the given topic
This method is used by the library to subscribe to each of the topic where IBM Watson IoT Platform will send the DM requests
topic - topic to be subscribedqos - Quality of Service for the subscriptionlistener - The IMqttMessageListener for the given topicorg.eclipse.paho.client.mqttv3.MqttException - When subscription failspublic void subscribe(String[] topics, int[] qos, org.eclipse.paho.client.mqttv3.IMqttMessageListener[] listeners) throws org.eclipse.paho.client.mqttv3.MqttException
Subscribe the given listeners to the given topics
This method is used by the library to subscribe to each of the topic where IBM Watson IoT Platform will send the DM requests
topics - List of topics to be subscribedqos - Quality of Service for the subscriptionlisteners - The list of IMqttMessageListeners for the given topicsorg.eclipse.paho.client.mqttv3.MqttException - When subscription failspublic void unsubscribe(String topic) throws org.eclipse.paho.client.mqttv3.MqttException
UnSubscribe the library from the given topic
This method is used by the library to unsubscribe each of the topic where IBM Watson IoT Platform will send the DM requests
topic - topic to be unsubscribedorg.eclipse.paho.client.mqttv3.MqttException - when unsubscribe failspublic void unsubscribe(String[] topics) throws org.eclipse.paho.client.mqttv3.MqttException
UnSubscribe the library from the given topics
This method is used by the library to unsubscribe each of the topic where IBM Watson IoT Platform will send the DM requests
topics - topics to be unsubscribedorg.eclipse.paho.client.mqttv3.MqttException - when unsubscribe failsprotected org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(String topic, org.eclipse.paho.client.mqttv3.MqttMessage message) throws org.eclipse.paho.client.mqttv3.MqttException
org.eclipse.paho.client.mqttv3.MqttExceptionpublic void publish(String topic, com.google.gson.JsonObject payload, int qos) throws org.eclipse.paho.client.mqttv3.MqttException
Publish the Device management response to IBm Watson IoT Platform
This method is used by the library to respond to each of the Device Management commands from IBM Watson IoT Platform
topic - Topic where the response to be publishedpayload - the Payloadqos - The Quality Of Serviceorg.eclipse.paho.client.mqttv3.MqttException - When MQTT operation failspublic void publish(String topic, com.google.gson.JsonObject payload) throws org.eclipse.paho.client.mqttv3.MqttException
org.eclipse.paho.client.mqttv3.MqttExceptionpublic com.google.gson.JsonObject sendAndWait(String topic, com.google.gson.JsonObject jsonPayload, long timeout) throws org.eclipse.paho.client.mqttv3.MqttException
Send the message and waits for the response from IBM Watson IoT Platform
This method is used by the library to send following messages to IBM Watson IoT Platform
topic - Topic where the message to be sentjsonPayload - The messagetimeout - How long to wait for the responseorg.eclipse.paho.client.mqttv3.MqttException - when MQTT operation failspublic void disconnect()
disconnect in class AbstractClientpublic void messageArrived(String topic, org.eclipse.paho.client.mqttv3.MqttMessage message)
DeviceClientmessageArrived in interface org.eclipse.paho.client.mqttv3.IMqttMessageListenermessageArrived in interface org.eclipse.paho.client.mqttv3.MqttCallbackmessageArrived in class DeviceClientpublic void addFirmwareHandler(DeviceFirmwareHandler fwHandler) throws Exception
Adds a firmware handler for this device,
that is of type DeviceFirmwareHandler
If the device supports firmware update, the abstract class
DeviceFirmwareHandler should be extended by the device code.
The DeviceFirmwareHandler.downloadFirmware(com.ibm.wiotp.sdk.devicemgmt.DeviceFirmware) and
DeviceFirmwareHandler.updateFirmware(com.ibm.wiotp.sdk.devicemgmt.DeviceFirmware)
must be implemented to handle the firmware actions.
fwHandler - DeviceFirmwareHandler that handles the Firmware actionsException - throws an exception if a handler is already addedpublic void addDeviceActionHandler(DeviceActionHandler actionHandler) throws Exception
Adds a device action handler which is of type DeviceActionHandler
If the device supports device actions like reboot and factory reset,
the abstract class DeviceActionHandler
should be extended by the device code. The DeviceActionHandler.handleReboot(com.ibm.wiotp.sdk.devicemgmt.DeviceAction) and
DeviceActionHandler.handleFactoryReset(com.ibm.wiotp.sdk.devicemgmt.DeviceAction)
must be implemented to handle the actions.
actionHandler - DeviceActionHandler that handles the Reboot and Factory reset actionsException - throws an exception if a handler is already addedpublic void addCustomActionHandler(CustomActionHandler actionHandler) throws Exception
Adds a device action handler which is of type CustomActionHandler
If a Gateway or Device supports custom actions, this abstract class CustomActionHandler
should be extended by the Gateway or Device code.
The method CustomActionHandler.handleCustomAction(com.ibm.wiotp.sdk.CustomAction)
must be implemented by the subclass to handle the actions sent by the IBM Watson IoT Platform.
actionHandler - Handler to handle the custom actionException - Thrown if an error occurs when setting action handlerCopyright © 2019. All rights reserved.