Browse Source

refactoring

pull/8803/head
dashevchenko 3 years ago
parent
commit
2eeb3a1639
  1. 10
      application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java
  2. 101
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

10
application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java

@ -708,7 +708,7 @@ public class DeviceControllerTest extends AbstractControllerTest {
credentials.getCredentialsId()));
assertThat(commands.get("mqtt")).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"",
credentials.getCredentialsId()));
assertThat(commands.get("mqtts")).isEqualTo(String.format("mosquitto_pub --cafile tb-server-chain.pem -d -q 1 -h localhost -p 8883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"",
assertThat(commands.get("mqtts")).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"",
credentials.getCredentialsId()));
assertThat(commands.get("coap")).isEqualTo(String.format("coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
credentials.getCredentialsId()));
@ -731,7 +731,7 @@ public class DeviceControllerTest extends AbstractControllerTest {
assertThat(commands).hasSize(2);
assertThat(commands.get("mqtt")).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s -u %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
assertThat(commands.get("mqtts")).isEqualTo(String.format("mosquitto_pub --cafile tb-server-chain.pem -d -q 1 -h localhost -p 8883 -t %s -u %s -m \"{temperature:25}\"",
assertThat(commands.get("mqtts")).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t %s -u %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
}
@ -762,7 +762,7 @@ public class DeviceControllerTest extends AbstractControllerTest {
assertThat(commands).hasSize(2);
assertThat(commands.get("mqtt")).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
assertThat(commands.get("mqtts")).isEqualTo(String.format("mosquitto_pub --cafile tb-server-chain.pem -d -q 1 -h localhost -p 8883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"",
assertThat(commands.get("mqtts")).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
}
@ -784,7 +784,7 @@ public class DeviceControllerTest extends AbstractControllerTest {
Map<String, String> commands =
doGetTyped("/api/device/" + savedDevice.getId().getId() + "/commands", new TypeReference<>() {});
assertThat(commands).hasSize(1);
assertThat(commands.get("mqtts")).isEqualTo("Not supported");
assertThat(commands.get("mqtts")).isEqualTo("Not provided");
}
@Test
@ -824,7 +824,7 @@ public class DeviceControllerTest extends AbstractControllerTest {
Map<String, String> commands =
doGetTyped("/api/device/" + savedDevice.getId().getId() + "/commands", new TypeReference<>() {});
assertThat(commands).hasSize(1);
assertThat(commands.get("coaps")).isEqualTo("Not supported");
assertThat(commands.get("coaps")).isEqualTo("Not provided");
}
@Test

101
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -109,7 +109,7 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
public static final String COAP_PROTOCOL = "coap";
public static final String COAPS_PROTOCOL = "coaps";
public static final String PAYLOAD = "\"{temperature:25}\"";
public static final String NOT_SUPPORTED = "Not supported";
public static final String NOT_PROVIDED = "Not provided";
public static final String DEFAULT_DEVICE_TELEMETRY_TOPIC = "v1/devices/me/telemetry";
@Autowired
@ -176,7 +176,7 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
Optional.ofNullable(getCoapsPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(COAPS_PROTOCOL, v));
break;
default:
commands.put(transportType.name(), NOT_SUPPORTED);
commands.put(transportType.name(), NOT_PROVIDED);
}
return commands;
}
@ -765,48 +765,11 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
}
private String getMqttPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
return getMqttPublishCommand(defaultHostname,"v1/devices/me/telemetry", deviceCredentials, " -m " + PAYLOAD);
return getMqttPublishCommand(defaultHostname,DEFAULT_DEVICE_TELEMETRY_TOPIC, deviceCredentials, " -m " + PAYLOAD);
}
private String getMqttPublishCommand(String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload) {
DeviceConnectivityInfo mqttProps = deviceConnectivityConfiguration.getConnectivity().get(MQTT_PROTOCOL);
if (mqttProps == null || !mqttProps.getEnabled()) {
return null;
}
StringBuilder command = new StringBuilder("mosquitto_pub -d -q 1");
command.append(" -h ").append(mqttProps.getHost().isEmpty() ? defaultHostname : mqttProps.getHost());
if (!mqttProps.getPort().isEmpty()) {
command.append(" -p ").append(mqttProps.getPort());
}
command.append(" -t ").append(deviceTelemetryTopic);
switch (deviceCredentials.getCredentialsType()) {
case ACCESS_TOKEN:
command.append(" -u ").append(deviceCredentials.getCredentialsId());
break;
case MQTT_BASIC:
BasicMqttCredentials credentials = JacksonUtil.fromString(deviceCredentials.getCredentialsValue(),
BasicMqttCredentials.class);
if (credentials != null) {
if (credentials.getClientId() != null) {
command.append(" -i ").append(credentials.getClientId());
}
if (credentials.getUserName() != null) {
command.append(" -u ").append(credentials.getUserName());
}
if (credentials.getPassword() != null) {
command.append(" -P ").append(credentials.getPassword());
}
} else {
return null;
}
break;
default:
return null;
}
command.append(payload);
return command.toString();
return getMosquittoPublishCommand(defaultHostname, deviceTelemetryTopic, deviceCredentials, payload, MQTT_PROTOCOL);
}
private String getMqttsPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
@ -814,16 +777,22 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
}
private String getMqttsPublishCommand(String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload) {
DeviceConnectivityInfo mqttsProps = deviceConnectivityConfiguration.getConnectivity().get(MQTTS_PROTOCOL);
if (mqttsProps == null || !mqttsProps.getEnabled()) {
return getMosquittoPublishCommand(defaultHostname, deviceTelemetryTopic, deviceCredentials, payload, MQTTS_PROTOCOL);
}
private String getMosquittoPublishCommand(String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload, String protocol) {
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
if (properties == null || !properties.getEnabled()) {
return null;
}
StringBuilder command = new StringBuilder("mosquitto_pub --cafile tb-server-chain.pem -d -q 1");
command.append(" -h ").append(mqttsProps.getHost().isEmpty() ? defaultHostname : mqttsProps.getHost());
if (!mqttsProps.getPort().isEmpty()) {
command.append(" -p ").append(mqttsProps.getPort());
StringBuilder command = new StringBuilder("mosquitto_pub -d -q 1");
if (MQTTS_PROTOCOL.equals(protocol)) {
command.append(" --cafile tb-server-chain.pem");
}
String mqttHost = properties.getHost().isEmpty() ? defaultHostname : properties.getHost();
String mqttPort = properties.getPort().isEmpty() ? "" : " -p " + properties.getPort();
command.append(" -h ").append(mqttHost).append(mqttPort);
command.append(" -t ").append(deviceTelemetryTopic);
switch (deviceCredentials.getCredentialsType()) {
@ -848,7 +817,9 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
}
break;
case X509_CERTIFICATE:
return NOT_SUPPORTED;
if (MQTTS_PROTOCOL.equals(protocol)) {
return NOT_PROVIDED;
}
default:
return null;
}
@ -857,33 +828,29 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
}
private String getCoapPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
DeviceConnectivityInfo coapProperties = deviceConnectivityConfiguration.getConnectivity().get(COAP_PROTOCOL);
if (coapProperties == null || !coapProperties.getEnabled()) {
return null;
}
if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.ACCESS_TOKEN) {
String hostName = coapProperties.getHost().isEmpty() ? defaultHostname : coapProperties.getHost();
String port = coapProperties.getPort().isEmpty() ? "" : ":" + coapProperties.getPort();
return String.format("coap-client -m POST coap://%s%s/api/v1/%s/telemetry -t json -e %s",
hostName, port, deviceCredentials.getCredentialsId(), PAYLOAD);
}
return null;
return getCoapClientCommand(defaultHostname, deviceCredentials, COAP_PROTOCOL);
}
private String getCoapsPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
DeviceConnectivityInfo coapsProperties = deviceConnectivityConfiguration.getConnectivity().get(COAPS_PROTOCOL);
if (coapsProperties == null || !coapsProperties.getEnabled()) {
return getCoapClientCommand(defaultHostname, deviceCredentials, COAPS_PROTOCOL);
}
private String getCoapClientCommand(String defaultHostname, DeviceCredentials deviceCredentials, String protocol) {
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
if (properties == null || !properties.getEnabled()) {
return null;
}
switch (deviceCredentials.getCredentialsType()) {
case ACCESS_TOKEN:
String hostName = coapsProperties.getHost().isEmpty() ? defaultHostname : coapsProperties.getHost();
String port = coapsProperties.getPort().isEmpty() ? "" : ":" + coapsProperties.getPort();
return String.format("coap-client-openssl -v 9 -m POST coaps://%s%s/api/v1/%s/telemetry -t json -e %s",
hostName, port, deviceCredentials.getCredentialsId(), PAYLOAD);
String hostName = properties.getHost().isEmpty() ? defaultHostname : properties.getHost();
String port = properties.getPort().isEmpty() ? "" : ":" + properties.getPort();
String client = COAPS_PROTOCOL.equals(protocol) ? "coap-client-openssl -v 9" : "coap-client";
return String.format("%s -m POST %s://%s%s/api/v1/%s/telemetry -t json -e %s",
client, protocol, hostName, port, deviceCredentials.getCredentialsId(), PAYLOAD);
case X509_CERTIFICATE:
return NOT_SUPPORTED;
if (COAPS_PROTOCOL.equals(protocol)) {
return NOT_PROVIDED;
}
default:
return null;
}

Loading…
Cancel
Save