Skip to content

REST Exporter Example

The REST exporter example is located in the examples/rest_exporter directory. This example shows how to use the REST Exporter component from inside a container using JSON configuration files.

The container uses component factories to manage components. The registered factories are as follows:

  • Logger component
  • ThreadPool component
  • Scheduler component
  • Core Bus component
  • Virtual Device Service component. This publishes data on the Bus
  • REST Exporter component. This subscribes to the Bus and exports data that matches the subscription pattern to the specified REST endpoint
  • Log Exporter component. This subscribes to the data published by the Virtual Device Service on the same topic set by the REST Exporter component, and outputs it to the log

The container uses component factories to manage components, which are specified in the config/main.json file. The REST Exporter component is configured using the rest_exporter.json file. For further information on the configuration of the REST Exporter component, see REST Exporter Component.

Note

There are three instances of the Logger component. One is configured to Info level and is used by the Log Exporter component that logs the messages available to the REST Exporter as input. The second is configured to log Debug level messages and is used by the REST exporter. The third is configured to log Warning level messages for reduced verbosity and is used by the other components in this example.

In this example, the REST Exporter component subscribes to data available on the Bus that matches the device/# pattern and exports it to the configured REST endpoint.

You must set the following environment variable:

Environment Variable Description
XRT_REST_ENDPOINT The address of the REST endpoint in one of the following formats:
  • For a secure connection with a REST endpoint:
    https://<endpoint-IP-address>:<port>
  • For an unsecure connection with a REST endpoint:
    http://<endpoint-IP-address>:<port>

If using a secure connection with a REST endpoint, the following environment variables must also be set:

Environment Variable Description
REST_EXPORTER_CA_FILE The path and filename of the Certificate authority file
REST_EXPORTER_SSL_CERT The path and filename of the SSL certificate in PEM format
REST_EXPORTER_SSL_KEY The path and filename of the SSL key in PRM format, used for a given REST secure configuration

To run the REST Exporter component example for five seconds, enter the following commands:

cd examples/rest_exporter
xrt config 5

The output is similar to the following:

[iot-0-0:1611166059:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":123,"type":"uint32"},"Uptime":{"value":456789,"type":"uint64"}}}
[iot-0-0:1611166059:console:Debug] data exported: {"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":123,"type":"uint32"},"Uptime":{"value":456789,"type":"uint64"}}}
[iot-0-1:1611166062:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime":{"value":456790,"type":"uint64"}}}
[iot-0-1:1611166062:console:Debug] data exported: {"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime":{"value":456790,"type":"uint64"}}}

In this output, the messages from the Virtual Device Service are available on the device/data topic, which is the input to the REST Exporter component, and the data exported entry is the output from the REST exporter to the REST endpoint.

Back to top