159 lines
4.9 KiB
C#
159 lines
4.9 KiB
C#
using Flir.Atlas.Live.Device;
|
|
using Flir.Atlas.Live.Discovery;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Thermo.Cam.Utils
|
|
{
|
|
public class DiscoveryHelper
|
|
{
|
|
#region Private Fields
|
|
|
|
private Discovery _discovery;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Protected Fields
|
|
|
|
protected List<CameraDeviceInfo> discoveredDevices = new List<CameraDeviceInfo>();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Events
|
|
|
|
public event EventHandler eh_CameraFound;
|
|
|
|
#endregion Public Events
|
|
|
|
#region Public Properties
|
|
|
|
public string CameraAddress { get; set; } = "";
|
|
public string CameraName { get; set; } = "";
|
|
|
|
public bool doAutoConnect
|
|
{
|
|
get
|
|
{
|
|
return (!string.IsNullOrEmpty(CameraAddress) || !string.IsNullOrEmpty(CameraName));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Camera selezionata
|
|
/// </summary>
|
|
public CameraDeviceInfo SelectedCameraDevice { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Private Methods
|
|
|
|
private static void DisposeDiscovery(Object context)
|
|
{
|
|
var discovery = (Discovery)context;
|
|
discovery.Dispose();
|
|
}
|
|
|
|
private static void ShowError(string message)
|
|
{
|
|
Console.WriteLine(message);
|
|
}
|
|
|
|
private void _discovery_DeviceError(object sender, DeviceErrorEventArgs e)
|
|
{
|
|
Task.Run((Action)(() => ShowError(e.ErrorMessage)));
|
|
}
|
|
|
|
private void _discovery_DeviceFound(object sender, CameraDeviceInfoEventArgs e)
|
|
{
|
|
Task.Run((Action)(() => AddDevice(e.CameraDevice)));
|
|
}
|
|
|
|
private void _discovery_DeviceLost(object sender, CameraDeviceInfoEventArgs e)
|
|
{
|
|
Task.Run((Action)(() => RemoveDevice(e.CameraDevice)));
|
|
}
|
|
|
|
private void AddDevice(CameraDeviceInfo cameraDeviceInfo)
|
|
{
|
|
// A camera have been found, add the camera to the list control.
|
|
// One camera might support multiple streaming formats, iterate and add...
|
|
foreach (var streamingFormat in cameraDeviceInfo.StreamingFormats)
|
|
{
|
|
// This can be filtered if your only want 16-bit cameras or Video overlay.
|
|
//if (streamingFormat != ImageFormat.FlirFileFormat)
|
|
//{
|
|
// continue;
|
|
//}
|
|
|
|
var info = new CameraDeviceInfo(cameraDeviceInfo);
|
|
discoveredDevices.Add(info);
|
|
// provo selezione
|
|
trySelectCamera(info);
|
|
}
|
|
}
|
|
|
|
private void RemoveDevice(CameraDeviceInfo cameraDeviceInfo)
|
|
{
|
|
discoveredDevices.Remove(cameraDeviceInfo);
|
|
}
|
|
|
|
private void trySelectCamera(CameraDeviceInfo testCamera)
|
|
{
|
|
if (doAutoConnect)
|
|
{
|
|
// se non connessa --> cerco disponibile e valore cercato
|
|
if (!IRCam.ThermoCamera.IsConnected)
|
|
{
|
|
if (testCamera.Name == CameraName || (testCamera.IpSettings != null && testCamera.IpSettings.IpAddress == CameraAddress))
|
|
{
|
|
SelectedCameraDevice = testCamera;
|
|
if (eh_CameraFound != null)
|
|
{
|
|
eh_CameraFound(this, new EventArgs());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Public Methods
|
|
|
|
public void Start()
|
|
{
|
|
Stop();
|
|
_discovery = new Discovery();
|
|
_discovery.DeviceFound += _discovery_DeviceFound;
|
|
_discovery.DeviceLost += _discovery_DeviceLost;
|
|
_discovery.DeviceError += _discovery_DeviceError;
|
|
|
|
// Scan for camera devices on all interfaces. If you are only interested in devices from i.e. USB
|
|
// then you can start Discovery with the bit-flag Interface.Usb.
|
|
|
|
// _discovery.Start(Interface.Usb);
|
|
|
|
// or with a combination
|
|
// _discovery.Start(Interface.Network | Interface.Usb);
|
|
|
|
// Start discovery, scan on all interfaces.
|
|
// This requires that Bonjour and the Pleora drivers are installed, see the Atlas web page for more information.
|
|
_discovery.Start();
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (_discovery == null) return;
|
|
_discovery.DeviceFound -= _discovery_DeviceFound;
|
|
_discovery.DeviceLost -= _discovery_DeviceLost;
|
|
_discovery.DeviceError -= _discovery_DeviceError;
|
|
// Stop discovery might take some time. Put dispose of discovery on a background thread.
|
|
ThreadPool.QueueUserWorkItem(DisposeDiscovery, _discovery);
|
|
_discovery = null;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |