/// <summary> /// Sends a DeviceMessage (e.g. <see cref="VibrateCmd"/> or <see cref="LinearCmd"/>). Handles /// constructing some parts of the message for the user. /// </summary> /// <param name="aDevice">The device to be controlled by the message.</param> /// <param name="aDeviceMsg">The device message (Id and DeviceIndex will be overriden).</param> /// <returns> /// <see cref="Ok"/> message on success, <see cref="Error"/> message with error info otherwise. /// </returns> public async Task <ButtplugMessage> SendDeviceMessage(ButtplugClientDevice aDevice, ButtplugDeviceMessage aDeviceMsg) { if (!_devices.TryGetValue(aDevice.Index, out ButtplugClientDevice dev)) { return(new Error("Device not available.", Error.ErrorClass.ERROR_DEVICE, ButtplugConsts.SystemMsgId)); } if (!dev.AllowedMessages.ContainsKey(aDeviceMsg.GetType().Name)) { return(new Error("Device does not accept message type: " + aDeviceMsg.GetType().Name, Error.ErrorClass.ERROR_DEVICE, ButtplugConsts.SystemMsgId)); } aDeviceMsg.DeviceIndex = aDevice.Index; return(await SendMessage(aDeviceMsg)); }
public async Task SendMessageAsync(ButtplugDeviceMessage aMsg, CancellationToken aToken = default(CancellationToken)) { ButtplugUtils.ArgumentNotNull(aMsg, nameof(aMsg)); if (!_owningClient.Connected) { throw new ButtplugClientConnectorException(_bpLogger, "Client that owns device is not connected"); } if (!_owningClient.Devices.Contains(this)) { throw new ButtplugDeviceException(_bpLogger, "Device no longer connected or valid"); } if (!AllowedMessages.ContainsKey(aMsg.GetType())) { throw new ButtplugDeviceException(_bpLogger, $"Device {Name} does not support message type {aMsg.GetType().Name}"); } aMsg.DeviceIndex = Index; await _sendClosure(this, aMsg, aToken).ConfigureAwait(false); }
public Task <ButtplugMessage> ParseMessageAsync(ButtplugDeviceMessage aMsg, CancellationToken aToken = default) { if (!Connected) { throw new ButtplugDeviceException(_bpLogger, $"{Name} has disconnected and can no longer process messages.", aMsg.Id); } if (!_msgTypes.ContainsKey(aMsg.GetType())) { throw new ButtplugDeviceException(_bpLogger, $"{Name} cannot handle message of type {aMsg.GetType().Name}", aMsg.Id); } switch (aMsg) { case SingleMotorVibrateCmd cmd: { var changed = false; var newSpeed = Convert.ToUInt16(Math.Min(cmd.Speed * 255, 255)); for (var i = 0; i < _speeds.Length; i++) { if (_speeds[i] == newSpeed) { continue; } _speeds[i] = newSpeed; changed = true; } if (changed) { _manager.sendPwm(_deviceNumber, _speeds); } break; } case StopDeviceCmd _: { for (var i = 0; i < _speeds.Length; i++) { _speeds[i] = 0; } _manager.sendPwm(_deviceNumber, _speeds); break; } case VibrateCmd cmd: { var changed = false; foreach (var vib in cmd.Speeds) { if (vib.Index >= 4) { continue; } var newSpeed = Convert.ToUInt16(Math.Min(vib.Speed * 255, 255)); if (_speeds[vib.Index] == newSpeed) { continue; } _speeds[vib.Index] = newSpeed; changed = true; } if (changed) { _manager.sendPwm(_deviceNumber, _speeds); } break; } } return(Task.FromResult <ButtplugMessage>(new Ok(aMsg.Id))); }