Skip to content

Commit

Permalink
Fix nextMsgId and add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyustorm authored and blackspherefollower committed Nov 19, 2024
1 parent 1c21523 commit 5d7738d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.github.blackspherefollower.buttplug4j.connectors.jetty.websocket.client;

import io.github.blackspherefollower.buttplug4j.client.ButtplugClientDevice;
import io.github.blackspherefollower.buttplug4j.client.ButtplugDeviceException;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ButtplugClientWSJettyClientTest {
Expand Down Expand Up @@ -50,4 +53,29 @@ public void TestConnect() throws Exception {

client.disconnect();
}

@Disabled
@Test
public void TestBattery() throws Exception {
ButtplugClientWSClient client = new ButtplugClientWSClient("Java Test");
client.connect(new URI("ws://localhost:12345/buttplug"));
client.startScanning();

Thread.sleep(2000);
client.requestDeviceList();
for (ButtplugClientDevice dev : client.getDevices()) {
if (dev.hasBatterySensor()) {
long battery = dev.readBatteryLevel();
System.out.println("Battery is " + battery);
assertTrue(battery >= 0);
assertTrue(battery <= 100);
} else {
assertThrows(ButtplugDeviceException.class, () -> {
long battery = dev.readBatteryLevel();
});
}
}

client.disconnect();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.blackspherefollower.buttplug4j.client;

import io.github.blackspherefollower.buttplug4j.protocol.ButtplugConsts;
import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage;
import io.github.blackspherefollower.buttplug4j.protocol.messages.*;
import io.github.blackspherefollower.buttplug4j.protocol.messages.Parts.*;
Expand All @@ -11,6 +10,8 @@
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


public class ButtplugClientDevice {
Expand Down Expand Up @@ -413,7 +414,7 @@ public final Future<ButtplugMessage> sendSensorReadCmd(final int sensorIndex, fi
throw new ButtplugDeviceException("Device doesn't support SensorReadCmd!");
}

final SensorReadCmd cmd = new SensorReadCmd(this.deviceIndex, ButtplugConsts.DEFAULT_MSG_ID);
final SensorReadCmd cmd = new SensorReadCmd(this.deviceIndex, client.getNextMsgId());
cmd.setSensorType(sensorType);
cmd.setSensorIndex(sensorIndex);

Expand Down Expand Up @@ -460,8 +461,9 @@ public final boolean hasBatterySensor() {
* does not have a "Battery" feature, or returns an invalid response.
* @throws InterruptedException if the thread is interrupted while waiting for a response from the device.
* @throws ExecutionException if an exception occurred during the execution of the sensor read command.
* @throws TimeoutException if the response from the device took more than 2 seconds
*/
public final long readBatteryLevel() throws ButtplugDeviceException, InterruptedException, ExecutionException {
public final long readBatteryLevel() throws ButtplugDeviceException, InterruptedException, ExecutionException, TimeoutException {
MessageAttributes attrs = getDeviceMessages().get("SensorReadCmd");
if (!(attrs instanceof SensorMessageAttributes)) {
throw new ButtplugDeviceException("Device doesn't support SensorReadCmd!");
Expand All @@ -483,7 +485,7 @@ public final long readBatteryLevel() throws ButtplugDeviceException, Interrupted
}

Future<ButtplugMessage> sensorReadFuture = sendSensorReadCmd(index, "Battery");
ButtplugMessage message = sensorReadFuture.get();
ButtplugMessage message = sensorReadFuture.get(2, TimeUnit.SECONDS);
if (!(message instanceof SensorReading)) {
throw new ButtplugDeviceException("Invalid ButtplugMessage returned. Expecting SensorReading and got " + message.getClass());
}
Expand Down

0 comments on commit 5d7738d

Please sign in to comment.