Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

introduce dropdown to select period of time

Vincentca9343c5871a
diff --git a/ui/component/or-attribute-card/package.json b/ui/component/or-attribute-card/package.json
index 6764e7b94..50819300b 100644
--- a/ui/component/or-attribute-card/package.json
+++ b/ui/component/or-attribute-card/package.json
@@ -1,7 +1,7 @@
{
"name": "@openremote/or-attribute-card",
"version": "1.0.0",
- "description": "OpenRemote asset detail",
+ "description": "OpenRemote attribute card",
"main": "dist/umd/index.bundle.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
@@ -13,7 +13,8 @@
"author": "OpenRemote",
"license": "AGPL-3.0-or-later",
"dependencies": {
- "lit-element": "^2.2.1"
+ "lit-element": "^2.2.1",
+ "moment": "^2.24.0"
},
"devDependencies": {
"tslint": ">=5.11.0",
diff --git a/ui/component/or-attribute-card/src/index.ts b/ui/component/or-attribute-card/src/index.ts
index 2398f984f..3ac99728d 100644
--- a/ui/component/or-attribute-card/src/index.ts
+++ b/ui/component/or-attribute-card/src/index.ts
@@ -4,8 +4,10 @@ import i18next from "i18next";
import {Asset, DatapointInterval, MetaItemType, ValueDatapoint} from "@openremote/model";
import {manager, DefaultColor4, Util} from "@openremote/core";
import Chart, {ChartTooltipCallback} from "chart.js";
+import {InputType, OrInputChangedEvent} from "@openremote/or-input";
import {OrAttributeHistoryEvent} from "@openremote/or-attribute-history";
import {getMetaValue} from "@openremote/core/dist/util";
+import moment from "moment";
// language=CSS
const style = css`
@@ -56,12 +58,15 @@ export class OrAttributeCard extends LitElement {
@property()
public attributeName: string = "";
- private asset: Asset = {};
- private mainValue?: string;
-
@property()
private data: ValueDatapoint<any>[] = [];
+ private period: moment.unitOfTime.Base = "month";
+ private fromTimestamp?: Date = new Date();
+
+ private asset: Asset = {};
+ private mainValue?: string;
+
@query("#chart")
private _chartElem!: HTMLCanvasElement;
private _chart?: Chart;
@@ -159,6 +164,7 @@ export class OrAttributeCard extends LitElement {
<div class="panel-content">
<canvas id="chart"></canvas>
<span>total: ${this.mainValue}</span>
+ <or-input .type="${InputType.SELECT}" .label="${i18next.t("Timeframe")}" @or-input-changed="${(evt: OrInputChangedEvent) => this.period = evt.detail.value}" .value="${this.period}" .options="${this._getPeriodOptions().map(item => item.value)}"></or-input>
</div>
</div>
</div>
@@ -179,13 +185,17 @@ export class OrAttributeCard extends LitElement {
}
protected async getDatapointsByAttribute(id: string): Promise<ValueDatapoint<any>[]> {
+
+ const startOfPeriod = moment(this.fromTimestamp).startOf(this.period).toDate().getTime();
+ const endOfPeriod = moment(this.fromTimestamp).endOf(this.period).toDate().getTime();
+
const response = await manager.rest.api.AssetDatapointResource.getDatapoints(
id,
this.attributeName,
{
- interval: DatapointInterval.DAY,
- fromTimestamp: 1585692000000,
- toTimestamp: 1588283999999
+ interval: this._getInterval(),
+ fromTimestamp: startOfPeriod,
+ toTimestamp: endOfPeriod
}
);
@@ -229,4 +239,51 @@ export class OrAttributeCard extends LitElement {
return i18next.t(format, { postProcess: 'sprintf', sprintf: [total] }).trim();
}
+ protected _getPeriodOptions() {
+ return [
+ {
+ text: i18next.t(DatapointInterval.HOUR),
+ value: "hour"
+ },
+ {
+ text: i18next.t(DatapointInterval.DAY),
+ value: "day"
+ },
+ {
+ text: i18next.t(DatapointInterval.WEEK),
+ value: "week"
+ },
+ {
+ text: i18next.t(DatapointInterval.MONTH),
+ value: "month"
+ },
+ {
+ text: i18next.t(DatapointInterval.YEAR),
+ value: "year"
+ }
+ ];
+ }
+
+ protected _getInterval() {
+ let interval: DatapointInterval = DatapointInterval.HOUR;
+ switch (this.period) {
+ case "hour":
+ interval = DatapointInterval.MINUTE;
+ break;
+ case "day":
+ interval = DatapointInterval.HOUR;
+ break;
+ case "week":
+ interval = DatapointInterval.HOUR;
+ break;
+ case "month":
+ interval = DatapointInterval.DAY;
+ break;
+ case "year":
+ interval = DatapointInterval.MONTH;
+ break;
+ }
+ return interval;
+ }
+
}