|
|
|
@ -17,6 +17,9 @@ package org.thingsboard.server.actors.ruleChain; |
|
|
|
|
|
|
|
import akka.actor.ActorRef; |
|
|
|
import com.datastax.driver.core.utils.UUIDs; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import io.netty.channel.EventLoopGroup; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.rule.engine.api.ListeningExecutor; |
|
|
|
@ -30,6 +33,11 @@ import org.thingsboard.rule.engine.api.ScriptEngine; |
|
|
|
import org.thingsboard.rule.engine.api.TbContext; |
|
|
|
import org.thingsboard.rule.engine.api.TbRelationTypes; |
|
|
|
import org.thingsboard.server.actors.ActorSystemContext; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.DataConstants; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.alarm.Alarm; |
|
|
|
import org.thingsboard.server.common.data.asset.Asset; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.RuleNodeId; |
|
|
|
@ -38,9 +46,11 @@ import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody; |
|
|
|
import org.thingsboard.server.common.data.rule.RuleNode; |
|
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
|
import org.thingsboard.server.common.msg.cluster.SendToClusterMsg; |
|
|
|
import org.thingsboard.server.common.msg.cluster.ServerAddress; |
|
|
|
import org.thingsboard.server.common.msg.cluster.ServerType; |
|
|
|
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; |
|
|
|
import org.thingsboard.server.common.msg.system.ServiceToRuleEngineMsg; |
|
|
|
import org.thingsboard.server.dao.alarm.AlarmService; |
|
|
|
import org.thingsboard.server.dao.asset.AssetService; |
|
|
|
import org.thingsboard.server.dao.attributes.AttributesService; |
|
|
|
@ -69,6 +79,8 @@ import java.util.function.Consumer; |
|
|
|
*/ |
|
|
|
class DefaultTbContext implements TbContext { |
|
|
|
|
|
|
|
public final static ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
|
|
|
private final ActorSystemContext mainCtx; |
|
|
|
private final RuleNodeCtx nodeCtx; |
|
|
|
|
|
|
|
@ -138,6 +150,48 @@ class DefaultTbContext implements TbContext { |
|
|
|
return new TbMsg(origMsg.getId(), type, originator, metaData.copy(), origMsg.getDataType(), data, origMsg.getTransactionData(), origMsg.getRuleChainId(), origMsg.getRuleNodeId(), mainCtx.getQueuePartitionId()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void sendTbMsgToRuleEngine(TbMsg msg) { |
|
|
|
mainCtx.getActorService().onMsg(new SendToClusterMsg(msg.getOriginator(), new ServiceToRuleEngineMsg(getTenantId(), msg))); |
|
|
|
} |
|
|
|
|
|
|
|
public TbMsg customerCreatedMsg(Customer customer, RuleNodeId ruleNodeId) { |
|
|
|
try { |
|
|
|
ObjectNode entityNode = mapper.valueToTree(customer); |
|
|
|
return new TbMsg(UUIDs.timeBased(), DataConstants.ENTITY_CREATED, customer.getId(), getActionMetaData(ruleNodeId), mapper.writeValueAsString(entityNode), null, null, 0L); |
|
|
|
} catch (JsonProcessingException | IllegalArgumentException e) { |
|
|
|
throw new RuntimeException("Failed to process customer created msg: " + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public TbMsg deviceCreatedMsg(Device device, RuleNodeId ruleNodeId) { |
|
|
|
try { |
|
|
|
ObjectNode entityNode = mapper.valueToTree(device); |
|
|
|
return new TbMsg(UUIDs.timeBased(), DataConstants.ENTITY_CREATED, device.getId(), getActionMetaData(ruleNodeId), mapper.writeValueAsString(entityNode), null, null, 0L); |
|
|
|
} catch (JsonProcessingException | IllegalArgumentException e) { |
|
|
|
throw new RuntimeException("Failed to process device created msg: " + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId) { |
|
|
|
try { |
|
|
|
ObjectNode entityNode = mapper.valueToTree(asset); |
|
|
|
return new TbMsg(UUIDs.timeBased(), DataConstants.ENTITY_CREATED, asset.getId(), getActionMetaData(ruleNodeId), mapper.writeValueAsString(entityNode), null, null, 0L); |
|
|
|
} catch (JsonProcessingException | IllegalArgumentException e) { |
|
|
|
throw new RuntimeException("Failed to process asset created msg: " + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public TbMsg alarmCreatedMsg(Alarm alarm, RuleNodeId ruleNodeId) { |
|
|
|
try { |
|
|
|
ObjectNode entityNode = mapper.valueToTree(alarm); |
|
|
|
return new TbMsg(UUIDs.timeBased(), DataConstants.ENTITY_CREATED, alarm.getId(), getActionMetaData(ruleNodeId), mapper.writeValueAsString(entityNode), null, null, 0L); |
|
|
|
} catch (JsonProcessingException | IllegalArgumentException e) { |
|
|
|
throw new RuntimeException("Failed to process alarm created msg: " + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public RuleNodeId getSelfId() { |
|
|
|
return nodeCtx.getSelf().getId(); |
|
|
|
@ -305,4 +359,10 @@ class DefaultTbContext implements TbContext { |
|
|
|
return mainCtx.getCassandraBufferedRateExecutor(); |
|
|
|
} |
|
|
|
|
|
|
|
private TbMsgMetaData getActionMetaData(RuleNodeId ruleNodeId) { |
|
|
|
TbMsgMetaData metaData = new TbMsgMetaData(); |
|
|
|
metaData.putValue("ruleNodeId", ruleNodeId.toString()); |
|
|
|
return metaData; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|