using Itala;
using Itala.GenApi;
/// <summary>
/// "NodeMapPolling" example shows how user-defined handlers are
/// executed by GenApi when the node they're registered to changes due
/// to a polling operation. Every "pollable" feature has a specific
/// polling time defined by the manufacturer. When the Poll function is
/// called on the nodemap by the client code, the time elapsed since the
/// last poll must be specified. All features with a polling time shorter
/// than the elapsed time are automatically read and updated by GenApi.
/// </summary>
internal class Program
{
const int TEMPERATURE_READS = 10;
const int POLL_FREQ_MS = 750;
private static void NodeMapPolling_Sample()
{
Console.WriteLine("######## NodeMapPolling ########");
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.");
IFloat deviceTemperature = device.GetNodeMap().GetNode<IFloat>("DeviceTemperature");
if (!deviceTemperature.IsReadable)
throw new ItalaRuntimeException("DeviceTemperature feature not available. Aborting.");
// Register the onDeviceTemperatureChanged handler to the DeviceTemperature node
NodeChangedEventHandler onDeviceTemperatureChanged = (INode node) =>
{
IFloat payloadSize = node.TryGetAs<IFloat>();
Console.WriteLine("\tOnDeviceTemperatureChanged function called.");
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("\tDevice temperature node changed. New value is " + payloadSize.Value + "C\n");
};
deviceTemperature.GetNode().NodeChanged += onDeviceTemperatureChanged;
Console.WriteLine("onDeviceTemperatureChanged registered to DeviceTemperature node.\n");
for(int i = 0; i<TEMPERATURE_READS; i++)
{
// Sleep for a bit...
Thread.Sleep(POLL_FREQ_MS);
Console.WriteLine("\tPolling temperature...");
// And then poll nodes of the map. At this point, all nodes with a polling time
// shorter than the elapsed time are updated and their handler executed, if any.
device.GetNodeMap().Poll(POLL_FREQ_MS);
}
deviceTemperature.GetNode().NodeChanged -= onDeviceTemperatureChanged;
device.Dispose();
Console.WriteLine("Device instance disposed.");
system.Dispose();
Console.WriteLine("System instance disposed.");
}
private static void Main(string[] args)
{
try
{
NodeMapPolling_Sample();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}