NodeCallback#

using Itala;
using Itala.GenApi;

/// <summary>
/// "NodeCallback" example shows how user-defined handlers are
/// executed by GenApi when the node they're registered to changes. In this
/// case, the PayloadSize node is used: it changes automatically
/// when a image-format related feature changes, e.g. PixelFormat.
/// </summary>
internal class Program
{

  private static void NodeCallback_Sample()
  {
    Console.WriteLine("######## NodeCallback ########");
    ISystem system = SystemFactory.Create();
    List<DeviceInfo> deviceInfos = system.EnumerateDevices();

    if (deviceInfos.Count == 0)
      throw new ItalaRuntimeException("No devices found. Example canceled.");
    if (deviceInfos[0].AccessStatus != DeviceAccessStatus.AvailableReadWrite)
      throw new ItalaRuntimeException("Target device is unaccessible in RW mode. Example canceled.");
    IDevice device = system.CreateDevice(deviceInfos[0]);
    Console.WriteLine("First device initialized.");

    // Get the PixelFormat node and set its value to Mono8 for example
    // purposes. The original value is saved and then restored at the end of
    // the program.
    IEnumeration pixelFormat = device.GetNodeMap().GetNode<IEnumeration>("PixelFormat");
    if (!pixelFormat.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the pixel format. Aborting.");
    Int64 originalPixelFormat = pixelFormat.IntValue;
    pixelFormat.FromString("Mono8");

    // Get the PayloadSize node.
    IInteger payloadSize = device.GetNodeMap().GetNode<IInteger>("PayloadSize");
    if (!payloadSize.IsReadable)
      throw new ItalaRuntimeException("Unable to get the image payload size. Aborting.");
    Console.WriteLine("\nThe payload size is " + payloadSize.Value + " with Mono8 format.");

    // Register the function to the PayloadSize node so that when a
    // change in its value occurs, the function is executed.
    NodeChangedEventHandler onPayloadSizeChanged = (INode node) =>
    {
      IInteger payloadSize = node.TryGetAs<IInteger>();
      Console.WriteLine("\tOnPayloadSizeChanged function called.");
      Console.WriteLine("\tPayloadSize node changed. New value is " + payloadSize.Value + "\n");

    };
    payloadSize.GetNode().NodeChanged += onPayloadSizeChanged;
    Console.WriteLine("onPayloadSizeChanged function registered to PayloadSize node.\n");

    Console.WriteLine("Setting pixel format to Mono12p...");

    // Set the pixel format to Mono12p. At this point, the handler gets
    // automatically executed by GenApi since the PayloadSize feature
    // is changed after the new PixelFormat.
    pixelFormat.FromString("Mono12p");

    // Deregister the handler when done.
    payloadSize.GetNode().NodeChanged -= onPayloadSizeChanged;

    // Restore the original pixel format value.
    pixelFormat.IntValue = originalPixelFormat;

    device.Dispose();
    Console.WriteLine("Device instance disposed.");
    system.Dispose();
    Console.WriteLine("System instance disposed.");
  }

  private static void Main(string[] args)
  {
    try
    {
      NodeCallback_Sample();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString());
    }
  }
}