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
Show whitespace changes
Inline
Side-by-side
Too many changes to show.
To preserve performance only
20 of 273+
files are displayed.
Plain diff
Email patch
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/DevicesEdgeEventFetcher.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.fetch
;
import
lombok.AllArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.Device
;
import
org.thingsboard.server.common.data.EdgeUtils
;
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.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.dao.device.DeviceService
;
@AllArgsConstructor
@Slf4j
public
class
DevicesEdgeEventFetcher
extends
BasePageableEdgeEventFetcher
<
Device
>
{
private
final
DeviceService
deviceService
;
@Override
PageData
<
Device
>
fetchPageData
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
{
return
deviceService
.
findDevicesByTenantIdAndEdgeId
(
tenantId
,
edge
.
getId
(),
pageLink
);
}
@Override
EdgeEvent
constructEdgeEvent
(
TenantId
tenantId
,
Edge
edge
,
Device
device
)
{
return
EdgeUtils
.
constructEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
DEVICE
,
EdgeEventActionType
.
ADDED
,
device
.
getId
(),
null
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/EdgeEventFetcher.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.fetch
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
public
interface
EdgeEventFetcher
{
PageLink
getPageLink
(
int
pageSize
);
PageData
<
EdgeEvent
>
fetchEdgeEvents
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
throws
Exception
;
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/EntityViewsEdgeEventFetcher.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.fetch
;
import
lombok.AllArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.EdgeUtils
;
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.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.dao.entityview.EntityViewService
;
@AllArgsConstructor
@Slf4j
public
class
EntityViewsEdgeEventFetcher
extends
BasePageableEdgeEventFetcher
<
EntityView
>
{
private
final
EntityViewService
entityViewService
;
@Override
PageData
<
EntityView
>
fetchPageData
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
{
return
entityViewService
.
findEntityViewsByTenantIdAndEdgeId
(
tenantId
,
edge
.
getId
(),
pageLink
);
}
@Override
EdgeEvent
constructEdgeEvent
(
TenantId
tenantId
,
Edge
edge
,
EntityView
entityView
)
{
return
EdgeUtils
.
constructEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
ENTITY_VIEW
,
EdgeEventActionType
.
ADDED
,
entityView
.
getId
(),
null
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/GeneralEdgeEventFetcher.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.fetch
;
import
lombok.AllArgsConstructor
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.page.SortOrder
;
import
org.thingsboard.server.common.data.page.TimePageLink
;
import
org.thingsboard.server.dao.edge.EdgeEventService
;
@AllArgsConstructor
public
class
GeneralEdgeEventFetcher
implements
EdgeEventFetcher
{
private
final
Long
queueStartTs
;
private
final
EdgeEventService
edgeEventService
;
@Override
public
PageLink
getPageLink
(
int
pageSize
)
{
return
new
TimePageLink
(
pageSize
,
0
,
null
,
new
SortOrder
(
"createdTime"
,
SortOrder
.
Direction
.
ASC
),
queueStartTs
,
null
);
}
@Override
public
PageData
<
EdgeEvent
>
fetchEdgeEvents
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
{
return
edgeEventService
.
findEdgeEvents
(
tenantId
,
edge
.
getId
(),
(
TimePageLink
)
pageLink
,
true
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/OtaPackagesEdgeEventFetcher.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.fetch
;
import
lombok.AllArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.OtaPackageInfo
;
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.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.dao.ota.OtaPackageService
;
@AllArgsConstructor
@Slf4j
public
class
OtaPackagesEdgeEventFetcher
extends
BasePageableEdgeEventFetcher
<
OtaPackageInfo
>
{
private
final
OtaPackageService
otaPackageService
;
@Override
PageData
<
OtaPackageInfo
>
fetchPageData
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
{
return
otaPackageService
.
findTenantOtaPackagesByTenantId
(
tenantId
,
pageLink
);
}
@Override
EdgeEvent
constructEdgeEvent
(
TenantId
tenantId
,
Edge
edge
,
OtaPackageInfo
otaPackageInfo
)
{
return
EdgeUtils
.
constructEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
OTA_PACKAGE
,
EdgeEventActionType
.
ADDED
,
otaPackageInfo
.
getId
(),
null
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/QueuesEdgeEventFetcher.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.fetch
;
import
lombok.AllArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.EdgeUtils
;
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.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.queue.Queue
;
import
org.thingsboard.server.dao.queue.QueueService
;
@AllArgsConstructor
@Slf4j
public
class
QueuesEdgeEventFetcher
extends
BasePageableEdgeEventFetcher
<
Queue
>
{
private
final
QueueService
queueService
;
@Override
PageData
<
Queue
>
fetchPageData
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
{
return
queueService
.
findQueuesByTenantId
(
tenantId
,
pageLink
);
}
@Override
EdgeEvent
constructEdgeEvent
(
TenantId
tenantId
,
Edge
edge
,
Queue
queue
)
{
return
EdgeUtils
.
constructEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
QUEUE
,
EdgeEventActionType
.
ADDED
,
queue
.
getId
(),
null
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/RuleChainsEdgeEventFetcher.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.fetch
;
import
lombok.AllArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.EdgeUtils
;
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.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.rule.RuleChain
;
import
org.thingsboard.server.dao.rule.RuleChainService
;
@Slf4j
@AllArgsConstructor
public
class
RuleChainsEdgeEventFetcher
extends
BasePageableEdgeEventFetcher
<
RuleChain
>
{
private
final
RuleChainService
ruleChainService
;
@Override
PageData
<
RuleChain
>
fetchPageData
(
TenantId
tenantId
,
Edge
edge
,
PageLink
pageLink
)
{
return
ruleChainService
.
findRuleChainsByTenantIdAndEdgeId
(
tenantId
,
edge
.
getId
(),
pageLink
);
}
@Override
EdgeEvent
constructEdgeEvent
(
TenantId
tenantId
,
Edge
edge
,
RuleChain
ruleChain
)
{
return
EdgeUtils
.
constructEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
RULE_CHAIN
,
EdgeEventActionType
.
ADDED
,
ruleChain
.
getId
(),
null
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/SystemWidgetsBundlesEdgeEventFetcher.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.fetch
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.widget.WidgetsBundle
;
import
org.thingsboard.server.dao.widget.WidgetsBundleService
;
@Slf4j
public
class
SystemWidgetsBundlesEdgeEventFetcher
extends
BaseWidgetsBundlesEdgeEventFetcher
{
public
SystemWidgetsBundlesEdgeEventFetcher
(
WidgetsBundleService
widgetsBundleService
)
{
super
(
widgetsBundleService
);
}
@Override
protected
PageData
<
WidgetsBundle
>
findWidgetsBundles
(
TenantId
tenantId
,
PageLink
pageLink
)
{
return
widgetsBundleService
.
findSystemWidgetsBundlesByPageLink
(
tenantId
,
pageLink
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/TenantAdminUsersEdgeEventFetcher.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.fetch
;
import
org.thingsboard.server.common.data.User
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.dao.user.UserService
;
public
class
TenantAdminUsersEdgeEventFetcher
extends
BaseUsersEdgeEventFetcher
{
public
TenantAdminUsersEdgeEventFetcher
(
UserService
userService
)
{
super
(
userService
);
}
@Override
protected
PageData
<
User
>
findUsers
(
TenantId
tenantId
,
PageLink
pageLink
)
{
return
userService
.
findTenantAdmins
(
tenantId
,
pageLink
);
}
}
\ No newline at end of file
application/src/main/java/org/thingsboard/server/service/edge/rpc/fetch/TenantWidgetsBundlesEdgeEventFetcher.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.fetch
;
import
lombok.extern.slf4j.Slf4j
;
import
org.thingsboard.server.common.data.edge.Edge
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.widget.WidgetsBundle
;
import
org.thingsboard.server.dao.widget.WidgetsBundleService
;
@Slf4j
public
class
TenantWidgetsBundlesEdgeEventFetcher
extends
BaseWidgetsBundlesEdgeEventFetcher
{
public
TenantWidgetsBundlesEdgeEventFetcher
(
WidgetsBundleService
widgetsBundleService
)
{
super
(
widgetsBundleService
);
}
@Override
protected
PageData
<
WidgetsBundle
>
findWidgetsBundles
(
TenantId
tenantId
,
PageLink
pageLink
)
{
return
widgetsBundleService
.
findTenantWidgetsBundlesByTenantId
(
tenantId
,
pageLink
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/AdminSettingsEdgeProcessor.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
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.common.util.JacksonUtil
;
import
org.thingsboard.server.common.data.AdminSettings
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.gen.edge.v1.AdminSettingsUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
@Component
@Slf4j
@TbCoreComponent
public
class
AdminSettingsEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertAdminSettingsEventToDownlink
(
EdgeEvent
edgeEvent
)
{
AdminSettings
adminSettings
=
JacksonUtil
.
OBJECT_MAPPER
.
convertValue
(
edgeEvent
.
getBody
(),
AdminSettings
.
class
);
AdminSettingsUpdateMsg
adminSettingsUpdateMsg
=
adminSettingsMsgConstructor
.
constructAdminSettingsUpdateMsg
(
adminSettings
);
return
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAdminSettingsUpdateMsg
(
adminSettingsUpdateMsg
)
.
build
();
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/AlarmEdgeProcessor.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.alarm.Alarm
;
import
org.thingsboard.server.common.data.alarm.AlarmSeverity
;
import
org.thingsboard.server.common.data.alarm.AlarmStatus
;
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.AlarmId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
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.List
;
import
java.util.UUID
;
@Component
@Slf4j
@TbCoreComponent
public
class
AlarmEdgeProcessor
extends
BaseEdgeProcessor
{
public
ListenableFuture
<
Void
>
processAlarmFromEdge
(
TenantId
tenantId
,
AlarmUpdateMsg
alarmUpdateMsg
)
{
log
.
trace
(
"[{}] onAlarmUpdate [{}]"
,
tenantId
,
alarmUpdateMsg
);
EntityId
originatorId
=
getAlarmOriginator
(
tenantId
,
alarmUpdateMsg
.
getOriginatorName
(),
EntityType
.
valueOf
(
alarmUpdateMsg
.
getOriginatorType
()));
if
(
originatorId
==
null
)
{
log
.
warn
(
"Originator not found for the alarm msg {}"
,
alarmUpdateMsg
);
return
Futures
.
immediateFuture
(
null
);
}
try
{
Alarm
existentAlarm
=
alarmService
.
findLatestByOriginatorAndType
(
tenantId
,
originatorId
,
alarmUpdateMsg
.
getType
()).
get
();
switch
(
alarmUpdateMsg
.
getMsgType
())
{
case
ENTITY_CREATED_RPC_MESSAGE:
case
ENTITY_UPDATED_RPC_MESSAGE:
if
(
existentAlarm
==
null
||
existentAlarm
.
getStatus
().
isCleared
())
{
existentAlarm
=
new
Alarm
();
existentAlarm
.
setTenantId
(
tenantId
);
existentAlarm
.
setType
(
alarmUpdateMsg
.
getName
());
existentAlarm
.
setOriginator
(
originatorId
);
existentAlarm
.
setSeverity
(
AlarmSeverity
.
valueOf
(
alarmUpdateMsg
.
getSeverity
()));
existentAlarm
.
setStartTs
(
alarmUpdateMsg
.
getStartTs
());
existentAlarm
.
setClearTs
(
alarmUpdateMsg
.
getClearTs
());
existentAlarm
.
setPropagate
(
alarmUpdateMsg
.
getPropagate
());
}
existentAlarm
.
setStatus
(
AlarmStatus
.
valueOf
(
alarmUpdateMsg
.
getStatus
()));
existentAlarm
.
setAckTs
(
alarmUpdateMsg
.
getAckTs
());
existentAlarm
.
setEndTs
(
alarmUpdateMsg
.
getEndTs
());
existentAlarm
.
setDetails
(
JacksonUtil
.
OBJECT_MAPPER
.
readTree
(
alarmUpdateMsg
.
getDetails
()));
alarmService
.
createOrUpdateAlarm
(
existentAlarm
);
break
;
case
ALARM_ACK_RPC_MESSAGE:
if
(
existentAlarm
!=
null
)
{
alarmService
.
ackAlarm
(
tenantId
,
existentAlarm
.
getId
(),
alarmUpdateMsg
.
getAckTs
());
}
break
;
case
ALARM_CLEAR_RPC_MESSAGE:
if
(
existentAlarm
!=
null
)
{
alarmService
.
clearAlarm
(
tenantId
,
existentAlarm
.
getId
(),
JacksonUtil
.
OBJECT_MAPPER
.
readTree
(
alarmUpdateMsg
.
getDetails
()),
alarmUpdateMsg
.
getAckTs
());
}
break
;
case
ENTITY_DELETED_RPC_MESSAGE:
if
(
existentAlarm
!=
null
)
{
alarmService
.
deleteAlarm
(
tenantId
,
existentAlarm
.
getId
());
}
break
;
}
return
Futures
.
immediateFuture
(
null
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Failed to process alarm update msg [{}]"
,
alarmUpdateMsg
,
e
);
return
Futures
.
immediateFailedFuture
(
new
RuntimeException
(
"Failed to process alarm update msg"
,
e
));
}
}
private
EntityId
getAlarmOriginator
(
TenantId
tenantId
,
String
entityName
,
EntityType
entityType
)
{
switch
(
entityType
)
{
case
DEVICE:
return
deviceService
.
findDeviceByTenantIdAndName
(
tenantId
,
entityName
).
getId
();
case
ASSET:
return
assetService
.
findAssetByTenantIdAndName
(
tenantId
,
entityName
).
getId
();
case
ENTITY_VIEW:
return
entityViewService
.
findEntityViewByTenantIdAndName
(
tenantId
,
entityName
).
getId
();
default
:
return
null
;
}
}
public
DownlinkMsg
convertAlarmEventToDownlink
(
EdgeEvent
edgeEvent
)
{
AlarmId
alarmId
=
new
AlarmId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
case
ALARM_ACK:
case
ALARM_CLEAR:
try
{
Alarm
alarm
=
alarmService
.
findAlarmByIdAsync
(
edgeEvent
.
getTenantId
(),
alarmId
).
get
();
if
(
alarm
!=
null
)
{
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAlarmUpdateMsg
(
alarmMsgConstructor
.
constructAlarmUpdatedMsg
(
edgeEvent
.
getTenantId
(),
msgType
,
alarm
))
.
build
();
}
}
catch
(
Exception
e
)
{
log
.
error
(
"Can't process alarm msg [{}] [{}]"
,
edgeEvent
,
msgType
,
e
);
}
break
;
case
DELETED:
Alarm
alarm
=
JacksonUtil
.
OBJECT_MAPPER
.
convertValue
(
edgeEvent
.
getBody
(),
Alarm
.
class
);
AlarmUpdateMsg
alarmUpdateMsg
=
alarmMsgConstructor
.
constructAlarmUpdatedMsg
(
edgeEvent
.
getTenantId
(),
msgType
,
alarm
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAlarmUpdateMsg
(
alarmUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processAlarmNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
throws
JsonProcessingException
{
EdgeEventActionType
actionType
=
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
());
AlarmId
alarmId
=
new
AlarmId
(
new
UUID
(
edgeNotificationMsg
.
getEntityIdMSB
(),
edgeNotificationMsg
.
getEntityIdLSB
()));
switch
(
actionType
)
{
case
DELETED:
EdgeId
edgeId
=
new
EdgeId
(
new
UUID
(
edgeNotificationMsg
.
getEdgeIdMSB
(),
edgeNotificationMsg
.
getEdgeIdLSB
()));
Alarm
deletedAlarm
=
JacksonUtil
.
OBJECT_MAPPER
.
readValue
(
edgeNotificationMsg
.
getBody
(),
Alarm
.
class
);
return
saveEdgeEvent
(
tenantId
,
edgeId
,
EdgeEventType
.
ALARM
,
actionType
,
alarmId
,
JacksonUtil
.
OBJECT_MAPPER
.
valueToTree
(
deletedAlarm
));
default
:
ListenableFuture
<
Alarm
>
alarmFuture
=
alarmService
.
findAlarmByIdAsync
(
tenantId
,
alarmId
);
return
Futures
.
transformAsync
(
alarmFuture
,
alarm
->
{
if
(
alarm
==
null
)
{
return
Futures
.
immediateFuture
(
null
);
}
EdgeEventType
type
=
EdgeUtils
.
getEdgeEventTypeByEntityType
(
alarm
.
getOriginator
().
getEntityType
());
if
(
type
==
null
)
{
return
Futures
.
immediateFuture
(
null
);
}
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
EdgeId
>
pageData
;
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
do
{
pageData
=
edgeService
.
findRelatedEdgeIdsByEntityId
(
tenantId
,
alarm
.
getOriginator
(),
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
for
(
EdgeId
relatedEdgeId
:
pageData
.
getData
())
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
relatedEdgeId
,
EdgeEventType
.
ALARM
,
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
()),
alarmId
,
null
));
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
},
dbCallbackExecutorService
);
}
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/AssetEdgeProcessor.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.DeviceProfile
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.asset.Asset
;
import
org.thingsboard.server.common.data.asset.AssetProfile
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.AssetId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.AssetUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
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
AssetEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertAssetEventToDownlink
(
EdgeEvent
edgeEvent
)
{
AssetId
assetId
=
new
AssetId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
case
ASSIGNED_TO_EDGE:
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
Asset
asset
=
assetService
.
findAssetById
(
edgeEvent
.
getTenantId
(),
assetId
);
if
(
asset
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
AssetUpdateMsg
assetUpdateMsg
=
assetMsgConstructor
.
constructAssetUpdatedMsg
(
msgType
,
asset
);
DownlinkMsg
.
Builder
builder
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAssetUpdateMsg
(
assetUpdateMsg
);
if
(
UpdateMsgType
.
ENTITY_CREATED_RPC_MESSAGE
.
equals
(
msgType
))
{
AssetProfile
assetProfile
=
assetProfileService
.
findAssetProfileById
(
edgeEvent
.
getTenantId
(),
asset
.
getAssetProfileId
());
builder
.
addAssetProfileUpdateMsg
(
assetProfileMsgConstructor
.
constructAssetProfileUpdatedMsg
(
msgType
,
assetProfile
));
}
downlinkMsg
=
builder
.
build
();
}
break
;
case
DELETED:
case
UNASSIGNED_FROM_EDGE:
AssetUpdateMsg
assetUpdateMsg
=
assetMsgConstructor
.
constructAssetDeleteMsg
(
assetId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAssetUpdateMsg
(
assetUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processAssetNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotification
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/AssetProfileEdgeProcessor.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.asset.AssetProfile
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.AssetProfileId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.AssetProfileUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
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
AssetProfileEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertAssetProfileEventToDownlink
(
EdgeEvent
edgeEvent
)
{
AssetProfileId
assetProfileId
=
new
AssetProfileId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
AssetProfile
assetProfile
=
assetProfileService
.
findAssetProfileById
(
edgeEvent
.
getTenantId
(),
assetProfileId
);
if
(
assetProfile
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
AssetProfileUpdateMsg
assetProfileUpdateMsg
=
assetProfileMsgConstructor
.
constructAssetProfileUpdatedMsg
(
msgType
,
assetProfile
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAssetProfileUpdateMsg
(
assetProfileUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
AssetProfileUpdateMsg
assetProfileUpdateMsg
=
assetProfileMsgConstructor
.
constructAssetProfileDeleteMsg
(
assetProfileId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addAssetProfileUpdateMsg
(
assetProfileUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processAssetProfileNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/BaseEdgeProcessor.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.databind.JsonNode
;
import
com.google.common.util.concurrent.Futures
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Lazy
;
import
org.thingsboard.server.cluster.TbClusterService
;
import
org.thingsboard.server.common.data.Device
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.EntityType
;
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.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.RuleChainId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.id.UserId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.rule.RuleChain
;
import
org.thingsboard.server.common.data.rule.RuleChainConnectionInfo
;
import
org.thingsboard.server.dao.alarm.AlarmService
;
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.customer.CustomerService
;
import
org.thingsboard.server.dao.dashboard.DashboardService
;
import
org.thingsboard.server.dao.device.DeviceCredentialsService
;
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.edge.EdgeService
;
import
org.thingsboard.server.dao.entityview.EntityViewService
;
import
org.thingsboard.server.dao.ota.OtaPackageService
;
import
org.thingsboard.server.dao.queue.QueueService
;
import
org.thingsboard.server.dao.relation.RelationService
;
import
org.thingsboard.server.dao.rule.RuleChainService
;
import
org.thingsboard.server.dao.service.DataValidator
;
import
org.thingsboard.server.dao.tenant.TenantService
;
import
org.thingsboard.server.dao.user.UserService
;
import
org.thingsboard.server.dao.widget.WidgetTypeService
;
import
org.thingsboard.server.dao.widget.WidgetsBundleService
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.discovery.PartitionService
;
import
org.thingsboard.server.queue.provider.TbQueueProducerProvider
;
import
org.thingsboard.server.service.edge.rpc.constructor.AdminSettingsMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.AlarmMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.AssetMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.AssetProfileMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.CustomerMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.DashboardMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.DeviceMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.DeviceProfileMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.EdgeMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.EntityDataMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.EntityViewMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.OtaPackageMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.QueueMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.RelationMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.RuleChainMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.UserMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.WidgetTypeMsgConstructor
;
import
org.thingsboard.server.service.edge.rpc.constructor.WidgetsBundleMsgConstructor
;
import
org.thingsboard.server.service.entitiy.TbNotificationEntityService
;
import
org.thingsboard.server.service.executors.DbCallbackExecutorService
;
import
org.thingsboard.server.service.profile.TbAssetProfileCache
;
import
org.thingsboard.server.service.profile.TbDeviceProfileCache
;
import
org.thingsboard.server.service.state.DeviceStateService
;
import
org.thingsboard.server.service.telemetry.TelemetrySubscriptionService
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
@Slf4j
public
abstract
class
BaseEdgeProcessor
{
protected
static
final
int
DEFAULT_PAGE_SIZE
=
100
;
@Autowired
protected
TelemetrySubscriptionService
tsSubService
;
@Autowired
protected
TbNotificationEntityService
notificationEntityService
;
@Autowired
protected
RuleChainService
ruleChainService
;
@Autowired
protected
AlarmService
alarmService
;
@Autowired
protected
DeviceService
deviceService
;
@Autowired
protected
TbDeviceProfileCache
deviceProfileCache
;
@Autowired
protected
TbAssetProfileCache
assetProfileCache
;
@Autowired
protected
DashboardService
dashboardService
;
@Autowired
protected
AssetService
assetService
;
@Autowired
protected
EntityViewService
entityViewService
;
@Autowired
protected
TenantService
tenantService
;
@Autowired
protected
EdgeService
edgeService
;
@Autowired
protected
CustomerService
customerService
;
@Autowired
protected
UserService
userService
;
@Autowired
protected
DeviceProfileService
deviceProfileService
;
@Autowired
protected
AssetProfileService
assetProfileService
;
@Autowired
protected
RelationService
relationService
;
@Autowired
protected
DeviceCredentialsService
deviceCredentialsService
;
@Autowired
protected
AttributesService
attributesService
;
@Autowired
protected
TbClusterService
tbClusterService
;
@Autowired
protected
DeviceStateService
deviceStateService
;
@Autowired
protected
EdgeEventService
edgeEventService
;
@Autowired
protected
WidgetsBundleService
widgetsBundleService
;
@Autowired
protected
WidgetTypeService
widgetTypeService
;
@Autowired
protected
OtaPackageService
otaPackageService
;
@Autowired
protected
QueueService
queueService
;
@Autowired
protected
PartitionService
partitionService
;
@Autowired
@Lazy
protected
TbQueueProducerProvider
producerProvider
;
@Autowired
protected
DataValidator
<
Device
>
deviceValidator
;
@Autowired
protected
EdgeMsgConstructor
edgeMsgConstructor
;
@Autowired
protected
EntityDataMsgConstructor
entityDataMsgConstructor
;
@Autowired
protected
RuleChainMsgConstructor
ruleChainMsgConstructor
;
@Autowired
protected
AlarmMsgConstructor
alarmMsgConstructor
;
@Autowired
protected
DeviceMsgConstructor
deviceMsgConstructor
;
@Autowired
protected
AssetMsgConstructor
assetMsgConstructor
;
@Autowired
protected
EntityViewMsgConstructor
entityViewMsgConstructor
;
@Autowired
protected
DashboardMsgConstructor
dashboardMsgConstructor
;
@Autowired
protected
RelationMsgConstructor
relationMsgConstructor
;
@Autowired
protected
UserMsgConstructor
userMsgConstructor
;
@Autowired
protected
CustomerMsgConstructor
customerMsgConstructor
;
@Autowired
protected
DeviceProfileMsgConstructor
deviceProfileMsgConstructor
;
@Autowired
protected
AssetProfileMsgConstructor
assetProfileMsgConstructor
;
@Autowired
protected
WidgetsBundleMsgConstructor
widgetsBundleMsgConstructor
;
@Autowired
protected
WidgetTypeMsgConstructor
widgetTypeMsgConstructor
;
@Autowired
protected
AdminSettingsMsgConstructor
adminSettingsMsgConstructor
;
@Autowired
protected
OtaPackageMsgConstructor
otaPackageMsgConstructor
;
@Autowired
protected
QueueMsgConstructor
queueMsgConstructor
;
@Autowired
protected
DbCallbackExecutorService
dbCallbackExecutorService
;
protected
ListenableFuture
<
Void
>
saveEdgeEvent
(
TenantId
tenantId
,
EdgeId
edgeId
,
EdgeEventType
type
,
EdgeEventActionType
action
,
EntityId
entityId
,
JsonNode
body
)
{
log
.
debug
(
"Pushing 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
);
}
protected
ListenableFuture
<
Void
>
processActionForAllEdges
(
TenantId
tenantId
,
EdgeEventType
type
,
EdgeEventActionType
actionType
,
EntityId
entityId
)
{
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
if
(
TenantId
.
SYS_TENANT_ID
.
equals
(
tenantId
))
{
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
TenantId
>
tenantsIds
;
do
{
tenantsIds
=
tenantService
.
findTenantsIds
(
pageLink
);
for
(
TenantId
tenantId1
:
tenantsIds
.
getData
())
{
futures
.
addAll
(
processActionForAllEdgesByTenantId
(
tenantId1
,
type
,
actionType
,
entityId
,
null
));
}
pageLink
=
pageLink
.
nextPageLink
();
}
while
(
tenantsIds
.
hasNext
());
}
else
{
futures
=
processActionForAllEdgesByTenantId
(
tenantId
,
type
,
actionType
,
entityId
,
null
);
}
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
}
protected
List
<
ListenableFuture
<
Void
>>
processActionForAllEdgesByTenantId
(
TenantId
tenantId
,
EdgeEventType
type
,
EdgeEventActionType
actionType
,
EntityId
entityId
,
JsonNode
body
)
{
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
Edge
>
pageData
;
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
do
{
pageData
=
edgeService
.
findEdgesByTenantId
(
tenantId
,
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
for
(
Edge
edge
:
pageData
.
getData
())
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
type
,
actionType
,
entityId
,
body
));
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
futures
;
}
protected
UpdateMsgType
getUpdateMsgType
(
EdgeEventActionType
actionType
)
{
switch
(
actionType
)
{
case
UPDATED:
case
CREDENTIALS_UPDATED:
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
return
UpdateMsgType
.
ENTITY_UPDATED_RPC_MESSAGE
;
case
ADDED:
case
ASSIGNED_TO_EDGE:
case
RELATION_ADD_OR_UPDATE:
return
UpdateMsgType
.
ENTITY_CREATED_RPC_MESSAGE
;
case
DELETED:
case
UNASSIGNED_FROM_EDGE:
case
RELATION_DELETED:
return
UpdateMsgType
.
ENTITY_DELETED_RPC_MESSAGE
;
case
ALARM_ACK:
return
UpdateMsgType
.
ALARM_ACK_RPC_MESSAGE
;
case
ALARM_CLEAR:
return
UpdateMsgType
.
ALARM_CLEAR_RPC_MESSAGE
;
default
:
throw
new
RuntimeException
(
"Unsupported actionType ["
+
actionType
+
"]"
);
}
}
protected
ListenableFuture
<
Void
>
processEntityNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
EdgeEventActionType
actionType
=
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
());
EdgeEventType
type
=
EdgeEventType
.
valueOf
(
edgeNotificationMsg
.
getType
());
EntityId
entityId
=
EntityIdFactory
.
getByEdgeEventTypeAndUuid
(
type
,
new
UUID
(
edgeNotificationMsg
.
getEntityIdMSB
(),
edgeNotificationMsg
.
getEntityIdLSB
()));
EdgeId
edgeId
=
safeGetEdgeId
(
edgeNotificationMsg
);
switch
(
actionType
)
{
case
ADDED:
case
UPDATED:
case
CREDENTIALS_UPDATED:
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
case
DELETED:
if
(
edgeId
!=
null
)
{
return
saveEdgeEvent
(
tenantId
,
edgeId
,
type
,
actionType
,
entityId
,
null
);
}
else
{
return
pushNotificationToAllRelatedEdges
(
tenantId
,
entityId
,
type
,
actionType
);
}
case
ASSIGNED_TO_EDGE:
case
UNASSIGNED_FROM_EDGE:
ListenableFuture
<
Void
>
future
=
saveEdgeEvent
(
tenantId
,
edgeId
,
type
,
actionType
,
entityId
,
null
);
return
Futures
.
transformAsync
(
future
,
unused
->
{
if
(
type
.
equals
(
EdgeEventType
.
RULE_CHAIN
))
{
return
updateDependentRuleChains
(
tenantId
,
new
RuleChainId
(
entityId
.
getId
()),
edgeId
);
}
else
{
return
Futures
.
immediateFuture
(
null
);
}
},
dbCallbackExecutorService
);
default
:
return
Futures
.
immediateFuture
(
null
);
}
}
private
EdgeId
safeGetEdgeId
(
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
if
(
edgeNotificationMsg
.
getEdgeIdMSB
()
!=
0
&&
edgeNotificationMsg
.
getEdgeIdLSB
()
!=
0
)
{
return
new
EdgeId
(
new
UUID
(
edgeNotificationMsg
.
getEdgeIdMSB
(),
edgeNotificationMsg
.
getEdgeIdLSB
()));
}
else
{
return
null
;
}
}
private
ListenableFuture
<
Void
>
pushNotificationToAllRelatedEdges
(
TenantId
tenantId
,
EntityId
entityId
,
EdgeEventType
type
,
EdgeEventActionType
actionType
)
{
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
EdgeId
>
pageData
;
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
do
{
pageData
=
edgeService
.
findRelatedEdgeIdsByEntityId
(
tenantId
,
entityId
,
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
for
(
EdgeId
relatedEdgeId
:
pageData
.
getData
())
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
relatedEdgeId
,
type
,
actionType
,
entityId
,
null
));
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
}
private
ListenableFuture
<
Void
>
updateDependentRuleChains
(
TenantId
tenantId
,
RuleChainId
processingRuleChainId
,
EdgeId
edgeId
)
{
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
RuleChain
>
pageData
;
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
do
{
pageData
=
ruleChainService
.
findRuleChainsByTenantIdAndEdgeId
(
tenantId
,
edgeId
,
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
for
(
RuleChain
ruleChain
:
pageData
.
getData
())
{
if
(!
ruleChain
.
getId
().
equals
(
processingRuleChainId
))
{
List
<
RuleChainConnectionInfo
>
connectionInfos
=
ruleChainService
.
loadRuleChainMetaData
(
ruleChain
.
getTenantId
(),
ruleChain
.
getId
()).
getRuleChainConnections
();
if
(
connectionInfos
!=
null
&&
!
connectionInfos
.
isEmpty
())
{
for
(
RuleChainConnectionInfo
connectionInfo
:
connectionInfos
)
{
if
(
connectionInfo
.
getTargetRuleChainId
().
equals
(
processingRuleChainId
))
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
edgeId
,
EdgeEventType
.
RULE_CHAIN_METADATA
,
EdgeEventActionType
.
UPDATED
,
ruleChain
.
getId
(),
null
));
}
}
}
}
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
}
protected
ListenableFuture
<
Void
>
processEntityNotificationForAllEdges
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
EdgeEventActionType
actionType
=
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
());
EdgeEventType
type
=
EdgeEventType
.
valueOf
(
edgeNotificationMsg
.
getType
());
EntityId
entityId
=
EntityIdFactory
.
getByEdgeEventTypeAndUuid
(
type
,
new
UUID
(
edgeNotificationMsg
.
getEntityIdMSB
(),
edgeNotificationMsg
.
getEntityIdLSB
()));
switch
(
actionType
)
{
case
ADDED:
case
UPDATED:
case
DELETED:
case
CREDENTIALS_UPDATED:
// used by USER entity
return
processActionForAllEdges
(
tenantId
,
type
,
actionType
,
entityId
);
default
:
return
Futures
.
immediateFuture
(
null
);
}
}
protected
EntityId
constructEntityId
(
String
entityTypeStr
,
long
entityIdMSB
,
long
entityIdLSB
)
{
EntityType
entityType
=
EntityType
.
valueOf
(
entityTypeStr
);
switch
(
entityType
)
{
case
DEVICE:
return
new
DeviceId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
ASSET:
return
new
AssetId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
ENTITY_VIEW:
return
new
EntityViewId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
DASHBOARD:
return
new
DashboardId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
TENANT:
return
TenantId
.
fromUUID
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
CUSTOMER:
return
new
CustomerId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
USER:
return
new
UserId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
case
EDGE:
return
new
EdgeId
(
new
UUID
(
entityIdMSB
,
entityIdLSB
));
default
:
log
.
warn
(
"Unsupported entity type [{}] during construct of entity id. entityIdMSB [{}], entityIdLSB [{}]"
,
entityTypeStr
,
entityIdMSB
,
entityIdLSB
);
return
null
;
}
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/CustomerEdgeProcessor.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.Futures
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.server.common.data.Customer
;
import
org.thingsboard.server.common.data.EdgeUtils
;
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.CustomerId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
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.PageLink
;
import
org.thingsboard.server.gen.edge.v1.CustomerUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
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.List
;
import
java.util.UUID
;
@Component
@Slf4j
@TbCoreComponent
public
class
CustomerEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertCustomerEventToDownlink
(
EdgeEvent
edgeEvent
)
{
CustomerId
customerId
=
new
CustomerId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
Customer
customer
=
customerService
.
findCustomerById
(
edgeEvent
.
getTenantId
(),
customerId
);
if
(
customer
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
CustomerUpdateMsg
customerUpdateMsg
=
customerMsgConstructor
.
constructCustomerUpdatedMsg
(
msgType
,
customer
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addCustomerUpdateMsg
(
customerUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
CustomerUpdateMsg
customerUpdateMsg
=
customerMsgConstructor
.
constructCustomerDeleteMsg
(
customerId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addCustomerUpdateMsg
(
customerUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processCustomerNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
EdgeEventActionType
actionType
=
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
());
EdgeEventType
type
=
EdgeEventType
.
valueOf
(
edgeNotificationMsg
.
getType
());
UUID
uuid
=
new
UUID
(
edgeNotificationMsg
.
getEntityIdMSB
(),
edgeNotificationMsg
.
getEntityIdLSB
());
CustomerId
customerId
=
new
CustomerId
(
EntityIdFactory
.
getByEdgeEventTypeAndUuid
(
type
,
uuid
).
getId
());
switch
(
actionType
)
{
case
UPDATED:
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
Edge
>
pageData
;
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
do
{
pageData
=
edgeService
.
findEdgesByTenantIdAndCustomerId
(
tenantId
,
customerId
,
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
for
(
Edge
edge
:
pageData
.
getData
())
{
futures
.
add
(
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
type
,
actionType
,
customerId
,
null
));
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
case
DELETED:
EdgeId
edgeId
=
new
EdgeId
(
new
UUID
(
edgeNotificationMsg
.
getEdgeIdMSB
(),
edgeNotificationMsg
.
getEdgeIdLSB
()));
return
saveEdgeEvent
(
tenantId
,
edgeId
,
type
,
actionType
,
customerId
,
null
);
default
:
return
Futures
.
immediateFuture
(
null
);
}
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/DashboardEdgeProcessor.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.Dashboard
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.DashboardId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.DashboardUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
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
DashboardEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertDashboardEventToDownlink
(
EdgeEvent
edgeEvent
)
{
DashboardId
dashboardId
=
new
DashboardId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
case
ASSIGNED_TO_EDGE:
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
Dashboard
dashboard
=
dashboardService
.
findDashboardById
(
edgeEvent
.
getTenantId
(),
dashboardId
);
if
(
dashboard
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
DashboardUpdateMsg
dashboardUpdateMsg
=
dashboardMsgConstructor
.
constructDashboardUpdatedMsg
(
msgType
,
dashboard
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDashboardUpdateMsg
(
dashboardUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
case
UNASSIGNED_FROM_EDGE:
DashboardUpdateMsg
dashboardUpdateMsg
=
dashboardMsgConstructor
.
constructDashboardDeleteMsg
(
dashboardId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDashboardUpdateMsg
(
dashboardUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processDashboardNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotification
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/DeviceEdgeProcessor.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.datastax.oss.driver.api.core.uuid.Uuids
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
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.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.thingsboard.common.util.JacksonUtil
;
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.StringUtils
;
import
org.thingsboard.server.common.data.device.data.DeviceData
;
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.CustomerId
;
import
org.thingsboard.server.common.data.id.DeviceId
;
import
org.thingsboard.server.common.data.id.DeviceProfileId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.EntityId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.common.data.relation.EntityRelation
;
import
org.thingsboard.server.common.data.relation.RelationTypeGroup
;
import
org.thingsboard.server.common.data.rpc.RpcError
;
import
org.thingsboard.server.common.data.security.DeviceCredentials
;
import
org.thingsboard.server.common.data.security.DeviceCredentialsType
;
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.common.msg.rpc.FromDeviceRpcResponse
;
import
org.thingsboard.server.common.msg.session.SessionMsgType
;
import
org.thingsboard.server.dao.exception.DataValidationException
;
import
org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg
;
import
org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DeviceRpcCallMsg
;
import
org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.UpdateMsgType
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.TbQueueCallback
;
import
org.thingsboard.server.queue.TbQueueMsgMetadata
;
import
org.thingsboard.server.queue.util.DataDecodingEncodingService
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
import
org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg
;
import
java.util.Optional
;
import
java.util.UUID
;
import
java.util.concurrent.locks.ReentrantLock
;
@Component
@Slf4j
@TbCoreComponent
public
class
DeviceEdgeProcessor
extends
BaseEdgeProcessor
{
@Autowired
private
DataDecodingEncodingService
dataDecodingEncodingService
;
private
static
final
ReentrantLock
deviceCreationLock
=
new
ReentrantLock
();
public
ListenableFuture
<
Void
>
processDeviceFromEdge
(
TenantId
tenantId
,
Edge
edge
,
DeviceUpdateMsg
deviceUpdateMsg
)
{
log
.
trace
(
"[{}] onDeviceUpdate [{}] from edge [{}]"
,
tenantId
,
deviceUpdateMsg
,
edge
.
getName
());
switch
(
deviceUpdateMsg
.
getMsgType
())
{
case
ENTITY_CREATED_RPC_MESSAGE:
String
deviceName
=
deviceUpdateMsg
.
getName
();
Device
device
=
deviceService
.
findDeviceByTenantIdAndName
(
tenantId
,
deviceName
);
if
(
device
!=
null
)
{
boolean
deviceAlreadyExistsForThisEdge
=
isDeviceAlreadyExistsOnCloudForThisEdge
(
tenantId
,
edge
,
device
);
if
(
deviceAlreadyExistsForThisEdge
)
{
log
.
info
(
"[{}] Device with name '{}' already exists on the cloud, and related to this edge [{}]. "
+
"deviceUpdateMsg [{}], Updating device"
,
tenantId
,
deviceName
,
edge
.
getId
(),
deviceUpdateMsg
);
return
updateDevice
(
tenantId
,
edge
,
deviceUpdateMsg
);
}
else
{
log
.
info
(
"[{}] Device with name '{}' already exists on the cloud, but not related to this edge [{}]. deviceUpdateMsg [{}]."
+
"Creating a new device with random prefix and relate to this edge"
,
tenantId
,
deviceName
,
edge
.
getId
(),
deviceUpdateMsg
);
String
newDeviceName
=
deviceUpdateMsg
.
getName
()
+
"_"
+
StringUtils
.
randomAlphabetic
(
15
);
Device
newDevice
;
try
{
newDevice
=
createDevice
(
tenantId
,
edge
,
deviceUpdateMsg
,
newDeviceName
);
}
catch
(
DataValidationException
e
)
{
log
.
error
(
"[{}] Device update msg can't be processed due to data validation [{}]"
,
tenantId
,
deviceUpdateMsg
,
e
);
return
Futures
.
immediateFuture
(
null
);
}
ObjectNode
body
=
JacksonUtil
.
OBJECT_MAPPER
.
createObjectNode
();
body
.
put
(
"conflictName"
,
deviceName
);
ListenableFuture
<
Void
>
input
=
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
DEVICE
,
EdgeEventActionType
.
ENTITY_MERGE_REQUEST
,
newDevice
.
getId
(),
body
);
return
Futures
.
transformAsync
(
input
,
unused
->
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
DEVICE
,
EdgeEventActionType
.
CREDENTIALS_REQUEST
,
newDevice
.
getId
(),
null
),
dbCallbackExecutorService
);
}
}
else
{
log
.
info
(
"[{}] Creating new device on the cloud [{}]"
,
tenantId
,
deviceUpdateMsg
);
try
{
device
=
createDevice
(
tenantId
,
edge
,
deviceUpdateMsg
,
deviceUpdateMsg
.
getName
());
}
catch
(
DataValidationException
e
)
{
log
.
error
(
"[{}] Device update msg can't be processed due to data validation [{}]"
,
tenantId
,
deviceUpdateMsg
,
e
);
return
Futures
.
immediateFuture
(
null
);
}
return
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
DEVICE
,
EdgeEventActionType
.
CREDENTIALS_REQUEST
,
device
.
getId
(),
null
);
}
case
ENTITY_UPDATED_RPC_MESSAGE:
return
updateDevice
(
tenantId
,
edge
,
deviceUpdateMsg
);
case
ENTITY_DELETED_RPC_MESSAGE:
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceUpdateMsg
.
getIdMSB
(),
deviceUpdateMsg
.
getIdLSB
()));
Device
deviceToDelete
=
deviceService
.
findDeviceById
(
tenantId
,
deviceId
);
if
(
deviceToDelete
!=
null
)
{
deviceService
.
unassignDeviceFromEdge
(
tenantId
,
deviceId
,
edge
.
getId
());
}
return
Futures
.
immediateFuture
(
null
);
case
UNRECOGNIZED:
default
:
log
.
error
(
"Unsupported msg type {}"
,
deviceUpdateMsg
.
getMsgType
());
return
Futures
.
immediateFailedFuture
(
new
RuntimeException
(
"Unsupported msg type "
+
deviceUpdateMsg
.
getMsgType
()));
}
}
private
boolean
isDeviceAlreadyExistsOnCloudForThisEdge
(
TenantId
tenantId
,
Edge
edge
,
Device
device
)
{
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
EdgeId
>
pageData
;
do
{
pageData
=
edgeService
.
findRelatedEdgeIdsByEntityId
(
tenantId
,
device
.
getId
(),
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
if
(
pageData
.
getData
().
contains
(
edge
.
getId
()))
{
return
true
;
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
false
;
}
public
ListenableFuture
<
Void
>
processDeviceCredentialsFromEdge
(
TenantId
tenantId
,
DeviceCredentialsUpdateMsg
deviceCredentialsUpdateMsg
)
{
log
.
debug
(
"Executing onDeviceCredentialsUpdate, deviceCredentialsUpdateMsg [{}]"
,
deviceCredentialsUpdateMsg
);
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceCredentialsUpdateMsg
.
getDeviceIdMSB
(),
deviceCredentialsUpdateMsg
.
getDeviceIdLSB
()));
ListenableFuture
<
Device
>
deviceFuture
=
deviceService
.
findDeviceByIdAsync
(
tenantId
,
deviceId
);
return
Futures
.
transform
(
deviceFuture
,
device
->
{
if
(
device
!=
null
)
{
log
.
debug
(
"Updating device credentials for device [{}]. New device credentials Id [{}], value [{}]"
,
device
.
getName
(),
deviceCredentialsUpdateMsg
.
getCredentialsId
(),
deviceCredentialsUpdateMsg
.
getCredentialsValue
());
try
{
DeviceCredentials
deviceCredentials
=
deviceCredentialsService
.
findDeviceCredentialsByDeviceId
(
tenantId
,
device
.
getId
());
deviceCredentials
.
setCredentialsType
(
DeviceCredentialsType
.
valueOf
(
deviceCredentialsUpdateMsg
.
getCredentialsType
()));
deviceCredentials
.
setCredentialsId
(
deviceCredentialsUpdateMsg
.
getCredentialsId
());
if
(
deviceCredentialsUpdateMsg
.
hasCredentialsValue
())
{
deviceCredentials
.
setCredentialsValue
(
deviceCredentialsUpdateMsg
.
getCredentialsValue
());
}
deviceCredentialsService
.
updateDeviceCredentials
(
tenantId
,
deviceCredentials
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Can't update device credentials for device [{}], deviceCredentialsUpdateMsg [{}]"
,
device
.
getName
(),
deviceCredentialsUpdateMsg
,
e
);
throw
new
RuntimeException
(
e
);
}
}
return
null
;
},
dbCallbackExecutorService
);
}
private
ListenableFuture
<
Void
>
updateDevice
(
TenantId
tenantId
,
Edge
edge
,
DeviceUpdateMsg
deviceUpdateMsg
)
{
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceUpdateMsg
.
getIdMSB
(),
deviceUpdateMsg
.
getIdLSB
()));
Device
device
=
deviceService
.
findDeviceById
(
tenantId
,
deviceId
);
if
(
device
!=
null
)
{
device
.
setName
(
deviceUpdateMsg
.
getName
());
device
.
setType
(
deviceUpdateMsg
.
getType
());
if
(
deviceUpdateMsg
.
hasLabel
())
{
device
.
setLabel
(
deviceUpdateMsg
.
getLabel
());
}
if
(
deviceUpdateMsg
.
hasAdditionalInfo
())
{
device
.
setAdditionalInfo
(
JacksonUtil
.
toJsonNode
(
deviceUpdateMsg
.
getAdditionalInfo
()));
}
if
(
deviceUpdateMsg
.
hasDeviceProfileIdMSB
()
&&
deviceUpdateMsg
.
hasDeviceProfileIdLSB
())
{
DeviceProfileId
deviceProfileId
=
new
DeviceProfileId
(
new
UUID
(
deviceUpdateMsg
.
getDeviceProfileIdMSB
(),
deviceUpdateMsg
.
getDeviceProfileIdLSB
()));
device
.
setDeviceProfileId
(
deviceProfileId
);
}
device
.
setCustomerId
(
getCustomerId
(
deviceUpdateMsg
));
Optional
<
DeviceData
>
deviceDataOpt
=
dataDecodingEncodingService
.
decode
(
deviceUpdateMsg
.
getDeviceDataBytes
().
toByteArray
());
if
(
deviceDataOpt
.
isPresent
())
{
device
.
setDeviceData
(
deviceDataOpt
.
get
());
}
Device
savedDevice
=
deviceService
.
saveDevice
(
device
);
tbClusterService
.
onDeviceUpdated
(
savedDevice
,
device
,
false
);
return
saveEdgeEvent
(
tenantId
,
edge
.
getId
(),
EdgeEventType
.
DEVICE
,
EdgeEventActionType
.
CREDENTIALS_REQUEST
,
deviceId
,
null
);
}
else
{
String
errMsg
=
String
.
format
(
"[%s] can't find device [%s], edge [%s]"
,
tenantId
,
deviceUpdateMsg
,
edge
.
getId
());
log
.
warn
(
errMsg
);
return
Futures
.
immediateFailedFuture
(
new
RuntimeException
(
errMsg
));
}
}
private
Device
createDevice
(
TenantId
tenantId
,
Edge
edge
,
DeviceUpdateMsg
deviceUpdateMsg
,
String
deviceName
)
{
Device
device
;
deviceCreationLock
.
lock
();
try
{
log
.
debug
(
"[{}] Creating device entity [{}] from edge [{}]"
,
tenantId
,
deviceUpdateMsg
,
edge
.
getName
());
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceUpdateMsg
.
getIdMSB
(),
deviceUpdateMsg
.
getIdLSB
()));
device
=
deviceService
.
findDeviceById
(
tenantId
,
deviceId
);
boolean
created
=
false
;
if
(
device
==
null
)
{
device
=
new
Device
();
device
.
setTenantId
(
tenantId
);
device
.
setCreatedTime
(
Uuids
.
unixTimestamp
(
deviceId
.
getId
()));
created
=
true
;
}
device
.
setName
(
deviceName
);
device
.
setType
(
deviceUpdateMsg
.
getType
());
if
(
deviceUpdateMsg
.
hasLabel
())
{
device
.
setLabel
(
deviceUpdateMsg
.
getLabel
());
}
if
(
deviceUpdateMsg
.
hasAdditionalInfo
())
{
device
.
setAdditionalInfo
(
JacksonUtil
.
toJsonNode
(
deviceUpdateMsg
.
getAdditionalInfo
()));
}
if
(
deviceUpdateMsg
.
hasDeviceProfileIdMSB
()
&&
deviceUpdateMsg
.
hasDeviceProfileIdLSB
())
{
DeviceProfileId
deviceProfileId
=
new
DeviceProfileId
(
new
UUID
(
deviceUpdateMsg
.
getDeviceProfileIdMSB
(),
deviceUpdateMsg
.
getDeviceProfileIdLSB
()));
device
.
setDeviceProfileId
(
deviceProfileId
);
}
device
.
setCustomerId
(
getCustomerId
(
deviceUpdateMsg
));
Optional
<
DeviceData
>
deviceDataOpt
=
dataDecodingEncodingService
.
decode
(
deviceUpdateMsg
.
getDeviceDataBytes
().
toByteArray
());
if
(
deviceDataOpt
.
isPresent
())
{
device
.
setDeviceData
(
deviceDataOpt
.
get
());
}
if
(
created
)
{
deviceValidator
.
validate
(
device
,
Device:
:
getTenantId
);
device
.
setId
(
deviceId
);
}
else
{
deviceValidator
.
validate
(
device
,
Device:
:
getTenantId
);
}
Device
savedDevice
=
deviceService
.
saveDevice
(
device
,
false
);
tbClusterService
.
onDeviceUpdated
(
savedDevice
,
created
?
null
:
device
,
false
);
if
(
created
)
{
DeviceCredentials
deviceCredentials
=
new
DeviceCredentials
();
deviceCredentials
.
setDeviceId
(
new
DeviceId
(
savedDevice
.
getUuidId
()));
deviceCredentials
.
setCredentialsType
(
DeviceCredentialsType
.
ACCESS_TOKEN
);
deviceCredentials
.
setCredentialsId
(
StringUtils
.
randomAlphanumeric
(
20
));
deviceCredentialsService
.
createDeviceCredentials
(
device
.
getTenantId
(),
deviceCredentials
);
}
createRelationFromEdge
(
tenantId
,
edge
.
getId
(),
device
.
getId
());
pushDeviceCreatedEventToRuleEngine
(
tenantId
,
edge
,
device
);
deviceService
.
assignDeviceToEdge
(
edge
.
getTenantId
(),
device
.
getId
(),
edge
.
getId
());
}
finally
{
deviceCreationLock
.
unlock
();
}
return
device
;
}
private
CustomerId
getCustomerId
(
DeviceUpdateMsg
deviceUpdateMsg
)
{
if
(
deviceUpdateMsg
.
hasCustomerIdMSB
()
&&
deviceUpdateMsg
.
hasCustomerIdLSB
())
{
return
new
CustomerId
(
new
UUID
(
deviceUpdateMsg
.
getCustomerIdMSB
(),
deviceUpdateMsg
.
getCustomerIdLSB
()));
}
else
{
return
null
;
}
}
private
void
createRelationFromEdge
(
TenantId
tenantId
,
EdgeId
edgeId
,
EntityId
entityId
)
{
EntityRelation
relation
=
new
EntityRelation
();
relation
.
setFrom
(
edgeId
);
relation
.
setTo
(
entityId
);
relation
.
setTypeGroup
(
RelationTypeGroup
.
COMMON
);
relation
.
setType
(
EntityRelation
.
EDGE_TYPE
);
relationService
.
saveRelation
(
tenantId
,
relation
);
}
private
void
pushDeviceCreatedEventToRuleEngine
(
TenantId
tenantId
,
Edge
edge
,
Device
device
)
{
try
{
DeviceId
deviceId
=
device
.
getId
();
ObjectNode
entityNode
=
JacksonUtil
.
OBJECT_MAPPER
.
valueToTree
(
device
);
TbMsg
tbMsg
=
TbMsg
.
newMsg
(
DataConstants
.
ENTITY_CREATED
,
deviceId
,
device
.
getCustomerId
(),
getActionTbMsgMetaData
(
edge
,
device
.
getCustomerId
()),
TbMsgDataType
.
JSON
,
JacksonUtil
.
OBJECT_MAPPER
.
writeValueAsString
(
entityNode
));
tbClusterService
.
pushMsgToRuleEngine
(
tenantId
,
deviceId
,
tbMsg
,
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
log
.
debug
(
"Successfully send ENTITY_CREATED EVENT to rule engine [{}]"
,
device
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
debug
(
"Failed to send ENTITY_CREATED EVENT to rule engine [{}]"
,
device
,
t
);
}
});
}
catch
(
JsonProcessingException
|
IllegalArgumentException
e
)
{
log
.
warn
(
"[{}] Failed to push device action to rule engine: {}"
,
device
.
getId
(),
DataConstants
.
ENTITY_CREATED
,
e
);
}
}
private
TbMsgMetaData
getActionTbMsgMetaData
(
Edge
edge
,
CustomerId
customerId
)
{
TbMsgMetaData
metaData
=
getTbMsgMetaData
(
edge
);
if
(
customerId
!=
null
&&
!
customerId
.
isNullUid
())
{
metaData
.
putValue
(
"customerId"
,
customerId
.
toString
());
}
return
metaData
;
}
private
TbMsgMetaData
getTbMsgMetaData
(
Edge
edge
)
{
TbMsgMetaData
metaData
=
new
TbMsgMetaData
();
metaData
.
putValue
(
"edgeId"
,
edge
.
getId
().
toString
());
metaData
.
putValue
(
"edgeName"
,
edge
.
getName
());
return
metaData
;
}
public
ListenableFuture
<
Void
>
processDeviceRpcCallFromEdge
(
TenantId
tenantId
,
Edge
edge
,
DeviceRpcCallMsg
deviceRpcCallMsg
)
{
log
.
trace
(
"[{}] processDeviceRpcCallFromEdge [{}]"
,
tenantId
,
deviceRpcCallMsg
);
if
(
deviceRpcCallMsg
.
hasResponseMsg
())
{
return
processDeviceRpcResponseFromEdge
(
tenantId
,
deviceRpcCallMsg
);
}
else
if
(
deviceRpcCallMsg
.
hasRequestMsg
())
{
return
processDeviceRpcRequestFromEdge
(
tenantId
,
edge
,
deviceRpcCallMsg
);
}
return
Futures
.
immediateFuture
(
null
);
}
private
ListenableFuture
<
Void
>
processDeviceRpcResponseFromEdge
(
TenantId
tenantId
,
DeviceRpcCallMsg
deviceRpcCallMsg
)
{
SettableFuture
<
Void
>
futureToSet
=
SettableFuture
.
create
();
UUID
requestUuid
=
new
UUID
(
deviceRpcCallMsg
.
getRequestUuidMSB
(),
deviceRpcCallMsg
.
getRequestUuidLSB
());
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceRpcCallMsg
.
getDeviceIdMSB
(),
deviceRpcCallMsg
.
getDeviceIdLSB
()));
FromDeviceRpcResponse
response
;
if
(!
StringUtils
.
isEmpty
(
deviceRpcCallMsg
.
getResponseMsg
().
getError
()))
{
response
=
new
FromDeviceRpcResponse
(
requestUuid
,
null
,
RpcError
.
valueOf
(
deviceRpcCallMsg
.
getResponseMsg
().
getError
()));
}
else
{
response
=
new
FromDeviceRpcResponse
(
requestUuid
,
deviceRpcCallMsg
.
getResponseMsg
().
getResponse
(),
null
);
}
TbQueueCallback
callback
=
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
futureToSet
.
set
(
null
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
error
(
"Can't process push notification to core [{}]"
,
deviceRpcCallMsg
,
t
);
futureToSet
.
setException
(
t
);
}
};
FromDeviceRpcResponseActorMsg
msg
=
new
FromDeviceRpcResponseActorMsg
(
deviceRpcCallMsg
.
getRequestId
(),
tenantId
,
deviceId
,
response
);
tbClusterService
.
pushMsgToCore
(
msg
,
callback
);
return
futureToSet
;
}
private
ListenableFuture
<
Void
>
processDeviceRpcRequestFromEdge
(
TenantId
tenantId
,
Edge
edge
,
DeviceRpcCallMsg
deviceRpcCallMsg
)
{
DeviceId
deviceId
=
new
DeviceId
(
new
UUID
(
deviceRpcCallMsg
.
getDeviceIdMSB
(),
deviceRpcCallMsg
.
getDeviceIdLSB
()));
try
{
TbMsgMetaData
metaData
=
new
TbMsgMetaData
();
String
requestId
=
Integer
.
toString
(
deviceRpcCallMsg
.
getRequestId
());
metaData
.
putValue
(
"requestId"
,
requestId
);
metaData
.
putValue
(
"serviceId"
,
deviceRpcCallMsg
.
getServiceId
());
metaData
.
putValue
(
"sessionId"
,
deviceRpcCallMsg
.
getSessionId
());
metaData
.
putValue
(
DataConstants
.
EDGE_ID
,
edge
.
getId
().
toString
());
Device
device
=
deviceService
.
findDeviceById
(
tenantId
,
deviceId
);
if
(
device
!=
null
)
{
metaData
.
putValue
(
"deviceName"
,
device
.
getName
());
metaData
.
putValue
(
"deviceType"
,
device
.
getType
());
metaData
.
putValue
(
DataConstants
.
DEVICE_ID
,
deviceId
.
getId
().
toString
());
}
ObjectNode
data
=
JacksonUtil
.
OBJECT_MAPPER
.
createObjectNode
();
data
.
put
(
"method"
,
deviceRpcCallMsg
.
getRequestMsg
().
getMethod
());
data
.
put
(
"params"
,
deviceRpcCallMsg
.
getRequestMsg
().
getParams
());
TbMsg
tbMsg
=
TbMsg
.
newMsg
(
SessionMsgType
.
TO_SERVER_RPC_REQUEST
.
name
(),
deviceId
,
null
,
metaData
,
TbMsgDataType
.
JSON
,
JacksonUtil
.
OBJECT_MAPPER
.
writeValueAsString
(
data
));
tbClusterService
.
pushMsgToRuleEngine
(
tenantId
,
deviceId
,
tbMsg
,
new
TbQueueCallback
()
{
@Override
public
void
onSuccess
(
TbQueueMsgMetadata
metadata
)
{
log
.
debug
(
"Successfully send TO_SERVER_RPC_REQUEST to rule engine [{}], deviceRpcCallMsg {}"
,
device
,
deviceRpcCallMsg
);
}
@Override
public
void
onFailure
(
Throwable
t
)
{
log
.
debug
(
"Failed to send TO_SERVER_RPC_REQUEST to rule engine [{}], deviceRpcCallMsg {}"
,
device
,
deviceRpcCallMsg
,
t
);
}
});
}
catch
(
JsonProcessingException
|
IllegalArgumentException
e
)
{
log
.
warn
(
"[{}] Failed to push TO_SERVER_RPC_REQUEST to rule engine. deviceRpcCallMsg {}"
,
deviceId
,
deviceRpcCallMsg
,
e
);
}
return
Futures
.
immediateFuture
(
null
);
}
public
DownlinkMsg
convertDeviceEventToDownlink
(
EdgeEvent
edgeEvent
)
{
DeviceId
deviceId
=
new
DeviceId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
case
ASSIGNED_TO_EDGE:
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
Device
device
=
deviceService
.
findDeviceById
(
edgeEvent
.
getTenantId
(),
deviceId
);
if
(
device
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
DeviceUpdateMsg
deviceUpdateMsg
=
deviceMsgConstructor
.
constructDeviceUpdatedMsg
(
msgType
,
device
,
null
);
DownlinkMsg
.
Builder
builder
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceUpdateMsg
(
deviceUpdateMsg
);
if
(
UpdateMsgType
.
ENTITY_CREATED_RPC_MESSAGE
.
equals
(
msgType
))
{
DeviceProfile
deviceProfile
=
deviceProfileService
.
findDeviceProfileById
(
edgeEvent
.
getTenantId
(),
device
.
getDeviceProfileId
());
builder
.
addDeviceProfileUpdateMsg
(
deviceProfileMsgConstructor
.
constructDeviceProfileUpdatedMsg
(
msgType
,
deviceProfile
));
}
downlinkMsg
=
builder
.
build
();
}
break
;
case
DELETED:
case
UNASSIGNED_FROM_EDGE:
DeviceUpdateMsg
deviceUpdateMsg
=
deviceMsgConstructor
.
constructDeviceDeleteMsg
(
deviceId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceUpdateMsg
(
deviceUpdateMsg
)
.
build
();
break
;
case
CREDENTIALS_UPDATED:
DeviceCredentials
deviceCredentials
=
deviceCredentialsService
.
findDeviceCredentialsByDeviceId
(
edgeEvent
.
getTenantId
(),
deviceId
);
if
(
deviceCredentials
!=
null
)
{
DeviceCredentialsUpdateMsg
deviceCredentialsUpdateMsg
=
deviceMsgConstructor
.
constructDeviceCredentialsUpdatedMsg
(
deviceCredentials
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceCredentialsUpdateMsg
(
deviceCredentialsUpdateMsg
)
.
build
();
}
break
;
case
RPC_CALL:
return
convertRpcCallEventToDownlink
(
edgeEvent
);
case
CREDENTIALS_REQUEST:
return
convertCredentialsRequestEventToDownlink
(
edgeEvent
);
case
ENTITY_MERGE_REQUEST:
return
convertEntityMergeRequestEventToDownlink
(
edgeEvent
);
}
return
downlinkMsg
;
}
private
DownlinkMsg
convertRpcCallEventToDownlink
(
EdgeEvent
edgeEvent
)
{
log
.
trace
(
"Executing convertRpcCallEventToDownlink, edgeEvent [{}]"
,
edgeEvent
);
return
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceRpcCallMsg
(
deviceMsgConstructor
.
constructDeviceRpcCallMsg
(
edgeEvent
.
getEntityId
(),
edgeEvent
.
getBody
()))
.
build
();
}
private
DownlinkMsg
convertCredentialsRequestEventToDownlink
(
EdgeEvent
edgeEvent
)
{
DeviceId
deviceId
=
new
DeviceId
(
edgeEvent
.
getEntityId
());
DeviceCredentialsRequestMsg
deviceCredentialsRequestMsg
=
DeviceCredentialsRequestMsg
.
newBuilder
()
.
setDeviceIdMSB
(
deviceId
.
getId
().
getMostSignificantBits
())
.
setDeviceIdLSB
(
deviceId
.
getId
().
getLeastSignificantBits
())
.
build
();
DownlinkMsg
.
Builder
builder
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceCredentialsRequestMsg
(
deviceCredentialsRequestMsg
);
return
builder
.
build
();
}
public
DownlinkMsg
convertEntityMergeRequestEventToDownlink
(
EdgeEvent
edgeEvent
)
{
DeviceId
deviceId
=
new
DeviceId
(
edgeEvent
.
getEntityId
());
Device
device
=
deviceService
.
findDeviceById
(
edgeEvent
.
getTenantId
(),
deviceId
);
String
conflictName
=
null
;
if
(
edgeEvent
.
getBody
()
!=
null
)
{
conflictName
=
edgeEvent
.
getBody
().
get
(
"conflictName"
).
asText
();
}
DeviceUpdateMsg
deviceUpdateMsg
=
deviceMsgConstructor
.
constructDeviceUpdatedMsg
(
UpdateMsgType
.
ENTITY_MERGE_RPC_MESSAGE
,
device
,
conflictName
);
return
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceUpdateMsg
(
deviceUpdateMsg
)
.
build
();
}
public
ListenableFuture
<
Void
>
processDeviceNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotification
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/DeviceProfileEdgeProcessor.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.DeviceProfile
;
import
org.thingsboard.server.common.data.EdgeUtils
;
import
org.thingsboard.server.common.data.edge.EdgeEvent
;
import
org.thingsboard.server.common.data.id.DeviceProfileId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.gen.edge.v1.DeviceProfileUpdateMsg
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
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
DeviceProfileEdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertDeviceProfileEventToDownlink
(
EdgeEvent
edgeEvent
)
{
DeviceProfileId
deviceProfileId
=
new
DeviceProfileId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ADDED:
case
UPDATED:
DeviceProfile
deviceProfile
=
deviceProfileService
.
findDeviceProfileById
(
edgeEvent
.
getTenantId
(),
deviceProfileId
);
if
(
deviceProfile
!=
null
)
{
UpdateMsgType
msgType
=
getUpdateMsgType
(
edgeEvent
.
getAction
());
DeviceProfileUpdateMsg
deviceProfileUpdateMsg
=
deviceProfileMsgConstructor
.
constructDeviceProfileUpdatedMsg
(
msgType
,
deviceProfile
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceProfileUpdateMsg
(
deviceProfileUpdateMsg
)
.
build
();
}
break
;
case
DELETED:
DeviceProfileUpdateMsg
deviceProfileUpdateMsg
=
deviceProfileMsgConstructor
.
constructDeviceProfileDeleteMsg
(
deviceProfileId
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
addDeviceProfileUpdateMsg
(
deviceProfileUpdateMsg
)
.
build
();
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processDeviceProfileNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
return
processEntityNotificationForAllEdges
(
tenantId
,
edgeNotificationMsg
);
}
}
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/EdgeProcessor.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.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.User
;
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.CustomerId
;
import
org.thingsboard.server.common.data.id.EdgeId
;
import
org.thingsboard.server.common.data.id.TenantId
;
import
org.thingsboard.server.common.data.page.PageData
;
import
org.thingsboard.server.common.data.page.PageLink
;
import
org.thingsboard.server.gen.edge.v1.DownlinkMsg
;
import
org.thingsboard.server.gen.edge.v1.EdgeConfiguration
;
import
org.thingsboard.server.gen.transport.TransportProtos
;
import
org.thingsboard.server.queue.util.TbCoreComponent
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
@Component
@Slf4j
@TbCoreComponent
public
class
EdgeProcessor
extends
BaseEdgeProcessor
{
public
DownlinkMsg
convertEdgeEventToDownlink
(
EdgeEvent
edgeEvent
)
{
EdgeId
edgeId
=
new
EdgeId
(
edgeEvent
.
getEntityId
());
DownlinkMsg
downlinkMsg
=
null
;
switch
(
edgeEvent
.
getAction
())
{
case
ASSIGNED_TO_CUSTOMER:
case
UNASSIGNED_FROM_CUSTOMER:
Edge
edge
=
edgeService
.
findEdgeById
(
edgeEvent
.
getTenantId
(),
edgeId
);
if
(
edge
!=
null
)
{
EdgeConfiguration
edgeConfigMsg
=
edgeMsgConstructor
.
constructEdgeConfiguration
(
edge
);
downlinkMsg
=
DownlinkMsg
.
newBuilder
()
.
setDownlinkMsgId
(
EdgeUtils
.
nextPositiveInt
())
.
setEdgeConfiguration
(
edgeConfigMsg
)
.
build
();
}
break
;
}
return
downlinkMsg
;
}
public
ListenableFuture
<
Void
>
processEdgeNotification
(
TenantId
tenantId
,
TransportProtos
.
EdgeNotificationMsgProto
edgeNotificationMsg
)
{
try
{
EdgeEventActionType
actionType
=
EdgeEventActionType
.
valueOf
(
edgeNotificationMsg
.
getAction
());
EdgeId
edgeId
=
new
EdgeId
(
new
UUID
(
edgeNotificationMsg
.
getEntityIdMSB
(),
edgeNotificationMsg
.
getEntityIdLSB
()));
switch
(
actionType
)
{
case
ASSIGNED_TO_CUSTOMER:
CustomerId
customerId
=
JacksonUtil
.
OBJECT_MAPPER
.
readValue
(
edgeNotificationMsg
.
getBody
(),
CustomerId
.
class
);
Edge
edge
=
edgeService
.
findEdgeById
(
tenantId
,
edgeId
);
if
(
edge
==
null
||
customerId
.
isNullUid
())
{
return
Futures
.
immediateFuture
(
null
);
}
List
<
ListenableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
futures
.
add
(
saveEdgeEvent
(
edge
.
getTenantId
(),
edge
.
getId
(),
EdgeEventType
.
CUSTOMER
,
EdgeEventActionType
.
ADDED
,
customerId
,
null
));
futures
.
add
(
saveEdgeEvent
(
edge
.
getTenantId
(),
edge
.
getId
(),
EdgeEventType
.
EDGE
,
EdgeEventActionType
.
ASSIGNED_TO_CUSTOMER
,
edgeId
,
null
));
PageLink
pageLink
=
new
PageLink
(
DEFAULT_PAGE_SIZE
);
PageData
<
User
>
pageData
;
do
{
pageData
=
userService
.
findCustomerUsers
(
tenantId
,
customerId
,
pageLink
);
if
(
pageData
!=
null
&&
pageData
.
getData
()
!=
null
&&
!
pageData
.
getData
().
isEmpty
())
{
log
.
trace
(
"[{}] [{}] user(s) are going to be added to edge."
,
edge
.
getId
(),
pageData
.
getData
().
size
());
for
(
User
user
:
pageData
.
getData
())
{
futures
.
add
(
saveEdgeEvent
(
edge
.
getTenantId
(),
edge
.
getId
(),
EdgeEventType
.
USER
,
EdgeEventActionType
.
ADDED
,
user
.
getId
(),
null
));
}
if
(
pageData
.
hasNext
())
{
pageLink
=
pageLink
.
nextPageLink
();
}
}
}
while
(
pageData
!=
null
&&
pageData
.
hasNext
());
return
Futures
.
transform
(
Futures
.
allAsList
(
futures
),
voids
->
null
,
dbCallbackExecutorService
);
case
UNASSIGNED_FROM_CUSTOMER:
CustomerId
customerIdToDelete
=
JacksonUtil
.
OBJECT_MAPPER
.
readValue
(
edgeNotificationMsg
.
getBody
(),
CustomerId
.
class
);
edge
=
edgeService
.
findEdgeById
(
tenantId
,
edgeId
);
if
(
edge
==
null
||
customerIdToDelete
.
isNullUid
())
{
return
Futures
.
immediateFuture
(
null
);
}
return
Futures
.
transformAsync
(
saveEdgeEvent
(
edge
.
getTenantId
(),
edge
.
getId
(),
EdgeEventType
.
EDGE
,
EdgeEventActionType
.
UNASSIGNED_FROM_CUSTOMER
,
edgeId
,
null
),
voids
->
saveEdgeEvent
(
edge
.
getTenantId
(),
edge
.
getId
(),
EdgeEventType
.
CUSTOMER
,
EdgeEventActionType
.
DELETED
,
customerIdToDelete
,
null
),
dbCallbackExecutorService
);
default
:
return
Futures
.
immediateFuture
(
null
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"Exception during processing edge event"
,
e
);
return
Futures
.
immediateFailedFuture
(
e
);
}
}
}
Prev
1
…
9
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