Commit b3aaf66f3fd01eb053fe9402ed5ee967eb5d947b
Committed by
Andrew Shvayka
1 parent
29429cb0
created TbCheckAlarmStatusNode
Showing
2 changed files
with
130 additions
and
0 deletions
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.rule.engine.filter; | |
17 | + | |
18 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
19 | +import com.google.common.util.concurrent.FutureCallback; | |
20 | +import com.google.common.util.concurrent.Futures; | |
21 | +import com.google.common.util.concurrent.ListenableFuture; | |
22 | +import lombok.extern.slf4j.Slf4j; | |
23 | +import org.thingsboard.rule.engine.api.RuleNode; | |
24 | +import org.thingsboard.rule.engine.api.TbContext; | |
25 | +import org.thingsboard.rule.engine.api.TbNode; | |
26 | +import org.thingsboard.rule.engine.api.TbNodeConfiguration; | |
27 | +import org.thingsboard.rule.engine.api.TbNodeException; | |
28 | +import org.thingsboard.rule.engine.api.util.TbNodeUtils; | |
29 | +import org.thingsboard.server.common.data.alarm.Alarm; | |
30 | +import org.thingsboard.server.common.data.plugin.ComponentType; | |
31 | +import org.thingsboard.server.common.msg.TbMsg; | |
32 | + | |
33 | +import javax.annotation.Nullable; | |
34 | +import java.io.IOException; | |
35 | + | |
36 | +import static org.thingsboard.rule.engine.api.TbRelationTypes.FAILURE; | |
37 | +import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; | |
38 | + | |
39 | +@Slf4j | |
40 | +@RuleNode( | |
41 | + type = ComponentType.FILTER, | |
42 | + name = "checks alarm status", | |
43 | + configClazz = TbCheckAlarmStatusNodeConfig.class, | |
44 | + nodeDescription = "Checks alarm status.", | |
45 | + nodeDetails = "If the alarm status matches the specified one - msg is success if does not match - msg is failure.", | |
46 | + uiResources = {"static/rulenode/rulenode-core-config.js"}, | |
47 | + configDirective = "tbFilterNodeCheckAlarmStatusConfig") | |
48 | +public class TbCheckAlarmStatusNode implements TbNode { | |
49 | + private TbCheckAlarmStatusNodeConfig config; | |
50 | + private final ObjectMapper mapper = new ObjectMapper(); | |
51 | + | |
52 | + @Override | |
53 | + public void init(TbContext tbContext, TbNodeConfiguration configuration) throws TbNodeException { | |
54 | + this.config = TbNodeUtils.convert(configuration, TbCheckAlarmStatusNodeConfig.class); | |
55 | + } | |
56 | + | |
57 | + @Override | |
58 | + public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException { | |
59 | + try { | |
60 | + Alarm alarm = mapper.readValue(msg.getData(), Alarm.class); | |
61 | + | |
62 | + ListenableFuture<Alarm> latest = ctx.getAlarmService().findAlarmByIdAsync(ctx.getTenantId(), alarm.getId()); | |
63 | + | |
64 | + Futures.addCallback(latest, new FutureCallback<Alarm>() { | |
65 | + @Override | |
66 | + public void onSuccess(@Nullable Alarm result) { | |
67 | + boolean isPresent = false; | |
68 | + for (String alarmStatus : config.getAlarmStatusList()) { | |
69 | + if (alarm.getStatus().name().equals(alarmStatus)) { | |
70 | + isPresent = true; | |
71 | + break; | |
72 | + } | |
73 | + } | |
74 | + | |
75 | + if (isPresent) { | |
76 | + ctx.tellNext(msg, SUCCESS); | |
77 | + } else { | |
78 | + ctx.tellNext(msg, FAILURE); | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + @Override | |
83 | + public void onFailure(Throwable t) { | |
84 | + ctx.tellFailure(msg, t); | |
85 | + } | |
86 | + }); | |
87 | + } catch (IOException e) { | |
88 | + log.error("Failed to parse alarm: [{}]", msg.getData()); | |
89 | + throw new TbNodeException(e); | |
90 | + } | |
91 | + } | |
92 | + | |
93 | + @Override | |
94 | + public void destroy() { | |
95 | + } | |
96 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.rule.engine.filter; | |
17 | + | |
18 | +import lombok.Data; | |
19 | +import org.thingsboard.rule.engine.api.NodeConfiguration; | |
20 | + | |
21 | +import java.util.Collections; | |
22 | +import java.util.List; | |
23 | + | |
24 | +@Data | |
25 | +public class TbCheckAlarmStatusNodeConfig implements NodeConfiguration<TbCheckAlarmStatusNodeConfig> { | |
26 | + private List<String> alarmStatusList; | |
27 | + | |
28 | + @Override | |
29 | + public TbCheckAlarmStatusNodeConfig defaultConfiguration() { | |
30 | + TbCheckAlarmStatusNodeConfig config = new TbCheckAlarmStatusNodeConfig(); | |
31 | + config.setAlarmStatusList(Collections.emptyList()); | |
32 | + return config; | |
33 | + } | |
34 | +} | ... | ... |