docs.unity3d.com
  • Manual
  • Scripting API
  • Changelog
  • License

    • Unity Live Systems Data
      • Installation
      • What's new
      • Upgrade guide
    • Get started
      • Take it further
    • Basic concepts
    • Configure your service
      • Add and configure user-supplied secrets
      • Associate 3D objects with source devices
      • Configure a facility device simulator
      • Configure data connectors
        • Device connectors
          • Azure IoT Hub device connector
          • Facility Simulator device connector
          • HTTP device connector
        • Telemetry connectors
          • Azure EventHub telemetry connector
    • Develop your application
      • Create environment settings
      • Create a services controller
      • Implement a telemetry history controller
    • Troubleshooting
    • Glossary
    • REST APIs

    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:

    • Create and populate valid environment settings
    • Create a services controller

    Declare and initialize the telemetry history service

    To declare and initialize the telemetry history service, follow these steps:

    1. In your ServicesController file, add a public property of type TelemetryHistoryService with a private setter.

      public ITelemetryHistoryService TelemetryHistoryService { get; private set; }
      
    2. 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:

    1. In your Assets/Scripts directory, create a new script and name it TelemetryHistoryController.
    2. 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;
      
    3. Declare a private ServicesController and assign it the SerializeField attribute. You must use the ServicesController class you created during the services controller setup.

      [SerializeField]
      ServicesController m_ServicesController;
      
    4. Add a private ITelemetryHistoryService.

       ITelemetryHistoryService m_TelemetryHistoryService;
      
    5. Add the MonoBehaviour Start method and assign the TelemetryHistoryService from m_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:

    1. Go to GameObject > Create Empty and rename the game object to TelemetryHistoryController.
    2. Select the TelemetryHistoryController game object you just created.
    3. In the Inspector panel, select Add Component and add the TelemetryHistoryController component.
    4. Go to the TelemetryHistoryController component you just added.
    5. To assign the ServicesController instance, drag and drop your Services game object from the Hierarchy panel into the Services Controller field.

    Subscribe to device telemetries

    To subscribe to device telemetries, follow these steps:

    1. In the Start method of your TelemetryHistoryController file, subscribe to DeviceTelemetriesReceivedSignal.

      m_ServicesController.SignalBus.Subscribe<DeviceTelemetriesReceivedSignal>(OnDeviceTelemetriesReceived);
      
    2. List the specific devices you want to receive telemetries from.

      m_TelemetryHistoryService.SubscribeToDeviceTelemetriesAsync([deviceIds]);
      
    3. Add an OnDeviceTelemetriesReceived method that accepts DeviceTelemetriesReceivedSignal 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:

    1. In your TelemetryHistoryController, create a new async task and name it GetTelemetryHistory.
    2. In your GetTelemetryHistory task, declare an IEnumerable<string> with the device IDs you want to query.

      IEnumerable<string> m_DeviceIds = new[] { "92dec7dd-3f80-4321-b4e6-07817ae32634" };
      
    3. Add the start and end time for the query window.

      DateTimeOffset endTime = DateTimeOffset.UtcNow;
      DateTimeOffset startTime = endTime.AddMinutes(-1);
      
    4. Add an int to represent the step resolution for the query.

      var stepResolutionInSeconds = 1;
      
    5. Add an IEnumerable<TelemetryHistory> to receive the results of TelemetryHistoryService.GetHistoryAsync.

      IEnumerable<TelemetryHistory> telemetryHistories = await m_TelemetryHistoryService.GetHistoryAsync(new[] { device.Device.Id }, m_TelemetryKey, startTime, endTime, stepResolutionInSeconds);
      
    6. 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);
      }
      
    Back to top Generated by DocFX
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023