Make OTA update live

This commit is contained in:
Pierre HUBERT 2024-10-09 18:46:51 +02:00
parent 386f0439e4
commit e7190bab71
2 changed files with 52 additions and 2 deletions

View File

@ -65,4 +65,23 @@ export class OTAAPI {
})
).data;
}
/**
* Set desired version for one or mor devices
*/
static async SetDesiredVersion(
update: OTAUpdate,
all_devices: boolean,
devices?: string[]
): Promise<void> {
await APIClient.exec({
method: "POST",
uri: "/ota/set_desired_version",
jsonData: {
version: update.version,
platform: update.platform,
devices: all_devices ? undefined : devices!,
},
});
}
}

View File

@ -16,13 +16,22 @@ import {
} from "@mui/material";
import React from "react";
import { Device, DeviceApi } from "../api/DeviceApi";
import { OTAUpdate } from "../api/OTAApi";
import { OTAAPI, OTAUpdate } from "../api/OTAApi";
import { useAlert } from "../hooks/context_providers/AlertDialogProvider";
import { useConfirm } from "../hooks/context_providers/ConfirmDialogProvider";
import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider";
import { useSnackbar } from "../hooks/context_providers/SnackbarProvider";
import { AsyncWidget } from "../widgets/AsyncWidget";
export function DeployOTAUpdateDialogProvider(p: {
update: OTAUpdate;
onClose: () => void;
}): React.ReactElement {
const loadingMessage = useLoadingMessage();
const alert = useAlert();
const confirm = useConfirm();
const snackbar = useSnackbar();
const [devicesList, setDevicesList] = React.useState<Device[] | undefined>();
const loadDevicesList = async () => {
@ -34,7 +43,29 @@ export function DeployOTAUpdateDialogProvider(p: {
const [allDevices, setAllDevices] = React.useState(false);
const [selectedDevices, setSelectedDevices] = React.useState<string[]>([]);
const startDeployment = () => {};
const startDeployment = async () => {
if (
allDevices &&
!(await confirm(
"Do you really want to deploy the update to all devices?"
))
)
return;
try {
loadingMessage.show("Applying OTA update...");
await OTAAPI.SetDesiredVersion(p.update, allDevices, selectedDevices);
snackbar("The update was successfully applied!");
p.onClose();
} catch (e) {
console.error("Failed to deploy the udpate!", e);
alert(`Failed to deploy the udpate! ${e}`);
} finally {
loadingMessage.hide();
}
};
return (
<Dialog open={true} onClose={p.onClose}>
<DialogTitle>