added docker coap commands and refactoring
This commit is contained in:
parent
bab3eef8d7
commit
3c6c565732
@ -164,7 +164,6 @@ import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@ -760,10 +759,6 @@ public abstract class BaseController {
|
||||
return checkEntityId(resourceId, resourceService::findResourceInfoById, operation);
|
||||
}
|
||||
|
||||
String checkSslServerPemFile(String protocol) throws ThingsboardException, IOException {
|
||||
return checkNotNull(deviceConnectivityService.getSslServerChain(protocol), "Mqtt ssl server chain pem file is not found");
|
||||
}
|
||||
|
||||
OtaPackage checkOtaPackageId(OtaPackageId otaPackageId, Operation operation) throws ThingsboardException {
|
||||
return checkEntityId(otaPackageId, otaPackageService::findOtaPackageById, operation);
|
||||
}
|
||||
|
||||
@ -22,7 +22,6 @@ import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -49,7 +48,7 @@ import static org.thingsboard.server.controller.ControllerConstants.PROTOCOL;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PROTOCOL_PARAM_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH;
|
||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT_SSL_PEM_FILE_NAME;
|
||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.PEM_CERT_FILE_NAME;
|
||||
|
||||
@RestController
|
||||
@TbCoreComponent
|
||||
@ -70,8 +69,8 @@ public class DeviceConnectivityController extends BaseController {
|
||||
examples = @io.swagger.annotations.Example(
|
||||
value = {
|
||||
@io.swagger.annotations.ExampleProperty(
|
||||
mediaType="application/json",
|
||||
value="{\"http\":\"curl -v -X POST http://localhost:8080/api/v1/0ySs4FTOn5WU15XLmal8/telemetry --header Content-Type:application/json --data {temperature:25}\"," +
|
||||
mediaType = "application/json",
|
||||
value = "{\"http\":\"curl -v -X POST http://localhost:8080/api/v1/0ySs4FTOn5WU15XLmal8/telemetry --header Content-Type:application/json --data {temperature:25}\"," +
|
||||
"\"mqtt\":\"mosquitto_pub -d -q 1 -h localhost -t v1/devices/me/telemetry -i myClient1 -u myUsername1 -P myPassword -m {temperature:25}\"," +
|
||||
"\"coap\":\"coap-client -m POST coap://localhost:5683/api/v1/0ySs4FTOn5WU15XLmal8/telemetry -t json -e {temperature:25}\"}")}))})
|
||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@ -92,15 +91,16 @@ public class DeviceConnectivityController extends BaseController {
|
||||
@ResponseBody
|
||||
public ResponseEntity<org.springframework.core.io.Resource> downloadMqttServerCertificate(@ApiParam(value = PROTOCOL_PARAM_DESCRIPTION)
|
||||
@PathVariable(PROTOCOL) String protocol) throws ThingsboardException, IOException {
|
||||
String certificate = checkSslServerPemFile(protocol);
|
||||
checkParameter(PROTOCOL, protocol);
|
||||
var pemCert =
|
||||
checkNotNull(deviceConnectivityService.getPemCertFile(protocol), protocol + " pem cert file is not found!");
|
||||
|
||||
ByteArrayResource cert = new ByteArrayResource(certificate.getBytes());
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + MQTT_SSL_PEM_FILE_NAME)
|
||||
.header("x-filename", MQTT_SSL_PEM_FILE_NAME)
|
||||
.contentLength(cert.contentLength())
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + PEM_CERT_FILE_NAME)
|
||||
.header("x-filename", PEM_CERT_FILE_NAME)
|
||||
.contentLength(pemCert.contentLength())
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(cert);
|
||||
.body(pemCert);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1004,7 +1004,7 @@ device:
|
||||
enabled: "${DEVICE_CONNECTIVITY_MQTTS_ENABLED:false}"
|
||||
host: "${DEVICE_CONNECTIVITY_MQTTS_HOST:}"
|
||||
port: "${DEVICE_CONNECTIVITY_MQTTS_PORT:8883}"
|
||||
ssl_server_pem_path: "${DEVICE_CONNECTIVITY_MQTTS_SERVER_CHAIN_PATH:}"
|
||||
pem_cert_file: "${DEVICE_CONNECTIVITY_MQTT_SSL_PEM_CERT:mqttserver.pem}"
|
||||
coap:
|
||||
enabled: "${DEVICE_CONNECTIVITY_COAP_ENABLED:true}"
|
||||
host: "${DEVICE_CONNECTIVITY_COAP_HOST:}"
|
||||
|
||||
@ -16,14 +16,14 @@
|
||||
package org.thingsboard.server.dao.device;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public interface DeviceConnectivityService {
|
||||
|
||||
JsonNode findDevicePublishTelemetryCommands(String baseUrl, Device device) throws URISyntaxException;
|
||||
|
||||
String getSslServerChain(String protocol) throws IOException;
|
||||
Resource getPemCertFile(String protocol);
|
||||
}
|
||||
|
||||
@ -26,4 +26,9 @@ import java.util.Map;
|
||||
@Data
|
||||
public class DeviceConnectivityConfiguration {
|
||||
private Map<String, DeviceConnectivityInfo> connectivity;
|
||||
|
||||
public boolean isEnabled(String protocol) {
|
||||
var info = connectivity.get(protocol);
|
||||
return info != null && info.isEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,8 +19,8 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DeviceConnectivityInfo {
|
||||
private Boolean enabled;
|
||||
private boolean enabled;
|
||||
private String host;
|
||||
private String port;
|
||||
private String sslServerPemPath;
|
||||
private String pemCertFile;
|
||||
}
|
||||
|
||||
@ -19,26 +19,25 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.DeviceProfile;
|
||||
import org.thingsboard.server.common.data.DeviceTransportType;
|
||||
import org.thingsboard.server.common.data.ResourceUtils;
|
||||
import org.thingsboard.server.common.data.StringUtils;
|
||||
import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
||||
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
|
||||
import org.thingsboard.server.dao.util.DeviceConnectivityUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@ -49,17 +48,12 @@ import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAPS;
|
||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.DOCKER;
|
||||
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.LINUX;
|
||||
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.getCoapClientCommand;
|
||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.getCurlCommand;
|
||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.getDockerMosquittoClientsPublishCommand;
|
||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.getMosquittoPubPublishCommand;
|
||||
|
||||
@Service("DeviceConnectivityDaoService")
|
||||
@Slf4j
|
||||
public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService {
|
||||
public class DeviceConnectivityServiceImpl implements DeviceConnectivityService {
|
||||
|
||||
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
|
||||
public static final String INCORRECT_DEVICE_ID = "Incorrect deviceId ";
|
||||
@ -113,12 +107,13 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSslServerChain(String protocol) throws IOException {
|
||||
String mqttSslPemPath = deviceConnectivityConfiguration.getConnectivity()
|
||||
public Resource getPemCertFile(String protocol) {
|
||||
String certFilePath = deviceConnectivityConfiguration.getConnectivity()
|
||||
.get(protocol)
|
||||
.getSslServerPemPath();
|
||||
if (!mqttSslPemPath.isEmpty() && ResourceUtils.resourceExists(this, mqttSslPemPath)) {
|
||||
return FileUtils.readFileToString(new File(mqttSslPemPath), StandardCharsets.UTF_8);
|
||||
.getPemCertFile();
|
||||
|
||||
if (StringUtils.isNotBlank(certFilePath) && ResourceUtils.resourceExists(this, certFilePath)) {
|
||||
return new ClassPathResource(certFilePath);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -134,15 +129,15 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
|
||||
}
|
||||
|
||||
private String getHttpPublishCommand(String protocol, String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
DeviceConnectivityInfo httpProps = deviceConnectivityConfiguration.getConnectivity().get(protocol);
|
||||
if (httpProps == null || !httpProps.getEnabled() ||
|
||||
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
|
||||
if (properties == null || !properties.isEnabled() ||
|
||||
deviceCredentials.getCredentialsType() != DeviceCredentialsType.ACCESS_TOKEN) {
|
||||
return null;
|
||||
}
|
||||
String hostName = httpProps.getHost().isEmpty() ? new URI(baseUrl).getHost() : httpProps.getHost();
|
||||
String port = httpProps.getPort().isEmpty() ? "" : ":" + httpProps.getPort();
|
||||
String hostName = getHost(baseUrl, properties);
|
||||
String port = properties.getPort().isEmpty() ? "" : ":" + properties.getPort();
|
||||
|
||||
return getCurlCommand(protocol, hostName, port, deviceCredentials);
|
||||
return DeviceConnectivityUtil.getHttpPublishCommand(protocol, hostName, port, deviceCredentials);
|
||||
}
|
||||
|
||||
private JsonNode getMqttTransportPublishCommands(String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
@ -152,23 +147,31 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
|
||||
private JsonNode getMqttTransportPublishCommands(String baseUrl, String topic, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
ObjectNode mqttCommands = JacksonUtil.newObjectNode();
|
||||
|
||||
Optional.ofNullable(getMqttPublishCommand(baseUrl, topic, deviceCredentials))
|
||||
.ifPresent(v -> mqttCommands.put(MQTT, v));
|
||||
List<String> mqttsPublishCommand = getMqttsPublishCommand(baseUrl, topic, deviceCredentials);
|
||||
if (mqttsPublishCommand != null){
|
||||
if (mqttsPublishCommand.size() > 1) {
|
||||
ArrayNode arrayNode = mqttCommands.putArray(MQTTS);
|
||||
mqttsPublishCommand.forEach(arrayNode::add);
|
||||
} else {
|
||||
mqttCommands.put(MQTTS, mqttsPublishCommand.get(0));
|
||||
}
|
||||
if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) {
|
||||
mqttCommands.put(MQTTS, CHECK_DOCUMENTATION);
|
||||
return mqttCommands;
|
||||
}
|
||||
|
||||
ObjectNode dockerMqttCommands = JacksonUtil.newObjectNode();
|
||||
Optional.ofNullable(getDockerMqttPublishCommand(MQTT,baseUrl, topic, deviceCredentials))
|
||||
|
||||
if (deviceConnectivityConfiguration.isEnabled(MQTT)) {
|
||||
Optional.ofNullable(getMqttPublishCommand(baseUrl, topic, deviceCredentials)).
|
||||
ifPresent(v -> mqttCommands.put(MQTT, v));
|
||||
|
||||
Optional.ofNullable(getDockerMqttPublishCommand(MQTT, baseUrl, topic, deviceCredentials))
|
||||
.ifPresent(v -> dockerMqttCommands.put(MQTT, v));
|
||||
}
|
||||
|
||||
if (deviceConnectivityConfiguration.isEnabled(MQTTS)) {
|
||||
List<String> mqttsPublishCommand = getMqttsPublishCommand(baseUrl, topic, deviceCredentials);
|
||||
if (mqttsPublishCommand != null) {
|
||||
ArrayNode arrayNode = mqttCommands.putArray(MQTTS);
|
||||
mqttsPublishCommand.forEach(arrayNode::add);
|
||||
}
|
||||
|
||||
Optional.ofNullable(getDockerMqttPublishCommand(MQTTS, baseUrl, topic, deviceCredentials))
|
||||
.ifPresent(v -> dockerMqttCommands.put(MQTTS, v));
|
||||
}
|
||||
|
||||
if (!dockerMqttCommands.isEmpty()) {
|
||||
mqttCommands.set(DOCKER, dockerMqttCommands);
|
||||
@ -178,70 +181,81 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
|
||||
|
||||
private String getMqttPublishCommand(String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(MQTT);
|
||||
if (properties == null || !properties.getEnabled()) {
|
||||
return null;
|
||||
}
|
||||
String mqttHost = properties.getHost().isEmpty() ? new URI(baseUrl).getHost() : properties.getHost();
|
||||
String mqttHost = getHost(baseUrl, properties);
|
||||
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort();
|
||||
return getMosquittoPubPublishCommand(MQTT, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
|
||||
return DeviceConnectivityUtil.getMqttPublishCommand(MQTT, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
|
||||
}
|
||||
|
||||
private List<String> getMqttsPublishCommand(String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
String pubCommand;
|
||||
if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) {
|
||||
return List.of(CHECK_DOCUMENTATION);
|
||||
} else {
|
||||
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(MQTTS);
|
||||
if (properties == null || !properties.getEnabled()) {
|
||||
return null;
|
||||
}
|
||||
String mqttHost = properties.getHost().isEmpty() ? new URI(baseUrl).getHost() : properties.getHost();
|
||||
String mqttHost = getHost(baseUrl, properties);
|
||||
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort();
|
||||
pubCommand = getMosquittoPubPublishCommand(MQTTS, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
|
||||
}
|
||||
String pubCommand = DeviceConnectivityUtil.getMqttPublishCommand(MQTTS, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
|
||||
|
||||
ArrayList<String> commands = new ArrayList<>();
|
||||
if (pubCommand != null) {
|
||||
commands.add("curl " + baseUrl + "/api/device-connectivity/mqtts/certificate/download -o /tmp/tb-server-chain.pem");
|
||||
commands.add(DeviceConnectivityUtil.getCurlPemCertCommand(baseUrl, MQTTS));
|
||||
commands.add(pubCommand);
|
||||
return commands;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private String getDockerMqttPublishCommand(String protocol, String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
|
||||
if (properties == null || !properties.getEnabled()) {
|
||||
return null;
|
||||
}
|
||||
String mqttHost = properties.getHost().isEmpty() ? new URI(baseUrl).getHost() : properties.getHost();
|
||||
String mqttHost = getHost(baseUrl, properties);
|
||||
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort();
|
||||
return getDockerMosquittoClientsPublishCommand(protocol, baseUrl, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
|
||||
return DeviceConnectivityUtil.getDockerMqttPublishCommand(protocol, baseUrl, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
|
||||
}
|
||||
|
||||
private JsonNode getCoapTransportPublishCommands(String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
ObjectNode coapCommands = JacksonUtil.newObjectNode();
|
||||
|
||||
if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) {
|
||||
coapCommands.put(COAPS, CHECK_DOCUMENTATION);
|
||||
return coapCommands;
|
||||
}
|
||||
|
||||
ObjectNode dockerCoapCommands = JacksonUtil.newObjectNode();
|
||||
|
||||
if (deviceConnectivityConfiguration.isEnabled(COAP)) {
|
||||
Optional.ofNullable(getCoapPublishCommand(COAP, baseUrl, deviceCredentials))
|
||||
.ifPresent(v -> coapCommands.put(COAP, v));
|
||||
|
||||
Optional.ofNullable(getDockerCoapPublishCommand(COAP, baseUrl, deviceCredentials))
|
||||
.ifPresent(v -> dockerCoapCommands.put(COAP, v));
|
||||
}
|
||||
|
||||
if (deviceConnectivityConfiguration.isEnabled(COAPS)) {
|
||||
Optional.ofNullable(getCoapPublishCommand(COAPS, baseUrl, deviceCredentials))
|
||||
.ifPresent(v -> coapCommands.put(COAPS, v));
|
||||
|
||||
Optional.ofNullable(getDockerCoapPublishCommand(COAPS, baseUrl, deviceCredentials))
|
||||
.ifPresent(v -> dockerCoapCommands.put(COAPS, v));
|
||||
}
|
||||
|
||||
if (!dockerCoapCommands.isEmpty()) {
|
||||
coapCommands.set(DOCKER, dockerCoapCommands);
|
||||
}
|
||||
|
||||
return coapCommands.isEmpty() ? null : coapCommands;
|
||||
}
|
||||
|
||||
private String getCoapPublishCommand(String protocol, String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
if (COAPS.equals(protocol) && deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) {
|
||||
return CHECK_DOCUMENTATION;
|
||||
}
|
||||
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
|
||||
if (properties == null || !properties.getEnabled()) {
|
||||
return null;
|
||||
}
|
||||
String hostName = properties.getHost().isEmpty() ? new URI(baseUrl).getHost() : properties.getHost();
|
||||
String hostName = getHost(baseUrl, properties);
|
||||
String port = properties.getPort().isEmpty() ? "" : ":" + properties.getPort();
|
||||
return DeviceConnectivityUtil.getCoapPublishCommand(protocol, hostName, port, deviceCredentials);
|
||||
}
|
||||
|
||||
return getCoapClientCommand(protocol, hostName, port, deviceCredentials);
|
||||
private String getDockerCoapPublishCommand(String protocol, String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException {
|
||||
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
|
||||
String host = getHost(baseUrl, properties);
|
||||
String port = properties.getPort().isEmpty() ? "" : ":" + properties.getPort();
|
||||
return DeviceConnectivityUtil.getDockerCoapPublishCommand(protocol, host, port, deviceCredentials);
|
||||
}
|
||||
|
||||
private String getHost(String baseUrl, DeviceConnectivityInfo properties) throws URISyntaxException {
|
||||
return properties.getHost().isEmpty() ? new URI(baseUrl).getHost() : properties.getHost();
|
||||
}
|
||||
}
|
||||
@ -30,19 +30,22 @@ public class DeviceConnectivityUtil {
|
||||
public static final String MQTTS = "mqtts";
|
||||
public static final String COAP = "coap";
|
||||
public static final String COAPS = "coaps";
|
||||
public static final String MQTT_SSL_PEM_FILE_NAME = "tb-server-chain.pem";
|
||||
public static final String PEM_CERT_FILE_NAME = "tb-server-chain.pem";
|
||||
public static final String CHECK_DOCUMENTATION = "Check documentation";
|
||||
public static final String JSON_EXAMPLE_PAYLOAD = "\"{temperature:25}\"";
|
||||
public static final String DOCKER_RUN = "docker run --rm -it ";
|
||||
public static final String MQTT_IMAGE = "thingsboard/mosquitto-clients ";
|
||||
public static final String COAP_IMAGE = "thingsboard/coap-clients ";
|
||||
|
||||
public static String getCurlCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
||||
public static String getHttpPublishCommand(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 getMosquittoPubPublishCommand(String protocol, String host, String port, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
|
||||
public static String getMqttPublishCommand(String protocol, String host, String port, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
|
||||
StringBuilder command = new StringBuilder("mosquitto_pub -d -q 1");
|
||||
if (MQTTS.equals(protocol)) {
|
||||
command.append(" --cafile tmp/" + MQTT_SSL_PEM_FILE_NAME);
|
||||
command.append(" --cafile ").append(PEM_CERT_FILE_NAME);
|
||||
}
|
||||
command.append(" -h ").append(host).append(port == null ? "" : " -p " + port);
|
||||
command.append(" -t ").append(deviceTelemetryTopic);
|
||||
@ -75,50 +78,34 @@ public class DeviceConnectivityUtil {
|
||||
return command.toString();
|
||||
}
|
||||
|
||||
public static String getDockerMosquittoClientsPublishCommand(String protocol, String baseUrl, String host, String port, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
|
||||
StringBuilder command = new StringBuilder("docker run -it --rm thingsboard/mosquitto-clients ");
|
||||
if (MQTTS.equals(protocol)) {
|
||||
command.append("/bin/sh -c \"curl -o /tmp/tb-server-chain.pem ").append(baseUrl).append("/api/device-connectivity/mqtts/certificate/download && ");
|
||||
}
|
||||
command.append("pub");
|
||||
if (MQTTS.equals(protocol)) {
|
||||
command.append(" --cafile tmp/" + MQTT_SSL_PEM_FILE_NAME);
|
||||
}
|
||||
command.append(" -h ").append(host).append(port == null ? "" : " -p " + port);
|
||||
command.append(" -t ").append(deviceTelemetryTopic);
|
||||
public static String getDockerMqttPublishCommand(String protocol, String baseUrl, String host, String port, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
|
||||
String mqttCommand = getMqttPublishCommand(protocol, host, port, deviceTelemetryTopic, deviceCredentials);
|
||||
|
||||
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());
|
||||
if (mqttCommand == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder mqttDockerCommand = new StringBuilder();
|
||||
mqttDockerCommand.append(DOCKER_RUN).append(MQTT_IMAGE);
|
||||
|
||||
if (MQTTS.equals(protocol)) {
|
||||
mqttDockerCommand.append("/bin/sh -c \"")
|
||||
.append(getCurlPemCertCommand(baseUrl, protocol))
|
||||
.append(" && ")
|
||||
.append(mqttCommand)
|
||||
.append("\"");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
command.append(" -m " + JSON_EXAMPLE_PAYLOAD);
|
||||
if (MQTTS.equals(protocol)) {
|
||||
command.append("\"");
|
||||
}
|
||||
return command.toString();
|
||||
mqttDockerCommand.append(mqttCommand);
|
||||
}
|
||||
|
||||
public static String getCoapClientCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
||||
return mqttDockerCommand.toString();
|
||||
}
|
||||
|
||||
public static String getCurlPemCertCommand(String baseUrl, String protocol) {
|
||||
return String.format("curl -f -S -o %s %s/api/device-connectivity/%s/certificate/download", PEM_CERT_FILE_NAME, baseUrl, protocol);
|
||||
}
|
||||
|
||||
public static String getCoapPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
||||
switch (deviceCredentials.getCredentialsType()) {
|
||||
case ACCESS_TOKEN:
|
||||
String client = COAPS.equals(protocol) ? "coap-client-openssl" : "coap-client";
|
||||
@ -128,4 +115,9 @@ public class DeviceConnectivityUtil {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDockerCoapPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
||||
String coapCommand = getCoapPublishCommand(protocol, host, port, deviceCredentials);
|
||||
return coapCommand != null ? String.format("%s%s%s", DOCKER_RUN, COAP_IMAGE, coapCommand) : null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user