ctucx.git: smartie-pwa

[js] smarthome web-gui

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
"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`
				<div id="connection-status">
					${state.connected ? html`
						<p><mwc-icon>cloud_queue</mwc-icon> Connected</p>
					` : html`
						<p><mwc-icon>cloud_off</mwc-icon> Disconnected</p>
						<p class="lastupdate">last updated ${formatDate(state.data.lastUpdated)}</p>
					`}
				</div>
				<smarthome-card>
					<div class="table">
						<div class="table-row table-head">
							<div class="table-column">Name</div>
							<div class="table-column">Voltage</div>
							<div class="table-column">Power</div>
							<div class="table-column">Power Factor</div>
							<div class="table-column">Frequency</div>
							<div class="table-column">Total Import</div>
							<div class="table-column">Import</div>
						</div>
						${config.meters.map(d => html`
							<div class="table-row">
								<div class="table-column">${d.name}</div>
								<div class="table-column">${round(state.data[d.device].voltage, 2)} V</div>
								<div class="table-column">${round(state.data[d.device].power, 2)} W</div>
								<div class="table-column">${round(state.data[d.device].cosphi, 2)}</div>
								<div class="table-column">${round(state.data[d.device].frequency, 2)}</div>
								<div class="table-column">${round(state.data[d.device].import, 2)} kWh</div>
								<div class="table-column">${round(this.getImport(d), 2)} kWh</div>
							</div>
						`)}
						<div class="table-row">
							<div class="table-column">Total</div>
							<div class="table-column">-</div>
							<div class="table-column">${round(sum.power, 2)} W</div>
							<div class="table-column">-</div>
							<div class="table-column">-</div>
							<div class="table-column">${round(sum.import, 2)} kWh</div>
							<div class="table-column">${round(sum.totalImport, 2)} kWh</div>
						</div>
					</div>
				</smarthome-card>
			` : html`
				<smarthome-spinner>Trying to connect...</smarthome-spinner>
			`}
		`;
	}

	static get styles() {
		return css`
			:host {
				display: flex;
				flex-direction: column;
				--mdc-theme-on-primary: white;
				--mdc-theme-primary: #43a047;
				--mdc-theme-on-secondary: white;
				--mdc-theme-secondary: #616161;
			}
			#connection-status mwc-icon {
				padding-right: .5em;
			}
			#connection-status {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
			}
			#connection-status>p {
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
			}
			#connection-status>p.lastupdate {
				font-size: 1em;
			}
			.table {
				display: flex;
				flex-direction: row;
			}
			.table-row {
				display: flex;
				flex-direction: column;
				width: 100%;
			}
			.table-column {
				padding: 1em;
				height: 1em;
				display: flex;
				flex-direction: column;
				justify-content: center;
			}

			.table-column {
				background-color: #ddd;
			}
			.table-column:nth-child(2n) {
				background-color: #fff;
			}
			@media (min-width: 600px) {
				#connection-status {
					margin-top: 20px;
				}
			}
		`;
	}

	static get properties() {
		return {
			viewid: { type: String }
		};
	}
}

customElements.define("smarthome-power-meter-live", PowerMeterLive);