mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:03:30 +02:00
Integrate new minicap along with a moderate rewrite. What's currently missing is recovering from socket death.
This commit is contained in:
@@ -1,25 +1,71 @@
|
||||
var util = require('util')
|
||||
|
||||
var syrup = require('stf-syrup')
|
||||
var EventEmitter = require('eventemitter3').EventEmitter
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var streamutil = require('../../../util/streamutil')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.dependency(require('../support/adb'))
|
||||
.dependency(require('../resources/minicap'))
|
||||
.dependency(require('./service'))
|
||||
.dependency(require('./screen'))
|
||||
.define(function(options, service, screen) {
|
||||
.dependency(require('./screen/options'))
|
||||
.define(function(options, adb, minicap, service, screenOptions) {
|
||||
var log = logger.createLogger('device:plugins:display')
|
||||
|
||||
function fetch() {
|
||||
log.info('Fetching display info')
|
||||
return service.getDisplay(0)
|
||||
.catch(function() {
|
||||
log.info('Falling back to screen API')
|
||||
return screen.info(0)
|
||||
})
|
||||
.then(function(display) {
|
||||
display.url = screen.publicUrl
|
||||
return display
|
||||
function Display(id, properties) {
|
||||
this.id = id
|
||||
this.properties = properties
|
||||
}
|
||||
|
||||
util.inherits(Display, EventEmitter)
|
||||
|
||||
Display.prototype.updateRotation = function(newRotation) {
|
||||
log.info('Rotation changed to %d', newRotation)
|
||||
this.properties.rotation = newRotation
|
||||
this.emit('rotationChange', newRotation)
|
||||
}
|
||||
|
||||
function infoFromMinicap(id) {
|
||||
return minicap.run(util.format('-d %d -i', id))
|
||||
.then(streamutil.readAll)
|
||||
.then(function(out) {
|
||||
var match
|
||||
if ((match = /^ERROR: (.*)$/.exec(out))) {
|
||||
throw new Error(match[1])
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(out)
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error(out.toString())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return fetch()
|
||||
function infoFromService(id) {
|
||||
return service.getDisplay(id)
|
||||
}
|
||||
|
||||
function readInfo(id) {
|
||||
log.info('Reading display info')
|
||||
return infoFromService(id)
|
||||
.catch(function() {
|
||||
return infoFromMinicap(id)
|
||||
})
|
||||
.then(function(properties) {
|
||||
properties.url = screenOptions.publicUrl
|
||||
return new Display(id, properties)
|
||||
})
|
||||
}
|
||||
|
||||
return readInfo(0).then(function(display) {
|
||||
service.on('rotationChange', function(data) {
|
||||
display.updateRotation(data.rotation)
|
||||
})
|
||||
|
||||
return display
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user