public void Poll() { while (_stream.DataAvailable) { var reader = new BinaryReader(_stream); var message = reader.DeserializePlist(); var deviceId = (NSNumber)message["DeviceID"]; var messageType = (NSString)message["MessageType"]; if (messageType == "Attached") { var properties = (NSMutableDictionary)message["Properties"]; var productId = (NSNumber)properties["ProductID"]; var serialNumber = (NSString)properties["SerialNumber"]; var locationId = (NSNumber)properties["LocationID"]; var device = new Device(deviceId.UInt32Value, (ushort)productId, UsbMux.StringToByteArray(serialNumber.ToString()), (uint)locationId); if (_devices.Find(x => x.DeviceID == device.DeviceID) != null) { throw new Exception("Adding an element twice!"); } _devices.Add(device); if (DeviceAttached != null) { DeviceAttached(this, new DeviceEventArgs() { Device = device }); } } else if (messageType == "Detached") { var device = _devices.Find(x => x.DeviceID == deviceId.UInt32Value); if (device == null) { throw new Exception("Removing non-existent device!"); } _devices.Remove(device); if (DeviceDetached != null) { DeviceDetached(this, new DeviceEventArgs() { Device = device }); } } } }
public DeviceListener() { _stream = UsbMux.Listen(); }