From 7eee581dbcaf086ab1f6a891990d9b10c59b0f0e Mon Sep 17 00:00:00 2001 From: jay <75509151@qq.com> Date: Wed, 31 May 2023 08:56:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20=E8=A7=84=E5=88=99=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RuleEngineController.java | 261 ++++++----------- .../manager/dto/bo/ruleinfo/RuleInfoBo.java | 72 +++++ .../manager/dto/bo/ruleinfo/RuleLogBo.java | 40 +++ .../manager/dto/bo/taskinfo/TaskInfoBo.java | 70 +++++ .../manager/dto/bo/taskinfo/TaskLogBo.java | 34 +++ .../manager/dto/vo/ruleinfo/RuleInfoVo.java | 68 +++++ .../manager/dto/vo/ruleinfo/RuleLogVo.java | 38 +++ .../manager/dto/vo/taskinfo/TaskInfoVo.java | 68 +++++ .../manager/dto/vo/taskinfo/TaskLogVo.java | 32 +++ .../manager/service/IRuleEngineService.java | 50 ++++ .../service/impl/RuleEngineServiceImpl.java | 264 ++++++++++++++++++ 11 files changed, 824 insertions(+), 173 deletions(-) create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleLogBo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskInfoBo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskLogBo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleLogVo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskInfoVo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskLogVo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IRuleEngineService.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java index 2ff5b5e2..24fe31c2 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java @@ -9,14 +9,25 @@ */ package cc.iotkit.manager.controller; +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Request; import cc.iotkit.common.enums.ErrCode; import cc.iotkit.common.exception.BizException; import cc.iotkit.common.satoken.utils.AuthUtil; import cc.iotkit.common.utils.ReflectUtil; import cc.iotkit.data.manager.IRuleInfoData; import cc.iotkit.data.manager.ITaskInfoData; +import cc.iotkit.manager.dto.bo.ruleinfo.RuleInfoBo; +import cc.iotkit.manager.dto.bo.ruleinfo.RuleLogBo; +import cc.iotkit.manager.dto.bo.taskinfo.TaskInfoBo; +import cc.iotkit.manager.dto.bo.taskinfo.TaskLogBo; +import cc.iotkit.manager.dto.vo.ruleinfo.RuleInfoVo; +import cc.iotkit.manager.dto.vo.ruleinfo.RuleLogVo; +import cc.iotkit.manager.dto.vo.taskinfo.TaskInfoVo; +import cc.iotkit.manager.dto.vo.taskinfo.TaskLogVo; import cc.iotkit.manager.service.DataOwnerService; import cc.iotkit.common.api.Paging; +import cc.iotkit.manager.service.IRuleEngineService; import cc.iotkit.model.rule.RuleInfo; import cc.iotkit.model.rule.RuleLog; import cc.iotkit.model.rule.TaskInfo; @@ -26,10 +37,13 @@ import cc.iotkit.ruleengine.task.TaskManager; import cc.iotkit.temporal.IRuleLogData; import cc.iotkit.temporal.ITaskLogData; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.quartz.SchedulerException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -42,214 +56,115 @@ import java.util.UUID; public class RuleEngineController { @Autowired - private ITaskInfoData taskInfoData; + IRuleEngineService ruleEngineService; - @Autowired - private IRuleInfoData ruleInfoData; - - @Autowired - private IRuleLogData ruleLogData; - - @Autowired - private DataOwnerService dataOwnerService; - - @Autowired - private TaskManager taskManager; - - @Autowired - private RuleManager ruleManager; - - @Autowired - private ITaskLogData taskLogData; - - @PostMapping("/rules/{type}/{size}/{page}") - public Paging rules( - @PathVariable("type") String type, - @PathVariable("size") int size, - @PathVariable("page") int page + @ApiOperation("规则列表") + @PostMapping("/list") + public Paging rules( + @Validated @RequestBody + PageRequest request ) { - RuleInfo ruleInfo = new RuleInfo(); - ruleInfo.setType(type); - if (AuthUtil.isAdmin()) { - return ruleInfoData.findByType(type, page, size); - } else { - return ruleInfoData.findByUidAndType(AuthUtil.getUserId(), type, page, size); - } + return ruleEngineService.selectPageList(request); } - @PostMapping("/rule/save") - public void saveRule(@RequestBody RuleInfo rule) { - if (StringUtils.isBlank(rule.getId())) { - rule.setId(UUID.randomUUID().toString()); - rule.setState(RuleInfo.STATE_STOPPED); - rule.setCreateAt(System.currentTimeMillis()); - rule.setUid(AuthUtil.getUserId()); - ruleInfoData.save(rule); - ruleManager.add(rule); - } else { - RuleInfo ruleInfo = ruleInfoData.findById(rule.getId()); - if (ruleInfo == null) { - throw new BizException(ErrCode.RULE_NOT_FOUND); - } - if (RuleInfo.STATE_RUNNING.equals(ruleInfo.getState())) { - throw new BizException(ErrCode.RULE_ALREADY_RUNNING); - } + @ApiOperation("规则编辑") + @PostMapping("/edit") + public boolean saveRule(@RequestBody @Validated RuleInfoBo ruleInfoBo) { + return ruleEngineService.saveRule(ruleInfoBo); - dataOwnerService.checkOwner(ruleInfo); - - ruleInfo.setListeners(rule.getListeners()); - ruleInfo.setFilters(rule.getFilters()); - ruleInfo.setActions(rule.getActions()); - ruleInfo.setName(rule.getName()); - ruleInfo.setDesc(rule.getDesc()); - - ruleInfoData.save(ruleInfo); - } } - @PostMapping("/rule/{ruleId}/pause") - public void pauseRule(@PathVariable("ruleId") String ruleId) { - RuleInfo ruleInfo = ruleInfoData.findById(ruleId); - if (ruleInfo == null) { - throw new BizException(ErrCode.RULE_NOT_FOUND); - } - dataOwnerService.checkOwner(ruleInfo); - ruleInfo.setState(RuleInfo.STATE_STOPPED); - ruleInfoData.save(ruleInfo); - ruleManager.pause(ruleInfo.getId()); + @ApiOperation("暂停规则") + @PostMapping("/pause") + public boolean pauseRule(@Validated @RequestBody Request request) { + String ruleId = request.getData(); + return ruleEngineService.pauseRule(ruleId); } - @PostMapping("/rule/{ruleId}/resume") - public void resumeRule(@PathVariable("ruleId") String ruleId) { - RuleInfo ruleInfo = ruleInfoData.findById(ruleId); - if (ruleInfo == null) { - throw new BizException(ErrCode.RULE_NOT_FOUND); - } - dataOwnerService.checkOwner(ruleInfo); - ruleInfo.setState(RuleInfo.STATE_RUNNING); - ruleInfoData.save(ruleInfo); - ruleManager.resume(ruleInfo); + @ApiOperation("恢复规则") + @PostMapping("/resume") + public boolean resumeRule(@Validated @RequestBody Request request) { + String ruleId = request.getData(); + return ruleEngineService.resumeRule(ruleId); + } - @DeleteMapping("/rule/{ruleId}/delete") - public void deleteRule(@PathVariable("ruleId") String ruleId) { - RuleInfo ruleInfo = ruleInfoData.findById(ruleId); - if (ruleInfo == null) { - throw new BizException(ErrCode.RULE_NOT_FOUND); - } - dataOwnerService.checkOwner(ruleInfo); - ruleInfoData.deleteById(ruleInfo.getId()); - ruleManager.remove(ruleInfo.getId()); - ruleLogData.deleteByRuleId(ruleId); + @ApiOperation("删除规则") + @DeleteMapping("/delete") + public boolean deleteRule(@Validated @RequestBody Request request) { + String ruleId = request.getData(); + return ruleEngineService.deleteRule(ruleId); } - @PostMapping("/rule/{ruleId}/logs/{size}/{page}") - public Paging getRuleLogs( - @PathVariable("ruleId") String ruleId, - @PathVariable("size") int size, - @PathVariable("page") int page + @ApiOperation("规则日志") + @PostMapping("/ruleLog/list") + public Paging getRuleLogs( + @Validated @RequestBody PageRequest request ) { - RuleLog ruleLog = new RuleLog(); - ruleLog.setRuleId(ruleId); - return ruleLogData.findByRuleId(ruleId, page, size); + return ruleEngineService.selectRuleLogPageList(request); } - @DeleteMapping("/rule/{ruleId}/logs/clear") - public void clearRuleLogs(@PathVariable("ruleId") String ruleId) { - ruleLogData.deleteByRuleId(ruleId); + @ApiOperation("清理日志") + @DeleteMapping("/ruleLog//clear") + public boolean clearRuleLogs(@Validated @RequestBody Request request) { + String ruleId = request.getData(); + return ruleEngineService.clearRuleLogs(ruleId); + } - @PostMapping("/tasks") - public List tasks() { - if (AuthUtil.isAdmin()) { - return taskInfoData.findAll(); - } - return taskInfoData.findByUid(AuthUtil.getUserId()); + @ApiOperation("定时任务列表") + @PostMapping("/tasks/list") + public Paging tasks(@Validated @RequestBody PageRequest request) { + return ruleEngineService.selectTaskPageList(request); } - @PostMapping("/saveTask") - public void saveTask(@RequestBody TaskInfo taskInfo) { - if (StringUtils.isBlank(taskInfo.getId())) { - taskInfo.setId(UUID.randomUUID().toString()); - taskInfo.setUid(AuthUtil.getUserId()); - taskInfo.setCreateAt(System.currentTimeMillis()); - taskInfo.setState(TaskInfo.STATE_STOP); - } else { - TaskInfo oldTask = taskInfoData.findById(taskInfo.getId()); - if (oldTask == null) { - throw new BizException(ErrCode.TASK_NOT_FOUND); - } - taskInfo = ReflectUtil.copyNoNulls(taskInfo, oldTask); - dataOwnerService.checkOwner(taskInfo); - } - - taskInfoData.save(taskInfo); + @ApiOperation("定时任务编辑") + @PostMapping("/task/save") + public boolean saveTask(@Validated @RequestBody TaskInfoBo taskInfo) { + return ruleEngineService.saveTask(taskInfo); } - @PostMapping("/task/{taskId}/pause") - public void pauseTask(@PathVariable("taskId") String taskId) { - TaskInfo taskInfo = taskInfoData.findById(taskId); - if (taskInfo == null) { - throw new BizException(ErrCode.TASK_NOT_FOUND); - } - dataOwnerService.checkOwner(taskInfo); - taskManager.pauseTask(taskId, "stop by " + AuthUtil.getUserId()); + @ApiOperation("停止定时任务") + @PostMapping("/task/pause") + public boolean pauseTask(@Validated @RequestBody Request request) { + String taskId = request.getData(); + return ruleEngineService.pauseTask(taskId); + + } - @PostMapping("/task/{taskId}/resume") - public void resumeTask(@PathVariable("taskId") String taskId) { - TaskInfo taskInfo = taskInfoData.findById(taskId); - if (taskInfo == null) { - throw new BizException(ErrCode.TASK_NOT_FOUND); - } - dataOwnerService.checkOwner(taskInfo); - taskManager.resumeTask(taskId, "resume by " + AuthUtil.getUserId()); + @ApiOperation("恢复定时任务") + @PostMapping("/task/resume") + public boolean resumeTask(@Validated @RequestBody Request request) { + return ruleEngineService.resumeTask(request.getData()); } - @PostMapping("/task/{taskId}/renew") - public void renewTask(@PathVariable("taskId") String taskId) { - TaskInfo taskInfo = taskInfoData.findById(taskId); - if (taskInfo == null) { - throw new BizException(ErrCode.TASK_NOT_FOUND); - } - dataOwnerService.checkOwner(taskInfo); - try { - taskManager.renewTask(taskInfo); - taskManager.updateTaskState(taskId, TaskInfo.STATE_RUNNING, "renew by " + AuthUtil.getUserId()); - } catch (SchedulerException e) { - log.error("renew task error", e); - throw new BizException(ErrCode.RENEW_TASK_ERROR); - } + @PostMapping("/task/renew") + public boolean renewTask(@Validated @RequestBody Request request) { + String taskId = request.getData(); + return ruleEngineService.renewTask(taskId); + } - @DeleteMapping("/task/{taskId}/delete") - public void deleteTask(@PathVariable("taskId") String taskId) { - TaskInfo taskInfo = taskInfoData.findById(taskId); - if (taskInfo == null) { - throw new BizException(ErrCode.TASK_NOT_FOUND); - } + @DeleteMapping("/task/delete") + public boolean deleteTask(@Validated @RequestBody Request request) { + String taskId = request.getData(); + return ruleEngineService.deleteTask(taskId); - dataOwnerService.checkOwner(taskInfo); - taskManager.deleteTask(taskId, "delete by " + AuthUtil.getUserId()); - taskInfoData.deleteById(taskId); - taskLogData.deleteByTaskId(taskId); } - @PostMapping("/task/{taskId}/logs/{size}/{page}") - public Paging getTaskLogs( - @PathVariable("taskId") String taskId, - @PathVariable("size") int size, - @PathVariable("page") int page + @PostMapping("/taskLogs/list") + public Paging getTaskLogs( + @Validated @RequestBody PageRequest request ) { - TaskLog taskLog = new TaskLog(); - taskLog.setTaskId(taskId); - return taskLogData.findByTaskId(taskId, page, size); + return ruleEngineService.selectTaskLogPageList(request); + } - @DeleteMapping("/task/{taskId}/logs/clear") - public void clearTaskLogs(@PathVariable("taskId") String taskId) { - taskLogData.deleteByTaskId(taskId); + @DeleteMapping("/taskLogs/clear") + public boolean clearTaskLogs( @Validated @RequestBody PageRequest request) { + return ruleEngineService.clearTaskLogs(request.getData()); } } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java new file mode 100644 index 00000000..2cf8a0e3 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java @@ -0,0 +1,72 @@ +package cc.iotkit.manager.dto.bo.ruleinfo; + +import cc.iotkit.model.rule.RuleInfo; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonFormat; +import org.springframework.format.annotation.DateTimeFormat; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import javax.validation.constraints.Size; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.Date; +import java.math.BigDecimal; + +import cc.iotkit.common.api.BaseDto; +import cc.iotkit.common.validate.AddGroup; +import cc.iotkit.common.validate.EditGroup; + +import io.github.linpeilie.annotations.AutoMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@ApiModel(value = "RuleInfoBo") +@Data +@EqualsAndHashCode(callSuper = true) +@AutoMapper(target = RuleInfo.class, reverseConvertGenerate = false) +public class RuleInfoBo extends BaseDto { + + private static final long serialVersionUID = -1L; + + @ApiModelProperty(value="id") + private String id; + + @ApiModelProperty(value="动作") + @Size(max = 65535, message = "动作长度不正确") + private String actions; + + @ApiModelProperty(value="创建时间") + private Long createAt; + + @ApiModelProperty(value="描述") + @Size(max = 255, message = "描述长度不正确") + private String desc; + + @ApiModelProperty(value="过滤器") + @Size(max = 65535, message = "过滤器长度不正确") + private String filters; + + @ApiModelProperty(value="监听器") + @Size(max = 65535, message = "监听器长度不正确") + private String listeners; + + @ApiModelProperty(value="规则名称") + @Size(max = 255, message = "规则名称长度不正确") + private String name; + + @ApiModelProperty(value="状态") + @Size(max = 255, message = "状态长度不正确") + private String state; + + @ApiModelProperty(value="规则类型") + @Size(max = 255, message = "规则类型长度不正确") + private String type; + + @ApiModelProperty(value="用户id") + @Size(max = 255, message = "用户id长度不正确") + private String uid; + + } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleLogBo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleLogBo.java new file mode 100644 index 00000000..1ee91cb9 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleLogBo.java @@ -0,0 +1,40 @@ +package cc.iotkit.manager.dto.bo.ruleinfo; + +import cc.iotkit.common.api.BaseDto; +import cc.iotkit.model.rule.RuleInfo; +import io.github.linpeilie.annotations.AutoMapper; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import javax.validation.constraints.Size; + +@ApiModel(value = "RuleLogBo") +@Data +@EqualsAndHashCode(callSuper = true) +@AutoMapper(target = RuleInfo.class, reverseConvertGenerate = false) +public class RuleLogBo extends BaseDto { + + private static final long serialVersionUID = -1L; + + @ApiModelProperty(value="时间") + private Long time; + + @ApiModelProperty(value="规则id") + + private String ruleId; + + @ApiModelProperty(value="状态") + + private String state1; + + @ApiModelProperty(value="内容") + + private String content; + + @ApiModelProperty(value="是否成功") + + private Boolean success; + + } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskInfoBo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskInfoBo.java new file mode 100644 index 00000000..2ea8e463 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskInfoBo.java @@ -0,0 +1,70 @@ +package cc.iotkit.manager.dto.bo.taskinfo; + +import cc.iotkit.model.rule.TaskInfo; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonFormat; +import org.springframework.format.annotation.DateTimeFormat; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import javax.validation.constraints.Size; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.Date; +import java.math.BigDecimal; + +import cc.iotkit.common.api.BaseDto; +import cc.iotkit.common.validate.AddGroup; +import cc.iotkit.common.validate.EditGroup; + +import io.github.linpeilie.annotations.AutoMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; + + +@ApiModel(value = "TaskInfoBo") +@Data +@EqualsAndHashCode(callSuper = true) +@AutoMapper(target = TaskInfo.class, reverseConvertGenerate = false) +public class TaskInfoBo extends BaseDto { + + private static final long serialVersionUID = -1L; + + @ApiModelProperty(value="任务输出") + @Size(max = 65535, message = "任务输出长度不正确") + private String actions; + + @ApiModelProperty(value="创建时间") + private Long createAt; + + @ApiModelProperty(value="描述") + @Size(max = 255, message = "描述长度不正确") + private String desc; + + @ApiModelProperty(value="表达式") + @Size(max = 255, message = "表达式长度不正确") + private String expression; + + @ApiModelProperty(value="任务名称") + @Size(max = 255, message = "任务名称长度不正确") + private String name; + + @ApiModelProperty(value="操作备注") + @Size(max = 255, message = "操作备注长度不正确") + private String reason; + + @ApiModelProperty(value="任务状态") + @Size(max = 255, message = "任务状态长度不正确") + private String state; + + @ApiModelProperty(value="任务类型") + @Size(max = 255, message = "任务类型长度不正确") + private String type; + + @ApiModelProperty(value="创建者") + @Size(max = 255, message = "创建者长度不正确") + private String uid; + + } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskLogBo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskLogBo.java new file mode 100644 index 00000000..10677e38 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/taskinfo/TaskLogBo.java @@ -0,0 +1,34 @@ +/* + * +---------------------------------------------------------------------- + * | Copyright (c) 奇特物联 2021-2022 All rights reserved. + * +---------------------------------------------------------------------- + * | Licensed 未经许可不能去掉「奇特物联」相关版权 + * +---------------------------------------------------------------------- + * | Author: xw2sy@163.com + * +---------------------------------------------------------------------- + */ +package cc.iotkit.manager.dto.bo.taskinfo; + +import cc.iotkit.common.api.BaseDto; +import cc.iotkit.model.rule.TaskInfo; +import cc.iotkit.model.rule.TaskLog; +import io.github.linpeilie.annotations.AutoMapper; +import io.swagger.annotations.ApiModel; +import lombok.*; + +@ApiModel(value = "TaskLogBo") +@Data +@EqualsAndHashCode(callSuper = true) +@AutoMapper(target = TaskLog.class, reverseConvertGenerate = false) +public class TaskLogBo extends BaseDto { + + private String id; + + private String taskId; + + private String content; + + private Boolean success; + + private Long logAt; +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java new file mode 100644 index 00000000..59184812 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java @@ -0,0 +1,68 @@ +package cc.iotkit.manager.dto.vo.ruleinfo; + +import cc.iotkit.model.rule.RuleInfo; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.ToString; +import java.util.Date; +import java.io.Serializable; +import java.math.BigDecimal; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; +import io.github.linpeilie.annotations.AutoMapper; + + +@ApiModel(value = "RuleInfoVo") +@Data +@ExcelIgnoreUnannotated +@AutoMapper(target = RuleInfo.class) + +public class RuleInfoVo implements Serializable { + + private static final long serialVersionUID = -1L; + + @ApiModelProperty(value="规则id") + @ExcelProperty(value = "规则id") + private String id; + + @ApiModelProperty(value="动作") + @ExcelProperty(value = "动作") + private String actions; + + @ApiModelProperty(value="创建时间") + @ExcelProperty(value = "创建时间") + private Long createAt; + + @ApiModelProperty(value="描述") + @ExcelProperty(value = "描述") + private String desc; + + @ApiModelProperty(value="过滤器") + @ExcelProperty(value = "过滤器") + private String filters; + + @ApiModelProperty(value="监听器") + @ExcelProperty(value = "监听器") + private String listeners; + + @ApiModelProperty(value="规则名称") + @ExcelProperty(value = "规则名称") + private String name; + + @ApiModelProperty(value="状态") + @ExcelProperty(value = "状态") + private String state; + + @ApiModelProperty(value="规则类型") + @ExcelProperty(value = "规则类型") + private String type; + + @ApiModelProperty(value="用户id") + @ExcelProperty(value = "用户id") + private String uid; + + + +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleLogVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleLogVo.java new file mode 100644 index 00000000..fcc0c6a8 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleLogVo.java @@ -0,0 +1,38 @@ +package cc.iotkit.manager.dto.vo.ruleinfo; + +import cc.iotkit.common.api.BaseDto; +import cc.iotkit.model.rule.RuleInfo; +import io.github.linpeilie.annotations.AutoMapper; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@ApiModel(value = "RuleLogBo") +@Data +@EqualsAndHashCode(callSuper = true) +@AutoMapper(target = RuleInfo.class, reverseConvertGenerate = false) +public class RuleLogVo extends BaseDto { + + private static final long serialVersionUID = -1L; + + @ApiModelProperty(value="时间") + private Long time; + + @ApiModelProperty(value="规则id") + + private String ruleId; + + @ApiModelProperty(value="状态") + + private String state1; + + @ApiModelProperty(value="内容") + + private String content; + + @ApiModelProperty(value="是否成功") + + private Boolean success; + + } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskInfoVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskInfoVo.java new file mode 100644 index 00000000..05b6b9c1 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskInfoVo.java @@ -0,0 +1,68 @@ +package cc.iotkit.manager.dto.vo.taskinfo; + +import cc.iotkit.model.rule.TaskInfo; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.ToString; +import java.util.Date; +import java.io.Serializable; +import java.math.BigDecimal; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; +import io.github.linpeilie.annotations.AutoMapper; + + +@ApiModel(value = "TaskInfoVo") +@Data +@ExcelIgnoreUnannotated +@AutoMapper(target = TaskInfo.class) + +public class TaskInfoVo implements Serializable { + + private static final long serialVersionUID = -1L; + + @ApiModelProperty(value="主键") + @ExcelProperty(value = "主键") + private String id; + + @ApiModelProperty(value="任务输出") + @ExcelProperty(value = "任务输出") + private String actions; + + @ApiModelProperty(value="创建时间") + @ExcelProperty(value = "创建时间") + private Long createAt; + + @ApiModelProperty(value="描述") + @ExcelProperty(value = "描述") + private String desc; + + @ApiModelProperty(value="表达式") + @ExcelProperty(value = "表达式") + private String expression; + + @ApiModelProperty(value="任务名称") + @ExcelProperty(value = "任务名称") + private String name; + + @ApiModelProperty(value="操作备注") + @ExcelProperty(value = "操作备注") + private String reason; + + @ApiModelProperty(value="任务状态") + @ExcelProperty(value = "任务状态") + private String state; + + @ApiModelProperty(value="任务类型") + @ExcelProperty(value = "任务类型") + private String type; + + @ApiModelProperty(value="创建者") + @ExcelProperty(value = "创建者") + private String uid; + + + +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskLogVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskLogVo.java new file mode 100644 index 00000000..35275534 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/taskinfo/TaskLogVo.java @@ -0,0 +1,32 @@ +/* + * +---------------------------------------------------------------------- + * | Copyright (c) 奇特物联 2021-2022 All rights reserved. + * +---------------------------------------------------------------------- + * | Licensed 未经许可不能去掉「奇特物联」相关版权 + * +---------------------------------------------------------------------- + * | Author: xw2sy@163.com + * +---------------------------------------------------------------------- + */ +package cc.iotkit.manager.dto.vo.taskinfo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class TaskLogVo { + + private String id; + + private String taskId; + + private String content; + + private Boolean success; + + private Long logAt; +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IRuleEngineService.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IRuleEngineService.java new file mode 100644 index 00000000..fec4d126 --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IRuleEngineService.java @@ -0,0 +1,50 @@ +package cc.iotkit.manager.service; + +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Paging; +import cc.iotkit.manager.dto.bo.ruleinfo.RuleInfoBo; +import cc.iotkit.manager.dto.bo.ruleinfo.RuleLogBo; +import cc.iotkit.manager.dto.bo.taskinfo.TaskInfoBo; +import cc.iotkit.manager.dto.bo.taskinfo.TaskLogBo; +import cc.iotkit.manager.dto.vo.ruleinfo.RuleInfoVo; +import cc.iotkit.manager.dto.vo.ruleinfo.RuleLogVo; +import cc.iotkit.manager.dto.vo.taskinfo.TaskInfoVo; +import cc.iotkit.manager.dto.vo.taskinfo.TaskLogVo; + +/** + * @Author: jay + * @Date: 2023/5/30 18:14 + * @Version: V1.0 + * @Description: 规则引擎服务接口 + */ +public interface IRuleEngineService { + Paging selectPageList(PageRequest request); + + boolean saveRule(RuleInfoBo ruleInfoBo); + + boolean pauseRule(String ruleId); + + boolean resumeRule(String ruleId); + + boolean deleteRule(String ruleId); + + Paging selectRuleLogPageList(PageRequest request); + + boolean clearRuleLogs(String ruleId); + + Paging selectTaskPageList(PageRequest request); + + boolean saveTask(TaskInfoBo taskInfo); + + boolean pauseTask(String taskId); + + boolean resumeTask(String data); + + boolean renewTask(String taskId); + + boolean deleteTask(String taskId); + + Paging selectTaskLogPageList(PageRequest request); + + boolean clearTaskLogs(String taskId); +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java new file mode 100644 index 00000000..3e2f323d --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java @@ -0,0 +1,264 @@ +package cc.iotkit.manager.service.impl; + +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Paging; +import cc.iotkit.common.enums.ErrCode; +import cc.iotkit.common.exception.BizException; +import cc.iotkit.common.satoken.utils.AuthUtil; +import cc.iotkit.common.utils.MapstructUtils; +import cc.iotkit.common.utils.ReflectUtil; +import cc.iotkit.data.manager.IRuleInfoData; +import cc.iotkit.data.manager.ITaskInfoData; +import cc.iotkit.manager.dto.bo.ruleinfo.RuleInfoBo; +import cc.iotkit.manager.dto.bo.ruleinfo.RuleLogBo; +import cc.iotkit.manager.dto.bo.taskinfo.TaskInfoBo; +import cc.iotkit.manager.dto.bo.taskinfo.TaskLogBo; +import cc.iotkit.manager.dto.vo.ruleinfo.RuleInfoVo; +import cc.iotkit.manager.dto.vo.ruleinfo.RuleLogVo; +import cc.iotkit.manager.dto.vo.taskinfo.TaskInfoVo; +import cc.iotkit.manager.dto.vo.taskinfo.TaskLogVo; +import cc.iotkit.manager.service.DataOwnerService; +import cc.iotkit.manager.service.IRuleEngineService; +import cc.iotkit.model.rule.RuleInfo; +import cc.iotkit.model.rule.RuleLog; +import cc.iotkit.model.rule.TaskInfo; +import cc.iotkit.model.rule.TaskLog; +import cc.iotkit.ruleengine.rule.RuleManager; +import cc.iotkit.ruleengine.task.TaskManager; +import cc.iotkit.temporal.IRuleLogData; +import cc.iotkit.temporal.ITaskLogData; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.quartz.SchedulerException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.UUID; + +/** + * @Author: jay + * @Date: 2023/5/30 18:15 + * @Version: V1.0 + * @Description: 规则引擎服务实现 + */ + +@Slf4j +@RequiredArgsConstructor +@Service +public class RuleEngineServiceImpl implements IRuleEngineService { + @Autowired + private ITaskInfoData taskInfoData; + + @Autowired + private IRuleInfoData ruleInfoData; + + @Autowired + private IRuleLogData ruleLogData; + + @Autowired + private DataOwnerService dataOwnerService; + + @Autowired + private TaskManager taskManager; + + @Autowired + private RuleManager ruleManager; + + @Autowired + private ITaskLogData taskLogData; + + @Override + public Paging selectPageList(PageRequest request) { + RuleInfoBo data = request.getData(); + String type = data.getType(); + Integer page = request.getPageNum(); + Integer size = request.getPageSize(); + if (AuthUtil.isAdmin()) { + return MapstructUtils.convert(ruleInfoData.findByType(type, page, size), RuleInfoVo.class); + } else { + return MapstructUtils.convert(ruleInfoData.findByUidAndType(AuthUtil.getUserId(), type, page, size), RuleInfoVo.class); + } + } + + @Override + public boolean saveRule(RuleInfoBo ruleInfoBo) { + RuleInfo rule = MapstructUtils.convert(ruleInfoBo, RuleInfo.class); + if (StringUtils.isBlank(rule.getId())) { + rule.setId(UUID.randomUUID().toString()); + rule.setState(RuleInfo.STATE_STOPPED); + rule.setCreateAt(System.currentTimeMillis()); + rule.setUid(AuthUtil.getUserId()); + ruleInfoData.save(rule); + ruleManager.add(rule); + } else { + RuleInfo ruleInfo = ruleInfoData.findById(rule.getId()); + if (ruleInfo == null) { + throw new BizException(ErrCode.RULE_NOT_FOUND); + } + if (RuleInfo.STATE_RUNNING.equals(ruleInfo.getState())) { + throw new BizException(ErrCode.RULE_ALREADY_RUNNING); + } + + dataOwnerService.checkOwner(ruleInfo); + + ruleInfo.setListeners(rule.getListeners()); + ruleInfo.setFilters(rule.getFilters()); + ruleInfo.setActions(rule.getActions()); + ruleInfo.setName(rule.getName()); + ruleInfo.setDesc(rule.getDesc()); + + ruleInfoData.save(ruleInfo); + } + return true; + } + + @Override + public boolean pauseRule(String ruleId) { + RuleInfo ruleInfo = ruleInfoData.findById(ruleId); + if (ruleInfo == null) { + throw new BizException(ErrCode.RULE_NOT_FOUND); + } + dataOwnerService.checkOwner(ruleInfo); + ruleInfo.setState(RuleInfo.STATE_STOPPED); + ruleInfoData.save(ruleInfo); + ruleManager.pause(ruleInfo.getId()); + return true; + } + + @Override + public boolean resumeRule(String ruleId) { + RuleInfo ruleInfo = ruleInfoData.findById(ruleId); + if (ruleInfo == null) { + throw new BizException(ErrCode.RULE_NOT_FOUND); + } + dataOwnerService.checkOwner(ruleInfo); + ruleInfo.setState(RuleInfo.STATE_RUNNING); + ruleInfoData.save(ruleInfo); + ruleManager.resume(ruleInfo); + return true; + } + + @Override + public boolean deleteRule(String ruleId) { + RuleInfo ruleInfo = ruleInfoData.findById(ruleId); + if (ruleInfo == null) { + throw new BizException(ErrCode.RULE_NOT_FOUND); + } + dataOwnerService.checkOwner(ruleInfo); + ruleInfoData.deleteById(ruleInfo.getId()); + ruleManager.remove(ruleInfo.getId()); + ruleLogData.deleteByRuleId(ruleId); + return true; + } + + @Override + public Paging selectRuleLogPageList(PageRequest request) { + RuleLog ruleLog = request.getData().to(RuleLog.class); + + return ruleLogData.findByRuleId(ruleLog.getRuleId(), request.getPageNum(), request.getPageSize()).to(RuleLogVo.class); + } + + @Override + public boolean clearRuleLogs(String ruleId) { + ruleLogData.deleteByRuleId(ruleId); + return true; + } + + @Override + public Paging selectTaskPageList(PageRequest request) { + if (AuthUtil.isAdmin()) { + return taskInfoData.findAll(request.to(TaskInfo.class)).to(TaskInfoVo.class); + } + return taskInfoData.findByUid(AuthUtil.getUserId(), request.getPageNum(), request.getPageSize()).to(TaskInfoVo.class); + } + + @Override + public boolean saveTask(TaskInfoBo bo) { + TaskInfo taskInfo = bo.to(TaskInfo.class); + if (StringUtils.isBlank(taskInfo.getId())) { + taskInfo.setId(UUID.randomUUID().toString()); + taskInfo.setUid(AuthUtil.getUserId()); + taskInfo.setCreateAt(System.currentTimeMillis()); + taskInfo.setState(TaskInfo.STATE_STOP); + } else { + TaskInfo oldTask = taskInfoData.findById(taskInfo.getId()); + if (oldTask == null) { + throw new BizException(ErrCode.TASK_NOT_FOUND); + } + taskInfo = ReflectUtil.copyNoNulls(taskInfo, oldTask); + dataOwnerService.checkOwner(taskInfo); + } + + taskInfoData.save(taskInfo); + return true; + } + + @Override + public boolean pauseTask(String taskId) { + TaskInfo taskInfo = taskInfoData.findById(taskId); + if (taskInfo == null) { + throw new BizException(ErrCode.TASK_NOT_FOUND); + } + dataOwnerService.checkOwner(taskInfo); + taskManager.pauseTask(taskId, "stop by " + AuthUtil.getUserId()); + return true; + } + + @Override + public boolean resumeTask(String taskId) { + TaskInfo taskInfo = taskInfoData.findById(taskId); + if (taskInfo == null) { + throw new BizException(ErrCode.TASK_NOT_FOUND); + } + dataOwnerService.checkOwner(taskInfo); + taskManager.resumeTask(taskId, "resume by " + AuthUtil.getUserId()); + return true; + } + + @Override + public boolean renewTask(String taskId) { + TaskInfo taskInfo = taskInfoData.findById(taskId); + if (taskInfo == null) { + throw new BizException(ErrCode.TASK_NOT_FOUND); + } + dataOwnerService.checkOwner(taskInfo); + try { + taskManager.renewTask(taskInfo); + taskManager.updateTaskState(taskId, TaskInfo.STATE_RUNNING, "renew by " + AuthUtil.getUserId()); + } catch (SchedulerException e) { + log.error("renew task error", e); + throw new BizException(ErrCode.RENEW_TASK_ERROR); + } + return true; + } + + @Override + public boolean deleteTask(String taskId) { + TaskInfo taskInfo = taskInfoData.findById(taskId); + if (taskInfo == null) { + throw new BizException(ErrCode.TASK_NOT_FOUND); + } + + dataOwnerService.checkOwner(taskInfo); + taskManager.deleteTask(taskId, "delete by " + AuthUtil.getUserId()); + taskInfoData.deleteById(taskId); + taskLogData.deleteByTaskId(taskId); + return true; + } + + @Override + public Paging selectTaskLogPageList(PageRequest request) { + + TaskLog taskLog = request.getData().to(TaskLog.class); + + Paging byTaskId = taskLogData.findByTaskId(taskLog.getTaskId(), request.getPageNum(), request.getPageSize()); + return byTaskId.to(TaskLogVo.class); + } + + @Override + public boolean clearTaskLogs(String taskId) { + taskLogData.deleteByTaskId(taskId); + return true; + } +}