优化设备数量不对的问题

This commit is contained in:
2025-09-10 16:15:47 +08:00
parent e2e9e0b348
commit 9cb27eb97f
9 changed files with 78 additions and 79 deletions

View File

@@ -26,6 +26,8 @@ app.config['JSON_AS_ASCII'] = False # Flask jsonify 不转义中文/emoji
app.config['JSONIFY_MIMETYPE'] = "application/json; charset=utf-8"
listData = []
listLock = threading.Lock()
dataQueue = Queue()
def start_socket_listener():
@@ -83,39 +85,29 @@ def start_socket_listener():
listener_thread = threading.Thread(target=start_socket_listener, daemon=True)
listener_thread.start()
# 传递token暂时用不到了
# @app.route('/passToken', methods=['POST'])
# def passToken():
# try:
# data = request.get_json()
# token = data['token']
# Requester.requestPrologue(token)
# return ResultData(data="").toJson()
# except Exception as e:
# print(e)
# return ResultData(data="").toJson()
# 获取设备列表
@app.route('/deviceList', methods=['GET'])
def deviceList():
try:
while not dataQueue.empty():
obj = dataQueue.get()
type = obj["type"]
if type == 1:
listData.append(obj)
else:
for data in listData:
if data.get("deviceId") == obj.get("deviceId") and data.get("screenPort") == obj.get("screenPort"):
listData.remove(data)
return ResultData(data=listData).toJson()
with listLock: # 1. 加锁
# 先一次性把队列全部消费完
while not dataQueue.empty():
obj = dataQueue.get()
if obj["type"] == 1:
listData.append(obj)
else:
# 倒序删除,安全
for i in range(len(listData) - 1, -1, -1):
d = listData[i]
if d.get("deviceId") == obj.get("deviceId") and \
d.get("screenPort") == obj.get("screenPort"):
listData.pop(i)
break # 同一端口同一设备只删一次
return ResultData(data=listData.copy()).toJson() # 2. 返回副本
except Exception as e:
print(e)
LogManager.error("获取设备列表失败:", e)
return ResultData(data=[]).toJson()
# 获取设备应用列表
@app.route('/deviceAppList', methods=['POST'])
def deviceAppList():
@@ -124,7 +116,6 @@ def deviceAppList():
apps = ControlUtils.getDeviceAppList(udid)
return ResultData(data=apps).toJson()
# 打开指定app
@app.route('/launchApp', methods=['POST'])
def launchApp():