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) {
SPUtils.getInstance().put("robotId", value)
}
//replyStrategy=replyAll+1 replyStrategy=0不回复 replyStrategy=1回复at replyStrategy=2回复所有
var replyStrategy: Int
get() = SPUtils.getInstance().getInt("replyStrategy", 1)
set(value) {

View File

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

View File

@@ -26,18 +26,19 @@ object WeworkLoopImpl {
mainLoopRunning = true
try {
while (mainLoopRunning) {
if (WeworkRoomUtil.getRoomType(false) != WeworkMessageBean.ROOM_TYPE_UNKNOWN
&& getChatMessageList()) {
if (!isAtHome() && WeworkRoomUtil.getRoomType(false) != WeworkMessageBean.ROOM_TYPE_UNKNOWN) {
LogUtils.d("当前在房间: ")
getChatMessageList()
if (mainLoopRunning) {
goHome()
}
continue
}
if (!mainLoopRunning) break
goHomeTab("消息")
getChatroomList()
if (!mainLoopRunning) break
if (getChatroomList()) {
}
if (!mainLoopRunning) break
if (getFriendRequest()) {
}
sleep(500)
getFriendRequest()
sleep(300)
}
} catch (e: Exception) {
mainLoopRunning = false
@@ -104,9 +105,8 @@ object WeworkLoopImpl {
* @param needInfer 是否需要推断@me并等待回复
* @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
AccessibilityUtil.performScrollDown(getRoot(), 0)
val roomType = WeworkRoomUtil.getRoomType()
var titleList = WeworkRoomUtil.getRoomTitle()
if (titleList.contains("对方正在输入…")) {
@@ -139,8 +139,10 @@ object WeworkLoopImpl {
)
//推测是否回复并在房间等待指令
if (needInfer) {
val lastMessage = messageList.lastOrNull { it.sender == 0 }
if (lastMessage != null) {
val lastMessage = messageList.lastOrNull()
if (lastMessage != null && lastMessage.sender == 0) {
when (Constant.replyStrategy) {
1 -> {
var tempContent = ""
for (itemMessage in lastMessage.itemMessageList) {
if (itemMessage.text.contains("@" + Constant.myName)) {
@@ -158,10 +160,20 @@ object WeworkLoopImpl {
sleep(Constant.POP_WINDOW_INTERVAL / 5)
currentTime = System.currentTimeMillis()
}
if (mainLoopRunning) {
return getChatMessageList(needInfer = false)
}
}
2 -> {
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)
}
else -> return true
}
}
}
return true
@@ -225,20 +237,53 @@ object WeworkLoopImpl {
*/
private fun getChatroomList(): Boolean {
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)
for (item in list) {
val childCount = item.parent?.parent?.parent?.childCount
if (childCount == 4 || childCount == 5) {
if (item.parent != null && item.parent.childCount > 1) {
LogUtils.d("消息有红点")
AccessibilityUtil.clickByNode(WeworkController.weworkService, item)
sleep(100)
AccessibilityUtil.clickByNode(WeworkController.weworkService, item)
hasNewMessage = 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) {
//让企微切换页面使APP保持活跃
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
}

View File

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

View File

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