setup-your-mac / Setup-Your-Mac

Setup Your Mac aims to simplify initial device configuration by leveraging swiftDialog and Jamf Pro Policy Custom Events to allow end-users to self-complete Mac setup post-enrollment.
https://snelson.us/sym
MIT License
231 stars 52 forks source link

Feature Request: Ability to set minimum required battery percentage #132

Closed arnoldtaw closed 1 month ago

arnoldtaw commented 5 months ago

Since SYM is intended for end users to setup, it'd great if we can incorporate a battery check feature into SYM process. This addition would help users avoid interruptions due to low battery levels, contributing to a smoother user experience. Whether the computer was shipped with low battery, organizations need to deploy multiple apps, or FileVault processes are time-consuming, this feature could save users from potential interruptions

Is your feature request related to a problem? Please describe. Nope.

Describe the solution you'd like An option for us to set minimum required battery percentage. By not meeting that required percentage, user will be prompted a message (with icon?) to connect their mac to a power source. Preferably at the very beginning of the SYM process, where the timer is counting down for 60 seconds right now.

pmset -g batt | grep -Eo "\d+%" | cut -d% -f1 can provide the percentage. system_profiler SPPowerDataType | grep "Connected" | awk '{print $2}' can provide the status if computer is connected to power source or not.

Describe alternatives you've considered Add a custom script to run before SYM script starts. OR add a function within SYM script to check power connection before starting the script.

Additional context

dan-snelson commented 5 months ago

Happy New Year, @arnoldtaw!

Excellent idea! (I'm confident Graham has some code we could copy-pasta.)

dan-snelson commented 5 months ago

What about ensuring AC power is connected?

https://github.com/grahampugh/erase-install/blob/main/erase-install.sh#L574-L654

arnoldtaw commented 4 months ago

That logic seems right. Not sure if it's a better idea to spin up a new dialog process before the SYM process starts or integrated as part of the SYM, where the count down is. Do you plan to add this in the future version of SYM? If so, would love to request the ability to set minimum % too 😅

dan-snelson commented 1 month ago

Updated lines: https://github.com/grahampugh/erase-install/blob/main/erase-install.sh#L632-L712

dan-snelson commented 1 month ago

@arnoldtaw:

Please test 1.15.0-b11.

Screenshot 2024-05-29 at 4 59 52 AM
arnoldtaw commented 1 month ago

Thanks @dan-snelson I wasn't sure if this will be added so I added myself something similar. The following will (1) use swiftDialog (2) allow admin to set preferred % (3) allow user to connect to power source and continue (instead of ending the process)

function acPowerCheck () {
    required_battery_percentage=60
    current_battery_percentage=$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1)
    current_power=null

    check_ac_status () {
        if /usr/bin/pmset -g ps | /usr/bin/grep "AC Power" > /dev/null ; then
            current_power="Yes"
        fi
    }

    # Initial Check (mostly for desktop type device)
    check_ac_status

    if [[ $current_power == "Yes" ]]; then 
        updateScriptLog "PRE-FLIGHT CHECK: Device connected to AC power."
    else 
        updateScriptLog "PRE-FLIGHT CHECK: Device is not connected to AC power."
        # IF low battery, display message and loop 
        if [[ $current_battery_percentage -lt $required_battery_percentage ]]; then
            # Does not meet required % 
            updateScriptLog "PRE-FLIGHT CHECK: Current battery ($current_battery_percentage %) is lower than required battery ($required_battery_percentage %). Prompting to connect to AC power."

            /usr/local/bin/dialog --title "Waiting for AC Power Connection" \
                --icon "SF=powerplug.fill,colour=red,bgcolor=none" \
                --message "Please connect your Mac to an AC power adapter. \n\nAC power is required to continue." \
                --button1text "Continue" \
                --button1disabled \
                --ontop \
                --quitkey d \
                --blurscreen \
                --position center \
                --commandfile "/var/tmp/deployment_power_check.log" & dialogPID=$!

            until [ "$current_power" = "Yes" ]; do
                # Run AC power checks
                sleep 1
                check_ac_status
                updateScriptLog "PRE-FLIGHT CHECK: Waiting for AC power connection"
                sleep 1
            done
            updateScriptLog "PRE-FLIGHT CHECK: AC power connected!"
            /bin/echo "icon: SF=powerplug.fill,colour=green,bgcolor=none" >> /var/tmp/deployment_power_check.log
            sleep 2 
            /bin/echo "quit: " >> /var/tmp/deployment_power_check.log
        else 
            # Meet required % 
            updateScriptLog "PRE-FLIGHT CHECK: Current battery ($current_battery_percentage %) is equal or more than required battery ($required_battery_percentage %)."
        fi 
    fi
}
dan-snelson commented 1 month ago

Thanks, @arnoldtaw.

You’re invited to submit a PR; have you had a chance to test your code with the latest 1.15.0 ?

(Just skimming while AFK, dialog has yet to be installed.)

dan-snelson commented 1 month ago

@arnoldtaw:

Your Item No. 3 above inspired me; please take a look at 1.15.0-b16:

Screenshot 2024-05-30 at 4 28 24 AM Screenshot 2024-05-30 at 4 30 28 AM
dan-snelson commented 1 month ago

Addressed in SYM 1.15.0.