Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Yassmine Mestiri
thingsboard
Commits
98ad3b98
Commit
98ad3b98
authored
Mar 10, 2023
by
Yassmine Mestiri
Browse files
iot
parent
aea55d6d
Pipeline
#2009
failed with stages
in 0 seconds
Changes
273
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Too many changes to show.
To preserve performance only
13 of 273+
files are displayed.
Plain diff
Email patch
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/EntityViewEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.EntityView
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.EntityViewId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.EntityViewUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
EntityViewEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertEntityViewEventToDownlink
(
EdgeEvent
edgeEvent
)
{
EntityViewId
entityViewId
=
new
EntityViewId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
case
ASSIGNED_TO_EDGE:
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
EntityView
entityView
=
entityViewService
.
findEntityViewById
(
edgeEvent
.
getTenantId
(),
entityViewId
);
if
(
entityView
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
EntityViewUpdateMsg
entityViewUpdateMsg
=
entityViewMsgConstructor
.
constructEntityViewUpdatedMsg
(
msgType
,
entityView
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addEntityViewUpdateMsg
(
entityViewUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
case
UNASSIGNED_FROM_EDGE:
EntityViewUpdateMsg
entityViewUpdateMsg
=
entityViewMsgConstructor
.
constructEntityViewDeleteMsg
(
entityViewId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addEntityViewUpdateMsg
(
entityViewUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processEntityViewNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotification
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/OtaPackageEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.OtaPackage
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.OtaPackageId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.OtaPackageUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
OtaPackageEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertOtaPackageEventToDownlink
(
EdgeEvent
edgeEvent
)
{
OtaPackageId
otaPackageId
=
new
OtaPackageId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
OtaPackage
otaPackage
=
otaPackageService
.
findOtaPackageById
(
edgeEvent
.
getTenantId
(),
otaPackageId
);
if
(
otaPackage
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
OtaPackageUpdateMsg
otaPackageUpdateMsg
=
otaPackageMsgConstructor
.
constructOtaPackageUpdatedMsg
(
msgType
,
otaPackage
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addOtaPackageUpdateMsg
(
otaPackageUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
OtaPackageUpdateMsg
otaPackageUpdateMsg
=
otaPackageMsgConstructor
.
constructOtaPackageDeleteMsg
(
otaPackageId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addOtaPackageUpdateMsg
(
otaPackageUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processOtaPackageNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/QueueEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.QueueId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.queue.Queue
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.QueueUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
QueueEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertQueueEventToDownlink
(
EdgeEvent
edgeEvent
)
{
QueueId
queueId
=
new
QueueId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
Queue
queue
=
queueService
.
findQueueById
(
edgeEvent
.
getTenantId
(),
queueId
);
if
(
queue
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
QueueUpdateMsg
queueUpdateMsg
=
queueMsgConstructor
.
constructQueueUpdatedMsg
(
msgType
,
queue
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addQueueUpdateMsg
(
queueUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
QueueUpdateMsg
queueDeleteMsg
=
queueMsgConstructor
.
constructQueueDeleteMsg
(
queueId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addQueueUpdateMsg
(
queueDeleteMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processQueueNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/RelationEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.google.common.util.concurrent.Futures
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.common.util.JacksonUtil
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.EntityType
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.edge.EdgeEventActionType
;
import
org.thingsboard.server.common.data.edge.EdgeEventType
;
import
org.thingsboard.server.common.data.exception.ThingsboardErrorCode
;
import
org.thingsboard.server.common.data.exception.ThingsboardException
;
import
org.thingsboard.server.common.data.id.AssetId
;
import
org.thingsboard.server.common.data.id.CustomerId
;
import
org.thingsboard.server.common.data.id.DashboardId
;
import
org.thingsboard.server.common.data.id.DeviceId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.EntityIdFactory
;
import
org.thingsboard.server.common.data.id.EntityViewId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.UserId
;
import
org.thingsboard.server.common.data.relation.EntityRelation
;
import
org.thingsboard.server.common.data.relation.RelationTypeGroup
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.RelationUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.UUID
;
@Component
@Slf4j
@TbCoreComponent
public
class
RelationEdgeProcessor
extends
BaseEdgeProcessor
{
public
ListenableFuture
<
Void
>
processRelationFromEdge
(
TenantId
tenantId
,
RelationUpdateMsg
relationUpdateMsg
)
{
log
.
trace
(
"[{}] onRelationUpdate [{}]"
,
tenantId
,
relationUpdateMsg
);
try
{
EntityRelation
entityRelation
=
new
EntityRelation
();
UUID
fromUUID
=
new
UUID
(
relationUpdateMsg
.
getFromIdMSB
(),
relationUpdateMsg
.
getFromIdLSB
());
EntityId
fromId
=
EntityIdFactory
.
getByTypeAndUuid
(
EntityType
.
valueOf
(
relationUpdateMsg
.
getFromEntityType
()),
fromUUID
);
entityRelation
.
setFrom
(
fromId
);
UUID
toUUID
=
new
UUID
(
relationUpdateMsg
.
getToIdMSB
(),
relationUpdateMsg
.
getToIdLSB
());
EntityId
toId
=
EntityIdFactory
.
getByTypeAndUuid
(
EntityType
.
valueOf
(
relationUpdateMsg
.
getToEntityType
()),
toUUID
);
entityRelation
.
setTo
(
toId
);
entityRelation
.
setType
(
relationUpdateMsg
.
getType
());
if
(
relationUpdateMsg
.
hasTypeGroup
())
{
entityRelation
.
setTypeGroup
(
RelationTypeGroup
.
valueOf
(
relationUpdateMsg
.
getTypeGroup
()));
}
entityRelation
.
setAdditionalInfo
(
JacksonUtil
.
OBJECT_MAPPER
.
readTree
(
relationUpdateMsg
.
getAdditionalInfo
()));
switch
(
relationUpdateMsg
.
getMsgType
())
{
case
ENTITY_CREATED_RPC_MESSAGE:
case
ENTITY_UPDATED_RPC_MESSAGE:
if
(
isEntityExists
(
tenantId
,
entityRelation
.
getTo
())
&&
isEntityExists
(
tenantId
,
entityRelation
.
getFrom
()))
{
relationService
.
saveRelationAsync
(
tenantId
,
entityRelation
);
}
break
;
case
ENTITY_DELETED_RPC_MESSAGE:
relationService
.
deleteRelation
(
tenantId
,
entityRelation
);
break
;
case
UNRECOGNIZED:
log
.
error
(
"Unsupported msg type"
);
}
return
Futures
.
immediateFuture
(
null
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Failed to process relation update msg [{}]"
,
relationUpdateMsg
,
e
);
return
Futures
.
immediateFailedFuture
(
new
RuntimeException
(
"Failed to process relation update msg"
,
e
));
}
}
private
boolean
isEntityExists
(
TenantId
tenantId
,
EntityId
entityId
)
throws
ThingsboardException
{
switch
(
entityId
.
getEntityType
())
{
case
DEVICE:
return
deviceService
.
findDeviceById
(
tenantId
,
new
DeviceId
(
entityId
.
getId
()))
!=
null
;
case
ASSET:
return
assetService
.
findAssetById
(
tenantId
,
new
AssetId
(
entityId
.
getId
()))
!=
null
;
case
ENTITY_VIEW:
return
entityViewService
.
findEntityViewById
(
tenantId
,
new
EntityViewId
(
entityId
.
getId
()))
!=
null
;
case
CUSTOMER:
return
customerService
.
findCustomerById
(
tenantId
,
new
CustomerId
(
entityId
.
getId
()))
!=
null
;
case
USER:
return
userService
.
findUserById
(
tenantId
,
new
UserId
(
entityId
.
getId
()))
!=
null
;
case
DASHBOARD:
return
dashboardService
.
findDashboardById
(
tenantId
,
new
DashboardId
(
entityId
.
getId
()))
!=
null
;
default
:
throw
new
ThingsboardException
(
"Unsupported entity type "
+
entityId
.
getEntityType
(),
ThingsboardErrorCode
.
INVALID_ARGUMENTS
);
}
}
public
DownlinkMsg
convertRelationEventToDownlink
(
EdgeEvent
edgeEvent
)
{
EntityRelation
entityRelation
=
JacksonUtil
.
OBJECT_MAPPER
.
convertValue
(
edgeEvent
.
getBody
(),
EntityRelation
.
class
);
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
RelationUpdateMsg
relationUpdateMsg
=
relationMsgConstructor
.
constructRelationUpdatedMsg
(
msgType
,
entityRelation
);
return
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addRelationUpdateMsg
(
relationUpdateMsg
)
.
build
();
}
public
ListenableFuture
<
Void
>
processRelationNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
throws
JsonProcessingException
{
EntityRelation
relation
=
JacksonUtil
.
OBJECT_MAPPER
.
readValue
(
edgeNotificationMsg
.
getBody
(),
EntityRelation
.
class
);
if
(
relation
.
getFrom
().
getEntityType
().
equals
(
EntityType
.
EDGE
)
||
relation
.
getTo
().
getEntityType
().
equals
(
EntityType
.
EDGE
))
{
return
Futures
.
immediateFuture
(
null
);
}
Set
<
EdgeId
>
uniqueEdgeIds
=
new
HashSet
<>();
uniqueEdgeIds
.
addAll
(
edgeService
.
findAllRelatedEdgeIds
(
tenantId
,
relation
.
getTo
()));
uniqueEdgeIds
.
addAll
(
edgeService
.
findAllRelatedEdgeIds
(
tenantId
,
relation
.
getFrom
()));
if
(
uniqueEdgeIds
.
isEmpty
())
{
return
Futures
.
immediateFuture
(
null
);
}
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
for
(
EdgeId
edgeId
:
uniqueEdgeIds
)
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
edgeId
,
EdgeEventType
.
RELATION
,
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
()),
null
,
JacksonUtil
.
OBJECT_MAPPER
.
valueToTree
(
relation
)));
}
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/RuleChainEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.RuleChainId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.rule.RuleChain
;
import
org.thingsboard.server.common.data.rule.RuleChainMetaData
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.EdgeVersion
;
import
org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.RuleChainUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
import
static
org
.
thingsboard
.
server
.
service
.
edge
.
DefaultEdgeNotificationService
.
EDGE_IS_ROOT_BODY_KEY
;
@Component
@Slf4j
@TbCoreComponent
public
class
RuleChainEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertRuleChainEventToDownlink
(
EdgeEvent
edgeEvent
)
{
RuleChainId
ruleChainId
=
new
RuleChainId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
case
ASSIGNED_TO_EDGE:
RuleChain
ruleChain
=
ruleChainService
.
findRuleChainById
(
edgeEvent
.
getTenantId
(),
ruleChainId
);
if
(
ruleChain
!=
null
)
{
boolean
isRoot
=
false
;
if
(
edgeEvent
.
getBody
()
!=
null
&&
edgeEvent
.
getBody
().
get
(
EDGE_IS_ROOT_BODY_KEY
)
!=
null
)
{
try
{
isRoot
=
Boolean
.
parseBoolean
(
edgeEvent
.
getBody
().
get
(
EDGE_IS_ROOT_BODY_KEY
).
asText
());
}
catch
(
Exception
ignored
)
{}
}
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
RuleChainUpdateMsg
ruleChainUpdateMsg
=
ruleChainMsgConstructor
.
constructRuleChainUpdatedMsg
(
msgType
,
ruleChain
,
isRoot
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addRuleChainUpdateMsg
(
ruleChainUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
case
UNASSIGNED_FROM_EDGE:
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addRuleChainUpdateMsg
(
ruleChainMsgConstructor
.
constructRuleChainDeleteMsg
(
ruleChainId
))
.
build
();
break
;
}
return
downlinkMsg
;
}
public
DownlinkMsg
convertRuleChainMetadataEventToDownlink
(
EdgeEvent
edgeEvent
,
EdgeVersion
edgeVersion
)
{
RuleChainId
ruleChainId
=
new
RuleChainId
(
edgeEvent
.
getEntityId
());
RuleChain
ruleChain
=
ruleChainService
.
findRuleChainById
(
edgeEvent
.
getTenantId
(),
ruleChainId
);
DownlinkMsg
downlinkMsg
=
null
;
if
(
ruleChain
!=
null
)
{
RuleChainMetaData
ruleChainMetaData
=
ruleChainService
.
loadRuleChainMetaData
(
edgeEvent
.
getTenantId
(),
ruleChainId
);
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
RuleChainMetadataUpdateMsg
ruleChainMetadataUpdateMsg
=
ruleChainMsgConstructor
.
constructRuleChainMetadataUpdatedMsg
(
edgeEvent
.
getTenantId
(),
msgType
,
ruleChainMetaData
,
edgeVersion
);
if
(
ruleChainMetadataUpdateMsg
!=
null
)
{
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addRuleChainMetadataUpdateMsg
(
ruleChainMetadataUpdateMsg
)
.
build
();
}
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processRuleChainNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotification
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/TelemetryEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.google.common.util.concurrent.FutureCallback
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
com.google.common.util.concurrent.SettableFuture
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.tuple.ImmutablePair
;
import
org.apache.commons.lang3.tuple.Pair
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.common.util.JacksonUtil
;
import
org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg
;
import
org.thingsboard.server.common.data.DataConstants
;
import
org.thingsboard.server.common.data.Device
;
import
org.thingsboard.server.common.data.DeviceProfile
;
import
org.thingsboard.server.common.data.EdgeUtils
;
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.asset.AssetProfile
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.edge.EdgeEventActionType
;
import
org.thingsboard.server.common.data.id.AssetId
;
import
org.thingsboard.server.common.data.id.CustomerId
;
import
org.thingsboard.server.common.data.id.DashboardId
;
import
org.thingsboard.server.common.data.id.DeviceId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.EntityViewId
;
import
org.thingsboard.server.common.data.id.RuleChainId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.UserId
;
import
org.thingsboard.server.common.data.kv.AttributeKvEntry
;
import
org.thingsboard.server.common.msg.TbMsg
;
import
org.thingsboard.server.common.msg.TbMsgMetaData
;
import
org.thingsboard.server.common.msg.queue.ServiceType
;
import
org.thingsboard.server.common.msg.queue.TopicPartitionInfo
;
import
org.thingsboard.server.common.msg.session.SessionMsgType
;
import
org.thingsboard.server.common.transport.adaptor.JsonConverter
;
import
org.thingsboard.server.common.transport.util.JsonUtils
;
import
org.thingsboard.server.dao.model.ModelConstants
;
import
org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.EntityDataProto
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.TbQueueCallback
;
import
org.thingsboard.server.queue.TbQueueMsgMetadata
;
import
org.thingsboard.server.queue.TbQueueProducer
;
import
org.thingsboard.server.queue.common.TbProtoQueueMsg
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
import
javax.annotation.Nullable
;
import
javax.annotation.PostConstruct
;
import
java.util.ArrayList
;
import
java.util.List
;
@Component
@Slf4j
@TbCoreComponent
public
class
TelemetryEdgeProcessor
extends
BaseEdgeProcessor
{
private
final
Gson
gson
=
new
Gson
();
private
TbQueueProducer
<
TbProtoQueueMsg
<
TransportProtos
.
ToCoreMsg
>>
tbCoreMsgProducer
;
@PostConstruct
public
void
init
()
{
tbCoreMsgProducer
=
producerProvider
.
getTbCoreMsgProducer
();
}
public
List
<
ListenableFuture
<
Void
>>
processTelemetryFromEdge
(
TenantId
tenantId
,
EntityDataProto
entityData
)
{
log
.
trace
(
"[{}] processTelemetryFromEdge [{}]"
,
tenantId
,
entityData
);
List
<
ListenableFuture
<
Void
>>
result
=
new
ArrayList
<>();
EntityId
entityId
=
constructEntityId
(
entityData
.
getEntityType
(),
entityData
.
getEntityIdMSB
(),
entityData
.
getEntityIdLSB
());
if
((
entityData
.
hasPostAttributesMsg
()
||
entityData
.
hasPostTelemetryMsg
()
||
entityData
.
hasAttributesUpdatedMsg
())
&&
entityId
!=
null
)
{
Pair
<
TbMsgMetaData
,
CustomerId
>
pair
=
getBaseMsgMetadataAndCustomerId
(
tenantId
,
entityId
);
TbMsgMetaData
metaData
=
pair
.
getKey
();
CustomerId
customerId
=
pair
.
getValue
();
metaData
.
putValue
(
DataConstants
.
MSG_SOURCE_KEY
,
DataConstants
.
EDGE_MSG_SOURCE
);
if
(
entityData
.
hasPostAttributesMsg
())
{
result
.
add
(
processPostAttributes
(
tenantId
,
customerId
,
entityId
,
entityData
.
getPostAttributesMsg
(),
metaData
));
}
if
(
entityData
.
hasAttributesUpdatedMsg
())
{
metaData
.
putValue
(
"scope"
,
entityData
.
getPostAttributeScope
());
result
.
add
(
processAttributesUpdate
(
tenantId
,
customerId
,
entityId
,
entityData
.
getAttributesUpdatedMsg
(),
metaData
));
}
if
(
entityData
.
hasPostTelemetryMsg
())
{
result
.
add
(
processPostTelemetry
(
tenantId
,
customerId
,
entityId
,
entityData
.
getPostTelemetryMsg
(),
metaData
));
}
if
(
EntityType
.
DEVICE
.
equals
(
entityId
.
getEntityType
()))
{
DeviceId
deviceId
=
new
DeviceId
(
entityId
.
getId
());
long
currentTs
=
System
.
currentTimeMillis
();
TransportProtos
.
DeviceActivityProto
deviceActivityMsg
=
TransportProtos
.
DeviceActivityProto
.
newBuilder
()
.
setTenantIdMSB
(
tenantId
.
getId
().
getMostSignificantBits
())
.
setTenantIdLSB
(
tenantId
.
getId
().
getLeastSignificantBits
())
.
setDeviceIdMSB
(
deviceId
.
getId
().
getMostSignificantBits
())
.
setDeviceIdLSB
(
deviceId
.
getId
().
getLeastSignificantBits
())
.
setLastActivityTime
(
currentTs
).
build
();
log
.
trace
(
"[{}][{}] device activity time is going to be updated, ts {}"
,
tenantId
,
deviceId
,
currentTs
);
TopicPartitionInfo
tpi
=
partitionService
.
resolve
(
ServiceType
.
TB_CORE
,
tenantId
,
deviceId
);
tbCoreMsgProducer
.
send
(
tpi
,
new
TbProtoQueueMsg
<>(
deviceId
.
getId
(),
TransportProtos
.
ToCoreMsg
.
newBuilder
().
setDeviceActivityMsg
(
deviceActivityMsg
).
build
()),
null
);
}
}
if
(
entityData
.
hasAttributeDeleteMsg
())
{
result
.
add
(
processAttributeDeleteMsg
(
tenantId
,
entityId
,
entityData
.
getAttributeDeleteMsg
(),
entityData
.
getEntityType
()));
}
return
result
;
}
private
Pair
<
TbMsgMetaData
,
CustomerId
>
getBaseMsgMetadataAndCustomerId
(
TenantId
tenantId
,
EntityId
entityId
)
{
TbMsgMetaData
metaData
=
new
TbMsgMetaData
();
CustomerId
customerId
=
null
;
switch
(
entityId
.
getEntityType
())
{
case
DEVICE:
Device
device
=
deviceService
.
findDeviceById
(
tenantId
,
new
DeviceId
(
entityId
.
getId
()));
if
(
device
!=
null
)
{
customerId
=
device
.
getCustomerId
();
metaData
.
putValue
(
"deviceName"
,
device
.
getName
());
metaData
.
putValue
(
"deviceType"
,
device
.
getType
());
}
break
;
case
ASSET:
Asset
asset
=
assetService
.
findAssetById
(
tenantId
,
new
AssetId
(
entityId
.
getId
()));
if
(
asset
!=
null
)
{
customerId
=
asset
.
getCustomerId
();
metaData
.
putValue
(
"assetName"
,
asset
.
getName
());
metaData
.
putValue
(
"assetType"
,
asset
.
getType
());
}
break
;
case
ENTITY_VIEW:
EntityView
entityView
=
entityViewService
.
findEntityViewById
(
tenantId
,
new
EntityViewId
(
entityId
.
getId
()));
if
(
entityView
!=
null
)
{
customerId
=
entityView
.
getCustomerId
();
metaData
.
putValue
(
"entityViewName"
,
entityView
.
getName
());
metaData
.
putValue
(
"entityViewType"
,
entityView
.
getType
());
}
break
;
case
EDGE:
Edge
edge
=
edgeService
.
findEdgeById
(
tenantId
,
new
EdgeId
(
entityId
.
getId
()));
if
(
edge
!=
null
)
{
customerId
=
edge
.
getCustomerId
();
metaData
.
putValue
(
"edgeName"
,
edge
.
getName
());
metaData
.
putValue
(
"edgeType"
,
edge
.
getType
());
}
break
;
default
:
log
.
debug
(
"Using empty metadata for entityId [{}]"
,
entityId
);
break
;
}
return
new
ImmutablePair
<>(
metaData
,
customerId
!=
null
?
customerId
:
new
CustomerId
(
ModelConstants
.
NULL_UUID
));
}
private
ListenableFuture
<
Void
>
processPostTelemetry
(
TenantId
tenantId
,
CustomerId
customerId
,
EntityId
entityId
,
TransportProtos
.
PostTelemetryMsg
msg
,
TbMsgMetaData
metaData
)
{
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
for
(
TransportProtos
.
TsKvListProto
tsKv
:
msg
.
getTsKvListList
())
{
JsonObject
json
=
JsonUtils
.
getJsonObject
(
tsKv
.
getKvList
());
metaData
.
putValue
(
"ts"
,
tsKv
.
getTs
()
+
""
);
var
defaultQueueAndRuleChain
=
getDefaultQueueNameAndRuleChainId
(
tenantId
,
entityId
);
TbMsg
tbMsg
=
TbMsg
.
newMsg
(
defaultQueueAndRuleChain
.
getKey
(),
SessionMsgType
.
POST_TELEMETRY_REQUEST
.
name
(),
entityId
,
customerId
,
metaData
,
gson
.
toJson
(
json
),
defaultQueueAndRuleChain
.
getValue
(),
null
);
tbClusterService
.
pushMsgToRuleEngine
(
tenantId
,
tbMsg
.
getOriginator
(),
tbMsg
,
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Can't process post telemetry [{}]"
,
msg
,
t
);
futureToSet
.
setException
(
t
);
}
});
}
return
futureToSet
;
}
private
Pair
<
String
,
RuleChainId
>
getDefaultQueueNameAndRuleChainId
(
TenantId
tenantId
,
EntityId
entityId
)
{
RuleChainId
ruleChainId
=
null
;
String
queueName
=
null
;
if
(
EntityType
.
DEVICE
.
equals
(
entityId
.
getEntityType
()))
{
DeviceProfile
deviceProfile
=
deviceProfileCache
.
get
(
tenantId
,
new
DeviceId
(
entityId
.
getId
()));
if
(
deviceProfile
==
null
)
{
log
.
warn
(
"[{}] Device profile is null!"
,
entityId
);
}
else
{
ruleChainId
=
deviceProfile
.
getDefaultRuleChainId
();
queueName
=
deviceProfile
.
getDefaultQueueName
();
}
}
else
if
(
EntityType
.
ASSET
.
equals
(
entityId
.
getEntityType
()))
{
AssetProfile
assetProfile
=
assetProfileCache
.
get
(
tenantId
,
new
AssetId
(
entityId
.
getId
()));
if
(
assetProfile
==
null
)
{
log
.
warn
(
"[{}] Asset profile is null!"
,
entityId
);
}
else
{
ruleChainId
=
assetProfile
.
getDefaultRuleChainId
();
queueName
=
assetProfile
.
getDefaultQueueName
();
}
}
return
new
ImmutablePair
<>(
queueName
,
ruleChainId
);
}
private
ListenableFuture
<
Void
>
processPostAttributes
(
TenantId
tenantId
,
CustomerId
customerId
,
EntityId
entityId
,
TransportProtos
.
PostAttributeMsg
msg
,
TbMsgMetaData
metaData
)
{
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
JsonObject
json
=
JsonUtils
.
getJsonObject
(
msg
.
getKvList
());
var
defaultQueueAndRuleChain
=
getDefaultQueueNameAndRuleChainId
(
tenantId
,
entityId
);
TbMsg
tbMsg
=
TbMsg
.
newMsg
(
defaultQueueAndRuleChain
.
getKey
(),
SessionMsgType
.
POST_ATTRIBUTES_REQUEST
.
name
(),
entityId
,
customerId
,
metaData
,
gson
.
toJson
(
json
),
defaultQueueAndRuleChain
.
getValue
(),
null
);
tbClusterService
.
pushMsgToRuleEngine
(
tenantId
,
tbMsg
.
getOriginator
(),
tbMsg
,
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Can't process post attributes [{}]"
,
msg
,
t
);
futureToSet
.
setException
(
t
);
}
});
return
futureToSet
;
}
private
ListenableFuture
<
Void
>
processAttributesUpdate
(
TenantId
tenantId
,
CustomerId
customerId
,
EntityId
entityId
,
TransportProtos
.
PostAttributeMsg
msg
,
TbMsgMetaData
metaData
)
{
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
JsonObject
json
=
JsonUtils
.
getJsonObject
(
msg
.
getKvList
());
List
<
AttributeKvEntry
>
attributes
=
new
ArrayList
<>(
JsonConverter
.
convertToAttributes
(
json
));
String
scope
=
metaData
.
getValue
(
"scope"
);
tsSubService
.
saveAndNotify
(
tenantId
,
entityId
,
scope
,
attributes
,
new
FutureCallback
<
Void
>()
{
@Override
public
void
onSuccess
(
@Nullable
Void
tmp
)
{
var
defaultQueueAndRuleChain
=
getDefaultQueueNameAndRuleChainId
(
tenantId
,
entityId
);
TbMsg
tbMsg
=
TbMsg
.
newMsg
(
defaultQueueAndRuleChain
.
getKey
(),
DataConstants
.
ATTRIBUTES_UPDATED
,
entityId
,
customerId
,
metaData
,
gson
.
toJson
(
json
),
defaultQueueAndRuleChain
.
getValue
(),
null
);
tbClusterService
.
pushMsgToRuleEngine
(
tenantId
,
tbMsg
.
getOriginator
(),
tbMsg
,
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Can't process attributes update [{}]"
,
msg
,
t
);
futureToSet
.
setException
(
t
);
}
});
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Can't process attributes update [{}]"
,
msg
,
t
);
futureToSet
.
setException
(
t
);
}
});
return
futureToSet
;
}
private
ListenableFuture
<
Void
>
processAttributeDeleteMsg
(
TenantId
tenantId
,
EntityId
entityId
,
AttributeDeleteMsg
attributeDeleteMsg
,
String
entityType
)
{
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
String
scope
=
attributeDeleteMsg
.
getScope
();
List
<
String
>
attributeNames
=
attributeDeleteMsg
.
getAttributeNamesList
();
attributesService
.
removeAll
(
tenantId
,
entityId
,
scope
,
attributeNames
);
if
(
EntityType
.
DEVICE
.
name
().
equals
(
entityType
))
{
tbClusterService
.
pushMsgToCore
(
DeviceAttributesEventNotificationMsg
.
onDelete
(
tenantId
,
(
DeviceId
)
entityId
,
scope
,
attributeNames
),
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Can't process attribute delete msg [{}]"
,
attributeDeleteMsg
,
t
);
futureToSet
.
setException
(
t
);
}
});
}
return
futureToSet
;
}
public
DownlinkMsg
convertTelemetryEventToDownlink
(
EdgeEvent
edgeEvent
)
throws
JsonProcessingException
{
EntityId
entityId
;
switch
(
edgeEvent
.
getType
())
{
case
DEVICE:
entityId
=
new
DeviceId
(
edgeEvent
.
getEntityId
());
break
;
case
ASSET:
entityId
=
new
AssetId
(
edgeEvent
.
getEntityId
());
break
;
case
ENTITY_VIEW:
entityId
=
new
EntityViewId
(
edgeEvent
.
getEntityId
());
break
;
case
DASHBOARD:
entityId
=
new
DashboardId
(
edgeEvent
.
getEntityId
());
break
;
case
TENANT:
entityId
=
TenantId
.
fromUUID
(
edgeEvent
.
getEntityId
());
break
;
case
CUSTOMER:
entityId
=
new
CustomerId
(
edgeEvent
.
getEntityId
());
break
;
case
USER:
entityId
=
new
UserId
(
edgeEvent
.
getEntityId
());
break
;
case
EDGE:
entityId
=
new
EdgeId
(
edgeEvent
.
getEntityId
());
break
;
default
:
log
.
warn
(
"Unsupported edge event type [{}]"
,
edgeEvent
);
return
null
;
}
return
constructEntityDataProtoMsg
(
entityId
,
edgeEvent
.
getAction
(),
JsonUtils
.
parse
(
JacksonUtil
.
OBJECT_MAPPER
.
writeValueAsString
(
edgeEvent
.
getBody
())));
}
private
DownlinkMsg
constructEntityDataProtoMsg
(
EntityId
entityId
,
EdgeEventActionType
actionType
,
JsonElement
entityData
)
{
EntityDataProto
entityDataProto
=
entityDataMsgConstructor
.
constructEntityDataMsg
(
entityId
,
actionType
,
entityData
);
return
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addEntityData
(
entityDataProto
)
.
build
();
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/UserEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.User
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.UserId
;
import
org.thingsboard.server.common.data.security.UserCredentials
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.edge.v1.UserCredentialsUpdateMsg
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
UserEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertUserEventToDownlink
(
EdgeEvent
edgeEvent
)
{
UserId
userId
=
new
UserId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
User
user
=
userService
.
findUserById
(
edgeEvent
.
getTenantId
(),
userId
);
if
(
user
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addUserUpdateMsg
(
userMsgConstructor
.
constructUserUpdatedMsg
(
msgType
,
user
))
.
build
();
}
break
;
case
DELETED:
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addUserUpdateMsg
(
userMsgConstructor
.
constructUserDeleteMsg
(
userId
))
.
build
();
break
;
case
CREDENTIALS_UPDATED:
UserCredentials
userCredentialsByUserId
=
userService
.
findUserCredentialsByUserId
(
edgeEvent
.
getTenantId
(),
userId
);
if
(
userCredentialsByUserId
!=
null
&&
userCredentialsByUserId
.
isEnabled
())
{
UserCredentialsUpdateMsg
userCredentialsUpdateMsg
=
userMsgConstructor
.
constructUserCredentialsUpdatedMsg
(
userCredentialsByUserId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addUserCredentialsUpdateMsg
(
userCredentialsUpdateMsg
)
.
build
();
}
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processUserNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/WidgetBundleEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.WidgetsBundleId
;
import
org.thingsboard.server.common.data.widget.WidgetsBundle
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.edge.v1.WidgetsBundleUpdateMsg
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
WidgetBundleEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertWidgetsBundleEventToDownlink
(
EdgeEvent
edgeEvent
)
{
WidgetsBundleId
widgetsBundleId
=
new
WidgetsBundleId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
WidgetsBundle
widgetsBundle
=
widgetsBundleService
.
findWidgetsBundleById
(
edgeEvent
.
getTenantId
(),
widgetsBundleId
);
if
(
widgetsBundle
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
WidgetsBundleUpdateMsg
widgetsBundleUpdateMsg
=
widgetsBundleMsgConstructor
.
constructWidgetsBundleUpdateMsg
(
msgType
,
widgetsBundle
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addWidgetsBundleUpdateMsg
(
widgetsBundleUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
WidgetsBundleUpdateMsg
widgetsBundleUpdateMsg
=
widgetsBundleMsgConstructor
.
constructWidgetsBundleDeleteMsg
(
widgetsBundleId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addWidgetsBundleUpdateMsg
(
widgetsBundleUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processWidgetsBundleNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/WidgetTypeEdgeProcessor.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.processor
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.WidgetTypeId
;
import
org.thingsboard.server.common.data.widget.WidgetTypeDetails
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.edge.v1.WidgetTypeUpdateMsg
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
WidgetTypeEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertWidgetTypeEventToDownlink
(
EdgeEvent
edgeEvent
)
{
WidgetTypeId
widgetTypeId
=
new
WidgetTypeId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
WidgetTypeDetails
widgetTypeDetails
=
widgetTypeService
.
findWidgetTypeDetailsById
(
edgeEvent
.
getTenantId
(),
widgetTypeId
);
if
(
widgetTypeDetails
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
WidgetTypeUpdateMsg
widgetTypeUpdateMsg
=
widgetTypeMsgConstructor
.
constructWidgetTypeUpdateMsg
(
msgType
,
widgetTypeDetails
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addWidgetTypeUpdateMsg
(
widgetTypeUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
WidgetTypeUpdateMsg
widgetTypeUpdateMsg
=
widgetTypeMsgConstructor
.
constructWidgetTypeDeleteMsg
(
widgetTypeId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addWidgetTypeUpdateMsg
(
widgetTypeUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processWidgetTypeNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/sync/DefaultEdgeRequestsService.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.sync
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
com.google.common.util.concurrent.FutureCallback
;
import
com.google.common.util.concurrent.Futures
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
com.google.common.util.concurrent.SettableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.checkerframework.checker.nullness.qual.Nullable
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Lazy
;
import
org.springframework.stereotype.Service
;
import
org.thingsboard.common.util.JacksonUtil
;
import
org.thingsboard.server.cluster.TbClusterService
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.EntityType
;
import
org.thingsboard.server.common.data.EntityView
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.edge.EdgeEventActionType
;
import
org.thingsboard.server.common.data.edge.EdgeEventType
;
import
org.thingsboard.server.common.data.id.DeviceId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.EntityIdFactory
;
import
org.thingsboard.server.common.data.id.RuleChainId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.UserId
;
import
org.thingsboard.server.common.data.id.WidgetsBundleId
;
import
org.thingsboard.server.common.data.kv.AttributeKvEntry
;
import
org.thingsboard.server.common.data.kv.DataType
;
import
org.thingsboard.server.common.data.relation.EntityRelation
;
import
org.thingsboard.server.common.data.relation.EntityRelationsQuery
;
import
org.thingsboard.server.common.data.relation.EntitySearchDirection
;
import
org.thingsboard.server.common.data.relation.RelationTypeGroup
;
import
org.thingsboard.server.common.data.relation.RelationsSearchParameters
;
import
org.thingsboard.server.common.data.widget.WidgetType
;
import
org.thingsboard.server.common.data.widget.WidgetsBundle
;
import
org.thingsboard.server.dao.asset.AssetProfileService
;
import
org.thingsboard.server.dao.asset.AssetService
;
import
org.thingsboard.server.dao.attributes.AttributesService
;
import
org.thingsboard.server.dao.device.DeviceProfileService
;
import
org.thingsboard.server.dao.device.DeviceService
;
import
org.thingsboard.server.dao.edge.EdgeEventService
;
import
org.thingsboard.server.dao.relation.RelationService
;
import
org.thingsboard.server.dao.widget.WidgetTypeService
;
import
org.thingsboard.server.dao.widget.WidgetsBundleService
;
import
org.thingsboard.server.gen.edge.v1.AttributesRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.EntityViewsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.RelationRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.RuleChainMetadataRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.UserCredentialsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.WidgetBundleTypesRequestMsg
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
import
org.thingsboard.server.service.entitiy.entityview.TbEntityViewService
;
import
org.thingsboard.server.service.executors.DbCallbackExecutorService
;
import
org.thingsboard.server.service.state.DefaultDeviceStateService
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
@Service
@TbCoreComponent
@Slf4j
public
class
DefaultEdgeRequestsService
implements
EdgeRequestsService
{
private
static
final
int
DEFAULT_PAGE_SIZE
=
1000
;
@Autowired
private
EdgeEventService
edgeEventService
;
@Autowired
private
AttributesService
attributesService
;
@Autowired
private
RelationService
relationService
;
@Autowired
private
DeviceService
deviceService
;
@Autowired
private
AssetService
assetService
;
@Lazy
@Autowired
private
TbEntityViewService
entityViewService
;
@Autowired
private
DeviceProfileService
deviceProfileService
;
@Autowired
private
AssetProfileService
assetProfileService
;
@Autowired
private
WidgetsBundleService
widgetsBundleService
;
@Autowired
private
WidgetTypeService
widgetTypeService
;
@Autowired
private
DbCallbackExecutorService
dbCallbackExecutorService
;
@Autowired
private
TbClusterService
tbClusterService
;
@Override
public
ListenableFuture
<
Void
>
processRuleChainMetadataRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
RuleChainMetadataRequestMsg
ruleChainMetadataRequestMsg
)
{
log
.
trace
(
"[{}] processRuleChainMetadataRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
ruleChainMetadataRequestMsg
);
if
(
ruleChainMetadataRequestMsg
.
getRuleChainIdMSB
()
==
0
||
ruleChainMetadataRequestMsg
.
getRuleChainIdLSB
()
==
0
)
{
return
Futures
.
immediateFuture
(
null
);
}
RuleChainId
ruleChainId
=
new
RuleChainId
(
new
UUID
(
ruleChainMetadataRequestMsg
.
getRuleChainIdMSB
(),
ruleChainMetadataRequestMsg
.
getRuleChainIdLSB
()));
return
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
RULE_CHAIN_METADATA
,
EdgeEventActionType
.
ADDED
,
ruleChainId
,
null
);
}
@Override
public
ListenableFuture
<
Void
>
processAttributesRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
AttributesRequestMsg
attributesRequestMsg
)
{
log
.
trace
(
"[{}] processAttributesRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
attributesRequestMsg
);
EntityId
entityId
=
EntityIdFactory
.
getByTypeAndUuid
(
EntityType
.
valueOf
(
attributesRequestMsg
.
getEntityType
()),
new
UUID
(
attributesRequestMsg
.
getEntityIdMSB
(),
attributesRequestMsg
.
getEntityIdLSB
()));
final
EdgeEventType
type
=
EdgeUtils
.
getEdgeEventTypeByEntityType
(
entityId
.
getEntityType
());
if
(
type
==
null
)
{
log
.
warn
(
"[{}] Type doesn't supported {}"
,
tenantId
,
entityId
.
getEntityType
());
return
Futures
.
immediateFuture
(
null
);
}
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
String
scope
=
attributesRequestMsg
.
getScope
();
ListenableFuture
<
List
<
AttributeKvEntry
>>
findAttrFuture
=
attributesService
.
findAll
(
tenantId
,
entityId
,
scope
);
Futures
.
addCallback
(
findAttrFuture
,
new
FutureCallback
<>()
{
@Override
public
void
onSuccess
(
@Nullable
List
<
AttributeKvEntry
>
ssAttributes
)
{
if
(
ssAttributes
==
null
||
ssAttributes
.
isEmpty
())
{
log
.
trace
(
"[{}][{}] No attributes found for entity {} [{}]"
,
tenantId
,
edge
.
getName
(),
entityId
.
getEntityType
(),
entityId
.
getId
());
futureToSet
.
set
(
null
);
return
;
}
try
{
Map
<
String
,
Object
>
entityData
=
new
HashMap
<>();
ObjectNode
attributes
=
JacksonUtil
.
OBJECT_MAPPER
.
createObjectNode
();
for
(
AttributeKvEntry
attr
:
ssAttributes
)
{
if
(
DefaultDeviceStateService
.
PERSISTENT_ATTRIBUTES
.
contains
(
attr
.
getKey
())
&&
!
DefaultDeviceStateService
.
INACTIVITY_TIMEOUT
.
equals
(
attr
.
getKey
()))
{
continue
;
}
if
(
attr
.
getDataType
()
==
DataType
.
BOOLEAN
&&
attr
.
getBooleanValue
().
isPresent
())
{
attributes
.
put
(
attr
.
getKey
(),
attr
.
getBooleanValue
().
get
());
}
else
if
(
attr
.
getDataType
()
==
DataType
.
DOUBLE
&&
attr
.
getDoubleValue
().
isPresent
())
{
attributes
.
put
(
attr
.
getKey
(),
attr
.
getDoubleValue
().
get
());
}
else
if
(
attr
.
getDataType
()
==
DataType
.
LONG
&&
attr
.
getLongValue
().
isPresent
())
{
attributes
.
put
(
attr
.
getKey
(),
attr
.
getLongValue
().
get
());
}
else
{
attributes
.
put
(
attr
.
getKey
(),
attr
.
getValueAsString
());
}
}
entityData
.
put
(
"kv"
,
attributes
);
entityData
.
put
(
"scope"
,
scope
);
JsonNode
body
=
JacksonUtil
.
OBJECT_MAPPER
.
valueToTree
(
entityData
);
log
.
debug
(
"Sending attributes data msg, entityId [{}], attributes [{}]"
,
entityId
,
body
);
ListenableFuture
<
Void
>
future
=
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
type
,
EdgeEventActionType
.
ATTRIBUTES_UPDATED
,
entityId
,
body
);
Futures
.
addCallback
(
future
,
new
FutureCallback
<>()
{
@Override
public
void
onSuccess
(
@Nullable
Void
unused
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
throwable
)
{
String
errMsg
=
String
.
format
(
"[%s] Failed to save edge event [%s]"
,
edge
.
getId
(),
attributesRequestMsg
);
log
.
error
(
errMsg
,
throwable
);
futureToSet
.
setException
(
new
RuntimeException
(
errMsg
,
throwable
));
}
},
dbCallbackExecutorService
);
}
catch
(
Exception
e
)
{
String
errMsg
=
String
.
format
(
"[%s] Failed to save attribute updates to the edge [%s]"
,
edge
.
getId
(),
attributesRequestMsg
);
log
.
error
(
errMsg
,
e
);
futureToSet
.
setException
(
new
RuntimeException
(
errMsg
,
e
));
}
}
@Override
public
void
onFailure
(
Throwable
t
)
{
String
errMsg
=
String
.
format
(
"[%s] Can't find attributes [%s]"
,
edge
.
getId
(),
attributesRequestMsg
);
log
.
error
(
errMsg
,
t
);
futureToSet
.
setException
(
new
RuntimeException
(
errMsg
,
t
));
}
},
dbCallbackExecutorService
);
return
futureToSet
;
}
@Override
public
ListenableFuture
<
Void
>
processRelationRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
RelationRequestMsg
relationRequestMsg
)
{
log
.
trace
(
"[{}] processRelationRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
relationRequestMsg
);
EntityId
entityId
=
EntityIdFactory
.
getByTypeAndUuid
(
EntityType
.
valueOf
(
relationRequestMsg
.
getEntityType
()),
new
UUID
(
relationRequestMsg
.
getEntityIdMSB
(),
relationRequestMsg
.
getEntityIdLSB
()));
List
<
ListenableFuture
<
List
<
EntityRelation
>>>
futures
=
new
ArrayList
<>();
futures
.
add
(
findRelationByQuery
(
tenantId
,
edge
,
entityId
,
EntitySearchDirection
.
FROM
));
futures
.
add
(
findRelationByQuery
(
tenantId
,
edge
,
entityId
,
EntitySearchDirection
.
TO
));
ListenableFuture
<
List
<
List
<
EntityRelation
>>>
relationsListFuture
=
Futures
.
allAsList
(
futures
);
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
Futures
.
addCallback
(
relationsListFuture
,
new
FutureCallback
<>()
{
@Override
public
void
onSuccess
(
@Nullable
List
<
List
<
EntityRelation
>>
relationsList
)
{
try
{
if
(
relationsList
!=
null
&&
!
relationsList
.
isEmpty
())
{
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
for
(
List
<
EntityRelation
>
entityRelations
:
relationsList
)
{
log
.
trace
(
"[{}] [{}] [{}] relation(s) are going to be pushed to edge."
,
edge
.
getId
(),
entityId
,
entityRelations
.
size
());
for
(
EntityRelation
relation
:
entityRelations
)
{
try
{
if
(!
relation
.
getFrom
().
getEntityType
().
equals
(
EntityType
.
EDGE
)
&&
!
relation
.
getTo
().
getEntityType
().
equals
(
EntityType
.
EDGE
))
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
RELATION
,
EdgeEventActionType
.
ADDED
,
null
,
JacksonUtil
.
OBJECT_MAPPER
.
valueToTree
(
relation
)));
}
}
catch
(
Exception
e
)
{
String
errMsg
=
String
.
format
(
"[%s] Exception during loading relation [%s] to edge on sync!"
,
edge
.
getId
(),
relation
);
log
.
error
(
errMsg
,
e
);
futureToSet
.
setException
(
new
RuntimeException
(
errMsg
,
e
));
return
;
}
}
}
Futures
.
addCallback
(
Futures
.
allAsList
(
futures
),
new
FutureCallback
<>()
{
@Override
public
void
onSuccess
(
@Nullable
List
<
Void
>
voids
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
throwable
)
{
String
errMsg
=
String
.
format
(
"[%s] Exception during saving edge events [%s]!"
,
edge
.
getId
(),
relationRequestMsg
);
log
.
error
(
errMsg
,
throwable
);
futureToSet
.
setException
(
new
RuntimeException
(
errMsg
,
throwable
));
}
},
dbCallbackExecutorService
);
}
else
{
futureToSet
.
set
(
null
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"Exception during loading relation(s) to edge on sync!"
,
e
);
futureToSet
.
setException
(
e
);
}
}
@Override
public
void
onFailure
(
Throwable
t
)
{
String
errMsg
=
String
.
format
(
"[%s] Can't find relation by query. Entity id [%s]!"
,
tenantId
,
entityId
);
log
.
error
(
errMsg
,
t
);
futureToSet
.
setException
(
new
RuntimeException
(
errMsg
,
t
));
}
},
dbCallbackExecutorService
);
return
futureToSet
;
}
private
ListenableFuture
<
List
<
EntityRelation
>>
findRelationByQuery
(
TenantId
tenantId
,
Edge
edge
,
EntityId
entityId
,
EntitySearchDirection
direction
)
{
EntityRelationsQuery
query
=
new
EntityRelationsQuery
();
query
.
setParameters
(
new
RelationsSearchParameters
(
entityId
,
direction
,
-
1
,
false
));
return
relationService
.
findByQuery
(
tenantId
,
query
);
}
@Override
public
ListenableFuture
<
Void
>
processDeviceCredentialsRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
DeviceCredentialsRequestMsg
deviceCredentialsRequestMsg
)
{
log
.
trace
(
"[{}] processDeviceCredentialsRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
deviceCredentialsRequestMsg
);
if
(
deviceCredentialsRequestMsg
.
getDeviceIdMSB
()
==
0
||
deviceCredentialsRequestMsg
.
getDeviceIdLSB
()
==
0
)
{
return
Futures
.
immediateFuture
(
null
);
}
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceCredentialsRequestMsg
.
getDeviceIdMSB
(),
deviceCredentialsRequestMsg
.
getDeviceIdLSB
()));
return
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
DEVICE
,
EdgeEventActionType
.
CREDENTIALS_UPDATED
,
deviceId
,
null
);
}
@Override
public
ListenableFuture
<
Void
>
processUserCredentialsRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
UserCredentialsRequestMsg
userCredentialsRequestMsg
)
{
log
.
trace
(
"[{}] processUserCredentialsRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
userCredentialsRequestMsg
);
if
(
userCredentialsRequestMsg
.
getUserIdMSB
()
==
0
||
userCredentialsRequestMsg
.
getUserIdLSB
()
==
0
)
{
return
Futures
.
immediateFuture
(
null
);
}
UserId
userId
=
new
UserId
(
new
UUID
(
userCredentialsRequestMsg
.
getUserIdMSB
(),
userCredentialsRequestMsg
.
getUserIdLSB
()));
return
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
USER
,
EdgeEventActionType
.
CREDENTIALS_UPDATED
,
userId
,
null
);
}
@Override
public
ListenableFuture
<
Void
>
processWidgetBundleTypesRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
WidgetBundleTypesRequestMsg
widgetBundleTypesRequestMsg
)
{
log
.
trace
(
"[{}] processWidgetBundleTypesRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
widgetBundleTypesRequestMsg
);
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
if
(
widgetBundleTypesRequestMsg
.
getWidgetBundleIdMSB
()
!=
0
&&
widgetBundleTypesRequestMsg
.
getWidgetBundleIdLSB
()
!=
0
)
{
WidgetsBundleId
widgetsBundleId
=
new
WidgetsBundleId
(
new
UUID
(
widgetBundleTypesRequestMsg
.
getWidgetBundleIdMSB
(),
widgetBundleTypesRequestMsg
.
getWidgetBundleIdLSB
()));
WidgetsBundle
widgetsBundleById
=
widgetsBundleService
.
findWidgetsBundleById
(
tenantId
,
widgetsBundleId
);
if
(
widgetsBundleById
!=
null
)
{
List
<
WidgetType
>
widgetTypesToPush
=
widgetTypeService
.
findWidgetTypesByTenantIdAndBundleAlias
(
widgetsBundleById
.
getTenantId
(),
widgetsBundleById
.
getAlias
());
for
(
WidgetType
widgetType
:
widgetTypesToPush
)
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
WIDGET_TYPE
,
EdgeEventActionType
.
ADDED
,
widgetType
.
getId
(),
null
));
}
}
}
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
}
@Override
public
ListenableFuture
<
Void
>
processEntityViewsRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
EntityViewsRequestMsg
entityViewsRequestMsg
)
{
log
.
trace
(
"[{}] processEntityViewsRequestMsg [{}][{}]"
,
tenantId
,
edge
.
getName
(),
entityViewsRequestMsg
);
EntityId
entityId
=
EntityIdFactory
.
getByTypeAndUuid
(
EntityType
.
valueOf
(
entityViewsRequestMsg
.
getEntityType
()),
new
UUID
(
entityViewsRequestMsg
.
getEntityIdMSB
(),
entityViewsRequestMsg
.
getEntityIdLSB
()));
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
Futures
.
addCallback
(
entityViewService
.
findEntityViewsByTenantIdAndEntityIdAsync
(
tenantId
,
entityId
),
new
FutureCallback
<>()
{
@Override
public
void
onSuccess
(
@Nullable
List
<
EntityView
>
entityViews
)
{
if
(
entityViews
==
null
||
entityViews
.
isEmpty
())
{
futureToSet
.
set
(
null
);
return
;
}
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
for
(
EntityView
entityView
:
entityViews
)
{
ListenableFuture
<
Boolean
>
future
=
relationService
.
checkRelationAsync
(
tenantId
,
edge
.
getId
(),
entityView
.
getId
(),
EntityRelation
.
CONTAINS_TYPE
,
RelationTypeGroup
.
EDGE
);
futures
.
add
(
Futures
.
transformAsync
(
future
,
result
->
{
if
(
Boolean
.
TRUE
.
equals
(
result
))
{
return
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
ENTITY_VIEW
,
EdgeEventActionType
.
ADDED
,
entityView
.
getId
(),
null
);
}
else
{
return
Futures
.
immediateFuture
(
null
);
}
},
dbCallbackExecutorService
));
}
Futures
.
addCallback
(
Futures
.
allAsList
(
futures
),
new
FutureCallback
<>()
{
@Override
public
void
onSuccess
(
@Nullable
List
<
Void
>
result
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Exception during loading relation to edge on sync!"
,
t
);
futureToSet
.
setException
(
t
);
}
},
dbCallbackExecutorService
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"[{}] Can't find entity views by entity id [{}]"
,
tenantId
,
entityId
,
t
);
futureToSet
.
setException
(
t
);
}
},
dbCallbackExecutorService
);
return
futureToSet
;
}
private
ListenableFuture
<
Void
>
saveEdgeEvent
(
TenantId
tenantId
,
EdgeId
edgeId
,
EdgeEventType
type
,
EdgeEventActionType
action
,
EntityId
entityId
,
JsonNode
body
)
{
log
.
trace
(
"Pushing edge event to edge queue. tenantId [{}], edgeId [{}], type [{}], action[{}], entityId [{}], body [{}]"
,
tenantId
,
edgeId
,
type
,
action
,
entityId
,
body
);
EdgeEvent
edgeEvent
=
EdgeUtils
.
constructEdgeEvent
(
tenantId
,
edgeId
,
type
,
action
,
entityId
,
body
);
return
Futures
.
transform
(
edgeEventService
.
saveAsync
(
edgeEvent
),
unused
->
{
tbClusterService
.
onEdgeEventUpdate
(
tenantId
,
edgeId
);
return
null
;
},
dbCallbackExecutorService
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/sync/EdgeRequestsService.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.sync
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.AttributesRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.EntityViewsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.RelationRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.RuleChainMetadataRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.UserCredentialsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.WidgetBundleTypesRequestMsg
;
public
interface
EdgeRequestsService
{
ListenableFuture
<
Void
>
processRuleChainMetadataRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
RuleChainMetadataRequestMsg
ruleChainMetadataRequestMsg
);
ListenableFuture
<
Void
>
processAttributesRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
AttributesRequestMsg
attributesRequestMsg
);
ListenableFuture
<
Void
>
processRelationRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
RelationRequestMsg
relationRequestMsg
);
ListenableFuture
<
Void
>
processDeviceCredentialsRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
DeviceCredentialsRequestMsg
deviceCredentialsRequestMsg
);
ListenableFuture
<
Void
>
processUserCredentialsRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
UserCredentialsRequestMsg
userCredentialsRequestMsg
);
ListenableFuture
<
Void
>
processWidgetBundleTypesRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
WidgetBundleTypesRequestMsg
widgetBundleTypesRequestMsg
);
ListenableFuture
<
Void
>
processEntityViewsRequestMsg
(
TenantId
tenantId
,
Edge
edge
,
EntityViewsRequestMsg
entityViewsRequestMsg
);
}
application/src/main/java/org/thingsboard/server/service/entitiy/AbstractTbEntityService.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.entitiy
;
import
com.google.common.util.concurrent.Futures
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.Getter
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.thingsboard.server.cluster.TbClusterService
;
import
org.thingsboard.server.common.data.EntityType
;
import
org.thingsboard.server.common.data.User
;
import
org.thingsboard.server.common.data.alarm.AlarmInfo
;
import
org.thingsboard.server.common.data.alarm.AlarmQuery
;
import
org.thingsboard.server.common.data.exception.ThingsboardErrorCode
;
import
org.thingsboard.server.common.data.exception.ThingsboardException
;
import
org.thingsboard.server.common.data.id.AlarmId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.EntityIdFactory
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.TimePageLink
;
import
org.thingsboard.server.dao.alarm.AlarmService
;
import
org.thingsboard.server.dao.customer.CustomerService
;
import
org.thingsboard.server.dao.edge.EdgeService
;
import
org.thingsboard.server.dao.model.ModelConstants
;
import
org.thingsboard.server.service.executors.DbCallbackExecutorService
;
import
org.thingsboard.server.service.sync.vc.EntitiesVersionControlService
;
import
org.thingsboard.server.service.telemetry.AlarmSubscriptionService
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
@Slf4j
public
abstract
class
AbstractTbEntityService
{
@Value
(
"${server.log_controller_error_stack_trace}"
)
@Getter
private
boolean
logControllerErrorStackTrace
;
@Autowired
protected
DbCallbackExecutorService
dbExecutor
;
@Autowired
(
required
=
false
)
protected
TbNotificationEntityService
notificationEntityService
;
@Autowired
(
required
=
false
)
protected
EdgeService
edgeService
;
@Autowired
protected
AlarmService
alarmService
;
@Autowired
protected
AlarmSubscriptionService
alarmSubscriptionService
;
@Autowired
protected
CustomerService
customerService
;
@Autowired
protected
TbClusterService
tbClusterService
;
@Autowired
(
required
=
false
)
private
EntitiesVersionControlService
vcService
;
protected
ListenableFuture
<
Void
>
removeAlarmsByEntityId
(
TenantId
tenantId
,
EntityId
entityId
)
{
ListenableFuture
<
PageData
<
AlarmInfo
>>
alarmsFuture
=
alarmService
.
findAlarms
(
tenantId
,
new
AlarmQuery
(
entityId
,
new
TimePageLink
(
Integer
.
MAX_VALUE
),
null
,
null
,
false
));
ListenableFuture
<
List
<
AlarmId
>>
alarmIdsFuture
=
Futures
.
transform
(
alarmsFuture
,
page
->
page
.
getData
().
stream
().
map
(
AlarmInfo:
:
getId
).
collect
(
Collectors
.
toList
()),
dbExecutor
);
return
Futures
.
transform
(
alarmIdsFuture
,
ids
->
{
ids
.
stream
().
map
(
alarmId
->
alarmService
.
deleteAlarm
(
tenantId
,
alarmId
)).
collect
(
Collectors
.
toList
());
return
null
;
},
dbExecutor
);
}
protected
<
T
>
T
checkNotNull
(
T
reference
)
throws
ThingsboardException
{
return
checkNotNull
(
reference
,
"Requested item wasn't found!"
);
}
protected
<
T
>
T
checkNotNull
(
T
reference
,
String
notFoundMessage
)
throws
ThingsboardException
{
if
(
reference
==
null
)
{
throw
new
ThingsboardException
(
notFoundMessage
,
ThingsboardErrorCode
.
ITEM_NOT_FOUND
);
}
return
reference
;
}
protected
<
T
>
T
checkNotNull
(
Optional
<
T
>
reference
)
throws
ThingsboardException
{
return
checkNotNull
(
reference
,
"Requested item wasn't found!"
);
}
protected
<
T
>
T
checkNotNull
(
Optional
<
T
>
reference
,
String
notFoundMessage
)
throws
ThingsboardException
{
if
(
reference
.
isPresent
())
{
return
reference
.
get
();
}
else
{
throw
new
ThingsboardException
(
notFoundMessage
,
ThingsboardErrorCode
.
ITEM_NOT_FOUND
);
}
}
protected
<
I
extends
EntityId
>
I
emptyId
(
EntityType
entityType
)
{
return
(
I
)
EntityIdFactory
.
getByTypeAndUuid
(
entityType
,
ModelConstants
.
NULL_UUID
);
}
protected
ListenableFuture
<
UUID
>
autoCommit
(
User
user
,
EntityId
entityId
)
throws
Exception
{
if
(
vcService
!=
null
)
{
return
vcService
.
autoCommit
(
user
,
entityId
);
}
else
{
// We do not support auto-commit for rule engine
return
Futures
.
immediateFailedFuture
(
new
RuntimeException
(
"Operation not supported!"
));
}
}
protected
ListenableFuture
<
UUID
>
autoCommit
(
User
user
,
EntityType
entityType
,
List
<
UUID
>
entityIds
)
throws
Exception
{
if
(
vcService
!=
null
)
{
return
vcService
.
autoCommit
(
user
,
entityType
,
entityIds
);
}
else
{
// We do not support auto-commit for rule engine
return
Futures
.
immediateFailedFuture
(
new
RuntimeException
(
"Operation not supported!"
));
}
}
}
application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java
0 → 100644
View file @
98ad3b98
/**
* Copyright © 2016-2022 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.entitiy
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Service
;
import
org.thingsboard.common.util.JacksonUtil
;
import
org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg
;
import
org.thingsboard.server.cluster.TbClusterService
;
import
org.thingsboard.server.common.data.DataConstants
;
import
org.thingsboard.server.common.data.Device
;
import
org.thingsboard.server.common.data.EntityType
;
import
org.thingsboard.server.common.data.HasName
;
import
org.thingsboard.server.common.data.Tenant
;
import
org.thingsboard.server.common.data.User
;
import
org.thingsboard.server.common.data.alarm.Alarm
;
import
org.thingsboard.server.common.data.audit.ActionType
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.edge.EdgeEventActionType
;
import
org.thingsboard.server.common.data.edge.EdgeEventType
;
import
org.thingsboard.server.common.data.id.CustomerId
;
import
org.thingsboard.server.common.data.id.DeviceId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.RuleChainId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent
;
import
org.thingsboard.server.common.data.relation.EntityRelation
;
import
org.thingsboard.server.common.data.rule.RuleChain
;
import
org.thingsboard.server.common.data.rule.RuleChainType
;
import
org.thingsboard.server.common.data.security.DeviceCredentials
;
import
org.thingsboard.server.common.msg.TbMsg
;
import
org.thingsboard.server.common.msg.TbMsgDataType
;
import
org.thingsboard.server.common.msg.TbMsgMetaData
;
import
org.thingsboard.server.service.action.EntityActionService
;
import
org.thingsboard.server.service.gateway_device.GatewayNotificationsService
;
import
java.util.List
;
@Slf4j
@Service
@RequiredArgsConstructor
public
class
DefaultTbNotificationEntityService
implements
TbNotificationEntityService
{
private
final
EntityActionService
entityActionService
;
private
final
TbClusterService
tbClusterService
;
private
final
GatewayNotificationsService
gatewayNotificationsService
;
@Override
public
<
I
extends
EntityId
>
void
logEntityAction
(
TenantId
tenantId
,
I
entityId
,
ActionType
actionType
,
User
user
,
Exception
e
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
null
,
null
,
actionType
,
user
,
e
,
additionalInfo
);
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
logEntityAction
(
TenantId
tenantId
,
I
entityId
,
E
entity
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
null
,
actionType
,
user
,
null
,
additionalInfo
);
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
logEntityAction
(
TenantId
tenantId
,
I
entityId
,
E
entity
,
ActionType
actionType
,
User
user
,
Exception
e
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
null
,
actionType
,
user
,
e
,
additionalInfo
);
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
logEntityAction
(
TenantId
tenantId
,
I
entityId
,
E
entity
,
CustomerId
customerId
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
customerId
,
actionType
,
user
,
null
,
additionalInfo
);
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
logEntityAction
(
TenantId
tenantId
,
I
entityId
,
E
entity
,
CustomerId
customerId
,
ActionType
actionType
,
User
user
,
Exception
e
,
Object
...
additionalInfo
)
{
if
(
user
!=
null
)
{
entityActionService
.
logEntityAction
(
user
,
entityId
,
entity
,
customerId
,
actionType
,
e
,
additionalInfo
);
}
else
if
(
e
==
null
)
{
entityActionService
.
pushEntityActionToRuleEngine
(
entityId
,
entity
,
tenantId
,
customerId
,
actionType
,
null
,
additionalInfo
);
}
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
notifyDeleteEntity
(
TenantId
tenantId
,
I
entityId
,
E
entity
,
CustomerId
customerId
,
ActionType
actionType
,
List
<
EdgeId
>
relatedEdgeIds
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
customerId
,
actionType
,
user
,
additionalInfo
);
sendDeleteNotificationMsg
(
tenantId
,
entityId
,
relatedEdgeIds
,
null
);
}
@Override
public
void
notifyDeleteAlarm
(
TenantId
tenantId
,
Alarm
alarm
,
EntityId
originatorId
,
CustomerId
customerId
,
List
<
EdgeId
>
relatedEdgeIds
,
User
user
,
String
body
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
originatorId
,
alarm
,
customerId
,
ActionType
.
DELETED
,
user
,
additionalInfo
);
sendAlarmDeleteNotificationMsg
(
tenantId
,
alarm
,
relatedEdgeIds
,
body
);
}
@Override
public
void
notifyDeleteRuleChain
(
TenantId
tenantId
,
RuleChain
ruleChain
,
List
<
EdgeId
>
relatedEdgeIds
,
User
user
)
{
RuleChainId
ruleChainId
=
ruleChain
.
getId
();
logEntityAction
(
tenantId
,
ruleChainId
,
ruleChain
,
null
,
ActionType
.
DELETED
,
user
,
null
,
ruleChainId
.
toString
());
if
(
RuleChainType
.
EDGE
.
equals
(
ruleChain
.
getType
()))
{
sendDeleteNotificationMsg
(
tenantId
,
ruleChainId
,
relatedEdgeIds
,
null
);
}
}
@Override
public
<
I
extends
EntityId
>
void
notifySendMsgToEdgeService
(
TenantId
tenantId
,
I
entityId
,
EdgeEventActionType
edgeEventActionType
)
{
sendEntityNotificationMsg
(
tenantId
,
entityId
,
edgeEventActionType
);
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
notifyAssignOrUnassignEntityToCustomer
(
TenantId
tenantId
,
I
entityId
,
CustomerId
customerId
,
E
entity
,
ActionType
actionType
,
User
user
,
boolean
sendToEdge
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
customerId
,
actionType
,
user
,
additionalInfo
);
if
(
sendToEdge
)
{
sendEntityNotificationMsg
(
tenantId
,
entityId
,
edgeTypeByActionType
(
actionType
),
JacksonUtil
.
toString
(
customerId
));
}
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
notifyAssignOrUnassignEntityToEdge
(
TenantId
tenantId
,
I
entityId
,
CustomerId
customerId
,
EdgeId
edgeId
,
E
entity
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
customerId
,
actionType
,
user
,
additionalInfo
);
sendEntityAssignToEdgeNotificationMsg
(
tenantId
,
edgeId
,
entityId
,
edgeTypeByActionType
(
actionType
));
}
@Override
public
void
notifyCreateOrUpdateTenant
(
Tenant
tenant
,
ComponentLifecycleEvent
event
)
{
tbClusterService
.
onTenantChange
(
tenant
,
null
);
tbClusterService
.
broadcastEntityStateChangeEvent
(
tenant
.
getId
(),
tenant
.
getId
(),
event
);
}
@Override
public
void
notifyDeleteTenant
(
Tenant
tenant
)
{
tbClusterService
.
onTenantDelete
(
tenant
,
null
);
tbClusterService
.
broadcastEntityStateChangeEvent
(
tenant
.
getId
(),
tenant
.
getId
(),
ComponentLifecycleEvent
.
DELETED
);
}
@Override
public
void
notifyCreateOrUpdateDevice
(
TenantId
tenantId
,
DeviceId
deviceId
,
CustomerId
customerId
,
Device
device
,
Device
oldDevice
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
tbClusterService
.
onDeviceUpdated
(
device
,
oldDevice
);
logEntityAction
(
tenantId
,
deviceId
,
device
,
customerId
,
actionType
,
user
,
additionalInfo
);
}
@Override
public
void
notifyDeleteDevice
(
TenantId
tenantId
,
DeviceId
deviceId
,
CustomerId
customerId
,
Device
device
,
List
<
EdgeId
>
relatedEdgeIds
,
User
user
,
Object
...
additionalInfo
)
{
gatewayNotificationsService
.
onDeviceDeleted
(
device
);
tbClusterService
.
onDeviceDeleted
(
device
,
null
);
notifyDeleteEntity
(
tenantId
,
deviceId
,
device
,
customerId
,
ActionType
.
DELETED
,
relatedEdgeIds
,
user
,
additionalInfo
);
}
@Override
public
void
notifyUpdateDeviceCredentials
(
TenantId
tenantId
,
DeviceId
deviceId
,
CustomerId
customerId
,
Device
device
,
DeviceCredentials
deviceCredentials
,
User
user
)
{
tbClusterService
.
pushMsgToCore
(
new
DeviceCredentialsUpdateNotificationMsg
(
tenantId
,
deviceCredentials
.
getDeviceId
(),
deviceCredentials
),
null
);
sendEntityNotificationMsg
(
tenantId
,
deviceId
,
EdgeEventActionType
.
CREDENTIALS_UPDATED
);
logEntityAction
(
tenantId
,
deviceId
,
device
,
customerId
,
ActionType
.
CREDENTIALS_UPDATED
,
user
,
deviceCredentials
);
}
@Override
public
void
notifyAssignDeviceToTenant
(
TenantId
tenantId
,
TenantId
newTenantId
,
DeviceId
deviceId
,
CustomerId
customerId
,
Device
device
,
Tenant
tenant
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
deviceId
,
device
,
customerId
,
ActionType
.
ASSIGNED_TO_TENANT
,
user
,
additionalInfo
);
pushAssignedFromNotification
(
tenant
,
newTenantId
,
device
);
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
notifyCreateOrUpdateEntity
(
TenantId
tenantId
,
I
entityId
,
E
entity
,
CustomerId
customerId
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
customerId
,
actionType
,
user
,
additionalInfo
);
if
(
actionType
==
ActionType
.
UPDATED
)
{
sendEntityNotificationMsg
(
tenantId
,
entityId
,
EdgeEventActionType
.
UPDATED
);
}
}
@Override
public
void
notifyCreateOrUpdateOrDeleteEdge
(
TenantId
tenantId
,
EdgeId
edgeId
,
CustomerId
customerId
,
Edge
edge
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
ComponentLifecycleEvent
lifecycleEvent
;
switch
(
actionType
)
{
case
ADDED:
lifecycleEvent
=
ComponentLifecycleEvent
.
CREATED
;
break
;
case
UPDATED:
lifecycleEvent
=
ComponentLifecycleEvent
.
UPDATED
;
break
;
case
DELETED:
lifecycleEvent
=
ComponentLifecycleEvent
.
DELETED
;
break
;
default
:
throw
new
IllegalArgumentException
(
"Unknown actionType: "
+
actionType
);
}
tbClusterService
.
broadcastEntityStateChangeEvent
(
tenantId
,
edgeId
,
lifecycleEvent
);
logEntityAction
(
tenantId
,
edgeId
,
edge
,
customerId
,
actionType
,
user
,
additionalInfo
);
}
@Override
public
void
notifyCreateOrUpdateAlarm
(
Alarm
alarm
,
ActionType
actionType
,
User
user
,
Object
...
additionalInfo
)
{
logEntityAction
(
alarm
.
getTenantId
(),
alarm
.
getOriginator
(),
alarm
,
alarm
.
getCustomerId
(),
actionType
,
user
,
additionalInfo
);
sendEntityNotificationMsg
(
alarm
.
getTenantId
(),
alarm
.
getId
(),
edgeTypeByActionType
(
actionType
));
}
@Override
public
<
E
extends
HasName
,
I
extends
EntityId
>
void
notifyCreateOrUpdateOrDelete
(
TenantId
tenantId
,
CustomerId
customerId
,
I
entityId
,
E
entity
,
User
user
,
ActionType
actionType
,
boolean
sendNotifyMsgToEdge
,
Exception
e
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
entityId
,
entity
,
customerId
,
actionType
,
user
,
e
,
additionalInfo
);
if
(
sendNotifyMsgToEdge
)
{
sendEntityNotificationMsg
(
tenantId
,
entityId
,
edgeTypeByActionType
(
actionType
));
}
}
@Override
public
void
notifyRelation
(
TenantId
tenantId
,
CustomerId
customerId
,
EntityRelation
relation
,
User
user
,
ActionType
actionType
,
Object
...
additionalInfo
)
{
logEntityAction
(
tenantId
,
relation
.
getFrom
(),
null
,
customerId
,
actionType
,
user
,
additionalInfo
);
logEntityAction
(
tenantId
,
relation
.
getTo
(),
null
,
customerId
,
actionType
,
user
,
additionalInfo
);
if
(!
EntityType
.
EDGE
.
equals
(
relation
.
getFrom
().
getEntityType
())
&&
!
EntityType
.
EDGE
.
equals
(
relation
.
getTo
().
getEntityType
()))
{
sendNotificationMsgToEdge
(
tenantId
,
null
,
null
,
JacksonUtil
.
toString
(
relation
),
EdgeEventType
.
RELATION
,
edgeTypeByActionType
(
actionType
));
}
}
private
void
sendEntityNotificationMsg
(
TenantId
tenantId
,
EntityId
entityId
,
EdgeEventActionType
action
)
{
sendEntityNotificationMsg
(
tenantId
,
entityId
,
action
,
null
);
}
private
void
sendEntityNotificationMsg
(
TenantId
tenantId
,
EntityId
entityId
,
EdgeEventActionType
action
,
String
body
)
{
sendNotificationMsgToEdge
(
tenantId
,
null
,
entityId
,
body
,
null
,
action
);
}
private
void
sendAlarmDeleteNotificationMsg
(
TenantId
tenantId
,
Alarm
alarm
,
List
<
EdgeId
>
edgeIds
,
String
body
)
{
sendDeleteNotificationMsg
(
tenantId
,
alarm
.
getId
(),
edgeIds
,
body
);
}
private
void
sendDeleteNotificationMsg
(
TenantId
tenantId
,
EntityId
entityId
,
List
<
EdgeId
>
edgeIds
,
String
body
)
{
if
(
edgeIds
!=
null
&&
!
edgeIds
.
isEmpty
())
{
for
(
EdgeId
edgeId
:
edgeIds
)
{
sendNotificationMsgToEdge
(
tenantId
,
edgeId
,
entityId
,
body
,
null
,
EdgeEventActionType
.
DELETED
);
}
}
}
private
void
sendEntityAssignToEdgeNotificationMsg
(
TenantId
tenantId
,
EdgeId
edgeId
,
EntityId
entityId
,
EdgeEventActionType
action
)
{
sendNotificationMsgToEdge
(
tenantId
,
edgeId
,
entityId
,
null
,
null
,
action
);
}
private
void
sendNotificationMsgToEdge
(
TenantId
tenantId
,
EdgeId
edgeId
,
EntityId
entityId
,
String
body
,
EdgeEventType
type
,
EdgeEventActionType
action
)
{
tbClusterService
.
sendNotificationMsgToEdge
(
tenantId
,
edgeId
,
entityId
,
body
,
type
,
action
);
}
private
void
pushAssignedFromNotification
(
Tenant
currentTenant
,
TenantId
newTenantId
,
Device
assignedDevice
)
{
String
data
=
JacksonUtil
.
toString
(
JacksonUtil
.
valueToTree
(
assignedDevice
));
if
(
data
!=
null
)
{
TbMsg
tbMsg
=
TbMsg
.
newMsg
(
DataConstants
.
ENTITY_ASSIGNED_FROM_TENANT
,
assignedDevice
.
getId
(),
assignedDevice
.
getCustomerId
(),
getMetaDataForAssignedFrom
(
currentTenant
),
TbMsgDataType
.
JSON
,
data
);
tbClusterService
.
pushMsgToRuleEngine
(
newTenantId
,
assignedDevice
.
getId
(),
tbMsg
,
null
);
}
}
private
TbMsgMetaData
getMetaDataForAssignedFrom
(
Tenant
tenant
)
{
TbMsgMetaData
metaData
=
new
TbMsgMetaData
();
metaData
.
putValue
(
"assignedFromTenantId"
,
tenant
.
getId
().
getId
().
toString
());
metaData
.
putValue
(
"assignedFromTenantName"
,
tenant
.
getName
());
return
metaData
;
}
public
static
EdgeEventActionType
edgeTypeByActionType
(
ActionType
actionType
)
{
switch
(
actionType
)
{
case
ADDED:
return
EdgeEventActionType
.
ADDED
;
case
UPDATED:
return
EdgeEventActionType
.
UPDATED
;
case
ALARM_ACK:
return
EdgeEventActionType
.
ALARM_ACK
;
case
ALARM_CLEAR:
return
EdgeEventActionType
.
ALARM_CLEAR
;
case
DELETED:
return
EdgeEventActionType
.
DELETED
;
case
RELATION_ADD_OR_UPDATE:
return
EdgeEventActionType
.
RELATION_ADD_OR_UPDATE
;
case
RELATION_DELETED:
return
EdgeEventActionType
.
RELATION_DELETED
;
case
ASSIGNED_TO_CUSTOMER:
return
EdgeEventActionType
.
ASSIGNED_TO_CUSTOMER
;
case
UNASSIGNED_FROM_CUSTOMER:
return
EdgeEventActionType
.
UNASSIGNED_FROM_CUSTOMER
;
case
ASSIGNED_TO_EDGE:
return
EdgeEventActionType
.
ASSIGNED_TO_EDGE
;
case
UNASSIGNED_FROM_EDGE:
return
EdgeEventActionType
.
UNASSIGNED_FROM_EDGE
;
default
:
return
null
;
}
}
}
Prev
1
…
10
11
12
13
14
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment