macos自动充放电脚本

为在质保期内多用电池,写此脚本自动放电。

  • 拔出电源,电量大于30%启动放电,少于10%停止放电。
  • 插上电源,停止放电。

配合小米智能插座,手机远程控制通电与断电。

待实现邮件通知功能:

  • 场景1:电量小于10%,发邮件给自己。收到邮件后,手机开启智能插座。
  • 场景2:电量充满,发邮件给自己。收到邮件后,手机关闭智能插座。

如此可实现全天候充放电。

#!/bin/bash
isStart=0

Info()
{
    time=$(date "+%Y-%m-%d %H:%M:%S")
    echo "$time $1"
}

KillAll()
{
    Info "Kill all yes"
    isStart=0
    killall yes
}

StartAll()
{
    isStart=1
    for num in {1..5}  
    do
        Info "Start task $num"
        yes > /dev/null &
    done
}

KillAll
StartAll

while :
do
    sleep 5
    info=`pmset -g batt | sed -n '2p'`
    status=`echo $info | awk '{print $4}'`
    if [ $? -ne 0 ];then
        Info "Get battle status failed. $info"
        continue
    fi
    percent=`echo $info | awk '{print $3}' | grep -o -E "\d+"`
    if [ $? -ne 0 ];then
        Info "Get battle percent failed. $info"
        continue
    fi

    Info "Current battle percent($percent%)."
    if [ $isStart -eq 1 ]; then
        Info "Task is running."
        # 有任务运行
        if [ "$status" = "charging;" ] || [ "$status" = "charged;" ];then
            Info "In power mode."
            # 插电状态,停止任务
            KillAll
            continue
        fi
        Info "In battle mode."
        if [ $percent -le 10 ];then
            Info "Battle le 10%."
            # 电量不足,停止任务
            KillAll
            continue
        fi
    else
        Info "Task is not running."
        # 无任务运行
        if [ "$status" = "discharging;" ];then
            Info "In battle mode."
            if [ $percent -ge 30 ];then
                Info "Battle ge 30%."
                # 未插电状态,启动任务
                StartAll
            fi
            continue
        fi
        Info "In power mode."
    fi
done
建站  ·  开发  ·  读书  ·  教程
↑长按或扫码关注“编程之海”公众号↑
↑长按或扫码关注“编程之海”公众号↑
编程之海 版权所有丨如未注明,均为原创丨转载请注明转自:https://codingsea.com/macos%e8%87%aa%e5%8a%a8%e5%85%85%e6%94%be%e7%94%b5%e8%84%9a%e6%9c%ac/
4 1 投票
文章评分
订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论

2

0

686

0
希望看到您的想法,请您发表评论x