compost.party/scripts/power-info.sh

21 lines
839 B
Bash
Executable file

#!/bin/sh
# outputs battery and power information as json
set -euo pipefail
printf "{"
for power_supply in /sys/class/power_supply/*; do
device=$(basename $power_supply)
current="$(cat $power_supply/current_now)"
voltage="$(cat $power_supply/voltage_now)"
printf "\"$device\":{"
[ -f $power_supply/capacity ] && printf "\"capacity\":{\"value\":%d,\"unit\":\"%%\"}," "$(cat $power_supply/capacity)"
printf "\"status\":\"%s\"," "$(cat $power_supply/status)"
printf "\"current\":{\"value\":%.1f,\"unit\":\"mA\"}," "$(echo "scale = 2; $current / 10^3" | bc -l)"
printf "\"voltage\":{\"value\":%.1f,\"unit\":\"V\"}," "$(echo "scale = 2; $voltage / 10^6" | bc -l)"
printf "\"power\":{\"value\":%.1f,\"unit\":\"W\"}" "$(echo "scale = 2; ($current * $voltage) / 10^12" | bc)"
printf "}\n"
done | paste -sd, | tr -d "\n"
printf "}\n"