Skip to content

Commit

Permalink
Merge pull request #176 from thingsboard/bug/ws-client-settings-update
Browse files Browse the repository at this point in the history
Fix WS client admin settings with missing parameter
  • Loading branch information
dmytro-landiak authored Nov 8, 2024
2 parents 91e7ab0 + 33f9b12 commit 5cdd8a4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void performInstall() {
case "2.0.0":
log.info("Upgrading TBMQ from version 2.0.0 to 2.0.1 ...");
databaseEntitiesUpgradeService.upgradeDatabase("2.0.0");
dataUpdateService.updateData("2.0.0");
break;
default:
throw new RuntimeException("Unable to upgrade TBMQ, unsupported fromVersion: " + upgradeFromVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public class WebSocketClientSettings {
public static AdminSettings createWsClientSettings() {
AdminSettings wsClientSettings = new AdminSettings();
wsClientSettings.setKey(BrokerConstants.WEBSOCKET_KEY);
wsClientSettings.setJsonValue(createWsClientJsonValue());
return wsClientSettings;
}

public static ObjectNode createWsClientJsonValue() {
ObjectNode objectNode = JacksonUtil.newObjectNode();
objectNode.put("isLoggingEnabled", false);
objectNode.put("maxMessages", 1000);
wsClientSettings.setJsonValue(objectNode);

return wsClientSettings;
return objectNode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.thingsboard.mqtt.broker.common.data.AdminSettings;
import org.thingsboard.mqtt.broker.common.data.BrokerConstants;
import org.thingsboard.mqtt.broker.common.data.security.ClientCredentialsType;
import org.thingsboard.mqtt.broker.common.data.security.MqttClientCredentials;
Expand Down Expand Up @@ -56,6 +57,10 @@ public void updateData(String fromVersion) throws Exception {
log.info("Updating data from version 1.4.0 to 2.0.0 ...");
createWsClientSettings();
break;
case "2.0.0":
log.info("Updating data from version 2.0.0 to 2.0.1 ...");
updateWsClientSettings();
break;
default:
throw new RuntimeException("Unable to update data, unsupported fromVersion: " + fromVersion);
}
Expand Down Expand Up @@ -100,19 +105,53 @@ String convertSslMqttClientCredentialsForVersion140(String oldCredentialsValueSt
}

private void createConnectivitySettings() {
if (adminSettingsService.findAdminSettingsByKey(BrokerConstants.CONNECTIVITY_KEY) == null) {
if (getAdminSettingsByKey(BrokerConstants.CONNECTIVITY_KEY) == null) {
log.info("Creating connectivity settings ...");
adminSettingsService.saveAdminSettings(ConnectivitySettings.createConnectivitySettings());
log.info("Connectivity settings created!");
}
}

private void createWsClientSettings() {
if (adminSettingsService.findAdminSettingsByKey(BrokerConstants.WEBSOCKET_KEY) == null) {
log.info("Creating WebSocket client settings ...");
adminSettingsService.saveAdminSettings(WebSocketClientSettings.createWsClientSettings());
log.info("WebSocket client settings created!");
if (getAdminSettingsByKey(BrokerConstants.WEBSOCKET_KEY) == null) {
saveWsClientSettings();
}
}

private void saveWsClientSettings() {
log.info("Creating WebSocket client settings ...");
adminSettingsService.saveAdminSettings(WebSocketClientSettings.createWsClientSettings());
log.info("WebSocket client settings created!");
}

private void updateWsClientSettings() {
AdminSettings wsSettings = getAdminSettingsByKey(BrokerConstants.WEBSOCKET_KEY);
if (wsSettings == null) {
saveWsClientSettings();
return;
}
JsonNode jsonValue = wsSettings.getJsonValue();
if (jsonValue == null) {
log.info("Creating correct JSON value for WebSocket client settings ...");
wsSettings.setJsonValue(WebSocketClientSettings.createWsClientJsonValue());
adminSettingsService.saveAdminSettings(wsSettings);
log.info("WebSocket client settings updated with correct JSON value!");
return;
}
JsonNode maxMessages = jsonValue.get("maxMessages");
if (maxMessages == null || maxMessages.isNull()) {
log.info("Setting 'maxMessages' value for WebSocket client settings ...");

ObjectNode objectNode = (ObjectNode) jsonValue;
objectNode.put("maxMessages", 1000);

wsSettings.setJsonValue(objectNode);
adminSettingsService.saveAdminSettings(wsSettings);
log.info("WebSocket client settings updated with 'maxMessages' value!");
}
}

private AdminSettings getAdminSettingsByKey(String key) {
return adminSettingsService.findAdminSettingsByKey(key);
}
}

0 comments on commit 5cdd8a4

Please sign in to comment.