Files
DP44/DataPRO/DASFactory/.svn/pristine/cd/cd10b367c1d084ddc8eb0e5b6b001ed7bdb0a5a8.svn-base
2026-04-17 14:55:32 -04:00

184 lines
6.4 KiB
Plaintext

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTS.Common.DASResource;
using DTS.Common.Utilities.Logging;
using DTS.Common.WINUSBConnection;
namespace DTS.DASLib.DASFactory
{
internal class CDCUSBHandling : WindowsNotification
{
/// <summary>
/// Timeout in milliseconds to connect an CDCUSB device
/// </summary>
public int ConnectCDCUSBTimeout { get; set; }
private IDeviceSetup deviceHandler { get; set; }
public CDCUSBHandling(DASFactory _factory, UpdateFinishedEventHandler _SerialUpdateFinished, IDeviceSetup _deviceHandler, BlockingCollection<Tuple<QueueActions, DeviceHandling>> queueActionPerDevice)
: base(_factory, _SerialUpdateFinished, _deviceHandler.GetGuid(), queueActionPerDevice)
{
deviceHandler = _deviceHandler;
ConnectCDCUSBTimeout = 60000; // 1000 ms
}
#region CDCUSB WinProc's
/// <summary>
/// This gets called whenever a CDCUSB devices connects
/// </summary>
/// <param name="m">The windows message</param>
protected override void NotificationDeviceArrived(ref Message m)
{
try
{
// \\?\hid#vid_1cb9&pid_0002#6&29dd55fe&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
APILogger.LogString("CDCUSBArrived: A device has been connected");
EnqueueConnect();
}
catch (Exception ex)
{
APILogger.Log("MessageBox", Strings.DASFactory_CDCUSBArrivedFailed, ex);
}
finally
{
APILogger.LogString("CDCUSBArrived: Exit");
}
}
/// <summary>
/// This gets called whenever a CDCUSB devices disconnectes
/// </summary>
/// <param name="m">The windows message</param>
protected override void NotificationDeviceRemoved(ref Message m)
{
try
{
APILogger.LogString("CDCUSBRemoved: A device has been disconnected");
EnqueueDisconnect();
}
catch (Exception ex)
{
APILogger.Log("MessageBox", Strings.DASFactory_CDCUSBRemovedFailed, ex);
}
finally
{
APILogger.LogString("CDCUSBRemoved: Exit");
}
}
#endregion
#region Connect
/// <summary>
/// Throw an exception if currently connected CDCUSB devices contains duplicate dev paths.
/// </summary>
private void CheckForConnectedCDCUSBDups()
{
// get a list of units currently connected thru CDCUSB
var ConnectedCDCUSBDevices = GetListOfConnectedDevices();
if (ConnectedCDCUSBDevices.Count != ConnectedCDCUSBDevices.Distinct().Count())
{
var dupList = new StringBuilder();
foreach (var s in ConnectedCDCUSBDevices)
{
dupList.AppendLine(s);
}
var msg = "CheckForConnectedCDCUSBDups: Connected CDCUSB contain duplicates" + Environment.NewLine + dupList;
APILogger.LogString(msg);
throw new Exception("CheckForConnectedCDCUSBDups: Connected CDCUSB contain duplicates");
}
}
private List<string> GetListOfConnectedDevices()
{
var newNames = new List<string>();
var AllCDCUSBDevicePathNames = new string[128];
try
{
// Fill an array with the device path names of all attached HIDs.
if (!MyDeviceManagement.FindDeviceFromGuid(deviceHandler.GetGuid(), ref AllCDCUSBDevicePathNames))
{
return newNames;
}
}
catch
{
return newNames;
}
// loop thru the array and copy the new ones (that actually contain something)
// over to the list newNames
foreach (var t in AllCDCUSBDevicePathNames)
{
var devPath = t;
if (string.IsNullOrEmpty(t))
continue;
var bValid = false;
for (var curKey = 0; curKey < CDCUSBConnection.RegKeys.Count && !bValid; curKey++)
{
var regKey = CDCUSBConnection.RegKeys[curKey];
if (devPath.Contains(regKey.ToLower()))
{
bValid = true;
}
}
if (!bValid) { continue; }
// protect against windows giving us more than one of the same
if (newNames.Find(str => str.Equals(devPath, StringComparison.OrdinalIgnoreCase)) == null)
{
// no, we haven't added it yet so do it now
newNames.Add(devPath);
}
}
return newNames;
}
public override void UpdateConnectedDevices()
{
ConnectNewDevices(delegate
{
// make sure the connected ones are OK
CheckForConnectedCDCUSBDups();
// get a list of units currently connected thru CDCUSB
return GetListOfConnectedDevices();
},
GetConnectedStrings,
() => deviceHandler.GetICommunication(),
comm => deviceHandler.GetIConnectedDevice(comm),
deviceHandler.GetDASType(),
deviceHandler,
ConnectCDCUSBTimeout);
}
#endregion
#region Disconnect
public override void UpdateDisconnectedDevices()
{
DisconnectRemovedDevices(GetListOfConnectedDevices,
dev => deviceHandler.IsCorrectType(dev),
dev => deviceHandler.GetICommunication(dev),
deviceHandler.GetDASType(),
ConnectCDCUSBTimeout);
}
#endregion
public override void UpdateDeviceSetups()
{
deviceHandler.SetHandler(this);
}
}
}