diff --git a/central_frontend/src/api/OTAApi.ts b/central_frontend/src/api/OTAApi.ts index b729df9..57593e9 100644 --- a/central_frontend/src/api/OTAApi.ts +++ b/central_frontend/src/api/OTAApi.ts @@ -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 { + await APIClient.exec({ + method: "POST", + uri: "/ota/set_desired_version", + jsonData: { + version: update.version, + platform: update.platform, + devices: all_devices ? undefined : devices!, + }, + }); + } } diff --git a/central_frontend/src/dialogs/DeployOTAUpdateDialogProvider.tsx b/central_frontend/src/dialogs/DeployOTAUpdateDialogProvider.tsx index 0756ca0..08adbd9 100644 --- a/central_frontend/src/dialogs/DeployOTAUpdateDialogProvider.tsx +++ b/central_frontend/src/dialogs/DeployOTAUpdateDialogProvider.tsx @@ -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(); const loadDevicesList = async () => { @@ -34,7 +43,29 @@ export function DeployOTAUpdateDialogProvider(p: { const [allDevices, setAllDevices] = React.useState(false); const [selectedDevices, setSelectedDevices] = React.useState([]); - 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 (