mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:13:31 +02:00
Move utility plugins into their own folder. They don't go into the support folder because they're not standalone units and require interaction with the main units. Further refactoring pending.
This commit is contained in:
71
lib/units/device/plugins/util/display.js
Normal file
71
lib/units/device/plugins/util/display.js
Normal file
@@ -0,0 +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/options'))
|
||||
.define(function(options, adb, minicap, service, screenOptions) {
|
||||
var log = logger.createLogger('device:plugins: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())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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