com.ibm.wiotp.sdk.gateway

Class ManagedGateway

  • All Implemented Interfaces:
    Runnable, org.eclipse.paho.client.mqttv3.IMqttMessageListener, org.eclipse.paho.client.mqttv3.MqttCallback, org.eclipse.paho.client.mqttv3.MqttCallbackExtended


    public class ManagedGateway
    extends GatewayClient
    implements org.eclipse.paho.client.mqttv3.IMqttMessageListener, Runnable
    A managed Gateway class, used by Gateway, to connect the Gateway and devices behind the Gateway as managed devices to IBM Watson IoT Platform and enables the Gateway to perform one or more Device Management operations.

    The device management feature enhances the IBM Watson IoT Platform service with new capabilities for managing devices and Gateways.

    This is a derived class from GatewayClient and can be used by Gateway devices to perform both Device operations and Device Management operations, i.e, the Gateways can use this class to do the following,

    • Connect devices behind the Gateway to IBM Watson IoT Platform
    • Send and receive its own sensor data like a directly connected device,
    • Send and receive data on behalf of the devices connected to it
    • Perform Device management operations like, manage, unmanage, firmware update, reboot, update location, Diagnostics informations, Factory Reset and etc.. for both Gateway and devices connected to the Gateway
    • Constructor Detail

      • ManagedGateway

        public ManagedGateway(GatewayConfig config,
                              DeviceData deviceData)
                       throws Exception

        Constructor that creates a ManagedGateway object using the properties file, but does not connect to IBM Watson IoT Platform yet. The properties must have the following definitions,

        • org - Your organization ID.
        • type - The type of your Gateway device.
        • id - The ID of your Gateway.
        • auth-method - Method of authentication (The only value currently supported is "token").
        • auth-token - API key token.
        Parameters:
        config - Configuration object for the gateway device
        deviceData - The Device Model
        Throws:
        Exception - If the essential parameters are not set
    • Method Detail

      • getGatewayDeviceData

        public DeviceData getGatewayDeviceData()
        Returns the DeviceData of the gateway.
        Returns:
        DeviceData returns the gateway device data
      • getAttachedDeviceData

        public DeviceData getAttachedDeviceData(String typeId,
                                                String deviceId)
        Returns the DeviceData of the attached device.
        Parameters:
        typeId - the device type of the device
        deviceId - the device id of the device
        Returns:
        DeviceData returns the attached device's device data
      • sendGatewayManageRequest

        public boolean sendGatewayManageRequest(long lifetime,
                                                boolean supportFirmwareActions,
                                                boolean supportDeviceActions)
                                         throws org.eclipse.paho.client.mqttv3.MqttException

        Sends a manage request to IBM Watson IoT Platform

        A Gateway uses this request to become a managed device. It should be the first device management request sent by the Gateway 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 Gateway to IBM Watson IoT Platform if its not connected already

        Parameters:
        lifetime - The length of time in seconds within which the Gateway must send another Manage device request. if set to 0, the managed Gateway will not become dormant. When set, the minimum supported setting is 3600 (1 hour).
        supportFirmwareActions - Tells whether the Gateway supports firmware actions or not. The Gateway must add a firmware handler to handle the firmware requests.
        supportDeviceActions - Tells whether the Gateway supports Device actions or not. The Gateway must add a Device action handler to handle the reboot and factory reset requests.
        Returns:
        boolean containing the status of the manage request
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • sendGatewayManageRequest

        public boolean sendGatewayManageRequest(long lifetime,
                                                boolean supportFirmwareActions,
                                                boolean supportDeviceActions,
                                                List<String> bundleIds)
                                         throws org.eclipse.paho.client.mqttv3.MqttException

        Sends a manage request to IBM Watson IoT Platform

        A Gateway uses this request to become a managed device. It should be the first device management request sent by the Gateway 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 Gateway to IBM Watson IoT Platform if its not connected already

        Parameters:
        lifetime - The length of time in seconds within which the Gateway must send another Manage device request. if set to 0, the managed Gateway will not become dormant. When set, the minimum supported setting is 3600 (1 hour).
        supportFirmwareActions - Tells whether the Gateway supports firmware actions or not. The Gateway must add a firmware handler to handle the firmware requests.
        supportDeviceActions - Tells whether the Gateway supports Device actions or not. The Gateway must add a Device action handler to handle the reboot and factory reset requests.
        bundleIds - List of bundle Ids to uniquely identify the device management extensions that this gateway supports, The device must add a Custom action handler to handle the custom action requests
        Returns:
        boolean containing the status of the manage request
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • sendDeviceManageRequest

        public boolean sendDeviceManageRequest(String typeId,
                                               String deviceId,
                                               long lifetime,
                                               boolean supportFirmwareActions,
                                               boolean supportDeviceActions)
                                        throws org.eclipse.paho.client.mqttv3.MqttException

        Sends a device manage request to IBM Watson IoT Platform

        A Gateway uses this request to manage the device connected to it, It should be the first device management request sent for 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 Gateway/Device to IBM Watson IoT Platform if its not connected already

        Parameters:
        typeId - The typeId of the device connected to the gateway
        deviceId - The deviceId of the device connected to the gateway
        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.
        Returns:
        True if successful
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • sendDeviceManageRequest

        public boolean sendDeviceManageRequest(String typeId,
                                               String deviceId,
                                               long lifetime,
                                               boolean supportFirmwareActions,
                                               boolean supportDeviceActions,
                                               List<String> bundleIds)
                                        throws org.eclipse.paho.client.mqttv3.MqttException

        Sends a device manage request to IBM Watson IoT Platform

        A Gateway uses this request to manage the device connected to it, It should be the first device management request sent for 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 Gateway/Device to IBM Watson IoT Platform if its not connected already

        Parameters:
        typeId - The typeId of the device connected to the gateway
        deviceId - The deviceId of the device connected to the gateway
        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 - array of bundle Ids to uniquely identify the device management extensions
        Returns:
        True if successful
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • sendDeviceManageRequest

        public boolean sendDeviceManageRequest(String typeId,
                                               String deviceId,
                                               DeviceData deviceData,
                                               long lifetime,
                                               boolean supportFirmwareActions,
                                               boolean supportDeviceActions)
                                        throws org.eclipse.paho.client.mqttv3.MqttException

        Sends a device manage request to IBM Watson IoT Platform

        A Gateway uses this request to manage the device connected to it, It should be the first device management request sent for 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 Gateway/Device to IBM Watson IoT Platform if its not connected already

        Parameters:
        typeId - The typeId of the device connected to the gateway
        deviceId - The deviceId of the device connected to the gateway
        deviceData - The DeviceData containing the information about the device
        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.
        Returns:
        True if successful
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • sendDeviceManageRequest

        public boolean sendDeviceManageRequest(String typeId,
                                               String deviceId,
                                               DeviceData deviceData,
                                               long lifetime,
                                               boolean supportFirmwareActions,
                                               boolean supportDeviceActions,
                                               List<String> bundleIds)
                                        throws org.eclipse.paho.client.mqttv3.MqttException

        Sends a device manage request to IBM Watson IoT Platform

        A Gateway uses this request to manage the device connected to it, It should be the first device management request sent for 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 Gateway/Device to IBM Watson IoT Platform if its not connected already

        Parameters:
        typeId - The typeId of the device connected to the gateway
        deviceId - The deviceId of the device connected to the gateway
        deviceData - The DeviceData containing the information about the device
        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 bundle Ids to uniquely identify the device management extensions that this device supports, The device must add a Custom action handler to handle the custom action requests
        Returns:
        True if successful
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • updateGatewayLocation

        public int updateGatewayLocation(Double latitude,
                                         Double longitude,
                                         Double elevation)
        Update the location of the Gateway to IBM Watson IoT Platform.
        Parameters:
        latitude - Latitude in decimal degrees using WGS84
        longitude - Longitude in decimal degrees using WGS84
        elevation - Elevation in meters using WGS84
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • updateDeviceLocation

        public int updateDeviceLocation(String typeId,
                                        String deviceId,
                                        Double latitude,
                                        Double longitude,
                                        Double elevation)
        Update the location of the Device connected via the Gateway to IBM Watson IoT Platform.
        Parameters:
        typeId - The device type of the device connected to the Gateway
        deviceId - The deviceId of the device connected to the Gateway
        latitude - Latitude in decimal degrees using WGS84
        longitude - Longitude in decimal degrees using WGS84
        elevation - Elevation in meters using WGS84
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • updateGatewayLocation

        public int updateGatewayLocation(Double latitude,
                                         Double longitude,
                                         Double elevation,
                                         Date measuredDateTime)
        Update the location of the Gateway. This method converts the date in the required format. The caller just need to pass the date in java.util.Date format
        Parameters:
        latitude - Latitude in decimal degrees using WGS84
        longitude - Longitude in decimal degrees using WGS84
        elevation - Elevation in meters using WGS84
        measuredDateTime - When the location information is retrieved
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • updateDeviceLocation

        public int updateDeviceLocation(String typeId,
                                        String deviceId,
                                        Double latitude,
                                        Double longitude,
                                        Double elevation,
                                        Date measuredDateTime)
        Update the location of the Device connected via the Gateway.This method converts the date in the required format. The caller just need to pass the date in java.util.Date format
        Parameters:
        typeId - The device type of the device connected to the Gateway
        deviceId - The deviceId of the device connected to the Gateway
        latitude - Latitude in decimal degrees using WGS84
        longitude - Longitude in decimal degrees using WGS84
        elevation - Elevation in meters using WGS84
        measuredDateTime - When the location information is retrieved
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • updateGatewayLocation

        public int updateGatewayLocation(Double latitude,
                                         Double longitude,
                                         Double elevation,
                                         Date measuredDateTime,
                                         Date updatedDateTime,
                                         Double accuracy)
        Update the location of the Gateway. This method converts the date in the required format. The caller just need to pass the date in java.util.Date format
        Parameters:
        latitude - Latitude in decimal degrees using WGS84
        longitude - Longitude in decimal degrees using WGS84
        elevation - Elevation in meters using WGS84
        measuredDateTime - When the location information is retrieved
        updatedDateTime - When the location information is updated
        accuracy - Accuracy of the position in meters
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • updateDeviceLocation

        public int updateDeviceLocation(String typeId,
                                        String deviceId,
                                        Double latitude,
                                        Double longitude,
                                        Double elevation,
                                        Date measuredDateTime,
                                        Date updatedDateTime,
                                        Double accuracy)
        Update the location of the device connected through the Gateway. This method converts the date in the required format. The caller just need to pass the date in java.util.Date format.
        Parameters:
        typeId - The device type of the device connected to the Gateway
        deviceId - The deviceId of the device connected to the Gateway
        latitude - Latitude in decimal degrees using WGS84
        longitude - Longitude in decimal degrees using WGS84
        elevation - Elevation in meters using WGS84
        measuredDateTime - Date of location measurement
        updatedDateTime - Date of the update to the device information
        accuracy - Accuracy of the position in meters
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • clearGatewayErrorCodes

        public int clearGatewayErrorCodes()
        Clear the Gateway ErrorCodes from IBM Watson IoT Platform.
        Returns:
        code indicating whether the clear operation is successful or not (200 means success, otherwise unsuccessful)
      • clearDeviceErrorCodes

        public int clearDeviceErrorCodes(String typeId,
                                         String deviceId)
        Clear the Device(connected via the Gateway) ErrorCodes from IBM Watson IoT Platform.
        Parameters:
        typeId - The device type of the device connected to the Gateway
        deviceId - The deviceId of the device connected to the Gateway
        Returns:
        code indicating whether the clear operation is successful or not (200 means success, otherwise unsuccessful)
      • clearGatewayLogs

        public int clearGatewayLogs()
        Clear the Gateway Logs from IBM Watson IoT Platform.
        Returns:
        code indicating whether the clear operation is successful or not (200 means success, otherwise unsuccessful).
      • clearDeviceLogs

        public int clearDeviceLogs(String typeId,
                                   String deviceId)
        Clear the Device(connected through the Gateway) Logs from IBM Watson IoT Platform.
        Parameters:
        typeId - the device type of the attached device
        deviceId - the device Id of the attached device
        Returns:
        code indicating whether the clear operation is successful or not (200 means success, otherwise unsuccessful).
      • addGatewayErrorCode

        public int addGatewayErrorCode(int errorCode)
        Adds the Gateway errorcode to IBM Watson IoT Platform.
        Parameters:
        errorCode - The "errorCode" is a current device error code that needs to be added to the IBM Watson IoT Platform.
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful).
      • addDeviceErrorCode

        public int addDeviceErrorCode(String typeId,
                                      String deviceId,
                                      int errorCode)
        Adds the Device errorcode to IBM Watson IoT Platform.
        Parameters:
        typeId - The device type of the device connected to the Gateway
        deviceId - The deviceId of the device connected to the Gateway
        errorCode - The "errorCode" is a current device error code that needs to be added to the IBM Watson IoT Platform.
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful)
      • addGatewayLog

        public int addGatewayLog(String message,
                                 Date timestamp,
                                 LogSeverity severity)
        Adds a Gateway Log message to the IBM Watson IoT Platform.
        Parameters:
        message - The Log message that needs to be added to the IBM Watson IoT Platform.
        timestamp - The Log timestamp.
        severity - the LogSeverity.
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful).
      • addGatewayLog

        public int addGatewayLog(String message,
                                 Date timestamp,
                                 LogSeverity severity,
                                 String data)
        Adds a Gateway Log message to the IBM Watson IoT Platform.
        Parameters:
        message - The Log message that needs to be added to the IBM Watson IoT Platform.
        timestamp - The Log timestamp.
        severity - The LogSeverity.
        data - The optional diagnostic string data - The library will encode the data in base64 format as required by the Platform.
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful).
      • addDeviceLog

        public int addDeviceLog(String typeId,
                                String deviceId,
                                String message,
                                Date timestamp,
                                LogSeverity severity)
        Adds a Device(connected via the Gateway) Log message to the IBM Watson IoT Platform.
        Parameters:
        typeId - The device type of the device connected to the Gateway.
        deviceId - The deviceId of the device connected to the Gateway.
        message - The Log message that needs to be added to the IBM Watson IoT Platform.
        timestamp - The Log timestamp.
        severity - The LogSeverity.
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful).
      • addDeviceLog

        public int addDeviceLog(String typeId,
                                String deviceId,
                                String message,
                                Date timestamp,
                                LogSeverity severity,
                                String data)
        Adds a Device(connected via the Gateway) Log message to the IBM Watson IoT Platform. This method converts the timestamp in the required format. The caller just need to pass the timestamp in java.util.Date format.
        Parameters:
        typeId - The device type of the device connected to the Gateway.
        deviceId - The deviceId of the device connected to the Gateway.
        message - The Log message that needs to be added to the IBM Watson IoT Platform.
        timestamp - The Log timestamp
        severity - The LogSeverity
        data - The optional diagnostic string data - The library will encode the data in base64 format as required by the Platform .
        Returns:
        code indicating whether the update is successful or not (200 means success, otherwise unsuccessful).
      • sendGatewayUnmanageRequet

        public boolean sendGatewayUnmanageRequet()
                                          throws org.eclipse.paho.client.mqttv3.MqttException
        Moves the Gateway from managed state to unmanaged state A device uses this request when it no longer needs to be managed. This means Watson IoT Platform will no longer send new device management requests to this device and device management requests from this device will be rejected apart from a Manage device request
        Returns:
        True if the unmanage command is successful
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - When failure
      • sendDeviceUnmanageRequet

        public boolean sendDeviceUnmanageRequet(String typeId,
                                                String deviceId)
                                         throws org.eclipse.paho.client.mqttv3.MqttException
        Moves the device connected via the Gateway from managed state to unmanaged state A device uses this request when it no longer needs to be managed. This means Watson IoT Platform will no longer send new device management requests to this device and device management requests from this device will be rejected apart from a Manage device request
        Parameters:
        typeId - The device type of the device connected to the Gateway
        deviceId - The deviceId of the device connected to the Gateway
        Returns:
        True if the unmanage command is successful
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException - when failure
      • publish

        protected org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(String topic,
                                                                            org.eclipse.paho.client.mqttv3.MqttMessage message)
                                                                     throws org.eclipse.paho.client.mqttv3.MqttException
        Throws:
        org.eclipse.paho.client.mqttv3.MqttException
      • disconnect

        public void disconnect()
        Disconnects the Gateway and all the devices connected, from IBM Watson IoT Platform
        Overrides:
        disconnect in class AbstractClient
      • reconnect

        protected void reconnect()
                          throws KeyManagementException,
                                 NoSuchAlgorithmException
        This method reconnects when the connection is lost due to n/w interruption and this method is called only when the connection is established originally by the library code. This method does the following activities, 1. Calculates the lifetime remaining 2. Sends the manage request for those devices/Gateway which are in managed state before disconnecting
        Overrides:
        reconnect in class DeviceClient
        Throws:
        NoSuchAlgorithmException - TLS issues
        KeyManagementException - TLS issues
      • messageArrived

        public void messageArrived(String topic,
                                   org.eclipse.paho.client.mqttv3.MqttMessage message)
        Description copied from class: GatewayClient

        This method is called by the MQTT library when a message(command) is sent by the IBM Watson IoT Platform.

        The message(command) will be processed by this class and corresponding callback method will be called if registered.
        Specified by:
        messageArrived in interface org.eclipse.paho.client.mqttv3.IMqttMessageListener
        Specified by:
        messageArrived in interface org.eclipse.paho.client.mqttv3.MqttCallback
        Overrides:
        messageArrived in class GatewayClient
      • run

        public void run()
        Specified by:
        run in interface Runnable

Copyright © 2019. All rights reserved.