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
ServicesControllerfile, add a public property of typeTelemetryHistoryServicewith 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/Scriptsdirectory, create a new script and name itTelemetryHistoryController. At the top of your
TelemetryHistoryControllerfile, add directives to include the required namespaces.using Unity.DigitalTwins.Live.Sdk.Interfaces; using Unity.DigitalTwins.Live.Sdk.Signals;Declare a private
ServicesControllerand assign it theSerializeFieldattribute. You must use theServicesControllerclass you created during the services controller setup.[SerializeField] ServicesController m_ServicesController;Add a private
ITelemetryHistoryService.ITelemetryHistoryService m_TelemetryHistoryService;Add the MonoBehaviour
Startmethod and assign theTelemetryHistoryServicefromm_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
TelemetryHistoryControllercomponent. - Go to the
TelemetryHistoryControllercomponent you just added. - To assign the
ServicesControllerinstance, drag and drop yourServicesgame object from the Hierarchy panel into theServices Controllerfield.
Subscribe to device telemetries
To subscribe to device telemetries, follow these steps:
In the
Startmethod of yourTelemetryHistoryControllerfile, subscribe toDeviceTelemetriesReceivedSignal.m_ServicesController.SignalBus.Subscribe<DeviceTelemetriesReceivedSignal>(OnDeviceTelemetriesReceived);List the specific devices you want to receive telemetries from.
m_TelemetryHistoryService.SubscribeToDeviceTelemetriesAsync([deviceIds]);Add an
OnDeviceTelemetriesReceivedmethod that acceptsDeviceTelemetriesReceivedSignalas 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 newasynctask and name itGetTelemetryHistory. In your
GetTelemetryHistorytask, 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
intto 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); }