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
/*
* Copyright 2026, OpenRemote Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { LngLat, LngLatBounds, type LngLatBoundsLike, type LngLatLike } from "maplibre-gl";
import {
type Asset,
type AssetDescriptor,
AssetModelUtil,
type Attribute,
type GeoJSONPoint,
type ValueHolder,
WellknownAttributes,
WellknownMetaItems,
} from "@openremote/model";
import type { AttributeMarkerColoursRange, MapMarkerColours } from "../markers/or-map-marker-asset";
import { Util } from "@openremote/core";
import type { AssetWithLocation } from "..";
let _compactFormatter: Intl.NumberFormat | undefined;
export function formatCount(n: number): string {
if (!_compactFormatter) {
_compactFormatter = new Intl.NumberFormat("en", {
notation: "compact",
compactDisplay: "short",
maximumFractionDigits: 1,
});
}
return _compactFormatter.format(n).toLowerCase();
}
export function metersToPixelsAtMaxZoom(meters: number, latitude: number) {
return meters / 0.075 / Math.cos((latitude * Math.PI) / 180);
}
export function getLngLat(
lngLatLike?: LngLatLike | Asset | ValueHolder<any> | GeoJSONPoint
): { lng: number; lat: number } | undefined {
if (!lngLatLike) {
return;
}
if (lngLatLike instanceof LngLat) {
return lngLatLike as LngLat;
}
if ((lngLatLike as LngLat).lat) {
if ((lngLatLike as any).lon) {
return { lng: (lngLatLike as any).lon, lat: (lngLatLike as LngLat).lat };
}
return { lng: (lngLatLike as LngLat).lng, lat: (lngLatLike as LngLat).lat };
}
if (Array.isArray(lngLatLike)) {
return { lng: (lngLatLike as number[])[0], lat: (lngLatLike as number[])[1] };
}
if (
(lngLatLike as GeoJSONPoint).coordinates &&
Array.isArray((lngLatLike as GeoJSONPoint).coordinates) &&
((lngLatLike as GeoJSONPoint).coordinates as number[]).length >= 2
) {
return getLngLat((lngLatLike as GeoJSONPoint).coordinates as LngLatLike);
}
if ((lngLatLike as Asset).attributes) {
// This is an asset
const locationAttribute = (lngLatLike as Asset).attributes![WellknownAttributes.LOCATION];
if (!locationAttribute) {
return;
}
if (locationAttribute.value) {
return getLngLat(locationAttribute.value);
}
return;
}
if ((lngLatLike as ValueHolder<any>).value) {
return getLngLat((lngLatLike as Attribute<any>).value as GeoJSONPoint);
}
}
export function getGeoJSONPoint(lngLat: LngLatLike | undefined): GeoJSONPoint | undefined {
if (!lngLat) {
return;
}
return Array.isArray(lngLat)
? { type: "Point", coordinates: lngLat as number[] }
: {
type: "Point",
coordinates: [(lngLat as any).hasOwnProperty("lng") ? (lngLat as LngLat).lng : (lngLat as any).lon, lngLat.lat],
};
}
export function getLngLatBounds(lngLatBoundsLike?: LngLatBoundsLike): LngLatBounds | undefined {
if (lngLatBoundsLike) {
if (lngLatBoundsLike instanceof LngLatBounds) {
return lngLatBoundsLike as LngLatBounds;
}
const arr = lngLatBoundsLike as any[];
if (arr.length === 2) {
const sw = getLngLat(arr[0]);
const ne = getLngLat(arr[1]);
if (sw && ne) {
return new LngLatBounds(sw, ne);
}
}
if (arr.length === 4) {
return new LngLatBounds([arr[0], arr[1], arr[2], arr[3]]);
}
}
}
export interface OverrideConfigSettings {
markerConfig: MapMarkerColours;
currentValue: any;
}
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;
const icon = descriptor && descriptor.icon ? descriptor.icon : "help-circle";
let colour = descriptor && descriptor.colour ? descriptor.colour : undefined;
if (configOverrideSettings) {
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 is
const colourFromRange =
ranges.sort((a, b) => b.min - a.min).find((r) => attrVal >= r.min) ||
ranges.reduce((a, b) => (b.min > a.min ? a : b));
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
}
return {
color: colour,
icon,
};
}
export function isAssetWithLocation(asset: Asset): asset is AssetWithLocation {
if (!asset?.attributes) return false;
const attr = asset.attributes[WellknownAttributes.LOCATION] as Attribute<GeoJSONPoint> | undefined;
return (
!!attr?.value?.coordinates &&
(!attr.meta ||
!(WellknownMetaItems.SHOWONDASHBOARD in attr.meta) ||
!!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr))
);
}