"use strict"; import { LitElement, html, css } from "lit-element"; import { state } from "../state.js"; import { pad, round, weekdays, months, get } from "../util.js"; import { archive } from "../archive.js"; import "@authentic/mwc-icon"; import "@authentic/mwc-select"; import "@authentic/mwc-menu"; import "@authentic/mwc-list"; import "../card.js"; import "../spinner.js"; const formatName = (type, name) => { if (type == "day") { const date = new Date(`${name.substr(0, 4)}-${name.substr(4, 2)}-${name.substr(6, 2)}`); return `${weekdays[date.getDay()]} ${date.getDate()}th`; } else if (type == "week") { let res = ""; const date = new Date(name.substr(0, 4), 0, 1 + 7 * (Number(name.substr(4, 2)) - 1)); date.setDate(date.getDate() + 1 - date.getDay()); res += `${date.getDate()}.${date.getMonth()+1}.`; date.setDate(date.getDate() + 7 - date.getDay()); res += ` - ${date.getDate()}.${date.getMonth()+1}.`; return res; } else if (type == "month") { const date = new Date(`${name.substr(0, 4)}-${name.substr(4, 2)}-01`); return months[date.getMonth()]; } }; class TemperatureHistory extends LitElement { constructor() { super(...arguments); this.type = "day"; const now = new Date(); this.year = now.getFullYear(); this.month = pad(now.getMonth() + 1, 2); this.data = {}; state.subscribe(this.requestUpdate.bind(this)); archive.subscribe(this.requestUpdate.bind(this)); } handleSelected(field) { return (evt) => { this[field] = evt.detail.item.value; this.requestUpdate(); }; } render() { const config = state.config.views.filter(view => view.url == this.viewid)[0]; const now = new Date(); this.data = {}; this.sel = this.year; if (this.type === "day") this.sel += "_" + this.month; archive.load("metadata"); const years = get(archive.data, [ "metadata", "availableData" ], {}); const months = get(archive.data, [ "metadata", "availableData", this.year ], []); for (let d of config.meters) { archive.load(`${this.type}/${d.device}_${this.sel}`); const data = get(archive.data, [ `${this.type}/${d.device}_${this.sel}` ], {}); for (let day of Object.keys(data)) { if (!this.data[day]) this.data[day] = {}; this.data[day][d.device] = data[day].imported; } } return html`