85 changed files with 1338 additions and 470 deletions
@ -0,0 +1,145 @@ |
|||
{ |
|||
"ruleChain": { |
|||
"additionalInfo": null, |
|||
"name": "Edge Root Rule Chain", |
|||
"type": "EDGE", |
|||
"firstRuleNodeId": null, |
|||
"root": true, |
|||
"debugMode": false, |
|||
"configuration": null, |
|||
"assignedEdges": [], |
|||
"assignedEdgesIds": [] |
|||
}, |
|||
"metadata": { |
|||
"firstNodeIndex": 2, |
|||
"nodes": [ |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 823, |
|||
"layoutY": 157 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", |
|||
"name": "Save Timeseries", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"defaultTTL": 0 |
|||
} |
|||
}, |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 824, |
|||
"layoutY": 52 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", |
|||
"name": "Save Client Attributes", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"scope": "CLIENT_SCOPE" |
|||
} |
|||
}, |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 347, |
|||
"layoutY": 149 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", |
|||
"name": "Message Type Switch", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"version": 0 |
|||
} |
|||
}, |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 825, |
|||
"layoutY": 266 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.action.TbLogNode", |
|||
"name": "Log RPC from Device", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);" |
|||
} |
|||
}, |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 824, |
|||
"layoutY": 378 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.action.TbLogNode", |
|||
"name": "Log Other", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);" |
|||
} |
|||
}, |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 824, |
|||
"layoutY": 466 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", |
|||
"name": "RPC Call Request", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"timeoutInSeconds": 60 |
|||
} |
|||
}, |
|||
{ |
|||
"additionalInfo": { |
|||
"layoutX": 1134, |
|||
"layoutY": 132 |
|||
}, |
|||
"type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", |
|||
"name": "Push to cloud", |
|||
"debugMode": false, |
|||
"configuration": { |
|||
"version": 0 |
|||
} |
|||
} |
|||
], |
|||
"connections": [ |
|||
{ |
|||
"fromIndex": 0, |
|||
"toIndex": 6, |
|||
"type": "Success" |
|||
}, |
|||
{ |
|||
"fromIndex": 1, |
|||
"toIndex": 6, |
|||
"type": "Success" |
|||
}, |
|||
{ |
|||
"fromIndex": 2, |
|||
"toIndex": 4, |
|||
"type": "Other" |
|||
}, |
|||
{ |
|||
"fromIndex": 2, |
|||
"toIndex": 1, |
|||
"type": "Post attributes" |
|||
}, |
|||
{ |
|||
"fromIndex": 2, |
|||
"toIndex": 0, |
|||
"type": "Post telemetry" |
|||
}, |
|||
{ |
|||
"fromIndex": 2, |
|||
"toIndex": 3, |
|||
"type": "RPC Request from Device" |
|||
}, |
|||
{ |
|||
"fromIndex": 2, |
|||
"toIndex": 5, |
|||
"type": "RPC Request to Device" |
|||
}, |
|||
{ |
|||
"fromIndex": 3, |
|||
"toIndex": 6, |
|||
"type": "Success" |
|||
} |
|||
], |
|||
"ruleChainConnections": null |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.constructor; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.bouncycastle.jcajce.provider.symmetric.DES; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.alarm.Alarm; |
|||
import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EntityViewId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.asset.AssetService; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
import org.thingsboard.server.dao.entityview.EntityViewService; |
|||
import org.thingsboard.server.dao.util.mapping.JacksonUtil; |
|||
import org.thingsboard.server.gen.edge.AlarmUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class AlarmUpdateMsgConstructor { |
|||
|
|||
@Autowired |
|||
private DeviceService deviceService; |
|||
|
|||
@Autowired |
|||
private AssetService assetService; |
|||
|
|||
@Autowired |
|||
private EntityViewService entityViewService; |
|||
|
|||
public AlarmUpdateMsg constructAlarmUpdatedMsg(TenantId tenantId, UpdateMsgType msgType, Alarm alarm) { |
|||
String entityName = null; |
|||
switch (alarm.getOriginator().getEntityType()) { |
|||
case DEVICE: |
|||
entityName = deviceService.findDeviceById(tenantId, new DeviceId(alarm.getOriginator().getId())).getName(); |
|||
break; |
|||
case ASSET: |
|||
entityName = assetService.findAssetById(tenantId, new AssetId(alarm.getOriginator().getId())).getName(); |
|||
break; |
|||
case ENTITY_VIEW: |
|||
entityName = entityViewService.findEntityViewById(tenantId, new EntityViewId(alarm.getOriginator().getId())).getName(); |
|||
break; |
|||
} |
|||
AlarmUpdateMsg.Builder builder = AlarmUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setName(alarm.getName()) |
|||
.setType(alarm.getType()) |
|||
.setOriginatorName(entityName) |
|||
.setOriginatorType(alarm.getOriginator().getEntityType().name()) |
|||
.setSeverity(alarm.getSeverity().name()) |
|||
.setStatus(alarm.getStatus().name()) |
|||
.setStartTs(alarm.getStartTs()) |
|||
.setEndTs(alarm.getEndTs()) |
|||
.setAckTs(alarm.getAckTs()) |
|||
.setClearTs(alarm.getClearTs()) |
|||
.setDetails(JacksonUtil.toString(alarm.getDetails())) |
|||
.setPropagate(alarm.isPropagate()); |
|||
return builder.build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.constructor; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.gen.edge.AssetUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class AssetUpdateMsgConstructor { |
|||
|
|||
public AssetUpdateMsg constructAssetUpdatedMsg(UpdateMsgType msgType, Asset asset) { |
|||
AssetUpdateMsg.Builder builder = AssetUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setName(asset.getName()) |
|||
.setType(asset.getType()); |
|||
return builder.build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.constructor; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.Dashboard; |
|||
import org.thingsboard.server.dao.dashboard.DashboardService; |
|||
import org.thingsboard.server.dao.util.mapping.JacksonUtil; |
|||
import org.thingsboard.server.gen.edge.DashboardUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class DashboardUpdateMsgConstructor { |
|||
|
|||
@Autowired |
|||
private DashboardService dashboardService; |
|||
|
|||
public DashboardUpdateMsg constructDashboardUpdatedMsg(UpdateMsgType msgType, Dashboard dashboard) { |
|||
dashboard = dashboardService.findDashboardById(dashboard.getTenantId(), dashboard.getId()); |
|||
DashboardUpdateMsg.Builder builder = DashboardUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setIdMSB(dashboard.getId().getId().getMostSignificantBits()) |
|||
.setIdLSB(dashboard.getId().getId().getLeastSignificantBits()) |
|||
.setTitle(dashboard.getTitle()) |
|||
.setConfiguration(JacksonUtil.toString(dashboard.getConfiguration())); |
|||
return builder.build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.constructor; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.gen.edge.DeviceUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class DeviceUpdateMsgConstructor { |
|||
|
|||
public DeviceUpdateMsg constructDeviceUpdatedMsg(UpdateMsgType msgType, Device device) { |
|||
DeviceUpdateMsg.Builder builder = DeviceUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setName(device.getName()) |
|||
.setType(device.getType()); |
|||
return builder.build(); |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.constructor; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.EntityView; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.dao.asset.AssetService; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
import org.thingsboard.server.gen.edge.EntityViewUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class EntityViewUpdateMsgConstructor { |
|||
|
|||
@Autowired |
|||
private DeviceService deviceService; |
|||
|
|||
@Autowired |
|||
private AssetService assetService; |
|||
|
|||
public EntityViewUpdateMsg constructEntityViewUpdatedMsg(UpdateMsgType msgType, EntityView entityView) { |
|||
String relatedName; |
|||
String relatedType; |
|||
org.thingsboard.server.gen.edge.EntityType relatedEntityType; |
|||
if (entityView.getEntityId().getEntityType().equals(EntityType.DEVICE)) { |
|||
Device device = deviceService.findDeviceById(entityView.getTenantId(), new DeviceId(entityView.getEntityId().getId())); |
|||
relatedName = device.getName(); |
|||
relatedType = device.getType(); |
|||
relatedEntityType = org.thingsboard.server.gen.edge.EntityType.DEVICE; |
|||
} else { |
|||
Asset asset = assetService.findAssetById(entityView.getTenantId(), new AssetId(entityView.getEntityId().getId())); |
|||
relatedName = asset.getName(); |
|||
relatedType = asset.getType(); |
|||
relatedEntityType = org.thingsboard.server.gen.edge.EntityType.ASSET; |
|||
} |
|||
EntityViewUpdateMsg.Builder builder = EntityViewUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setName(entityView.getName()) |
|||
.setType(entityView.getType()) |
|||
.setRelatedName(relatedName) |
|||
.setRelatedType(relatedType) |
|||
.setRelatedEntityType(relatedEntityType); |
|||
return builder.build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,141 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.constructor; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.rule.NodeConnectionInfo; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainConnectionInfo; |
|||
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|||
import org.thingsboard.server.common.data.rule.RuleNode; |
|||
import org.thingsboard.server.dao.util.mapping.JacksonUtil; |
|||
import org.thingsboard.server.gen.edge.NodeConnectionInfoProto; |
|||
import org.thingsboard.server.gen.edge.RuleChainConnectionInfoProto; |
|||
import org.thingsboard.server.gen.edge.RuleChainMetadataUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.RuleChainUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.RuleNodeProto; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class RuleChainUpdateMsgConstructor { |
|||
|
|||
private static final ObjectMapper objectMapper = new ObjectMapper(); |
|||
|
|||
public RuleChainUpdateMsg constructRuleChainUpdatedMsg(Edge edge, UpdateMsgType msgType, RuleChain ruleChain) { |
|||
RuleChainUpdateMsg.Builder builder = RuleChainUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setIdMSB(ruleChain.getId().getId().getMostSignificantBits()) |
|||
.setIdLSB(ruleChain.getId().getId().getLeastSignificantBits()) |
|||
.setName(ruleChain.getName()) |
|||
.setRoot(ruleChain.getId().equals(edge.getRootRuleChainId())) |
|||
.setDebugMode(ruleChain.isDebugMode()) |
|||
.setConfiguration(JacksonUtil.toString(ruleChain.getConfiguration())); |
|||
if (ruleChain.getFirstRuleNodeId() != null) { |
|||
builder.setFirstRuleNodeIdMSB(ruleChain.getFirstRuleNodeId().getId().getMostSignificantBits()) |
|||
.setFirstRuleNodeIdLSB(ruleChain.getFirstRuleNodeId().getId().getLeastSignificantBits()); |
|||
} |
|||
return builder.build(); |
|||
} |
|||
|
|||
public RuleChainMetadataUpdateMsg constructRuleChainMetadataUpdatedMsg(UpdateMsgType msgType, RuleChainMetaData ruleChainMetaData) { |
|||
try { |
|||
RuleChainMetadataUpdateMsg.Builder builder = RuleChainMetadataUpdateMsg.newBuilder() |
|||
.setRuleChainIdMSB(ruleChainMetaData.getRuleChainId().getId().getMostSignificantBits()) |
|||
.setRuleChainIdLSB(ruleChainMetaData.getRuleChainId().getId().getLeastSignificantBits()) |
|||
.addAllNodes(constructNodes(ruleChainMetaData.getNodes())) |
|||
.addAllConnections(constructConnections(ruleChainMetaData.getConnections())) |
|||
.addAllRuleChainConnections(constructRuleChainConnections(ruleChainMetaData.getRuleChainConnections())); |
|||
if (ruleChainMetaData.getFirstNodeIndex() != null) { |
|||
builder.setFirstNodeIndex(ruleChainMetaData.getFirstNodeIndex()); |
|||
} |
|||
builder.setMsgType(msgType); |
|||
return builder.build(); |
|||
} catch (JsonProcessingException ex) { |
|||
log.error("Can't construct RuleChainMetadataUpdateMsg", ex); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
private List<NodeConnectionInfoProto> constructConnections(List<NodeConnectionInfo> connections) { |
|||
List<NodeConnectionInfoProto> result = new ArrayList<>(); |
|||
if (connections != null && !connections.isEmpty()) { |
|||
for (NodeConnectionInfo connection : connections) { |
|||
result.add(constructConnection(connection)); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
private NodeConnectionInfoProto constructConnection(NodeConnectionInfo connection) { |
|||
return NodeConnectionInfoProto.newBuilder() |
|||
.setFromIndex(connection.getFromIndex()) |
|||
.setToIndex(connection.getToIndex()) |
|||
.setType(connection.getType()) |
|||
.build(); |
|||
} |
|||
|
|||
private List<RuleNodeProto> constructNodes(List<RuleNode> nodes) throws JsonProcessingException { |
|||
List<RuleNodeProto> result = new ArrayList<>(); |
|||
if (nodes != null && !nodes.isEmpty()) { |
|||
for (RuleNode node : nodes) { |
|||
result.add(constructNode(node)); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
private List<RuleChainConnectionInfoProto> constructRuleChainConnections(List<RuleChainConnectionInfo> ruleChainConnections) throws JsonProcessingException { |
|||
List<RuleChainConnectionInfoProto> result = new ArrayList<>(); |
|||
if (ruleChainConnections != null && !ruleChainConnections.isEmpty()) { |
|||
for (RuleChainConnectionInfo ruleChainConnectionInfo : ruleChainConnections) { |
|||
result.add(constructRuleChainConnection(ruleChainConnectionInfo)); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
private RuleChainConnectionInfoProto constructRuleChainConnection(RuleChainConnectionInfo ruleChainConnectionInfo) throws JsonProcessingException { |
|||
return RuleChainConnectionInfoProto.newBuilder() |
|||
.setFromIndex(ruleChainConnectionInfo.getFromIndex()) |
|||
.setTargetRuleChainIdMSB(ruleChainConnectionInfo.getTargetRuleChainId().getId().getMostSignificantBits()) |
|||
.setTargetRuleChainIdLSB(ruleChainConnectionInfo.getTargetRuleChainId().getId().getLeastSignificantBits()) |
|||
.setType(ruleChainConnectionInfo.getType()) |
|||
.setAdditionalInfo(objectMapper.writeValueAsString(ruleChainConnectionInfo.getAdditionalInfo())) |
|||
.build(); |
|||
} |
|||
|
|||
|
|||
private RuleNodeProto constructNode(RuleNode node) throws JsonProcessingException { |
|||
return RuleNodeProto.newBuilder() |
|||
.setIdMSB(node.getId().getId().getMostSignificantBits()) |
|||
.setIdLSB(node.getId().getId().getLeastSignificantBits()) |
|||
.setType(node.getType()) |
|||
.setName(node.getName()) |
|||
.setDebugMode(node.isDebugMode()) |
|||
.setConfiguration(objectMapper.writeValueAsString(node.getConfiguration())) |
|||
.setAdditionalInfo(objectMapper.writeValueAsString(node.getAdditionalInfo())) |
|||
.build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,272 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.init; |
|||
|
|||
import io.grpc.stub.StreamObserver; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.Dashboard; |
|||
import org.thingsboard.server.common.data.DashboardInfo; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EntityView; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.page.TextPageData; |
|||
import org.thingsboard.server.common.data.page.TextPageLink; |
|||
import org.thingsboard.server.common.data.page.TimePageData; |
|||
import org.thingsboard.server.common.data.page.TimePageLink; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|||
import org.thingsboard.server.dao.asset.AssetService; |
|||
import org.thingsboard.server.dao.dashboard.DashboardService; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
import org.thingsboard.server.dao.entityview.EntityViewService; |
|||
import org.thingsboard.server.dao.rule.RuleChainService; |
|||
import org.thingsboard.server.gen.edge.AssetUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.DashboardUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.DeviceUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.EntityUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.EntityViewUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.ResponseMsg; |
|||
import org.thingsboard.server.gen.edge.RuleChainMetadataRequestMsg; |
|||
import org.thingsboard.server.gen.edge.RuleChainMetadataUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.RuleChainUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.UpdateMsgType; |
|||
import org.thingsboard.server.service.edge.rpc.constructor.AssetUpdateMsgConstructor; |
|||
import org.thingsboard.server.service.edge.rpc.constructor.DashboardUpdateMsgConstructor; |
|||
import org.thingsboard.server.service.edge.rpc.constructor.DeviceUpdateMsgConstructor; |
|||
import org.thingsboard.server.service.edge.rpc.constructor.EntityViewUpdateMsgConstructor; |
|||
import org.thingsboard.server.service.edge.rpc.constructor.RuleChainUpdateMsgConstructor; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class DefaultInitEdgeService implements InitEdgeService { |
|||
|
|||
@Autowired |
|||
private RuleChainService ruleChainService; |
|||
|
|||
@Autowired |
|||
private DeviceService deviceService; |
|||
|
|||
@Autowired |
|||
private AssetService assetService; |
|||
|
|||
@Autowired |
|||
private EntityViewService entityViewService; |
|||
|
|||
@Autowired |
|||
private DashboardService dashboardService; |
|||
|
|||
@Autowired |
|||
private RuleChainUpdateMsgConstructor ruleChainUpdateMsgConstructor; |
|||
|
|||
@Autowired |
|||
private DeviceUpdateMsgConstructor deviceUpdateMsgConstructor; |
|||
|
|||
@Autowired |
|||
private AssetUpdateMsgConstructor assetUpdateMsgConstructor; |
|||
|
|||
@Autowired |
|||
private EntityViewUpdateMsgConstructor entityViewUpdateMsgConstructor; |
|||
|
|||
@Autowired |
|||
private DashboardUpdateMsgConstructor dashboardUpdateMsgConstructor; |
|||
|
|||
@Override |
|||
public void init(Edge edge, StreamObserver<ResponseMsg> outputStream) { |
|||
initRuleChains(edge, outputStream); |
|||
initDevices(edge, outputStream); |
|||
initAssets(edge, outputStream); |
|||
initEntityViews(edge, outputStream); |
|||
initDashboards(edge, outputStream); |
|||
} |
|||
|
|||
private void initDevices(Edge edge, StreamObserver<ResponseMsg> outputStream) { |
|||
try { |
|||
TextPageLink pageLink = new TextPageLink(100); |
|||
TextPageData<Device> pageData; |
|||
do { |
|||
pageData = deviceService.findDevicesByTenantIdAndEdgeId(edge.getTenantId(), edge.getId(), pageLink); |
|||
if (!pageData.getData().isEmpty()) { |
|||
log.trace("[{}] [{}] device(s) are going to be pushed to edge.", edge.getId(), pageData.getData().size()); |
|||
for (Device device : pageData.getData()) { |
|||
DeviceUpdateMsg deviceUpdateMsg = |
|||
deviceUpdateMsgConstructor.constructDeviceUpdatedMsg( |
|||
UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, |
|||
device); |
|||
EntityUpdateMsg entityUpdateMsg = EntityUpdateMsg.newBuilder() |
|||
.setDeviceUpdateMsg(deviceUpdateMsg) |
|||
.build(); |
|||
outputStream.onNext(ResponseMsg.newBuilder() |
|||
.setEntityUpdateMsg(entityUpdateMsg) |
|||
.build()); |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageData.getNextPageLink(); |
|||
} |
|||
} while (pageData.hasNext()); |
|||
} catch (Exception e) { |
|||
log.error("Exception during loading edge device(s) on init!"); |
|||
} |
|||
} |
|||
|
|||
private void initAssets(Edge edge, StreamObserver<ResponseMsg> outputStream) { |
|||
try { |
|||
TextPageLink pageLink = new TextPageLink(100); |
|||
TextPageData<Asset> pageData; |
|||
do { |
|||
pageData = assetService.findAssetsByTenantIdAndEdgeId(edge.getTenantId(), edge.getId(), pageLink); |
|||
if (!pageData.getData().isEmpty()) { |
|||
log.trace("[{}] [{}] asset(s) are going to be pushed to edge.", edge.getId(), pageData.getData().size()); |
|||
for (Asset asset : pageData.getData()) { |
|||
AssetUpdateMsg assetUpdateMsg = |
|||
assetUpdateMsgConstructor.constructAssetUpdatedMsg( |
|||
UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, |
|||
asset); |
|||
EntityUpdateMsg entityUpdateMsg = EntityUpdateMsg.newBuilder() |
|||
.setAssetUpdateMsg(assetUpdateMsg) |
|||
.build(); |
|||
outputStream.onNext(ResponseMsg.newBuilder() |
|||
.setEntityUpdateMsg(entityUpdateMsg) |
|||
.build()); |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageData.getNextPageLink(); |
|||
} |
|||
} while (pageData.hasNext()); |
|||
} catch (Exception e) { |
|||
log.error("Exception during loading edge asset(s) on init!"); |
|||
} |
|||
} |
|||
|
|||
private void initEntityViews(Edge edge, StreamObserver<ResponseMsg> outputStream) { |
|||
try { |
|||
TextPageLink pageLink = new TextPageLink(100); |
|||
TextPageData<EntityView> pageData; |
|||
do { |
|||
pageData = entityViewService.findEntityViewsByTenantIdAndEdgeId(edge.getTenantId(), edge.getId(), pageLink); |
|||
if (!pageData.getData().isEmpty()) { |
|||
log.trace("[{}] [{}] entity view(s) are going to be pushed to edge.", edge.getId(), pageData.getData().size()); |
|||
for (EntityView entityView : pageData.getData()) { |
|||
EntityViewUpdateMsg entityViewUpdateMsg = |
|||
entityViewUpdateMsgConstructor.constructEntityViewUpdatedMsg( |
|||
UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, |
|||
entityView); |
|||
EntityUpdateMsg entityUpdateMsg = EntityUpdateMsg.newBuilder() |
|||
.setEntityViewUpdateMsg(entityViewUpdateMsg) |
|||
.build(); |
|||
outputStream.onNext(ResponseMsg.newBuilder() |
|||
.setEntityUpdateMsg(entityUpdateMsg) |
|||
.build()); |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageData.getNextPageLink(); |
|||
} |
|||
} while (pageData.hasNext()); |
|||
} catch (Exception e) { |
|||
log.error("Exception during loading edge entity view(s) on init!"); |
|||
} |
|||
} |
|||
|
|||
private void initDashboards(Edge edge, StreamObserver<ResponseMsg> outputStream) { |
|||
try { |
|||
TimePageLink pageLink = new TimePageLink(100); |
|||
TimePageData<DashboardInfo> pageData; |
|||
do { |
|||
pageData = dashboardService.findDashboardsByTenantIdAndEdgeId(edge.getTenantId(), edge.getId(), pageLink).get(); |
|||
if (!pageData.getData().isEmpty()) { |
|||
log.trace("[{}] [{}] dashboard(s) are going to be pushed to edge.", edge.getId(), pageData.getData().size()); |
|||
for (DashboardInfo dashboardInfo : pageData.getData()) { |
|||
Dashboard dashboard = dashboardService.findDashboardById(edge.getTenantId(), dashboardInfo.getId()); |
|||
DashboardUpdateMsg dashboardUpdateMsg = |
|||
dashboardUpdateMsgConstructor.constructDashboardUpdatedMsg( |
|||
UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, |
|||
dashboard); |
|||
EntityUpdateMsg entityUpdateMsg = EntityUpdateMsg.newBuilder() |
|||
.setDashboardUpdateMsg(dashboardUpdateMsg) |
|||
.build(); |
|||
outputStream.onNext(ResponseMsg.newBuilder() |
|||
.setEntityUpdateMsg(entityUpdateMsg) |
|||
.build()); |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageData.getNextPageLink(); |
|||
} |
|||
} while (pageData.hasNext()); |
|||
} catch (Exception e) { |
|||
log.error("Exception during loading edge dashboard(s) on init!"); |
|||
} |
|||
} |
|||
|
|||
private void initRuleChains(Edge edge, StreamObserver<ResponseMsg> outputStream) { |
|||
try { |
|||
TimePageLink pageLink = new TimePageLink(100); |
|||
TimePageData<RuleChain> pageData; |
|||
do { |
|||
pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(edge.getTenantId(), edge.getId(), pageLink).get(); |
|||
if (!pageData.getData().isEmpty()) { |
|||
log.trace("[{}] [{}] rule chains(s) are going to be pushed to edge.", edge.getId(), pageData.getData().size()); |
|||
for (RuleChain ruleChain : pageData.getData()) { |
|||
RuleChainUpdateMsg ruleChainUpdateMsg = |
|||
ruleChainUpdateMsgConstructor.constructRuleChainUpdatedMsg( |
|||
edge, |
|||
UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, |
|||
ruleChain); |
|||
EntityUpdateMsg entityUpdateMsg = EntityUpdateMsg.newBuilder() |
|||
.setRuleChainUpdateMsg(ruleChainUpdateMsg) |
|||
.build(); |
|||
outputStream.onNext(ResponseMsg.newBuilder() |
|||
.setEntityUpdateMsg(entityUpdateMsg) |
|||
.build()); |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageData.getNextPageLink(); |
|||
} |
|||
} while (pageData.hasNext()); |
|||
} catch (Exception e) { |
|||
log.error("Exception during loading edge rule chain(s) on init!"); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void initRuleChainMetadata(Edge edge, RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg, StreamObserver<ResponseMsg> outputStream) { |
|||
if (ruleChainMetadataRequestMsg.getRuleChainIdMSB() != 0 && ruleChainMetadataRequestMsg.getRuleChainIdLSB() != 0) { |
|||
RuleChainId ruleChainId = new RuleChainId(new UUID(ruleChainMetadataRequestMsg.getRuleChainIdMSB(), ruleChainMetadataRequestMsg.getRuleChainIdLSB())); |
|||
RuleChainMetaData ruleChainMetaData = ruleChainService.loadRuleChainMetaData(edge.getTenantId(), ruleChainId); |
|||
RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = |
|||
ruleChainUpdateMsgConstructor.constructRuleChainMetadataUpdatedMsg( |
|||
UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, |
|||
ruleChainMetaData); |
|||
if (ruleChainMetadataUpdateMsg != null) { |
|||
EntityUpdateMsg entityUpdateMsg = EntityUpdateMsg.newBuilder() |
|||
.setRuleChainMetadataUpdateMsg(ruleChainMetadataUpdateMsg) |
|||
.build(); |
|||
outputStream.onNext(ResponseMsg.newBuilder() |
|||
.setEntityUpdateMsg(entityUpdateMsg) |
|||
.build()); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.edge.rpc.init; |
|||
|
|||
import io.grpc.stub.StreamObserver; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.gen.edge.ResponseMsg; |
|||
import org.thingsboard.server.gen.edge.RuleChainMetadataRequestMsg; |
|||
|
|||
public interface InitEdgeService { |
|||
|
|||
void init(Edge edge, StreamObserver<ResponseMsg> outputStream); |
|||
|
|||
void initRuleChainMetadata(Edge edge, RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg, StreamObserver<ResponseMsg> outputStream); |
|||
} |
|||
Loading…
Reference in new issue