Skip to content

Commit

Permalink
Improve publish coap in case docker is running: validate only default…
Browse files Browse the repository at this point in the history
… port config
  • Loading branch information
AndriiLandiak committed Feb 13, 2024
1 parent a905c1f commit 8efc921
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ private String getHttpPublishCommand(String protocol, String baseUrl, DeviceCred
String port = (propertiesPort.isEmpty() || HTTP_DEFAULT_PORT.equals(propertiesPort) || HTTPS_DEFAULT_PORT.equals(propertiesPort))
? "" : ":" + propertiesPort;
// Edge only:
port = ":" + getPortFromBaseUrl(baseUrl);
Pattern pattern = Pattern.compile("https?://[^:/]+:(\\d+)");
Matcher matcher = pattern.matcher(baseUrl);
if (matcher.find()) {
Expand Down Expand Up @@ -300,6 +301,9 @@ private String getMqttPublishCommand(String baseUrl, String deviceTelemetryTopic
// String mqttPort = getPort(properties);
String mqttHost = getHost(baseUrl, properties, MQTT);
String mqttPort = mqttBindPort.toString();
if (mqttPort.equals("1883") && getPortFromBaseUrl(baseUrl).equals("18080")) {
mqttPort = "11883";
}
return DeviceConnectivityUtil.getMqttPublishCommand(MQTT, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
}

Expand All @@ -325,7 +329,10 @@ private String getDockerMqttPublishCommand(String protocol, String baseUrl, Stri
String mqttHost = getHost(baseUrl, properties, protocol);
// edge-only:
// String mqttPort = getPort(properties);
String mqttPort = mqttsBindPort.toString();
String mqttPort = mqttSslEnabled && MQTTS.equals(protocol) ? mqttsBindPort.toString() : mqttBindPort.toString();
if (mqttPort.equals("1883") && getPortFromBaseUrl(baseUrl).equals("18080")) {
mqttPort = "11883";
}
return DeviceConnectivityUtil.getDockerMqttPublishCommand(protocol, baseUrl, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
}

Expand Down Expand Up @@ -371,7 +378,11 @@ private String getCoapPublishCommand(String protocol, String baseUrl, DeviceCred
String hostName = getHost(baseUrl, properties, protocol);
// edge-only:
// String port = StringUtils.isBlank(properties.getPort()) ? "" : ":" + properties.getPort();
String port = ":" + (coapDtlsEnabled && COAPS.equals(protocol) ? coapsBindPort.toString() : coapBindPort.toString());
String port = coapDtlsEnabled && COAPS.equals(protocol) ? coapsBindPort.toString() : coapBindPort.toString();
if (port.equals("5683") && getPortFromBaseUrl(baseUrl).equals("18080")) {
port = "15683";
}
port = ":" + port;
return DeviceConnectivityUtil.getCoapPublishCommand(protocol, hostName, port, deviceCredentials);
}

Expand All @@ -380,8 +391,22 @@ private String getDockerCoapPublishCommand(String protocol, String baseUrl, Devi
String host = getHost(baseUrl, properties, protocol);
// edge-only:
// String port = StringUtils.isBlank(properties.getPort()) ? "" : ":" + properties.getPort();
String port = ":" + (coapDtlsEnabled && COAPS.equals(protocol) ? coapsBindPort.toString() : coapBindPort.toString());
String port = coapDtlsEnabled && COAPS.equals(protocol) ? coapsBindPort.toString() : coapBindPort.toString();
if (port.equals("5683") && getPortFromBaseUrl(baseUrl).equals("18080")) {
port = "15683";
}
port = ":" + port;
return DeviceConnectivityUtil.getDockerCoapPublishCommand(protocol, host, port, deviceCredentials);
}

//edge-only:
private String getPortFromBaseUrl(String baseUrl) {
Pattern pattern = Pattern.compile("https?://[^:/]+:(\\d+)");
Matcher matcher = pattern.matcher(baseUrl);
if (matcher.find()) {
return matcher.group(1);
}
return "8080";
}

}

0 comments on commit 8efc921

Please sign in to comment.