Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from snieguu/master
Browse files Browse the repository at this point in the history
Version 2.85, pass ADB version to device session
  • Loading branch information
lastverb committed Nov 7, 2019
2 parents f9bfcf0 + 25e1e14 commit 9eb4b2b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

<groupId>com.bitbar</groupId>
<artifactId>remote-device-client</artifactId>
<version>2.84</version>
<version>2.85</version>
<packaging>jar</packaging>

<properties>
<java.version>1.8</java.version>
<testdroid-api.version>2.84</testdroid-api.version>
<testdroid-api.version>2.85</testdroid-api.version>
<cli.version>1.3.1</cli.version>
<jsch.version>0.1.55</jsch.version>
<springboot.version>2.2.0.RELEASE</springboot.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class RemoteDeviceClientMain {

private static Object keyboardLock = new Object();

private static String ADB_VERSION;

private RemoteDeviceClientMain(CommandLine commandLine) throws RequiredParameterIsEmptyException, APIException {
String cloudUrl = commandLine.getOptionValue(CommandLineParameter.CLOUD_URI.getArgument());
String apiKey = commandLine.getOptionValue(CommandLineParameter.API_KEY.getArgument());
Expand Down Expand Up @@ -130,8 +128,6 @@ private void devices() {

private void connect(CommandLine commandLine)
throws RequiredParameterIsEmptyException, APIException, WrongParameterException {
ADB_VERSION = computeAdbVersion(commandLine);
LOGGER.info("ADB version detected: {}", ADB_VERSION);
Long deviceModelId;
try {
deviceModelId = Long.parseLong(commandLine.getOptionValue(CommandLineParameter.DEVICE_MODEL_ID.getArgument()));
Expand All @@ -151,16 +147,18 @@ private void connect(CommandLine commandLine)
Runtime.getRuntime().addShutdownHook(new Thread(this::finishDeviceSessions));

if (device.getOsType() == APIDevice.OsType.ANDROID) {
String adbVersion = computeAdbVersion(commandLine);
LOGGER.info("ADB version provided/detected: {}", adbVersion);
try {
checkPortFree(PortForwardingParameters.LOCAL_PORT);
APIDeviceSession apiSession = apiClientManager.createDeviceSession(deviceModelId);
APIDeviceSession apiSession = apiClientManager.createDeviceSession(deviceModelId, adbVersion);
remoteDeviceSessions.put(apiSession.getId(),
new RemoteAndroidDeviceSession(websocketManager, apiSession));
} catch (PortNotFreeException e) {
LOGGER.error(e.getMessage());
}
} else if (device.getOsType() == APIDevice.OsType.IOS) {
APIDeviceSession apiSession = apiClientManager.createDeviceSession(deviceModelId);
APIDeviceSession apiSession = apiClientManager.createDeviceSession(deviceModelId, null);
remoteDeviceSessions.put(apiSession.getId(),
new RemoteIOSDeviceSession(websocketManager, apiSession));
} else {
Expand Down
64 changes: 24 additions & 40 deletions src/main/java/com/bitbar/remotedevice/api/APIClientManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
import com.testdroid.api.*;
import com.testdroid.api.dto.Context;
import com.testdroid.api.dto.MappingKey;
import com.testdroid.api.dto.Operand;
import com.testdroid.api.filter.FilterEntry;
import com.testdroid.api.filter.ListStringFilterEntry;
import com.testdroid.api.filter.NumberFilterEntry;
import com.testdroid.api.filter.StringFilterEntry;
import com.testdroid.api.model.*;
import com.testdroid.api.model.APICloudInfo;
import com.testdroid.api.model.APIDevice;
import com.testdroid.api.model.APIDeviceSession;
import com.testdroid.api.model.APIDeviceSessionConfig;
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

import static com.bitbar.remotedevice.StaticParameters.DEVICE_SESSIONS_URI;
import static com.bitbar.remotedevice.StaticParameters.RELEASE_DS_URI;
import static com.testdroid.api.dto.MappingKey.LABEL_IDS_ARR;
import static com.testdroid.api.dto.MappingKey.*;
import static com.testdroid.api.dto.Operand.EQ;
import static com.testdroid.api.dto.Operand.IN;
import static com.testdroid.api.model.APIDevice.OsType.ANDROID;
import static com.testdroid.api.model.APIDevice.OsType.IOS;
import static com.testdroid.api.model.APIDeviceSession.Type.REMOTE;

public class APIClientManager {

private static final String DEVICE_MODEL_ID_PARAM = "deviceModelId";

private static final String INFO_URI = "/info";

private static final String TYPE_PARAM = "type";

private static final String TYPE_PARAM_VALUE = "REMOTE";

private APIClient apiClient;

public APIClientManager(String cloudUrl, String apiKey) throws RequiredParameterIsEmptyException {
Expand All @@ -45,27 +44,14 @@ private APIClient createAPIClient(String cloudUrl, String apiKey) throws Require
return new APIKeyClient(cloudUrl, apiKey);
}

private Optional<Long> getRemoteSessionsLabelId() throws APIException {
Optional<Long> result = Optional.empty();
Context<APILabelGroup> ctx = new Context<>(APILabelGroup.class);
ctx.addFilter(new StringFilterEntry(MappingKey.NAME, Operand.EQ, "supported-frameworks"));
List<APILabelGroup> labelGroups = apiClient.getLabelGroups(ctx).getEntity().getData();
if (labelGroups.size() > 0) {
Context<APIDeviceProperty> lCtx = new Context<>(APIDeviceProperty.class);
lCtx.addFilter(new StringFilterEntry(MappingKey.NAME, Operand.EQ, "remote-session"));
result = labelGroups.get(0).getDevicePropertiesResource(lCtx).getEntity().getData()
.stream().findFirst().map(APIEntity::getId);
}
return result;
}

public List<APIDevice> getSupportedDevices() throws APIException {
Context<APIDevice> ctx = new Context<>(APIDevice.class);
ctx.getFilters().add(new ListStringFilterEntry(MappingKey.OS_TYPE, Operand.IN,
Arrays.asList(APIDevice.OsType.ANDROID.getDisplayName(), APIDevice.OsType.IOS.getDisplayName())));
getRemoteSessionsLabelId().ifPresent(val -> ctx.setExtraParams(
new HashSetValuedHashMap<>(Collections.singletonMap(LABEL_IDS_ARR, val))
));
ctx.getFilters().add(new ListStringFilterEntry(OS_TYPE, IN,
Arrays.asList(ANDROID.getDisplayName(), IOS.getDisplayName())));
apiClient.findDevicePropertyInLabelGroup("supported-frameworks", "remote-session").
ifPresent(val -> ctx.setExtraParams(
new HashSetValuedHashMap<>(Collections.singletonMap(LABEL_IDS_ARR, val.getId()))
));
ctx.setLimit(0);
List<APISort.SortItem> sortItems = new ArrayList<>();
sortItems.add(new APISort.SortItem(MappingKey.DISPLAY_NAME, APISort.Type.ASC));
Expand All @@ -76,30 +62,28 @@ public List<APIDevice> getSupportedDevices() throws APIException {
public APIDevice getDevice(Long id) throws APIException {
Context<APIDevice> ctx = new Context<>(APIDevice.class);
List<FilterEntry> filters = ctx.getFilters();
filters.add(new NumberFilterEntry(MappingKey.ID, Operand.EQ, id));
filters.add(new NumberFilterEntry(ID, EQ, id));
APIList<APIDevice> devices = apiClient.getDevices(ctx).getEntity();
if (devices.isEmpty()) {
throw new APIException(String.format("Could not find device with id %d", id));
}
return devices.get(0);
}

public APIDeviceSession createDeviceSession(Long deviceModelId)
public APIDeviceSession createDeviceSession(Long deviceModelId, String adbVersion)
throws APIException, RequiredParameterIsEmptyException {
if (deviceModelId == null) {
throw new RequiredParameterIsEmptyException(CommandLineParameter.DEVICE_MODEL_ID);
}

Map<String, Object> parameters = new HashMap<>();
parameters.put(DEVICE_MODEL_ID_PARAM, deviceModelId);
parameters.put(TYPE_PARAM, TYPE_PARAM_VALUE);

APIDeviceSession deviceSession = apiClient.post(DEVICE_SESSIONS_URI, parameters, APIDeviceSession.class);
return deviceSession;
APIDeviceSessionConfig config = new APIDeviceSessionConfig();
config.setDeviceModelId(deviceModelId);
config.setType(REMOTE);
Optional.ofNullable(adbVersion).ifPresent(config::setAdbVersion);
return apiClient.post(DEVICE_SESSIONS_URI, config, APIDeviceSession.class);
}

public void releaseDeviceSession(APIDeviceSession deviceSession) throws APIException {
apiClient.post(String.format(RELEASE_DS_URI, deviceSession.getId()), null, APIDeviceSession.class);
deviceSession.release();
}

public String getBackendUrl() throws APIException {
Expand Down

0 comments on commit 9eb4b2b

Please sign in to comment.