【功能新增】IoT:增加插件管理功能,包含插件实例和类型的定义及相关配置
This commit is contained in:
parent
39ba4e72da
commit
555310de66
|
@ -66,6 +66,7 @@
|
|||
<ip2region.version>2.7.0</ip2region.version>
|
||||
<bizlog-sdk.version>3.0.6</bizlog-sdk.version>
|
||||
<mqtt.version>1.2.5</mqtt.version>
|
||||
<pf4j-spring.version>0.9.0</pf4j-spring.version>
|
||||
<!-- 三方云服务相关 -->
|
||||
<okio.version>3.5.0</okio.version>
|
||||
<okhttp3.version>4.11.0</okhttp3.version>
|
||||
|
@ -605,6 +606,13 @@
|
|||
<version>${mqtt.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- PF4J -->
|
||||
<dependency>
|
||||
<groupId>org.pf4j</groupId>
|
||||
<artifactId>pf4j-spring</artifactId>
|
||||
<version>${pf4j-spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
<modules>
|
||||
<module>yudao-module-iot-api</module>
|
||||
<module>yudao-module-iot-biz</module>
|
||||
<module>yudao-module-iot-plugin</module>
|
||||
</modules>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -36,4 +36,15 @@ public interface ErrorCodeConstants {
|
|||
// ========== 设备分组 1-050-005-000 ==========
|
||||
ErrorCode DEVICE_GROUP_NOT_EXISTS = new ErrorCode(1_050_005_000, "设备分组不存在");
|
||||
ErrorCode DEVICE_GROUP_DELETE_FAIL_DEVICE_EXISTS = new ErrorCode(1_050_005_001, "设备分组下存在设备,不允许删除");
|
||||
}
|
||||
|
||||
// ========== 插件信息 1-050-006-000 ==========
|
||||
ErrorCode PLUGIN_INFO_NOT_EXISTS = new ErrorCode(1_050_006_000, "插件信息不存在");
|
||||
ErrorCode PLUGIN_INSTALL_FAILED = new ErrorCode(1_050_006_001, "插件安装失败");
|
||||
ErrorCode PLUGIN_INSTALL_FAILED_FILE_NAME_NOT_MATCH = new ErrorCode(1_050_006_002, "插件安装失败,文件名与原插件id不匹配");
|
||||
ErrorCode PLUGIN_INFO_DELETE_FAILED_RUNNING = new ErrorCode(1_050_006_003, "请先停止插件");
|
||||
ErrorCode PLUGIN_STATUS_INVALID = new ErrorCode(1_050_006_004, "插件状态无效");
|
||||
|
||||
// ========== 插件实例 1-050-007-000 ==========
|
||||
ErrorCode PLUGIN_INSTANCE_NOT_EXISTS = new ErrorCode(1_050_007_000, "插件实例不存在");
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package cn.iocoder.yudao.module.iot.enums.plugin;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* IoT 部署方式枚举
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Getter
|
||||
public enum IotPluginDeployTypeEnum implements IntArrayValuable {
|
||||
|
||||
UPLOAD(0, "上传jar"),
|
||||
ALONE(1, "独立运行");
|
||||
|
||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotPluginDeployTypeEnum::getDeployType).toArray();
|
||||
|
||||
/**
|
||||
* 部署方式
|
||||
*/
|
||||
private final Integer deployType;
|
||||
|
||||
/**
|
||||
* 部署方式名
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
IotPluginDeployTypeEnum(Integer deployType, String name) {
|
||||
this.deployType = deployType;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static IotPluginDeployTypeEnum fromDeployType(Integer deployType) {
|
||||
for (IotPluginDeployTypeEnum value : values()) {
|
||||
if (value.getDeployType().equals(deployType)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isValidDeployType(Integer deployType) {
|
||||
return fromDeployType(deployType) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package cn.iocoder.yudao.module.iot.enums.plugin;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* IoT 插件状态枚举
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Getter
|
||||
public enum IotPluginStatusEnum implements IntArrayValuable {
|
||||
|
||||
STOPPED(0, "停止"),
|
||||
RUNNING(1, "运行");
|
||||
|
||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotPluginStatusEnum::getStatus).toArray();
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private final Integer status;
|
||||
|
||||
/**
|
||||
* 状态名
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
IotPluginStatusEnum(Integer status, String name) {
|
||||
this.status = status;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static IotPluginStatusEnum fromState(Integer state) {
|
||||
for (IotPluginStatusEnum value : values()) {
|
||||
if (value.getStatus().equals(state)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isValidState(Integer state) {
|
||||
return fromState(state) != null;
|
||||
}
|
||||
|
||||
public static boolean contains(Integer status) {
|
||||
return Arrays.stream(values()).anyMatch(e -> e.getStatus().equals(status));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return new int[0];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package cn.iocoder.yudao.module.iot.enums.plugin;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* IoT 插件类型枚举
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Getter
|
||||
public enum IotPluginTypeEnum implements IntArrayValuable {
|
||||
|
||||
NORMAL(0, "普通插件"),
|
||||
DEVICE(1, "设备插件");
|
||||
|
||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotPluginTypeEnum::getType).toArray();
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final Integer type;
|
||||
|
||||
/**
|
||||
* 类型名
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
IotPluginTypeEnum(Integer type, String name) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static IotPluginTypeEnum fromType(Integer type) {
|
||||
for (IotPluginTypeEnum value : values()) {
|
||||
if (value.getType().equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isValidType(Integer type) {
|
||||
return fromType(type) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
}
|
|
@ -69,6 +69,12 @@
|
|||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- PF4J -->
|
||||
<dependency>
|
||||
<groupId>org.pf4j</groupId>
|
||||
<artifactId>pf4j-spring</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininfo;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoPageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoRespVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.service.plugininfo.PluginInfoService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;
|
||||
|
||||
@Tag(name = "管理后台 - IoT 插件信息")
|
||||
@RestController
|
||||
@RequestMapping("/iot/plugin-info")
|
||||
@Validated
|
||||
public class PluginInfoController {
|
||||
|
||||
@Resource
|
||||
private PluginInfoService pluginInfoService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建插件信息")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-info:create')")
|
||||
public CommonResult<Long> createPluginInfo(@Valid @RequestBody PluginInfoSaveReqVO createReqVO) {
|
||||
return success(pluginInfoService.createPluginInfo(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新插件信息")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-info:update')")
|
||||
public CommonResult<Boolean> updatePluginInfo(@Valid @RequestBody PluginInfoSaveReqVO updateReqVO) {
|
||||
pluginInfoService.updatePluginInfo(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除插件信息")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-info:delete')")
|
||||
public CommonResult<Boolean> deletePluginInfo(@RequestParam("id") Long id) {
|
||||
pluginInfoService.deletePluginInfo(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得插件信息")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-info:query')")
|
||||
public CommonResult<PluginInfoRespVO> getPluginInfo(@RequestParam("id") Long id) {
|
||||
PluginInfoDO pluginInfo = pluginInfoService.getPluginInfo(id);
|
||||
return success(BeanUtils.toBean(pluginInfo, PluginInfoRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得插件信息分页")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-info:query')")
|
||||
public CommonResult<PageResult<PluginInfoRespVO>> getPluginInfoPage(@Valid PluginInfoPageReqVO pageReqVO) {
|
||||
PageResult<PluginInfoDO> pageResult = pluginInfoService.getPluginInfoPage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, PluginInfoRespVO.class));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/update-jar",
|
||||
method = {RequestMethod.POST, RequestMethod.PUT}) // 解决 uni-app 不支持 Put 上传文件的问题
|
||||
@Operation(summary = "上传Jar包")
|
||||
public CommonResult<Boolean> uploadJar(
|
||||
@RequestParam("id") Long id,
|
||||
@RequestParam("jar") MultipartFile file) throws Exception {
|
||||
if (file.isEmpty()) {
|
||||
throw exception(FILE_IS_EMPTY);
|
||||
}
|
||||
pluginInfoService.uploadJar(id, file);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
// 修改插件状态
|
||||
@PutMapping("/update-status")
|
||||
@Operation(summary = "修改插件状态")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-info:update')")
|
||||
public CommonResult<Boolean> updateUserStatus(@Valid @RequestBody PluginInfoSaveReqVO reqVO) {
|
||||
pluginInfoService.updatePluginStatus(reqVO.getId(), reqVO.getStatus());
|
||||
return success(true);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo;
|
||||
|
||||
import lombok.*;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 插件信息分页 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class PluginInfoPageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "插件包id", example = "24627")
|
||||
private String pluginId;
|
||||
|
||||
@Schema(description = "插件名称", example = "赵六")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "描述", example = "你猜")
|
||||
private String description;
|
||||
|
||||
@Schema(description = "部署方式", example = "2")
|
||||
private Integer deployType;
|
||||
|
||||
@Schema(description = "插件包文件名")
|
||||
private String file;
|
||||
|
||||
@Schema(description = "插件版本")
|
||||
private String version;
|
||||
|
||||
@Schema(description = "插件类型", example = "2")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "设备插件协议类型")
|
||||
private String protocol;
|
||||
|
||||
@Schema(description = "状态")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "插件配置项描述信息")
|
||||
private String configSchema;
|
||||
|
||||
@Schema(description = "插件配置信息")
|
||||
private String config;
|
||||
|
||||
@Schema(description = "插件脚本")
|
||||
private String script;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
private LocalDateTime[] createTime;
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import com.alibaba.excel.annotation.*;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 插件信息 Response VO")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class PluginInfoRespVO {
|
||||
|
||||
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "11546")
|
||||
@ExcelProperty("主键ID")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "插件包id", requiredMode = Schema.RequiredMode.REQUIRED, example = "24627")
|
||||
@ExcelProperty("插件包id")
|
||||
private String pluginId;
|
||||
|
||||
@Schema(description = "插件名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "赵六")
|
||||
@ExcelProperty("插件名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "描述", example = "你猜")
|
||||
@ExcelProperty("描述")
|
||||
private String description;
|
||||
|
||||
@Schema(description = "部署方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
|
||||
@ExcelProperty("部署方式")
|
||||
private Integer deployType;
|
||||
|
||||
@Schema(description = "插件包文件名", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("插件包文件名")
|
||||
private String file;
|
||||
|
||||
@Schema(description = "插件版本", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("插件版本")
|
||||
private String version;
|
||||
|
||||
@Schema(description = "插件类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
|
||||
@ExcelProperty("插件类型")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "设备插件协议类型")
|
||||
@ExcelProperty("设备插件协议类型")
|
||||
private String protocol;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("状态")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "插件配置项描述信息")
|
||||
@ExcelProperty("插件配置项描述信息")
|
||||
private String configSchema;
|
||||
|
||||
@Schema(description = "插件配置信息")
|
||||
@ExcelProperty("插件配置信息")
|
||||
private String config;
|
||||
|
||||
@Schema(description = "插件脚本")
|
||||
@ExcelProperty("插件脚本")
|
||||
private String script;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 插件信息新增/修改 Request VO")
|
||||
@Data
|
||||
public class PluginInfoSaveReqVO {
|
||||
|
||||
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "11546")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "插件包id", requiredMode = Schema.RequiredMode.REQUIRED, example = "24627")
|
||||
private String pluginId;
|
||||
|
||||
@Schema(description = "插件名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "赵六")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "描述", example = "你猜")
|
||||
private String description;
|
||||
|
||||
@Schema(description = "部署方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
|
||||
private Integer deployType;
|
||||
|
||||
@Schema(description = "插件包文件名", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String file;
|
||||
|
||||
@Schema(description = "插件版本", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String version;
|
||||
|
||||
@Schema(description = "插件类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "设备插件协议类型")
|
||||
private String protocol;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "插件配置项描述信息")
|
||||
private String configSchema;
|
||||
|
||||
@Schema(description = "插件配置信息")
|
||||
private String config;
|
||||
|
||||
@Schema(description = "插件脚本")
|
||||
private String script;
|
||||
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininstance;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
|
||||
import jakarta.validation.*;
|
||||
import jakarta.servlet.http.*;
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
||||
|
||||
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
||||
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.*;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininstance.PluginInstanceDO;
|
||||
import cn.iocoder.yudao.module.iot.service.plugininstance.PluginInstanceService;
|
||||
|
||||
@Tag(name = "管理后台 - IoT 插件实例")
|
||||
@RestController
|
||||
@RequestMapping("/iot/plugin-instance")
|
||||
@Validated
|
||||
public class PluginInstanceController {
|
||||
|
||||
@Resource
|
||||
private PluginInstanceService pluginInstanceService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建IoT 插件实例")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-instance:create')")
|
||||
public CommonResult<Long> createPluginInstance(@Valid @RequestBody PluginInstanceSaveReqVO createReqVO) {
|
||||
return success(pluginInstanceService.createPluginInstance(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新IoT 插件实例")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-instance:update')")
|
||||
public CommonResult<Boolean> updatePluginInstance(@Valid @RequestBody PluginInstanceSaveReqVO updateReqVO) {
|
||||
pluginInstanceService.updatePluginInstance(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除IoT 插件实例")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-instance:delete')")
|
||||
public CommonResult<Boolean> deletePluginInstance(@RequestParam("id") Long id) {
|
||||
pluginInstanceService.deletePluginInstance(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得IoT 插件实例")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-instance:query')")
|
||||
public CommonResult<PluginInstanceRespVO> getPluginInstance(@RequestParam("id") Long id) {
|
||||
PluginInstanceDO pluginInstance = pluginInstanceService.getPluginInstance(id);
|
||||
return success(BeanUtils.toBean(pluginInstance, PluginInstanceRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得IoT 插件实例分页")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-instance:query')")
|
||||
public CommonResult<PageResult<PluginInstanceRespVO>> getPluginInstancePage(@Valid PluginInstancePageReqVO pageReqVO) {
|
||||
PageResult<PluginInstanceDO> pageResult = pluginInstanceService.getPluginInstancePage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, PluginInstanceRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/export-excel")
|
||||
@Operation(summary = "导出IoT 插件实例 Excel")
|
||||
@PreAuthorize("@ss.hasPermission('iot:plugin-instance:export')")
|
||||
@ApiAccessLog(operateType = EXPORT)
|
||||
public void exportPluginInstanceExcel(@Valid PluginInstancePageReqVO pageReqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
||||
List<PluginInstanceDO> list = pluginInstanceService.getPluginInstancePage(pageReqVO).getList();
|
||||
// 导出 Excel
|
||||
ExcelUtils.write(response, "IoT 插件实例.xls", "数据", PluginInstanceRespVO.class,
|
||||
BeanUtils.toBean(list, PluginInstanceRespVO.class));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo;
|
||||
|
||||
import lombok.*;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 插件实例分页 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class PluginInstancePageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "插件主程序id", example = "23738")
|
||||
private String mainId;
|
||||
|
||||
@Schema(description = "插件id", example = "26498")
|
||||
private Long pluginId;
|
||||
|
||||
@Schema(description = "插件主程序所在ip")
|
||||
private String ip;
|
||||
|
||||
@Schema(description = "插件主程序端口")
|
||||
private Integer port;
|
||||
|
||||
@Schema(description = "心跳时间,心路时间超过30秒需要剔除")
|
||||
private Long heartbeatAt;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
private LocalDateTime[] createTime;
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import com.alibaba.excel.annotation.*;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 插件实例 Response VO")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class PluginInstanceRespVO {
|
||||
|
||||
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "23864")
|
||||
@ExcelProperty("主键ID")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "插件主程序id", requiredMode = Schema.RequiredMode.REQUIRED, example = "23738")
|
||||
@ExcelProperty("插件主程序id")
|
||||
private String mainId;
|
||||
|
||||
@Schema(description = "插件id", requiredMode = Schema.RequiredMode.REQUIRED, example = "26498")
|
||||
@ExcelProperty("插件id")
|
||||
private Long pluginId;
|
||||
|
||||
@Schema(description = "插件主程序所在ip", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("插件主程序所在ip")
|
||||
private String ip;
|
||||
|
||||
@Schema(description = "插件主程序端口", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("插件主程序端口")
|
||||
private Integer port;
|
||||
|
||||
@Schema(description = "心跳时间,心路时间超过30秒需要剔除", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("心跳时间,心路时间超过30秒需要剔除")
|
||||
private Long heartbeatAt;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import jakarta.validation.constraints.*;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 插件实例新增/修改 Request VO")
|
||||
@Data
|
||||
public class PluginInstanceSaveReqVO {
|
||||
|
||||
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "23864")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "插件主程序id", requiredMode = Schema.RequiredMode.REQUIRED, example = "23738")
|
||||
@NotEmpty(message = "插件主程序id不能为空")
|
||||
private String mainId;
|
||||
|
||||
@Schema(description = "插件id", requiredMode = Schema.RequiredMode.REQUIRED, example = "26498")
|
||||
@NotNull(message = "插件id不能为空")
|
||||
private Long pluginId;
|
||||
|
||||
@Schema(description = "插件主程序所在ip", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "插件主程序所在ip不能为空")
|
||||
private String ip;
|
||||
|
||||
@Schema(description = "插件主程序端口", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "插件主程序端口不能为空")
|
||||
private Integer port;
|
||||
|
||||
@Schema(description = "心跳时间,心路时间超过30秒需要剔除", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "心跳时间,心路时间超过30秒需要剔除不能为空")
|
||||
private Long heartbeatAt;
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* IoT 插件信息 DO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@TableName("iot_plugin_info")
|
||||
@KeySequence("iot_plugin_info_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PluginInfoDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 插件包id
|
||||
*/
|
||||
private String pluginId;
|
||||
/**
|
||||
* 插件名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* 部署方式
|
||||
*/
|
||||
private Integer deployType;
|
||||
/**
|
||||
* 插件包文件名
|
||||
*/
|
||||
private String file;
|
||||
/**
|
||||
* 插件版本
|
||||
*/
|
||||
private String version;
|
||||
/**
|
||||
* 插件类型
|
||||
*/
|
||||
private Integer type;
|
||||
/**
|
||||
* 设备插件协议类型
|
||||
*/
|
||||
private String protocol;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 插件配置项描述信息
|
||||
*/
|
||||
private String configSchema;
|
||||
/**
|
||||
* 插件配置信息
|
||||
*/
|
||||
private String config;
|
||||
/**
|
||||
* 插件脚本
|
||||
*/
|
||||
private String script;
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package cn.iocoder.yudao.module.iot.dal.dataobject.plugininstance;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalDateTime;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
|
||||
/**
|
||||
* IoT 插件实例 DO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@TableName("iot_plugin_instance")
|
||||
@KeySequence("iot_plugin_instance_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PluginInstanceDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 插件主程序id
|
||||
*/
|
||||
private String mainId;
|
||||
/**
|
||||
* 插件id
|
||||
*/
|
||||
private Long pluginId;
|
||||
/**
|
||||
* 插件主程序所在ip
|
||||
*/
|
||||
private String ip;
|
||||
/**
|
||||
* 插件主程序端口
|
||||
*/
|
||||
private Integer port;
|
||||
/**
|
||||
* 心跳时间,心路时间超过30秒需要剔除
|
||||
*/
|
||||
private Long heartbeatAt;
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package cn.iocoder.yudao.module.iot.dal.mysql.plugininfo;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.*;
|
||||
|
||||
/**
|
||||
* IoT 插件信息 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface PluginInfoMapper extends BaseMapperX<PluginInfoDO> {
|
||||
|
||||
default PageResult<PluginInfoDO> selectPage(PluginInfoPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<PluginInfoDO>()
|
||||
.eqIfPresent(PluginInfoDO::getPluginId, reqVO.getPluginId())
|
||||
.likeIfPresent(PluginInfoDO::getName, reqVO.getName())
|
||||
.eqIfPresent(PluginInfoDO::getDescription, reqVO.getDescription())
|
||||
.eqIfPresent(PluginInfoDO::getDeployType, reqVO.getDeployType())
|
||||
.eqIfPresent(PluginInfoDO::getFile, reqVO.getFile())
|
||||
.eqIfPresent(PluginInfoDO::getVersion, reqVO.getVersion())
|
||||
.eqIfPresent(PluginInfoDO::getType, reqVO.getType())
|
||||
.eqIfPresent(PluginInfoDO::getProtocol, reqVO.getProtocol())
|
||||
.eqIfPresent(PluginInfoDO::getStatus, reqVO.getStatus())
|
||||
.eqIfPresent(PluginInfoDO::getConfigSchema, reqVO.getConfigSchema())
|
||||
.eqIfPresent(PluginInfoDO::getConfig, reqVO.getConfig())
|
||||
.eqIfPresent(PluginInfoDO::getScript, reqVO.getScript())
|
||||
.betweenIfPresent(PluginInfoDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(PluginInfoDO::getId));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package cn.iocoder.yudao.module.iot.dal.mysql.plugininstance;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininstance.PluginInstanceDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.*;
|
||||
|
||||
/**
|
||||
* IoT 插件实例 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface PluginInstanceMapper extends BaseMapperX<PluginInstanceDO> {
|
||||
|
||||
default PageResult<PluginInstanceDO> selectPage(PluginInstancePageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<PluginInstanceDO>()
|
||||
.eqIfPresent(PluginInstanceDO::getMainId, reqVO.getMainId())
|
||||
.eqIfPresent(PluginInstanceDO::getPluginId, reqVO.getPluginId())
|
||||
.eqIfPresent(PluginInstanceDO::getIp, reqVO.getIp())
|
||||
.eqIfPresent(PluginInstanceDO::getPort, reqVO.getPort())
|
||||
.eqIfPresent(PluginInstanceDO::getHeartbeatAt, reqVO.getHeartbeatAt())
|
||||
.betweenIfPresent(PluginInstanceDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(PluginInstanceDO::getId));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package cn.iocoder.yudao.module.iot.framework.plugin;
|
||||
|
||||
import org.pf4j.spring.SpringPluginManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SpringConfiguration {
|
||||
|
||||
@Bean
|
||||
public SpringPluginManager pluginManager() {
|
||||
return new SpringPluginManager();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package cn.iocoder.yudao.module.iot.service.plugininfo;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoPageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* IoT 插件信息 Service 接口
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public interface PluginInfoService {
|
||||
|
||||
/**
|
||||
* 创建IoT 插件信息
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createPluginInfo(@Valid PluginInfoSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新IoT 插件信息
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updatePluginInfo(@Valid PluginInfoSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除IoT 插件信息
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deletePluginInfo(Long id);
|
||||
|
||||
/**
|
||||
* 获得IoT 插件信息
|
||||
*
|
||||
* @param id 编号
|
||||
* @return IoT 插件信息
|
||||
*/
|
||||
PluginInfoDO getPluginInfo(Long id);
|
||||
|
||||
/**
|
||||
* 获得IoT 插件信息分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return IoT 插件信息分页
|
||||
*/
|
||||
PageResult<PluginInfoDO> getPluginInfoPage(PluginInfoPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 上传插件的 JAR 包
|
||||
*
|
||||
* @param id 插件id
|
||||
* @param file 文件
|
||||
*/
|
||||
void uploadJar(Long id, MultipartFile file);
|
||||
|
||||
/**
|
||||
* 更新插件的状态
|
||||
*
|
||||
* @param id 插件id
|
||||
* @param status 状态
|
||||
*/
|
||||
void updatePluginStatus(Long id, Integer status);
|
||||
}
|
|
@ -0,0 +1,267 @@
|
|||
package cn.iocoder.yudao.module.iot.service.plugininfo;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.infra.api.file.FileApi;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoPageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.PluginInfoSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.plugininfo.PluginInfoMapper;
|
||||
import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.PluginDescriptor;
|
||||
import org.pf4j.PluginState;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.pf4j.spring.SpringPluginManager;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* IoT 插件信息 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class PluginInfoServiceImpl implements PluginInfoService {
|
||||
|
||||
@Resource
|
||||
private PluginInfoMapper pluginInfoMapper;
|
||||
|
||||
@Resource
|
||||
private SpringPluginManager pluginManager;
|
||||
|
||||
@Resource
|
||||
private FileApi fileApi;
|
||||
|
||||
@Override
|
||||
public Long createPluginInfo(PluginInfoSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
PluginInfoDO pluginInfo = BeanUtils.toBean(createReqVO, PluginInfoDO.class);
|
||||
pluginInfoMapper.insert(pluginInfo);
|
||||
// 返回
|
||||
return pluginInfo.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePluginInfo(PluginInfoSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
validatePluginInfoExists(updateReqVO.getId());
|
||||
// 更新
|
||||
PluginInfoDO updateObj = BeanUtils.toBean(updateReqVO, PluginInfoDO.class);
|
||||
pluginInfoMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePluginInfo(Long id) {
|
||||
// 校验存在
|
||||
PluginInfoDO pluginInfoDO = validatePluginInfoExists(id);
|
||||
|
||||
// 停止插件
|
||||
if (IotPluginStatusEnum.RUNNING.getStatus().equals(pluginInfoDO.getStatus())) {
|
||||
throw exception(PLUGIN_INFO_DELETE_FAILED_RUNNING);
|
||||
}
|
||||
|
||||
// 卸载插件
|
||||
PluginWrapper plugin = pluginManager.getPlugin(pluginInfoDO.getPluginId());
|
||||
if (plugin != null) {
|
||||
// 查询插件是否是启动状态
|
||||
if (plugin.getPluginState().equals(PluginState.STARTED)) {
|
||||
// 停止插件
|
||||
pluginManager.stopPlugin(plugin.getPluginId());
|
||||
}
|
||||
// 卸载插件
|
||||
pluginManager.unloadPlugin(plugin.getPluginId());
|
||||
}
|
||||
|
||||
// 删除
|
||||
pluginInfoMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private PluginInfoDO validatePluginInfoExists(Long id) {
|
||||
PluginInfoDO pluginInfo = pluginInfoMapper.selectById(id);
|
||||
if (pluginInfo == null) {
|
||||
throw exception(PLUGIN_INFO_NOT_EXISTS);
|
||||
}
|
||||
return pluginInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginInfoDO getPluginInfo(Long id) {
|
||||
return pluginInfoMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<PluginInfoDO> getPluginInfoPage(PluginInfoPageReqVO pageReqVO) {
|
||||
return pluginInfoMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadJar(Long id, MultipartFile file) {
|
||||
// 1. 校验存在
|
||||
PluginInfoDO pluginInfoDo = validatePluginInfoExists(id);
|
||||
|
||||
// 2. 判断文件名称与插件 ID 是否匹配
|
||||
String pluginId = pluginInfoDo.getPluginId();
|
||||
|
||||
// 3. 停止卸载旧的插件
|
||||
// 3.1. 获取插件信息
|
||||
PluginWrapper plugin = pluginManager.getPlugin(pluginId);
|
||||
if (plugin != null) {
|
||||
// 3.2. 如果插件状态是启动的,停止插件
|
||||
if (plugin.getPluginState().equals(PluginState.STARTED)) {
|
||||
pluginManager.stopPlugin(pluginId);
|
||||
}
|
||||
// 3.3. 卸载插件
|
||||
pluginManager.unloadPlugin(pluginId);
|
||||
}
|
||||
|
||||
// 4. 上传插件
|
||||
String pluginIdNew;
|
||||
try {
|
||||
String path = fileApi.createFile(IoUtil.readBytes(file.getInputStream()));
|
||||
Path pluginPath = Path.of(path);
|
||||
pluginIdNew = pluginManager.loadPlugin(pluginPath);
|
||||
} catch (Exception e) {
|
||||
throw exception(PLUGIN_INSTALL_FAILED);
|
||||
}
|
||||
|
||||
PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginIdNew);
|
||||
if (pluginWrapper == null) {
|
||||
throw exception(PLUGIN_INSTALL_FAILED);
|
||||
}
|
||||
|
||||
|
||||
// 5. 读取配置文件和脚本
|
||||
String configJson = "";
|
||||
String script = "";
|
||||
|
||||
pluginInfoDo.setPluginId(pluginIdNew);
|
||||
pluginInfoDo.setStatus(IotPluginStatusEnum.STOPPED.getStatus());
|
||||
pluginInfoDo.setFile(file.getOriginalFilename());
|
||||
pluginInfoDo.setConfigSchema(configJson);
|
||||
pluginInfoDo.setScript(script);
|
||||
|
||||
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
|
||||
pluginInfoDo.setVersion(pluginDescriptor.getVersion());
|
||||
pluginInfoDo.setDescription(pluginDescriptor.getPluginDescription());
|
||||
pluginInfoMapper.updateById(pluginInfoDo);
|
||||
|
||||
|
||||
// 5. 读取配置文件和脚本
|
||||
// String configJson = "";
|
||||
// String script = "";
|
||||
// try (JarFile jarFile = new JarFile(pluginInfoUpdate.getPluginPath())) {
|
||||
// // 5.1 获取config文件在jar包中的路径
|
||||
// String configFile = "classes/config.json";
|
||||
// JarEntry configEntry = jarFile.getJarEntry(configFile);
|
||||
//
|
||||
// if (configEntry != null) {
|
||||
// // 5.2 读取配置文件
|
||||
// configJson = IoUtil.read(jarFile.getInputStream(configEntry), Charset.defaultCharset());
|
||||
// log.info("configJson:{}", configJson);
|
||||
// }
|
||||
//
|
||||
// // 5.3 读取script.js脚本
|
||||
// String scriptFile = "classes/script.js";
|
||||
// JarEntry scriptEntity = jarFile.getJarEntry(scriptFile);
|
||||
// if (scriptEntity != null) {
|
||||
// // 5.4 读取脚本文件
|
||||
// script = IoUtil.read(jarFile.getInputStream(scriptEntity), Charset.defaultCharset());
|
||||
// log.info("script:{}", script);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// throw exception(PLUGIN_INSTALL_FAILED);
|
||||
// }
|
||||
|
||||
|
||||
// PluginState pluginState = pluginInfoUpdate.getPluginState();
|
||||
// if (pluginState == PluginState.STARTED) {
|
||||
// pluginInfoDo.setStatus(IotPluginStatusEnum.RUNNING.getStatus());
|
||||
// }
|
||||
// pluginInfoDo.setPluginId(pluginInfoUpdate.getPluginId());
|
||||
// pluginInfoDo.setFile(file.getOriginalFilename());
|
||||
// pluginInfoDo.setConfigSchema(configJson);
|
||||
// pluginInfoDo.setScript(script);
|
||||
//
|
||||
// PluginDescriptor pluginDescriptor = pluginInfoUpdate.getPluginDescriptor();
|
||||
// pluginInfoDo.setVersion(pluginDescriptor.getPluginVersion());
|
||||
// pluginInfoDo.setDescription(pluginDescriptor.getDescription());
|
||||
// pluginInfoMapper.updateById(pluginInfoDo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePluginStatus(Long id, Integer status) {
|
||||
// 1. 校验存在
|
||||
PluginInfoDO pluginInfoDo = validatePluginInfoExists(id);
|
||||
|
||||
// 插件状态无效
|
||||
if (!IotPluginStatusEnum.contains(status)) {
|
||||
throw exception(PLUGIN_STATUS_INVALID);
|
||||
}
|
||||
|
||||
// 插件包为空
|
||||
// String pluginId = pluginInfoDo.getPluginId();
|
||||
// if (StrUtil.isBlank(pluginId)) {
|
||||
// throw exception(PLUGIN_INFO_NOT_EXISTS);
|
||||
// }
|
||||
// com.gitee.starblues.core.PluginInfo pluginInfo = pluginOperator.getPluginInfo(pluginId);
|
||||
// if (pluginInfo != null) {
|
||||
// if (pluginInfoDo.getStatus().equals(IotPluginStatusEnum.RUNNING.getStatus()) && pluginInfo.getPluginState() != PluginState.STARTED) {
|
||||
// // 启动插件
|
||||
// pluginOperator.start(pluginId);
|
||||
// } else if (pluginInfoDo.getStatus().equals(IotPluginStatusEnum.STOPPED.getStatus()) && pluginInfo.getPluginState() == PluginState.STARTED) {
|
||||
// // 停止插件
|
||||
// pluginOperator.stop(pluginId);
|
||||
// }
|
||||
// } else {
|
||||
// // 已经停止,未获取到插件
|
||||
// if (IotPluginStatusEnum.STOPPED.getStatus().equals(pluginInfoDo.getStatus())) {
|
||||
// throw exception(PLUGIN_STATUS_INVALID);
|
||||
// }
|
||||
// }
|
||||
pluginInfoDo.setStatus(status);
|
||||
pluginInfoMapper.updateById(pluginInfoDo);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Executors.newSingleThreadScheduledExecutor().schedule(this::startPlugins, 3, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private void startPlugins() {
|
||||
// while (!pluginOperator.inited()) {
|
||||
// Thread.sleep(1000L);
|
||||
// }
|
||||
|
||||
for (PluginInfoDO pluginInfoDO : pluginInfoMapper.selectList()) {
|
||||
if (!IotPluginStatusEnum.RUNNING.getStatus().equals(pluginInfoDO.getStatus())) {
|
||||
continue;
|
||||
}
|
||||
log.info("start plugin:{}", pluginInfoDO.getPluginId());
|
||||
try {
|
||||
// com.gitee.starblues.core.PluginInfo plugin = pluginOperator.getPluginInfo(pluginInfoDO.getPluginId());
|
||||
// if (plugin != null) {
|
||||
// pluginOperator.start(plugin.getPluginId());
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
log.error("start plugin error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package cn.iocoder.yudao.module.iot.service.plugininstance;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.PluginInstancePageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.PluginInstanceSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininstance.PluginInstanceDO;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
/**
|
||||
* IoT 插件实例 Service 接口
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public interface PluginInstanceService {
|
||||
|
||||
/**
|
||||
* 创建IoT 插件实例
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createPluginInstance(@Valid PluginInstanceSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新IoT 插件实例
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updatePluginInstance(@Valid PluginInstanceSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除IoT 插件实例
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deletePluginInstance(Long id);
|
||||
|
||||
/**
|
||||
* 获得IoT 插件实例
|
||||
*
|
||||
* @param id 编号
|
||||
* @return IoT 插件实例
|
||||
*/
|
||||
PluginInstanceDO getPluginInstance(Long id);
|
||||
|
||||
/**
|
||||
* 获得IoT 插件实例分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return IoT 插件实例分页
|
||||
*/
|
||||
PageResult<PluginInstanceDO> getPluginInstancePage(PluginInstancePageReqVO pageReqVO);
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package cn.iocoder.yudao.module.iot.service.plugininstance;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.PluginInstancePageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.PluginInstanceSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininstance.PluginInstanceDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.plugininstance.PluginInstanceMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PLUGIN_INSTANCE_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* IoT 插件实例 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class PluginInstanceServiceImpl implements PluginInstanceService {
|
||||
|
||||
@Resource
|
||||
private PluginInstanceMapper pluginInstanceMapper;
|
||||
|
||||
@Override
|
||||
public Long createPluginInstance(PluginInstanceSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
PluginInstanceDO pluginInstance = BeanUtils.toBean(createReqVO, PluginInstanceDO.class);
|
||||
pluginInstanceMapper.insert(pluginInstance);
|
||||
// 返回
|
||||
return pluginInstance.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePluginInstance(PluginInstanceSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
validatePluginInstanceExists(updateReqVO.getId());
|
||||
// 更新
|
||||
PluginInstanceDO updateObj = BeanUtils.toBean(updateReqVO, PluginInstanceDO.class);
|
||||
pluginInstanceMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePluginInstance(Long id) {
|
||||
// 校验存在
|
||||
validatePluginInstanceExists(id);
|
||||
// 删除
|
||||
pluginInstanceMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private void validatePluginInstanceExists(Long id) {
|
||||
if (pluginInstanceMapper.selectById(id) == null) {
|
||||
throw exception(PLUGIN_INSTANCE_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginInstanceDO getPluginInstance(Long id) {
|
||||
return pluginInstanceMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<PluginInstanceDO> getPluginInstancePage(PluginInstancePageReqVO pageReqVO) {
|
||||
return pluginInstanceMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.plugininfo.PluginInfoMapper">
|
||||
|
||||
<!--
|
||||
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
|
||||
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
|
||||
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
|
||||
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
|
||||
-->
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.plugininstance.PluginInstanceMapper">
|
||||
|
||||
<!--
|
||||
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
|
||||
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
|
||||
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
|
||||
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
|
||||
-->
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,171 @@
|
|||
package cn.iocoder.yudao.module.iot.service.plugininfo;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininfo.vo.*;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.plugininfo.PluginInfoMapper;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* {@link PluginInfoServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(PluginInfoServiceImpl.class)
|
||||
public class PluginInfoServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private PluginInfoServiceImpl pluginInfoService;
|
||||
|
||||
@Resource
|
||||
private PluginInfoMapper pluginInfoMapper;
|
||||
|
||||
@Test
|
||||
public void testCreatePluginInfo_success() {
|
||||
// 准备参数
|
||||
PluginInfoSaveReqVO createReqVO = randomPojo(PluginInfoSaveReqVO.class).setId(null);
|
||||
|
||||
// 调用
|
||||
Long pluginInfoId = pluginInfoService.createPluginInfo(createReqVO);
|
||||
// 断言
|
||||
assertNotNull(pluginInfoId);
|
||||
// 校验记录的属性是否正确
|
||||
PluginInfoDO pluginInfo = pluginInfoMapper.selectById(pluginInfoId);
|
||||
assertPojoEquals(createReqVO, pluginInfo, "id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePluginInfo_success() {
|
||||
// mock 数据
|
||||
PluginInfoDO dbPluginInfo = randomPojo(PluginInfoDO.class);
|
||||
pluginInfoMapper.insert(dbPluginInfo);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
PluginInfoSaveReqVO updateReqVO = randomPojo(PluginInfoSaveReqVO.class, o -> {
|
||||
o.setId(dbPluginInfo.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
pluginInfoService.updatePluginInfo(updateReqVO);
|
||||
// 校验是否更新正确
|
||||
PluginInfoDO pluginInfo = pluginInfoMapper.selectById(updateReqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(updateReqVO, pluginInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePluginInfo_notExists() {
|
||||
// 准备参数
|
||||
PluginInfoSaveReqVO updateReqVO = randomPojo(PluginInfoSaveReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> pluginInfoService.updatePluginInfo(updateReqVO), PLUGIN_INFO_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePluginInfo_success() {
|
||||
// mock 数据
|
||||
PluginInfoDO dbPluginInfo = randomPojo(PluginInfoDO.class);
|
||||
pluginInfoMapper.insert(dbPluginInfo);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbPluginInfo.getId();
|
||||
|
||||
// 调用
|
||||
pluginInfoService.deletePluginInfo(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(pluginInfoMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePluginInfo_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> pluginInfoService.deletePluginInfo(id), PLUGIN_INFO_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||
public void testGetPluginInfoPage() {
|
||||
// mock 数据
|
||||
PluginInfoDO dbPluginInfo = randomPojo(PluginInfoDO.class, o -> { // 等会查询到
|
||||
o.setPluginId(null);
|
||||
o.setName(null);
|
||||
o.setDescription(null);
|
||||
o.setDeployType(null);
|
||||
o.setFile(null);
|
||||
o.setVersion(null);
|
||||
o.setType(null);
|
||||
o.setProtocol(null);
|
||||
o.setStatus(null);
|
||||
o.setConfigSchema(null);
|
||||
o.setConfig(null);
|
||||
o.setScript(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
pluginInfoMapper.insert(dbPluginInfo);
|
||||
// 测试 pluginId 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setPluginId(null)));
|
||||
// 测试 name 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setName(null)));
|
||||
// 测试 description 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setDescription(null)));
|
||||
// 测试 deployType 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setDeployType(null)));
|
||||
// 测试 file 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setFile(null)));
|
||||
// 测试 version 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setVersion(null)));
|
||||
// 测试 type 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setType(null)));
|
||||
// 测试 protocol 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setProtocol(null)));
|
||||
// 测试 state 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setStatus(null)));
|
||||
// 测试 configSchema 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setConfigSchema(null)));
|
||||
// 测试 config 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setConfig(null)));
|
||||
// 测试 script 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setScript(null)));
|
||||
// 测试 createTime 不匹配
|
||||
pluginInfoMapper.insert(cloneIgnoreId(dbPluginInfo, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
PluginInfoPageReqVO reqVO = new PluginInfoPageReqVO();
|
||||
reqVO.setPluginId(null);
|
||||
reqVO.setName(null);
|
||||
reqVO.setDescription(null);
|
||||
reqVO.setDeployType(null);
|
||||
reqVO.setFile(null);
|
||||
reqVO.setVersion(null);
|
||||
reqVO.setType(null);
|
||||
reqVO.setProtocol(null);
|
||||
reqVO.setStatus(null);
|
||||
reqVO.setConfigSchema(null);
|
||||
reqVO.setConfig(null);
|
||||
reqVO.setScript(null);
|
||||
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
||||
|
||||
// 调用
|
||||
PageResult<PluginInfoDO> pageResult = pluginInfoService.getPluginInfoPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbPluginInfo, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
package cn.iocoder.yudao.module.iot.service.plugininstance;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.plugininstance.vo.*;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininstance.PluginInstanceDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.plugininstance.PluginInstanceMapper;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import java.util.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.*;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link PluginInstanceServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(PluginInstanceServiceImpl.class)
|
||||
public class PluginInstanceServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private PluginInstanceServiceImpl pluginInstanceService;
|
||||
|
||||
@Resource
|
||||
private PluginInstanceMapper pluginInstanceMapper;
|
||||
|
||||
@Test
|
||||
public void testCreatePluginInstance_success() {
|
||||
// 准备参数
|
||||
PluginInstanceSaveReqVO createReqVO = randomPojo(PluginInstanceSaveReqVO.class).setId(null);
|
||||
|
||||
// 调用
|
||||
Long pluginInstanceId = pluginInstanceService.createPluginInstance(createReqVO);
|
||||
// 断言
|
||||
assertNotNull(pluginInstanceId);
|
||||
// 校验记录的属性是否正确
|
||||
PluginInstanceDO pluginInstance = pluginInstanceMapper.selectById(pluginInstanceId);
|
||||
assertPojoEquals(createReqVO, pluginInstance, "id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePluginInstance_success() {
|
||||
// mock 数据
|
||||
PluginInstanceDO dbPluginInstance = randomPojo(PluginInstanceDO.class);
|
||||
pluginInstanceMapper.insert(dbPluginInstance);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
PluginInstanceSaveReqVO updateReqVO = randomPojo(PluginInstanceSaveReqVO.class, o -> {
|
||||
o.setId(dbPluginInstance.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
pluginInstanceService.updatePluginInstance(updateReqVO);
|
||||
// 校验是否更新正确
|
||||
PluginInstanceDO pluginInstance = pluginInstanceMapper.selectById(updateReqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(updateReqVO, pluginInstance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePluginInstance_notExists() {
|
||||
// 准备参数
|
||||
PluginInstanceSaveReqVO updateReqVO = randomPojo(PluginInstanceSaveReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> pluginInstanceService.updatePluginInstance(updateReqVO), PLUGIN_INSTANCE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePluginInstance_success() {
|
||||
// mock 数据
|
||||
PluginInstanceDO dbPluginInstance = randomPojo(PluginInstanceDO.class);
|
||||
pluginInstanceMapper.insert(dbPluginInstance);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbPluginInstance.getId();
|
||||
|
||||
// 调用
|
||||
pluginInstanceService.deletePluginInstance(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(pluginInstanceMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePluginInstance_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> pluginInstanceService.deletePluginInstance(id), PLUGIN_INSTANCE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||
public void testGetPluginInstancePage() {
|
||||
// mock 数据
|
||||
PluginInstanceDO dbPluginInstance = randomPojo(PluginInstanceDO.class, o -> { // 等会查询到
|
||||
o.setMainId(null);
|
||||
o.setPluginId(null);
|
||||
o.setIp(null);
|
||||
o.setPort(null);
|
||||
o.setHeartbeatAt(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
pluginInstanceMapper.insert(dbPluginInstance);
|
||||
// 测试 mainId 不匹配
|
||||
pluginInstanceMapper.insert(cloneIgnoreId(dbPluginInstance, o -> o.setMainId(null)));
|
||||
// 测试 pluginId 不匹配
|
||||
pluginInstanceMapper.insert(cloneIgnoreId(dbPluginInstance, o -> o.setPluginId(null)));
|
||||
// 测试 ip 不匹配
|
||||
pluginInstanceMapper.insert(cloneIgnoreId(dbPluginInstance, o -> o.setIp(null)));
|
||||
// 测试 port 不匹配
|
||||
pluginInstanceMapper.insert(cloneIgnoreId(dbPluginInstance, o -> o.setPort(null)));
|
||||
// 测试 heartbeatAt 不匹配
|
||||
pluginInstanceMapper.insert(cloneIgnoreId(dbPluginInstance, o -> o.setHeartbeatAt(null)));
|
||||
// 测试 createTime 不匹配
|
||||
pluginInstanceMapper.insert(cloneIgnoreId(dbPluginInstance, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
PluginInstancePageReqVO reqVO = new PluginInstancePageReqVO();
|
||||
reqVO.setMainId(null);
|
||||
reqVO.setPluginId(null);
|
||||
reqVO.setIp(null);
|
||||
reqVO.setPort(null);
|
||||
reqVO.setHeartbeatAt(null);
|
||||
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
||||
|
||||
// 调用
|
||||
PageResult<PluginInstanceDO> pageResult = pluginInstanceService.getPluginInstancePage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbPluginInstance, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>yudao-module-iot</artifactId>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>yudao-module-iot-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>
|
||||
物联网 模块 - 插件
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.pf4j</groupId>
|
||||
<artifactId>pf4j-spring</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* 占位
|
||||
*
|
||||
* TODO 芋艿:后续删除
|
||||
*/
|
||||
package cn.iocoder.yudao.module.iot.plugin;
|
Loading…
Reference in New Issue