|
|
|
@ -19,11 +19,13 @@ 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.jetbrains.annotations.NotNull; |
|
|
|
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.DataConstants; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
import org.thingsboard.server.common.data.DeviceTransportType; |
|
|
|
@ -46,10 +48,13 @@ import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.CHECK_DOCUM |
|
|
|
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.DOCKER; |
|
|
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.GATEWAY; |
|
|
|
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.WINDOWS; |
|
|
|
|
|
|
|
@Service("DeviceConnectivityDaoService") |
|
|
|
@Slf4j |
|
|
|
@ -79,6 +84,12 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
DeviceTransportType transportType = deviceProfile.getTransportType(); |
|
|
|
|
|
|
|
ObjectNode commands = JacksonUtil.newObjectNode(); |
|
|
|
if (checkIsGateway(device)) { |
|
|
|
Optional.ofNullable(getMqttTransportPublishCommands(baseUrl, creds, true)) |
|
|
|
.ifPresent(v -> commands.set(MQTT, v)); |
|
|
|
return commands; |
|
|
|
} |
|
|
|
|
|
|
|
switch (transportType) { |
|
|
|
case DEFAULT: |
|
|
|
Optional.ofNullable(getHttpTransportPublishCommands(baseUrl, creds)) |
|
|
|
@ -148,10 +159,18 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
} |
|
|
|
|
|
|
|
private JsonNode getMqttTransportPublishCommands(String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException { |
|
|
|
return getMqttTransportPublishCommands(baseUrl, DEFAULT_DEVICE_TELEMETRY_TOPIC, deviceCredentials); |
|
|
|
return getMqttTransportPublishCommands(baseUrl, deviceCredentials, false); |
|
|
|
} |
|
|
|
|
|
|
|
private JsonNode getMqttTransportPublishCommands(String baseUrl, DeviceCredentials deviceCredentials, boolean isGateway) throws URISyntaxException { |
|
|
|
return getMqttTransportPublishCommands(baseUrl, DEFAULT_DEVICE_TELEMETRY_TOPIC, deviceCredentials, isGateway); |
|
|
|
} |
|
|
|
|
|
|
|
private JsonNode getMqttTransportPublishCommands(String baseUrl, String topic, DeviceCredentials deviceCredentials) throws URISyntaxException { |
|
|
|
return getMqttTransportPublishCommands(baseUrl, topic, deviceCredentials, false); |
|
|
|
} |
|
|
|
|
|
|
|
private JsonNode getMqttTransportPublishCommands(String baseUrl, String topic, DeviceCredentials deviceCredentials, boolean isGateway) throws URISyntaxException { |
|
|
|
ObjectNode mqttCommands = JacksonUtil.newObjectNode(); |
|
|
|
|
|
|
|
if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) { |
|
|
|
@ -160,6 +179,7 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
} |
|
|
|
|
|
|
|
ObjectNode dockerMqttCommands = JacksonUtil.newObjectNode(); |
|
|
|
ObjectNode gatewayDockerMqttCommands = JacksonUtil.newObjectNode(); |
|
|
|
|
|
|
|
if (deviceConnectivityConfiguration.isEnabled(MQTT)) { |
|
|
|
Optional.ofNullable(getMqttPublishCommand(baseUrl, topic, deviceCredentials)). |
|
|
|
@ -167,6 +187,11 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
|
|
|
|
Optional.ofNullable(getDockerMqttPublishCommand(MQTT, baseUrl, topic, deviceCredentials)) |
|
|
|
.ifPresent(v -> dockerMqttCommands.put(MQTT, v)); |
|
|
|
|
|
|
|
if (isGateway) { |
|
|
|
Optional.ofNullable(getGatewayDockerCommands(baseUrl, deviceCredentials, MQTT)) |
|
|
|
.ifPresent(v -> gatewayDockerMqttCommands.set(MQTT, v)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (deviceConnectivityConfiguration.isEnabled(MQTTS)) { |
|
|
|
@ -178,11 +203,21 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
|
|
|
|
Optional.ofNullable(getDockerMqttPublishCommand(MQTTS, baseUrl, topic, deviceCredentials)) |
|
|
|
.ifPresent(v -> dockerMqttCommands.put(MQTTS, v)); |
|
|
|
|
|
|
|
if (isGateway) { |
|
|
|
Optional.ofNullable(getGatewayDockerCommands(baseUrl, deviceCredentials, MQTTS)) |
|
|
|
.ifPresent(v -> gatewayDockerMqttCommands.set(MQTTS, v)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!dockerMqttCommands.isEmpty()) { |
|
|
|
mqttCommands.set(DOCKER, dockerMqttCommands); |
|
|
|
} |
|
|
|
|
|
|
|
if (!gatewayDockerMqttCommands.isEmpty()) { |
|
|
|
mqttCommands.set(GATEWAY, gatewayDockerMqttCommands); |
|
|
|
} |
|
|
|
|
|
|
|
return mqttCommands.isEmpty() ? null : mqttCommands; |
|
|
|
} |
|
|
|
|
|
|
|
@ -208,6 +243,18 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private JsonNode getGatewayDockerCommands(String baseUrl, DeviceCredentials deviceCredentials, String mqttType) throws URISyntaxException { |
|
|
|
ObjectNode dockerLaunchCommands = JacksonUtil.newObjectNode(); |
|
|
|
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(mqttType); |
|
|
|
String mqttHost = getHost(baseUrl, properties); |
|
|
|
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort(); |
|
|
|
Optional.ofNullable(DeviceConnectivityUtil.getGatewayLaunchCommand(LINUX, mqttHost, mqttPort, deviceCredentials)) |
|
|
|
.ifPresent(v -> dockerLaunchCommands.put(LINUX, v)); |
|
|
|
Optional.ofNullable(DeviceConnectivityUtil.getGatewayLaunchCommand(WINDOWS, mqttHost, mqttPort, deviceCredentials)) |
|
|
|
.ifPresent(v -> dockerLaunchCommands.put(WINDOWS, v)); |
|
|
|
return dockerLaunchCommands.isEmpty() ? null : dockerLaunchCommands; |
|
|
|
} |
|
|
|
|
|
|
|
private String getDockerMqttPublishCommand(String protocol, String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException { |
|
|
|
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol); |
|
|
|
String mqttHost = getHost(baseUrl, properties); |
|
|
|
@ -265,4 +312,9 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService |
|
|
|
private String getHost(String baseUrl, DeviceConnectivityInfo properties) throws URISyntaxException { |
|
|
|
return properties.getHost().isEmpty() ? new URI(baseUrl).getHost() : properties.getHost(); |
|
|
|
} |
|
|
|
|
|
|
|
private static boolean checkIsGateway(Device device) { |
|
|
|
return device.getAdditionalInfo().has(DataConstants.GATEWAY_PARAMETER) && |
|
|
|
device.getAdditionalInfo().get(DataConstants.GATEWAY_PARAMETER).asBoolean(); |
|
|
|
} |
|
|
|
} |
|
|
|
|