✅ How to pick from a field in a list in with priority
foreach (Device obj in devicesBlock)
{
Program.Log($"Found device {obj.Model} {obj.Serial} {GetConnectionType(obj.ConnectionType)}");
}
foreach (Device obj in devicesBlock)
{
Program.Log($"Found device {obj.Model} {obj.Serial} {GetConnectionType(obj.ConnectionType)}");
}
2 Replies
I tried this:
this doesn't work since it just starts at the top of the list, I need to search the entire list with similar logic
i think i can do
how would i refer to that device object out of the list to connect to it
ultimately i need to connect to the device
this didn't work
this works
foreach (Device obj in devicesBlock)
{
if (obj.ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
}
foreach (Device obj in devicesBlock)
{
if (obj.ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
}
if(devicesBlock.Any(attribute=>attribute.getName()=="ConnectionType.USB"))
{
do stuff
}
if(devicesBlock.Any(attribute=>attribute.getName()=="ConnectionType.USB"))
{
do stuff
}
ConnectDevice(device);
right now i'm just doing Device device = devicesBlock.First();
what I want to do is search the device list List<Device> devicesBlock = _api.GetDevicesBlock();
and connect by priority of connection type, USB first, the ethernet, then Bluetooth, then wifi or wifidirect
because right now when I connect via usb and wifi is on, wifi is at the top of the list and devicesBlock.First();
always picks wifi with weaker connection than usb
maybe I can get the index of the list and use that?
trying this int deviceIndex;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}
int deviceIndex;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}
int deviceIndex = 0;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || devicesBlock[i].ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}
device = (Device)devicesBlock.Select((Device, index) => index = deviceIndex);
int deviceIndex = 0;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || devicesBlock[i].ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}
device = (Device)devicesBlock.Select((Device, index) => index = deviceIndex);
int deviceIndex = devicesBlock.FindIndex(d => d.ConnectionType == ConnectionType.USB);
Program.Log("Device index: " + deviceIndex);
just need to figure out how to select from an index to connect
device = (Device)devicesBlock.Select((Device, index) => index = deviceIndex);
didn't work
!closeClosed!