Browse Source

refactoring

pull/8803/head
dashevchenko 3 years ago
parent
commit
21329bf74e
  1. 6
      application/src/main/resources/thingsboard.yml
  2. 35
      application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java
  3. 158
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java
  4. 77
      dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java

6
application/src/main/resources/thingsboard.yml

@ -994,15 +994,15 @@ device:
host: "${DEVICE_CONNECTIVITY_MQTT_HOST:localhost}"
port: "${DEVICE_CONNECTIVITY_MQTT_PORT:1883}"
mqtts:
enabled: "${DEVICE_CONNECTIVITY_MQTT_ENABLED:false}"
enabled: "${DEVICE_CONNECTIVITY_MQTTS_ENABLED:false}"
host: "${DEVICE_CONNECTIVITY_MQTTS_HOST:localhost}"
port: "${DEVICE_CONNECTIVITY_MQTTS_PORT:8883}"
coap:
enabled: "${DEVICE_CONNECTIVITY_MQTT_ENABLED:true}"
enabled: "${DEVICE_CONNECTIVITY_COAP_ENABLED:true}"
host: "${DEVICE_CONNECTIVITY_COAP_HOST:localhost}"
port: "${DEVICE_CONNECTIVITY_COAP_PORT:5683}"
coaps:
enabled: "${DEVICE_CONNECTIVITY_MQTT_ENABLED:false}"
enabled: "${DEVICE_CONNECTIVITY_COAPS_ENABLED:false}"
host: "${DEVICE_CONNECTIVITY_COAPS_HOST:localhost}"
port: "${DEVICE_CONNECTIVITY_COAPS_PORT:5684}"

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

