-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Connect command divided into three connectors * FileParamsDeviceConnector created * FileParamsDeviceConnector readParamsFromFile_validFullFile_exactMatching test implemented * readme updated with new -f parameter * readme updated with new -f parameter description * version updated to 0.3.2
- Loading branch information
Showing
25 changed files
with
559 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
client/src/main/java/com/github/e13mort/stf/console/commands/CommandContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 0 additions & 133 deletions
133
client/src/main/java/com/github/e13mort/stf/console/commands/ConnectCommand.java
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
.../src/main/java/com/github/e13mort/stf/console/commands/connect/CacheDevicesConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.github.e13mort.stf.console.commands.connect; | ||
|
||
import com.github.e13mort.stf.adapter.filters.InclusionType; | ||
import com.github.e13mort.stf.adapter.filters.StringsFilterDescription; | ||
import com.github.e13mort.stf.client.DevicesParams; | ||
import com.github.e13mort.stf.client.DevicesParamsImpl; | ||
import com.github.e13mort.stf.client.FarmClient; | ||
import com.github.e13mort.stf.console.AdbRunner; | ||
import com.github.e13mort.stf.console.commands.cache.DeviceListCache; | ||
import com.github.e13mort.stf.model.device.Device; | ||
import io.reactivex.Flowable; | ||
import io.reactivex.Notification; | ||
import org.reactivestreams.Publisher; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
class CacheDevicesConnector extends DeviceConnector { | ||
|
||
private final List<Integer> devicesIndexesFromCache; | ||
private final DeviceListCache cache; | ||
|
||
protected CacheDevicesConnector(FarmClient client, AdbRunner runner, Logger logger, List<Integer> devicesIndexesFromCache, DeviceListCache cache) { | ||
super(client, runner, logger); | ||
this.devicesIndexesFromCache = devicesIndexesFromCache; | ||
this.cache = cache; | ||
} | ||
|
||
@Override | ||
protected Publisher<Notification<String>> createConnectionPublisher() { | ||
return readParamsFromCache(cache.getCachedFiles()).flatMap(this::connectWithParams); | ||
} | ||
|
||
private Flowable<DevicesParams> readParamsFromCache(List<Device> cachedFiles) { | ||
return Flowable.fromIterable(devicesIndexesFromCache) | ||
.doOnNext(integer -> validate(cachedFiles, integer)) | ||
.map(integer -> --integer) | ||
.map(cachedFiles::get) | ||
.map(Device::getSerial) | ||
.toList() | ||
.map(this::createStringsFilterDescription) | ||
.map(this::createDevicesParams) | ||
.toFlowable(); | ||
} | ||
|
||
private void validate(List<Device> cachedFiles, int index) throws InvalidCacheIndexException { | ||
if (!isIndexValid(cachedFiles, index)) { | ||
throw new InvalidCacheIndexException(index); | ||
} | ||
} | ||
|
||
private boolean isIndexValid(List<Device> cachedFiles, Integer integer) { | ||
return integer > 0 && integer <= cachedFiles.size(); | ||
} | ||
|
||
private StringsFilterDescription createStringsFilterDescription(List<String> l) { | ||
return new StringsFilterDescription(InclusionType.INCLUDE, l); | ||
} | ||
|
||
private DevicesParams createDevicesParams(StringsFilterDescription filter) { | ||
final DevicesParamsImpl params = new DevicesParamsImpl(); | ||
params.setSerialFilterDescription(filter); | ||
return params; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
client/src/main/java/com/github/e13mort/stf/console/commands/connect/ConnectCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.e13mort.stf.console.commands.connect; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
import com.beust.jcommander.ParametersDelegate; | ||
import com.beust.jcommander.converters.FileConverter; | ||
import com.beust.jcommander.converters.IntegerConverter; | ||
import com.github.e13mort.stf.client.FarmClient; | ||
import com.github.e13mort.stf.console.AdbRunner; | ||
import com.github.e13mort.stf.console.commands.CommandContainer; | ||
import com.github.e13mort.stf.console.commands.ConsoleDeviceParamsImpl; | ||
import com.github.e13mort.stf.console.commands.cache.DeviceListCache; | ||
import io.reactivex.Completable; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
@Parameters(commandDescription = "Connect to devices") | ||
public class ConnectCommand implements CommandContainer.Command { | ||
private final FarmClient client; | ||
private final AdbRunner adbRunner; | ||
private final Logger logger; | ||
|
||
@ParametersDelegate | ||
private ConsoleDeviceParamsImpl params = new ConsoleDeviceParamsImpl(); | ||
@Parameter(names = "--my", description = "Connect to currently taken devices") | ||
private Boolean connectToMyDevices = false; | ||
@Parameter(names = "-l", variableArity = true, listConverter = IntegerConverter.class, | ||
description = "Connect to devices by its indexes from the results of previous \"devices\" command. E.g. \"-l 1 2 5\"") | ||
private List<Integer> devicesIndexesFromCache = new ArrayList<>(); | ||
@Parameter(names = "-f", description = "Read connection params from a file", converter = FileConverter.class) | ||
private File storedConnectionParamsFile; | ||
private DeviceListCache cache; | ||
|
||
public ConnectCommand(FarmClient client, AdbRunner adbRunner, DeviceListCache cache, Logger logger) { | ||
this.client = client; | ||
this.adbRunner = adbRunner; | ||
this.cache = cache; | ||
this.logger = logger; | ||
} | ||
|
||
@Override | ||
public Completable execute() { | ||
DeviceConnector connector = chooseConnector(); | ||
return connector.connect(); | ||
} | ||
|
||
void setDevicesIndexesFromCache(List<Integer> devicesIndexesFromCache) { | ||
this.devicesIndexesFromCache = devicesIndexesFromCache; | ||
} | ||
|
||
private DeviceConnector chooseConnector() { | ||
DeviceConnector connector; | ||
if (connectToMyDevices) { | ||
connector = new MyDevicesConnector(client, adbRunner, logger); | ||
} else if (!devicesIndexesFromCache.isEmpty()) { | ||
connector = new CacheDevicesConnector(client, adbRunner, logger, devicesIndexesFromCache, cache); | ||
} else if (storedConnectionParamsFile != null) { | ||
connector = new FileParamsDeviceConnector(client, adbRunner, logger, storedConnectionParamsFile); | ||
} else { | ||
connector = new ParamsConnector(client, adbRunner, logger, params); | ||
} | ||
return connector; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
client/src/main/java/com/github/e13mort/stf/console/commands/connect/DeviceConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.github.e13mort.stf.console.commands.connect; | ||
|
||
import com.github.e13mort.stf.client.DevicesParams; | ||
import com.github.e13mort.stf.client.FarmClient; | ||
import com.github.e13mort.stf.console.AdbRunner; | ||
import com.github.e13mort.stf.console.commands.EmptyDevicesException; | ||
import io.reactivex.Completable; | ||
import io.reactivex.Flowable; | ||
import io.reactivex.Notification; | ||
import org.reactivestreams.Publisher; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
abstract class DeviceConnector { | ||
protected final FarmClient client; | ||
protected final AdbRunner runner; | ||
protected final Logger logger; | ||
|
||
protected DeviceConnector(FarmClient client, AdbRunner runner, Logger logger) { | ||
this.client = client; | ||
this.runner = runner; | ||
this.logger = logger; | ||
} | ||
|
||
Completable connect() { | ||
return Completable.fromPublisher(createConnectionPublisher()); | ||
} | ||
|
||
protected abstract Publisher<Notification<String>> createConnectionPublisher(); | ||
|
||
protected Flowable<Notification<String>> getEmptyError() { | ||
return Flowable.error(new EmptyDevicesException()); | ||
} | ||
|
||
protected void handleDevices(Notification<String> deviceNotification) { | ||
if (deviceNotification.isOnNext()) { | ||
handleConnectedDevice(deviceNotification.getValue()); | ||
} else if (deviceNotification.isOnError()) { | ||
handleNotConnectedDevice(deviceNotification.getError()); | ||
} | ||
} | ||
|
||
protected void handleConnectedDevice(String deviceIp) { | ||
try { | ||
runner.connectToDevice(deviceIp); | ||
} catch (IOException e) { | ||
logger.log(Level.WARNING, "Failed to connect to a device", e); | ||
} | ||
} | ||
|
||
protected void handleNotConnectedDevice(Throwable error) { | ||
logger.log(Level.WARNING, "Failed to lock a device", error); | ||
} | ||
|
||
protected Flowable<Notification<String>> connectWithParams(DevicesParams params) { | ||
return client.connectToDevices(params) | ||
.doOnNext(this::handleDevices) | ||
.switchIfEmpty(getEmptyError()); | ||
} | ||
} |
Oops, something went wrong.