Skip to content

Commit 572bab3

Browse files
authored
Bugfix: update center device status and list totally (#632)
<!-- Please provide brief information about the PR, what it contains & its purpose, new behaviors after the change. And let us know here if you need any help: https://github.com/microsoft/HydraLab/issues/new --> ## Description <!-- A few words to explain your changes --> ### Linked GitHub issue ID: # ## Pull Request Checklist <!-- Put an x in the boxes that apply. This is simply a reminder of what we are going to look for before merging your code. --> - [ ] Tests for the changes have been added (for bug fixes / features) - [ ] Code compiles correctly with all tests are passed. - [ ] I've read the [contributing guide](https://github.com/microsoft/HydraLab/blob/main/CONTRIBUTING.md#making-changes-to-the-code) and followed the recommended practices. - [ ] [Wikis](https://github.com/microsoft/HydraLab/wiki) or [README](https://github.com/microsoft/HydraLab/blob/main/README.md) have been reviewed and added / updated if needed (for bug fixes / features) ### Does this introduce a breaking change? *If this introduces a breaking change for Hydra Lab users, please describe the impact and migration path.* - [ ] Yes - [ ] No ## How you tested it *Please make sure the change is tested, you can test it by adding UTs, do local test and share the screenshots, etc.* Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Technical design - [ ] Build related changes - [ ] Refactoring (no functional changes, no api changes) - [ ] Code style update (formatting, renaming) or Documentation content changes - [ ] Other (please describe): ### Feature UI screenshots or Technical design diagrams *If this is a relatively large or complex change, kick it off by drawing the tech design with PlantUML and explaining why you chose the solution you did and what alternatives you considered, etc...*
1 parent ee8ef12 commit 572bab3

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

center/src/main/java/com/microsoft/hydralab/center/service/DeviceAgentManagementService.java

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package com.microsoft.hydralab.center.service;
55

6-
import cn.hutool.core.bean.BeanUtil;
76
import com.alibaba.fastjson.JSONArray;
87
import com.alibaba.fastjson.JSONObject;
98
import com.android.ddmlib.IDevice;
@@ -387,36 +386,29 @@ private void updateAgentDeviceGroup(AgentSessionInfo savedSession, List<DeviceIn
387386
* 2. ONLINE/OFFLINE/UNSTABLE can only be set in AGENT and sync to CENTER
388387
*/
389388
updateDeviceGroup(latestDeviceInfos, savedSession.agentUser.getId());
390-
391389
AgentDeviceGroup agentDeviceGroup = agentDeviceGroups.get(savedSession.agentUser.getId());
392-
if (agentDeviceGroup != null) {
393-
// update
394-
updateAgentDevices(latestDeviceInfos, agentDeviceGroup);
395-
} else {
396-
AgentDeviceGroup newAgentDeviceGroup = new AgentDeviceGroup();
397-
newAgentDeviceGroup.initWithAgentUser(savedSession.agentUser);
398-
newAgentDeviceGroup.setDevices(new ArrayList<>(latestDeviceInfos));
399-
agentDeviceGroups.put(savedSession.agentUser.getId(), newAgentDeviceGroup);
400-
log.info("Adding info of new agent: {}, device SN: {}", newAgentDeviceGroup.getAgentName(),
390+
if (agentDeviceGroup == null) {
391+
agentDeviceGroup = new AgentDeviceGroup();
392+
agentDeviceGroup.initWithAgentUser(savedSession.agentUser);
393+
agentDeviceGroups.put(savedSession.agentUser.getId(), agentDeviceGroup);
394+
log.info("Adding info of new agent: {}, device SN: {}", agentDeviceGroup.getAgentName(),
401395
latestDeviceInfos.stream().map(MobileDevice::getSerialNum).collect(Collectors.joining(",")));
402396
}
397+
agentDeviceGroup.setDevices(new ArrayList<>(latestDeviceInfos));
403398
}
404399

405400
public void updateDeviceGroup(List<DeviceInfo> agentDeviceInfos, String agentId) {
406401
for (DeviceInfo agentDeviceInfo : agentDeviceInfos) {
407402
//init agent info
408403
agentDeviceInfo.setAgentId(agentId);
409-
404+
DeviceInfo centerDevice = deviceListMap.get(agentDeviceInfo.getSerialNum());
410405
// if the status saved in Center is testing, the value will not be covered
411-
if (deviceListMap.get(agentDeviceInfo.getSerialNum()) != null) {
412-
if (deviceListMap.get(agentDeviceInfo.getSerialNum()).isTesting()) {
413-
log.warn("Center status: {}, Agent status: {}, status should be synced to CENTER's value when TESTING.",
414-
deviceListMap.get(agentDeviceInfo.getSerialNum()).getStatus(), agentDeviceInfo.getStatus());
415-
agentDeviceInfo.setStatus(DeviceInfo.TESTING);
416-
} else if (agentDeviceInfo.isTesting()) {
417-
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");
418-
agentDeviceInfo.setStatus(DeviceInfo.ONLINE);
419-
}
406+
if (centerDevice != null && centerDevice.isTesting()) {
407+
log.warn("Center status: {}, Agent status: {}, status should be synced to CENTER's value when TESTING.", centerDevice.getStatus(), agentDeviceInfo.getStatus());
408+
agentDeviceInfo.setStatus(DeviceInfo.TESTING);
409+
} else if (agentDeviceInfo.isTesting()) {
410+
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");
411+
agentDeviceInfo.setStatus(DeviceInfo.ONLINE);
420412
}
421413

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

568-
private void updateAgentDevices(List<DeviceInfo> agentDeviceInfos, AgentDeviceGroup agentDeviceGroup) {
569-
for (DeviceInfo agentDeviceInfo : agentDeviceInfos) {
570-
boolean hasDevice = false;
571-
for (DeviceInfo centerDeviceInfo : agentDeviceGroup.getDevices()) {
572-
//if the status saved in Center is testing, the value will not be covered
573-
if (deviceListMap.get(centerDeviceInfo.getSerialNum()) != null && deviceListMap.get(centerDeviceInfo.getSerialNum()).isTesting()) {
574-
centerDeviceInfo.setStatus(DeviceInfo.TESTING);
575-
hasDevice = true;
576-
log.info("Updating device status of agent: {}, device SN: {}", agentDeviceGroup.getAgentName(), agentDeviceInfo.getSerialNum());
577-
break;
578-
}
579-
if (centerDeviceInfo.getSerialNum().equals(agentDeviceInfo.getSerialNum())) {
580-
hasDevice = true;
581-
if (DeviceInfo.TESTING.equals(agentDeviceInfo.getStatus())) {
582-
log.warn("Device status is out-of-sync between center/agent, CENTER: {}, AGENT: {}", centerDeviceInfo.getStatus(), agentDeviceInfo.getStatus());
583-
agentDeviceInfo.setStatus(centerDeviceInfo.getStatus());
584-
}
585-
BeanUtil.copyProperties(agentDeviceInfo, centerDeviceInfo);
586-
log.info("Updating device info of agent: {}, device SN: {}", agentDeviceGroup.getAgentName(), agentDeviceInfo.getSerialNum());
587-
break;
588-
}
589-
}
590-
if (!hasDevice) {
591-
log.info("Adding device info of agent: {}, device SN: {}", agentDeviceGroup.getAgentName(), agentDeviceInfo.getSerialNum());
592-
agentDeviceGroup.getDevices().add(agentDeviceInfo);
593-
}
594-
}
595-
}
596-
597560
private void requestList(Session session) {
598561
Message message = new Message();
599562
message.setPath(Const.Path.DEVICE_LIST);

0 commit comments

Comments
 (0)