public async Task <T> EstablishDeviceConnectionByPort <T>(byte port) where T : IDevice { while (!ConnectedDevices.ContainsKey(port) || !ConnectedDevices[port].IsReady) { await Task.Delay(250); } return((T)ConnectedDevices[port]); }
public Guid DeviceDescriptorToInstanceGuid(DeviceDescriptor deviceDescriptor) { if (ConnectedDevices.ContainsKey(deviceDescriptor.DeviceHandle) && ConnectedDevices[deviceDescriptor.DeviceHandle].Count >= deviceDescriptor.DeviceInstance) { return(ConnectedDevices[deviceDescriptor.DeviceHandle][deviceDescriptor.DeviceInstance]); } throw new Exception($"Could not find device Handle {deviceDescriptor.DeviceHandle}, Instance {deviceDescriptor.DeviceInstance}"); //return Guid.Empty; }
public void RefreshConnectedDevices() { //ConnectedDevices = new ConcurrentDictionary<string, List<Guid>>(); var diDeviceInstances = DiInstance.GetDevices(); var connectedHandles = ConnectedDevices.Keys.ToList(); foreach (var device in diDeviceInstances) { if (!IsStickType(device)) { continue; } var joystick = new Joystick(DiInstance, device.InstanceGuid); var handle = JoystickToHandle(joystick); if (connectedHandles.Contains(handle)) { connectedHandles.Remove(handle); } if (ConnectedDevices.ContainsKey(handle)) { if (ConnectedDevices[handle].Contains(device.InstanceGuid)) { continue; } } else { ConnectedDevices[handle] = new List <Guid>(); } ConnectedDevices[handle].Add(device.InstanceGuid); } foreach (var connectedHandle in connectedHandles) { ConnectedDevices.TryRemove(connectedHandle, out _); } }
public void ReceiveMessage(IMessage message) { if (message.MessageType == MessageType.Hub__Attached_IO) { var attachedIOMessage = new AttachedIOMessage(message.Bytes.ToArray()); switch (attachedIOMessage.Event) { case IOEvent.Attached_IO: switch (attachedIOMessage.DeviceType) { case IODeviceType.TechnicMotorL: ConnectedDevices[attachedIOMessage.Port] = new TechnicMotorL(this, attachedIOMessage.Port); break; case IODeviceType.TechnicMotorXL: ConnectedDevices[attachedIOMessage.Port] = new TechnicMotorXL(this, attachedIOMessage.Port); break; } break; case IOEvent.Attached_Virtual_IO: switch (attachedIOMessage.DeviceType) { case IODeviceType.LED_Light: ConnectedDevices[attachedIOMessage.Port] = new LED(this, attachedIOMessage.Port); break; } break; } } else if (message.MessageType == MessageType.Port_Information) { var portInformationMessage = new PortInformationMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portInformationMessage.Port)) { ConnectedDevices[portInformationMessage.Port].ReceiveMessage(portInformationMessage); } } else if (message.MessageType == MessageType.Port_Mode_Information) { var portModeInformationMessage = new PortModeInformationMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portModeInformationMessage.Port)) { ConnectedDevices[portModeInformationMessage.Port].ReceiveMessage(portModeInformationMessage); } } else if (message.MessageType == MessageType.Port_Input_Format__Single) { var portInputFormatMessage = new PortInputFormatSingleMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portInputFormatMessage.Port)) { ConnectedDevices[portInputFormatMessage.Port].ReceiveMessage(portInputFormatMessage); } } else if (message.MessageType == MessageType.Port_Input_Format__Combined_Mode) { var portInputFormatMessage = new PortInputFormatCombinedMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portInputFormatMessage.Port)) { ConnectedDevices[portInputFormatMessage.Port].ReceiveMessage(portInputFormatMessage); } } else if (message.MessageType == MessageType.Port_Value__Single) { var portValueMessage = new PortValueSingleMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portValueMessage.Port)) { ConnectedDevices[portValueMessage.Port].ReceiveMessage(portValueMessage); } } else if (message.MessageType == MessageType.Port_Value__Combined_Mode) { var portValueMessage = new PortValueCombinedMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portValueMessage.Port)) { ConnectedDevices[portValueMessage.Port].ReceiveMessage(portValueMessage); } } else { Messages.Enqueue(message); } }