Skip to content

Commit

Permalink
Merge branch 'SonicCloudOrg:main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyi0412 authored May 18, 2023
2 parents 2d4735a + b3751b5 commit 383955e
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sonic-server-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.26</version>
<version>2.0.28</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ knife4j:
- name: sonic-server-controller
url: /api/controller/v3/api-docs?group=default
service-name: sonic-server-controller
context-path: /api/controller
order: 1
- name: sonic-server-folder
url: /api/folder/v3/api-docs?group=default
service-name: sonic-server-folder
context-path: /api/folder
order: 2

spring:
Expand Down
4 changes: 2 additions & 2 deletions sonic-server-controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.3</version>
<version>1.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.14.3</version>
<version>1.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SonicRunner implements ApplicationRunner {
public void run(ApplicationArguments args) throws Exception {
resourceInit();
remoteInit();
idleInit();
}

/**
Expand Down Expand Up @@ -84,4 +85,21 @@ private void remoteInit() {
log.error("init remote conf error", e);
}
}

private void idleInit() {
try {
ConfList conf = confListService.searchByKey(ConfType.IDEL_DEBUG_TIMEOUT);
if (conf != null) {
log.info("idle conf has been init...");
return;
}

confListService.save(ConfType.IDEL_DEBUG_TIMEOUT, "480", null);
log.info("idle conf init finish!");

} catch (Exception e) {
log.error("init idle conf error", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,20 @@ public RespModel setRemoteTimeout(@RequestParam(name = "timeout") int timeout) {
}
return new RespModel<>(RespEnum.HANDLE_OK);
}

@WebAspect
@Operation(summary = "获取闲置超时时间", description = "获取闲置超时时间")
@GetMapping("/getIdleTimeout")
public RespModel getIdleTimeout() {
return new RespModel<>(RespEnum.SEARCH_OK,
Integer.parseInt(confListService.searchByKey(ConfType.IDEL_DEBUG_TIMEOUT).getContent()));
}

@WebAspect
@Operation(summary = "设置闲置超时时间", description = "设置闲置超时时间")
@GetMapping("/setIdleTimeout")
public RespModel setIdleTimeout(@RequestParam(name = "timeout") int timeout) {
confListService.save(ConfType.IDEL_DEBUG_TIMEOUT, timeout + "", null);
return new RespModel<>(RespEnum.HANDLE_OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.cloud.sonic.common.config.WebAspect;
import org.cloud.sonic.common.http.RespEnum;
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.models.base.CommentPage;
import org.cloud.sonic.controller.models.domain.Devices;
import org.cloud.sonic.controller.models.http.DeviceDetailChange;
import org.cloud.sonic.controller.models.http.OccupyParams;
import org.cloud.sonic.controller.models.http.UpdateDeviceImg;
import org.cloud.sonic.controller.services.DevicesService;
import org.cloud.sonic.controller.transport.TransportWorker;
Expand All @@ -46,9 +48,32 @@ public class DevicesController {
@Autowired
private DevicesService devicesService;

@WebAspect
@Operation(summary = "通过REST API占用设备", description = "远程占用设备并开启相关端口")
@PostMapping("/occupy")
public RespModel occupy(@Validated @RequestBody OccupyParams occupyParams, HttpServletRequest request) {
String token = request.getHeader("SonicToken");
if (token == null) {
return new RespModel(RespEnum.UNAUTHORIZED);
}
return devicesService.occupy(occupyParams, token);
}

@WebAspect
@Operation(summary = "通过REST API释放设备", description = "远程释放设备并释放相关端口,只能由占用者释放")
@Parameter(name = "udId", description = "设备序列号")
@GetMapping("/release")
public RespModel release(@RequestParam(name = "udId") String udId, HttpServletRequest request) {
String token = request.getHeader("SonicToken");
if (token == null) {
return new RespModel(RespEnum.UNAUTHORIZED);
}
return devicesService.release(udId, token);
}

@WebAspect
@Operation(summary = "强制解除设备占用", description = "强制解除设备占用")
@Parameter(name = "udId", description = "平台")
@Parameter(name = "udId", description = "设备序列号")
@GetMapping("/stopDebug")
public RespModel<List<Devices>> stopDebug(@RequestParam(name = "udId") String udId) {
Devices devices = devicesService.findByUdId(udId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ public RespModel<String> save(@Validated @RequestBody PackageDTO pkg) {
public RespModel<CommentPage<PackageDTO>> findAll(@RequestParam(name = "projectId") int projectId,
@RequestParam(name = "branch", required = false) String branch,
@RequestParam(name = "platform", required = false) String platform,
@RequestParam(name = "packageName", required = false) String packageName,
@RequestParam(name = "page") int page,
@RequestParam(name = "pageSize") int pageSize) {

Page<Packages> pageable = new Page<>(page, pageSize);

return new RespModel<>(RespEnum.SEARCH_OK, packagesService.findByProjectId(projectId, branch, platform, pageable));
return new RespModel<>(RespEnum.SEARCH_OK, packagesService.findByProjectId(projectId, branch, platform,
packageName, pageable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @des
* @date 2021/10/13 18:45
*/
@Schema(name = "更改密码请求模型")
public class ChangePwd implements Serializable {
@NotNull
@Schema(description = "旧密码", required = true, example = "123456")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cloud.sonic.controller.models.http;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Data;

import java.io.Serializable;

@Schema(name = "占用安卓设备请求模型")
@Data
public class OccupyParams implements Serializable {
@NotNull
@Schema(description = "设备udId", required = true, example = "xxxx")
private String udId;

@Schema(description = "(仅Android设备生效)设备adb远程连接端口。非必填,为0或不填时不暴露也不启动对应服务,建议取值范围30000-65535。", example = "30000")
private int sasRemotePort;

@Schema(description = "(仅Android设备生效)设备uia2 server远程连接端口。非必填,为0或不填时不暴露也不启动对应服务,建议取值范围30000-65535。", example = "30001")
private int uia2RemotePort;

@Schema(description = "(仅iOS设备生效)设备sib远程连接端口。非必填,为0或不填时不暴露也不启动对应服务,建议取值范围30000-65535。", example = "30002")
private int sibRemotePort;

@Schema(description = "(仅iOS设备生效)设备wda server远程连接端口。非必填,为0或不填时不暴露也不启动对应服务,建议取值范围30000-65535。", example = "30003")
private int wdaServerRemotePort;

@Schema(description = "(仅iOS设备生效)设备wda mjpeg远程连接端口。非必填,为0或不填时不暴露也不启动对应服务,建议取值范围30000-65535。", example = "30004")
private int wdaMjpegRemotePort;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface ConfType {
String RESOURCE = "resource";

String REMOTE_DEBUG_TIMEOUT = "remote-debug-timeout";

String IDEL_DEBUG_TIMEOUT = "idle-debug-timeout";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.models.domain.Devices;
import org.cloud.sonic.controller.models.http.DeviceDetailChange;
import org.cloud.sonic.controller.models.http.OccupyParams;
import org.cloud.sonic.controller.models.http.UpdateDeviceImg;

import java.io.IOException;
Expand All @@ -34,6 +35,11 @@
* @date 2021/8/16 22:51
*/
public interface DevicesService extends IService<Devices> {

RespModel occupy(OccupyParams occupyParams, String token);

RespModel release(String udId, String token);

boolean saveDetail(DeviceDetailChange deviceDetailChange);

void updatePosition(int id, int position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public interface PackagesService extends IService<Packages> {
*
* @return
*/
CommentPage<PackageDTO> findByProjectId(int projectId, String branch, String platform, Page<Packages> pageable);
CommentPage<PackageDTO> findByProjectId(int projectId, String branch, String platform, String packageName, Page<Packages> pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.mapper.DevicesMapper;
import org.cloud.sonic.controller.mapper.TestSuitesDevicesMapper;
import org.cloud.sonic.controller.models.domain.Agents;
import org.cloud.sonic.controller.models.domain.Devices;
import org.cloud.sonic.controller.models.domain.TestSuitesDevices;
import org.cloud.sonic.controller.models.domain.Users;
import org.cloud.sonic.controller.models.http.DeviceDetailChange;
import org.cloud.sonic.controller.models.http.OccupyParams;
import org.cloud.sonic.controller.models.http.UpdateDeviceImg;
import org.cloud.sonic.controller.models.interfaces.DeviceStatus;
import org.cloud.sonic.controller.models.interfaces.PlatformType;
import org.cloud.sonic.controller.services.AgentsService;
import org.cloud.sonic.controller.services.DevicesService;
import org.cloud.sonic.controller.services.UsersService;
import org.cloud.sonic.controller.services.impl.base.SonicServiceImpl;
import org.cloud.sonic.controller.transport.TransportWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -70,6 +74,71 @@ public class DevicesServiceImpl extends SonicServiceImpl<DevicesMapper, Devices>
@Autowired
private AgentsService agentsService;

@Override
public RespModel occupy(OccupyParams occupyParams, String token) {
Devices devices = findByUdId(occupyParams.getUdId());
if (devices != null) {
if (devices.getStatus().equals(DeviceStatus.ONLINE)) {
Agents agents = agentsService.findById(devices.getAgentId());
if (agents != null) {
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(occupyParams);
jsonObject.put("msg", "occupy");
jsonObject.put("token", token);
jsonObject.put("platform", devices.getPlatform());
TransportWorker.send(agents.getId(), jsonObject);
JSONObject result = new JSONObject();
switch (devices.getPlatform()) {
case PlatformType.ANDROID -> {
if (occupyParams.getSasRemotePort() != 0) {
result.put("sas", String.format("adb connect %s:%d", agents.getHost(), occupyParams.getSasRemotePort()));
}
if (occupyParams.getUia2RemotePort() != 0) {
result.put("uia2", String.format("http://%s:%d/uia/%d", agents.getHost(), agents.getPort(), occupyParams.getUia2RemotePort()));
}
}
case PlatformType.IOS -> {
if (occupyParams.getSibRemotePort() != 0) {
result.put("sib", String.format("sib remote connect --host %s -p %d", agents.getHost(), occupyParams.getSibRemotePort()));
}
if (occupyParams.getWdaServerRemotePort() != 0) {
result.put("wdaServer", String.format("http://%s:%d", agents.getHost(), occupyParams.getWdaServerRemotePort()));
}
if (occupyParams.getWdaMjpegRemotePort() != 0) {
result.put("wdaMjpeg", String.format("http://%s:%d", agents.getHost(), occupyParams.getWdaMjpegRemotePort()));
}
}
}
return new RespModel<>(RespEnum.HANDLE_OK, result);
} else {
return new RespModel<>(RespEnum.ID_NOT_FOUND);
}
} else {
return new RespModel<>(RespEnum.DEVICE_NOT_FOUND);
}
} else {
return new RespModel<>(RespEnum.DEVICE_NOT_FOUND);
}
}

@Override
public RespModel release(String udId, String token) {
Users users = usersService.getUserInfo(token);
Devices devices = findByUdId(udId);
if (devices != null) {
if (!devices.getUser().equals(users.getUserName())) {
return new RespModel<>(RespEnum.UNAUTHORIZED);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", "release");
jsonObject.put("udId", udId);
jsonObject.put("platform", devices.getPlatform());
TransportWorker.send(devices.getAgentId(), jsonObject);
return new RespModel<>(RespEnum.HANDLE_OK);
} else {
return new RespModel<>(RespEnum.DEVICE_NOT_FOUND);
}
}

@Override
public boolean saveDetail(DeviceDetailChange deviceDetailChange) {
if (existsById(deviceDetailChange.getId())) {
Expand Down Expand Up @@ -166,7 +235,7 @@ public Page<Devices> findAll(List<String> iOSVersion, List<String> androidVersio
chainWrapper.in(Devices::getStatus, status);
}

if (!StringUtils.isEmpty(deviceInfo)) {
if (StringUtils.hasText(deviceInfo)) {
chainWrapper.like(Devices::getUdId, deviceInfo)
.or().like(Devices::getModel, deviceInfo)
.or().like(Devices::getChiName, deviceInfo)
Expand All @@ -184,8 +253,7 @@ public Page<Devices> findAll(List<String> iOSVersion, List<String> androidVersio
" status asc,\n" +
" id desc");

Page<Devices> devicesPage = chainWrapper.page(pageable);
return devicesPage;
return chainWrapper.page(pageable);
}

@Override
Expand Down Expand Up @@ -347,7 +415,7 @@ public void refreshDevicesBattery(JSONObject jsonObject) {

@Override
public Integer findTemper() {
OptionalDouble tempers = new LambdaQueryChainWrapper<Devices>(devicesMapper).ne(Devices::getTemperature, 0)
OptionalDouble tempers = new LambdaQueryChainWrapper<>(devicesMapper).ne(Devices::getTemperature, 0)
.in(Devices::getStatus, Arrays.asList(DeviceStatus.ONLINE, DeviceStatus.DEBUGGING, DeviceStatus.TESTING))
.list().stream().mapToInt(Devices::getTemperature).average();
return tempers.isPresent() ? (int) tempers.getAsDouble() : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ public String findOne(int projectId, String branch, String platform) {
}

@Override
public CommentPage<PackageDTO> findByProjectId(int projectId, String branch, String platform, Page<Packages> pageable) {
public CommentPage<PackageDTO> findByProjectId(int projectId, String branch, String platform, String packageName,
Page<Packages> pageable) {
Page<Packages> page = lambdaQuery().eq(Packages::getProjectId, projectId)
.eq(StringUtils.isNotBlank(platform), Packages::getPlatform, platform)
.like(StringUtils.isNotBlank(branch), Packages::getBranch, branch)
.like(StringUtils.isNotBlank(packageName), Packages::getPkgName, packageName)
.orderByDesc(Packages::getId)
.page(pageable);

List<PackageDTO> packageDTOList = page.getRecords()
.stream().map(TypeConverter::convertTo).collect(Collectors.toList());

return CommentPage.convertFrom(page, packageDTOList);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ public List<Steps> listStepsByElementsId(int elementsId) {

@Override
public boolean deleteByProjectId(int projectId) {
// 先删除steps_elements表中,属于该projectId下的"步骤-元素"关联记录
List<Steps> allSteps = stepsMapper.selectList(new LambdaQueryWrapper<Steps>().eq(Steps::getProjectId, projectId));
for (Steps curStep : allSteps) {
stepsElementsMapper.delete(new LambdaQueryWrapper<StepsElements>().eq(StepsElements::getStepsId, curStep.getId()));
}
// 再删除指定projectId的Step
return baseMapper.delete(new LambdaQueryWrapper<Steps>().eq(Steps::getProjectId, projectId)) > 0;
}

Expand Down
Loading

0 comments on commit 383955e

Please sign in to comment.