Skip to content

Commit

Permalink
Added readme.todo, add exceptions in adb commands.
Browse files Browse the repository at this point in the history
Renamed Functions and extracted more helpers.
  • Loading branch information
vinicioslc committed Dec 15, 2019
1 parent f8a468f commit 47a63d3
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ An simple adb extesion that makes possible to connect to an device over wifi con
# TODO

- [x] easy test and mockable terminal interface
- [ ] unit tests in everywhere 100% Code Coverage
- [ ] add exceptions instead ADBResultState.Error
- [ ] unit tests in everywhere 100% Code Coverage currently 76.8%
- [ ] more easy architecture and easy to extend features
- [ ] clean arquitecture for every module
2 changes: 1 addition & 1 deletion src/__tests__/list-adb-suite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('Test ADB Listed Devices', async () => {
List of devices attached
`)
const result = await adbInterfaceInstance.GetConnectedDevices()
const result = await adbInterfaceInstance.FindConnectedDevices()

expect(typeof result).toStrictEqual(typeof Array())
})
6 changes: 4 additions & 2 deletions src/adb-manager/adb-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default {
CONNECT_IP_AND_PORT: (deviceIP: string = '192.168.1.100', port = '5555') =>
`adb connect ${deviceIP}:${port}`,
RESET_PORTS: (port = 5555) => `adb tcpip ${port}`,
SHELL_GETPROP_ROPRODUCTMODEL: (deviceIP = '192.168.1.100') =>
`adb -s ${deviceIP} shell getprop ro.product.model`
GET_DEVICE_MODEL: (deviceIP = '192.168.1.100') =>
`adb -s ${deviceIP} shell getprop ro.product.model`,
GET_DEVICE_MANUFACTURER: (deviceIP = '192.168.1.100') =>
`adb -s ${deviceIP} shell getprop ro.product.manufacturer`
}
3 changes: 2 additions & 1 deletion src/adb-manager/adb-returns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ missing port in specification: tcp:${ipAddress}`,
`cannot connect to ${ipAddress}:${port}: No connection could be made because the target machine actively refused it. (10061)`,
LISTING_DEVICES: () => 'List of devices attached',
NO_DEVICES_FOUND: () => `error: no devices/emulators found`,
ALLREADY_CONNECTED_TO: () => 'already connected to'
ALLREADY_CONNECTED_TO: () => 'already connected to',
ADB_KILLED_SUCCESS_RETURN: () => ''
}
22 changes: 22 additions & 0 deletions src/adb-manager/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// let ipv4PortRegex = /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):*[0-9]*/gim
import { IConsoleInterface } from './../console-interface/iconsole-interface'
import adbCommands from './adb-commands'
export class IPHelpers {
static ipv4Regex = /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/gim

Expand Down Expand Up @@ -26,3 +28,23 @@ export class IPHelpers {
return /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/gim.test(ipAddress)
}
}

export class DeviceHelpers {
static getDeviceManufacturer(
ConsoleInstance: IConsoleInterface,
ipAddress: string
): string {
return ConsoleInstance.execConsoleSync(
adbCommands.GET_DEVICE_MODEL(ipAddress)
).toLocaleString()
}

static getDeviceModel(
ConsoleInstance: IConsoleInterface,
deviceIP: string
): string {
return ConsoleInstance.execConsoleSync(
adbCommands.GET_DEVICE_MODEL(deviceIP)
).toLocaleString()
}
}
34 changes: 18 additions & 16 deletions src/adb-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import adbCommands from './adb-commands'
import { ConsoleChannel } from '../console-channel'
import adbReturns from './adb-returns'
import adbMessages from './adb-messages'
import { IPHelpers } from './helpers'
import { IPHelpers, DeviceHelpers } from './helpers'

export class ADBChannel extends ConsoleChannel {
/**
Expand All @@ -19,11 +19,14 @@ export class ADBChannel extends ConsoleChannel {
'Some error ocurred during connection'
)

const result = this.consoleInterface.execConsoleSync(
const result = this.consoleInstance.execConsoleSync(
adbCommands.CONNECT_IP_AND_PORT(deviceIP)
)
const output = result.toLocaleString()
const deviceName = this.queryDeviceName(deviceIP)
const deviceName = DeviceHelpers.getDeviceModel(
this.consoleInstance,
deviceIP
)

if (output.includes(adbReturns.CONNECTED_TO())) {
finalResult = new ADBResult(
Expand Down Expand Up @@ -58,7 +61,7 @@ export class ADBChannel extends ConsoleChannel {
'Error while reset TCP IP Ports'
)
try {
const result = this.consoleInterface.execConsoleSync(
const result = this.consoleInstance.execConsoleSync(
adbCommands.RESET_PORTS()
)
const output = result.toLocaleString()
Expand All @@ -81,19 +84,13 @@ export class ADBChannel extends ConsoleChannel {
return finalResult
}

queryDeviceName(deviceIP: string): string {
return this.consoleInterface
.execConsoleSync(adbCommands.SHELL_GETPROP_ROPRODUCTMODEL(deviceIP))
.toLocaleString()
}

async DisconnectFromAllDevices(): Promise<ADBResult> {
var finalResult = new ADBResult(
ADBResultState.Error,
'Error while reset TCPIP Ports'
)
try {
const result = this.consoleInterface.execConsoleSync(
const result = this.consoleInstance.execConsoleSync(
adbCommands.ADB_DISCONNECT_ALL()
)
const output = result.toLocaleString()
Expand All @@ -108,10 +105,10 @@ export class ADBChannel extends ConsoleChannel {
}
return finalResult
}
async GetConnectedDevices(): Promise<Array<string>> {
async FindConnectedDevices(): Promise<Array<string>> {
var devicesArray = []
try {
const result = this.consoleInterface.execConsoleSync(
const result = this.consoleInstance.execConsoleSync(
adbCommands.LIST_ADB_DEVICES()
)
const output = result.toLocaleString()
Expand All @@ -120,7 +117,10 @@ export class ADBChannel extends ConsoleChannel {
ips = ips.filter(ip => IPHelpers.isAnIPAddress(ip))
ips = ips.map(ipAddress => {
let deviceIP = IPHelpers.extractIPRegex(ipAddress)
let nameOfDevice = this.queryDeviceName(deviceIP)
let nameOfDevice = DeviceHelpers.getDeviceModel(
this.consoleInstance,
deviceIP
)
return `${deviceIP} | ${nameOfDevice}`
})
// found devices on lan
Expand All @@ -137,11 +137,13 @@ export class ADBChannel extends ConsoleChannel {
async KillADBServer(): Promise<ADBResult> {
let returned = new ADBResult(ADBResultState.Error, 'Fail during ADB Kill')
try {
const result = this.consoleInterface.execConsoleSync(
const result = this.consoleInstance.execConsoleSync(
adbCommands.ADB_KILL_SERVER()
)
if (result.toLocaleString() == '') {
if (result.toLocaleString() == adbReturns.ADB_KILLED_SUCCESS_RETURN()) {
returned = new ADBResult(ADBResultState.Success, 'ADB Server killed')
} else {
throw Error('Internal error ocurred')
}
} catch (e) {
returned.message = 'Fail \n ' + e.message
Expand Down
6 changes: 3 additions & 3 deletions src/console-channel/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IConsoleInterface } from '../console-interface/iconsole-interface'

export class ConsoleChannel {
consoleInterface: IConsoleInterface
consoleInstance: IConsoleInterface
constructor(CiInstance: IConsoleInterface) {
this.consoleInterface = CiInstance
this.consoleInstance = CiInstance
}
sendCommandSync(consoleCommand: string): Buffer {
return this.consoleInterface.execConsoleSync(consoleCommand)
return this.consoleInstance.execConsoleSync(consoleCommand)
}
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function activate(context: vscode.ExtensionContext) {
},
{
name: 'adbInterface.disableFirebaseDebug',
callback: () => DisableFirebaseDebugView(context)
callback: () => DisableFirebaseDebugView()
},
{
name: 'adbInterface.killserver',
Expand Down
2 changes: 1 addition & 1 deletion src/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function ConnectToDeviceFromList(
}

async function getIPAddressList(context) {
const connectedDevices = await adbInstance.GetConnectedDevices()
const connectedDevices = await adbInstance.FindConnectedDevices()
const lastIPSelected = await context.globalState.get(
globalStateKeys.lastIPUsed,
''
Expand Down
4 changes: 2 additions & 2 deletions src/firebase-actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class FirebaseManagerChannel extends ConsoleChannel {
enableFirebaseDebugView(appPackageID: string) {
let finalResult = new ADBResult(ADBResultState.Error, 'Error ocurred')

const output = this.consoleInterface
const output = this.consoleInstance
.execConsoleSync(
firebaseCommands.SHELL_SETPROP_FIREBASE_ANALYTICS(appPackageID)
)
Expand All @@ -23,7 +23,7 @@ export class FirebaseManagerChannel extends ConsoleChannel {
disableFirebaseDebugView() {
let finalResult = new ADBResult(ADBResultState.Error, 'Some Error Ocurred')

const output: String = this.consoleInterface
const output: String = this.consoleInstance
.execConsoleSync(firebaseCommands.DISABLE_FIREBASE_ANALYTICS())
.toLocaleString()

Expand Down

0 comments on commit 47a63d3

Please sign in to comment.