Skip to content

Commit

Permalink
Bugfix: update center device status and list totally (#632)
Browse files Browse the repository at this point in the history
<!-- 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...*
  • Loading branch information
zhou9584 authored Jan 3, 2024
1 parent ee8ef12 commit 572bab3
Showing 1 changed file with 13 additions and 50 deletions.
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

0 comments on commit 572bab3

Please sign in to comment.