Commit aa816d61ab93b06305b28dba421ff73a4ee8c61b
Committed by
Andrew Shvayka
1 parent
856555f5
Handle device update or delete event in device's session
Showing
7 changed files
with
52 additions
and
2 deletions
... | ... | @@ -118,6 +118,7 @@ public class DeviceController extends BaseController { |
118 | 118 | |
119 | 119 | Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken)); |
120 | 120 | |
121 | + tbClusterService.onDeviceChange(savedDevice, null); | |
121 | 122 | tbClusterService.pushMsgToCore(new DeviceNameOrTypeUpdateMsg(savedDevice.getTenantId(), |
122 | 123 | savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); |
123 | 124 | tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), |
... | ... | @@ -150,6 +151,9 @@ public class DeviceController extends BaseController { |
150 | 151 | Device device = checkDeviceId(deviceId, Operation.DELETE); |
151 | 152 | deviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId); |
152 | 153 | |
154 | + tbClusterService.onDeviceDeleted(device, null); | |
155 | + tbClusterService.onEntityStateChange(device.getTenantId(), deviceId, ComponentLifecycleEvent.DELETED); | |
156 | + | |
153 | 157 | logEntityAction(deviceId, device, |
154 | 158 | device.getCustomerId(), |
155 | 159 | ActionType.DELETED, null, strDeviceId); | ... | ... |
... | ... | @@ -22,6 +22,7 @@ import org.springframework.scheduling.annotation.Scheduled; |
22 | 22 | import org.springframework.stereotype.Service; |
23 | 23 | import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; |
24 | 24 | import org.thingsboard.server.common.data.ApiUsageState; |
25 | +import org.thingsboard.server.common.data.Device; | |
25 | 26 | import org.thingsboard.server.common.data.DeviceProfile; |
26 | 27 | import org.thingsboard.server.common.data.EntityType; |
27 | 28 | import org.thingsboard.server.common.data.HasName; |
... | ... | @@ -32,7 +33,6 @@ import org.thingsboard.server.common.data.id.DeviceProfileId; |
32 | 33 | import org.thingsboard.server.common.data.id.EntityId; |
33 | 34 | import org.thingsboard.server.common.data.id.RuleChainId; |
34 | 35 | import org.thingsboard.server.common.data.id.TenantId; |
35 | -import org.thingsboard.server.common.data.id.TenantProfileId; | |
36 | 36 | import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
37 | 37 | import org.thingsboard.server.common.msg.TbMsg; |
38 | 38 | import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg; |
... | ... | @@ -237,6 +237,16 @@ public class DefaultTbClusterService implements TbClusterService { |
237 | 237 | onEntityDelete(TenantId.SYS_TENANT_ID, entity.getId(), entity.getName(), callback); |
238 | 238 | } |
239 | 239 | |
240 | + @Override | |
241 | + public void onDeviceChange(Device entity, TbQueueCallback callback) { | |
242 | + onEntityChange(entity.getTenantId(), entity.getId(), entity, callback); | |
243 | + } | |
244 | + | |
245 | + @Override | |
246 | + public void onDeviceDeleted(Device entity, TbQueueCallback callback) { | |
247 | + onEntityDelete(entity.getTenantId(), entity.getId(), entity.getName(), callback); | |
248 | + } | |
249 | + | |
240 | 250 | public <T> void onEntityChange(TenantId tenantId, EntityId entityid, T entity, TbQueueCallback callback) { |
241 | 251 | String entityName = (entity instanceof HasName) ? ((HasName) entity).getName() : entity.getClass().getName(); |
242 | 252 | log.trace("[{}][{}][{}] Processing [{}] change event", tenantId, entityid.getEntityType(), entityid.getId(), entityName); | ... | ... |
... | ... | @@ -17,6 +17,7 @@ package org.thingsboard.server.service.queue; |
17 | 17 | |
18 | 18 | import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; |
19 | 19 | import org.thingsboard.server.common.data.ApiUsageState; |
20 | +import org.thingsboard.server.common.data.Device; | |
20 | 21 | import org.thingsboard.server.common.data.DeviceProfile; |
21 | 22 | import org.thingsboard.server.common.data.Tenant; |
22 | 23 | import org.thingsboard.server.common.data.TenantProfile; |
... | ... | @@ -66,4 +67,8 @@ public interface TbClusterService { |
66 | 67 | void onTenantDelete(Tenant tenant, TbQueueCallback callback); |
67 | 68 | |
68 | 69 | void onApiStateChange(ApiUsageState apiUsageState, TbQueueCallback callback); |
70 | + | |
71 | + void onDeviceChange(Device device, TbQueueCallback callback); | |
72 | + | |
73 | + void onDeviceDeleted(Device device, TbQueueCallback callback); | |
69 | 74 | } | ... | ... |
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.common.msg.session; |
17 | 17 | |
18 | +import org.thingsboard.server.common.data.Device; | |
18 | 19 | import org.thingsboard.server.common.data.DeviceProfile; |
19 | 20 | |
20 | 21 | import java.util.UUID; |
... | ... | @@ -26,4 +27,6 @@ public interface SessionContext { |
26 | 27 | int nextMsgId(); |
27 | 28 | |
28 | 29 | void onProfileUpdate(DeviceProfile deviceProfile); |
30 | + | |
31 | + void onDeviceUpdate(Device device); | |
29 | 32 | } | ... | ... |
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.common.transport; |
17 | 17 | |
18 | +import org.thingsboard.server.common.data.Device; | |
18 | 19 | import org.thingsboard.server.common.data.DeviceProfile; |
19 | 20 | import org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg; |
20 | 21 | import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg; |
... | ... | @@ -40,4 +41,6 @@ public interface SessionMsgListener { |
40 | 41 | default void onProfileUpdate(DeviceProfile deviceProfile) { |
41 | 42 | } |
42 | 43 | |
44 | + default void onDeviceUpdate(Device device) { | |
45 | + } | |
43 | 46 | } | ... | ... |
... | ... | @@ -27,6 +27,7 @@ import org.springframework.stereotype.Service; |
27 | 27 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
28 | 28 | import org.thingsboard.server.common.data.ApiUsageRecordKey; |
29 | 29 | import org.thingsboard.server.common.data.ApiUsageState; |
30 | +import org.thingsboard.server.common.data.Device; | |
30 | 31 | import org.thingsboard.server.common.data.DeviceProfile; |
31 | 32 | import org.thingsboard.server.common.data.DeviceTransportType; |
32 | 33 | import org.thingsboard.server.common.data.EntityType; |
... | ... | @@ -643,6 +644,9 @@ public class DefaultTransportService implements TransportService { |
643 | 644 | rateLimitService.update(apiUsageState.getTenantId(), apiUsageState.isTransportEnabled()); |
644 | 645 | //TODO: if transport is disabled, we should close all sessions and not to check credentials. |
645 | 646 | } |
647 | + } else if (EntityType.DEVICE.equals(entityType)) { | |
648 | + Optional<Device> deviceOpt = dataDecodingEncodingService.decode(msg.getData().toByteArray()); | |
649 | + deviceOpt.ifPresent(this::onDeviceUpdate); | |
646 | 650 | } |
647 | 651 | } else if (toSessionMsg.hasEntityDeleteMsg()) { |
648 | 652 | TransportProtos.EntityDeleteMsg msg = toSessionMsg.getEntityDeleteMsg(); |
... | ... | @@ -675,6 +679,22 @@ public class DefaultTransportService implements TransportService { |
675 | 679 | }); |
676 | 680 | } |
677 | 681 | |
682 | + private void onDeviceUpdate(Device device) { | |
683 | + long deviceIdMSB = device.getId().getId().getMostSignificantBits(); | |
684 | + long deviceIdLSB = device.getId().getId().getLeastSignificantBits(); | |
685 | + sessions.forEach((id, md) -> { | |
686 | + if (md.getSessionInfo().getDeviceIdMSB() == deviceIdMSB | |
687 | + && md.getSessionInfo().getDeviceIdLSB() == deviceIdLSB) { | |
688 | + long deviceProfileIdMSB = device.getDeviceProfileId().getId().getMostSignificantBits(); | |
689 | + long deviceProfileIdLSB = device.getDeviceProfileId().getId().getLeastSignificantBits(); | |
690 | + if (md.getSessionInfo().getDeviceProfileIdMSB() != deviceProfileIdMSB | |
691 | + && md.getSessionInfo().getDeviceProfileIdLSB() != deviceProfileIdLSB) { | |
692 | + transportCallbackExecutor.submit(() -> md.getListener().onDeviceUpdate(device)); | |
693 | + } | |
694 | + } | |
695 | + }); | |
696 | + } | |
697 | + | |
678 | 698 | protected UUID toSessionId(TransportProtos.SessionInfoProto sessionInfo) { |
679 | 699 | return new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB()); |
680 | 700 | } | ... | ... |
... | ... | @@ -18,12 +18,12 @@ package org.thingsboard.server.common.transport.session; |
18 | 18 | import lombok.Data; |
19 | 19 | import lombok.Getter; |
20 | 20 | import lombok.Setter; |
21 | +import org.thingsboard.server.common.data.Device; | |
21 | 22 | import org.thingsboard.server.common.data.DeviceProfile; |
22 | 23 | import org.thingsboard.server.common.data.id.DeviceId; |
23 | 24 | import org.thingsboard.server.common.msg.session.SessionContext; |
24 | 25 | import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; |
25 | 26 | import org.thingsboard.server.gen.transport.TransportProtos; |
26 | -import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; | |
27 | 27 | |
28 | 28 | import java.util.UUID; |
29 | 29 | |
... | ... | @@ -65,6 +65,11 @@ public abstract class DeviceAwareSessionContext implements SessionContext { |
65 | 65 | this.sessionInfo = TransportProtos.SessionInfoProto.newBuilder().mergeFrom(sessionInfo).setDeviceType(deviceProfile.getName()).build(); |
66 | 66 | } |
67 | 67 | |
68 | + @Override | |
69 | + public void onDeviceUpdate(Device device) { | |
70 | + this.deviceInfo.setDeviceProfileId(device.getDeviceProfileId()); | |
71 | + } | |
72 | + | |
68 | 73 | public boolean isConnected() { |
69 | 74 | return connected; |
70 | 75 | } | ... | ... |