update 优化返回首页和回复速度

This commit is contained in:
gallonyin
2023-01-04 17:04:52 +08:00
parent 89dba7bdb2
commit 363ba0a60f
5 changed files with 97 additions and 73 deletions

View File

@@ -26,6 +26,7 @@ object Constant {
set(value) { set(value) {
SPUtils.getInstance().put("robotId", value) SPUtils.getInstance().put("robotId", value)
} }
//replyStrategy=replyAll+1 replyStrategy=0不回复 replyStrategy=1回复at replyStrategy=2回复所有
var replyStrategy: Int var replyStrategy: Int
get() = SPUtils.getInstance().getInt("replyStrategy", 1) get() = SPUtils.getInstance().getInt("replyStrategy", 1)
set(value) { set(value) {

View File

@@ -30,9 +30,8 @@ fun goHome() {
*/ */
fun goHomeTab(title: String): Boolean { fun goHomeTab(title: String): Boolean {
var atHome = false var atHome = false
var find = false
while (!atHome) { while (!atHome) {
val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true) val list = AccessibilityUtil.findAllOnceByText(getRoot(), title, exact = true)
for (item in list) { for (item in list) {
val childCount = item.parent?.parent?.parent?.childCount val childCount = item.parent?.parent?.parent?.childCount
if (childCount == 4 || childCount == 5) { if (childCount == 4 || childCount == 5) {
@@ -41,19 +40,14 @@ fun goHomeTab(title: String): Boolean {
val rect = Rect() val rect = Rect()
item.getBoundsInScreen(rect) item.getBoundsInScreen(rect)
if (rect.left > ScreenUtils.getScreenWidth() / 2) { if (rect.left > ScreenUtils.getScreenWidth() / 2) {
goHomeTab("工作台") return goHomeTab("工作台") && goHomeTab("消息")
} }
} }
if (!item.isSelected) {
AccessibilityUtil.performClick(item)
sleep(300)
}
atHome = true atHome = true
val tempList = AccessibilityUtil.findAllOnceByText(getRoot(), title, exact = true)
for (tempItem in tempList) {
val tempChildCount = tempItem.parent?.parent?.parent?.childCount
if (tempChildCount == 4 || tempChildCount == 5) {
AccessibilityUtil.performClick(tempItem)
sleep(300)
find = true
}
}
} }
} }
if (!atHome) { if (!atHome) {
@@ -68,7 +62,7 @@ fun goHomeTab(title: String): Boolean {
} }
} }
LogUtils.v("进入首页-${title}") LogUtils.v("进入首页-${title}")
return find return atHome
} }
/** /**
@@ -76,10 +70,15 @@ fun goHomeTab(title: String): Boolean {
*/ */
fun isAtHome(): Boolean { fun isAtHome(): Boolean {
val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true) val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true)
return list.count { val item = list.firstOrNull {
val childCount = it.parent?.parent?.parent?.childCount val childCount = it.parent?.parent?.parent?.childCount
childCount == 4 || childCount == 5 (childCount == 4 || childCount == 5)
} > 0 } ?: return false
if (!item.isSelected) {
AccessibilityUtil.performClick(item)
sleep(300)
}
return true
} }
/** /**

View File

@@ -26,18 +26,19 @@ object WeworkLoopImpl {
mainLoopRunning = true mainLoopRunning = true
try { try {
while (mainLoopRunning) { while (mainLoopRunning) {
if (WeworkRoomUtil.getRoomType(false) != WeworkMessageBean.ROOM_TYPE_UNKNOWN if (!isAtHome() && WeworkRoomUtil.getRoomType(false) != WeworkMessageBean.ROOM_TYPE_UNKNOWN) {
&& getChatMessageList()) { LogUtils.d("当前在房间: ")
getChatMessageList()
if (mainLoopRunning) {
goHome()
}
continue
} }
if (!mainLoopRunning) break if (!mainLoopRunning) break
goHomeTab("消息") getChatroomList()
if (!mainLoopRunning) break if (!mainLoopRunning) break
if (getChatroomList()) { getFriendRequest()
} sleep(300)
if (!mainLoopRunning) break
if (getFriendRequest()) {
}
sleep(500)
} }
} catch (e: Exception) { } catch (e: Exception) {
mainLoopRunning = false mainLoopRunning = false
@@ -104,9 +105,8 @@ object WeworkLoopImpl {
* @param needInfer 是否需要推断@me并等待回复 * @param needInfer 是否需要推断@me并等待回复
* @param timeout 在房间内等待回复的时长 * @param timeout 在房间内等待回复的时长
*/ */
fun getChatMessageList(needInfer: Boolean = true, timeout: Long = 3000): Boolean { fun getChatMessageList(needInfer: Boolean = true, timeout: Long = 5000): Boolean {
if (Constant.autoReply == 0) return true if (Constant.autoReply == 0) return true
AccessibilityUtil.performScrollDown(getRoot(), 0)
val roomType = WeworkRoomUtil.getRoomType() val roomType = WeworkRoomUtil.getRoomType()
var titleList = WeworkRoomUtil.getRoomTitle() var titleList = WeworkRoomUtil.getRoomTitle()
if (titleList.contains("对方正在输入…")) { if (titleList.contains("对方正在输入…")) {
@@ -139,28 +139,40 @@ object WeworkLoopImpl {
) )
//推测是否回复并在房间等待指令 //推测是否回复并在房间等待指令
if (needInfer) { if (needInfer) {
val lastMessage = messageList.lastOrNull { it.sender == 0 } val lastMessage = messageList.lastOrNull()
if (lastMessage != null) { if (lastMessage != null && lastMessage.sender == 0) {
var tempContent = "" when (Constant.replyStrategy) {
for (itemMessage in lastMessage.itemMessageList) { 1 -> {
if (itemMessage.text.contains("@" + Constant.myName)) { var tempContent = ""
tempContent = itemMessage.text for (itemMessage in lastMessage.itemMessageList) {
if (itemMessage.text.contains("@" + Constant.myName)) {
tempContent = itemMessage.text
}
}
if (roomType == WeworkMessageBean.ROOM_TYPE_EXTERNAL_CONTACT
|| roomType == WeworkMessageBean.ROOM_TYPE_INTERNAL_CONTACT
|| tempContent.isNotBlank()
) {
LogUtils.v("推测需要回复: $tempContent")
val startTime = System.currentTimeMillis()
var currentTime = startTime
while (mainLoopRunning && currentTime - startTime < timeout) {
sleep(Constant.POP_WINDOW_INTERVAL / 5)
currentTime = System.currentTimeMillis()
}
return getChatMessageList(needInfer = false)
}
} }
} 2 -> {
if (roomType == WeworkMessageBean.ROOM_TYPE_EXTERNAL_CONTACT val startTime = System.currentTimeMillis()
|| roomType == WeworkMessageBean.ROOM_TYPE_INTERNAL_CONTACT var currentTime = startTime
|| tempContent.isNotBlank() while (mainLoopRunning && currentTime - startTime < timeout) {
) { sleep(Constant.POP_WINDOW_INTERVAL / 5)
LogUtils.v("推测需要回复: $tempContent") currentTime = System.currentTimeMillis()
val startTime = System.currentTimeMillis() }
var currentTime = startTime
while (mainLoopRunning && currentTime - startTime < timeout) {
sleep(Constant.POP_WINDOW_INTERVAL / 5)
currentTime = System.currentTimeMillis()
}
if (mainLoopRunning) {
return getChatMessageList(needInfer = false) return getChatMessageList(needInfer = false)
} }
else -> return true
} }
} }
} }
@@ -225,20 +237,53 @@ object WeworkLoopImpl {
*/ */
private fun getChatroomList(): Boolean { private fun getChatroomList(): Boolean {
if (Constant.autoReply == 0) return true if (Constant.autoReply == 0) return true
if (!isAtHome()) return true if (!isAtHome()) { goHome() }
if (logIndex++ % 30 == 0) {
LogUtils.d("读取首页聊天列表")
if (logIndex % 120 == 0) log("读取首页聊天列表")
}
var hasNewMessage: AccessibilityNodeInfo? = null
val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true) val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true)
for (item in list) { for (item in list) {
val childCount = item.parent?.parent?.parent?.childCount val childCount = item.parent?.parent?.parent?.childCount
if (childCount == 4 || childCount == 5) { if (childCount == 4 || childCount == 5) {
if (item.parent != null && item.parent.childCount > 1) { if (item.parent != null && item.parent.childCount > 1) {
LogUtils.d("消息有红点") LogUtils.d("消息有红点")
AccessibilityUtil.clickByNode(WeworkController.weworkService, item) hasNewMessage = item
sleep(100)
AccessibilityUtil.clickByNode(WeworkController.weworkService, item)
} }
} }
} }
val listview = AccessibilityUtil.findOneByClazz(getRoot(), Views.RecyclerView, Views.ListView, Views.ViewGroup)
if (listview != null && listview.childCount >= 2) {
if (hasNewMessage != null) {
if (checkUnreadChatRoom(listview)) {
//如果有红点 点击进入聊天页
return true
} else {
AccessibilityUtil.clickByNode(WeworkController.weworkService, hasNewMessage)
sleep(Constant.POP_WINDOW_INTERVAL / 5)
AccessibilityUtil.clickByNode(WeworkController.weworkService, hasNewMessage)
sleep(Constant.POP_WINDOW_INTERVAL / 5)
//双击消息再试一次
if (checkUnreadChatRoom(listview)) {
//如果有红点 点击进入聊天页
return true
}
}
} else {
if (checkNoTipMessage(listview) == 1) {
//如果发现拉入群聊/修改群名/移出群聊 点击进入聊天页
return true
} else {
LogUtils.v("未发现新消息或无提示消息")
}
}
} else {
LogUtils.e("读取聊天列表失败")
error("读取聊天列表失败")
}
if (logIndex % 120 == 0) { if (logIndex % 120 == 0) {
//让企微切换页面使APP保持活跃 //让企微切换页面使APP保持活跃
goHomeTab("通讯录") goHomeTab("通讯录")
@@ -259,26 +304,6 @@ object WeworkLoopImpl {
} }
} }
} }
if (!isAtHome()) return true
if (logIndex++ % 30 == 0) {
LogUtils.i("读取首页聊天列表")
if (logIndex % 120 == 0) log("读取首页聊天列表")
}
val listview = AccessibilityUtil.findOneByClazz(getRoot(), Views.RecyclerView, Views.ListView, Views.ViewGroup)
if (listview != null && listview.childCount >= 2) {
if (checkUnreadChatRoom(listview)) {
//如果有红点 点击进入聊天页
return true
} else if (checkNoTipMessage(listview) == 1) {
//如果发现拉入群聊/修改群名/移出群聊 点击进入聊天页
return true
} else {
LogUtils.v("未发现新消息或无提示消息")
}
} else {
LogUtils.e("读取聊天列表失败")
error("读取聊天列表失败")
}
return false return false
} }

