From 5a5bef44c26cb82592d6e348e9f28048ac738655 Mon Sep 17 00:00:00 2001 From: "jieying.li" Date: Wed, 27 Nov 2024 03:54:06 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E5=BC=95=E6=93=8E=E7=94=B3?= =?UTF-8?q?=E8=AF=B7=E8=AE=BE=E5=A4=87=E8=B6=85=E6=97=B6=E5=90=8E=E5=9B=A0?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=AE=BE=E5=A4=87id=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=BC=95=E8=B5=B7=E6=AD=A3=E5=9C=A8=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E4=BB=BB=E5=8A=A1=E7=9A=84=E8=AE=BE=E5=A4=87=E5=A4=B1?= =?UTF-8?q?=E5=8E=BB=E4=BD=BF=E7=94=A8=E6=9D=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改方案: 1.请求方申请设备时自带token值,设备管理服务判断设备是否空闲,如果空闲,那么请求方提供的token作为本次申请的token; 2.如果在申请过程中出现问题,请求方依然可以根据token释放设备,设备管理服务接收到改token的释放也会检查token是否有效 设备管理 1.增加接口:批量申请设备使用时,使用请求方提供的token 执行引擎 1.更改设备申请接口 2.修改移动和PC的设备申请逻辑,将两者相同逻辑部分进行抽取,简化代码 3.配置文件增加配置,亦可以不修改,接口地址有默认值。 --- .../cctp/se/config/AtuServerConfig.java | 6 + .../cctp/se/device/dto/ApplyDeviceDto.java | 21 +++ .../se/device/dto/BatchApplyDeviceDto.java | 25 +++ .../cctp/se/plan/AtuPlanTaskSchedule.java | 171 ++++++++---------- .../resources/application-devFront-linux.yml | 1 + .../resources/application-devFront-uos.yml | 1 + .../resources/application-devFront-win.yml | 1 + .../main/resources/application-prod-linux.yml | 1 + .../main/resources/application-prod-uos.yml | 1 + .../main/resources/application-prod-win.yml | 1 + .../main/resources/application-test-linux.yml | 1 + .../main/resources/application-test-uos.yml | 1 + .../main/resources/application-test-win.yml | 1 + .../main/resources/application-uat-linux.yml | 1 + .../main/resources/application-uat-uos.yml | 1 + .../main/resources/application-uat-win.yml | 1 + .../service/DeviceActivityApiService.java | 14 +- .../service/DeviceActivityApiServiceImpl.java | 76 +++++++- .../device/bus/receiver/DeviceMQReceiver.java | 19 +- .../device/constants/DeviceConstants.java | 7 +- .../device/dto/device/ApplyDeviceDto.java | 21 +++ .../dto/device/BatchApplyDeviceDto.java | 25 +++ .../cctp/device/pub/DevicePubCtrl.java | 13 +- 23 files changed, 281 insertions(+), 129 deletions(-) create mode 100644 cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/ApplyDeviceDto.java create mode 100644 cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/BatchApplyDeviceDto.java create mode 100644 cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/ApplyDeviceDto.java create mode 100644 cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/BatchApplyDeviceDto.java diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/config/AtuServerConfig.java b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/config/AtuServerConfig.java index 01070dc..734ef7f 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/config/AtuServerConfig.java +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/config/AtuServerConfig.java @@ -143,4 +143,10 @@ public class AtuServerConfig { private String remoteProtocol; private Boolean IsPcExecutor; + + private String acquireDeviceWithTokenUrl; + + public String getAcquireDeviceWithTokenUrl() { + return StringUtils.isBlank(this.acquireDeviceWithTokenUrl) ? "/api/cctp-device-mgr/pub/deviceList/apply/batchWithToken" : this.acquireDeviceWithTokenUrl; + } } diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/ApplyDeviceDto.java b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/ApplyDeviceDto.java new file mode 100644 index 0000000..559e94b --- /dev/null +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/ApplyDeviceDto.java @@ -0,0 +1,21 @@ +package net.northking.cctp.se.device.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.ToString; + +@Data +@ApiModel(value = "设备申请") +@ToString +public class ApplyDeviceDto { + + @ApiModelProperty(value = "设备Id") + private String id; + + @ApiModelProperty(value = "设备类型", notes = "1-pc,2-移动") + private String type; + + @ApiModelProperty(value = "token", notes = "可由请求方带上") + private String token; +} diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/BatchApplyDeviceDto.java b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/BatchApplyDeviceDto.java new file mode 100644 index 0000000..416823c --- /dev/null +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/device/dto/BatchApplyDeviceDto.java @@ -0,0 +1,25 @@ +package net.northking.cctp.se.device.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +@Data +@ApiModel(value = "设备批量申请dto") +@ToString +public class BatchApplyDeviceDto { + + + @ApiModelProperty(value = "申请人id") + private String id; + + @ApiModelProperty(value = "使用来源,engine为引擎使用0") + private String userBy; + + @ApiModelProperty(value = "设备列表") + private List deviceList; + +} diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/plan/AtuPlanTaskSchedule.java b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/plan/AtuPlanTaskSchedule.java index 4a573ee..a1ddf2a 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/plan/AtuPlanTaskSchedule.java +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/java/net/northking/cctp/se/plan/AtuPlanTaskSchedule.java @@ -8,6 +8,8 @@ import com.rabbitmq.client.GetResponse; import net.northking.cctp.common.http.ResultWrapper; import net.northking.cctp.element.core.exception.ExecuteException; import net.northking.cctp.se.config.AtuServerConfig; +import net.northking.cctp.se.device.dto.ApplyDeviceDto; +import net.northking.cctp.se.device.dto.BatchApplyDeviceDto; import net.northking.cctp.se.dto.ScriptEngineInfo; import net.northking.cctp.se.exec.constant.AtuExecConstant; import net.northking.cctp.se.lifecycle.EngineRuntime; @@ -24,11 +26,12 @@ import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; +import org.springframework.util.DigestUtils; import org.springframework.web.client.RestTemplate; import java.util.*; @@ -98,42 +101,10 @@ public class AtuPlanTaskSchedule { //获取引擎注册后的设备id EngineRuntime engineRuntime = EngineRuntime.getRuntime(); ScriptEngineInfo info = engineRuntime.getEngineInfo(); - Map paramMap = new HashMap<>(); - paramMap.put("id",info.getEngineId()); - List tryAcqurieDeviceIdList = Collections.singletonList(taskDeviceId); - paramMap.put("deviceList",tryAcqurieDeviceIdList); - paramMap.put("type","1"); - paramMap.put("userBy","engine"); - String url = atuServerConfig.getServerUrl() + atuServerConfig.getTryAcqurieDeviceUrl(); - try { - ResponseEntity result = restTemplate.postForEntity(url, paramMap, ResultWrapper.class); - if(HttpStatus.OK.equals(result.getStatusCode())){ - ResultWrapper deviceResult = result.getBody(); - if (deviceResult != null && deviceResult.isSuccess()) { - List> body = (List>)deviceResult.getData(); - if(!CollectionUtils.isEmpty(body)){ - logger.debug("移动任务请求占用成功移动端设备信息:"+JSON.toJSONString(body)); - for (int i = 0; i < body.size(); i++) { - Map deviceMap = body.get(i); - String deviceId = deviceMap.get("id"); - String deviceToken = deviceMap.get("token"); - //获取设备对应的一条计划信息 - boolean isUsed = getTaskFromQueue(deviceId, deviceToken,TYPE_MOBILE); - if(!isUsed){ - //无任务 释放设备 - logger.debug("无任务释放移动端设备:"+deviceId); - releaseDevice(deviceToken); - } - } - } - } + List deviceList = Collections.singletonList(taskDeviceId); // 需要申请的设备集合 - } - }catch (Exception e){ - logger.error("请求占用设备失败,原因:",e); - //释放所有设备 - releaseDevices(e, info.getEngineId(), tryAcqurieDeviceIdList); - } + List resultList = acquireDevice(deviceList, TYPE_MOBILE, info.getEngineId()); + useDeviceGetTask(resultList, TYPE_MOBILE); } } } @@ -152,74 +123,88 @@ public class AtuPlanTaskSchedule { if(!CollectionUtils.isEmpty(batchList)){ logger.debug("存在PC类型的委托任务共{}条", batchList.size()); if(batchList.size()>0){ - Map paramMap = new HashMap<>(); List list = new ArrayList<>(); list.add(deviceId); - paramMap.put("id",engineId); - paramMap.put("deviceList",list); - paramMap.put("type","2"); - paramMap.put("userBy","engine"); - //判断设备是否空闲 - String url = atuServerConfig.getServerUrl() + atuServerConfig.getTryAcqurieDeviceUrl(); + + List resultList = acquireDevice(list, TYPE_PC, engineId); + useDeviceGetTask(resultList, TYPE_PC); + } + } + } + + private void useDeviceGetTask(List deviceList, String type) { + if (!deviceList.isEmpty()) { + for (ApplyDeviceDto device : deviceList) { + boolean isUsed = false; try { - ResponseEntity resultEntity = restTemplate.postForEntity(url, paramMap, ResultWrapper.class); - logger.debug("pc任务请求设备使用结果:"+ JSON.toJSONString(resultEntity)); - if (HttpStatus.OK.equals(resultEntity.getStatusCode())) { - ResultWrapper deviceResult = resultEntity.getBody(); - if (deviceResult!= null && deviceResult.isSuccess()) { - List> body = (List>) deviceResult.getData(); - if (!CollectionUtils.isEmpty(body)) { - Map deviceMap = body.get(0); - String deviceToken = deviceMap.get("token"); - logger.debug("设备token:" + deviceToken); - logger.debug("当前设备{}占用成功。", deviceId); - boolean isUsed = getTaskFromQueue(deviceId, deviceToken, TYPE_PC); - if (!isUsed) { - //设备无任务可领 - logger.debug("无任务释放PC设备:" + deviceId); - releaseDevice(deviceToken); - } - } - } + logger.debug("设备{}占用成功,token:{}", device.getId(), device.getToken()); + isUsed = getTaskFromQueue(device.getId(), device.getToken(), type); + if (!isUsed) { + // 设备无任务可领 + logger.debug("无任务释放设备:{}", device.getId()); } } catch (Exception e) { - logger.error("设备请求占用异常:",e); - releaseDevices(e, info.getEngineId(), list); + logger.error("从队列获取任务失败,准备释放设备{}", device.getId(), e); + } finally { + if (!isUsed) { + releaseDevice(device.getToken()); + } } } } } - private void releaseDevices(Exception e, String engineId, List deviceIds) { - //释放所有设备 - String errorMsg = e.getMessage(); - if (errorMsg.contains("Read timed out")) { - int count = 0; - Map requestMap = new HashMap<>(); - requestMap.put("id", engineId); - requestMap.put("deviceList", deviceIds); - while (count++ < 50) { - try { - logger.debug("pc任务请求占用设备超时处理===="); - //请求超时释放请求设备 - String urlRelease = atuServerConfig.getServerUrl() + atuServerConfig.getReleaseDeviceBatch(); - ResponseEntity entity = restTemplate.postForEntity(urlRelease, requestMap, ResultWrapper.class); - if (HttpStatus.OK == entity.getStatusCode()) { - count += 50; - } - Thread.sleep(2000); - } catch (Exception innerE) { - // todo: 增加发送队列 - logger.error("超时释放设备失败:", innerE); - logger.debug("使用队列方式释放使用的设备:{}", requestMap); - try { - rabbitTemplate.convertAndSend(AtuExecConstant.ENGINE_RELEASE_ID, new Message(om.writeValueAsBytes(requestMap))); - count += 50; - } catch (JsonProcessingException jpe) { - logger.error("释放设备失败", jpe); - } + + /** + * + * @param deviceList 需要申请的设备集合 + * @param type 1-pc 2-移动 + * @param engineId 引擎id + * @return + */ + private List acquireDevice(List deviceList, String type, String engineId) { + if (deviceList.isEmpty()) { + return new ArrayList<>(); + } + BatchApplyDeviceDto queryDto = new BatchApplyDeviceDto(); + queryDto.setId(engineId); + queryDto.setUserBy("engine"); + List queryDeviceList = new ArrayList<>(); + queryDto.setDeviceList(queryDeviceList); + for (String deviceId : deviceList) { + ApplyDeviceDto dto = new ApplyDeviceDto(); + dto.setId(deviceId); + dto.setType(type); + dto.setToken(DigestUtils.md5DigestAsHex(String.format("%s@%s@%s", deviceId, engineId, System.currentTimeMillis()).getBytes())); + queryDeviceList.add(dto); + } + try { + String url = atuServerConfig.getServerUrl() + atuServerConfig.getAcquireDeviceWithTokenUrl(); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(queryDto, headers); + ResponseEntity>> resultEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, new ParameterizedTypeReference>>(){}); + logger.debug("{} 任务请求设备使用是结果:{}", TYPE_MOBILE.equals(type) ? "移动" : "PC", JSON.toJSONString(resultEntity)); + if (resultEntity.getStatusCode().is2xxSuccessful()) { + ResultWrapper> deviceResult = resultEntity.getBody(); + if (deviceResult != null && deviceResult.isSuccess() && deviceResult.getData() != null && !deviceResult.getData().isEmpty()) { + return deviceResult.getData(); } } + } catch (Exception e) { + logger.error("设备请求占用异常", e); + releaseDeviceAsException(queryDeviceList); + } + return new ArrayList<>(); + } + + /** + * 由于token是引擎自行创建,如果出现申请出错的时候,也可以使用该token释放,设备管理会自行判断该token是否有效 + * @param queryDeviceList 申请的设备集合 + */ + private void releaseDeviceAsException(List queryDeviceList) { + for (ApplyDeviceDto dto : queryDeviceList) { + releaseDevice(dto.getToken()); } } diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-linux.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-linux.yml index b1a887a..acf81a9 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-linux.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-linux.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-uos.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-uos.yml index 3331455..8a05953 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-uos.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-uos.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-win.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-win.yml index d922974..3cb98e5 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-win.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-devFront-win.yml @@ -45,6 +45,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-linux.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-linux.yml index 14e5a28..d0bd626 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-linux.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-linux.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-uos.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-uos.yml index a914d83..be293ad 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-uos.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-uos.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-win.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-win.yml index 2a72e43..465f429 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-win.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-prod-win.yml @@ -45,6 +45,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-linux.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-linux.yml index 0544803..9dd285c 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-linux.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-linux.yml @@ -46,6 +46,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-uos.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-uos.yml index 3331455..8a05953 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-uos.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-uos.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-win.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-win.yml index 2d0a014..1ecc7e1 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-win.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-test-win.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-linux.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-linux.yml index cc540aa..732c705 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-linux.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-linux.yml @@ -46,6 +46,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-uos.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-uos.yml index 2bb9a0b..c0a4c69 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-uos.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-uos.yml @@ -46,6 +46,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-win.yml b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-win.yml index d964f66..33b4b27 100644 --- a/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-win.yml +++ b/cctp-atu/atu-engine/atu-script-engine/src/main/resources/application-uat-win.yml @@ -44,6 +44,7 @@ atu: projectInfo: /api/cctp-projects/pub/project/query/info?projectId={projectId} testSysUrl: /api/cctp-projects/pub/CpProjectEnvSyss/querySysUrl busComInfoUrl: /api/atu-script-case/pub/busCom/findBusComName + acquireDeviceWithTokenUrl: /api/cctp-device-mgr/pub/deviceList/apply/batchWithToken engine: executor-pool: pcSize: 1 diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiService.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiService.java index 0917ee6..81394e4 100644 --- a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiService.java +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiService.java @@ -4,10 +4,7 @@ import net.northking.cctp.common.db.Pagination; import net.northking.cctp.common.http.QueryByPage; import net.northking.cctp.device.db.entity.CdDeviceToken; import net.northking.cctp.device.db.entity.CdDeviceUsageLog; -import net.northking.cctp.device.dto.device.CdMobileDeviceExitDto; -import net.northking.cctp.device.dto.device.DeviceLockDto; -import net.northking.cctp.device.dto.device.DeviceLogDto; -import net.northking.cctp.device.dto.device.DeviceInfoDto; +import net.northking.cctp.device.dto.device.*; import net.northking.cctp.device.dto.pcDevice.CdDeviceRemoteDto; import net.northking.cctp.device.dto.pcDevice.CdPcDeviceExitDto; import net.northking.cctp.device.dto.pcDevice.CdPcDeviceRemoteDto; @@ -62,4 +59,13 @@ public interface DeviceActivityApiService { * @param deviceTokens 设备token信息 */ void release(List deviceTokens); + + /** + * 批量申请设备 + * @param devices + * @param request + * @return + */ + List deviceListApply(BatchApplyDeviceDto devices, HttpServletRequest request); + } diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiServiceImpl.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiServiceImpl.java index e41a1db..888a41f 100644 --- a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiServiceImpl.java +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/api/activity/service/DeviceActivityApiServiceImpl.java @@ -3,7 +3,6 @@ package net.northking.cctp.device.api.activity.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; -import com.github.pagehelper.page.PageMethod; import net.northking.cctp.common.cache.EntityNameCache; import net.northking.cctp.common.cache.EntityNameCatalog; import net.northking.cctp.common.db.Pagination; @@ -15,10 +14,7 @@ import net.northking.cctp.device.constants.DeviceConstants; import net.northking.cctp.device.constants.DeviceError; import net.northking.cctp.device.db.entity.*; import net.northking.cctp.device.db.service.*; -import net.northking.cctp.device.dto.device.CdMobileDeviceExitDto; -import net.northking.cctp.device.dto.device.DeviceInfoDto; -import net.northking.cctp.device.dto.device.DeviceLockDto; -import net.northking.cctp.device.dto.device.DeviceLogDto; +import net.northking.cctp.device.dto.device.*; import net.northking.cctp.device.dto.pcDevice.CdDeviceRemoteDto; import net.northking.cctp.device.dto.pcDevice.CdPcDeviceExitDto; import net.northking.cctp.device.dto.pcDevice.CdPcDeviceRemoteDto; @@ -31,6 +27,7 @@ import org.springframework.data.redis.core.ZSetOperations; import org.springframework.integration.redis.util.RedisLockRegistry; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import org.springframework.util.DigestUtils; import org.springframework.util.StringUtils; @@ -38,7 +35,6 @@ import javax.servlet.http.HttpServletRequest; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; -import java.util.stream.Collectors; @Service @@ -690,6 +686,10 @@ public class DeviceActivityApiServiceImpl implements DeviceActivityApiService { } public Map applyMobDevice(String deviceId, String userId,HttpServletRequest request,String userBy) { + return applyMobDevice(deviceId, userId, request, userBy, null); + } + + public Map applyMobDevice(String deviceId, String userId,HttpServletRequest request,String userBy, String token) { //校验设备是否使用中 如果设备使用中 直接返回null CdDeviceStatus status = this.cdDeviceStatusService.findByPrimaryKey(deviceId); if (!status.getUsageStatus().equals(DeviceConstants.DEVICE_STATUS_ONLINE)) { @@ -705,8 +705,10 @@ public class DeviceActivityApiServiceImpl implements DeviceActivityApiService { CdDeviceCtrl ctrl = this.cdDeviceCtrlService.findByPrimaryKey(device.getCtrlId()); Date date = new Date(); - String index = deviceId + DeviceConstants.STR_REGEX + ctrl.getId() + DeviceConstants.STR_REGEX + date; - String token = DigestUtils.md5DigestAsHex(index.getBytes()); + if (StringUtils.hasText(token)) { // 如果传入token,那么使用传入token + String index = deviceId + DeviceConstants.STR_REGEX + ctrl.getId() + DeviceConstants.STR_REGEX + date; + token = DigestUtils.md5DigestAsHex(index.getBytes()); + } //记录到设备令牌表 CdDeviceToken cdDeviceToken = new CdDeviceToken(); @@ -748,6 +750,10 @@ public class DeviceActivityApiServiceImpl implements DeviceActivityApiService { } public Map applyPcDevice(String deviceId, String userId,HttpServletRequest request,String userBy) { + return applyPcDevice(deviceId, userId, request, userBy, null); + } + + public Map applyPcDevice(String deviceId, String userId,HttpServletRequest request,String userBy, String token) { CdDeviceStatus status = this.cdDeviceStatusService.findByPrimaryKey(deviceId); if (!status.getUsageStatus().equals(DeviceConstants.DEVICE_STATUS_ONLINE)) { return null; @@ -760,8 +766,10 @@ public class DeviceActivityApiServiceImpl implements DeviceActivityApiService { List engineList = this.cdEngineInfoService.query(cdEngineInfo); //新增令牌 Date date = new Date(); - String index = deviceId + DeviceConstants.STR_REGEX + engineList.get(0).getId() + DeviceConstants.STR_REGEX + date; - String token = DigestUtils.md5DigestAsHex(index.getBytes()); + if (StringUtils.hasText(token)) { + String index = deviceId + DeviceConstants.STR_REGEX + engineList.get(0).getId() + DeviceConstants.STR_REGEX + date; + token = DigestUtils.md5DigestAsHex(index.getBytes()); + } //记录到设备令牌表 CdDeviceToken cdDeviceToken = new CdDeviceToken(); cdDeviceToken.setId(deviceId); @@ -837,4 +845,52 @@ public class DeviceActivityApiServiceImpl implements DeviceActivityApiService { } } } + + /** + * 批量申请设备 + * type解释:引擎中1代表pc设备,2代表移动设备,注意 + * + * @param devices + * @param request + * @return + */ + @Override + public List deviceListApply(BatchApplyDeviceDto devices, HttpServletRequest request) { + List resultList = new ArrayList<>(); + String userId = devices.getId(); + String userBy = devices.getUserBy(); + if (!CollectionUtils.isEmpty(devices.getDeviceList())) { + List applyList = devices.getDeviceList(); + for (ApplyDeviceDto device : applyList) { + ApplyDeviceDto result = new ApplyDeviceDto(); + Map deviceTokenMap = null; + String deviceId = device.getId(); + String lockKey = String.format("%s%s", DeviceConstants.ENGINE_DEVICE_TYPE_MOBILE.equals(device.getType()) ? DeviceConstants.LOCK_MOBILE : DeviceConstants.LOCK_PC, deviceId); + Lock lock = redisLockRegistry.obtain(lockKey); + try { + if (lock.tryLock(5000, TimeUnit.MILLISECONDS)) { + if (DeviceConstants.ENGINE_DEVICE_TYPE_MOBILE.equals(device.getType())) { + deviceTokenMap = applyMobDevice(deviceId, userId, request, userBy, device.getToken()); + } else { + deviceTokenMap = applyPcDevice(deviceId, userId, request, userBy, device.getToken()); + } + } + } catch (InterruptedException e) { + logger.error("申请移动设备异常:",e); + Thread.currentThread().interrupt(); + } finally { + lock.unlock(); + } + if (deviceTokenMap != null) { + result.setId(deviceTokenMap.get("id")); + result.setToken(deviceTokenMap.get("token")); + if (StringUtils.hasText(result.getToken())) { + resultList.add(result); + } + } + } + } + return resultList; + } + } diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/bus/receiver/DeviceMQReceiver.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/bus/receiver/DeviceMQReceiver.java index a572f76..6b691a9 100644 --- a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/bus/receiver/DeviceMQReceiver.java +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/bus/receiver/DeviceMQReceiver.java @@ -38,7 +38,7 @@ public class DeviceMQReceiver { private final static Logger logger = LoggerFactory.getLogger(DeviceMQReceiver.class); @RabbitListener(queuesToDeclare = {@Queue(value = "execute.engine.release")}) - public void DeviceMQReceiver(Message message){ + public void releaseReceive(Message message){ // 释放设备 try{ byte[] body = message.getBody(); @@ -73,21 +73,4 @@ public class DeviceMQReceiver { logger.error("释放设备异常",e); } } - - /** - * - * @param message - */ - @RabbitListener(queuesToDeclare = {@Queue(value = "execute.engine.release.id")}) - public void releaseById(Message message){ - try{ - byte[] body = message.getBody(); - Map mqParam = om.readValue(body, new TypeReference>() { - }); - - activityService.releaseDeviceList(mqParam); - }catch (Exception e){ - logger.error("释放设备异常",e); - } - } } diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/constants/DeviceConstants.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/constants/DeviceConstants.java index 394f8ec..c5fcacb 100644 --- a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/constants/DeviceConstants.java +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/constants/DeviceConstants.java @@ -20,7 +20,7 @@ public class DeviceConstants { */ public final static String LOCK_MOBILE = "lock:mobile:"; - public final static String LOCK_PC = "lock:mobile:"; + public final static String LOCK_PC = "lock:pc:"; //安卓类型 public final static String MOBILE_PLATFORM_ANDROID = "0"; @@ -202,4 +202,9 @@ public class DeviceConstants { public final static String WS_AMOUNT = "amount"; public static final String ENGINE_MOB_ACTIVE_THREAD_KEY = "engine:mobile:activeThread"; + + public final static String ENGINE_DEVICE_TYPE_PC = "1"; // 引擎使用的设备类型,PC + + public final static String ENGINE_DEVICE_TYPE_MOBILE = "2"; // 引擎使用的设备类型,移动 + } diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/ApplyDeviceDto.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/ApplyDeviceDto.java new file mode 100644 index 0000000..4a25215 --- /dev/null +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/ApplyDeviceDto.java @@ -0,0 +1,21 @@ +package net.northking.cctp.device.dto.device; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.ToString; + +@Data +@ApiModel(value = "设备申请") +@ToString +public class ApplyDeviceDto { + + @ApiModelProperty(value = "设备Id") + private String id; + + @ApiModelProperty(value = "设备类型", notes = "1-pc,2-移动") + private String type; + + @ApiModelProperty(value = "token", notes = "可由请求方带上") + private String token; +} diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/BatchApplyDeviceDto.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/BatchApplyDeviceDto.java new file mode 100644 index 0000000..cb7c32c --- /dev/null +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/dto/device/BatchApplyDeviceDto.java @@ -0,0 +1,25 @@ +package net.northking.cctp.device.dto.device; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +@Data +@ApiModel(value = "设备批量申请dto") +@ToString +public class BatchApplyDeviceDto { + + + @ApiModelProperty(value = "申请人id") + private String id; + + @ApiModelProperty(value = "使用来源,engine为引擎使用0") + private String userBy; + + @ApiModelProperty(value = "设备列表") + private List deviceList; + +} diff --git a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/pub/DevicePubCtrl.java b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/pub/DevicePubCtrl.java index baa4793..96c17ad 100644 --- a/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/pub/DevicePubCtrl.java +++ b/cctp-production/cctp-device-mgr/src/main/java/net/northking/cctp/device/pub/DevicePubCtrl.java @@ -10,9 +10,7 @@ import net.northking.cctp.device.db.entity.CdDeviceStatus; import net.northking.cctp.device.db.entity.CdDeviceToken; import net.northking.cctp.device.db.service.CdDeviceStatusService; import net.northking.cctp.device.db.service.CdDeviceTokenService; -import net.northking.cctp.device.dto.device.CdMobileDeviceExitDto; -import net.northking.cctp.device.dto.device.DeviceInfoDto; -import net.northking.cctp.device.dto.device.DeviceLockDto; +import net.northking.cctp.device.dto.device.*; import net.northking.cctp.device.dto.pcDevice.CdDeviceRemoteDto; import net.northking.cctp.device.dto.pcDevice.CdPcDeviceExitDto; import net.northking.cctp.device.dto.pcDevice.CdPcDeviceRemoteDto; @@ -161,4 +159,13 @@ public class DevicePubCtrl { Map> result = apiService.queryDeviceType(deviceIds); return wrapper.success(result); } + + @ApiOperation(value = "批量申请设备使用", notes = "token由请求方提供") + @PostMapping(value = "/deviceList/apply/batchWithToken") + public ResultWrapper> batchApplyWithToken(@RequestBody BatchApplyDeviceDto devices, HttpServletRequest request) { + logger.debug("批量申请设备(pub),{}", devices); + ResultWrapper> wrapper = new ResultWrapper<>(); + List resultList = apiService.deviceListApply(devices, request); + return wrapper.success(resultList); + } }