|
@@ -47,11 +47,12 @@ import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
|
@@ -47,11 +47,12 @@ import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
47
|
|
47
|
|
48
|
public class TbMsgGeneratorNode implements TbNode {
|
48
|
public class TbMsgGeneratorNode implements TbNode {
|
49
|
|
49
|
|
50
|
- public static final String TB_MSG_GENERATOR_NODE_MSG = "TbMsgGeneratorNodeMsg";
|
50
|
+ private static final String TB_MSG_GENERATOR_NODE_MSG = "TbMsgGeneratorNodeMsg";
|
51
|
|
51
|
|
52
|
private TbMsgGeneratorNodeConfiguration config;
|
52
|
private TbMsgGeneratorNodeConfiguration config;
|
53
|
private ScriptEngine jsEngine;
|
53
|
private ScriptEngine jsEngine;
|
54
|
private long delay;
|
54
|
private long delay;
|
|
|
55
|
+ private long lastScheduledTs;
|
55
|
private EntityId originatorId;
|
56
|
private EntityId originatorId;
|
56
|
private UUID nextTickId;
|
57
|
private UUID nextTickId;
|
57
|
private TbMsg prevMsg;
|
58
|
private TbMsg prevMsg;
|
|
@@ -66,28 +67,40 @@ public class TbMsgGeneratorNode implements TbNode { |
|
@@ -66,28 +67,40 @@ public class TbMsgGeneratorNode implements TbNode { |
66
|
originatorId = ctx.getSelfId();
|
67
|
originatorId = ctx.getSelfId();
|
67
|
}
|
68
|
}
|
68
|
this.jsEngine = ctx.createJsScriptEngine(config.getJsScript(), "prevMsg", "prevMetadata", "prevMsgType");
|
69
|
this.jsEngine = ctx.createJsScriptEngine(config.getJsScript(), "prevMsg", "prevMetadata", "prevMsgType");
|
69
|
- sentTickMsg(ctx);
|
70
|
+ scheduleTickMsg(ctx);
|
70
|
}
|
71
|
}
|
71
|
|
72
|
|
72
|
@Override
|
73
|
@Override
|
73
|
public void onMsg(TbContext ctx, TbMsg msg) {
|
74
|
public void onMsg(TbContext ctx, TbMsg msg) {
|
74
|
if (msg.getType().equals(TB_MSG_GENERATOR_NODE_MSG) && msg.getId().equals(nextTickId)) {
|
75
|
if (msg.getType().equals(TB_MSG_GENERATOR_NODE_MSG) && msg.getId().equals(nextTickId)) {
|
75
|
withCallback(generate(ctx),
|
76
|
withCallback(generate(ctx),
|
76
|
- m -> {ctx.tellNext(m, SUCCESS); sentTickMsg(ctx);},
|
|
|
77
|
- t -> {ctx.tellFailure(msg, t); sentTickMsg(ctx);});
|
77
|
+ m -> {
|
|
|
78
|
+ ctx.tellNext(m, SUCCESS);
|
|
|
79
|
+ scheduleTickMsg(ctx);
|
|
|
80
|
+ },
|
|
|
81
|
+ t -> {
|
|
|
82
|
+ ctx.tellFailure(msg, t);
|
|
|
83
|
+ scheduleTickMsg(ctx);
|
|
|
84
|
+ });
|
78
|
}
|
85
|
}
|
79
|
}
|
86
|
}
|
80
|
|
87
|
|
81
|
- private void sentTickMsg(TbContext ctx) {
|
88
|
+ private void scheduleTickMsg(TbContext ctx) {
|
|
|
89
|
+ long curTs = System.currentTimeMillis();
|
|
|
90
|
+ if (lastScheduledTs == 0L) {
|
|
|
91
|
+ lastScheduledTs = curTs;
|
|
|
92
|
+ }
|
|
|
93
|
+ lastScheduledTs = lastScheduledTs + delay;
|
|
|
94
|
+ long curDelay = Math.max(0L, (lastScheduledTs - curTs));
|
82
|
TbMsg tickMsg = ctx.newMsg(TB_MSG_GENERATOR_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), "");
|
95
|
TbMsg tickMsg = ctx.newMsg(TB_MSG_GENERATOR_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), "");
|
83
|
nextTickId = tickMsg.getId();
|
96
|
nextTickId = tickMsg.getId();
|
84
|
- ctx.tellSelf(tickMsg, delay);
|
97
|
+ ctx.tellSelf(tickMsg, curDelay);
|
85
|
}
|
98
|
}
|
86
|
|
99
|
|
87
|
private ListenableFuture<TbMsg> generate(TbContext ctx) {
|
100
|
private ListenableFuture<TbMsg> generate(TbContext ctx) {
|
88
|
return ctx.getJsExecutor().executeAsync(() -> {
|
101
|
return ctx.getJsExecutor().executeAsync(() -> {
|
89
|
if (prevMsg == null) {
|
102
|
if (prevMsg == null) {
|
90
|
- prevMsg = ctx.newMsg( "", originatorId, new TbMsgMetaData(), "{}");
|
103
|
+ prevMsg = ctx.newMsg("", originatorId, new TbMsgMetaData(), "{}");
|
91
|
}
|
104
|
}
|
92
|
TbMsg generated = jsEngine.executeGenerate(prevMsg);
|
105
|
TbMsg generated = jsEngine.executeGenerate(prevMsg);
|
93
|
prevMsg = ctx.newMsg(generated.getType(), originatorId, generated.getMetaData(), generated.getData());
|
106
|
prevMsg = ctx.newMsg(generated.getType(), originatorId, generated.getMetaData(), generated.getData());
|