add options send notifications

This commit is contained in:
Yuriy Lytvynchuk 2022-11-22 14:33:52 +02:00
parent d8f87c45c8
commit 29d3efa205
4 changed files with 19 additions and 3 deletions

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.rule.engine.telemetry; package org.thingsboard.rule.engine.telemetry;
import com.google.common.util.concurrent.FutureCallback;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
@ -65,11 +66,15 @@ public class TbMsgAttributesNode implements TbNode {
return; return;
} }
String src = msg.getData(); String src = msg.getData();
List<AttributeKvEntry> attributes = new ArrayList<>(JsonConverter.convertToAttributes(new JsonParser().parse(src))); List<AttributeKvEntry> attributes = new ArrayList<>(JsonConverter.convertToAttributes(JsonParser.parseString(src)));
if (attributes.isEmpty()) { if (attributes.isEmpty()) {
ctx.tellSuccess(msg); ctx.tellSuccess(msg);
return; return;
} }
FutureCallback<Void> callback = new TelemetryNodeCallback(ctx, msg);
if (config.isSendAttributesUpdatedNotification()) {
callback = new AttributesUpdateNodeCallback(ctx, msg, config.getScope(), attributes);
}
String notifyDeviceStr = msg.getMetaData().getValue("notifyDevice"); String notifyDeviceStr = msg.getMetaData().getValue("notifyDevice");
ctx.getTelemetryService().saveAndNotify( ctx.getTelemetryService().saveAndNotify(
ctx.getTenantId(), ctx.getTenantId(),
@ -77,7 +82,7 @@ public class TbMsgAttributesNode implements TbNode {
config.getScope(), config.getScope(),
attributes, attributes,
config.getNotifyDevice() || StringUtils.isEmpty(notifyDeviceStr) || Boolean.parseBoolean(notifyDeviceStr), config.getNotifyDevice() || StringUtils.isEmpty(notifyDeviceStr) || Boolean.parseBoolean(notifyDeviceStr),
new AttributesUpdateNodeCallback(ctx, msg, config.getScope(), attributes) callback
); );
} }

View File

@ -25,12 +25,14 @@ public class TbMsgAttributesNodeConfiguration implements NodeConfiguration<TbMsg
private String scope; private String scope;
private Boolean notifyDevice; private Boolean notifyDevice;
private boolean sendAttributesUpdatedNotification;
@Override @Override
public TbMsgAttributesNodeConfiguration defaultConfiguration() { public TbMsgAttributesNodeConfiguration defaultConfiguration() {
TbMsgAttributesNodeConfiguration configuration = new TbMsgAttributesNodeConfiguration(); TbMsgAttributesNodeConfiguration configuration = new TbMsgAttributesNodeConfiguration();
configuration.setScope(DataConstants.SERVER_SCOPE); configuration.setScope(DataConstants.SERVER_SCOPE);
configuration.setNotifyDevice(false); configuration.setNotifyDevice(false);
configuration.setSendAttributesUpdatedNotification(false);
return configuration; return configuration;
} }
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.rule.engine.telemetry; package org.thingsboard.rule.engine.telemetry;
import com.google.common.util.concurrent.FutureCallback;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thingsboard.rule.engine.api.RuleNode; import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
@ -49,12 +50,14 @@ public class TbMsgDeleteAttributes implements TbNode {
private TbMsgDeleteAttributesConfiguration config; private TbMsgDeleteAttributesConfiguration config;
private String scope; private String scope;
private List<String> keys; private List<String> keys;
private boolean sendAttributesDeletedNotification;
@Override @Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbMsgDeleteAttributesConfiguration.class); this.config = TbNodeUtils.convert(configuration, TbMsgDeleteAttributesConfiguration.class);
this.scope = config.getScope(); this.scope = config.getScope();
this.keys = config.getKeys(); this.keys = config.getKeys();
this.sendAttributesDeletedNotification = config.isSendAttributesDeletedNotification();
} }
@Override @Override
@ -67,7 +70,11 @@ public class TbMsgDeleteAttributes implements TbNode {
if (keysToDelete.isEmpty()) { if (keysToDelete.isEmpty()) {
ctx.tellSuccess(msg); ctx.tellSuccess(msg);
} else { } else {
ctx.getTelemetryService().deleteAndNotify(ctx.getTenantId(), msg.getOriginator(), scope, keysToDelete, new AttributesDeleteNodeCallback(ctx, msg, scope, keysToDelete)); FutureCallback<Void> callback = new TelemetryNodeCallback(ctx, msg);
if (sendAttributesDeletedNotification) {
callback = new AttributesDeleteNodeCallback(ctx, msg, scope, keysToDelete);
}
ctx.getTelemetryService().deleteAndNotify(ctx.getTenantId(), msg.getOriginator(), scope, keysToDelete, callback);
} }
} }
} }

View File

@ -27,12 +27,14 @@ public class TbMsgDeleteAttributesConfiguration implements NodeConfiguration<TbM
private String scope; private String scope;
private List<String> keys; private List<String> keys;
private boolean sendAttributesDeletedNotification;
@Override @Override
public TbMsgDeleteAttributesConfiguration defaultConfiguration() { public TbMsgDeleteAttributesConfiguration defaultConfiguration() {
TbMsgDeleteAttributesConfiguration configuration = new TbMsgDeleteAttributesConfiguration(); TbMsgDeleteAttributesConfiguration configuration = new TbMsgDeleteAttributesConfiguration();
configuration.setScope(DataConstants.SERVER_SCOPE); configuration.setScope(DataConstants.SERVER_SCOPE);
configuration.setKeys(Collections.emptyList()); configuration.setKeys(Collections.emptyList());
configuration.setSendAttributesDeletedNotification(false);
return configuration; return configuration;
} }
} }