mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
Change MqttClient without restart, use secureClient for port >8800
This commit is contained in:
@@ -1,17 +1,12 @@
|
|||||||
import CancelIcon from '@mui/icons-material/Cancel';
|
import CancelIcon from '@mui/icons-material/Cancel';
|
||||||
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
|
|
||||||
import WarningIcon from '@mui/icons-material/Warning';
|
import WarningIcon from '@mui/icons-material/Warning';
|
||||||
import { Button, Checkbox, MenuItem, Grid, Typography, InputAdornment, TextField } from '@mui/material';
|
import { Button, Checkbox, MenuItem, Grid, Typography, InputAdornment, TextField } from '@mui/material';
|
||||||
import { useRequest } from 'alova';
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { toast } from 'react-toastify';
|
|
||||||
import RestartMonitor from '../system/RestartMonitor';
|
|
||||||
import type { ValidateFieldsError } from 'async-validator';
|
import type { ValidateFieldsError } from 'async-validator';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
|
|
||||||
import type { MqttSettings } from 'types';
|
import type { MqttSettings } from 'types';
|
||||||
import * as MqttApi from 'api/mqtt';
|
import * as MqttApi from 'api/mqtt';
|
||||||
import * as SystemApi from 'api/system';
|
|
||||||
import {
|
import {
|
||||||
BlockFormControlLabel,
|
BlockFormControlLabel,
|
||||||
ButtonRow,
|
ButtonRow,
|
||||||
@@ -19,7 +14,6 @@ import {
|
|||||||
SectionContent,
|
SectionContent,
|
||||||
ValidatedPasswordField,
|
ValidatedPasswordField,
|
||||||
ValidatedTextField,
|
ValidatedTextField,
|
||||||
MessageBox,
|
|
||||||
BlockNavigation
|
BlockNavigation
|
||||||
} from 'components';
|
} from 'components';
|
||||||
import { useI18nContext } from 'i18n/i18n-react';
|
import { useI18nContext } from 'i18n/i18n-react';
|
||||||
@@ -38,21 +32,15 @@ const MqttSettingsForm: FC = () => {
|
|||||||
setDirtyFlags,
|
setDirtyFlags,
|
||||||
blocker,
|
blocker,
|
||||||
saveData,
|
saveData,
|
||||||
errorMessage,
|
errorMessage
|
||||||
restartNeeded
|
|
||||||
} = useRest<MqttSettings>({
|
} = useRest<MqttSettings>({
|
||||||
read: MqttApi.readMqttSettings,
|
read: MqttApi.readMqttSettings,
|
||||||
update: MqttApi.updateMqttSettings
|
update: MqttApi.updateMqttSettings
|
||||||
});
|
});
|
||||||
|
|
||||||
const { send: restartCommand } = useRequest(SystemApi.restart(), {
|
|
||||||
immediate: false
|
|
||||||
});
|
|
||||||
|
|
||||||
const { LL } = useI18nContext();
|
const { LL } = useI18nContext();
|
||||||
|
|
||||||
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
||||||
const [restarting, setRestarting] = useState(false);
|
|
||||||
|
|
||||||
const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, updateDataValue);
|
const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, updateDataValue);
|
||||||
|
|
||||||
@@ -71,13 +59,6 @@ const MqttSettingsForm: FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const restart = async () => {
|
|
||||||
await restartCommand().catch((error) => {
|
|
||||||
toast.error(error.message);
|
|
||||||
});
|
|
||||||
setRestarting(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<BlockFormControlLabel
|
<BlockFormControlLabel
|
||||||
@@ -432,15 +413,7 @@ const MqttSettingsForm: FC = () => {
|
|||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
{restartNeeded && (
|
{dirtyFlags && dirtyFlags.length !== 0 && (
|
||||||
<MessageBox my={2} level="warning" message={LL.RESTART_TEXT()}>
|
|
||||||
<Button startIcon={<PowerSettingsNewIcon />} variant="contained" color="error" onClick={restart}>
|
|
||||||
{LL.RESTART()}
|
|
||||||
</Button>
|
|
||||||
</MessageBox>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{!restartNeeded && dirtyFlags && dirtyFlags.length !== 0 && (
|
|
||||||
<ButtonRow>
|
<ButtonRow>
|
||||||
<Button
|
<Button
|
||||||
startIcon={<CancelIcon />}
|
startIcon={<CancelIcon />}
|
||||||
@@ -471,7 +444,7 @@ const MqttSettingsForm: FC = () => {
|
|||||||
return (
|
return (
|
||||||
<SectionContent title={LL.SETTINGS_OF('MQTT')} titleGutter>
|
<SectionContent title={LL.SETTINGS_OF('MQTT')} titleGutter>
|
||||||
{blocker ? <BlockNavigation blocker={blocker} /> : null}
|
{blocker ? <BlockNavigation blocker={blocker} /> : null}
|
||||||
{restarting ? <RestartMonitor /> : content()}
|
{content()}
|
||||||
</SectionContent>
|
</SectionContent>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -44,11 +44,21 @@ MqttSettingsService::~MqttSettingsService() {
|
|||||||
|
|
||||||
void MqttSettingsService::begin() {
|
void MqttSettingsService::begin() {
|
||||||
_fsPersistence.readFromFS();
|
_fsPersistence.readFromFS();
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MqttSettingsService::startClient() {
|
||||||
|
static bool isSecure = false;
|
||||||
if (_mqttClient != nullptr) {
|
if (_mqttClient != nullptr) {
|
||||||
|
// do we need to change the client?
|
||||||
|
if ((isSecure && _state.rootCA.length() > 0) || (!isSecure && _state.rootCA.length() == 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
delete _mqttClient;
|
delete _mqttClient;
|
||||||
}
|
}
|
||||||
#if CONFIG_IDF_TARGET_ESP32S3
|
#if CONFIG_IDF_TARGET_ESP32S3
|
||||||
if (_state.rootCA.length() > 0) {
|
if (_state.rootCA.length() > 0) {
|
||||||
|
isSecure = true;
|
||||||
_mqttClient = static_cast<MqttClient *>(new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO));
|
_mqttClient = static_cast<MqttClient *>(new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO));
|
||||||
if (_state.rootCA == "insecure") {
|
if (_state.rootCA == "insecure") {
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)->setInsecure();
|
static_cast<espMqttClientSecure *>(_mqttClient)->setInsecure();
|
||||||
@@ -61,6 +71,7 @@ void MqttSettingsService::begin() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
isSecure = false;
|
||||||
_mqttClient = static_cast<MqttClient *>(new espMqttClient(espMqttClientTypes::UseInternalTask::NO));
|
_mqttClient = static_cast<MqttClient *>(new espMqttClient(espMqttClientTypes::UseInternalTask::NO));
|
||||||
static_cast<espMqttClient *>(_mqttClient)->onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1));
|
static_cast<espMqttClient *>(_mqttClient)->onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1));
|
||||||
static_cast<espMqttClient *>(_mqttClient)->onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1));
|
static_cast<espMqttClient *>(_mqttClient)->onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1));
|
||||||
@@ -134,6 +145,7 @@ void MqttSettingsService::onConfigUpdated() {
|
|||||||
_disconnectedAt = 0;
|
_disconnectedAt = 0;
|
||||||
|
|
||||||
// added by proddy
|
// added by proddy
|
||||||
|
startClient();
|
||||||
emsesp::EMSESP::mqtt_.start(); // reload EMS-ESP MQTT settings
|
emsesp::EMSESP::mqtt_.start(); // reload EMS-ESP MQTT settings
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,9 +250,8 @@ void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & settings) {
|
StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & settings) {
|
||||||
MqttSettings newSettings = {};
|
MqttSettings newSettings = {};
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
bool restartNeeded = false;
|
|
||||||
|
|
||||||
#if CONFIG_IDF_TARGET_ESP32S3
|
#if CONFIG_IDF_TARGET_ESP32S3
|
||||||
newSettings.rootCA = root["rootCA"] | "";
|
newSettings.rootCA = root["rootCA"] | "";
|
||||||
@@ -367,9 +378,11 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
|
|||||||
newSettings.rootCA.replace("-----BEGIN CERTIFICATE-----", "");
|
newSettings.rootCA.replace("-----BEGIN CERTIFICATE-----", "");
|
||||||
newSettings.rootCA.replace("-----END CERTIFICATE-----", "");
|
newSettings.rootCA.replace("-----END CERTIFICATE-----", "");
|
||||||
newSettings.rootCA.replace(" ", "");
|
newSettings.rootCA.replace(" ", "");
|
||||||
|
if (newSettings.rootCA.length() == 0 && newSettings.port > 8800) {
|
||||||
|
newSettings.rootCA = "insecure";
|
||||||
|
}
|
||||||
if (newSettings.rootCA != settings.rootCA) {
|
if (newSettings.rootCA != settings.rootCA) {
|
||||||
changed = true;
|
changed = true;
|
||||||
restartNeeded = true;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// save the new settings
|
// save the new settings
|
||||||
@@ -379,9 +392,5 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
|
|||||||
emsesp::EMSESP::mqtt_.reset_mqtt();
|
emsesp::EMSESP::mqtt_.reset_mqtt();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (restartNeeded) {
|
|
||||||
return StateUpdateResult::CHANGED_RESTART; // tell WebUI that a restart is needed
|
|
||||||
}
|
|
||||||
|
|
||||||
return StateUpdateResult::CHANGED;
|
return StateUpdateResult::CHANGED;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
|
|||||||
~MqttSettingsService();
|
~MqttSettingsService();
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
|
void startClient();
|
||||||
void loop();
|
void loop();
|
||||||
bool isEnabled();
|
bool isEnabled();
|
||||||
bool isConnected();
|
bool isConnected();
|
||||||
|
|||||||
Reference in New Issue
Block a user