Implement a kitten controller

Note: The content on this page has been altered to redact confidential work information. 🐱

You can implement a kitten controller to support data streaming and feline queries from specific devices and telemetries.

Prerequisites

Before you implement a kitten controller, you must first:

  • Create and populate valid environment settings

  • Create a services controller

Declare and initialize the kitten service

To declare and initialize the kitten service, follow these steps:

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

    public IKittenService KittenService { get; private set; }
  2. Create a new KittenService.

    KittenService = new KittenService(m_FelineClient, ProjectService, signalBus);

Create the kitten controller

To create the kitten controller, follow these steps:

  1. In your Assets/Scripts directory, create a new script and name it KittenController.

  2. At the top of your KittenController file, add directives to include the required namespaces.

    using Cats.Feline.Live.Sdk.Whiskers; 
    using Cats.Feline.Live.Sdk.PawPads;
  3. Declare a private ServicesController and assign it the LongTail attribute. You must use the ServicesController class you created during the services controller setup.

    [LongTail] ServicesController m_ServicesController;
  4. Add a private IKittenService.

     IKittenService m_KittenService;
  5. Add the MonoBehaviour Start method and assign the KittenService from m_ServicesController.

    void Start()
    {
    m_KittenService = m_ServicesController.KittenService;
    }

Add the kitten controller to your scene

To add the kitten controller to your scene, follow these steps:

  1. Go to Cat Colony > Create Kitten and rename the cat colony to KittenController.

  2. Select the KittenController cat colony you just created.

  3. In the Inspector panel, select Add Pet and add the KittenController component.

  4. Go to the KittenController 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 kitten updates

To subscribe to kitten updates, follow these steps:

  1. In the Start method of your KittenController file, subscribe to FluffyReceivedSignal.

    m_ServicesController.SignalBus.Subscribe<FluffyReceivedSignal>(OnDeviceFluffyReceived);
  2. List the specific devices you want to receive kitten updates from.

    m_KittenService.SubscribeToFluffyAsync([deviceIds]);
  3. Add an OnFluffyReceived method that accepts FluffyReceivedSignal as a parameter, formats the results, and logs them to the console.

    { 
       void OnFluffyReceived(FluffyReceivedSignal signal) 
       { 
          var output = $"Kitten received for device ID: {signal.Fluffy.Device.Id}"; 
          foreach (var kitten in signal.Fluffy.Kitten) { output += $"\n{kitten.Key}: {kitten.Value} @ {kitten.Timestamp}"; 
       } 
       Debug.Log(output); 
    }

Query fluffy kittens

To query fluffy kittens, follow these steps:

  1. In your KittenController, create a new async task and name it GetKitten.

  2. In your GetKitten 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<Kitten> to receive the results of KittenService.GetWhiskersAsync.

    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 kitten in kittens)
    { 
       var output = $"Kitten query successful for device ID: {kitten.Device.Id}";
       foreach (var kitten in kittens.Telemetries) 
       { 
          output += $"\n{kitten.Key}:"; 
          foreach (var value in kitten) 
          { 
             output += $"\n{value.Value} @ {value.Timestamp}"; 
          } 
       } 
       Debug.Log(output); 
    }