Skip to content

armgit / core

Public Imported from GitHub
0

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

7.6 KB
Raw Blame
/*
 * 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 * as Util from "@openremote/core/lib/util";
import type { Asset, AssetTypeInfo } from "@openremote/model";

export const notes = { name: "notes", type: "text" };
export const location = { name: "location", type: "GEO_JSONPoint" };
export const commonAttrs = { notes, location };

const assets = [
  {
    name: "Battery",
    type: "ThingAsset",
    realm: "smartcity",
    attributes: {
      ...commonAttrs,
      energyLevel: { name: "energyLevel", type: "positiveNumber", meta: { readOnly: true } },
      power: { name: "power", type: "number", meta: { readOnly: false } },
      powerSetpoint: { name: "powerSetpoint", type: "number" },
      energyCapacity: { name: "energyCapacity", type: "positiveNumber" },
    },
  },
  {
    name: "Solar Panel",
    type: "ThingAsset",
    realm: "smartcity",
    attributes: {
      ...commonAttrs,
      panelPitch: { name: "panelPitch", type: "positiveInteger", meta: { readOnly: true } },
      power: { name: "power", type: "number", meta: { readOnly: false } },
      powerForecast: { name: "powerForecast", type: "number", meta: { readOnly: true } },
    },
  },
] as const;

export const preparedAssetsWithLocation = Object.values(
  Util.mergeObjects(
    structuredClone(assets),
    [
      {
        attributes: {
          location: { value: { type: "Point", coordinates: [4.482259693115793, 51.91756799273] } },
        },
      },
      {
        attributes: {
          location: { value: { type: "Point", coordinates: [4.4845127486877345, 51.917435642781214] } },
        },
      },
    ],
    true
  )
);

export const preparedAssetsForRules = Object.values(
  Util.mergeObjects(
    structuredClone(assets),
    [
      {
        attributes: {
          energyLevel: { meta: { ruleState: true, storeDataPoints: true } },
          power: { meta: { ruleState: true, storeDataPoints: true } },
        },
      },
      {
        attributes: {
          power: { meta: { ruleState: true, storeDataPoints: true } },
          powerForecast: { meta: { ruleState: true, storeDataPoints: true } },
        },
      },
    ],
    true
  )
);

export const preparedAssetsForInsights: Asset[] = [
  {
    name: "Energy Asset 1",
    type: "ThingAsset",
    realm: "smartcity",
    attributes: {
      ...commonAttrs,
      energyLevel: { name: "energyLevel", type: "number", meta: { storeDataPoints: true } },
      power: { name: "power", type: "number", meta: { storeDataPoints: true } },
    },
  },
  {
    name: "Energy Asset 2",
    type: "ThingAsset",
    realm: "smartcity",
    attributes: {
      ...commonAttrs,
      energyLevel: { name: "energyLevel", type: "number", meta: { storeDataPoints: true } },
      power: { name: "power", type: "number", meta: { storeDataPoints: true } },
    },
  },
];

/**
 * Asset with one predicted-datapoint attribute and one stored-datapoint
 * attribute, for tests that need to distinguish the two datapoint types.
 */
export const assetWithPredictedAndStoredDatapoints: Asset = {
  name: "Import Asset",
  type: "ThingAsset",
  realm: "smartcity",
  attributes: {
    ...commonAttrs,
    energyImportTotal: { name: "energyImportTotal", type: "number", meta: { hasPredictedDataPoints: true } },
    energyExportTotal: { name: "energyExportTotal", type: "number", meta: { storeDataPoints: true } },
  },
};

export type DefaultAssets = typeof assets;
export default assets;

export const thing: Asset = {
  name: "Thing",
  realm: "smartcity",
  type: "ThingAsset",
  attributes: { ...commonAttrs },
};

export const weather: Asset = {
  name: "Weather",
  realm: "smartcity",
  type: "ThingAsset",
  attributes: {
    ...commonAttrs,
    temperature: { name: "temperature", type: "number", meta: { readOnly: true } },
    humidity: { name: "humidity", type: "positiveInteger", meta: { readOnly: false } },
    rainfall: { name: "rainfall", type: "number" },
  },
};

export const agent: Asset = {
  name: "Simulator",
  realm: "smartcity",
  type: "SimulatorAgent",
  attributes: {
    ...commonAttrs,
    agentDisabled: { name: "agentDisabled", type: "boolean", meta: {} },
    agentStatus: { name: "agentStatus", type: "connectionStatus", meta: { readOnly: true } },
  },
};

export type BBox = { south: number; north: number; west: number; east: number };

const rotterdam: BBox = {
  south: 51.89,
  north: 51.99,
  west: 4.24,
  east: 4.51,
};

/**
 * Assigns location at the specified coordinates
 *
 * @param asset The asset to assign the location attribute
 * @param coordinates The `[lng, lat]` coordinate pair to use for the asset location
 * @default `[4.483890476199264, 51.91547581791727]` (Jufferkade)
 */
export function assignLocation(asset: Asset, coordinates = [4.483890476199264, 51.91547581791727]): Asset {
  asset.attributes ??= {};
  Object.assign(asset.attributes, {
    location: {
      name: "location",
      type: "GEO_JSONPoint",
      value: { type: "Point", coordinates },
      meta: {},
    },
  });

  return asset;
}

/**
 * Assigns a random location within the specified bounding box
 *
 * @param asset The asset to assign the location attribute
 * @param boundingBox The bounding box to add assets within
 * @default {@link rotterdam}
 */
export function assignRandomLocationInArea(asset: Asset, { south, north, east, west }: BBox = rotterdam): Asset {
  const y = randomBetween(south, north);
  const x = randomBetween(east, west);
  assignLocation(asset, [x, y]);
  return asset;
}

export function getAssetAt(assetInfos: AssetTypeInfo[], index = 0): Asset {
  const validAssetInfos = assetInfos.filter(
    ({ assetDescriptor }) => assetDescriptor?.name && assetDescriptor.descriptorType === "asset"
  );

  const info = validAssetInfos[index % validAssetInfos.length];

  const type = info.assetDescriptor!.name;
  const attributes = Object.fromEntries(
    Object.values(info?.attributeDescriptors ?? {}).map(({ name, type }) => [name, { name, type, meta: {} }])
  );

  return { type, name: type, attributes };
}

export function getAssetsForAllTypes(
  assetInfos: AssetTypeInfo[],
  options?: { realm?: string; limit?: number; bbox?: BBox }
): Asset[] {
  return Array.from({ length: options?.limit ?? assetInfos.length }).map((_, i) => {
    return {
      ...assignRandomLocationInArea(getAssetAt(assetInfos, i), options?.bbox),
      name: String(i),
      realm: options?.realm ?? "smartcity",
    };
  });
}

export function getAssetTypes(assets: Asset[]) {
  return assets.map(({ type }) => type!).filter((value, index, array) => array.indexOf(value) === index);
}

export function getAssetTypeColour(type: string, infos: AssetTypeInfo[]) {
  return infos.find(({ assetDescriptor }) => assetDescriptor?.name === type)?.assetDescriptor?.colour ?? "";
}

export const getRGBColor = (el: Element): string[] => window.getComputedStyle(el).color.match(/\d+/g)!;

export function rgbToHex(rgb: string[]) {
  return rgb?.map((i) => ("0" + parseInt(i).toString(16)).slice(-2)).join("");
}

function randomBetween(max: number, min: number) {
  return Math.random() * (max - min) + min;
}