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
UI updates: Map marker bug fixes
Rich Turner3ebbb0d6f9f6
diff --git a/ui/app/manager/src/pages/page-map.ts b/ui/app/manager/src/pages/page-map.tsindex 2ecf37823..a7eefefcd 100644--- a/ui/app/manager/src/pages/page-map.ts+++ b/ui/app/manager/src/pages/page-map.ts@@ -1,6 +1,5 @@import {css, html} from "lit";-import {customElement, property, query} from "lit/decorators.js";-import {until} from 'lit/directives/until.js';+import {customElement, property, query, state} from "lit/decorators.js";import {createSlice, EnhancedStore, PayloadAction} from "@reduxjs/toolkit";import "@openremote/or-map";import {@@ -11,7 +10,7 @@ import {OrMapMarkerAsset,OrMapMarkerClickedEvent,OrMapGeocoderChangeEvent,- MapMarkerConfig+ MapMarkerAssetConfig} from "@openremote/or-map";import manager, {Util} from "@openremote/core";import {createSelector} from "reselect";@@ -65,9 +64,12 @@ const pageMapSlice = createSlice({return state;},- attributeEventReceived(state: MapState, action: PayloadAction<AttributeEvent>) {+ attributeEventReceived(state: MapState, action: PayloadAction<[string[], AttributeEvent]>) {const assets = state.assets;- const assetId = action.payload.attributeState.ref.id;+ const attrsOfInterest = action.payload[0];+ const attrEvent = action.payload[1];+ const attrName = attrEvent.attributeState.ref.name;+ const assetId = attrEvent.attributeState.ref.id;const index = assets.findIndex((asst) => asst.id === assetId);const asset = index >= 0 ? assets[index] : null;@@ -75,14 +77,19 @@ const pageMapSlice = createSlice({return state;}- if (action.payload.attributeState.deleted) {+ if (attrName === WellknownAttributes.LOCATION && attrEvent.attributeState.deleted) {return {...state,assets: [...assets.splice(index, 1)]};}- assets[index] = Util.updateAsset({...asset}, action.payload);+ // Only react if attribute is an attribute of interest+ if (!attrsOfInterest.includes(attrName)) {+ return;+ }++ assets[index] = Util.updateAsset({...asset}, attrEvent);return state;},setAssets(state, action: PayloadAction<Asset[]>) {@@ -97,12 +104,10 @@ const pageMapSlice = createSlice({const {assetEventReceived, attributeEventReceived, setAssets} = pageMapSlice.actions;export const pageMapReducer = pageMapSlice.reducer;--export interface PageMapConfig {card?: MapAssetCardConfig,assetQuery?: AssetQuery,- markers?: MapMarkerConfig+ markers?: MapMarkerAssetConfig}export function pageMapProvider(store: EnhancedStore<MapStateKeyed>, config?: PageMapConfig): PageProvider<MapStateKeyed> {@@ -164,10 +169,10 @@ export class PageMap extends Page<MapStateKeyed> {@query("#map")protected _map?: OrMap;- @property()+ @state()protected _assets: Asset[] = [];- @property()+ @state()protected _currentAsset?: Asset;protected _assetSelector = (state: MapStateKeyed) => state.map.assets;@@ -177,15 +182,32 @@ export class PageMap extends Page<MapStateKeyed> {protected assetSubscriptionId: string;protected attributeSubscriptionId: string;+ protected getAttributesOfInterest(): (string | WellknownAttributes)[] {+ // Extract all label attributes configured in marker config+ let markerLabelAttributes = [];++ if (this.config && this.config.markers) {+ markerLabelAttributes = Object.values(this.config.markers)+ .filter(assetTypeMarkerConfig => assetTypeMarkerConfig.showLabel && assetTypeMarkerConfig.attributeName)+ .map(assetTypeMarkerConfig => assetTypeMarkerConfig.attributeName);+ }++ return [+ ...markerLabelAttributes,+ WellknownAttributes.LOCATION,+ WellknownAttributes.DIRECTION+ ];+ }+protected subscribeAssets = async (realm: string) => {let response: GenericAxiosResponse<Asset[]>;-+ const attrsOfInterest = this.getAttributesOfInterest();const assetQuery: AssetQuery = this.config && this.config.assetQuery ? this.config.assetQuery : {tenant: {realm: realm},select: {- attributes: [WellknownAttributes.LOCATION],+ attributes: attrsOfInterest,},attributes: {items: [@@ -243,7 +265,7 @@ export class PageMap extends Page<MapStateKeyed> {this.assetSubscriptionId = assetSubscriptionId;const attributeSubscriptionId = await manager.events.subscribeAttributeEvents(undefined, false, (event) => {- this._store.dispatch(attributeEventReceived(event));+ this._store.dispatch(attributeEventReceived([attrsOfInterest, event]));});if (!this.isConnected || realm !== this._realmSelector(this.getState())) {@@ -334,21 +356,7 @@ export class PageMap extends Page<MapStateKeyed> {return !attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr);}).map(asset => {return html`- ${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>- `;- })- )};+ <or-map-marker-asset ?active="${this._currentAsset && this._currentAsset.id === asset.id}" .asset="${asset}" .config="${this.config.markers}"></or-map-marker-asset>`;})}diff --git a/ui/app/manager/webpack.config.js b/ui/app/manager/webpack.config.jsindex 8577939a7..9acfd3ad8 100644--- a/ui/app/manager/webpack.config.js+++ b/ui/app/manager/webpack.config.js@@ -7,6 +7,7 @@ module.exports = (env, argv) => {const customConfigDir = env.config;const managerUrl = env.managerUrl;const keycloakUrl = env.keycloakUrl;+ const port = env.port;const IS_DEV_SERVER = !!process.argv.find(arg => arg.includes("serve"));if (IS_DEV_SERVER) {@@ -18,7 +19,7 @@ module.exports = (env, argv) => {console.log("");}- const config = util.getAppConfig(argv.mode, IS_DEV_SERVER, __dirname, managerUrl, keycloakUrl);+ const config = util.getAppConfig(argv.mode, IS_DEV_SERVER, __dirname, managerUrl, keycloakUrl, port);if (IS_DEV_SERVER) {console.log("CUSTOM_CONFIG_DIR: " + customConfigDir || "");diff --git a/ui/component/or-map/src/index.ts b/ui/component/or-map/src/index.tsindex 181c4dc9b..2be87a193 100644--- a/ui/component/or-map/src/index.ts+++ b/ui/component/or-map/src/index.ts@@ -599,6 +599,3 @@ export class OrMap extends LitElement {});}}--export * from "./markers/or-map-marker";-export * from "./markers/or-map-marker-asset";diff --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 bd4561ed9..9cf97d1e4 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@@ -5,33 +5,35 @@ import {Asset,AssetEvent,AssetEventCause,+ AssetModelUtil,AttributeEvent,GeoJSONPoint,- ReadAttributeEvent,SharedEvent,- WellknownAttributes,- AssetModelUtil+ WellknownAttributes} from "@openremote/model";import manager, {subscribe, Util} from "@openremote/core";-import { getMarkerIconAndColorFromAssetType } from "../util";+import {getMarkerIconAndColorFromAssetType, OverrideConfigSettings} from "../util";-export type BaseMapMarkerConfig = {+export type MapMarkerConfig = {attributeName: string;showLabel?: boolean;showUnits?: boolean;- showDirection?: boolean;+ hideDirection?: boolean;+ colours?: MapMarkerColours;}-export type MapMarkerConfig = {- [assetType: string]: AttributeMarkerColours[] | RangeAttributeMarkerColours[];+export type MapMarkerColours = AttributeMarkerColours | RangeAttributeMarkerColours;++export type MapMarkerAssetConfig = {+ [assetType: string]: MapMarkerConfig}-export type AttributeMarkerColours = BaseMapMarkerConfig & {+export type AttributeMarkerColours = {type: "string" | "boolean";[value: string]: string;}-export type RangeAttributeMarkerColours = BaseMapMarkerConfig & {+export type RangeAttributeMarkerColours = {type: "range";ranges: AttributeMarkerColoursRange[];}@@ -41,6 +43,24 @@ export type AttributeMarkerColoursRange = {colour: string;}+export function getMarkerConfigForAssetType(config: MapMarkerAssetConfig | undefined, assetType: string | undefined): MapMarkerConfig | undefined {+ if (!config || !assetType || !config[assetType]) {+ return;+ }++ return config[assetType];+}++export function getMarkerConfigLabelAttributeName(config: MapMarkerAssetConfig | undefined, assetType: string | undefined): string | undefined {+ const assetTypeConfig = getMarkerConfigForAssetType(config, assetType);++ if (!assetTypeConfig) {+ return;+ }++ return assetTypeConfig.showLabel ? assetTypeConfig.attributeName : undefined;+}+@customElement("or-map-marker-asset")export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {@@ -51,7 +71,7 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {public asset?: Asset;@property()- public config?: MapMarkerConfig;+ public config?: MapMarkerAssetConfig;public assetTypeAsIcon: boolean = true;@@ -64,13 +84,15 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {protected set type(type: string | undefined) {- let overrideOpts;- if (this.config && type && this.config[type]) {- const markerConfig = this.config[type][0] || undefined;+ let overrideOpts: OverrideConfigSettings | undefined;+ const assetTypeConfig = getMarkerConfigForAssetType(this.config, type);- if (this.displayValue) {- overrideOpts = {markerConfig: markerConfig, attributeValue: this.displayValue};- }+ if (assetTypeConfig && assetTypeConfig.attributeName && this.asset && this.asset.attributes && this.asset.attributes[assetTypeConfig.attributeName] && assetTypeConfig.colours) {+ const currentValue = this.asset.attributes[assetTypeConfig.attributeName].value;+ overrideOpts = {+ markerConfig: assetTypeConfig.colours,+ currentValue: currentValue+ };}const iconAndColour = getMarkerIconAndColorFromAssetType(type, overrideOpts);@@ -95,6 +117,8 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {this.lat = undefined;this.lng = undefined;this.type = undefined;+ this.direction = undefined;+ this.displayValue = undefined;this.assetIds = this.assetId && this.assetId.length > 0 ? [this.assetId] : undefined;if (Object.keys(_changedProperties).length === 1) {@@ -103,12 +127,21 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {}if (_changedProperties.has("asset")) {- this.onAssetChanged(this.asset);+ try {+ this.onAssetChanged(this.asset);+ } catch (e) {+ console.error(e);+ }}return super.shouldUpdate(_changedProperties);}+ /**+ * This will only get called when assetId is set; if asset is set then it is expected that attribute changes are+ * handled outside this component and the asset should be replaced when attributes change that require the marker+ * to re-render+ */public _onEvent(event: SharedEvent) {if (event.eventType === "attribute") {const attributeEvent = event as AttributeEvent;@@ -144,36 +177,35 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {protected async onAssetChanged(asset?: Asset) {if (asset) {+ this.direction = undefined;+ this.displayValue = undefined;+const attr = asset.attributes ? asset.attributes[WellknownAttributes.LOCATION] : undefined;this._updateLocation(attr ? attr.value as GeoJSONPoint : null);- if (asset.attributes && asset.attributes[WellknownAttributes.DIRECTION]) {- const directionVal = await this.getAttrValue(asset, WellknownAttributes.DIRECTION);- if (directionVal !== undefined) {- this.direction = directionVal.toString();- }- }-- if (this.config && asset.type && this.config[asset.type]) {- const assetTypeConfig = this.config[asset.type][0] || undefined;+ const assetTypeConfig = getMarkerConfigForAssetType(this.config, asset.type);+ const showDirection = !assetTypeConfig || !assetTypeConfig.hideDirection;+ const showLabel = assetTypeConfig && assetTypeConfig.showLabel && !!assetTypeConfig.attributeName;- if (assetTypeConfig && assetTypeConfig.showLabel) {- if (assetTypeConfig.showLabel) {- const attrVal = await this.getAttrValue(asset, assetTypeConfig.attributeName);- if (attrVal === undefined) return;+ if (showLabel && asset.attributes && asset.attributes[assetTypeConfig?.attributeName]) {+ const attrVal = asset.attributes[assetTypeConfig?.attributeName].value;+ if (attrVal !== undefined && attrVal !== null) {+ this.displayValue = attrVal.toString();- 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.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;+ if (showDirection) {+ if (asset.attributes && asset.attributes[WellknownAttributes.DIRECTION]) {+ const directionVal = asset.attributes[WellknownAttributes.DIRECTION].value as number;+ if (directionVal !== undefined && directionVal !== null) {+ this.direction = directionVal.toString();+ }}}@@ -183,19 +215,6 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {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/or-map-asset-card.ts b/ui/component/or-map/src/or-map-asset-card.tsindex 68c050913..141250db8 100644--- a/ui/component/or-map/src/or-map-asset-card.ts+++ b/ui/component/or-map/src/or-map-asset-card.ts@@ -23,7 +23,7 @@ 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";+import {MapMarkerConfig, getMarkerConfigLabelAttributeName, MapMarkerAssetConfig} from "./markers/or-map-marker-asset";export interface MapAssetCardTypeConfig {include?: string[];@@ -77,7 +77,7 @@ export class OrMapAssetCard extends subscribe(manager)(LitElement) {public config?: MapAssetCardConfig;@property({type: Object})- public markerconfig?: MapMarkerConfig;+ public markerconfig?: MapMarkerAssetConfig;@property({type: Boolean, attribute: true})public useAssetColor: boolean = true;@@ -154,6 +154,8 @@ export class OrMapAssetCard extends subscribe(manager)(LitElement) {&& (!attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr))).sort(Util.sortByString((listItem) => listItem.name!));+ const highlightedAttr = getMarkerConfigLabelAttributeName(this.markerconfig, this.asset.type);+return html`<div id="card-container" style="${styleStr}"><div id="header">@@ -168,7 +170,7 @@ export class OrMapAssetCard extends subscribe(manager)(LitElement) {if (descriptors && descriptors.length) {const label = Util.getAttributeLabel(attr, descriptors[0], this.asset.type, true);const value = Util.getAttributeValueAsString(attr, descriptors[0], this.asset.type, false, "-");- const classes = {highlighted: (this.markerconfig!! && this.markerconfig![this.asset.type] && attr.name === this.markerconfig![this.asset.type][0].attributeName)};+ const classes = {highlighted: highlightedAttr === attr.name};return html`<li class="${classMap(classes)}"><span class="attribute-name">${label}</span><span class="attribute-value">${value}</span></li>`;}})}diff --git a/ui/component/or-map/src/util.ts b/ui/component/or-map/src/util.tsindex d2a023e51..148b4bd4a 100644--- a/ui/component/or-map/src/util.ts+++ b/ui/component/or-map/src/util.ts@@ -1,6 +1,14 @@import {LngLat, LngLatBounds, LngLatBoundsLike, LngLatLike} from "maplibre-gl";-import {Asset, AssetDescriptor, AssetModelUtil, Attribute, GeoJSONPoint, ValueHolder, WellknownAttributes} from "@openremote/model";-import { AttributeMarkerColours, AttributeMarkerColoursRange, RangeAttributeMarkerColours } from "./markers/or-map-marker-asset";+import {+ Asset,+ AssetDescriptor,+ AssetModelUtil,+ Attribute,+ GeoJSONPoint,+ ValueHolder,+ WellknownAttributes+} from "@openremote/model";+import {AttributeMarkerColoursRange, MapMarkerColours} from "./markers/or-map-marker-asset";export function getLngLat(lngLatLike?: LngLatLike | Asset | ValueHolder<any> | GeoJSONPoint): { lng: number, lat: number } | undefined {if (!lngLatLike) {@@ -80,9 +88,9 @@ export function getLatLngBounds(lngLatBoundsLike?: LngLatBoundsLike): L.LatLngBo}}-interface OverrideConfigSettings {- markerConfig: AttributeMarkerColours | RangeAttributeMarkerColours;- attributeValue: number | string | boolean;+export interface OverrideConfigSettings {+ markerConfig: MapMarkerColours;+ currentValue: any;}export function getMarkerIconAndColorFromAssetType(@@ -95,36 +103,33 @@ export function getMarkerIconAndColorFromAssetType(}const descriptor = typeof(type) === "string" ? AssetModelUtil.getAssetDescriptor(type) : type;+ const icon = descriptor && descriptor.icon ? descriptor.icon : "help-circle";+ let colour = descriptor && descriptor.colour ? descriptor.colour : undefined;- 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;+ if (configOverrideSettings.markerConfig) {++ const colourConfig = configOverrideSettings.markerConfig;+ const attrVal = configOverrideSettings.currentValue;++ if (colourConfig.type === "range" && colourConfig.ranges && typeof attrVal === "number") {+ const ranges = colourConfig.ranges;// see in what range the attrVal fits and if not, what the setting for the highest range isconst 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;+ colour = colourFromRange.colour || undefined;+ } else if (colourConfig.type === "boolean") {+ const value = !!attrVal + "";+ colour = colourConfig[value];+ } else if (colourConfig.type === "string") {+ const value = attrVal + "";+ colour = colourConfig[value];}}// 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,+ color: colour,icon: icon};}diff --git a/ui/util/webpack.util.js b/ui/util/webpack.util.jsindex 1595975fb..de9c068c3 100644--- a/ui/util/webpack.util.js+++ b/ui/util/webpack.util.js@@ -41,8 +41,9 @@ function getStandardModuleRules() {};}-function getAppConfig(mode, isDevServer, dirname, managerUrl, keycloakUrl) {+function getAppConfig(mode, isDevServer, dirname, managerUrl, keycloakUrl, port) {const production = mode === "production";+ port = port || 9000;managerUrl = managerUrl || (production && !isDevServer ? undefined : "http://localhost:8080");const OUTPUT_PATH = isDevServer ? 'src' : 'dist';@@ -204,7 +205,7 @@ function getAppConfig(mode, isDevServer, dirname, managerUrl, keycloakUrl) {historyApiFallback: {index: "/" + dirname.split(path.sep).slice(-1)[0] + "/",},- port: 9000,+ port: port,open: false,hot: false, // HMR doesn't work with webcomponents at presentliveReload: true,