@ -93,6 +93,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE;
import static org.thingsboard.server.common.data.ota.OtaPackageType.SOFTWARE;
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAP;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAPS;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTTS;
@TestPropertySource(properties = {
"device.connectivity.https.enabled=true",
@ -106,6 +112,7 @@ public class DeviceControllerTest extends AbstractControllerTest {
};
private static final String DEVICE_TELEMETRY_TOPIC = "v1/devices/customTopic";
private static final String CHECK_DOCUMENTATION = "Check documentation";
ListeningExecutorService executor;
@ -702,17 +709,17 @@ public class DeviceControllerTest extends AbstractControllerTest {
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class);
assertThat(commands).hasSize(6);
assertThat(commands.get("http")).isEqualTo(String.format("curl -v -X POST http://localhost:8080/api/v1/%s/telemetry --header Content-Type:application/json --data \"{temperature:25}\"",
assertThat(commands.get(HTTP)).isEqualTo(String.format("curl -v -X POST http://localhost:8080/api/v1/%s/telemetry --header Content-Type:application/json --data \"{temperature:25}\"",
credentials.getCredentialsId()));
assertThat(commands.get("https")).isEqualTo(String.format("curl -v -X POST https://localhost:443/api/v1/%s/telemetry --header Content-Type:application/json --data \"{temperature:25}\"",
assertThat(commands.get(HTTPS)).isEqualTo(String.format("curl -v -X POST https://localhost:443/api/v1/%s/telemetry --header Content-Type:application/json --data \"{temperature:25}\"",
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}\"",
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 -d -q 1 --cafile tb-server-chain.pem -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}\"",
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()));
assertThat(commands.get("coaps")).isEqualTo(String.format("coap-client-openssl -v 9 -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
assertThat(commands.get(COAPS)).isEqualTo(String.format("coap-client-openssl -v 9 -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
credentials.getCredentialsId()));
}
@ -729,9 +736,9 @@ public class DeviceControllerTest extends AbstractControllerTest {
Map<String, String> commands =
doGetTyped("/api/device/" + savedDevice.getId().getId() + "/commands", new TypeReference<>() {});
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}\"",
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 -d -q 1 --cafile tb-server-chain.pem -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()));
}
@ -760,9 +767,9 @@ public class DeviceControllerTest extends AbstractControllerTest {
Map<String, String> commands =
doGetTyped("/api/device/" + savedDevice.getId().getId() + "/commands", new TypeReference<>() {});
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}\"",
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 -d -q 1 --cafile tb-server-chain.pem -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 +791,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 provided");
assertThat(commands.get(MQTTS)).isEqualTo(CHECK_DOCUMENTATION);
}
@Test
@ -800,9 +807,9 @@ public class DeviceControllerTest extends AbstractControllerTest {
Map<String, String> commands =
doGetTyped("/api/device/" + savedDevice.getId().getId() + "/commands", new TypeReference<>() {});
assertThat(commands).hasSize(2);
assertThat(commands.get("coap")).isEqualTo(String.format("coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
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()));
assertThat(commands.get("coaps")).isEqualTo(String.format("coap-client-openssl -v 9 -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
assertThat(commands.get(COAPS)).isEqualTo(String.format("coap-client-openssl -v 9 -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
credentials.getCredentialsId()));
}
@ -824,7 +831,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 provided");
assertThat(commands.get(COAPS)).isEqualTo(CHECK_DOCUMENTATION);
}
@Test

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

@ -91,6 +91,17 @@ import static org.thingsboard.server.dao.service.Validator.validateId;
import static org.thingsboard.server.dao.service.Validator.validateIds;
import static org.thingsboard.server.dao.service.Validator.validatePageLink;
import static org.thingsboard.server.dao.service.Validator.validateString;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAP;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAPS;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.JSON_EXAMPLE_PAYLOAD;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTTS;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.CHECK_DOCUMENTATION;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.getCoapClientCommand;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.getCurlCommand;
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.getMosquittoPublishCommand;
@Service("DeviceDaoService")
@Slf4j
@ -102,14 +113,6 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId ";
public static final String INCORRECT_DEVICE_ID = "Incorrect deviceId ";
public static final String INCORRECT_EDGE_ID = "Incorrect edgeId ";
public static final String HTTP_PROTOCOL = "http";
public static final String HTTPS_PROTOCOL = "https";
public static final String MQTT_PROTOCOL = "mqtt";
public static final String MQTTS_PROTOCOL = "mqtts";
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_PROVIDED = "Not provided";
public static final String DEFAULT_DEVICE_TELEMETRY_TOPIC = "v1/devices/me/telemetry";
@Autowired
@ -154,29 +157,29 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
Map<String, String> commands = new HashMap<>();
switch (transportType) {
case DEFAULT:
Optional.ofNullable(getHttpPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(HTTP_PROTOCOL, v));
Optional.ofNullable(getHttpsPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(HTTPS_PROTOCOL, v));
Optional.ofNullable(getMqttPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(MQTT_PROTOCOL, v));
Optional.ofNullable(getMqttsPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(MQTTS_PROTOCOL, v));
Optional.ofNullable(getCoapPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(COAP_PROTOCOL, v));
Optional.ofNullable(getCoapsPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(COAPS_PROTOCOL, v));
Optional.ofNullable(getHttpPublishCommand(HTTP, defaultHostname, creds)).ifPresent(v -> commands.put(HTTP, v));
Optional.ofNullable(getHttpPublishCommand(HTTPS, defaultHostname, creds)).ifPresent(v -> commands.put(HTTPS, v));
Optional.ofNullable(getMqttPublishCommand(MQTT, defaultHostname, creds)).ifPresent(v -> commands.put(MQTT, v));
Optional.ofNullable(getMqttPublishCommand(MQTTS, defaultHostname, creds)).ifPresent(v -> commands.put(MQTTS, v));
Optional.ofNullable(getCoapPublishCommand(COAP, defaultHostname, creds)).ifPresent(v -> commands.put(COAP, v));
Optional.ofNullable(getCoapPublishCommand(COAPS, defaultHostname, creds)).ifPresent(v -> commands.put(COAPS, v));
break;
case MQTT:
MqttDeviceProfileTransportConfiguration transportConfiguration =
(MqttDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
String topicName = transportConfiguration.getDeviceTelemetryTopic();
TransportPayloadType payloadType = transportConfiguration.getTransportPayloadTypeConfiguration().getTransportPayloadType();
String payload = (payloadType == TransportPayloadType.PROTOBUF) ? " -f protobufFileName" : " -m " + PAYLOAD;
String payload = (payloadType == TransportPayloadType.PROTOBUF) ? " -f protobufFileName" : " -m " + JSON_EXAMPLE_PAYLOAD;
Optional.ofNullable(getMqttPublishCommand(defaultHostname, topicName, creds, payload)).ifPresent(v -> commands.put(MQTT_PROTOCOL, v));
Optional.ofNullable(getMqttsPublishCommand(defaultHostname, topicName, creds, payload)).ifPresent(v -> commands.put(MQTTS_PROTOCOL, v));
Optional.ofNullable(getMqttPublishCommand(MQTT, defaultHostname, topicName, creds, payload)).ifPresent(v -> commands.put(MQTT, v));
Optional.ofNullable(getMqttPublishCommand(MQTTS, defaultHostname, topicName, creds, payload)).ifPresent(v -> commands.put(MQTTS, v));
break;
case COAP:
Optional.ofNullable(getCoapPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(COAP_PROTOCOL, v));
Optional.ofNullable(getCoapsPublishCommand(defaultHostname, creds)).ifPresent(v -> commands.put(COAPS_PROTOCOL, v));
Optional.ofNullable(getCoapPublishCommand(COAP, defaultHostname, creds)).ifPresent(v -> commands.put(COAP, v));
Optional.ofNullable(getCoapPublishCommand(COAPS, defaultHostname, creds)).ifPresent(v -> commands.put(COAPS, v));
break;
default:
commands.put(transportType.name(), NOT_PROVIDED);
commands.put(transportType.name(), CHECK_DOCUMENTATION);
}
return commands;
}
@ -740,119 +743,38 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
return EntityType.DEVICE;
}
private String getHttpPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
DeviceConnectivityInfo httpProps = deviceConnectivityConfiguration.getConnectivity().get(HTTP_PROTOCOL);
if (httpProps != null && httpProps.getEnabled() &&
deviceCredentials.getCredentialsType() == DeviceCredentialsType.ACCESS_TOKEN) {
String hostName = httpProps.getHost().isEmpty() ? defaultHostname : httpProps.getHost();
String port = httpProps.getPort().isEmpty() ? "" : ":" + httpProps.getPort();
return String.format("curl -v -X POST http://%s%s/api/v1/%s/telemetry --header Content-Type:application/json --data " + PAYLOAD,
hostName, port, deviceCredentials.getCredentialsId());
}
return null;
}
private String getHttpsPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
DeviceConnectivityInfo httpsProps = deviceConnectivityConfiguration.getConnectivity().get(HTTPS_PROTOCOL);
if (httpsProps != null && httpsProps.getEnabled() &&
deviceCredentials.getCredentialsType() == DeviceCredentialsType.ACCESS_TOKEN) {
String hostName = httpsProps.getHost().isEmpty() ? defaultHostname : httpsProps.getHost();
String port = httpsProps.getPort().isEmpty() ? "" : ":" + httpsProps.getPort();
return String.format("curl -v -X POST https://%s%s/api/v1/%s/telemetry --header Content-Type:application/json --data " + PAYLOAD,
hostName, port, deviceCredentials.getCredentialsId());
private String getHttpPublishCommand(String protocol, String defaultHostname, DeviceCredentials deviceCredentials) {
DeviceConnectivityInfo httpProps = deviceConnectivityConfiguration.getConnectivity().get(protocol);
if (httpProps == null || !httpProps.getEnabled() ||
deviceCredentials.getCredentialsType() != DeviceCredentialsType.ACCESS_TOKEN) {
return null;
}
return null;
}
private String getMqttPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
return getMqttPublishCommand(defaultHostname,DEFAULT_DEVICE_TELEMETRY_TOPIC, deviceCredentials, " -m " + PAYLOAD);
}
private String getMqttPublishCommand(String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload) {
return getMosquittoPublishCommand(defaultHostname, deviceTelemetryTopic, deviceCredentials, payload, MQTT_PROTOCOL);
String hostName = httpProps.getHost().isEmpty() ? defaultHostname : httpProps.getHost();
String port = httpProps.getPort().isEmpty() ? "" : ":" + httpProps.getPort();
return getCurlCommand(protocol, hostName, port, deviceCredentials);
}
private String getMqttsPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
return getMqttsPublishCommand(defaultHostname, DEFAULT_DEVICE_TELEMETRY_TOPIC, deviceCredentials, " -m " + PAYLOAD);
private String getMqttPublishCommand(String protocol, String defaultHostname, DeviceCredentials deviceCredentials) {
return getMqttPublishCommand(protocol, defaultHostname, DEFAULT_DEVICE_TELEMETRY_TOPIC, deviceCredentials, " -m " + JSON_EXAMPLE_PAYLOAD);
}
private String getMqttsPublishCommand(String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload) {
return getMosquittoPublishCommand(defaultHostname, deviceTelemetryTopic, deviceCredentials, payload, MQTTS_PROTOCOL);
}
private String getMosquittoPublishCommand(String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload, String protocol) {
private String getMqttPublishCommand(String protocol, String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload) {
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
if (properties == null || !properties.getEnabled()) {
return null;
}
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()) {
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;
case X509_CERTIFICATE:
if (MQTTS_PROTOCOL.equals(protocol)) {
return NOT_PROVIDED;
}
default:
return null;
}
command.append(payload);
return command.toString();
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort();
return getMosquittoPublishCommand(protocol, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials, payload);
}
private String getCoapPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
return getCoapClientCommand(defaultHostname, deviceCredentials, COAP_PROTOCOL);
}
private String getCoapsPublishCommand(String defaultHostname, DeviceCredentials deviceCredentials) {
return getCoapClientCommand(defaultHostname, deviceCredentials, COAPS_PROTOCOL);
}
private String getCoapClientCommand(String defaultHostname, DeviceCredentials deviceCredentials, String protocol) {
private String getCoapPublishCommand(String protocol, String defaultHostname, DeviceCredentials deviceCredentials) {
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
if (properties == null || !properties.getEnabled()) {
return null;
}
switch (deviceCredentials.getCredentialsType()) {
case ACCESS_TOKEN:
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:
if (COAPS_PROTOCOL.equals(protocol)) {
return NOT_PROVIDED;
}
default:
return null;
}
String hostName = properties.getHost().isEmpty() ? defaultHostname : properties.getHost();
String port = properties.getPort().isEmpty() ? "" : ":" + properties.getPort();
return getCoapClientCommand(protocol, hostName, port, deviceCredentials);
}
}