View File

@@ -102,7 +102,6 @@ object WeworkOperationImpl {
LogUtils.v("开始回复") LogUtils.v("开始回复")
if (sendChatMessage(receivedContent, reply = true)) { if (sendChatMessage(receivedContent, reply = true)) {
LogUtils.d("$title: 回复成功") LogUtils.d("$title: 回复成功")
WeworkLoopImpl.getChatMessageList()
uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime)
return true return true
} else { } else {
@@ -116,7 +115,6 @@ object WeworkOperationImpl {
val text = if (originalContent.isNotEmpty()) "$originalContent\n$receivedContent" else receivedContent val text = if (originalContent.isNotEmpty()) "$originalContent\n$receivedContent" else receivedContent
if (sendChatMessage(text, receivedName)) { if (sendChatMessage(text, receivedName)) {
LogUtils.d("$title: 直接发送答案成功") LogUtils.d("$title: 直接发送答案成功")
WeworkLoopImpl.getChatMessageList()
uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime)
return true return true
} else { } else {
@@ -1608,6 +1606,7 @@ object WeworkOperationImpl {
LogUtils.d("发送消息: \n$content") LogUtils.d("发送消息: \n$content")
log("发送消息: \n$content") log("发送消息: \n$content")
AccessibilityUtil.performClick(sendButton) AccessibilityUtil.performClick(sendButton)
WeworkLoopImpl.getChatMessageList()
return true return true
} else { } else {
LogUtils.e("未找到发送按钮") LogUtils.e("未找到发送按钮")

View File

@@ -157,7 +157,7 @@ public class WebSocketManager {
//重连后刷新连接时间 //重连后刷新连接时间
lastConnectedTime = System.currentTimeMillis(); lastConnectedTime = System.currentTimeMillis();
} }
if (System.currentTimeMillis() - lastConnectedTime > heartBeatRate * 2000 && !FloatWindowHelper.INSTANCE.isPause()) { if (System.currentTimeMillis() - lastConnectedTime > heartBeatRate * 3000 && !FloatWindowHelper.INSTANCE.isPause()) {
ToastUtils.show("机器人运行中 请勿人工操作手机~"); ToastUtils.show("机器人运行中 请勿人工操作手机~");
} }
}; };