tidy up restart

This commit is contained in:
proddy
2024-08-30 11:09:05 +02:00
parent d2f6f8203f
commit 117b55cc16
10 changed files with 72 additions and 186 deletions

View File

@@ -4,7 +4,6 @@ import { toast } from 'react-toastify';
import CancelIcon from '@mui/icons-material/Cancel'; import CancelIcon from '@mui/icons-material/Cancel';
import DownloadIcon from '@mui/icons-material/GetApp'; import DownloadIcon from '@mui/icons-material/GetApp';
import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined'; import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import WarningIcon from '@mui/icons-material/Warning'; import WarningIcon from '@mui/icons-material/Warning';
import { import {
Box, Box,
@@ -25,7 +24,13 @@ import {
getSchedule, getSchedule,
getSettings getSettings
} from 'api/app'; } from 'api/app';
import { checkUpgrade, getDevVersion, getStableVersion } from 'api/system'; import {
checkUpgrade,
getDevVersion,
getStableVersion,
restart,
uploadURL
} from 'api/system';
import { dialogStyle } from 'CustomTheme'; import { dialogStyle } from 'CustomTheme';
import { useRequest } from 'alova/client'; import { useRequest } from 'alova/client';
@@ -33,7 +38,6 @@ import type { APIcall } from 'app/main/types';
import RestartMonitor from 'app/status/RestartMonitor'; import RestartMonitor from 'app/status/RestartMonitor';
import { import {
FormLoader, FormLoader,
MessageBox,
SectionContent, SectionContent,
SingleUpload, SingleUpload,
useLayoutTitle useLayoutTitle
@@ -44,7 +48,6 @@ const DownloadUpload = () => {
const { LL } = useI18nContext(); const { LL } = useI18nContext();
const [restarting, setRestarting] = useState<boolean>(false); const [restarting, setRestarting] = useState<boolean>(false);
const [restartNeeded, setRestartNeeded] = useState<boolean>(false);
const [openDialog, setOpenDialog] = useState<boolean>(false); const [openDialog, setOpenDialog] = useState<boolean>(false);
const [useDev, setUseDev] = useState<boolean>(false); const [useDev, setUseDev] = useState<boolean>(false);
const [upgradeAvailable, setUpgradeAvailable] = useState<boolean>(false); const [upgradeAvailable, setUpgradeAvailable] = useState<boolean>(false);
@@ -89,24 +92,21 @@ const DownloadUpload = () => {
} = useRequest(SystemApi.readHardwareStatus); } = useRequest(SystemApi.readHardwareStatus);
const { send: sendUploadURL } = useRequest( const { send: sendUploadURL } = useRequest(
(data: { url: string }) => SystemApi.uploadURL(data), (data: { url: string }) => uploadURL(data),
{ {
immediate: false immediate: false
} }
); );
const { send: restartCommand } = useRequest(SystemApi.restart(), { const { send: restartCommand } = useRequest(restart(), {
immediate: false immediate: false
}); });
const restart = async () => { const callRestart = async () => {
await restartCommand() setRestarting(true);
.then(() => { await restartCommand().catch((error: Error) => {
setRestarting(true); toast.error(error.message);
}) });
.catch((error: Error) => {
toast.error(error.message);
});
}; };
const { send: sendCheckUpgrade } = useRequest(checkUpgrade, { const { send: sendCheckUpgrade } = useRequest(checkUpgrade, {
@@ -387,10 +387,11 @@ const DownloadUpload = () => {
</Button> </Button>
)} )}
</Typography> </Typography>
{upgradeAvailable ? ( <Typography mt={2} color="secondary">
<Typography mt={2} color="secondary"> <InfoOutlinedIcon color="secondary" sx={{ verticalAlign: 'middle' }} />
<InfoOutlinedIcon color="secondary" sx={{ verticalAlign: 'middle' }} /> &nbsp;&nbsp;
&nbsp;&nbsp;{LL.UPGRADE_AVAILABLE()} {upgradeAvailable ? LL.UPGRADE_AVAILABLE() : LL.LATEST_VERSION()}
{upgradeAvailable && (
<Button <Button
sx={{ ml: 2 }} sx={{ ml: 2 }}
size="small" size="small"
@@ -400,10 +401,9 @@ const DownloadUpload = () => {
> >
{LL.INSTALL('v' + isDev ? latestDevVersion : latestVersion)} {LL.INSTALL('v' + isDev ? latestDevVersion : latestVersion)}
</Button> </Button>
</Typography> )}
) : ( </Typography>
<Typography mt={2}>{LL.LATEST_VERSION()}</Typography>
)}
{renderUploadDialog()} {renderUploadDialog()}
</Box> </Box>
@@ -411,24 +411,11 @@ const DownloadUpload = () => {
{LL.UPLOAD()} {LL.UPLOAD()}
</Typography> </Typography>
<Box mb={2} color="warning.main"> <Box color="warning.main" sx={{ pb: 2 }}>
<Typography variant="body2">{LL.UPLOAD_TEXT()}</Typography> <Typography variant="body2">{LL.UPLOAD_TEXT()}</Typography>
</Box> </Box>
{restartNeeded ? ( <SingleUpload callRestart={callRestart} />
<MessageBox mt={2} level="warning" message={LL.RESTART_TEXT(0)}>
<Button
startIcon={<PowerSettingsNewIcon />}
variant="contained"
color="error"
onClick={restart}
>
{LL.RESTART()}
</Button>
</MessageBox>
) : (
<SingleUpload setRestartNeeded={setRestartNeeded} />
)}
</> </>
); );
}; };

View File

@@ -1,13 +1,13 @@
import { type FC, useEffect, useRef, useState } from 'react'; import { type FC, useEffect, useRef, useState } from 'react';
import * as SystemApi from 'api/system'; import { readHardwareStatus } from 'api/system';
import { useRequest } from 'alova/client'; import { useRequest } from 'alova/client';
import { FormLoader } from 'components'; import { FormLoader } from 'components';
import { useI18nContext } from 'i18n/i18n-react'; import { useI18nContext } from 'i18n/i18n-react';
const RESTART_TIMEOUT = 2 * 60 * 1000; // 2 minutes const RESTART_TIMEOUT = 2 * 60 * 1000; // 2 minutes
const POLL_INTERVAL = 1000; // every 1 second const POLL_INTERVAL = 2000; // every 2 seconds
export interface RestartMonitorProps { export interface RestartMonitorProps {
message?: string; message?: string;
@@ -16,10 +16,12 @@ export interface RestartMonitorProps {
const RestartMonitor: FC<RestartMonitorProps> = ({ message }) => { const RestartMonitor: FC<RestartMonitorProps> = ({ message }) => {
const [failed, setFailed] = useState<boolean>(false); const [failed, setFailed] = useState<boolean>(false);
const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout>(); const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout>();
const { LL } = useI18nContext(); const { LL } = useI18nContext();
const timeoutAt = useRef(new Date().getTime() + RESTART_TIMEOUT); const timeoutAt = useRef(new Date().getTime() + RESTART_TIMEOUT);
const { send } = useRequest(SystemApi.readSystemStatus); const { send } = useRequest(readHardwareStatus, { immediate: false });
const poll = useRef(async () => { const poll = useRef(async () => {
try { try {
@@ -35,7 +37,7 @@ const RestartMonitor: FC<RestartMonitorProps> = ({ message }) => {
}); });
useEffect(() => { useEffect(() => {
void poll.current(); setTimeoutId(setTimeout(poll.current, POLL_INTERVAL));
}, []); }, []);
useEffect(() => () => timeoutId && clearTimeout(timeoutId), [timeoutId]); useEffect(() => () => timeoutId && clearTimeout(timeoutId), [timeoutId]);

View File

@@ -52,24 +52,12 @@ const SystemStatus = () => {
const { me } = useContext(AuthenticatedContext); const { me } = useContext(AuthenticatedContext);
const [confirmRestart, setConfirmRestart] = useState<boolean>(false); const [confirmRestart, setConfirmRestart] = useState<boolean>(false);
const [processing, setProcessing] = useState<boolean>(false);
const [restarting, setRestarting] = useState<boolean>(); const [restarting, setRestarting] = useState<boolean>();
const { send: restartCommand } = useRequest(SystemApi.restart(), { const { send: restartCommand } = useRequest(SystemApi.restart(), {
immediate: false immediate: false
}); });
const { send: partitionCommand } = useRequest(SystemApi.partition(), {
immediate: false
});
const { send: factoryPartitionCommand } = useRequest(
SystemApi.factoryPartition(),
{
immediate: false
}
);
const { const {
data: data, data: data,
send: loadData, send: loadData,
@@ -208,7 +196,6 @@ const SystemStatus = () => {
value ? theme.palette.success.main : theme.palette.info.main; value ? theme.palette.success.main : theme.palette.info.main;
const restart = async () => { const restart = async () => {
setProcessing(true);
await restartCommand() await restartCommand()
.then(() => { .then(() => {
setRestarting(true); setRestarting(true);
@@ -218,37 +205,6 @@ const SystemStatus = () => {
}) })
.finally(() => { .finally(() => {
setConfirmRestart(false); setConfirmRestart(false);
setProcessing(false);
});
};
const partition = async () => {
setProcessing(true);
await partitionCommand()
.then(() => {
setRestarting(true);
})
.catch((error: Error) => {
toast.error(error.message);
})
.finally(() => {
setConfirmRestart(false);
setProcessing(false);
});
};
const factoryPartition = async () => {
setProcessing(true);
await factoryPartitionCommand()
.then(() => {
setRestarting(true);
})
.catch((error: Error) => {
toast.error(error.message);
})
.finally(() => {
setConfirmRestart(false);
setProcessing(false);
}); });
}; };
@@ -265,38 +221,14 @@ const SystemStatus = () => {
startIcon={<CancelIcon />} startIcon={<CancelIcon />}
variant="outlined" variant="outlined"
onClick={() => setConfirmRestart(false)} onClick={() => setConfirmRestart(false)}
disabled={processing}
color="secondary" color="secondary"
> >
{LL.CANCEL()} {LL.CANCEL()}
</Button> </Button>
{data.has_loader && (
<Button
startIcon={<PowerSettingsNewIcon />}
variant="outlined"
onClick={factoryPartition}
disabled={processing}
color="warning"
>
EMS-ESP Boot
</Button>
)}
{data.has_partition && (
<Button
startIcon={<PowerSettingsNewIcon />}
variant="outlined"
onClick={partition}
disabled={processing}
color="warning"
>
Partition
</Button>
)}
<Button <Button
startIcon={<PowerSettingsNewIcon />} startIcon={<PowerSettingsNewIcon />}
variant="outlined" variant="outlined"
onClick={restart} onClick={restart}
disabled={processing}
color="error" color="error"
> >
{LL.RESTART()} {LL.RESTART()}

View File

@@ -8,7 +8,7 @@ import { Box, Button } from '@mui/material';
import { useI18nContext } from 'i18n/i18n-react'; import { useI18nContext } from 'i18n/i18n-react';
import './drag-drop.css'; import './dragNdrop.css';
const DragNdrop = ({ onFileSelected }) => { const DragNdrop = ({ onFileSelected }) => {
const [file, setFile] = useState<File>(); const [file, setFile] = useState<File>();

View File

@@ -32,7 +32,7 @@ function LinearProgressWithLabel(props: LinearProgressProps & { value: number })
); );
} }
const SingleUpload = ({ setRestartNeeded }) => { const SingleUpload = ({ callRestart }) => {
const [md5, setMd5] = useState<string>(); const [md5, setMd5] = useState<string>();
const [file, setFile] = useState<File>(); const [file, setFile] = useState<File>();
const { LL } = useI18nContext(); const { LL } = useI18nContext();
@@ -44,19 +44,18 @@ const SingleUpload = ({ setRestartNeeded }) => {
abort: cancelUpload abort: cancelUpload
} = useRequest(SystemApi.uploadFile, { } = useRequest(SystemApi.uploadFile, {
immediate: false immediate: false
}).onSuccess(({ data }) => { }).onComplete(({ data }) => {
if (data) { if (data) {
setMd5(data.md5 as string); setMd5(data.md5 as string);
toast.success(LL.UPLOAD() + ' MD5 ' + LL.SUCCESSFUL()); toast.success(LL.UPLOAD() + ' MD5 ' + LL.SUCCESSFUL());
setFile(undefined); setFile(undefined);
} else { } else {
setRestartNeeded(true); callRestart();
} }
}); });
useEffect(async () => { useEffect(async () => {
if (file) { if (file) {
console.log('going to upload file ', file.name);
await sendUpload(file).catch((error: Error) => { await sendUpload(file).catch((error: Error) => {
if (error.message === 'The user aborted a request') { if (error.message === 'The user aborted a request') {
toast.warning(LL.UPLOAD() + ' ' + LL.ABORTED()); toast.warning(LL.UPLOAD() + ' ' + LL.ABORTED());
@@ -73,7 +72,7 @@ const SingleUpload = ({ setRestartNeeded }) => {
<> <>
{isUploading ? ( {isUploading ? (
<> <>
<Box width="100%" p={2}> <Box width="100%" pl={2} pr={2}>
<LinearProgressWithLabel <LinearProgressWithLabel
value={ value={
progress.total === 0 || progress.loaded === 0 progress.total === 0 || progress.loaded === 0
@@ -86,7 +85,7 @@ const SingleUpload = ({ setRestartNeeded }) => {
</Box> </Box>
<Button <Button
sx={{ ml: 2 }} sx={{ ml: 2, mt: 2 }}
startIcon={<CancelIcon />} startIcon={<CancelIcon />}
variant="outlined" variant="outlined"
color="error" color="error"

View File

@@ -8,52 +8,13 @@ RestartService::RestartService(AsyncWebServer * server, SecurityManager * securi
server->on(RESTART_SERVICE_PATH, server->on(RESTART_SERVICE_PATH,
HTTP_POST, HTTP_POST,
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { restart(request); }, AuthenticationPredicates::IS_ADMIN)); securityManager->wrapRequest([this](AsyncWebServerRequest * request) { restart(request); }, AuthenticationPredicates::IS_ADMIN));
server->on(PARTITION_SERVICE_PATH,
HTTP_POST,
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { partition(request); }, AuthenticationPredicates::IS_ADMIN));
server->on(FACTORYPARTITION_SERVICE_PATH,
HTTP_POST,
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { factory(request); }, AuthenticationPredicates::IS_ADMIN));
} }
void RestartService::restartNow() { void RestartService::restartNow() {
WiFi.disconnect(true); emsesp::EMSESP::system_.restart_requested(true); // will be handled by the main loop
delay(500);
ESP.restart();
} }
void RestartService::restart(AsyncWebServerRequest * request) { void RestartService::restart(AsyncWebServerRequest * request) {
emsesp::EMSESP::system_.store_nvs_values();
request->onDisconnect(RestartService::restartNow);
request->send(200);
}
void RestartService::partition(AsyncWebServerRequest * request) {
const esp_partition_t * ota_partition = esp_ota_get_next_update_partition(nullptr);
if (!ota_partition) {
request->send(400); // bad request
return;
}
uint64_t buffer;
esp_partition_read(ota_partition, 0, &buffer, 8);
if (buffer == 0xFFFFFFFFFFFFFFFF) { // partition empty
request->send(400); // bad request
return;
}
esp_ota_set_boot_partition(ota_partition);
emsesp::EMSESP::system_.store_nvs_values();
request->onDisconnect(RestartService::restartNow);
request->send(200);
}
void RestartService::factory(AsyncWebServerRequest * request) {
const esp_partition_t * factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, nullptr);
if (!factory_partition) {
request->send(400);
return;
}
esp_ota_set_boot_partition(factory_partition);
emsesp::EMSESP::system_.store_nvs_values();
request->onDisconnect(RestartService::restartNow); request->onDisconnect(RestartService::restartNow);
request->send(200); request->send(200);
} }

View File

@@ -8,8 +8,6 @@
#include "SecurityManager.h" #include "SecurityManager.h"
#define RESTART_SERVICE_PATH "/rest/restart" #define RESTART_SERVICE_PATH "/rest/restart"
#define PARTITION_SERVICE_PATH "/rest/partition"
#define FACTORYPARTITION_SERVICE_PATH "/rest/factoryPartition"
class RestartService { class RestartService {
public: public:
@@ -19,8 +17,6 @@ class RestartService {
private: private:
void restart(AsyncWebServerRequest * request); void restart(AsyncWebServerRequest * request);
void partition(AsyncWebServerRequest * request);
void factory(AsyncWebServerRequest * request);
}; };
#endif #endif

View File

@@ -121,20 +121,18 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
// did we just complete uploading a json file? // did we just complete uploading a json file?
if (request->_tempFile) { if (request->_tempFile) {
request->_tempFile.close(); // close the file handle as the upload is now done request->_tempFile.close(); // close the file handle as the upload is now done
emsesp::EMSESP::system_.store_nvs_values();
request->onDisconnect(RestartService::restartNow);
AsyncWebServerResponse * response = request->beginResponse(200); AsyncWebServerResponse * response = request->beginResponse(200);
request->send(response); request->send(response);
request->onDisconnect(RestartService::restartNow);
return; return;
} }
// check if it was a firmware upgrade // check if it was a firmware upgrade
// if no error, send the success response as a JSON // if no error, send the success response as a JSON
if (_is_firmware && !request->_tempObject) { if (_is_firmware && !request->_tempObject) {
emsesp::EMSESP::system_.store_nvs_values();
request->onDisconnect(RestartService::restartNow);
AsyncWebServerResponse * response = request->beginResponse(200); AsyncWebServerResponse * response = request->beginResponse(200);
request->send(response); request->send(response);
request->onDisconnect(RestartService::restartNow);
return; return;
} }

View File

@@ -288,40 +288,50 @@ void System::store_nvs_values() {
} }
// restart EMS-ESP // restart EMS-ESP
// app0 or app1
// on 16MB we have the additional boot and factory partitions
void System::system_restart(const char * partitionname) { void System::system_restart(const char * partitionname) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
// see if we are forcing a partition to use
if (partitionname != nullptr) { if (partitionname != nullptr) {
// Factory partition - label will be "factory"
const esp_partition_t * partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL); const esp_partition_t * partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
if (partition && strcmp(partition->label, partitionname) == 0) { if (partition && strcmp(partition->label, partitionname) == 0) {
esp_ota_set_boot_partition(partition); esp_ota_set_boot_partition(partition);
} else if (strcmp(esp_ota_get_running_partition()->label, partitionname) != 0) { } else
partition = esp_ota_get_next_update_partition(NULL); // try and find the parition by name
if (!partition) { if (strcmp(esp_ota_get_running_partition()->label, partitionname) != 0) {
LOG_ERROR("Partition '%s' not found", partitionname); partition = esp_ota_get_next_update_partition(nullptr);
return; if (!partition) {
}
if (strcmp(partition->label, partitionname) != 0 && strcmp(partitionname, "boot") != 0) {
partition = esp_ota_get_next_update_partition(partition);
if (!partition || strcmp(partition->label, partitionname)) {
LOG_ERROR("Partition '%s' not found", partitionname); LOG_ERROR("Partition '%s' not found", partitionname);
return; return;
} }
if (strcmp(partition->label, partitionname) != 0 && strcmp(partitionname, "boot") != 0) {
partition = esp_ota_get_next_update_partition(partition);
if (!partition || strcmp(partition->label, partitionname)) {
LOG_ERROR("Partition '%s' not found", partitionname);
return;
}
}
// check if partition is empty
uint64_t buffer;
esp_partition_read(partition, 0, &buffer, 8);
if (buffer == 0xFFFFFFFFFFFFFFFF) {
LOG_ERROR("Partition '%s' is empty, not bootable", partition->label);
return;
}
// set the boot partition
esp_ota_set_boot_partition(partition);
} }
uint64_t buffer;
esp_partition_read(partition, 0, &buffer, 8);
if (buffer == 0xFFFFFFFFFFFFFFFF) { // partition empty
LOG_ERROR("Partition '%s' is empty, not bootable", partition->label);
return;
}
esp_ota_set_boot_partition(partition);
}
LOG_INFO("Restarting EMS-ESP from %s partition", partitionname); LOG_INFO("Restarting EMS-ESP from %s partition", partitionname);
} else { } else {
LOG_INFO("Restarting EMS-ESP..."); LOG_INFO("Restarting EMS-ESP...");
} }
store_nvs_values();
Shell::loop_all(); restart_requested(false); // make sure it's not repeated
delay(1000); // wait a second store_nvs_values(); // save any NVS values
Shell::loop_all(); // flush log to output
delay(1000); // wait 1 second
ESP.restart(); ESP.restart();
#endif #endif
} }
@@ -597,12 +607,13 @@ void System::upload_status(bool in_progress) {
void System::loop() { void System::loop() {
// check if we're supposed to do a reset/restart // check if we're supposed to do a reset/restart
if (restart_requested()) { if (restart_requested()) {
this->system_restart(); system_restart();
} }
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
myPButton_.check(); // check button press myPButton_.check(); // check button press
// syslog
if (syslog_enabled_) { if (syslog_enabled_) {
syslog_.loop(); syslog_.loop();
} }
@@ -1875,7 +1886,7 @@ bool System::uploadFirmwareURL(const char * url) {
return false; return false;
} }
// flush buffers so latest log messages are shown // flush log buffers so latest messages are shown
Shell::loop_all(); Shell::loop_all();
// get tcp stream and send it to Updater // get tcp stream and send it to Updater