<template>
  <h2>Battery Status</h2>
  <table>
    <tbody>
      <tr>
        <td>{{ fmt(reading.voltage, 'V') }}</td>
        <td>{{ fmt(reading.current, 'A') }}</td>
        <td>{{ fmt(reading.power,   'W') }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
import axios  from 'axios'
import config from './config'

export default {
  data() {
    return {
      reading: {
        voltage: 0,
        current: 0,
        power:   0
      }
    }
  },
  mounted() {
    this.getReading()
  },
  methods: {
    async getReading() {
      const res    = await axios.get(config.api + '/ina226')
      this.reading = res.data

      setTimeout(this.getReading, 1000)
    },
    fmt(val, sfx) {
      return val.toFixed(2) + sfx
    }
  }
}
</script>

<style>
table {
  width: 100%;
}
</style>
