#!/usr/bin/env python3

from distutils.util import strtobool

PATH_PRESENT = '/sys/class/power_supply/axp20x-battery/present'
PATH_STATUS = '/sys/class/power_supply/axp20x-battery/status'
PATH_VOLTAGE = '/sys/class/power_supply/axp20x-battery/voltage_now'
PATH_CURRENT = '/sys/class/power_supply/axp20x-battery/current_now'
PATH_CAPACITY = '/sys/class/power_supply/axp20x-battery/capacity'
PATH_HEALTH = '/sys/class/power_supply/axp20x-battery/health'

present = False
with open(PATH_PRESENT, 'r') as f:
    present = bool(strtobool(f.read().strip()))

result = {'present': present}

if present:
    with open(PATH_STATUS,'r') as f:
        result['status'] = f.read().strip()
    with open(PATH_VOLTAGE, 'r') as f:
        voltage = f.read().strip()
        result['voltage'] = (int(voltage)/10000.0)*0.01
    with open(PATH_CURRENT, 'r') as f:
        current = f.read().strip()
        result['current'] = int(current)/1000
    with open(PATH_CAPACITY, 'r') as f:
        result['capacity'] = f.read().strip()
    with open(PATH_HEALTH, 'r') as f:
        result['health'] = f.read().strip()

import json
print(json.dumps(result))
