rubick
Version:
tools for mtc
91 lines (79 loc) • 2.09 kB
JavaScript
/**
* Created by hongzhiyuan on 2017/4/14.
*/
let os = require('os');
let co = require('co');
let logger = require('../../log').init('rubick');
class Device {
/**
* 设备实例工厂方法
*
* @param {String} deviceType
* @param {String} serial
* @param {String} adbStatus
* @return {Device}
*/
static getInstance(deviceType, serial, adbStatus) {
logger.debug(`device getInstance ${deviceType} ${serial}`);
if (deviceType === 'android') {
let AndroidDevice = require('./android');
return new AndroidDevice(serial, adbStatus);
}
logger.fatal('unknow device type');
}
/**
* 构造方法
*
* @param {String} serial
* @param {String} adbStatus
*/
constructor(serial, adbStatus) {
this.serial = serial;
this.host = 'local';
this.base_host = os.hostname();
this.io = null;
this.init();
this._adbStatus = adbStatus;
}
/**
* 设备初始化
*
*/
init() {
}
get adbStatus() {
return this._adbStatus;
}
set adbStatus(newStatus) {
let oldStatus = this.adbStatus;
if (oldStatus !== newStatus) {
this._adbStatus = newStatus;
logger.debug(`device ${this.serial} adb_status change: from ${oldStatus} to ${newStatus}`);
}
}
setDetail(detail) {
for (let key of Object.keys(detail)) {
if (key !== 'status') {
this[key] = detail[key];
}
}
this.status = detail.status;
}
load(detail) {
return this.setDetail(detail);
}
toJSON() {
return {
host: this.host,
base_host: this.base_host,
serial: this.serial,
status: this.status,
adbStatus: this.adbStatus,
};
}
detail() {
return this.toJSON();
}
}
module.exports = Device;