|
|
@@ -1,74 +1,33 @@
|
|
|
package com.usky.rule.util;
|
|
|
|
|
|
-//import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
-//import com.usky.rule.vo.Result;
|
|
|
-//import com.leo.common.context.UserContext;
|
|
|
-//import com.usky.rule.util.DateTimeUtil;
|
|
|
-//import com.usky.rule.util.JsonUtil;
|
|
|
-//import com.leo.common.util.SnowflakeIdGenerator;
|
|
|
-//import com.leo.dao.po.AlarmEvent;
|
|
|
-//import com.leo.dao.po.Message;
|
|
|
-//import com.leo.dao.po.RuleEngineLog;
|
|
|
-//import com.leo.dao.po.Space;
|
|
|
-//import com.leo.dao.po.UserMessage;
|
|
|
-//import com.leo.dao.po.WorkOrder;
|
|
|
-//import com.leo.dao.po.WorkOrderStatusRecord;
|
|
|
-//import com.leo.model.dto.GatewayControlParamsDTO;
|
|
|
-//import com.usky.rule.enums.ActionTypeEnum;
|
|
|
-//import com.usky.rule.enums.AlarmEventStatusEnum;
|
|
|
-//import com.usky.rule.enums.WorkOrderStatusEnum;
|
|
|
-//import com.leo.model.vo.ruleEngine.action.AlarmEventAction;
|
|
|
-//import com.usky.rule.vo.action.DeviceControlAction;
|
|
|
-//import com.usky.rule.vo.action.RuleEngineAction;
|
|
|
-//import com.leo.model.vo.ruleEngine.log.vo.AlarmActionLog;
|
|
|
-//import com.leo.model.vo.ruleEngine.log.vo.AlarmEventLog;
|
|
|
-//import com.usky.rule.vo.log.BaseLog;
|
|
|
-//import com.usky.rule.vo.log.Control;
|
|
|
-//import com.usky.rule.vo.log.ControlAction;
|
|
|
-//import com.usky.rule.vo.log.DeviceControlResult;
|
|
|
-//import com.usky.rule.vo.log.RuleEngineDetailLog;
|
|
|
-//import com.usky.rule.vo.visualization.SimpleVO;
|
|
|
-//import com.usky.rule.listeners.CommonListener;
|
|
|
-//import com.leo.service.alarm.AlarmEventService;
|
|
|
-//import com.leo.service.device.DeviceFunctionService;
|
|
|
-//import com.leo.service.device.DeviceService;
|
|
|
-//import com.leo.service.device.GatewayService;
|
|
|
-//import com.leo.service.message.MessageService;
|
|
|
-//import com.leo.service.message.UserMessageService;
|
|
|
-//import com.leo.service.product.ProductTemplateFunctionValueService;
|
|
|
-//import com.usky.rule.service.RuleEngineLogService;
|
|
|
-//import com.usky.rule.service.RuleEngineService;
|
|
|
-//import com.usky.rule.service.BaseSpaceService;
|
|
|
-//import com.leo.service.workOrder.WorkOrderService;
|
|
|
-//import com.leo.service.workOrder.WorkOrderStatusRecordService;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.Year;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
-
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.usky.common.security.utils.SecurityUtils;
|
|
|
+import com.usky.rule.domain.DmpProductCommand;
|
|
|
import com.usky.rule.domain.RuleEngineLog;
|
|
|
+import com.usky.rule.service.*;
|
|
|
import com.usky.rule.enums.ActionTypeEnum;
|
|
|
-import com.usky.rule.listeners.CommonListener;
|
|
|
-import com.usky.rule.service.BaseSpaceService;
|
|
|
-import com.usky.rule.service.RuleEngineLogService;
|
|
|
-import com.usky.rule.service.RuleEngineService;
|
|
|
-import com.usky.rule.vo.Result;
|
|
|
import com.usky.rule.vo.action.AlarmEventAction;
|
|
|
import com.usky.rule.vo.action.DeviceControlAction;
|
|
|
import com.usky.rule.vo.action.RuleEngineAction;
|
|
|
-import com.usky.rule.vo.log.BaseLog;
|
|
|
-import com.usky.rule.vo.log.ControlAction;
|
|
|
-import com.usky.rule.vo.log.DeviceControlResult;
|
|
|
-import com.usky.rule.vo.log.RuleEngineDetailLog;
|
|
|
+import com.usky.rule.vo.log.*;
|
|
|
import com.usky.rule.vo.visualization.SimpleVO;
|
|
|
+import com.usky.rule.listeners.CommonListener;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.Year;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import com.usky.rule.vo.Result;
|
|
|
+import com.usky.transfer.RemoteTransferService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@Component
|
|
|
@@ -76,15 +35,13 @@ public class RuleEngineUtil {
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CommonListener.class);
|
|
|
private RuleEngineLogService ruleEngineLogService;
|
|
|
private RuleEngineService ruleEngineService;
|
|
|
-// private GatewayService gatewayService;
|
|
|
-// private BaseSpaceService spaceService;
|
|
|
-// private DeviceFunctionService deviceFunctionService;
|
|
|
-// private DeviceService deviceService;
|
|
|
-// private AlarmEventService alarmEventService;
|
|
|
-// private MessageService messageService;
|
|
|
-// private UserMessageService userMessageService;
|
|
|
-// private WorkOrderStatusRecordService workOrderStatusRecordService;
|
|
|
-// private ProductTemplateFunctionValueService productTemplateFunctionValueService;
|
|
|
+ @Autowired
|
|
|
+ private RemoteTransferService remoteTransferService;
|
|
|
+ @Autowired
|
|
|
+ private DmpProductCommandService dmpProductCommandService;
|
|
|
+
|
|
|
+ @Value("${alarm.url}")
|
|
|
+ private String alarmUrl;
|
|
|
|
|
|
public static String getJobGroup(Long ruleEngineId) {
|
|
|
return "trigger-" + ruleEngineId;
|
|
|
@@ -98,248 +55,189 @@ public class RuleEngineUtil {
|
|
|
List<BaseLog> baseLogs = new ArrayList();
|
|
|
ruleEngineDetail.setActions(baseLogs);
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
- Set<String> deviceActionTypes = new HashSet(3);
|
|
|
+ Set<String> deviceActionTypes = new HashSet(2);
|
|
|
String ruleEngineName = this.ruleEngineService.getName(ruleEngineId);
|
|
|
|
|
|
-// for(RuleEngineAction ruleEngineAction : actionList) {
|
|
|
-// if (ActionTypeEnum.DEVICE_CONTROL.getType().equals(ruleEngineAction.getType())) {
|
|
|
-// DeviceControlAction deviceControlAction = (DeviceControlAction)ruleEngineAction;
|
|
|
-// deviceActionTypes.add("deviceControl");
|
|
|
-// BaseLog baseLog = new BaseLog();
|
|
|
-// baseLog.setType(ActionTypeEnum.DEVICE_CONTROL.getType());
|
|
|
-// ControlAction controlAction = new ControlAction();
|
|
|
-// this.performSingleDeviceControl(deviceControlAction, controlAction, now);
|
|
|
-// baseLog.setDetail(controlAction);
|
|
|
-// baseLogs.add(baseLog);
|
|
|
-// } else if (ActionTypeEnum.ALARM_EVENT.getType().equals(ruleEngineAction.getType())) {
|
|
|
-// this.generateAlarmEvent(ruleEngineId, ruleEngineName, projectId, spaceId, Collections.singletonList(ruleEngineAction), ruleEngineDetail);
|
|
|
-// deviceActionTypes.add("alarmEvent");
|
|
|
-// BaseLog baseLog = new BaseLog();
|
|
|
-// baseLog.setType(ActionTypeEnum.ALARM_EVENT.getType());
|
|
|
-// baseLog.setDetail(getAlarmActionLog(now, (AlarmEventAction)ruleEngineAction));
|
|
|
-// baseLogs.add(baseLog);
|
|
|
-// } else if (ActionTypeEnum.WORK_ORDER.getType().equals(ruleEngineAction.getType())) {
|
|
|
-// this.generateWorkOrder(ruleEngineId, ruleEngineName, projectId, spaceId, Collections.singletonList(ruleEngineAction), ruleEngineDetail);
|
|
|
-// deviceActionTypes.add("workOrder");
|
|
|
-// BaseLog baseLog = new BaseLog();
|
|
|
-// baseLog.setType(ActionTypeEnum.WORK_ORDER.getType());
|
|
|
-// baseLog.setDetail(getAlarmActionLog(now, (AlarmEventAction)ruleEngineAction));
|
|
|
-// baseLogs.add(baseLog);
|
|
|
-// }
|
|
|
-//
|
|
|
-// RuleEngineLog ruleEngineLog = new RuleEngineLog();
|
|
|
-// ruleEngineLog.setDetail(JsonUtil.toJson(ruleEngineDetail));
|
|
|
-// ruleEngineLog.setRuleEngineId(ruleEngineId);
|
|
|
-// ruleEngineLog.setRuleEngineName(ruleEngineName);
|
|
|
-// ruleEngineLog.setAutoTrigger((byte)(isAuto ? 1 : 0));
|
|
|
-// ruleEngineLog.setProjectId(projectId);
|
|
|
-// ruleEngineLog.setAction(String.join(",", deviceActionTypes));
|
|
|
-// ruleEngineLog.setTriggerType(triggerType);
|
|
|
-// ruleEngineLog.setUpdateTime(now);
|
|
|
-// ruleEngineLog.setCreateTime(now);
|
|
|
-// ruleEngineLog.setCreatedBy(1L);
|
|
|
-// ruleEngineLog.setUpdatedBy(1L);
|
|
|
-// ruleEngineLog.setTime(now);
|
|
|
-// this.ruleEngineLogService.save(ruleEngineLog);
|
|
|
-// }
|
|
|
+ for(RuleEngineAction ruleEngineAction : actionList) {
|
|
|
+ if (ActionTypeEnum.DEVICE_CONTROL.getType().equals(ruleEngineAction.getType())) {
|
|
|
+ DeviceControlAction deviceControlAction = (DeviceControlAction)ruleEngineAction;
|
|
|
+ deviceActionTypes.add("deviceControl");
|
|
|
+ BaseLog baseLog = new BaseLog();
|
|
|
+ baseLog.setType(ActionTypeEnum.DEVICE_CONTROL.getType());
|
|
|
+ ControlAction controlAction = new ControlAction();
|
|
|
+ this.performSingleDeviceControl(deviceControlAction, controlAction, now);
|
|
|
+ baseLog.setDetail(controlAction);
|
|
|
+ baseLogs.add(baseLog);
|
|
|
+ } else if (ActionTypeEnum.ALARM_EVENT.getType().equals(ruleEngineAction.getType())) {
|
|
|
+ this.generateAlarmEvent(ruleEngineId, ruleEngineName, projectId, spaceId, Collections.singletonList(ruleEngineAction));
|
|
|
+ deviceActionTypes.add("alarmEvent");
|
|
|
+ BaseLog baseLog = new BaseLog();
|
|
|
+ baseLog.setType(ActionTypeEnum.ALARM_EVENT.getType());
|
|
|
+ baseLog.setDetail(getAlarmActionLog(now, (AlarmEventAction)ruleEngineAction));
|
|
|
+ baseLogs.add(baseLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ RuleEngineLog ruleEngineLog = new RuleEngineLog();
|
|
|
+ ruleEngineLog.setDetail(JsonUtil.toJson(ruleEngineDetail));
|
|
|
+ ruleEngineLog.setRuleEngineId(ruleEngineId);
|
|
|
+ ruleEngineLog.setRuleEngineName(ruleEngineName);
|
|
|
+ ruleEngineLog.setAutoTrigger((byte) (isAuto ? 1 : 0));
|
|
|
+ ruleEngineLog.setProjectId(projectId);
|
|
|
+ ruleEngineLog.setAction(String.join(",", deviceActionTypes));
|
|
|
+ ruleEngineLog.setTriggerType(triggerType);
|
|
|
+ ruleEngineLog.setUpdateTime(now);
|
|
|
+ ruleEngineLog.setCreateTime(now);
|
|
|
+ ruleEngineLog.setCreatedBy(SecurityUtils.getUsername());
|
|
|
+ ruleEngineLog.setUpdatedBy(SecurityUtils.getUsername());
|
|
|
+ ruleEngineLog.setTime(now);
|
|
|
+ this.ruleEngineLogService.save(ruleEngineLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+ @NotNull
|
|
|
+ private static AlarmActionLog getAlarmActionLog(LocalDateTime now, AlarmEventAction ruleEngineAction) {
|
|
|
+ AlarmActionLog alarmActionLog = new AlarmActionLog();
|
|
|
+ List<AlarmEventAction> alarmEventLogs = new ArrayList();
|
|
|
+ alarmActionLog.setAlarms(alarmEventLogs);
|
|
|
+ alarmActionLog.setTime(DateTimeUtil.format(now));
|
|
|
+ alarmEventLogs.add(ruleEngineAction);
|
|
|
+ return alarmActionLog;
|
|
|
}
|
|
|
|
|
|
-// @NotNull
|
|
|
-// private static AlarmActionLog getAlarmActionLog(LocalDateTime now, AlarmEventAction ruleEngineAction) {
|
|
|
-// AlarmActionLog alarmActionLog = new AlarmActionLog();
|
|
|
-// List<AlarmEventLog> alarmEventLogs = new ArrayList();
|
|
|
-// alarmActionLog.setAlarms(alarmEventLogs);
|
|
|
-// alarmActionLog.setTime(DateTimeUtil.format(now));
|
|
|
-// AlarmEventLog alarmEventLog = new AlarmEventLog();
|
|
|
-// alarmEventLog.setNotifiers(ruleEngineAction.getNotifiers());
|
|
|
-// alarmEventLog.setLevel(ruleEngineAction.getLevel());
|
|
|
-// alarmEventLog.setMethod(ruleEngineAction.getMethod());
|
|
|
-// alarmEventLogs.add(alarmEventLog);
|
|
|
-// return alarmActionLog;
|
|
|
-// }
|
|
|
-//
|
|
|
-// public void generateAlarmEvent(Long ruleEngineId, String ruleEngineName, Long projectId, Long spaceId, List<RuleEngineAction> actions, RuleEngineDetailLog ruleEngineDetail) {
|
|
|
-// List<AlarmEvent> events = new ArrayList();
|
|
|
-// actions.forEach((action) -> {
|
|
|
-// AlarmEventAction alarmEventAction = (AlarmEventAction)action;
|
|
|
-// String alarmContent = this.ruleEngineLogService.getAlarmContent(ruleEngineDetail);
|
|
|
-// AlarmEvent alarmEvent = new AlarmEvent();
|
|
|
-// Short level = Short.parseShort(alarmEventAction.getLevel());
|
|
|
-// assignValueForAlarmEvent(ruleEngineId, projectId, spaceId, level, alarmEvent);
|
|
|
-// Space space = (Space)this.spaceService.getById(spaceId);
|
|
|
-// alarmEvent.setSpacePathName(space != null ? space.getPathName() : "");
|
|
|
-// alarmEvent.setStatus(AlarmEventStatusEnum.PENDING.getValue());
|
|
|
-// alarmEvent.setContent(alarmContent);
|
|
|
-// LocalDateTime now = LocalDateTime.now();
|
|
|
-// alarmEvent.setCreateTime(now);
|
|
|
-// alarmEvent.setCreatedBy(0L);
|
|
|
-// alarmEvent.setUpdatedBy(0L);
|
|
|
-// alarmEvent.setUpdateTime(now);
|
|
|
-// events.add(alarmEvent);
|
|
|
-// this.generateMessage(ruleEngineName, alarmEventAction.getNotifiers(), alarmContent, now);
|
|
|
-// });
|
|
|
-// this.alarmEventService.saveBatch(events);
|
|
|
-// }
|
|
|
-//
|
|
|
-// public void generateWorkOrder(Long ruleEngineId, String ruleEngineName, Long projectId, Long spaceId, List<RuleEngineAction> actions, RuleEngineDetailLog ruleEngineDetail) {
|
|
|
-// actions.forEach((action) -> {
|
|
|
-// AlarmEventAction alarmEventAction = (AlarmEventAction)action;
|
|
|
-// AlarmEvent alarmEvent = new AlarmEvent();
|
|
|
-// Short level = Short.parseShort(alarmEventAction.getLevel());
|
|
|
-// assignValueForAlarmEvent(ruleEngineId, projectId, spaceId, level, alarmEvent);
|
|
|
-// Space space = (Space)this.spaceService.getById(spaceId);
|
|
|
-// String spacePathName = space != null ? space.getPathName() : "";
|
|
|
-// alarmEvent.setSpacePathName(spacePathName);
|
|
|
-// alarmEvent.setStatus(AlarmEventStatusEnum.PENDING.getValue());
|
|
|
-// String alarmContent = this.ruleEngineLogService.getAlarmContent(ruleEngineDetail);
|
|
|
-// alarmEvent.setContent(alarmContent);
|
|
|
-// LocalDateTime now = LocalDateTime.now();
|
|
|
-// alarmEvent.setCreateTime(now);
|
|
|
-// alarmEvent.setCreatedBy(0L);
|
|
|
-// alarmEvent.setUpdatedBy(0L);
|
|
|
-// alarmEvent.setUpdateTime(now);
|
|
|
-// this.alarmEventService.save(alarmEvent);
|
|
|
-// WorkOrder workOrder = new WorkOrder();
|
|
|
-// workOrder.setProjectId(projectId);
|
|
|
-// workOrder.setSpaceId(spaceId);
|
|
|
-// workOrder.setSpacePathName(spacePathName);
|
|
|
-// workOrder.setContent(alarmContent);
|
|
|
-// workOrder.setNumber(String.valueOf(Year.now().getValue()) + SnowflakeIdGenerator.getInstance().nextId());
|
|
|
-// workOrder.setLevel(Short.parseShort(alarmEventAction.getLevel()));
|
|
|
-// workOrder.setStatus(WorkOrderStatusEnum.NOT_ASSIGN.getValue());
|
|
|
-// workOrder.setClassification(Short.valueOf((short)1));
|
|
|
-// workOrder.setCreateTime(now);
|
|
|
-// workOrder.setCreatedBy(0L);
|
|
|
-// workOrder.setUpdatedBy(0L);
|
|
|
-// workOrder.setUpdateTime(now);
|
|
|
-// workOrder.setBizId(alarmEvent.getId().toString());
|
|
|
-// this.workOrderService.save(workOrder);
|
|
|
-// WorkOrderStatusRecord workOrderStatusRecord = new WorkOrderStatusRecord();
|
|
|
-// workOrderStatusRecord.setWorkOrderId(workOrder.getId());
|
|
|
-// workOrderStatusRecord.setStatus(WorkOrderStatusEnum.NOT_ASSIGN.getValue());
|
|
|
-// workOrderStatusRecord.setOperatorId(UserContext.getUserId());
|
|
|
-// workOrderStatusRecord.setOperator("雷优人");
|
|
|
-// workOrderStatusRecord.setOperatorId(0L);
|
|
|
-// workOrderStatusRecord.setBizParamDescr("雷优人 提交工单 " + DateTimeUtil.format(LocalDateTime.now()));
|
|
|
-// workOrderStatusRecord.setCreateTime(now);
|
|
|
-// workOrderStatusRecord.setCreatedBy(0L);
|
|
|
-// workOrderStatusRecord.setUpdatedBy(0L);
|
|
|
-// workOrderStatusRecord.setUpdateTime(now);
|
|
|
-// this.workOrderStatusRecordService.save(workOrderStatusRecord);
|
|
|
-// this.generateMessage(ruleEngineName, alarmEventAction.getNotifiers(), alarmContent, now);
|
|
|
-// });
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void generateMessage(String ruleEngineName, List<SimpleVO> notifiers, String alarmContent, LocalDateTime now) {
|
|
|
-// Message message = new Message();
|
|
|
-// message.setContent(alarmContent);
|
|
|
-// message.setCreateTime(now);
|
|
|
-// message.setCreatedBy(0L);
|
|
|
-// message.setUpdatedBy(0L);
|
|
|
-// message.setUpdateTime(now);
|
|
|
-// this.messageService.save(message);
|
|
|
-// if (notifiers != null) {
|
|
|
-// List<UserMessage> userMessages = new ArrayList();
|
|
|
-// notifiers.forEach((notifier) -> {
|
|
|
-// UserMessage userMessage = new UserMessage();
|
|
|
-// userMessage.setMsgId(message.getId());
|
|
|
-// userMessage.setTitle(ruleEngineName);
|
|
|
-// userMessage.setMsgType(Short.valueOf((short)1));
|
|
|
-// userMessage.setSubMsgType(Short.valueOf((short)1));
|
|
|
-// userMessage.setUserId(Long.parseLong(notifier.getId()));
|
|
|
-// userMessage.setSentTime(now);
|
|
|
-// userMessage.setCreateTime(now);
|
|
|
-// userMessage.setCreatedBy(0L);
|
|
|
-// userMessage.setUpdatedBy(0L);
|
|
|
-// userMessage.setUpdateTime(now);
|
|
|
-// userMessages.add(userMessage);
|
|
|
-// });
|
|
|
-// this.userMessageService.saveBatch(userMessages);
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-//
|
|
|
-// private static void assignValueForAlarmEvent(Long ruleEngineId, Long projectId, Long spaceId, Short level, AlarmEvent alarmEvent) {
|
|
|
-// alarmEvent.setProjectId(projectId);
|
|
|
-// alarmEvent.setSn(String.valueOf(Year.now().getValue()) + SnowflakeIdGenerator.getInstance().nextId());
|
|
|
-// alarmEvent.setLevel(level);
|
|
|
-// alarmEvent.setType(Short.valueOf((short)1));
|
|
|
-// alarmEvent.setBizId(ruleEngineId.toString());
|
|
|
-// alarmEvent.setTime(LocalDateTime.now());
|
|
|
-// alarmEvent.setSpaceId(spaceId);
|
|
|
-// }
|
|
|
-//
|
|
|
-// public void performSingleDeviceControl(DeviceControlAction deviceControlAction, ControlAction controlAction, LocalDateTime now) {
|
|
|
-// List<SimpleVO> devices = deviceControlAction.getDevices();
|
|
|
-// if (devices != null && !devices.isEmpty()) {
|
|
|
-// String deviceId = ((SimpleVO)devices.get(0)).getId();
|
|
|
-// List<DeviceControlAction.Functions> functions = deviceControlAction.getFunctions();
|
|
|
-// List<Control> controls = new ArrayList();
|
|
|
-// List<DeviceControlResult> deviceControlResults = new ArrayList();
|
|
|
-// controlAction.setControls(controls);
|
|
|
-// controlAction.setDevices(deviceControlResults);
|
|
|
-// controlAction.setTime(DateTimeUtil.format(now));
|
|
|
-//
|
|
|
-// for(DeviceControlAction.Functions function : functions) {
|
|
|
-// HashMap<String, String> functionMap = new HashMap();
|
|
|
-// functionMap.put(function.getIdentifier(), function.getValue());
|
|
|
-// Control control = new Control();
|
|
|
-// control.setDelaySeconds(function.getDelaySeconds());
|
|
|
-// control.setIdentifier(function.getIdentifier());
|
|
|
-// control.setName(this.deviceFunctionService.getName(deviceId, function.getIdentifier()));
|
|
|
-// control.setValue(function.getValue());
|
|
|
-// String valueDesc = this.productTemplateFunctionValueService.getValueDesc(deviceId, function.getIdentifier(), function.getValue());
|
|
|
-// control.setValueDesc(valueDesc);
|
|
|
-// controls.add(control);
|
|
|
-//
|
|
|
-// for(SimpleVO device : devices) {
|
|
|
-// String id = device.getId();
|
|
|
-// String name = device.getName();
|
|
|
-// GatewayControlParamsDTO dto = new GatewayControlParamsDTO();
|
|
|
-// dto.setDeviceId(id);
|
|
|
-// dto.setFunctions(functionMap);
|
|
|
-// dto.setTimeout(function.getDelaySeconds());
|
|
|
-// dto.setType(Short.valueOf((short)5));
|
|
|
-// DeviceControlResult deviceControlResult = new DeviceControlResult();
|
|
|
-// deviceControlResult.setId(id);
|
|
|
-// deviceControlResult.setName(StringUtils.isBlank(name) ? this.deviceService.getName(id) : name);
|
|
|
-// ArrayList<DeviceControlResult.Result> results = new ArrayList();
|
|
|
-// deviceControlResult.setResult(results);
|
|
|
-// deviceControlResults.add(deviceControlResult);
|
|
|
-// DeviceControlResult.Result result = new DeviceControlResult.Result();
|
|
|
-//
|
|
|
-// try {
|
|
|
-// result.setReqTime(DateTimeUtil.format(LocalDateTime.now()));
|
|
|
-// String content = this.gatewayService.controlDevice(dto);
|
|
|
-// Result<?> reponseResult = (Result)JsonUtil.parse(content, new TypeReference<Result<?>>() {
|
|
|
-// });
|
|
|
-// result.setCode(reponseResult.getCode());
|
|
|
-// } catch (Exception e) {
|
|
|
-// result.setCode("200001");
|
|
|
-// result.setMessage(e.getMessage());
|
|
|
-// }
|
|
|
-//
|
|
|
-// result.setRespTime(DateTimeUtil.format(LocalDateTime.now()));
|
|
|
-// results.add(result);
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// public RuleEngineUtil(final RuleEngineLogService ruleEngineLogService, final RuleEngineService ruleEngineService, final GatewayService gatewayService, final SpaceService spaceService, final DeviceFunctionService deviceFunctionService, final DeviceService deviceService, final AlarmEventService alarmEventService, final WorkOrderService workOrderService, final MessageService messageService, final UserMessageService userMessageService, final WorkOrderStatusRecordService workOrderStatusRecordService, final ProductTemplateFunctionValueService productTemplateFunctionValueService) {
|
|
|
-// this.ruleEngineLogService = ruleEngineLogService;
|
|
|
-// this.ruleEngineService = ruleEngineService;
|
|
|
-// this.gatewayService = gatewayService;
|
|
|
-// this.spaceService = spaceService;
|
|
|
-// this.deviceFunctionService = deviceFunctionService;
|
|
|
-// this.deviceService = deviceService;
|
|
|
-// this.alarmEventService = alarmEventService;
|
|
|
-// this.workOrderService = workOrderService;
|
|
|
-// this.messageService = messageService;
|
|
|
-// this.userMessageService = userMessageService;
|
|
|
-// this.workOrderStatusRecordService = workOrderStatusRecordService;
|
|
|
-// this.productTemplateFunctionValueService = productTemplateFunctionValueService;
|
|
|
-// }
|
|
|
+ /**
|
|
|
+ * 推送告警
|
|
|
+ * @param ruleEngineId
|
|
|
+ * @param ruleEngineName
|
|
|
+ * @param projectId
|
|
|
+ * @param spaceId
|
|
|
+ * @param actions
|
|
|
+ */
|
|
|
+ public void generateAlarmEvent(Long ruleEngineId, String ruleEngineName, Long projectId, Long spaceId, List<RuleEngineAction> actions) {
|
|
|
+ actions.forEach((action) -> {
|
|
|
+ AlarmEventAction alarmEventAction = (AlarmEventAction)action;
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("deviceId",alarmEventAction.getDeviceId());
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ String alarmTime = now.format(formatter);
|
|
|
+ jsonObject.put("alarmTime",alarmTime);
|
|
|
+ jsonObject.put("alarmType",alarmEventAction.getAlarmType());
|
|
|
+ jsonObject.put("alarmObject",alarmEventAction.getDeviceName());
|
|
|
+ jsonObject.put("alarmData",0);
|
|
|
+ jsonObject.put("alarmAttribute",alarmEventAction.getAlarmAttribute());
|
|
|
+ jsonObject.put("alarmContent",alarmEventAction.getAlarmAttribute());
|
|
|
+ jsonObject.put("alarmGrade",alarmEventAction.getAlarmGrade());
|
|
|
+ jsonObject.put("alarmAddress",alarmEventAction.getCommAddress());
|
|
|
+ jsonObject.put("productCode",alarmEventAction.getProductCode());
|
|
|
+
|
|
|
+ HttpClientUtils.doPostJson(alarmUrl,jsonObject.toJSONString());
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从 JSON 数组字符串中,根据 value 值获取对应的 name 值
|
|
|
+ * @param jsonArrayStr JSON数组字符串(如:[{"name":"开","value":"1"},{"name":"关","value":"2"}])
|
|
|
+ * @param targetValue 要匹配的value值(如:"1")
|
|
|
+ * @return 匹配的name值,无匹配返回空字符串
|
|
|
+ */
|
|
|
+ public static String getNameByValue(String jsonArrayStr, String targetValue) {
|
|
|
+ // 1. 空值校验
|
|
|
+ if (StringUtils.isBlank(jsonArrayStr) || StringUtils.isBlank(targetValue)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 解析 JSON 数组
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(jsonArrayStr);
|
|
|
+
|
|
|
+ // 3. 遍历数组,匹配 value,返回对应 name(一行核心逻辑)
|
|
|
+ return jsonArray.stream()
|
|
|
+ .map(obj -> (JSONObject) obj) // 转为 JSONObject
|
|
|
+ .filter(jsonObj -> targetValue.equals(jsonObj.getString("value"))) // 匹配 value
|
|
|
+ .findFirst() // 获取第一个匹配项
|
|
|
+ .map(jsonObj -> jsonObj.getString("name")) // 提取 name
|
|
|
+ .orElse(""); // 无匹配返回空字符串(也可改为"未知")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void performSingleDeviceControl(DeviceControlAction deviceControlAction, ControlAction controlAction, LocalDateTime now) {
|
|
|
+ List<SimpleVO> devices = deviceControlAction.getDevices();
|
|
|
+ if (devices != null && !devices.isEmpty()) {
|
|
|
+ Integer productId = deviceControlAction.getProductId();
|
|
|
+ String productCode = deviceControlAction.getProductCode();
|
|
|
+ //String productCode = dmpProductService.getOne(new LambdaQueryWrapper<DmpProduct>().eq(DmpProduct::getId, productId).eq(DmpProduct::getDeleteFlag,0).eq(DmpProduct::getTenantId, SecurityUtils.getTenantId())).getProductCode();
|
|
|
+ List<DmpProductCommand> dmpProductCommandList = Optional.ofNullable(
|
|
|
+ dmpProductCommandService.getBaseMapper().selectList(new LambdaQueryWrapper<DmpProductCommand>()
|
|
|
+ .eq(DmpProductCommand::getProductCode, productCode)
|
|
|
+ .eq(DmpProductCommand::getTenantId, SecurityUtils.getTenantId())))
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
+ List<DeviceControlAction.Functions> functions = deviceControlAction.getFunctions();
|
|
|
+ List<Control> controls = new ArrayList();
|
|
|
+ List<DeviceControlResult> deviceControlResults = new ArrayList();
|
|
|
+ controlAction.setControls(controls);
|
|
|
+ controlAction.setDevices(deviceControlResults);
|
|
|
+ controlAction.setTime(DateTimeUtil.format(now));
|
|
|
+
|
|
|
+ for(DeviceControlAction.Functions function : functions) {
|
|
|
+ String identifier = function.getIdentifier();
|
|
|
+ DmpProductCommand dmpProductCommand = dmpProductCommandList.stream()
|
|
|
+ .filter(cmd -> StringUtils.equals(cmd.getCommandCode(), identifier))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ HashMap<String, String> functionMap = new HashMap();
|
|
|
+ functionMap.put(identifier, function.getValue());
|
|
|
+ Control control = new Control();
|
|
|
+ control.setDelaySeconds(function.getDelaySeconds());
|
|
|
+ control.setIdentifier(identifier);
|
|
|
+ control.setName(dmpProductCommand != null ? dmpProductCommand.getCommandName() : null);
|
|
|
+ control.setValue(function.getValue());
|
|
|
+ control.setValueDesc(getNameByValue(dmpProductCommand != null ? dmpProductCommand.getCommandDict() : null, function.getValue()));
|
|
|
+ controls.add(control);
|
|
|
+
|
|
|
+ for(SimpleVO device : devices) {
|
|
|
+ String id = device.getId();
|
|
|
+ String name = device.getName();
|
|
|
+ String deviceUuid = device.getDeviceUuid();
|
|
|
+ String gatewayUuid = device.getGatewayUuid();
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("method", "control");
|
|
|
+ map.put("deviceUuid", deviceUuid);
|
|
|
+ Map<String, Object> map1 = new HashMap<>();
|
|
|
+ map1.put("commandCode", function.getIdentifier());
|
|
|
+ map1.put("commandValue", function.getValue());
|
|
|
+ map.put("params", map1);
|
|
|
+
|
|
|
+
|
|
|
+ DeviceControlResult deviceControlResult = new DeviceControlResult();
|
|
|
+ deviceControlResult.setId(id);
|
|
|
+ deviceControlResult.setName(name);
|
|
|
+ ArrayList<DeviceControlResult.Result> results = new ArrayList();
|
|
|
+ deviceControlResult.setResult(results);
|
|
|
+ deviceControlResults.add(deviceControlResult);
|
|
|
+ DeviceControlResult.Result result = new DeviceControlResult.Result();
|
|
|
+
|
|
|
+ try {
|
|
|
+ result.setReqTime(DateTimeUtil.format(LocalDateTime.now()));
|
|
|
+ Map<String,Object> responseData = remoteTransferService.deviceControl(productCode, gatewayUuid, JSON.toJSONString(map), SecurityUtils.getTenantId(), SecurityUtils.getUserId(), SecurityUtils.getUsername());
|
|
|
+
|
|
|
+ result.setCode(responseData.get("code").toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.setCode("200001");
|
|
|
+ result.setMessage(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ result.setRespTime(DateTimeUtil.format(LocalDateTime.now()));
|
|
|
+ results.add(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public RuleEngineUtil(final RuleEngineLogService ruleEngineLogService, final RuleEngineService ruleEngineService) {
|
|
|
+ this.ruleEngineLogService = ruleEngineLogService;
|
|
|
+ this.ruleEngineService = ruleEngineService;
|
|
|
+ }
|
|
|
}
|