30 changed files with 494 additions and 53 deletions
@ -0,0 +1,52 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.notification.rule.trigger; |
||||
|
|
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.alarm.Alarm; |
||||
|
import org.thingsboard.server.common.data.alarm.AlarmComment; |
||||
|
import org.thingsboard.server.common.data.notification.info.AlarmCommentNotificationInfo; |
||||
|
import org.thingsboard.server.common.data.notification.info.NotificationInfo; |
||||
|
import org.thingsboard.server.common.data.notification.rule.trigger.AlarmCommentNotificationRuleTriggerConfig; |
||||
|
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; |
||||
|
import org.thingsboard.server.common.msg.TbMsg; |
||||
|
|
||||
|
@Service |
||||
|
public class AlarmCommentTriggerProcessor implements NotificationRuleTriggerProcessor<TbMsg, AlarmCommentNotificationRuleTriggerConfig> { |
||||
|
|
||||
|
@Override |
||||
|
public boolean matchesFilter(TbMsg ruleEngineMsg, AlarmCommentNotificationRuleTriggerConfig triggerConfig) { |
||||
|
return ruleEngineMsg.getMetaData().getValue("comment") != null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public NotificationInfo constructNotificationInfo(TbMsg ruleEngineMsg, AlarmCommentNotificationRuleTriggerConfig triggerConfig) { |
||||
|
AlarmComment comment = JacksonUtil.fromString(ruleEngineMsg.getMetaData().getValue("comment"), AlarmComment.class); |
||||
|
Alarm alarm = JacksonUtil.fromString(ruleEngineMsg.getData(), Alarm.class); |
||||
|
return AlarmCommentNotificationInfo.builder() |
||||
|
.comment(comment.getComment().get("text").asText()) |
||||
|
.alarmType(alarm.getType()) |
||||
|
.alarmId(comment.getAlarmId().getId()) |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public NotificationRuleTriggerType getTriggerType() { |
||||
|
return NotificationRuleTriggerType.ALARM_COMMENT; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,97 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.notification.rule.trigger; |
||||
|
|
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import org.apache.commons.collections.CollectionUtils; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.id.EntityId; |
||||
|
import org.thingsboard.server.common.data.id.RuleChainId; |
||||
|
import org.thingsboard.server.common.data.notification.info.NotificationInfo; |
||||
|
import org.thingsboard.server.common.data.notification.info.RuleEngineComponentLifecycleEventNotificationInfo; |
||||
|
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; |
||||
|
import org.thingsboard.server.common.data.notification.rule.trigger.RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig; |
||||
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
||||
|
import org.thingsboard.server.service.notification.rule.trigger.RuleEngineComponentLifecycleEventTriggerProcessor.RuleEngineComponentLifecycleEventTriggerObject; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Service |
||||
|
public class RuleEngineComponentLifecycleEventTriggerProcessor implements NotificationRuleTriggerProcessor<RuleEngineComponentLifecycleEventTriggerObject, RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig> { |
||||
|
|
||||
|
@Override |
||||
|
public boolean matchesFilter(RuleEngineComponentLifecycleEventTriggerObject triggerObject, RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig triggerConfig) { |
||||
|
if (CollectionUtils.isNotEmpty(triggerConfig.getRuleChains())) { |
||||
|
if (!triggerConfig.getRuleChains().contains(triggerObject.getRuleChainId().getId())) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
EntityType componentType = triggerObject.getComponentId().getEntityType(); |
||||
|
Set<ComponentLifecycleEvent> trackedEvents; |
||||
|
boolean onlyFailures; |
||||
|
if (componentType == EntityType.RULE_CHAIN) { |
||||
|
trackedEvents = triggerConfig.getRuleChainEvents(); |
||||
|
onlyFailures = triggerConfig.isOnlyRuleChainLifecycleFailures(); |
||||
|
} else if (componentType == EntityType.RULE_NODE && triggerConfig.isTrackRuleNodeEvents()) { |
||||
|
trackedEvents = triggerConfig.getRuleNodeEvents(); |
||||
|
onlyFailures = triggerConfig.isOnlyRuleNodeLifecycleFailures(); |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
if (CollectionUtils.isEmpty(trackedEvents)) { |
||||
|
trackedEvents = Set.of(ComponentLifecycleEvent.STARTED, ComponentLifecycleEvent.UPDATED, ComponentLifecycleEvent.STOPPED); |
||||
|
} |
||||
|
|
||||
|
if (!trackedEvents.contains(triggerObject.getEventType())) { |
||||
|
return false; |
||||
|
} |
||||
|
if (onlyFailures) { |
||||
|
return triggerObject.getError() != null; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public NotificationInfo constructNotificationInfo(RuleEngineComponentLifecycleEventTriggerObject triggerObject, RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig triggerConfig) { |
||||
|
return RuleEngineComponentLifecycleEventNotificationInfo.builder() |
||||
|
.ruleChainId(triggerObject.getRuleChainId()) |
||||
|
.componentId(triggerObject.getComponentId()) |
||||
|
.componentName(triggerObject.getComponentName()) |
||||
|
.eventType(triggerObject.getEventType()) |
||||
|
.error(Optional.ofNullable(triggerObject.getError()).map(Throwable::getMessage).orElse(null)) |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public NotificationRuleTriggerType getTriggerType() { |
||||
|
return NotificationRuleTriggerType.RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT; |
||||
|
} |
||||
|
|
||||
|
@Data |
||||
|
@Builder |
||||
|
public static class RuleEngineComponentLifecycleEventTriggerObject { |
||||
|
private final RuleChainId ruleChainId; |
||||
|
private final EntityId componentId; |
||||
|
private final String componentName; |
||||
|
private final ComponentLifecycleEvent eventType; |
||||
|
private final Exception error; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.info; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Builder |
||||
|
public class AlarmCommentNotificationInfo implements NotificationInfo { |
||||
|
|
||||
|
private String comment; |
||||
|
private String alarmType; |
||||
|
private UUID alarmId; |
||||
|
|
||||
|
@Override |
||||
|
public Map<String, String> getTemplateData() { |
||||
|
return Map.of( |
||||
|
"comment", comment, |
||||
|
"alarmType", alarmType, |
||||
|
"alarmId", alarmId.toString() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.info; |
||||
|
|
||||
|
import com.google.common.base.Strings; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
import org.thingsboard.server.common.data.id.EntityId; |
||||
|
import org.thingsboard.server.common.data.id.RuleChainId; |
||||
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Builder |
||||
|
public class RuleEngineComponentLifecycleEventNotificationInfo implements NotificationInfo { |
||||
|
|
||||
|
private RuleChainId ruleChainId; |
||||
|
private EntityId componentId; |
||||
|
private String componentName; |
||||
|
private ComponentLifecycleEvent eventType; |
||||
|
private String error; |
||||
|
// TODO: add rule chain name
|
||||
|
|
||||
|
@Override |
||||
|
public Map<String, String> getTemplateData() { |
||||
|
return Map.of( |
||||
|
"ruleChainId", ruleChainId.toString(), |
||||
|
"componentId", componentId.toString(), |
||||
|
"componentType", componentId.getEntityType().name(), |
||||
|
"componentName", componentName, |
||||
|
"eventType", eventType.name(), |
||||
|
"error", Strings.nullToEmpty(error) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.rule.trigger; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class AlarmCommentNotificationRuleTriggerConfig implements NotificationRuleTriggerConfig { |
||||
|
|
||||
|
@Override |
||||
|
public NotificationRuleTriggerType getTriggerType() { |
||||
|
return NotificationRuleTriggerType.ALARM_COMMENT; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.rule.trigger; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
||||
|
|
||||
|
import java.util.Set; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Data |
||||
|
public class RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig implements NotificationRuleTriggerConfig { |
||||
|
|
||||
|
private Set<ComponentLifecycleEvent> ruleChainEvents; // available options: STARTED, UPDATED, STOPPED. if empty - all events
|
||||
|
private boolean onlyRuleChainLifecycleFailures; |
||||
|
|
||||
|
private boolean trackRuleNodeEvents; |
||||
|
private Set<ComponentLifecycleEvent> ruleNodeEvents; // available options: STARTED, UPDATED, STOPPED. if empty - all events
|
||||
|
private boolean onlyRuleNodeLifecycleFailures; |
||||
|
|
||||
|
private Set<UUID> ruleChains; // if empty - all rule chains
|
||||
|
|
||||
|
@Override |
||||
|
public NotificationRuleTriggerType getTriggerType() { |
||||
|
return NotificationRuleTriggerType.RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue