Implement a telemetry history controller
You can implement a telemetry history controller to support data streaming and history queries from specific devices and telemetries.
Prerequisites
Before you implement a telemetry history controller, you must first:
Declare and initialize the telemetry history service
To declare and initialize the telemetry history service, follow these steps:
In your
ServicesController
file, add a public property of typeTelemetryHistoryService
with a private setter.public ITelemetryHistoryService TelemetryHistoryService { get; private set; }
Create a new
TelemetryHistoryService
.TelemetryHistoryService = new TelemetryHistoryService(m_DigitalTwinsLiveClient, ProjectService, signalBus);
Create the telemetry history controller
To create the telemetry history controller, follow these steps:
- In your
Assets/Scripts
directory, create a new script and name itTelemetryHistoryController
. At the top of your
TelemetryHistoryController
file, add directives to include the required namespaces.using Unity.DigitalTwins.Live.Sdk.Interfaces; using Unity.DigitalTwins.Live.Sdk.Signals;
Declare a private
ServicesController
and assign it theSerializeField
attribute. You must use theServicesController
class you created during the services controller setup.[SerializeField] ServicesController m_ServicesController;
Add a private
ITelemetryHistoryService
.ITelemetryHistoryService m_TelemetryHistoryService;
Add the MonoBehaviour
Start
method and assign theTelemetryHistoryService
fromm_ServicesController
.void Start() { m_TelemetryHistoryService = m_ServicesController.TelemetryHistoryService; }
Add the telemetry history controller to your scene
To add the telemetry history controller to your scene, follow these steps:
- Go to GameObject > Create Empty and rename the game object to TelemetryHistoryController.
- Select the TelemetryHistoryController game object you just created.
- In the Inspector panel, select Add Component and add the
TelemetryHistoryController
component. - Go to the
TelemetryHistoryController
component you just added. - To assign the
ServicesController
instance, drag and drop yourServices
game object from the Hierarchy panel into theServices Controller
field.
Subscribe to device telemetries
To subscribe to device telemetries, follow these steps:
In the
Start
method of yourTelemetryHistoryController
file, subscribe toDeviceTelemetriesReceivedSignal
.m_ServicesController.SignalBus.Subscribe<DeviceTelemetriesReceivedSignal>(OnDeviceTelemetriesReceived);
List the specific devices you want to receive telemetries from.
m_TelemetryHistoryService.SubscribeToDeviceTelemetriesAsync([deviceIds]);
Add an
OnDeviceTelemetriesReceived
method that acceptsDeviceTelemetriesReceivedSignal
as a parameter, formats the results, and logs them to the console.void OnDeviceTelemetriesReceived(DeviceTelemetriesReceivedSignal signal) { var output = $"Telemetry received for device ID: {signal.DeviceTelemetries.Device.Id}"; foreach (var telemetry in signal.DeviceTelemetries.Telemetries) { output += $"\n{telemetry.Key}: {telemetry.Value} @ {telemetry.Timestamp}"; } Debug.Log(output); }
Query the device telemetry
To query device telemetry, follow these steps:
- In your
TelemetryHistoryController
, create a newasync
task and name itGetTelemetryHistory
. In your
GetTelemetryHistory
task, declare anIEnumerable<string>
with the device IDs you want to query.IEnumerable<string> m_DeviceIds = new[] { "92dec7dd-3f80-4321-b4e6-07817ae32634" };
Add the start and end time for the query window.
DateTimeOffset endTime = DateTimeOffset.UtcNow; DateTimeOffset startTime = endTime.AddMinutes(-1);
Add an
int
to represent the step resolution for the query.var stepResolutionInSeconds = 1;
Add an
IEnumerable<TelemetryHistory>
to receive the results ofTelemetryHistoryService.GetHistoryAsync
.IEnumerable<TelemetryHistory> telemetryHistories = await m_TelemetryHistoryService.GetHistoryAsync(new[] { device.Device.Id }, m_TelemetryKey, startTime, endTime, stepResolutionInSeconds);
To check your results, log them to your console.
foreach (var telemetryHistory in telemetryHistories) { var output = $"Telemetry query successful for device ID: {telemetryHistory.Device.Id}"; foreach (var telemetry in telemetryHistory.Telemetries) { output += $"\n{telemetry.Key}:"; foreach (var value in telemetry) { output += $"\n{value.Value} @ {value.Timestamp}"; } } Debug.Log(output); }