Skip to content

Virtual Device Service Example

The Virtual Device Service example is located in the examples/device-virtual-c directory. This example shows how to use the Virtual Device Service component from inside a container using JSON configuration files.

When the example is run, a container is instantiated and populated with components. The container uses component factories to manage components, which are specified in the config/main.json file. Each entry for a component in this file specifies the following:

  • The name of the component, for example: virtual_device_service
  • The name of the registered factory used to create the component, for example: XRT::VirtualDeviceService

The components used in this example are as follows:

Each component can be configured using the associated JSON file; for example, the Virtual Device Service component is configured using the virtual_device_service.json file. For further information on configuration of the Virtual Device Service component, see Virtual Device Service Component.

In this example, the Virtual Device Service is configured to generate a reading event every half second and publish it to the Bus. The reading is logged by the Log Exporter component.

To run the Virtual Device Service component example for five seconds, enter the following commands:

cd examples/device-virtual-c
xrt config 5

The output is similar to the following:

[iot-0-1:1611166068:console:Info] virtual_device_service/data:{"device":"RandomDevice","resource":"RandomUint8","readings":{"RandomUint8":{"value":86,"type":"uint8"}}}
[iot-0-0:1611166070:console:Info] virtual_device_service/data:{"device":"RandomDevice","resource":"RandomUint8","readings":{"RandomUint8":

The supplied device profile used in this example defines resources that can be used to generate random values of various types, a sequence generator, or a waveform generator.

To change the resource used in the example, complete the following steps:

  1. Open the config/virtual_device_service.json file in your preferred editor
  2. Update the Schedules section of the file and change the resource value, which is set to RandomUint8. For example, you could change the value to any of the following:
    • RandomUint32. This get 32-bit unsigned integers
    • CountingUInt32. This gets a sequence of increasing values
    • Sinewave. This gets values corresponding to a sinewave

To change the characteristics of the generated values, for example to constrain the random values to a specified range or alter the amplitude of the sinewave, update the profiles/devicevirtual.json file as described in Virtual Device Profile.

Back to top