Skip to content

Commit

Permalink
Merge pull request #927 from ignacionar/#924
Browse files Browse the repository at this point in the history
#923 and #924 fixes, storing info. on first session
  • Loading branch information
vdelendik authored Mar 18, 2024
2 parents 15480e0 + 71fef46 commit adbf194
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 119 deletions.
32 changes: 22 additions & 10 deletions lib/db/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1527,18 +1527,24 @@ dbapi.setDeviceIosVersion = function(message) {
))
})
}
dbapi.sizeIosDevice = function(message) {
return db.run(r.table('devices').get(message.id).update({
dbapi.sizeIosDevice = function(serial, height, width, scale) {
return db.run(r.table('devices').get(serial).update({
display: {
height: message.height,
width: message.width,
scale: message.scale
height: height,
width: width,
scale: scale
}
}
))
}
dbapi.getDeviceSize = function(serial) {
return db.run(r.table('devices').get(serial).pluck('display'))
return db.run(r.table('devices').get(serial)).then(result => {
if (!result) {
return null
} else {
return result.display
}
})
}
dbapi.checkIosDeviceConnected = function(data) {
return db.run(r.table('devices').get(data.id))
Expand Down Expand Up @@ -1625,14 +1631,20 @@ dbapi.setDeviceGroupOwner = function(message) {
}}))
}

dbapi.setDeviceType = function(message) {
return db.run(r.table('devices').get(message.serial).update({
deviceType: message.type
dbapi.setDeviceType = function(serial, type) {
return db.run(r.table('devices').get(serial).update({
deviceType: type
}))
}

dbapi.getDeviceType = function(serial) {
return db.run(r.table('devices').get(serial).pluck('deviceType'))
return db.run(r.table('devices').get(serial)).then(result => {
if (!result) {
return null
} else {
return result.deviceType
}
})
}

dbapi.initializeIosDeviceState = function(publicIp, message) {
Expand Down
75 changes: 75 additions & 0 deletions lib/units/ios-device/plugins/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const wire = require('../../../wire')
const wireutil = require('../../../wire/util')
const grouputil = require('../../../util/grouputil')
const lifecycle = require('../../../util/lifecycle')
const dbapi = require('../../../db/api')
const request = require('request-promise')
const iosutil = require('./util/iosutil')

module.exports = syrup.serial()
.dependency(require('./solo'))
Expand All @@ -15,6 +18,7 @@ module.exports = syrup.serial()
.dependency(require('../support/channels'))
.define((options, solo, router, push, sub, channels) => {
const log = logger.createLogger('device:plugins:group')
const baseUrl = iosutil.getUri(options.wdaHost, options.wdaPort)
let currentGroup = null
let plugin = new events.EventEmitter()

Expand Down Expand Up @@ -55,6 +59,77 @@ module.exports = syrup.serial()
))
])

const handleRequest = (reqOptions) => {
return new Promise((resolve, reject) => {
request(reqOptions)
.then((res) => {
resolve(res)
})
.catch((err) => {
reject(err)
})
})
}

dbapi.loadDeviceBySerial(options.serial).then((device) => {
// No device version = First device session
if (!device.version) {
// Get device type
handleRequest({
method: 'GET',
uri: `${baseUrl}/wda/device/info`,
json: true
})
.then((deviceInfo) => {
log.info('Storing device type value: ' + deviceInfo.value.model)
let = deviceType = deviceInfo.value.model
dbapi.setDeviceType(options.serial, deviceType)
})
.catch((err) => {
log.error('Error storing device type')
return lifecycle.fatal(err)
})

// Get device size
handleRequest({
method: 'POST',
uri: `${baseUrl}/session`,
body: { capabilities: {}},
json: true,
})
.then((sessionResponse) => {
let sessionId = sessionResponse.sessionId

return handleRequest({
method: 'GET',
uri: `${baseUrl}/session/${sessionId}/window/size`,
json: true
}).then((firstSessionSize) => {
let deviceSize = firstSessionSize.value
let { width, height } = deviceSize
return handleRequest({
method: 'GET',
uri: `${baseUrl}/session/${sessionId}/wda/screen`,
}).then((scaleResponse) => {
let parsedResponse = JSON.parse(scaleResponse)
let scale = parsedResponse.value.scale

height *= scale
width *= scale

log.info('Storing device size/scale')

dbapi.sizeIosDevice(options.serial, height, width, scale)
})
})
})
.catch((err) => {
log.error('Error storing device size/scale')
return lifecycle.fatal(err)
})
}
})

plugin.emit('join', currentGroup)

return currentGroup
Expand Down
144 changes: 43 additions & 101 deletions lib/units/ios-device/plugins/wda/WdaClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const wireutil = require('../../../../wire/util')
const wire = require('../../../../wire')
const lifecycle = require('../../../../util/lifecycle')
const {exec} = require('child_process')
const dbapi = require('../../../../db/api')

const LOG_REQUEST_MSG = 'Request has been sent to WDA with data: '

Expand All @@ -34,51 +35,34 @@ module.exports = syrup.serial()
isRotating: false,
deviceType: null,

setDeviceTypeInDB: function() {
this.handleRequest({
method: 'GET',
uri: `${this.baseUrl}/wda/device/info`,
json: true
}).then((deviceInfo) => {
log.info('Storing device type value: ' + deviceInfo.value.model)
this.deviceType = deviceInfo.value.model

return push.send([
wireutil.global,
wireutil.envelope(new wire.DeviceTypeMessage(
options.serial,
deviceInfo.value.model
))
])
})
},
getDeviceType: function() {
if (this.deviceType != null) {
return this.deviceType
}

return this.handleRequest({
method: 'GET',
uri: `${options.storageUrl}api/v1/devices/${options.serial}/type`,
}).then((response) => {
let deviceType = JSON.parse(response).deviceType

return dbapi.getDeviceType(options.serial).then((deviceType) => {
if (!deviceType) {
return this.setDeviceTypeInDB()
return null
}

log.info('Reusing device type value: ', deviceType)
this.deviceType = deviceType
return this.deviceType
}).catch((err) => {
log.error('Error getting device type from DB')
return lifecycle.fatal(err)
})
},
startSession: function() {
log.info('verifying wda session status...')

this.getDeviceType()

const params = {
capabilities: {},
}

this.handleRequest({
return this.handleRequest({
method: 'POST',
uri: `${this.baseUrl}/session`,
body: params,
Expand All @@ -100,7 +84,7 @@ module.exports = syrup.serial()

this.setStatus(3)
if (this.deviceType !== 'Apple TV') {
this.getOrientation()
this.getOrientation()
}

this.setVersion(sessionResponse)
Expand All @@ -120,7 +104,6 @@ module.exports = syrup.serial()
// {"value":{"sessionId":"C4A07D30-E2E2-4922-9829-2B3ED2C4DBAE",
// "capabilities":{"device":"iphone","browserName":" ","sdkVersion":"14.7.1","CFBundleIdentifier":"com.apple.springboard"}},
// "sessionId":"C4A07D30-E2E2-4922-9829-2B3ED2C4DBAE"}

this.setVersion(sessionResponse)

this.sessionId = sessionResponse.sessionId
Expand Down Expand Up @@ -482,77 +465,44 @@ module.exports = syrup.serial()
})
}
},
setSizeInDB: function() {
return this.handleRequest({
method: 'GET',
uri: `${this.baseUrl}/session/${this.sessionId}/window/size`,
}).then((firstSessionSize) => {
this.deviceSize = JSON.parse(firstSessionSize).value
let {height, width} = this.deviceSize

return this.handleRequest({
method: 'GET',
uri: `${this.baseUrl}/session/${this.sessionId}/wda/screen`,
})
.then((wdaScreenResponse) => {
const {scale} = JSON.parse(wdaScreenResponse).value

log.info(scale)

height *= scale
width *= scale

log.info('Storing device size/scale')

push.send([
wireutil.global,
wireutil.envelope(new wire.SizeIosDevice(
options.serial,
height,
width,
scale
))
])

return this.deviceSize
})
})
},
size: function() {
if (this.deviceSize != null) {
return this.deviceSize
}
log.info('getting device window size...')

// #556: Get device size from RethinkDB
return this.handleRequest({
method: 'GET',
uri: `${options.storageUrl}api/v1/devices/${options.serial}/size`,
return dbapi.getDeviceSize(options.serial).then((deviceSize) => {

if (!deviceSize) {
return null
}

let dbHeight = deviceSize.height
let dbWidth = deviceSize.width
let dbScale = deviceSize.scale

if (!dbHeight || !dbWidth || !dbScale) {
return null
}

// Reuse DB values:
log.info('Reusing device size/scale')
// Set device size based on orientation, default is PORTRAIT
if (this.orientation === 'PORTRAIT' || !this.orientation) {
this.deviceSize = { height: dbHeight /= dbScale, width: dbWidth /= dbScale }
} else if (this.orientation === 'LANDSCAPE') {
this.deviceSize = { height: dbWidth /= dbScale, width: dbHeight /= dbScale }
} else if (this.deviceType === 'Apple TV') {
this.deviceSize = { height: dbHeight, width: dbWidth }
}
return this.deviceSize
})
.catch((err) => {
// #883: Track API errors if any
log.error('Error getting device size from DB')
return lifecycle.fatal(err)
})
.then((windowSizeResponse) => {
let { height: dbHeight, width: dbWidth, scale: dbScale } = JSON.parse(windowSizeResponse);
// First device session:
if (!dbHeight || !dbWidth || !dbScale) {
return this.setSizeInDB()
} else {
// Reuse DB values:
log.info('Reusing device size/scale')
// Set device size based on orientation, default is PORTRAIT
if (this.orientation === 'PORTRAIT' || !this.orientation) {
this.deviceSize = { height: dbHeight /= dbScale, width: dbWidth /= dbScale }
} else if (this.orientation === 'LANDSCAPE') {
this.deviceSize = { height: dbWidth /= dbScale, width: dbHeight /= dbScale }
} else if (this.deviceType === 'Apple TV') {
this.deviceSize = { height: dbHeight, width: dbWidth }
}
return this.deviceSize
}
})
.catch((err) => {
// #883: Track API errors if any
log.error(err)
return setSizeInDB()
})
},
setVersion: function(currentSession) {
log.info('Setting device version: ' + currentSession.value.capabilities.sdkVersion)
Expand Down Expand Up @@ -607,7 +557,6 @@ module.exports = syrup.serial()
log.info('Current device orientation: ' + this.orientation)
})
},

rotation: function(params) {
this.orientation = params.orientation

Expand Down Expand Up @@ -738,7 +687,7 @@ module.exports = syrup.serial()
.catch(err => {
let errMes = ''

if (err.error.value.message) {
if (err?.error?.value?.message) {
errMes = err.error.value.message
}

Expand Down Expand Up @@ -785,13 +734,6 @@ module.exports = syrup.serial()
))
])

//TODO: handle negative case when device type not detected. Seems lifecycle.fatal(err) is ok.
WdaClient.getDeviceType()

//TODO: start session, init size/scale and stop session asap. Improve size getting info from db only
//WdaClient.startSession()
//WdaClient.setSizeInDB()
//WdaClient.stopSession()
}

async function wdaMjpegCloseEventHandler(hadError) {
Expand All @@ -812,4 +754,4 @@ module.exports = syrup.serial()
connectToWdaMjpeg(options)

return WdaClient
})
})
Loading

0 comments on commit adbf194

Please sign in to comment.