Battery level check before update (#1387)

Co-authored-by: Uriel <[email protected]>
This commit is contained in:
lucas lelievreandUriel authored and GitHub committed 2025-04-15 18:59:33 +02:00
1 parent 44fc23d2f8
commit 7fec2f7468
4 files changed
+101 -36

No files matched your search

+1
View File
@@ -283,6 +283,7 @@ tracker-settings-forget = Forget tracker
tracker-settings-forget-description = Removes the tracker from the SlimeVR Server and prevent it from connecting to it until the server is restarted. The configuration of the tracker won't be lost.
tracker-settings-forget-label = Forget tracker
tracker-settings-update-unavailable = Cannot be updated (DIY)
tracker-settings-update-low-battery = Cannot update. Battery lower than 50%
tracker-settings-update-up_to_date = Up to date
tracker-settings-update-available = { $versionName } is now available
tracker-settings-update = Update now
@@ -10,7 +10,6 @@ import {
FirmwareUpdateStatus,
FirmwareUpdateStatusResponseT,
FirmwareUpdateStopQueuesRequestT,
HardwareInfoT,
RpcMessage,
TrackerStatus,
} from 'solarxr-protocol';
@@ -37,18 +36,30 @@ import { A } from '@/components/commons/A';
export function checkForUpdate(
currentFirmwareRelease: FirmwareRelease,
hardwareInfo: HardwareInfoT
) {
return (
// TODO: This is temporary, end goal is to support all board types
hardwareInfo.officialBoardType === BoardType.SLIMEVR &&
semver.valid(currentFirmwareRelease.version) &&
semver.valid(hardwareInfo.firmwareVersion?.toString() ?? 'none') &&
semver.lt(
hardwareInfo.firmwareVersion?.toString() ?? 'none',
currentFirmwareRelease.version
)
device: DeviceDataT
): 'need-update' | 'low-battery' | 'updated' | 'unavailable' {
if (
device.hardwareInfo?.officialBoardType !== BoardType.SLIMEVR ||
!semver.valid(currentFirmwareRelease.version) ||
!semver.valid(device.hardwareInfo.firmwareVersion?.toString() ?? 'none')
) {
return 'unavailable';
}
const canUpdate = semver.lt(
device.hardwareInfo.firmwareVersion?.toString() ?? 'none',
currentFirmwareRelease.version
);
if (
canUpdate &&
device.hardwareStatus?.batteryPctEstimate &&
device.hardwareStatus?.batteryPctEstimate < 50
) {
return 'low-battery';
}
return canUpdate ? 'need-update' : 'updated';
}
interface FirmwareUpdateForm {
@@ -126,12 +137,12 @@ export function FirmwareUpdate() {
const devices =
state.datafeed?.devices.filter(
({ trackers, hardwareInfo }) =>
trackers.length > 0 &&
(device) =>
device.trackers.length > 0 &&
currentFirmwareRelease &&
hardwareInfo &&
checkForUpdate(currentFirmwareRelease, hardwareInfo) &&
trackers.every(({ status }) => status === TrackerStatus.OK)
device.hardwareInfo &&
checkForUpdate(currentFirmwareRelease, device) === 'need-update' &&
device.trackers.every(({ status }) => status === TrackerStatus.OK)
) || [];
useRPCPacket(
+56 -13
View File
@@ -15,6 +15,52 @@ import { DownloadIcon } from '@/components/commons/icon/DownloadIcon';
import { Link } from 'react-router-dom';
import { useAppContext } from '@/hooks/app';
import { checkForUpdate } from '@/components/firmware-update/FirmwareUpdate';
import { Tooltip } from '@/components/commons/Tooltip';
import { Localized } from '@fluent/react';
function UpdateIcon({
showUpdate,
}: {
showUpdate: 'need-update' | 'low-battery' | 'updated' | 'unavailable';
}) {
const content = (
<div className="relative">
<div
className={classNames(
'absolute rounded-full h-6 w-6 left-1 top-1 bg-accent-background-10 animate-[ping_2s_linear_infinite]',
showUpdate !== 'need-update' && 'hidden'
)}
></div>
<div
className={classNames(
'absolute rounded-full h-8 w-8 justify-center flex items-center',
showUpdate === 'low-battery'
? 'cursor-not-allowed bg-background-80 outline-2 outline-status-critical outline'
: 'hover:bg-background-40 hover:cursor-pointer bg-background-50'
)}
>
<DownloadIcon width={15}></DownloadIcon>
</div>
</div>
);
return showUpdate !== 'need-update' ? (
<Tooltip
preferedDirection="top"
content={
<Localized id={'tracker-settings-update-low-battery'}>
<Typography></Typography>
</Localized>
}
>
<div className="absolute right-5 -top-2.5">{content}</div>
</Tooltip>
) : (
<Link to="/firmware-update" className="absolute right-5 -top-2.5">
{content}
</Link>
);
}
function TrackerBig({
device,
@@ -143,6 +189,12 @@ export function TrackerCard({
const { useVelocity } = useTracker(tracker);
const velocity = useVelocity();
const showUpdate =
showUpdates &&
tracker.status !== TrackerStatusEnum.DISCONNECTED &&
currentFirmwareRelease &&
device &&
checkForUpdate(currentFirmwareRelease, device);
return (
<div className="relative">
<div
@@ -167,19 +219,10 @@ export function TrackerCard({
{smol && <TrackerSmol tracker={tracker} device={device}></TrackerSmol>}
{!smol && <TrackerBig tracker={tracker} device={device}></TrackerBig>}
</div>
{showUpdates &&
tracker.status !== TrackerStatusEnum.DISCONNECTED &&
currentFirmwareRelease &&
device?.hardwareInfo &&
checkForUpdate(currentFirmwareRelease, device.hardwareInfo) && (
<Link to="/firmware-update" className="absolute right-5 -top-2.5">
<div className="relative">
<div className="absolute rounded-full h-6 w-6 left-1 top-1 bg-accent-background-10 animate-[ping_2s_linear_infinite]"></div>
<div className="absolute rounded-full h-8 w-8 hover:bg-background-40 hover:cursor-pointer bg-background-50 justify-center flex items-center">
<DownloadIcon width={15}></DownloadIcon>
</div>
</div>
</Link>
{showUpdate &&
showUpdate !== 'unavailable' &&
showUpdate !== 'updated' && (
<UpdateIcon showUpdate={showUpdate}></UpdateIcon>
)}
</div>
);
+16 -6
View File
@@ -189,7 +189,7 @@ export function TrackerSettingsPage() {
const needUpdate =
currentFirmwareRelease &&
tracker?.device?.hardwareInfo &&
checkForUpdate(currentFirmwareRelease, tracker?.device?.hardwareInfo);
checkForUpdate(currentFirmwareRelease, tracker?.device);
const updateUnavailable =
tracker?.device?.hardwareInfo?.officialBoardType !== BoardType.SLIMEVR ||
!semver.valid(
@@ -242,28 +242,38 @@ export function TrackerSettingsPage() {
)}
{!updateUnavailable && (
<>
{!needUpdate && (
{needUpdate === 'updated' && (
<Localized id="tracker-settings-update-up_to_date">
<Typography>Up to date</Typography>
</Localized>
)}
{needUpdate && (
{needUpdate === 'need-update' && currentFirmwareRelease && (
<Localized
id="tracker-settings-update-available"
vars={{ versionName: currentFirmwareRelease?.name }}
vars={{ versionName: currentFirmwareRelease.name }}
>
<Typography color="text-accent-background-10">
New version available
</Typography>
</Localized>
)}
{needUpdate === 'low-battery' && currentFirmwareRelease && (
<Localized
id="tracker-settings-update-low-battery"
vars={{ versionName: currentFirmwareRelease.name }}
>
<Typography color="text-status-critical"></Typography>
</Localized>
)}
</>
)}
</div>
<Localized id="tracker-settings-update">
<Button
variant={needUpdate ? 'primary' : 'secondary'}
disabled={!needUpdate}
variant={
needUpdate === 'need-update' ? 'primary' : 'secondary'
}
disabled={needUpdate !== 'need-update'}
to="/firmware-update"
>
Update now