Browse Source

spark[lug - add deviceId to label

pull/14987/head
nickAS21 6 months ago
parent
commit
aecf580e04
  1. 5
      application/src/main/java/org/thingsboard/server/service/transport/DefaultTransportApiService.java
  2. 18
      application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/AbstractMqttV5ClientSparkplugTest.java
  3. 5
      application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/connection/MqttV5ClientSparkplugBConnectionDevicesCreatingBeforeTest.java

5
application/src/main/java/org/thingsboard/server/service/transport/DefaultTransportApiService.java

@ -392,6 +392,7 @@ public class DefaultTransportApiService implements TransportApiService {
return null; return null;
} }
existingDevice.setName(requestMsg.getDeviceName()); existingDevice.setName(requestMsg.getDeviceName());
existingDevice.setLabel(deviceId);
return deviceService.saveDevice(existingDevice); return deviceService.saveDevice(existingDevice);
} }
@ -402,6 +403,10 @@ public class DefaultTransportApiService implements TransportApiService {
Device device = new Device(); Device device = new Device();
device.setTenantId(tenantId); device.setTenantId(tenantId);
device.setName(requestMsg.getDeviceName()); device.setName(requestMsg.getDeviceName());
if (requestMsg.getIsSparkplug()){
String [] topicDevice = requestMsg.getDeviceName().split(DEVICE_NAME_SPLIT_REGEXP);
if (topicDevice.length == 3) device.setLabel(topicDevice[2]);
}
device.setType(requestMsg.getDeviceType()); device.setType(requestMsg.getDeviceType());
device.setCustomerId(gateway.getCustomerId()); device.setCustomerId(gateway.getCustomerId());
DeviceProfile profile = DeviceProfile profile =

18
application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/AbstractMqttV5ClientSparkplugTest.java

@ -234,18 +234,18 @@ public abstract class AbstractMqttV5ClientSparkplugTest extends AbstractMqttInte
valueDeviceInt32 = 4024; valueDeviceInt32 = 4024;
metric = createMetric(valueDeviceInt32, ts, metricBirthName_Int32, metricBirthDataType_Int32, -1L); metric = createMetric(valueDeviceInt32, ts, metricBirthName_Int32, metricBirthDataType_Int32, -1L);
// as old device name -> deviceId // as old device name -> deviceId
String deviceiDName1 = deviceId + "_1"; String deviceIdNameLabel1 = deviceId + "_1";
if (client.isConnected()) { if (client.isConnected()) {
SparkplugBProto.Payload.Builder payloadBirthDevice1 = SparkplugBProto.Payload.newBuilder() SparkplugBProto.Payload.Builder payloadBirthDevice1 = SparkplugBProto.Payload.newBuilder()
.setTimestamp(ts) .setTimestamp(ts)
.setSeq(getSeqNum()); .setSeq(getSeqNum());
payloadBirthDevice1.addMetrics(metric); payloadBirthDevice1.addMetrics(metric);
client.publish(TOPIC_ROOT_SPB_V_1_0 + TOPIC_SPLIT_REGEXP + groupId + TOPIC_SPLIT_REGEXP + SparkplugMessageType.DBIRTH.name() + TOPIC_SPLIT_REGEXP + edgeNode + TOPIC_SPLIT_REGEXP + deviceiDName1, client.publish(TOPIC_ROOT_SPB_V_1_0 + TOPIC_SPLIT_REGEXP + groupId + TOPIC_SPLIT_REGEXP + SparkplugMessageType.DBIRTH.name() + TOPIC_SPLIT_REGEXP + edgeNode + TOPIC_SPLIT_REGEXP + deviceIdNameLabel1,
payloadBirthDevice1.build().toByteArray(), 0, false); payloadBirthDevice1.build().toByteArray(), 0, false);
} }
String deviceName1 = groupId + DEVICE_NAME_SPLIT_REGEXP + edgeNode + DEVICE_NAME_SPLIT_REGEXP + deviceiDName1;; String deviceName1 = groupId + DEVICE_NAME_SPLIT_REGEXP + edgeNode + DEVICE_NAME_SPLIT_REGEXP + deviceIdNameLabel1;;
AtomicReference<Device> device1 = new AtomicReference<>(); AtomicReference<Device> device1 = new AtomicReference<>();
await(alias + "find device [" + deviceName1 + "] before connecting") await(alias + "find device [" + deviceName1 + "] before connecting")
.atMost(200, TimeUnit.SECONDS) .atMost(200, TimeUnit.SECONDS)
@ -256,16 +256,16 @@ public abstract class AbstractMqttV5ClientSparkplugTest extends AbstractMqttInte
devices.add(device1.get()); devices.add(device1.get());
// as new device name -> groupId + ":" + edgeNode + ":" + deviceId; // as new device name -> groupId + ":" + edgeNode + ":" + deviceId;
String deviceiDName2 = deviceId + "_2"; String deviceIdName2 = deviceId + "_2";
if (client.isConnected()) { if (client.isConnected()) {
SparkplugBProto.Payload.Builder payloadBirthDevice2 = SparkplugBProto.Payload.newBuilder() SparkplugBProto.Payload.Builder payloadBirthDevice2 = SparkplugBProto.Payload.newBuilder()
.setTimestamp(ts) .setTimestamp(ts)
.setSeq(getSeqNum()); .setSeq(getSeqNum());
payloadBirthDevice2.addMetrics(metric); payloadBirthDevice2.addMetrics(metric);
client.publish(TOPIC_ROOT_SPB_V_1_0 + TOPIC_SPLIT_REGEXP + groupId + TOPIC_SPLIT_REGEXP + SparkplugMessageType.DBIRTH.name() + TOPIC_SPLIT_REGEXP + edgeNode + TOPIC_SPLIT_REGEXP + deviceiDName2, client.publish(TOPIC_ROOT_SPB_V_1_0 + TOPIC_SPLIT_REGEXP + groupId + TOPIC_SPLIT_REGEXP + SparkplugMessageType.DBIRTH.name() + TOPIC_SPLIT_REGEXP + edgeNode + TOPIC_SPLIT_REGEXP + deviceIdName2,
payloadBirthDevice2.build().toByteArray(), 0, false); payloadBirthDevice2.build().toByteArray(), 0, false);
} }
String deviceName2 = groupId + ":" + edgeNode + ":" + deviceiDName2; String deviceName2 = groupId + DEVICE_NAME_SPLIT_REGEXP + edgeNode + DEVICE_NAME_SPLIT_REGEXP + deviceIdName2;
AtomicReference<Device> device2 = new AtomicReference<>(); AtomicReference<Device> device2 = new AtomicReference<>();
await(alias + "find device [" + deviceName2 + "] before connecting") await(alias + "find device [" + deviceName2 + "] before connecting")
.atMost(200, TimeUnit.SECONDS) .atMost(200, TimeUnit.SECONDS)
@ -276,6 +276,10 @@ public abstract class AbstractMqttV5ClientSparkplugTest extends AbstractMqttInte
devices.add(device2.get()); devices.add(device2.get());
Assert.assertEquals(cntDevices, devices.size()); Assert.assertEquals(cntDevices, devices.size());
state_ONLINE_ALL (devices, calendar.getTimeInMillis()); state_ONLINE_ALL (devices, calendar.getTimeInMillis());
// Without full topic: as it was in the old version. When deviceId is updated to full theme, Label is also updated to old deviceId
Assert.assertEquals(deviceIdNameLabel1, device1.get().getLabel());
// // With a full topic: if new. When creating a device by a client to a full topic, if the Label was not filled in - we do not touch it.
Assert.assertNull(device2.get().getLabel());
} }
protected void state_ONLINE_ALL (List<Device> devices, long ts) { protected void state_ONLINE_ALL (List<Device> devices, long ts) {
@ -328,7 +332,7 @@ public abstract class AbstractMqttV5ClientSparkplugTest extends AbstractMqttInte
SparkplugBProto.Payload.Builder payloadBirthDevice = SparkplugBProto.Payload.newBuilder() SparkplugBProto.Payload.Builder payloadBirthDevice = SparkplugBProto.Payload.newBuilder()
.setTimestamp(ts) .setTimestamp(ts)
.setSeq(getSeqNum()); .setSeq(getSeqNum());
String deviceIdName = deviceId + "_" + 1; String deviceIdName = deviceId + "_1";
String deviceName = groupId + ":" + edgeNode + ":" + deviceIdName; String deviceName = groupId + ":" + edgeNode + ":" + deviceIdName;
payloadBirthDevice.addMetrics(metric); payloadBirthDevice.addMetrics(metric);

5
application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/connection/MqttV5ClientSparkplugBConnectionDevicesCreatingBeforeTest.java

@ -27,6 +27,11 @@ import org.thingsboard.server.dao.service.DaoSqlTest;
@DaoSqlTest @DaoSqlTest
public class MqttV5ClientSparkplugBConnectionDevicesCreatingBeforeTest extends AbstractMqttV5ClientSparkplugConnectionTest { public class MqttV5ClientSparkplugBConnectionDevicesCreatingBeforeTest extends AbstractMqttV5ClientSparkplugConnectionTest {
/**
* String deviceName_1 = deviceId + "_1"; Only name device. Without a complete topic: how it was in the old version.
* String deviceName_2 = groupId + DEVICE_NAME_SPLIT_REGEXP + edgeNode + DEVICE_NAME_SPLIT_REGEXP + deviceId + "_2"; With complete topic: how it was in the new version.
* @throws Exception
*/
@Before @Before
public void beforeTest() throws Exception { public void beforeTest() throws Exception {
beforeSparkplugTest(true); beforeSparkplugTest(true);

Loading…
Cancel
Save