Use the same device session info in transport service and in context

This commit is contained in:
Viacheslav Kukhtyn 2020-11-23 13:08:45 +02:00 committed by Andrew Shvayka
parent 7834b92217
commit 63406b010f
7 changed files with 27 additions and 22 deletions

View File

@ -706,7 +706,7 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement
} }
@Override @Override
public void onDeviceProfileUpdate(Device device) { public void onDeviceProfileUpdate(Device device, TransportProtos.SessionInfoProto sessionInfo) {
deviceSessionCtx.onDeviceProfileUpdate(device); deviceSessionCtx.onDeviceProfileUpdate(device, sessionInfo);
} }
} }

View File

@ -20,7 +20,6 @@ import io.netty.channel.ChannelHandlerContext;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.DeviceTransportType;
import org.thingsboard.server.common.data.TransportPayloadType; import org.thingsboard.server.common.data.TransportPayloadType;
@ -114,11 +113,6 @@ public class DeviceSessionCtx extends MqttDeviceAwareSessionContext {
updateTopicFilters(deviceProfile); updateTopicFilters(deviceProfile);
} }
@Override
public void onDeviceProfileUpdate(Device device) {
super.onDeviceProfileUpdate(device);
}
private void updateTopicFilters(DeviceProfile deviceProfile) { private void updateTopicFilters(DeviceProfile deviceProfile) {
DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
if (transportConfiguration.getType().equals(DeviceTransportType.MQTT) && if (transportConfiguration.getType().equals(DeviceTransportType.MQTT) &&

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.common.transport;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.gen.transport.TransportProtos;
import org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg; import org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg;
import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg; import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg;
import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg; import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg;
@ -41,6 +42,6 @@ public interface SessionMsgListener {
default void onProfileUpdate(DeviceProfile deviceProfile) { default void onProfileUpdate(DeviceProfile deviceProfile) {
} }
default void onDeviceProfileUpdate(Device device) { default void onDeviceProfileUpdate(Device device, TransportProtos.SessionInfoProto sessionInfo) {
} }
} }

View File

@ -15,7 +15,7 @@
*/ */
package org.thingsboard.server.common.transport; package org.thingsboard.server.common.transport;
import org.thingsboard.server.common.msg.session.SessionContext; import org.thingsboard.server.common.transport.session.SessionContext;
public interface TransportAdaptor<C extends SessionContext, T, V> { public interface TransportAdaptor<C extends SessionContext, T, V> {

View File

@ -684,14 +684,28 @@ public class DefaultTransportService implements TransportService {
long deviceIdLSB = device.getId().getId().getLeastSignificantBits(); long deviceIdLSB = device.getId().getId().getLeastSignificantBits();
long deviceProfileIdMSB = device.getDeviceProfileId().getId().getMostSignificantBits(); long deviceProfileIdMSB = device.getDeviceProfileId().getId().getMostSignificantBits();
long deviceProfileIdLSB = device.getDeviceProfileId().getId().getLeastSignificantBits(); long deviceProfileIdLSB = device.getDeviceProfileId().getId().getLeastSignificantBits();
sessions.forEach((id, md) -> { for (Map.Entry<UUID, SessionMetaData> entry : sessions.entrySet()) {
SessionMetaData md = entry.getValue();
if ((md.getSessionInfo().getDeviceIdMSB() == deviceIdMSB if ((md.getSessionInfo().getDeviceIdMSB() == deviceIdMSB
&& md.getSessionInfo().getDeviceIdLSB() == deviceIdLSB) && md.getSessionInfo().getDeviceIdLSB() == deviceIdLSB)
&& (md.getSessionInfo().getDeviceProfileIdMSB() != deviceProfileIdMSB && (md.getSessionInfo().getDeviceProfileIdMSB() != deviceProfileIdMSB
&& md.getSessionInfo().getDeviceProfileIdLSB() != deviceProfileIdLSB)) { && md.getSessionInfo().getDeviceProfileIdLSB() != deviceProfileIdLSB)) {
transportCallbackExecutor.submit(() -> md.getListener().onDeviceProfileUpdate(device)); updateSessionMetadata(device, entry, md);
} }
}); }
}
private void updateSessionMetadata(Device device, Map.Entry<UUID, SessionMetaData> entry, SessionMetaData md) {
TransportProtos.SessionInfoProto newSessionInfo = TransportProtos.SessionInfoProto.newBuilder()
.mergeFrom(md.getSessionInfo())
.setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits())
.setDeviceProfileIdLSB(device.getDeviceProfileId().getId().getLeastSignificantBits())
.setDeviceType(device.getType())
.build();
SessionMetaData newSessionMetaData = new SessionMetaData(newSessionInfo, md.getSessionType(), md.getListener());
entry.setValue(newSessionMetaData);
transportCallbackExecutor.submit(() -> newSessionMetaData.getListener().onDeviceProfileUpdate(device,
newSessionMetaData.getSessionInfo()));
} }
protected UUID toSessionId(TransportProtos.SessionInfoProto sessionInfo) { protected UUID toSessionId(TransportProtos.SessionInfoProto sessionInfo) {

View File

@ -21,7 +21,6 @@ import lombok.Setter;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.msg.session.SessionContext;
import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; import org.thingsboard.server.common.transport.auth.TransportDeviceInfo;
import org.thingsboard.server.gen.transport.TransportProtos; import org.thingsboard.server.gen.transport.TransportProtos;
@ -65,14 +64,10 @@ public abstract class DeviceAwareSessionContext implements SessionContext {
this.sessionInfo = TransportProtos.SessionInfoProto.newBuilder().mergeFrom(sessionInfo).setDeviceType(deviceProfile.getName()).build(); this.sessionInfo = TransportProtos.SessionInfoProto.newBuilder().mergeFrom(sessionInfo).setDeviceType(deviceProfile.getName()).build();
} }
public void onDeviceProfileUpdate(Device device) { public void onDeviceProfileUpdate(Device device, TransportProtos.SessionInfoProto sessionInfo) {
this.deviceInfo.setDeviceProfileId(device.getDeviceProfileId()); this.deviceInfo.setDeviceProfileId(device.getDeviceProfileId());
this.deviceInfo.setDeviceType(device.getType()); this.deviceInfo.setDeviceType(device.getType());
this.sessionInfo = TransportProtos.SessionInfoProto.newBuilder().mergeFrom(sessionInfo) this.sessionInfo = sessionInfo;
.setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits())
.setDeviceProfileIdLSB(device.getDeviceProfileId().getId().getLeastSignificantBits())
.setDeviceType(device.getType())
.build();
} }
public boolean isConnected() { public boolean isConnected() {

View File

@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.common.msg.session; package org.thingsboard.server.common.transport.session;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.gen.transport.TransportProtos;
import java.util.UUID; import java.util.UUID;
@ -28,5 +29,5 @@ public interface SessionContext {
void onProfileUpdate(DeviceProfile deviceProfile); void onProfileUpdate(DeviceProfile deviceProfile);
void onDeviceProfileUpdate(Device device); void onDeviceProfileUpdate(Device device, TransportProtos.SessionInfoProto sessionInfo);
} }