Skip to content

armgit / core

Public Imported from GitHub
0

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

Feature/insights/image widget (#1060)

Mihaela Aleksandrovaaa7d6440cb4a
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index a6950e196..a738f596f 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -602,6 +602,8 @@
"widget-kpi": "KPI",
"widget-linechart": "Line Chart",
"chartConfig": "Chart Configuration",
+ "markerCoordinates": "Marker Coordinates",
+ "imageSettings": "Image Settings",
"presets.wrap_to_single_column": "Wrap to single column",
"presets.keep_layout": "Keep layout",
"presets.block_device": "Block this device",
@@ -642,7 +644,9 @@
"showUnits": "Show units",
"showLabels": "Show labels",
"zoom": "Zoom",
- "center": "Center"
+ "center": "Center",
+ "imageUrl": "Image URL",
+ "noImageSelected": "No image selected"
},
"south": "South",
"east": "East",
diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.json
index 309a884c0..ec896b083 100644
--- a/manager/src/web/shared/locales/nl/or.json
+++ b/manager/src/web/shared/locales/nl/or.json
@@ -602,6 +602,8 @@
"widget-kpi": "KPI",
"widget-linechart": "Lijngrafiek",
"chartConfig": "Grafiek configuratie",
+ "markerCoordinates": "Marker coordinaten",
+ "imageSettings": "Afbeeldingsinstellingen",
"presets.wrap_to_single_column": "Widgets onder elkaar zetten",
"presets.keep_layout": "Behoud indeling",
"presets.block_device": "Blokkeer dit apparaat",
@@ -642,7 +644,9 @@
"showUnits": "Toon eenheden",
"showLabels": "Toon labels",
"zoom": "Zoom",
- "center": "Middelpunt"
+ "center": "Middelpunt",
+ "imageUrl": "Afbeelding URL",
+ "noImageSelected": "Geen afbeelding geselecteerd"
},
"south": "Zuid",
"east": "Oost",
diff --git a/ui/component/or-dashboard-builder/src/index.ts b/ui/component/or-dashboard-builder/src/index.ts
index 58f0674d8..e0468635d 100644
--- a/ui/component/or-dashboard-builder/src/index.ts
+++ b/ui/component/or-dashboard-builder/src/index.ts
@@ -34,6 +34,7 @@ import {OrChartWidget} from "./widgets/or-chart-widget";
import { OrKpiWidget } from "./widgets/or-kpi-widget";
import { OrGaugeWidget } from "./widgets/or-gauge-widget";
import {OrMapWidget} from "./widgets/or-map-widget";
+import {OrImageWidget} from "./widgets/or-image-widget";
// language=CSS
const styling = css`
@@ -214,6 +215,7 @@ export function registerWidgetTypes() {
widgetTypes.set("kpi", new OrKpiWidget());
widgetTypes.set("gauge", new OrGaugeWidget());
widgetTypes.set("map", new OrMapWidget());
+ widgetTypes.set("image", new OrImageWidget());
}
@customElement("or-dashboard-builder")
diff --git a/ui/component/or-dashboard-builder/src/or-dashboard-browser.ts b/ui/component/or-dashboard-builder/src/or-dashboard-browser.ts
index 27927e29b..cfd64570a 100644
--- a/ui/component/or-dashboard-builder/src/or-dashboard-browser.ts
+++ b/ui/component/or-dashboard-builder/src/or-dashboard-browser.ts
@@ -65,7 +65,7 @@ export class OrDashboardBrowser extends LitElement {
protected renderGrid() {
const sidebarElement = this.shadowRoot?.getElementById("sidebarElement");
- const coords: Array<[number, number]> = new Array<[number, number]>([0,0], [2,0], [0,2], [2,2], [4,2], [4,4], [6,4], [6,6], [8,6], [8,8]) // TODO: make this unlimited possibilities with a formula
+ const coords: Array<[number, number]> = new Array<[number, number]>([0,0],[2,0], [0,2], [2,2], [0,4], [2,4], [0,6], [2,6], [0,8], [2,8]) // TODO: make this unlimited possibilities with a formula
const sidebarItems: any[] = Array.from(widgetTypes).map((typeArr, index) => {
return {x: coords[index][0], y: coords[index][1], w: 2, h: 2, widgetTypeId: typeArr[0], locked: true, content: `<div class="sidebarItem"><or-icon icon="${typeArr[1].DISPLAY_MDI_ICON}"></or-icon><span class="itemText">${typeArr[1].DISPLAY_NAME}</span>`}
});
@@ -78,8 +78,15 @@ export class OrDashboardBrowser extends LitElement {
}
});
+ let newSidebarHeight = 0;
+ if (sidebarItems.length % 2 !== 0) {
+ // If sidebarHeight is based on nr of widgets in array & is not even add 1
+ newSidebarHeight = sidebarItems.length + 1
+ } else {
+ newSidebarHeight = sidebarItems.length
+ }
// Creation of the sidebarGrid. Only loads items if already existing
- if(this.sidebarGrid != undefined) {
+ if(this.sidebarGrid !== undefined) {
this.sidebarGrid.removeAll();
this.sidebarGrid.load(sidebarItems);
} else {
@@ -93,19 +100,18 @@ export class OrDashboardBrowser extends LitElement {
appendTo: 'parent'
},
margin: 8,
- row: sidebarHeight
+ row: newSidebarHeight
// @ts-ignore typechecking, because we can only provide an HTMLElement (which GridHTMLElement inherits)
}, sidebarElement);
this.sidebarGrid.load(sidebarItems);
-
// If an item gets dropped on the main grid, the dragged item needs to be reset to the sidebar.
// This is done by just loading the initial/original widget back in the sidebar.
// @ts-ignore typechecking since we assume they are not undefined
this.sidebarGrid.on('removed', (_event: Event, items: GridStackNode[]) => {
- if(items.length == 1) {
+ if(items.length === 1) {
const filteredItems = sidebarItems.filter(widget => {
- return (widget.content == items[0].content);
+ return (widget.content === items[0].content);
});
this.sidebarGrid?.load([filteredItems[0]]);
}
@@ -124,6 +130,7 @@ export class OrDashboardBrowser extends LitElement {
if(this.backgroundGrid != undefined) {
this.backgroundGrid.removeAll();
this.backgroundGrid.load(sidebarItems);
+
} else {
this.backgroundGrid = GridStack.init({
staticGrid: true,
diff --git a/ui/component/or-dashboard-builder/src/widgets/or-image-widget.ts b/ui/component/or-dashboard-builder/src/widgets/or-image-widget.ts
new file mode 100644
index 000000000..587df5d16
--- /dev/null
+++ b/ui/component/or-dashboard-builder/src/widgets/or-image-widget.ts
@@ -0,0 +1,446 @@
+import manager, {DefaultColor2, DefaultColor3, Util} from '@openremote/core';
+import {
+ Asset,
+ AssetModelUtil,
+ Attribute,
+ AttributeRef,
+ DashboardWidget, WellknownValueTypes
+} from "@openremote/model";
+import {showSnackbar} from '@openremote/or-mwc-components/or-mwc-snackbar';
+import {i18next} from '@openremote/or-translate';
+import {css, html, LitElement, TemplateResult, unsafeCSS} from 'lit';
+import {customElement, property, state} from 'lit/decorators.js';
+import {OrWidgetConfig, OrWidgetEntity} from './or-base-widget';
+import {when} from 'lit/directives/when.js';
+import {map} from 'lit/directives/map.js';
+import {styleMap} from 'lit/directives/style-map.js';
+import {style} from '../style';
+import {SettingsPanelType, widgetSettingsStyling} from '../or-dashboard-settingspanel';
+import {InputType, OrInputChangedEvent} from '@openremote/or-mwc-components/or-mwc-input';
+
+export interface ImageAssetMarker {
+ attributeRef: AttributeRef,
+ coordinates: [number, number]
+}
+
+export interface ImageWidgetConfig extends OrWidgetConfig {
+ displayName: string;
+ attributeRefs: AttributeRef[];
+ markers: ImageAssetMarker[];
+ showTimestampControls: boolean;
+ imagePath: string;
+}
+
+export class OrImageWidget implements OrWidgetEntity {
+
+ readonly DISPLAY_MDI_ICON: string = 'file-image-marker'; // https://materialdesignicons.com;
+ readonly DISPLAY_NAME: string = 'Image';
+ readonly MIN_COLUMN_WIDTH: number = 2;
+ readonly MIN_PIXEL_HEIGHT: number = 0;
+ readonly MIN_PIXEL_WIDTH: number = 0;
+
+ getDefaultConfig(widget: DashboardWidget): OrWidgetConfig {
+ return {
+ displayName: widget?.displayName,
+ attributeRefs: [],
+ showTimestampControls: false,
+ imagePath: '',
+ markers: [],
+ } as unknown as ImageWidgetConfig;
+ }
+
+ getSettingsHTML(widget: DashboardWidget, realm: string) {
+ return html`
+ <or-image-widgetsettings .widget="${widget}" realm="${realm}"></or-image-widgetsettings>`;
+ }
+
+ getWidgetHTML(widget: DashboardWidget, editMode: boolean, realm: string) {
+ return html`
+ <or-image-widget .widget="${widget}" .editMode="${editMode}" realm="${realm}" style="height: 100%; overflow: hidden;"></or-image-widget>`;
+ }
+
+// Triggered every update to double check if the specification.
+ // It will merge missing values, or you can add custom logic to process here.
+ verifyConfigSpec(widget: DashboardWidget): ImageWidgetConfig {
+ return Util.mergeObjects(this.getDefaultConfig(widget), widget.widgetConfig, false) as ImageWidgetConfig;
+ }
+}
+
+const contentStyling = css`
+ #img-wrapper {
+ height: 100%;
+ width: 100%;
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ overflow: hidden;
+ z-index: 1;
+ }
+
+ #img-container {
+ position: relative;
+ max-height: 100%;
+ }
+
+ #img-content {
+ height: 100%;
+ max-height: 100%;
+ max-width: 100%;
+ }
+
+ #overlay {
+ position: absolute;
+ z-index: 3;
+
+ /*additional marker styling*/
+ color: var(--or-app-color2, ${unsafeCSS(DefaultColor2)});
+ background-color: var(--or-app-color3, ${unsafeCSS(DefaultColor3)});
+ border-radius: 15px;
+ padding: 3px 8px 5px 8px;
+ object-fit: contain;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+`;
+
+@customElement('or-image-widget')
+export class OrImageWidgetContent extends LitElement {
+
+ @property()
+ public realm?: string;
+
+ @property()
+ public readonly widget?: DashboardWidget;
+
+ @property()
+ public editMode?: boolean;
+
+ @state()
+ protected assets: Asset[] = [];
+
+ @state()
+ protected assetAttributes: [number, Attribute<any>][] = [];
+
+ static styles = contentStyling;
+
+ // method to render and update the markers on the image
+ private handleMarkerPlacement(config: ImageWidgetConfig) {
+ if (this.assetAttributes.length && config.attributeRefs.length > 0) {
+
+ if(config.markers.length === 0) {
+ console.error("No markers found!");
+ return [];
+ }
+ return config.attributeRefs.map((attributeRef, index) => {
+ const marker = config.markers.find(m => m.attributeRef.id === attributeRef.id && m.attributeRef.name === attributeRef.name);
+ const asset = this.assets.find(a => a.id === attributeRef.id);
+ let value: string | undefined;
+ const styles: any = {
+ "left": `${marker!.coordinates[0]}%`,
+ "top": `${marker!.coordinates[1]}%`
+ };
+ if(asset) {
+ const attribute = asset.attributes![attributeRef.name!];
+ const descriptors = AssetModelUtil.getAttributeAndValueDescriptors(asset.type, attributeRef.name, attribute);
+ value = Util.getAttributeValueAsString(attribute, descriptors[0], asset.type, true, "-");
+ if(attribute?.type === WellknownValueTypes.COLOURRGB && value !== "-") {
+ styles.backgroundColor = value;
+ styles.minHeight = "21px";
+ styles.minWidth = "13px";
+ value = undefined;
+ }
+ }
+ return html`
+ <span id="overlay" style="${styleMap(styles)}">
+ ${value}
+ </span>
+ `;
+ });
+ }
+ }
+
+
+ render() {
+ const imagePath = this.widget?.widgetConfig.imagePath;
+ return html`
+ <div id="img-wrapper">
+ ${when(imagePath, () => html`
+ <div id="img-container">
+ <img id="img-content" src="${imagePath}" alt=""/>
+ <div>
+ ${this.handleMarkerPlacement(this.widget?.widgetConfig)}
+ </div>
+ </div>
+ `, () => html`
+ <span>${i18next.t('dashboard.noImageSelected')}</span>
+ `)}
+ </div>
+ `;
+ }
+
+ updated(changedProperties: Map<string, any>) {
+ if (changedProperties.has('widget') || changedProperties.has('editMode')) {
+ if (this.assetAttributes.length !== this.widget!.widgetConfig.attributeRefs.length) {
+ this.fetchAssets(this.widget?.widgetConfig).then(assets => {
+ this.assets = assets!;
+ this.assetAttributes = this.widget?.widgetConfig.attributeRefs.map((attrRef: AttributeRef) => {
+ const assetIndex = assets!.findIndex(asset => asset.id === attrRef.id);
+ const foundAsset = assetIndex >= 0 ? assets![assetIndex] : undefined;
+ return foundAsset && foundAsset.attributes ? [assetIndex, foundAsset.attributes[attrRef.name!]] : undefined;
+ }).filter((indexAndAttr: any) => !!indexAndAttr) as [number, Attribute<any>][];
+ });
+ }
+ }
+ }
+
+
+ // Fetching the assets according to the AttributeRef[] input in DashboardWidget if required. TODO: Simplify this to only request data needed for attribute list
+ async fetchAssets(config: OrWidgetConfig | any): Promise<Asset[] | undefined> {
+ if (config.attributeRefs && config.attributeRefs.length > 0) {
+ let assets: Asset[] = [];
+ await manager.rest.api.AssetResource.queryAssets({
+ ids: config.attributeRefs?.map((x: AttributeRef) => x.id) as string[],
+ realm: {
+ name: this.realm
+ },
+ select: {
+ attributes: config.attributeRefs?.map((x: AttributeRef) => x.name) as string[]
+ }
+ }).then(response => {
+ assets = response.data;
+ }).catch(reason => {
+ console.error(reason);
+ showSnackbar(undefined, i18next.t('errorOccurred'));
+ });
+ return assets;
+ }
+ }
+}
+
+const markerContainerStyling = css`
+ #marker-container {
+ display: flex;
+ justify-content: flex-end;
+ align-items: center;
+ }
+`;
+
+@customElement('or-image-widgetsettings')
+export class OrImageWidgetSettings extends LitElement {
+
+ static get styles() {
+ return [style, widgetSettingsStyling, markerContainerStyling];
+ }
+
+ private expandedPanels: string[] = [i18next.t('attributes'), i18next.t('dashboard.markerCoordinates'), i18next.t('dashboard.imageSettings')];
+
+ @state()
+ private loadedAssets?: Asset[];
+
+ @property()
+ public readonly widget?: DashboardWidget;
+
+ protected updated(changedProps: Map<string, any>) {
+ if(this.loadedAssets === undefined) {
+ this.fetchAssets(this.widget?.widgetConfig).then((assets) => {
+ if(assets === undefined) {
+ this.loadedAssets = [];
+ } else {
+ this.loadedAssets = assets;
+ }
+ });
+ }
+ }
+
+ // Fetching the assets according to the AttributeRef[] input in DashboardWidget if required. TODO: Simplify this to only request data needed for attribute list
+ async fetchAssets(config: OrWidgetConfig | any): Promise<Asset[] | undefined> {
+ if (config.attributeRefs && config.attributeRefs.length > 0) {
+ let assets: Asset[] = [];
+ await manager.rest.api.AssetResource.queryAssets({
+ ids: config.attributeRefs?.map((x: AttributeRef) => x.id) as string[],
+ realm: {
+ name: manager.displayRealm // TODO: Improve this
+ },
+ select: {
+ attributes: config.attributeRefs?.map((x: AttributeRef) => x.name) as string[]
+ }
+ }).then(response => {
+ assets = response.data;
+ }).catch((reason) => {
+ console.error(reason);
+ showSnackbar(undefined, i18next.t('errorOccurred'));
+ });
+ return assets;
+ }
+ }
+
+ // UI Rendering
+ render() {
+ const config = JSON.parse(JSON.stringify(this.widget!.widgetConfig)) as ImageWidgetConfig; // duplicate to edit, to prevent parent updates. Please trigger updateConfig()
+ return html`
+ <div>
+ ${this.generateExpandableHeader(i18next.t('attributes'))}
+ </div>
+ <div>
+ ${this.expandedPanels.includes(i18next.t('attributes')) ? html`
+ <or-dashboard-settingspanel .type="${SettingsPanelType.MULTI_ATTRIBUTE}" .onlyDataAttrs="${false}" .widgetConfig="${this.widget!.widgetConfig}"
+ @updated="${(event: CustomEvent) => {
+ this.onAttributesUpdate(event.detail.changes);
+ this.updateConfig(this.widget!, event.detail.changes.get('config'));
+ }}"
+ ></or-dashboard-settingspanel>
+ ` : null}
+ </div>
+ <div>
+ ${this.generateExpandableHeader(i18next.t('dashboard.markerCoordinates'))}
+ </div>
+ <div>
+ ${when(this.expandedPanels.includes(i18next.t('dashboard.markerCoordinates')), () => html`
+ <div class="expanded-panel" style="gap: 8px;">
+ ${map(this.draftCoordinateEntries(config), template => template)}
+ </div>
+ `)}
+ </div>
+ <div>
+ ${this.generateExpandableHeader(i18next.t('dashboard.imageSettings'))}
+ </div>
+ <div>
+ ${this.expandedPanels.includes(i18next.t('dashboard.imageSettings')) ? html`
+ <div class="expanded-panel">
+ <or-mwc-input style="width: 100%;" type="${InputType.TEXT}" label="${i18next.t('dashboard.imageUrl')}" .value="${config.imagePath}"
+ @or-mwc-input-changed="${(event: OrInputChangedEvent) => {
+ config.imagePath = event.detail.value;
+ this.updateConfig(this.widget!, config);
+ }}"
+ ></or-mwc-input>
+ </div>
+ ` : null}
+ </div>
+ `;
+ }
+
+
+ private draftCoordinateEntries(config: ImageWidgetConfig): TemplateResult[] {
+ const min = 0;
+ const max = 100;
+
+ if (config.markers.length > 0) {
+ return config.attributeRefs.map((attributeRef) => {
+ const marker = config.markers.find(m => m.attributeRef.id === attributeRef.id && m.attributeRef.name === attributeRef.name);
+ if(marker === undefined) {
+ console.error("A marker could not be found during drafting coordinate entries.");
+ return html``;
+ }
+ const index = config.markers.indexOf(marker);
+ const coordinates = marker.coordinates;
+ const asset = this.loadedAssets?.find(a => a.id === attributeRef.id);
+ let label: string | undefined;
+ if(asset) {
+ const attribute = asset.attributes![attributeRef.name!];
+ const descriptors = AssetModelUtil.getAttributeAndValueDescriptors(asset.type, attributeRef.name, attribute);
+ label = Util.getAttributeLabel(attribute, descriptors[0], asset.type, false);
+ }
+ return html`
+ <div id="marker-container">
+ <div style="flex: 1; display: flex; flex-direction: column;">
+ <span>${this.loadedAssets?.find(a => a.id === attributeRef.id)?.name}</span>
+ ${when(label, () => html`
+ <span style="color: gray;">${label}</span>
+ `)}
+ </div>
+ <div style="display: flex; gap: 8px;">
+ <or-mwc-input .disableSliderNumberInput="${true}" compact style="max-width: 64px;"
+ .type="${InputType.NUMBER}" .min="${min}" .max="${max}" .value="${coordinates[0]}"
+ @or-mwc-input-changed="${(event: OrInputChangedEvent) => {
+ config.markers[index].coordinates = [event.detail.value, coordinates[1]];
+ this.updateConfig(this.widget!, config);
+ }}"
+ ></or-mwc-input>
+
+ <or-mwc-input .disableSliderNumberInput="${true}" compact style="max-width: 64px;"
+ .type="${InputType.NUMBER}" .min="${min}" .max="${max}" .value="${coordinates[1]}"
+ @or-mwc-input-changed="${(event: OrInputChangedEvent) => {
+ config.markers[index].coordinates = [coordinates[0], event.detail.value];
+ this.updateConfig(this.widget!, config);
+ }}"
+ ></or-mwc-input>
+ </div>
+ </div>
+ `;
+ });
+ } else {
+ return [
+ html`<span>${i18next.t('noAttributeConnected')}</span>`
+ ];
+ }
+ }
+
+ // updates coordinate map according to the attributeRef entries per id
+ updateCoordinateMap(config: ImageWidgetConfig) {
+ for (let i = 0; i < config.attributeRefs.length; i++) {
+ const attributeRef = config.attributeRefs[i];
+ if (attributeRef === undefined) {
+ console.error('attributeRef is undefined');
+ return;
+ }
+ const index = config.markers.findIndex(m => m.attributeRef.id === attributeRef.id && m.attributeRef.name === attributeRef.name);
+ if (index === -1) {
+ config.markers.push({
+ attributeRef: attributeRef,
+ coordinates: [50, 50]
+ });
+ }
+ }
+ }
+
+ private updateConfig(widget: DashboardWidget, config: OrWidgetConfig | any, force = false) {
+ const oldWidget = JSON.parse(JSON.stringify(widget)) as DashboardWidget;
+ widget.widgetConfig = config;
+ this.requestUpdate('widget', oldWidget);
+ this.forceParentUpdate(new Map<string, any>([['widget', widget]]), force);
+ }
+
+
+ private onAttributesUpdate(changes: Map<string, any>) {
+ if (changes.has('loadedAssets')) {
+ this.loadedAssets = changes.get('loadedAssets');
+ }
+ if (changes.has('config')) {
+ const config = changes.get('config') as ImageWidgetConfig;
+ this.updateCoordinateMap(config);
+ }
+ }
+
+
+ // Method to update the Grid. For example after changing a setting.
+ private forceParentUpdate(changes: Map<string, any>, force = false) {
+ this.requestUpdate();
+ this.dispatchEvent(new CustomEvent('updated', {detail: {changes: changes, force: force}}));
+ }
+
+
+ private generateExpandableHeader(name: string): TemplateResult {
+ return html`
+ <span class="expandableHeader" @click="${() => {
+ this.expandPanel(name);
+ }}">
+ <or-icon icon="${this.expandedPanels.includes(name) ? 'chevron-down' : 'chevron-right'}"></or-icon>
+ <span style="margin-left: 6px; height: 25px; line-height: 25px;">${name}</span>
+ </span>
+ `;
+ }
+
+
+ private expandPanel(panelName: string): void {
+ if (this.expandedPanels.includes(panelName)) {
+ const indexOf = this.expandedPanels.indexOf(panelName, 0);
+ if (indexOf > -1) {
+ this.expandedPanels.splice(indexOf, 1);
+ }
+ } else {
+ this.expandedPanels.push(panelName);
+ }
+ this.requestUpdate();
+ }
+}