Enumeration#

using Itala;

/// <summary>
/// "Enumeration" is the simplest example available and the best starting
/// point to be up and running with ItalaSDK. Device enumeration is the process by
/// which connected devices are discovered and identified by the host. It also
/// represents the last known state of the cameras. The enumeration must be performed
/// by the user code. Once a device is known and reachable, it can be accessed and
/// via its interface to acquire images or configure its parameters.
/// </summary>
internal class Program
{

  private static void Enumeration_Sample()
  {
    Console.WriteLine("######## Enumeration ########");

    // Initialize the system instance to enables the usage of the entire API.
    // It is the root of Itala and only one system object can exist at a time. If
    // two systems are created simultaneously, an error occurs. When the system
    // instance is no longer needed (e.g. on application exit) it MUST be released
    // by calling its dedicated function Dispose().
    ISystem system = SystemFactory.Create();

    // Look for available devices across all network interfaces on the host and
    // retrieve a list of DeviceInfo objects, with a limit of 700ms to show up.
    // Each info object uniquely identifies a detected device and provides a set
    // of useful information about it. DeviceInfo objects are nothing more than
    // information containers with no effects on the device state.
    List<DeviceInfo> deviceInfos = system.EnumerateDevices(700);

    if(deviceInfos.Count == 0)
    {
      throw new ItalaRuntimeException("No devices found. Example canceled.");
    }

    // Let's print some data..
    Console.WriteLine(deviceInfos.Count + " devices discovered.\n");

    // For every device found some information is printed. Note that some devices
    // may be in a misconfigured state (e.g. a different subnet) thus impossible
    // to be reached or used, as eventually reported by DeviceInfo.
    foreach (var (deviceInfo, index) in deviceInfos.Select((deviceInfo, i) => (deviceInfo, i))) 
    {
      Console.WriteLine("Device " + index + " property");
      Console.WriteLine("\tDisplay name: " + deviceInfo.DisplayName);
      Console.WriteLine("\tMAC Address: " + ToNETString.MacAddressToString(deviceInfo.MacAddress));
      Console.WriteLine("\tIP Address: " + ToNETString.AddressToString(deviceInfo.IpAddress));
      Console.WriteLine("\tModel: " + deviceInfo.Model);
      Console.WriteLine("\tAccess status: " + ToNETString.AccessStatusToString(deviceInfo.AccessStatus));
    }

    if (deviceInfos[0].AccessStatus != DeviceAccessStatus.AvailableReadWrite)
    {
      throw new ItalaRuntimeException("Target device is unaccessible in RW mode. Example canceled.");
    }

    // Create and initialize the device identified by the first DeviceInfo object.
    IDevice device = system.CreateDevice(deviceInfos[0]);
    Console.WriteLine(deviceInfos[0].SerialNumber + " device instance created.");

    // Do something with the device..

    // Never forget to dispose the used device when done! It's strongly suggested
    // to null the released pointer to avoid violations. The Dispose() method
    // and the "delete" operator (which is not available here) are the exact same
    // thing hence they must be used the same way.
    device.Dispose();
    Console.WriteLine(deviceInfos[0].SerialNumber + " device instance disposed.");

    // Never forget to dispose the system when done! It's strongly suggested
    // to null the released pointer to avoid violations. The Dispose() method
    // and the "delete" operator (which is not available here) are the exact same
    // thing hence they must be used the same way.
    system.Dispose();
    Console.WriteLine("System instance disposed");
  }

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