using Itala;
/// <summary>
/// "AdvancedEnumeration" provides a more granular approach to device
/// enumeration.It shows how devices can be enumerated under a single (or multiple)
/// specific network interface.
/// </summary>
internal class Program
{
private static void AdvancedEnumeration_Sample()
{
Console.WriteLine("######## AdvancedEnumeration ########");
ISystem system = SystemFactory.Create();
// The goal is to open the first correctly configured device found for
// each available interface. To do so, when a suitable device is found, it is
// created and pushed to this list.
List<IDevice> openDevices = new List<IDevice>();
// Look for available network interfaces on the host and retrieve a list of
// InterfaceInfo objects. Each info object uniquely identifies a detected
// network interface and provides a set of useful information about it.
List<InterfaceInfo> interfaceInfos = system.EnumerateInterfaces();
if (interfaceInfos.Count == 0)
{
throw new ItalaRuntimeException("No interfaces found. Example canceled.");
}
// For every interface found, some information is printed. Note that
// InterfaceInfo objects are nothing more than information containers having
// no influence on the interface state.
// Each InterfaceInfo object is also used to enumerate the available devices
// under the interface it uniquely identifies. Then, the information set of
// each discovered device is printed.
foreach(var (itfInfo, i) in interfaceInfos.Select((itfInfo, i) => (itfInfo, i)))
{
Console.WriteLine("Interface " + i + " property");
Console.WriteLine("\tDisplay name: " + itfInfo.DisplayName);
Console.WriteLine("\tMAC Address: " + ToNETString.MacAddressToString(itfInfo.MacAddress));
Console.WriteLine("\tIP Address: " + ToNETString.AddressToString(itfInfo.IpAddress));
Console.WriteLine("\tSubnet mask: " + ToNETString.AddressToString(itfInfo.SubnetMask));
// Discover the available devices under the current interface, with a time
// limit of 700ms to show up.
List<DeviceInfo> deviceInfos = system.EnumerateDevices(itfInfo, 700);
// Flag used to determine if a device suitable for initialization have
// already been found and initialized under the current interface.
bool suitableDeviceFound = false;
foreach (var (deviceInfo, j) in deviceInfos.Select((deviceInfo, i) => (deviceInfo, i)))
{
Console.WriteLine("\t\tDevice " + j + " property");
Console.WriteLine("\t\tDisplay name: " + deviceInfo.DisplayName);
Console.WriteLine("\t\tMAC Address: " + ToNETString.MacAddressToString(deviceInfo.MacAddress));
Console.WriteLine("\t\tIP Address: " + ToNETString.AddressToString(deviceInfo.IpAddress));
Console.WriteLine("\t\tModel: " + deviceInfo.Model);
Console.WriteLine("\t\tAccess status: " + ToNETString.AccessStatusToString(deviceInfo.AccessStatus));
// Open the current device if accessible and no other devices are open under the current
// interface.
if (!suitableDeviceFound)
{
if(deviceInfo.AccessStatus == DeviceAccessStatus.AvailableReadWrite)
{
openDevices.Add(system.CreateDevice(deviceInfo));
suitableDeviceFound=true;
Console.WriteLine("\t\tDevice created!");
}
else
{
Console.WriteLine("\t\tDevice misconfigured, skipped.");
}
}
}
Console.WriteLine();
}
// Do something with the devices..
foreach(var (iDev, i) in openDevices.Select((iDev, i)=>(iDev, i)))
{
iDev.Dispose();
Console.WriteLine("Device instance " + i + " disposed.");
}
system.Dispose();
Console.WriteLine("System instance disposed");
}
private static void Main(string[] args)
{
try
{
AdvancedEnumeration_Sample();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}