100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Feature/map pins (#578)
vlichtenbergbc474928cf55
diff --git a/ui/app/manager/src/index.ts b/ui/app/manager/src/index.tsindex 34e9ca553..d9f863b80 100644--- a/ui/app/manager/src/index.ts+++ b/ui/app/manager/src/index.ts@@ -1,35 +1,26 @@// Declare require method which we'll use for importing webpack resources (using ES6 imports will confuse typescript parser)import {pageProvisioningProvider} from "./pages/page-provisioning";--declare var CONFIG_URL_PREFIX: string;-import {combineReducers, configureStore} from "@reduxjs/toolkit";import "@openremote/or-app";+import {AppConfig, appReducer, HeaderConfig, HeaderItem, OrApp, PageProvider, RealmAppConfig} from "@openremote/or-app";import {- OrApp,- AppConfig,- appReducer,- PageProvider,- RealmAppConfig,- HeaderConfig,- HeaderItem} from "@openremote/or-app";-import {+ headerItemAccount,+ headerItemAssets,+ headerItemExport,headerItemGatewayConnection,+ headerItemInsights,headerItemLanguage,headerItemLogout,headerItemLogs,headerItemMap,- headerItemAssets,- headerItemRules,- headerItemAccount,- headerItemUsers,- headerItemRoles,+ headerItemProvisioning,headerItemRealms,- headerItemInsights,- headerItemExport, headerItemProvisioning+ headerItemRoles,+ headerItemRules,+ headerItemUsers} from "./headers";import "./pages/page-map";-import {pageMapReducer, pageMapProvider, PageMapConfig} from "./pages/page-map";+import {PageMapConfig, pageMapProvider, pageMapReducer} from "./pages/page-map";import "./pages/page-assets";import {PageAssetsConfig, pageAssetsProvider} from "./pages/page-assets";import "./pages/page-gateway";@@ -48,9 +39,10 @@ import "./pages/page-roles";import {pageRolesProvider} from "./pages/page-roles";import "./pages/page-realms";import {pageRealmsProvider} from "./pages/page-realms";-import "./pages/page-realms";import {pageExportProvider} from "./pages/page-export";-import { ManagerConfig } from "@openremote/core";+import {ManagerConfig} from "@openremote/core";++declare var CONFIG_URL_PREFIX: string;const rootReducer = combineReducers({app: appReducer,diff --git a/ui/app/manager/src/pages/page-map.ts b/ui/app/manager/src/pages/page-map.tsindex 0c57805c5..8472843ca 100644--- a/ui/app/manager/src/pages/page-map.ts+++ b/ui/app/manager/src/pages/page-map.ts@@ -1,5 +1,6 @@import {css, html} from "lit";import {customElement, property, query} from "lit/decorators.js";+import {until} from 'lit/directives/until.js';import {createSlice, EnhancedStore, PayloadAction} from "@reduxjs/toolkit";import "@openremote/or-map";import {@@ -26,7 +27,7 @@ import {} from "@openremote/model";import {getAssetsRoute, getMapRoute} from "../routes";import {AppStateKeyed, Page, PageProvider, router} from "@openremote/or-app";-import {GenericAxiosResponse, RestResponse } from "@openremote/rest";+import {GenericAxiosResponse} from "@openremote/rest";export interface MapState {assets: Asset[];@@ -99,7 +100,8 @@ export const pageMapReducer = pageMapSlice.reducer;export interface PageMapConfig {card?: MapAssetCardConfig,- assetQuery?: AssetQuery+ assetQuery?: AssetQuery,+ markers?: MapMarkerConfig}export function pageMapProvider(store: EnhancedStore<MapStateKeyed>, config?: PageMapConfig): PageProvider<MapStateKeyed> {@@ -111,7 +113,7 @@ export function pageMapProvider(store: EnhancedStore<MapStateKeyed>, config?: Pa],pageCreator: () => {const page = new PageMap(store);- if(config) page.config = config;+ page.config = config || {};return page}};@@ -319,7 +321,7 @@ export class PageMap extends Page<MapStateKeyed> {return html`- ${this._currentAsset ? html `<or-map-asset-card .config="${this.config?.card}" .assetId="${this._currentAsset.id}"></or-map-asset-card>` : ``}+ ${this._currentAsset ? html `<or-map-asset-card .config="${this.config?.card}" .assetId="${this._currentAsset.id}" .markerconfig="${this.config?.markers}"></or-map-asset-card>` : ``}<or-map id="map" class="or-map" showGeoCodingControl @or-map-geocoder-change="${(ev: OrMapGeocoderChangeEvent) => {this._setCenter(ev.detail.geocode);}}">${@@ -328,11 +330,24 @@ export class PageMap extends Page<MapStateKeyed> {return false;}const attr = asset.attributes[WellknownAttributes.LOCATION] as Attribute<GeoJSONPoint>;- const showOnMap = !attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr);- return showOnMap;- }).map((asset) => {+ return !attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr);+ }).map(asset => {return html`- <or-map-marker-asset ?active="${this._currentAsset && this._currentAsset.id === asset.id}" .asset="${asset}"></or-map-marker-asset>+ ${until(+ manager.events.sendEventWithReply({+ event: {+ eventType: "read-asset",+ assetId: asset.id+ }+ }).then((result: AssetEvent) => {+ return html`+ <or-map-marker-asset+ ?active="${this._currentAsset && this._currentAsset.id === result.asset.id}"+ .asset="${result.asset}"+ .config="${this.config.markers}"></or-map-marker-asset>+ `;+ })+ )};`;})}diff --git a/ui/component/or-map/src/index.ts b/ui/component/or-map/src/index.tsindex 719a072c3..181c4dc9b 100644--- a/ui/component/or-map/src/index.ts+++ b/ui/component/or-map/src/index.ts@@ -16,7 +16,7 @@ import {ValueInputTemplateFunction} from "@openremote/or-mwc-components/or-mwc-input";import {OrMwcDialog, showDialog} from "@openremote/or-mwc-components/or-mwc-dialog";-import {getMarkerIconAndColorFromAssetType} from "./markers/or-map-marker-asset";+import {getMarkerIconAndColorFromAssetType} from "./util";import {i18next} from "@openremote/or-translate";// Re-exportsdiff --git a/ui/component/or-map/src/markers/or-map-marker-asset.ts b/ui/component/or-map/src/markers/or-map-marker-asset.tsindex 43a42852d..4051c44c7 100644--- a/ui/component/or-map/src/markers/or-map-marker-asset.ts+++ b/ui/component/or-map/src/markers/or-map-marker-asset.ts@@ -1,27 +1,44 @@import {PropertyValues} from "lit";import {customElement, property} from "lit/decorators.js";import {OrMapMarker} from "./or-map-marker";-import {AttributeEvent, GeoJSONPoint, AssetEvent, AssetEventCause, Asset, SharedEvent, AssetDescriptor, WellknownAttributes} from "@openremote/model";-import manager, {subscribe} from "@openremote/core";-import {AssetModelUtil} from "@openremote/model";+import {+ Asset,+ AssetEvent,+ AssetEventCause,+ AttributeEvent,+ GeoJSONPoint,+ ReadAttributeEvent,+ SharedEvent,+ WellknownAttributes,+ AssetModelUtil+} from "@openremote/model";+import manager, {subscribe, Util} from "@openremote/core";+import { getMarkerIconAndColorFromAssetType } from "../util";++export type BaseMapMarkerConfig = {+ attributeName: string;+ showLabel?: boolean;+ showUnits?: boolean;+ showDirection?: boolean;+}-export function getMarkerIconAndColorFromAssetType(type: AssetDescriptor | string | undefined): {icon: string, color: string | undefined} | undefined {- if (!type) {- return;- }+export type MapMarkerConfig = {+ [assetType: string]: AttributeMarkerColours[] | RangeAttributeMarkerColours[];+}- const descriptor = typeof(type) !== "string" ? type : AssetModelUtil.getAssetDescriptor(type);- const icon = descriptor && descriptor.icon ? descriptor.icon : "help-circle";- let color: string | undefined;+export type AttributeMarkerColours = BaseMapMarkerConfig & {+ type: "string" | "boolean";+ [value: string]: string;+}- if (descriptor && descriptor.colour) {- color = descriptor.colour;- }+export type RangeAttributeMarkerColours = BaseMapMarkerConfig & {+ type: "range";+ ranges: AttributeMarkerColoursRange[];+}- return {- color: color,- icon: icon- };+export type AttributeMarkerColoursRange = {+ max: number;+ colour: string;}@customElement("or-map-marker-asset")@@ -33,6 +50,9 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {@property({type: Object, attribute: true})public asset?: Asset;+ @property()+ public config?: MapMarkerConfig;+public assetTypeAsIcon: boolean = true;constructor() {@@ -43,18 +63,28 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {protected markerColor?: string;protected set type(type: string | undefined) {- const iconAndColor = getMarkerIconAndColorFromAssetType(type);- if (!iconAndColor) {+ let overrideOpts;+ if (this.config && type && this.config[type]) {+ const markerConfig = this.config[type][0] || undefined;++ if (this.displayValue) {+ overrideOpts = {markerConfig: markerConfig, attributeValue: this.displayValue};+ }+ }++ const iconAndColour = getMarkerIconAndColorFromAssetType(type, overrideOpts);++ if (!iconAndColour) {this.visible = false;return;}if (this.assetTypeAsIcon) {- this.icon = iconAndColor.icon;+ this.icon = iconAndColour.icon;}- this.markerColor = iconAndColor.color;+ this.markerColor = (Array.isArray(iconAndColour.color)) ? iconAndColour.color[0].colour : iconAndColour.color || undefined;this.updateColor(this.markerContainer);this.visible = true;}@@ -88,6 +118,11 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {return;}+ if (this.asset) {+ this.asset = Util.updateAsset(this.asset, event as AttributeEvent);+ this.requestUpdate();+ }+return;}@@ -107,16 +142,57 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {}}- protected onAssetChanged(asset?: Asset) {+ protected async onAssetChanged(asset?: Asset) {if (asset) {const attr = asset.attributes ? asset.attributes[WellknownAttributes.LOCATION] : undefined;this._updateLocation(attr ? attr.value as GeoJSONPoint : null);++ if (asset.attributes && asset.attributes[WellknownAttributes.DIRECTION]) {+ this.direction = (await this.getAttrValue(asset, WellknownAttributes.DIRECTION))!.toString() || undefined;+ }++ if (this.config && asset.type && this.config[asset.type]) {+ const assetTypeConfig = this.config[asset.type][0] || undefined;++ if (assetTypeConfig && assetTypeConfig.showLabel) {+ if (assetTypeConfig.showLabel) {+ const attrVal = await this.getAttrValue(asset, assetTypeConfig.attributeName);+ if (attrVal === undefined) return;++ this.displayValue = attrVal.toString();++ if (assetTypeConfig.showUnits !== false && attr) {+ const attributeDescriptor = AssetModelUtil.getAttributeDescriptor(assetTypeConfig.attributeName, asset.type);+ const unit = Util.resolveUnits(Util.getAttributeUnits(attr, attributeDescriptor, asset.type));+ this.displayValue = `${this.displayValue} ${unit}`;+ }+ }+ }++ if (assetTypeConfig && assetTypeConfig.showDirection === false) {+ this.direction = undefined;+ }+ }+this.type = asset.type;} else {this.lat = undefined;this.lng = undefined;}}++ protected async getAttrValue(asset: Asset, attributeName: string): Promise<string | number | boolean | undefined> {+ const currentValue: AttributeEvent = await manager.events!.sendEventWithReply({+ event: {+ eventType: "read-asset-attribute",+ ref: {+ id: asset.id,+ name: attributeName+ }+ } as ReadAttributeEvent+ });+ return currentValue.attributeState?.value;+ }protected _updateLocation(location: GeoJSONPoint | null) {this.lat = location && location.coordinates ? (location.coordinates as any)[1] : undefined;diff --git a/ui/component/or-map/src/markers/or-map-marker.ts b/ui/component/or-map/src/markers/or-map-marker.tsindex e9aff507e..e79dd33ee 100644--- a/ui/component/or-map/src/markers/or-map-marker.ts+++ b/ui/component/or-map/src/markers/or-map-marker.ts@@ -1,6 +1,7 @@-import {html, LitElement, PropertyValues} from "lit";+import {css, CSSResultGroup, html, LitElement, PropertyValues, unsafeCSS} from "lit";import {customElement, property, query} from "lit/decorators.js";import {markerActiveColorVar, markerColorVar} from "../style";+import {DefaultBoxShadowBottom} from "@openremote/core";export class OrMapMarkerChangedEvent extends CustomEvent<OrMapMarkerChangedEventDetail> {@@ -41,6 +42,11 @@ export interface OrMapMarkerChangedEventDetail extends OrMapMarkerEventDetail {property: string;}+export interface TemplateOptions {+ displayValue?: string;+ direction?: string;+}+declare global {export interface HTMLElementEventMap {[OrMapMarkerChangedEvent.NAME]: OrMapMarkerChangedEvent;@@ -56,7 +62,48 @@ declare global {@customElement("or-map-marker")export class OrMapMarker extends LitElement {- protected static _defaultTemplate = (icon: string | undefined) => `+ static get styles(): CSSResultGroup {+ return css`+ .label {+ background-color: white;+ width: auto;+ height: 20px;+ position: absolute;+ top: -20px;+ left: 50%;+ padding: 0 3px;+ transform: translate(-50%, -50%);+ text-align: center;+ border-radius: 3px;+ -webkit-box-shadow: ${unsafeCSS(DefaultBoxShadowBottom)};+ -moz-box-shadow: ${unsafeCSS(DefaultBoxShadowBottom)};+ box-shadow: ${unsafeCSS(DefaultBoxShadowBottom)};+ }+ .label > span {+ white-space: nowrap;+ }++ .icon-direction {+ position: absolute;+ top: 13px;+ left: 4px;+ width: 24px;+ fill: white;+ stroke: black;+ stroke-width: 1px;+ }+ `;+ }++ protected static _defaultTemplate = (icon: string | undefined, options?: TemplateOptions) => `+ ${options && options.displayValue !== undefined+ ? `<div class="label"><span>${options.displayValue}</span></div>`+ : ``+ }+ ${options && options.direction+ ? `<or-icon class="icon-direction" icon="navigation" style="transform: rotate(${options.direction}deg);"></or-icon>`+ : ``+ }<or-icon icon="or:marker"></or-icon><or-icon class="marker-icon" icon="${icon || ""}"></or-icon>`@@ -70,6 +117,12 @@ export class OrMapMarker extends LitElement {@property({type: Number, reflect: true})public radius?: number;+ @property({reflect: true})+ public displayValue?: string;++ @property({reflect: true})+ public direction?: string;+@property({type: Boolean})public visible: boolean = true;@@ -151,7 +204,7 @@ export class OrMapMarker extends LitElement {}protected shouldUpdate(_changedProperties: PropertyValues): boolean {- if (_changedProperties.has("icon")) {+ if (_changedProperties.has("icon") || _changedProperties.has("displayValue") || _changedProperties.has("direction")) {this.refreshMarkerContent();}@@ -268,7 +321,7 @@ export class OrMapMarker extends LitElement {protected createDefaultMarkerContent(): HTMLElement {const div = document.createElement("div");- div.innerHTML = OrMapMarker._defaultTemplate(this.icon);+ div.innerHTML = OrMapMarker._defaultTemplate(this.icon, {displayValue: this.displayValue, direction: this.direction});return div;}}diff --git a/ui/component/or-map/src/or-map-asset-card.ts b/ui/component/or-map/src/or-map-asset-card.tsindex 7bd425c60..6d0d27534 100644--- a/ui/component/or-map/src/or-map-asset-card.ts+++ b/ui/component/or-map/src/or-map-asset-card.ts@@ -1,6 +1,4 @@import {- CSSResult,- CSSResultArray,CSSResultGroup,html,LitElement,@@ -8,6 +6,7 @@ import {TemplateResult} from "lit";import {customElement, property} from "lit/decorators.js";+import { classMap } from 'lit-html/directives/class-map.js';import {Asset,AssetEvent,@@ -23,6 +22,8 @@ import "@openremote/or-icon";import {mapAssetCardStyle} from "./style";import { InputType } from "@openremote/or-mwc-components/or-mwc-input";import { i18next } from "@openremote/or-translate";+import { getMarkerIconAndColorFromAssetType } from "./util";+import { MapMarkerConfig } from "./markers/or-map-marker-asset";export interface MapAssetCardTypeConfig {include?: string[];@@ -75,6 +76,9 @@ export class OrMapAssetCard extends subscribe(manager)(LitElement) {@property({type: Object})public config?: MapAssetCardConfig;+ @property({type: Object})+ public markerconfig?: MapMarkerConfig;+@property({type: Boolean, attribute: true})public useAssetColor: boolean = true;@@ -162,7 +166,8 @@ export class OrMapAssetCard extends subscribe(manager)(LitElement) {const descriptors = AssetModelUtil.getAttributeAndValueDescriptors(this.asset!.type, attr.name, attr);const label = Util.getAttributeLabel(attr, descriptors[0], this.asset!.type, true);const value = Util.getAttributeValueAsString(attr, descriptors[0], this.asset!.type, false, "-");- return html`<li><span class="attribute-name">${label}</span><span class="attribute-value">${value}</span></li>`;+ const classes = {highlighted: attr.name === this.markerconfig![this.asset!.type!][0].attributeName};+ return html`<li class="${classMap(classes)}"><span class="attribute-name">${label}</span><span class="attribute-value">${value}</span></li>`;})}</ul></div>@@ -182,14 +187,17 @@ export class OrMapAssetCard extends subscribe(manager)(LitElement) {protected getIcon(): string | undefined {if (this.asset) {const descriptor = AssetModelUtil.getAssetDescriptor(this.asset.type);- return descriptor ? descriptor.icon : undefined;+ const icon = getMarkerIconAndColorFromAssetType(descriptor)?.icon;+ return icon ? icon : undefined;}}protected getColor(): string | undefined {if (this.asset) {const descriptor = AssetModelUtil.getAssetDescriptor(this.asset.type);- return descriptor ? descriptor.colour : undefined;+ const color = getMarkerIconAndColorFromAssetType(descriptor)?.color;+ // check if range+ return (typeof color === 'string') ? color : color![0].colour;}}}diff --git a/ui/component/or-map/src/style.ts b/ui/component/or-map/src/style.tsindex a34f86949..a1df4d83f 100644--- a/ui/component/or-map/src/style.ts+++ b/ui/component/or-map/src/style.ts@@ -207,6 +207,9 @@ export const mapAssetCardStyle = css`display: flex;line-height: 30px;}+ li.highlighted {+ font-weight: bold;+ }.attribute-name {flex: 1;diff --git a/ui/component/or-map/src/util.ts b/ui/component/or-map/src/util.tsindex e4acda1c7..d2a023e51 100644--- a/ui/component/or-map/src/util.ts+++ b/ui/component/or-map/src/util.ts@@ -1,5 +1,6 @@import {LngLat, LngLatBounds, LngLatBoundsLike, LngLatLike} from "maplibre-gl";-import {Asset, Attribute, GeoJSONPoint, ValueHolder, WellknownAttributes} from "@openremote/model";+import {Asset, AssetDescriptor, AssetModelUtil, Attribute, GeoJSONPoint, ValueHolder, WellknownAttributes} from "@openremote/model";+import { AttributeMarkerColours, AttributeMarkerColoursRange, RangeAttributeMarkerColours } from "./markers/or-map-marker-asset";export function getLngLat(lngLatLike?: LngLatLike | Asset | ValueHolder<any> | GeoJSONPoint): { lng: number, lat: number } | undefined {if (!lngLatLike) {@@ -78,3 +79,52 @@ export function getLatLngBounds(lngLatBoundsLike?: LngLatBoundsLike): L.LatLngBoreturn L.latLngBounds(lngLatBounds.getNorthEast()!, lngLatBounds.getSouthWest()!);}}++interface OverrideConfigSettings {+ markerConfig: AttributeMarkerColours | RangeAttributeMarkerColours;+ attributeValue: number | string | boolean;+}++export function getMarkerIconAndColorFromAssetType(+ type: AssetDescriptor | string | undefined,+ configOverrideSettings?: OverrideConfigSettings+): {icon: string, color: string | undefined | AttributeMarkerColoursRange[]} | undefined {++ if (!type) {+ return;+ }++ const descriptor = typeof(type) === "string" ? AssetModelUtil.getAssetDescriptor(type) : type;++ let colourOverride: string | AttributeMarkerColoursRange[] | undefined;+ if (configOverrideSettings) {+ if (configOverrideSettings.markerConfig && descriptor) {+ const markerConfig = configOverrideSettings.markerConfig;+ const attrVal = configOverrideSettings.attributeValue;+ if (markerConfig.type === 'range' && attrVal) {+ const ranges = markerConfig.ranges;+ // see in what range the attrVal fits and if not, what the setting for the highest range is+ const colourFromRange = ranges.find(r => attrVal < r.max) || ranges.reduce((a, b) => (a.max > b.max) ? a : b);+ colourOverride = colourFromRange.colour || undefined;+ } else if (markerConfig.type === 'boolean') {+ const value = attrVal ? 'true' : 'false';+ colourOverride = markerConfig[value] || undefined;+ }+ }+ // todo icon override+ }++ const icon = descriptor && descriptor.icon ? descriptor.icon : "help-circle";++ let color: string | undefined | AttributeMarkerColoursRange[];+ if (colourOverride) {+ color = colourOverride as AttributeMarkerColoursRange[];+ } else if (descriptor && descriptor.colour) {+ color = descriptor.colour as string;+ }++ return {+ color: color,+ icon: icon+ };+}