Skip to content

Commit

Permalink
完善代码
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed Apr 25, 2020
1 parent 3c5ca5f commit b77fc2d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
3 changes: 2 additions & 1 deletion lib/CommonFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ module.exports = function (__runtime__, scope) {
this.minimize = function () {
_logUtils.debugInfo(['直接返回最小化'])
try {
while (automator.clickBack() || automator.clickClose()) {
let maxRepeat = 10
while (maxRepeat--> 0 && (automator.clickBack() || automator.clickClose())) {
sleep(500)
}
} catch (e) {
Expand Down
74 changes: 35 additions & 39 deletions lib/WidgetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
* @Author: TonyJiangWJ
* @Date: 2019-11-05 09:12:00
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-04-25 16:24:29
* @Last Modified time: 2020-04-25 19:22:30
* @Description:
*/

module.exports = function (__runtime__, scope) {
if (typeof scope.WidgetUtils === 'undefined') {

let { config: _config } = require('../config.js')(__runtime__, scope)
let pwd = files.cwd()
let logLibExists = files.exists(pwd + '/lib/LogUtils.js')
let {
debugInfo, debugForDev, logInfo, infoLog, warnInfo, errorInfo, clearLogFile, appendLog, removeOldLogFiles
debugInfo, debugForDev, logInfo, infoLog, warnInfo, errorInfo
} = logLibExists ? require(pwd + '/lib/LogUtils.js')(__runtime__, scope) : {
logInfo: (str) => {
console.log(str)
Expand All @@ -37,7 +38,7 @@ module.exports = function (__runtime__, scope) {
* 校验控件是否存在,并打印相应日志
* @param {String} contentVal 控件文本
* @param {String} position 日志内容 当前所在位置是否成功进入
* @param {Number} timeoutSetting 超时时间 默认为_config.timeout_existing
* @param {Number} timeoutSetting 超时时间 单位毫秒 默认为_config.timeout_existing
*/
const widgetWaiting = function (contentVal, position, timeoutSetting) {
let waitingSuccess = widgetCheck(contentVal, timeoutSetting)
Expand All @@ -54,19 +55,22 @@ module.exports = function (__runtime__, scope) {
/**
* 校验控件是否存在
* @param {String} contentVal 控件文本
* @param {Number} timeoutSetting 超时时间 不设置则为_config.timeout_existing
* @param {Number} timeoutSetting 超时时间 单位毫秒 不设置则为_config.timeout_existing
* @param {Boolean} containType 返回结果附带文本是desc还是text
* 超时返回false
*/
const widgetCheck = function (contentVal, timeoutSetting) {
const widgetCheck = function (contentVal, timeoutSetting, containType) {
let timeout = timeoutSetting || _config.timeout_existing
let timeoutFlag = true
let countDown = new java.util.concurrent.CountDownLatch(1)
let matchRegex = new RegExp(contentVal)
let isDesc = false
let descThread = threads.start(function () {
descMatches(matchRegex).waitFor()
let res = descMatches(matchRegex).findOne().desc()
debugInfo('find desc ' + contentVal + " " + res)
timeoutFlag = false
isDesc = true
countDown.countDown()
})

Expand All @@ -86,6 +90,12 @@ module.exports = function (__runtime__, scope) {
descThread.interrupt()
textThread.interrupt()
timeoutThread.interrupt()
if (containType) {
return {
timeout: timeoutFlag,
isDesc: isDesc
}
}
return !timeoutFlag
}

Expand Down Expand Up @@ -163,20 +173,19 @@ module.exports = function (__runtime__, scope) {
const widgetGetOne = function (contentVal, timeout, containType, suspendWarning) {
let target = null
let isDesc = false
let waitTime = timeout || _config.timeout_findOne
let waitTime = timeout || _config.timeout_existing
let timeoutFlag = true
let matchRegex = new RegExp(contentVal)
if (textMatches(matchRegex).exists()) {
debugInfo('text ' + contentVal + ' found')
target = textMatches(matchRegex).findOne(waitTime)
debugInfo(['try to find one: {} timeout: {}ms', contentVal.toString(), waitTime])
let checkResult = widgetCheck(contentVal, waitTime, true)
if (!checkResult.timeout) {
timeoutFlag = false
} else if (descMatches(matchRegex).exists()) {
isDesc = true
debugInfo('desc ' + contentVal + ' found')
target = descMatches(matchRegex).findOne(waitTime)
timeoutFlag = false
} else {
debugInfo('none of text or desc found for ' + contentVal)
let matchRegex = new RegExp(contentVal)
if (!checkResult.isDesc) {
target = textMatches(matchRegex).findOne(_config.timeout_findOne)
} else {
isDesc = true
target = descMatches(matchRegex).findOne(_config.timeout_findOne)
}
}
// 当需要带回类型时返回对象 传递target以及是否是desc
if (target && containType) {
Expand Down Expand Up @@ -207,32 +216,19 @@ module.exports = function (__runtime__, scope) {
let target = null
let isDesc = false
let timeoutFlag = true
let countDown = new java.util.concurrent.CountDownLatch(1)
let waitTime = timeout || _config.timeout_findOne
let matchRegex = new RegExp(contentVal)
let findThread = threads.start(function () {
if (textMatches(matchRegex).exists()) {
debugInfo('text ' + contentVal + ' found')
let waitTime = timeout || _config.timeout_existing
debugInfo(['try to find all: {} timeout: {}ms', contentVal.toString(), waitTime])
let checkResult = widgetCheck(contentVal, waitTime, true)
if (!checkResult.timeout) {
timeoutFlag = false
let matchRegex = new RegExp(contentVal)
if (!checkResult.isDesc) {
target = textMatches(matchRegex).untilFind()
timeoutFlag = false
} else if (descMatches(matchRegex).exists()) {
} else {
isDesc = true
debugInfo('desc ' + contentVal + ' found')
target = descMatches(matchRegex).untilFind()
timeoutFlag = false
} else {
debugInfo('none of text or desc found for ' + contentVal)
}
countDown.countDown()
})
let timeoutThread = threads.start(function () {
sleep(waitTime)
countDown.countDown()
warnInfo('timeout for finding ' + contentVal)
})
countDown.await()
findThread.interrupt()
timeoutThread.interrupt()
}
if (timeoutFlag && !target) {
return null
} else if (target && containType) {
Expand Down
8 changes: 5 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
let { config } = require('./config.js')(runtime, this)
let runningQueueDispatcher = require('./lib/RunningQueueDispatcher.js')(runtime, this)
let LogUtils = require('./lib/LogUtils.js')(runtime, this)
let { logInfo, errorInfo } = require('./lib/LogUtils.js')(runtime, this)
let FloatyInstance = require('./lib/FloatyUtil.js')(runtime, this)
let commonFunctions = require('./lib/CommonFunction.js')(runtime, this)
let unlocker = require('./lib/Unlock.js')
Expand Down Expand Up @@ -89,8 +89,10 @@ if (config.develop_mode) {
commonFunctions.setUpAutoStart(1)
errorInfo('执行异常, 1分钟后重新开始' + e)
} finally {
if (config.auto_lock === true && unlocker.needRelock() === true) {
debugInfo('重新锁定屏幕')
automator.lockScreen()
}
runningQueueDispatcher.removeRunningTask(true)
// 30秒后关闭,防止立即停止
setTimeout(() => { exit() }, 1000 * 30)
}
}

0 comments on commit b77fc2d

Please sign in to comment.