"use strict"; import { LitElement, html, css } from "lit-element"; import { state } from "../state.js"; import { pad, formatDate, round, get } from "../util.js"; import { archive } from "../archive.js"; import "@authentic/mwc-icon"; import "../card.js"; import "../spinner.js"; class PowerMeterLive extends LitElement { constructor() { super(...arguments); this._cachedDayArchive = {}; state.subscribe(this.requestUpdate.bind(this)); archive.subscribe(this.requestUpdate.bind(this)); } getImport(d) { const currentValue = state.data[d.device].import; const now = new Date(); const filename = `day/${d.device}_${now.getFullYear()}_${pad(now.getMonth() + 1, 2)}`; if (this._cachedDayArchive[filename] !== now.getDate()) delete archive.data[filename]; archive.load(`day/${d.device}_${now.getFullYear()}_${pad(now.getMonth() + 1, 2)}`); this._cachedDayArchive[filename] = now.getDate(); const lastValue = get(archive.data, [filename, `${now.getFullYear()}${pad(now.getMonth() + 1, 2)}${pad(now.getDate() - 1, 2)}`, "totalImported" ]); return currentValue && lastValue ? currentValue - lastValue : 0; } render() { const config = state.config.views.filter(view => view.url == this.viewid)[0]; const sum = config.meters.reduce((acc, d) => { if (state.data[d.device].power) acc.power += state.data[d.device].power; if (state.data[d.device].import) acc.import += state.data[d.device].import; const totalImport = this.getImport(d); if (totalImport) acc.totalImport += totalImport; return acc; }, { power: 0, import: 0, totalImport: 0 }); return html` ${state.data[config.meters[0].device] ? html`
last updated ${formatDate(state.data.lastUpdated)}
`}