Skip to content

Commit

Permalink
restart dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Aug 12, 2024
1 parent f10d875 commit 8a409e8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
1 change: 1 addition & 0 deletions interface/src/api/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const readSystemStatus = () =>
// commands
export const restart = () => alovaInstance.Post('/rest/restart');
export const partition = () => alovaInstance.Post('/rest/partition');
export const factoryPartition = () => alovaInstance.Post('/rest/factoryPartition');
export const factoryReset = () => alovaInstance.Post('/rest/factoryReset');

// SystemLog
Expand Down
50 changes: 41 additions & 9 deletions interface/src/app/status/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const SystemStatus: FC = () => {
immediate: false
});

const { send: factoryPartitionCommand } = useRequest(SystemApi.factoryPartition(), {
immediate: false
});

const {
data: data,
send: loadData,
Expand Down Expand Up @@ -229,6 +233,21 @@ const SystemStatus: FC = () => {
});
};

const factoryPartition = async () => {
setProcessing(true);
await factoryPartitionCommand()
.then(() => {
setRestarting(true);
})
.catch((error: Error) => {
toast.error(error.message);
})
.finally(() => {
setConfirmRestart(false);
setProcessing(false);
});
};

const renderRestartDialog = () => (
<Dialog
sx={dialogStyle}
Expand All @@ -247,15 +266,28 @@ const SystemStatus: FC = () => {
>
{LL.CANCEL()}
</Button>
<Button
startIcon={<PowerSettingsNewIcon />}
variant="outlined"
onClick={partition}
disabled={processing}
color="warning"
>
EMS-ESP Loader
</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
startIcon={<PowerSettingsNewIcon />}
variant="outlined"
Expand Down
4 changes: 3 additions & 1 deletion interface/src/types/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface HardwareStatus {
psram: boolean;
psram_size?: number;
free_psram?: number;
has_loader: boolean;

free_caps: number;
model: string;
}
Expand All @@ -44,6 +44,8 @@ export interface SystemStatus {
ap_status: boolean;
network_status: NetworkConnectionStatus;
wifi_rssi: number;
has_loader: boolean;
has_partition: boolean;
}

export enum LogLevel {
Expand Down
23 changes: 15 additions & 8 deletions lib/framework/RestartService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ RestartService::RestartService(AsyncWebServer * server, SecurityManager * securi
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() {
Expand All @@ -26,14 +29,6 @@ void RestartService::restart(AsyncWebServerRequest * request) {
}

void RestartService::partition(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) {
esp_ota_set_boot_partition(factory_partition);
emsesp::EMSESP::system_.store_nvs_values();
request->onDisconnect(RestartService::restartNow);
request->send(200);
return;
}
const esp_partition_t * ota_partition = esp_ota_get_next_update_partition(nullptr);
if (!ota_partition) {
request->send(400); // bad request
Expand All @@ -50,3 +45,15 @@ void RestartService::partition(AsyncWebServerRequest * request) {
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->send(200);
}
2 changes: 2 additions & 0 deletions lib/framework/RestartService.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define RESTART_SERVICE_PATH "/rest/restart"
#define PARTITION_SERVICE_PATH "/rest/partition"
#define FACTORYPARTITION_SERVICE_PATH "/rest/factoryPartition"

class RestartService {
public:
Expand All @@ -19,6 +20,7 @@ class RestartService {
private:
void restart(AsyncWebServerRequest * request);
void partition(AsyncWebServerRequest * request);
void factory(AsyncWebServerRequest * request);
};

#endif
24 changes: 11 additions & 13 deletions src/web/WebStatusService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
#endif
}

const esp_partition_t * partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, nullptr);
root["has_loader"] = partition != NULL;
partition = esp_ota_get_next_update_partition(nullptr);
if (partition) {
uint64_t buffer;
esp_partition_read(partition, 0, &buffer, 8);
root["has_partition"] = (buffer != 0xFFFFFFFFFFFFFFFF);
} else {
root["has_partition"] = false;
}

response->setLength();
request->send(response);
}
Expand Down Expand Up @@ -127,19 +138,6 @@ void WebStatusService::hardwareStatus(AsyncWebServerRequest * request) {
root["free_psram"] = ESP.getFreePsram() / 1024;
}

const esp_partition_t * partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, nullptr);
if (partition != NULL) { // factory partition found
root["has_loader"] = true;
} else { // check for not empty, smaller OTA partition
partition = esp_ota_get_next_update_partition(nullptr);
if (partition) {
uint64_t buffer;
esp_partition_read(partition, 0, &buffer, 8);
const esp_partition_t * running = esp_ota_get_running_partition();
root["has_loader"] = (buffer != 0xFFFFFFFFFFFFFFFF && running->size != partition->size);
}
}

root["model"] = EMSESP::system_.getBBQKeesGatewayDetails();

#endif
Expand Down

0 comments on commit 8a409e8

Please sign in to comment.