From 86ab4fb8d767581502ca7308467ca72cdf0d345b Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 4 Aug 2020 19:45:08 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=AE=BE=E5=A4=87=E6=8E=A5=E5=8F=A3=E5=8F=8A=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=202.=20=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=8E=A5=E5=8F=A3=E5=8F=8A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dispose/common/CommonEnumHandler.java | 20 ++++ .../DisposeDeviceManagerController.java | 96 +++++++++++++++++- .../exception/GlobalExceptionHandler.java | 11 ++- .../dispose/manager/DisposeDeviceManager.java | 6 +- .../impl/DisposeDeviceManagerImpl.java | 14 ++- .../manager/impl/UserAccountManagerImpl.java | 2 +- .../pojo/dto/protocol/auth/LoginReq.java | 2 +- .../pojo/dto/protocol/auth/LoginRsp.java | 8 +- .../pojo/dto/protocol/base/BaseIdResp.java | 2 +- .../pojo/dto/protocol/base/IdArraysReq.java | 35 +++++++ .../pojo/dto/protocol/base/ValidGroups.java | 32 +++++- .../device/manager/AddDeviceInfo.java | 9 +- .../protocol/device/manager/AddDeviceReq.java | 3 +- .../protocol/device/manager/AddDeviceRet.java | 34 +++++++ .../protocol/device/manager/AddDeviceRsp.java | 9 +- .../protocol/device/manager/DelDeviceRsp.java | 30 ++++++ .../service/DisposeDeviceManagerService.java | 14 +++ .../impl/DisposeDeviceManagerServiceImpl.java | 40 +++++++- src/main/resources/mappers/DisposeDevice.xml | 8 +- .../test/mapper/DisposeDeviceMapperTest.java | 97 ++++++++++--------- 20 files changed, 387 insertions(+), 85 deletions(-) create mode 100644 src/main/java/com/dispose/pojo/dto/protocol/base/IdArraysReq.java create mode 100644 src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRet.java create mode 100644 src/main/java/com/dispose/pojo/dto/protocol/device/manager/DelDeviceRsp.java diff --git a/src/main/java/com/dispose/common/CommonEnumHandler.java b/src/main/java/com/dispose/common/CommonEnumHandler.java index 6c4cb475..e05718b8 100644 --- a/src/main/java/com/dispose/common/CommonEnumHandler.java +++ b/src/main/java/com/dispose/common/CommonEnumHandler.java @@ -122,4 +122,24 @@ public final class CommonEnumHandler extends BaseTypeHandler } throw new IllegalArgumentException(enumType.getName() + " unknown enumerated type index:" + index); } + + /** + * Code of e. + * + * @param the type parameter + * @param enumClass the enum class + * @param code the code + * @return the e + */ + public static & BaseEnum> E codeOf(Class enumClass, int code) { + E[] enumCodes = enumClass.getEnumConstants(); + + for(E e : enumCodes) { + if(e.getValue() == code) { + return e; + } + } + + return null; + } } diff --git a/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java b/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java index 5ca791df..5b2d8e70 100644 --- a/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java +++ b/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java @@ -1,10 +1,19 @@ package com.dispose.controller; +import com.dispose.common.CommonEnumHandler; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.DisposeObjectType; import com.dispose.common.ErrorCode; -import com.dispose.pojo.dto.protocol.base.BaseRespStatus; +import com.dispose.common.IpAddrType; +import com.dispose.pojo.dto.protocol.base.IdArraysReq; import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO; import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; +import com.dispose.pojo.dto.protocol.base.ValidGroups; import com.dispose.pojo.dto.protocol.device.manager.AddDeviceReq; +import com.dispose.pojo.dto.protocol.device.manager.AddDeviceRet; +import com.dispose.pojo.dto.protocol.device.manager.AddDeviceRsp; +import com.dispose.pojo.dto.protocol.device.manager.DelDeviceRsp; +import com.dispose.pojo.entity.DisposeCapacity; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.MulReturnType; import com.dispose.service.DisposeDeviceManagerService; @@ -14,6 +23,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -36,6 +46,9 @@ import java.util.List; @Validated public class DisposeDeviceManagerController { + /** + * The Dispose device manager service. + */ @Resource private DisposeDeviceManagerService disposeDeviceManagerService; @@ -48,12 +61,13 @@ public class DisposeDeviceManagerController { @PutMapping("/device") @ResponseBody @ApiOperation("添加处置能力节点") - public ProtocolRespDTO addDisposeDevice( - @Validated + public ProtocolRespDTO addDisposeDevice( + @Validated(ValidGroups.AddDeviceValid.class) @RequestBody ProtocolReqDTO mr) { List devs = new ArrayList<>(); + // 获取请求中的需要添加的设备列表 mr.getMsgContent().getItems().forEach(v -> { DisposeDevice dev = DisposeDevice.builder() .ipAddr(v.getIpAddr()) @@ -70,13 +84,87 @@ public class DisposeDeviceManagerController { .urlType(v.getUrlType()) .readme(v.getReadme()) .build(); + + // 初始化设备能力信息 + dev.setDevCapacity(new ArrayList<>()); + + v.getCapacity().forEach(k -> { + // 添加设备能力 + DisposeCapacity cap = DisposeCapacity.builder() + .capacityType(CommonEnumHandler.codeOf(DisposeCapacityType.class, k.getCapacityType())) + .ipType(CommonEnumHandler.codeOf(IpAddrType.class, k.getIpType())) + .objectType(CommonEnumHandler.codeOf(DisposeObjectType.class, k.getObjectType())) + .protectIp(k.getProtectIp()) + .build(); + dev.getDevCapacity().add(cap); + }); + devs.add(dev); }); + // 添加设备 List> ret = disposeDeviceManagerService.addDisposeDevice(devs); + // 协议响应消息 + AddDeviceRsp rspInfo = new AddDeviceRsp(); + ret.forEach(v -> { + DisposeDevice dev = v.getSecondParam(); + AddDeviceRet rsp = AddDeviceRet.builder() + .ipAddr(dev.getIpAddr()) + .ipPort(dev.getIpPort()) + .build(); - return ProtocolRespDTO.result(ErrorCode.ERR_OK); + rsp.setStatus(v.getFirstParam().getCode()); + rsp.setMessage(new String[] {v.getFirstParam().getMsg()}); + + // 添加设备成功,记录新增设备ID + if(rsp.getStatus() == ErrorCode.ERR_OK.getCode()) { + rsp.setDevId(String.valueOf(dev.getId())); + } + + rspInfo.getItems().add(rsp); + }); + + return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); + } + + /** + * Remove dispose device protocol resp dto. + * + * @param mr the mr + * @return the protocol resp dto + */ + @DeleteMapping("/device") + @ResponseBody + @ApiOperation("删除处置能力节点") + public ProtocolRespDTO> removeDisposeDevice( + @Validated(ValidGroups.ExplicitIdArrayValid.class) + @RequestBody ProtocolReqDTO mr) { + + List idList = new ArrayList<>(); + + for (String s : mr.getMsgContent().getId()) { + idList.add(Long.parseLong(s)); + } + + List> ret = + disposeDeviceManagerService.removeDisposeDevice(idList); + + List rspInfo = new ArrayList<>(); + + ret.forEach(v -> { + DelDeviceRsp rsp = new DelDeviceRsp(); + DisposeDevice dev = v.getSecondParam(); + + rsp.setId(String.valueOf(dev.getId())); + rsp.setDevStatus(dev.getStatus()); + rsp.setStatus(v.getFirstParam().getCode()); + rsp.setMessage(new String[] {v.getFirstParam().getMsg()}); + + rspInfo.add(rsp); + }); + + return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } } diff --git a/src/main/java/com/dispose/exception/GlobalExceptionHandler.java b/src/main/java/com/dispose/exception/GlobalExceptionHandler.java index 2a9f13c2..d0c0db7b 100644 --- a/src/main/java/com/dispose/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/dispose/exception/GlobalExceptionHandler.java @@ -32,7 +32,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ProtocolRespDTO handleException(MethodArgumentNotValidException ex) { - log.error("Argument Exception: {}", ex.getMessage()); + log.error("Argument Exception: ", ex); List exMsg = new ArrayList<>(); AtomicInteger idx = new AtomicInteger(); @@ -45,4 +45,13 @@ public class GlobalExceptionHandler { HttpStatus.error400().status(), exMsg.toArray(new String[0])); } + + @ExceptionHandler(Throwable.class) + @ResponseBody + public ProtocolRespDTO handleException(Throwable ex) { + log.error("Throwable Exception: ", ex); + + return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, + HttpStatus.error400().status(), new String[] {ErrorCode.ERR_PARAMEXCEPTION.getMsg()}); + } } diff --git a/src/main/java/com/dispose/manager/DisposeDeviceManager.java b/src/main/java/com/dispose/manager/DisposeDeviceManager.java index 6dafd1ed..f8ac025b 100644 --- a/src/main/java/com/dispose/manager/DisposeDeviceManager.java +++ b/src/main/java/com/dispose/manager/DisposeDeviceManager.java @@ -18,8 +18,12 @@ public interface DisposeDeviceManager { * * @param dev the dev * @return the mul return type + * @throws IllegalAccessException the illegal access exception + * @throws NoSuchMethodException the no such method exception + * @throws InvocationTargetException the invocation target exception */ - MulReturnType addDisposeDevice(DisposeDevice dev); + MulReturnType addDisposeDevice(DisposeDevice dev) throws IllegalAccessException, + NoSuchMethodException, InvocationTargetException; /** * Upgrade dispose device error code. diff --git a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java index 2a1cbfdf..d44320cc 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java @@ -44,13 +44,22 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { * * @param dev the dev * @return the mul return type + * @throws IllegalAccessException the illegal access exception + * @throws NoSuchMethodException the no such method exception + * @throws InvocationTargetException the invocation target exception */ @Override - public MulReturnType addDisposeDevice(DisposeDevice dev) { + public MulReturnType addDisposeDevice(DisposeDevice dev) throws IllegalAccessException, + NoSuchMethodException, InvocationTargetException { // 看看系统中有没有存在相同IP+端口地址的设备,有的话返回失败 DisposeDevice tDev = disposeDeviceMapper.getDeviceByAddress(dev.getIpAddr(), dev.getIpPort()); if (tDev != null) { + + if(dev.getStatus() == ObjectStatus.DELETED) { + return new MulReturnType<>(upgradeDisposeDevice(dev), tDev.getId()); + } + return new MulReturnType<>(ErrorCode.ERR_DEVICEEXISTS, tDev.getId()); } @@ -72,6 +81,9 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { * * @param dev the dev * @return the error code + * @throws IllegalAccessException the illegal access exception + * @throws NoSuchMethodException the no such method exception + * @throws InvocationTargetException the invocation target exception */ @Override public ErrorCode upgradeDisposeDevice(DisposeDevice dev) throws IllegalAccessException, NoSuchMethodException, diff --git a/src/main/java/com/dispose/manager/impl/UserAccountManagerImpl.java b/src/main/java/com/dispose/manager/impl/UserAccountManagerImpl.java index 1882266a..0b9613ba 100644 --- a/src/main/java/com/dispose/manager/impl/UserAccountManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/UserAccountManagerImpl.java @@ -388,7 +388,7 @@ public class UserAccountManagerImpl implements UserAccountManager { return ErrorCode.ERR_LOGOUT; } - if (ADMIN_USERS.length == 0) { + if (ADMIN_USERS == null || ADMIN_USERS.length == 0) { ADMIN_USERS = userPermissionConfigure.getAdminUsers().split(disposeConfigure.getSplitChar()); } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java b/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java index cfe1c7f0..5bd425ea 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java @@ -26,7 +26,7 @@ public class LoginReq { /** * The User name. */ - @NotBlank(message = "userName 用户名不能为空") + @NotBlank(message = "userName 用户名不能为空", groups = ValidGroups.LogoutReqValid.class) @Pattern(regexp = AuthConfigValue.MYSQL_REGEX_CHARS, flags = Pattern.Flag.CASE_INSENSITIVE, message = "userName 用户名存在非法字符串") diff --git a/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginRsp.java index 656c39c7..cd8f4208 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginRsp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginRsp.java @@ -24,21 +24,21 @@ import lombok.NoArgsConstructor; public class LoginRsp extends BaseRespStatus { /** - * 登录的用户名 + * The User name. */ private String userName; /** - * 访问权限token + * The Token. */ private String token; /** - * 登录UTC时间戳(ms) + * The Log time. */ private Long logTime; /** - * token超时时间 (分钟) + * The Expire time. */ private Long expireTime; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/base/BaseIdResp.java b/src/main/java/com/dispose/pojo/dto/protocol/base/BaseIdResp.java index 10f16914..b9336a3a 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/base/BaseIdResp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/base/BaseIdResp.java @@ -16,7 +16,7 @@ import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor -@JsonPropertyOrder({"id", "devId", "status", "message"}) +@JsonPropertyOrder({"ipAddr", "devId", "status", "message"}) @JsonInclude(JsonInclude.Include.NON_NULL) public class BaseIdResp extends BaseRespStatus { /** diff --git a/src/main/java/com/dispose/pojo/dto/protocol/base/IdArraysReq.java b/src/main/java/com/dispose/pojo/dto/protocol/base/IdArraysReq.java new file mode 100644 index 00000000..d13d607d --- /dev/null +++ b/src/main/java/com/dispose/pojo/dto/protocol/base/IdArraysReq.java @@ -0,0 +1,35 @@ +package com.dispose.pojo.dto.protocol.base; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +/** + * The type Id arrays req. + * + * @author + */ +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class IdArraysReq { + + /** + * The Id. + */ + @NotNull(message = "id 字段不能为空", groups = ValidGroups.IdArrayValid.class) + @Size(min = 1, message = "id 字段必须指定明确id编号", groups = ValidGroups.ExplicitIdArrayValid.class) + private String[] id; + + /** + * The Task id. + */ + private String[] taskId; +} diff --git a/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java b/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java index accda024..6aedc957 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java @@ -7,7 +7,7 @@ package com.dispose.pojo.dto.protocol.base; */ public class ValidGroups { /** - * The interface Protocol common. + * The interface Protocol common valid. * * @author */ @@ -15,18 +15,42 @@ public class ValidGroups { } /** - * The interface Login req. + * The interface Login req valid. * * @author */ - public interface LoginReqValid extends ProtocolCommonValid { + public interface LoginReqValid extends LogoutReqValid { } /** - * The interface Logout req. + * The interface Logout req valid. * * @author */ public interface LogoutReqValid extends ProtocolCommonValid { } + + /** + * The interface Add device valid. + * + * @author + */ + public interface AddDeviceValid extends ProtocolCommonValid { + } + + /** + * The interface Id array valid. + * + * @author + */ + public interface IdArrayValid extends ProtocolCommonValid { + } + + /** + * The interface Explicit id array valid. + * + * @author + */ + public interface ExplicitIdArrayValid extends IdArrayValid{ + } } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java index 2324d2b2..c7fb8f50 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java @@ -2,6 +2,7 @@ package com.dispose.pojo.dto.protocol.device.manager; import com.dispose.common.DisposeDeviceType; import com.dispose.common.HttpType; +import com.dispose.pojo.dto.protocol.base.ValidGroups; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; @@ -27,13 +28,13 @@ public class AddDeviceInfo { /** * The Ip addr. */ - @NotBlank(message = "ipAddr IP地址不能为空") + @NotBlank(message = "ipAddr IP地址不能为空", groups = ValidGroups.AddDeviceValid.class) private String ipAddr; /** * The Ip port. */ - @NotNull(message = "ipPort 端口不能为null, 默认端口用空字符串表示") + @NotNull(message = "ipPort 端口不能为null, 默认端口用空字符串表示", groups = ValidGroups.AddDeviceValid.class) private String ipPort; /** @@ -79,7 +80,7 @@ public class AddDeviceInfo { /** * The Url path. */ - @NotBlank(message = "urlPath URL接口路径不能为空") + @NotBlank(message = "urlPath URL接口路径不能为空", groups = ValidGroups.AddDeviceValid.class) private String urlPath; /** @@ -95,7 +96,7 @@ public class AddDeviceInfo { /** * The Capacity. */ - @NotNull(message = "capacity 处置设备能力不能为空") + @NotNull(message = "capacity 处置设备能力不能为空", groups = ValidGroups.AddDeviceValid.class) @Valid private List capacity; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java index 05fa171f..1a4c02c7 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java @@ -1,5 +1,6 @@ package com.dispose.pojo.dto.protocol.device.manager; +import com.dispose.pojo.dto.protocol.base.ValidGroups; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; @@ -24,7 +25,7 @@ public class AddDeviceReq { /** * The Items. */ - @NotNull(message = "items 设备列表不能为空") + @NotNull(message = "items 设备列表不能为空", groups = ValidGroups.AddDeviceValid.class) @Valid private List items; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRet.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRet.java new file mode 100644 index 00000000..f32cedd1 --- /dev/null +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRet.java @@ -0,0 +1,34 @@ +package com.dispose.pojo.dto.protocol.device.manager; + +import com.dispose.pojo.dto.protocol.base.BaseIdResp; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * The type Add device ret. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonPropertyOrder({"ipAddr", "ipPort", "devId", "status", "message"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class AddDeviceRet extends BaseIdResp { + /** + * The Ip addr. + */ + private String ipAddr; + + /** + * The Ip port. + */ + private String ipPort; +} diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java index d6d9680f..3a0ef51d 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java @@ -1,10 +1,7 @@ package com.dispose.pojo.dto.protocol.device.manager; -import com.dispose.pojo.dto.protocol.base.BaseIdResp; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.Data; -import lombok.EqualsAndHashCode; import java.util.ArrayList; import java.util.List; @@ -14,15 +11,13 @@ import java.util.List; * * @author */ -@EqualsAndHashCode(callSuper = true) @Data -@JsonPropertyOrder({"ipAddr", "ipPort", "devId", "status", "message"}) @JsonInclude(JsonInclude.Include.NON_NULL) -public class AddDeviceRsp extends BaseIdResp { +public class AddDeviceRsp { /** * The Items. */ - private List items; + private List items; /** * Instantiates a new Add device rsp. diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/DelDeviceRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/DelDeviceRsp.java new file mode 100644 index 00000000..4187f327 --- /dev/null +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/DelDeviceRsp.java @@ -0,0 +1,30 @@ +package com.dispose.pojo.dto.protocol.device.manager; + +import com.dispose.common.ObjectStatus; +import com.dispose.pojo.dto.protocol.base.BaseIdResp; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * The type Del device rsp. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"id", "devStatus", "status", "message"}) +public class DelDeviceRsp extends BaseIdResp { + /** + * The Dev status. + */ + private ObjectStatus devStatus; +} diff --git a/src/main/java/com/dispose/service/DisposeDeviceManagerService.java b/src/main/java/com/dispose/service/DisposeDeviceManagerService.java index ec034ea4..2f1ee36a 100644 --- a/src/main/java/com/dispose/service/DisposeDeviceManagerService.java +++ b/src/main/java/com/dispose/service/DisposeDeviceManagerService.java @@ -12,5 +12,19 @@ import java.util.List; * @author */ public interface DisposeDeviceManagerService { + /** + * Add dispose device list. + * + * @param devs the devs + * @return the list + */ List> addDisposeDevice(List devs); + + /** + * Remove dispose device list. + * + * @param ids the ids + * @return the list + */ + List> removeDisposeDevice(List ids); } diff --git a/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java index 0f25d86d..62a00360 100644 --- a/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java @@ -1,6 +1,7 @@ package com.dispose.service.impl; import com.dispose.common.ErrorCode; +import com.dispose.common.ObjectStatus; import com.dispose.manager.DisposeDeviceManager; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.MulReturnType; @@ -9,6 +10,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; @@ -38,13 +40,41 @@ public class DisposeDeviceManagerServiceImpl implements DisposeDeviceManagerServ List> rspList = new ArrayList<>(); devs.forEach(v -> { - MulReturnType ret = disposeDeviceManager.addDisposeDevice(v); - - v.setId(ret.getSecondParam()); - rspList.add(new MulReturnType<>(ret.getFirstParam(), v)); + MulReturnType ret = null; + try { + ret = disposeDeviceManager.addDisposeDevice(v); + v.setId(ret.getSecondParam()); + rspList.add(new MulReturnType<>(ret.getFirstParam(), v)); + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + rspList.add(new MulReturnType<>(ErrorCode.ERR_SYSTEMEXCEPTION, v)); + } }); - //AddDeviceRsp rspInfo = AddDeviceRsp.b + return rspList; + } + + /** + * Remove dispose device list. + * + * @param ids the ids + * @return the list + */ + @Override + public List> removeDisposeDevice(List ids) { + List> rspList = new ArrayList<>(); + + for (Long v : ids) { + DisposeDevice dev = new DisposeDevice(); + + + MulReturnType ret = disposeDeviceManager.changeDisposeDeviceStatus(v, + ObjectStatus.DELETED); + + dev.setId(v); + dev.setStatus(ret.getSecondParam()); + + rspList.add(new MulReturnType<>(ret.getFirstParam(), dev)); + } return rspList; } diff --git a/src/main/resources/mappers/DisposeDevice.xml b/src/main/resources/mappers/DisposeDevice.xml index 857a658a..da8512aa 100644 --- a/src/main/resources/mappers/DisposeDevice.xml +++ b/src/main/resources/mappers/DisposeDevice.xml @@ -59,12 +59,12 @@ VALUES (#{ipAddr}, #{ipPort}, #{deviceType}, #{areaCode}, #{deviceName}, #{manufacturer}, #{model}, #{version}, #{userName}, #{password}, #{urlPath}, #{urlType}, #{readme}, - ${@com.dispose.common.ObjectStatus@NORMAL.getCode()}) + ${@com.dispose.common.ObjectStatus@NORMAL.getValue()}) UPDATE dispose_device - SET status = ${@com.dispose.common.ObjectStatus@DELETED.getCode()} + SET status = ${@com.dispose.common.ObjectStatus@DELETED.getValue()} WHERE id = #{id} @@ -90,14 +90,14 @@ UPDATE dispose_device - SET status = ${@com.dispose.common.ObjectStatus@DISABLED.getCode()} + SET status = ${@com.dispose.common.ObjectStatus@DISABLED.getValue()} WHERE id = #{id} UPDATE dispose_device - SET status = ${@com.dispose.common.ObjectStatus@NORMAL.getCode()} + SET status = ${@com.dispose.common.ObjectStatus@NORMAL.getValue()} WHERE id = #{id} \ No newline at end of file diff --git a/src/test/java/com/dispose/test/mapper/DisposeDeviceMapperTest.java b/src/test/java/com/dispose/test/mapper/DisposeDeviceMapperTest.java index f1495ae7..7304697d 100644 --- a/src/test/java/com/dispose/test/mapper/DisposeDeviceMapperTest.java +++ b/src/test/java/com/dispose/test/mapper/DisposeDeviceMapperTest.java @@ -20,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; +import java.util.Optional; /** * The type Dispose device mapper test. @@ -106,43 +107,47 @@ public class DisposeDeviceMapperTest { */ @Test public void b1_addNewDisposeDevice() throws JsonProcessingException { - DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress("10.88.77.15", ""); - - String beforeVer = dev.getVersion(); - if (dev.getVersion().equals("B5.2.27.7")) { - dev.setVersion("5.7.13"); - } else { - dev.setVersion("B5.2.27.7"); - } - - DisposeDevice newDev = DisposeDevice.builder() - .id(dev.getId()) - .ipAddr("10.88.77.15") - .ipPort("") - .deviceType(DisposeDeviceType.DPTECH_UMC) - .areaCode(0) - .deviceName("中移杭研实验室迪普清洗设备") - .manufacturer("DPTech") - .model("UMC") - .version(dev.getVersion()) - .userName("admin") - .password("UMCAdministrator") - .urlPath("UMC/service/AbnormalFlowCleaningService") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); - - Assert.assertEquals(1, disposeDeviceMapper.upgradeDisposeDevice(newDev)); - Assert.assertNotNull(dev.getId()); - Assert.assertNotEquals(disposeDeviceMapper.getDeviceById(newDev.getId()).getVersion(), beforeVer); - try { - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - List devList = disposeDeviceMapper.selectAll(); + + devList.forEach(v -> { + DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), v.getIpPort()); + + String beforeVer = dev.getVersion(); + if (dev.getVersion().equals("B5.2.27.7")) { + dev.setVersion("5.7.13"); + } else { + dev.setVersion("B5.2.27.7"); + } + + DisposeDevice newDev = DisposeDevice.builder() + .id(dev.getId()) + .ipAddr("10.88.77.15") + .ipPort("") + .deviceType(DisposeDeviceType.DPTECH_UMC) + .areaCode(0) + .deviceName("中移杭研实验室迪普清洗设备") + .manufacturer("DPTech") + .model("UMC") + .version(dev.getVersion()) + .userName("admin") + .password("UMCAdministrator") + .urlPath("UMC/service/AbnormalFlowCleaningService") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); + + Assert.assertEquals(1, disposeDeviceMapper.upgradeDisposeDevice(newDev)); + Assert.assertNotNull(dev.getId()); + Assert.assertNotEquals(disposeDeviceMapper.getDeviceById(newDev.getId()).getVersion(), beforeVer); + try { + log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + }); + + devList = disposeDeviceMapper.selectAll(); log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(devList)); } @@ -152,18 +157,18 @@ public class DisposeDeviceMapperTest { * @throws JsonProcessingException the json processing exception */ @Test - public void d1_getDeviceByAddress() throws JsonProcessingException { - DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress("10.88.77.15", ""); - Assert.assertNotNull(dev); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev)); + public void d1_getDeviceByAddress() { + List devList = disposeDeviceMapper.selectAll(); - dev = disposeDeviceMapper.getDeviceByAddress("10.88.77.88", "18080"); - Assert.assertNotNull(dev); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev)); + devList.forEach(v -> { + DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), v.getIpPort()); + Assert.assertNotNull(dev); - dev = disposeDeviceMapper.getDeviceByAddress("10.88.77.15", "80"); - Assert.assertNull(dev); - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(null)); + dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), + String.valueOf(Long.parseLong(Optional.ofNullable(v.getIpPort()).orElse("123")) + 1)); + + Assert.assertNull(dev); + }); } /**