浏览代码

修改告警推送体Json结构,优化推送代码逻辑,可以选择一个产品的多个设备

james 2 周之前
父节点
当前提交
f3e360e7bc

+ 17 - 8
service-rule/service-rule-biz/src/main/java/com/usky/rule/util/RuleEngineUtil.java

@@ -13,6 +13,7 @@ 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.*;
+import com.usky.rule.vo.visualization.AlarmSimpleVO;
 import com.usky.rule.vo.visualization.SimpleVO;
 import com.usky.rule.listeners.CommonListener;
 import java.time.LocalDateTime;
@@ -29,8 +30,10 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
 
 @Component
+@Service
 public class RuleEngineUtil {
     private static final Logger LOGGER = LoggerFactory.getLogger(CommonListener.class);
     private RuleEngineLogService ruleEngineLogService;
@@ -114,25 +117,28 @@ public class RuleEngineUtil {
      * @param actions
      */
     public void generateAlarmEvent(Long ruleEngineId, String ruleEngineName, Long projectId, Long spaceId, List<RuleEngineAction> actions) {
-        actions.forEach((action) -> {
-            AlarmEventAction alarmEventAction = (AlarmEventAction)action;
+        AlarmEventAction alarmEventAction = (AlarmEventAction)actions;
+        List<AlarmSimpleVO> devices = alarmEventAction.getDevices();
+        if (devices != null && !devices.isEmpty()) {
             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());
+            for (AlarmSimpleVO device : devices) {
+                jsonObject.put("deviceId",device.getDeviceId());
+                jsonObject.put("alarmObject",device.getDeviceName());
+                jsonObject.put("alarmAddress",device.getCommAddress());
 
-            HttpClientUtils.doPostJson(alarmUrl,jsonObject.toJSONString());
-        });
+                HttpClientUtils.doPostJson(alarmUrl,jsonObject.toJSONString());
+            }
+        }
     }
 
     /**
@@ -200,6 +206,9 @@ public class RuleEngineUtil {
                     String name = device.getName();
                     String deviceUuid = device.getDeviceUuid();
                     String gatewayUuid = device.getGatewayUuid();
+                    Integer tenantId = SecurityUtils.getTenantId();
+                    long userId = SecurityUtils.getUserId();
+                    String userName = SecurityUtils.getUsername();
 
                     Map<String, Object> map = new HashMap<>();
                     map.put("method", "control");
@@ -220,7 +229,7 @@ public class RuleEngineUtil {
 
                     try {
                         result.setReqTime(DateTimeUtil.format(LocalDateTime.now()));
-                        Map<String,Object> responseData =  remoteTransferService.deviceControl(productCode, gatewayUuid, JSON.toJSONString(map), SecurityUtils.getTenantId(), SecurityUtils.getUserId(), SecurityUtils.getUsername());
+                        Map<String,Object> responseData =  remoteTransferService.deviceControl(productCode, gatewayUuid, JSON.toJSONString(map), tenantId, userId, userName);
 
                         result.setCode(responseData.get("code").toString());
                     } catch (Exception e) {

+ 20 - 63
service-rule/service-rule-biz/src/main/java/com/usky/rule/vo/action/AlarmEventAction.java

@@ -2,22 +2,22 @@ package com.usky.rule.vo.action;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.usky.rule.enums.ActionTypeEnum;
+import com.usky.rule.vo.visualization.AlarmSimpleVO;
+import com.usky.rule.vo.visualization.SimpleVO;
+
+import java.util.List;
 
 public class AlarmEventAction implements RuleEngineAction {
     @JsonProperty("alarmGrade")
     private Integer alarmGrade;
     @JsonProperty("productCode")
     private String productCode;
-    @JsonProperty("deviceId")
-    private String deviceId;
-    @JsonProperty("deviceName")
-    private String deviceName;
     @JsonProperty("alarmType")
     private String alarmType;
     @JsonProperty("alarmAttribute")
     private String alarmAttribute;
-    @JsonProperty("commAddress")
-    private String commAddress;
+    @JsonProperty("devices")
+    private List<AlarmSimpleVO> devices;
 
     public String getType() {
         return ActionTypeEnum.ALARM_EVENT.getType();
@@ -34,14 +34,6 @@ public class AlarmEventAction implements RuleEngineAction {
         return this.productCode;
     }
 
-    public String getDeviceId() {
-        return this.deviceId;
-    }
-
-    public String getDeviceName() {
-        return this.deviceName;
-    }
-
     public String getAlarmType() {
         return this.alarmType;
     }
@@ -50,8 +42,8 @@ public class AlarmEventAction implements RuleEngineAction {
         return this.alarmAttribute;
     }
 
-    public String getCommAddress() {
-        return this.commAddress;
+    public List<AlarmSimpleVO> getDevices() {
+        return this.devices;
     }
 
     @JsonProperty("alarmGrade")
@@ -64,16 +56,6 @@ public class AlarmEventAction implements RuleEngineAction {
         this.productCode = productCode;
     }
 
-    @JsonProperty("deviceId")
-    public void setDeviceId(final String deviceId) {
-        this.deviceId = deviceId;
-    }
-
-    @JsonProperty("deviceName")
-    public void setDeviceName(final String deviceName) {
-        this.deviceName = deviceName;
-    }
-
     @JsonProperty("alarmType")
     public void setAlarmType(final String alarmType) {
         this.alarmType = alarmType;
@@ -84,9 +66,9 @@ public class AlarmEventAction implements RuleEngineAction {
         this.alarmAttribute = alarmAttribute;
     }
 
-    @JsonProperty("commAddress")
-    public void setCommAddress(final String commAddress) {
-        this.commAddress = commAddress;
+    @JsonProperty("devices")
+    public void setDevices(final List<AlarmSimpleVO> devices) {
+        this.devices = devices;
     }
 
     public boolean equals(final Object o) {
@@ -119,26 +101,6 @@ public class AlarmEventAction implements RuleEngineAction {
                     return false;
                 }
 
-                Object this$deviceId = this.getDeviceId();
-                Object other$deviceId = other.getDeviceId();
-                if (this$deviceId == null) {
-                    if (other$deviceId != null) {
-                        return false;
-                    }
-                } else if (!this$deviceId.equals(other$deviceId)) {
-                    return false;
-                }
-
-                Object this$deviceName = this.getDeviceName();
-                Object other$deviceName = other.getDeviceName();
-                if (this$deviceName == null) {
-                    if (other$deviceName != null) {
-                        return false;
-                    }
-                } else if (!this$deviceName.equals(other$deviceName)) {
-                    return false;
-                }
-
                 Object this$alarmType = this.getAlarmType();
                 Object other$alarmType = other.getAlarmType();
                 if (this$alarmType == null) {
@@ -159,13 +121,13 @@ public class AlarmEventAction implements RuleEngineAction {
                     return false;
                 }
 
-                Object this$commAddress = this.getCommAddress();
-                Object other$commAddress = other.getCommAddress();
-                if (this$commAddress == null) {
-                    if (other$commAddress != null) {
+                Object this$devices = this.getDevices();
+                Object other$devices = other.getDevices();
+                if (this$devices == null) {
+                    if (other$devices != null) {
                         return false;
                     }
-                } else if (!this$commAddress.equals(other$commAddress)) {
+                } else if (!this$devices.equals(other$devices)) {
                     return false;
                 }
 
@@ -185,23 +147,18 @@ public class AlarmEventAction implements RuleEngineAction {
         result = result * 59 + ($alarmGrade == null ? 43 : $alarmGrade.hashCode());
         Object $productCode = this.getProductCode();
         result = result * 59 + ($productCode == null ? 43 : $productCode.hashCode());
-        Object $deviceId = this.getDeviceId();
-        result = result * 59 + ($deviceId == null ? 43 : $deviceId.hashCode());
-        Object $deviceName = this.getDeviceName();
-        result = result * 59 + ($deviceName == null ? 43 : $deviceName.hashCode());
         Object $alarmType = this.getAlarmType();
         result = result * 59 + ($alarmType == null ? 43 : $alarmType.hashCode());
         Object $alarmAttribute = this.getAlarmAttribute();
         result = result * 59 + ($alarmAttribute == null ? 43 : $alarmAttribute.hashCode());
-        Object $commAddress = this.getCommAddress();
-        result = result * 59 + ($commAddress == null ? 43 : $commAddress.hashCode());
+        Object $devices = this.getDevices();
+        result = result * 59 + ($devices == null ? 43 : $devices.hashCode());
         return result;
     }
 
     public String toString() {
         return "AlarmEventAction(alarmGrade=" + this.getAlarmGrade() + ", productCode=" + this.getProductCode()
-            + ", deviceId=" + this.getDeviceId() + ", deviceName=" + this.getDeviceName() + ", alarmType="
-            + this.getAlarmType() + ", alarmAttribute=" + this.getAlarmAttribute() + ", commAddress="
-            + this.getCommAddress() + ")";
+            + ", alarmType="
+            + this.getAlarmType() + ", alarmAttribute=" + this.getAlarmAttribute() + ", devices=" + this.getDevices() + ")";
     }
 }

+ 10 - 0
service-rule/service-rule-biz/src/main/java/com/usky/rule/vo/visualization/AlarmSimpleVO.java

@@ -0,0 +1,10 @@
+package com.usky.rule.vo.visualization;
+
+import lombok.Data;
+
+@Data
+public class AlarmSimpleVO {
+    private String deviceId;
+    private String deviceName;
+    private String commAddress;
+}