77
dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java

@ -0,0 +1,77 @@
package org.thingsboard.server.dao.util;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
import org.thingsboard.server.common.data.security.DeviceCredentials;
public class DeviceConnectivityUtil {
public static final String HTTP = "http";
public static final String HTTPS = "https";
public static final String MQTT = "mqtt";
public static final String MQTTS = "mqtts";
public static final String COAP = "coap";
public static final String COAPS = "coaps";
public static final String CHECK_DOCUMENTATION = "Check documentation";
public static final String JSON_EXAMPLE_PAYLOAD = "\"{temperature:25}\"";
public static String getCurlCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
return String.format("curl -v -X POST %s://%s%s/api/v1/%s/telemetry --header Content-Type:application/json --data " + JSON_EXAMPLE_PAYLOAD,
protocol, host, port, deviceCredentials.getCredentialsId());
}
public static String getMosquittoPublishCommand(String protocol, String host, String port, String deviceTelemetryTopic, DeviceCredentials deviceCredentials, String payload) {
StringBuilder command = new StringBuilder("mosquitto_pub -d -q 1");
if (MQTTS.equals(protocol)) {
command.append(" --cafile tb-server-chain.pem");
}
command.append(" -h ").append(host).append(port == null ? "" : " -p " + port);
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;
case X509_CERTIFICATE:
if (MQTTS.equals(protocol)) {
return CHECK_DOCUMENTATION;
}
default:
return null;
}
command.append(payload);
return command.toString();
}
public static String getCoapClientCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
switch (deviceCredentials.getCredentialsType()) {
case ACCESS_TOKEN:
String client = COAPS.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, host, port, deviceCredentials.getCredentialsId(), JSON_EXAMPLE_PAYLOAD);
case X509_CERTIFICATE:
if (COAPS.equals(protocol)) {
return CHECK_DOCUMENTATION;
}
default:
return null;
}
}
}
Loading…
Cancel
Save