From 478b2843776d168d05dae78a582e33a0016b05a5 Mon Sep 17 00:00:00 2001 From: sanshengshui Date: Sun, 15 Oct 2023 23:58:11 +0800 Subject: [PATCH] :bug: the mqtt connection configuration is lost --- .../client/toolkit/common/utils/FileUtils.java | 10 +++++++++- .../client/toolkit/mqtt/service/MqttBizService.java | 2 +- .../toolkit/mqtt/service/MqttSettingService.java | 2 +- .../toolkit/mqtt/service/MqttShellModeService.java | 3 ++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/toolkit-common/src/main/java/iot/technology/client/toolkit/common/utils/FileUtils.java b/toolkit-common/src/main/java/iot/technology/client/toolkit/common/utils/FileUtils.java index 7446b99a..5d43b3c4 100644 --- a/toolkit-common/src/main/java/iot/technology/client/toolkit/common/utils/FileUtils.java +++ b/toolkit-common/src/main/java/iot/technology/client/toolkit/common/utils/FileUtils.java @@ -75,7 +75,15 @@ public static boolean notExistOrContents(String fileName) { public static void updateAllFileContents(String fileName, List bodyList) { try { - Files.write(Paths.get(fileName), bodyList, StandardCharsets.UTF_8); + StringBuilder content = new StringBuilder(); + if (!bodyList.isEmpty()) { + bodyList.forEach(body -> { + content.append(body).append(System.lineSeparator()); + }); + } + //overlay update + Files.write(Path.of(fileName), content.toString().getBytes()); + } catch (IOException e) { System.out.format("fileName updateAllFileContents failed!!!"); } diff --git a/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttBizService.java b/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttBizService.java index ccf954bf..051acda2 100644 --- a/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttBizService.java +++ b/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttBizService.java @@ -146,7 +146,7 @@ public boolean mqttProcessorAfterLogic(String code, MqttConfigSettingsDomain dom domain.setClient(mqttClientService); //write settings to file MqttSettings settings = domain.convertMqttSettingsJsonString(); - settings.setUsage(0); + settings.setUsage(1); FileUtils.writeDataToFile(SystemConfigConst.MQTT_SETTINGS_FILE_NAME, JsonUtils.object2Json(settings)); // add settings config, not enter into sub terminal diff --git a/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttSettingService.java b/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttSettingService.java index 10844cdd..ef3e9899 100644 --- a/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttSettingService.java +++ b/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttSettingService.java @@ -39,7 +39,7 @@ public void updateAllMqttConfigsByUsage(MqttSettings waitUpdateSettings, Integer waitUpdateSettings.setSettingTime(System.currentTimeMillis()); } settingsList.add(waitUpdateSettings); - settingsList.stream().forEach(st -> { + settingsList.forEach(st -> { if (st.getSettingTime() == null) { st.setSettingTime(System.currentTimeMillis()); } diff --git a/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttShellModeService.java b/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttShellModeService.java index 9e8af1d2..d0ea3efb 100644 --- a/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttShellModeService.java +++ b/toolkit-mqtt/src/main/java/iot/technology/client/toolkit/mqtt/service/MqttShellModeService.java @@ -92,8 +92,8 @@ public boolean call(MqttShellModeDomain domain, Terminal terminal) { MqttClientCallback clientCallback = new MqttClientCallback() { @Override public void connectionLost(DisReason reason) { - mqttSettingService.updateAllMqttConfigsByUsage(domain.getSettings(), 0); if (reason.getActionType().equals(0)) { + mqttSettingService.updateAllMqttConfigsByUsage(domain.getSettings(), 0); StringBuilder sb = new StringBuilder(); sb.append(StringUtils.lineSeparator); sb.append("Server Unavailable" + String.format(EmojiEnum.disconnectEmoji)); @@ -110,6 +110,7 @@ public void onSuccessfulReconnect() { String data = reader.readLine(prompt); context.setData(data); if (data.equals("exit")) { + isEnd = false; mqttSettingService.updateAllMqttConfigsByUsage(domain.getSettings(), 0); domain.getClient().disconnect(1); return false;