Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: update center device status and list totally #632

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package com.microsoft.hydralab.center.service;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.ddmlib.IDevice;
Expand Down Expand Up @@ -387,36 +386,29 @@ private void updateAgentDeviceGroup(AgentSessionInfo savedSession, List<DeviceIn
* 2. ONLINE/OFFLINE/UNSTABLE can only be set in AGENT and sync to CENTER
*/
updateDeviceGroup(latestDeviceInfos, savedSession.agentUser.getId());

AgentDeviceGroup agentDeviceGroup = agentDeviceGroups.get(savedSession.agentUser.getId());
if (agentDeviceGroup != null) {
// update
updateAgentDevices(latestDeviceInfos, agentDeviceGroup);
} else {
AgentDeviceGroup newAgentDeviceGroup = new AgentDeviceGroup();
newAgentDeviceGroup.initWithAgentUser(savedSession.agentUser);
newAgentDeviceGroup.setDevices(new ArrayList<>(latestDeviceInfos));
agentDeviceGroups.put(savedSession.agentUser.getId(), newAgentDeviceGroup);
log.info("Adding info of new agent: {}, device SN: {}", newAgentDeviceGroup.getAgentName(),
if (agentDeviceGroup == null) {
agentDeviceGroup = new AgentDeviceGroup();
agentDeviceGroup.initWithAgentUser(savedSession.agentUser);
agentDeviceGroups.put(savedSession.agentUser.getId(), agentDeviceGroup);
log.info("Adding info of new agent: {}, device SN: {}", agentDeviceGroup.getAgentName(),
latestDeviceInfos.stream().map(MobileDevice::getSerialNum).collect(Collectors.joining(",")));
}
agentDeviceGroup.setDevices(new ArrayList<>(latestDeviceInfos));
}

public void updateDeviceGroup(List<DeviceInfo> agentDeviceInfos, String agentId) {
for (DeviceInfo agentDeviceInfo : agentDeviceInfos) {
//init agent info
agentDeviceInfo.setAgentId(agentId);

DeviceInfo centerDevice = deviceListMap.get(agentDeviceInfo.getSerialNum());
// if the status saved in Center is testing, the value will not be covered
if (deviceListMap.get(agentDeviceInfo.getSerialNum()) != null) {
if (deviceListMap.get(agentDeviceInfo.getSerialNum()).isTesting()) {
log.warn("Center status: {}, Agent status: {}, status should be synced to CENTER's value when TESTING.",
deviceListMap.get(agentDeviceInfo.getSerialNum()).getStatus(), agentDeviceInfo.getStatus());
agentDeviceInfo.setStatus(DeviceInfo.TESTING);
} else if (agentDeviceInfo.isTesting()) {
log.warn("Test on the device is canceled, status of device in AGENT should be reset to ONLINE, otherwise TESTING would never be covered by agent");
agentDeviceInfo.setStatus(DeviceInfo.ONLINE);
}
if (centerDevice != null && centerDevice.isTesting()) {
log.warn("Center status: {}, Agent status: {}, status should be synced to CENTER's value when TESTING.", centerDevice.getStatus(), agentDeviceInfo.getStatus());
agentDeviceInfo.setStatus(DeviceInfo.TESTING);
} else if (agentDeviceInfo.isTesting()) {
log.warn("Test on the device is canceled, status of device in AGENT should be reset to ONLINE, otherwise TESTING would never be covered by agent");
agentDeviceInfo.setStatus(DeviceInfo.ONLINE);
}

deviceListMap.put(agentDeviceInfo.getSerialNum(), agentDeviceInfo);
Expand Down Expand Up @@ -565,35 +557,6 @@ public void updateDeviceScope(String deviceSerial, Boolean isPrivate) {
sendMessageToSession(agentSession.session, message);
}

private void updateAgentDevices(List<DeviceInfo> agentDeviceInfos, AgentDeviceGroup agentDeviceGroup) {
for (DeviceInfo agentDeviceInfo : agentDeviceInfos) {
boolean hasDevice = false;
for (DeviceInfo centerDeviceInfo : agentDeviceGroup.getDevices()) {
//if the status saved in Center is testing, the value will not be covered
if (deviceListMap.get(centerDeviceInfo.getSerialNum()) != null && deviceListMap.get(centerDeviceInfo.getSerialNum()).isTesting()) {
centerDeviceInfo.setStatus(DeviceInfo.TESTING);
hasDevice = true;
log.info("Updating device status of agent: {}, device SN: {}", agentDeviceGroup.getAgentName(), agentDeviceInfo.getSerialNum());
break;
}
if (centerDeviceInfo.getSerialNum().equals(agentDeviceInfo.getSerialNum())) {
hasDevice = true;
if (DeviceInfo.TESTING.equals(agentDeviceInfo.getStatus())) {
log.warn("Device status is out-of-sync between center/agent, CENTER: {}, AGENT: {}", centerDeviceInfo.getStatus(), agentDeviceInfo.getStatus());
agentDeviceInfo.setStatus(centerDeviceInfo.getStatus());
}
BeanUtil.copyProperties(agentDeviceInfo, centerDeviceInfo);
log.info("Updating device info of agent: {}, device SN: {}", agentDeviceGroup.getAgentName(), agentDeviceInfo.getSerialNum());
break;
}
}
if (!hasDevice) {
log.info("Adding device info of agent: {}, device SN: {}", agentDeviceGroup.getAgentName(), agentDeviceInfo.getSerialNum());
agentDeviceGroup.getDevices().add(agentDeviceInfo);
}
}
}

private void requestList(Session session) {
Message message = new Message();
message.setPath(Const.Path.DEVICE_LIST);
Expand Down