Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
import jakarta.validation.constraints.Min;
import java.math.BigInteger;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.util.JSONSchemaUtil.JsonSchemaTemplateParameter;
import org.openremote.model.util.JSONSchemaUtil.JsonSchemaTitle;

public class ModbusAgentLink extends AgentLink<ModbusAgentLink> {

@JsonPropertyDescription(
"Modbus unit ID (1-255). Required for Serial, optional for TCP (defaults to 1)")
@JsonSchemaTitle("Unit ID")
@JsonSchemaTemplateParameter
Comment thread
Ekhorn marked this conversation as resolved.
Resolved
@Min(1) private Integer unitId;

@JsonPropertyDescription(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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
*/
package org.openremote.agent.protocol.modbus

import org.openremote.model.asset.agent.AgentLink
import org.openremote.model.util.ValueUtil
import spock.lang.Specification

class ModbusAgentLinkTest extends Specification {

def setupSpec() {
ValueUtil.initialise(null)
}

def "schema marks the unit id of a Modbus agent link as a template parameter"() {
when:
def properties = ValueUtil.getSchema(AgentLink).at("/definitions/ModbusAgentLink/properties")

then:
properties.at("/unitId/templateParameter").asBoolean()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
@TsIgnoreTypeParams
public abstract class AgentLink<T extends AgentLink<?>> implements Serializable {

@JsonSchemaTitle("Agent ID")
@JsonSchemaFormat("or-agent-id")
@JsonSchemaTemplateParameter
protected String id;

@JsonSchemaDescription(
Expand Down
14 changes: 14 additions & 0 deletions model/src/main/java/org/openremote/model/util/JSONSchemaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ public static ObjectNode getSuppliedNode(JsonSchemaSupplier annotation)
String supplier();
}

/**
* Marks a field whose value a configuration template asks for on import, such as the agent of an
* agent link. Only mark fields that the standard JSON Forms renderers can render, as the import
* renders them outside of their custom renderers.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface JsonSchemaTemplateParameter {
String keyword() default "templateParameter";

String value() default "true";
}

private static class CustomModule implements Module {

private final ObjectMapper mapper;
Expand Down Expand Up @@ -338,6 +351,7 @@ public String getDefinitionNameForKey(
applyFieldAnnotation(fieldScope, JsonSchemaFormat.class, attrs);
applyFieldAnnotation(fieldScope, JsonSchemaDefault.class, attrs);
applyFieldAnnotation(fieldScope, JsonSchemaExamples.class, attrs);
applyFieldAnnotation(fieldScope, JsonSchemaTemplateParameter.class, attrs);

String key = getCanonicalMemberKey(fieldScope);
if (!applyI18nAnnotation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.openremote.model.util

import com.fasterxml.jackson.annotation.*
import org.openremote.model.asset.agent.AgentLink
import org.openremote.model.util.JSONSchemaUtil.*
import org.openremote.model.value.ValueType
import org.reflections.Reflections
Expand Down Expand Up @@ -146,6 +147,7 @@ class JSONSchemaUtilTest extends Specification {
@JsonSchemaFormat("test")
@JsonSchemaDefault("false")
@JsonSchemaExamples(["test"])
@JsonSchemaTemplateParameter
public Boolean all
}

Expand Down Expand Up @@ -300,6 +302,15 @@ class JSONSchemaUtilTest extends Specification {
schemaMatchesSnapshot(ReflectedPolymorphicType)
}

def "schema marks the agent id of an agent link as a template parameter"() {
when:
def properties = ValueUtil.getSchema(AgentLink).at("/definitions/DefaultAgentLink/properties")

then:
properties.at("/id/templateParameter").asBoolean()
!properties.at("/type").has("templateParameter")
}

def "schemas do not use allOf"() {
expect:
reflections.getTypesAnnotatedWith(JsonTypeInfo).each { clazz ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"format" : "test",
"default" : false,
"examples" : [ "test" ],
"templateParameter" : true,
"title" : "test",
"description" : "test"
}
Expand Down
103 changes: 102 additions & 1 deletion ui/app/manager/test/configuration-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { expect, type Page } from "@openremote/test";
import { expect, type Locator, type Page } from "@openremote/test";
import type { Asset } from "@openremote/model";
import type { AssetViewer } from "../../../component/or-asset-viewer/test/fixtures";
import { test, userStatePath } from "./fixtures/manager.js";
import { agent } from "./fixtures/data/assets.js";
import {
configuredLight,
lightTemplate,
linkedLightTemplate,
partlyImportableTemplate,
pumpTemplate,
targetLight,
Expand Down Expand Up @@ -248,6 +250,105 @@ test("should go back to the file selection from a template that cannot be import
await expect(assetViewer.getConfigurationTemplateAttribute(matched, "Dimmer")).toBeVisible();
});

/**
* Continue from what an import would do to the parameters of its agent links.
* @param page The page showing the import
* @param assetViewer The asset viewer fixture
*/
async function continueToParameters(page: Page, assetViewer: AssetViewer) {
const matched = assetViewer.getConfigurationTemplateSection("Matched attributes");
await expect(assetViewer.getConfigurationTemplateAttribute(matched, "Power")).toBeVisible();
await page.getByRole("dialog").getByRole("button", { name: "Next", exact: true }).click();
}

/**
* Pick an agent for a parameter of the configuration template import.
* @param parameter The parameter
* @param agent The agent to pick
*/
async function pickAgent(parameter: Locator, agent: Asset) {
await parameter.getByLabel("Agent ID").click();
// A closing select still shows its options, so look them up in this parameter only
await parameter.getByRole("option", { name: `${agent.name} (${agent.id})` }).click();
}

/**
* @given A simulator agent and a light to import into are set up in the "smartcity" realm
* @when Importing a template whose agent links point at two agents of another instance
* @then An agent is asked for once per template agent, listing the attributes that share it
* @and Import is blocked, as the template's agents do not exist here
* @when Picking the simulator agent for both, importing and saving the light
* @then Every agent link of the light points at the simulator agent
*/
test("should ask for an agent for each agent of the template", async ({ page, manager, assetsPage, assetViewer }) => {
await manager.setup("smartcity", { assets: [agent, targetLight] });
const simulator = manager.assets.find(({ type }) => type === agent.type)!;
const light = manager.assets.find(({ name }) => name === targetLight.name)!;
await assetsPage.gotoAssetId("smartcity", light.id!, true);
await selectTemplateFile(page, assetViewer, "linked.json", JSON.stringify(linkedLightTemplate));
await continueToParameters(page, assetViewer);

const shared = assetViewer.getConfigurationTemplateParameter("Dimmer");
const single = assetViewer.getConfigurationTemplateParameter("Power");
await expect(shared).toContainText("White tone");
await expect(single).not.toContainText("Dimmer");
const importButton = page.getByRole("dialog").getByRole("button", { name: "Import", exact: true });
for (const parameter of [shared, single]) {
await expect(parameter.getByText("The template's agent does not exist here. Pick another one.")).toBeVisible();
}
await expect(importButton).toBeDisabled();

await pickAgent(shared, simulator);
await pickAgent(single, simulator);
await expect(importButton).toBeEnabled();
await importButton.click();
const saveButton = page.getByRole("button", { name: "Save" });
await saveButton.click();
await expect(saveButton).toBeDisabled();

const { data: saved } = await manager.api.AssetResource.get(light.id!, await manager.adminConfig());
for (const name of ["brightness", "colourTemperature", "onOff"]) {
expect(saved.attributes![name].meta!.agentLink).toMatchObject({ type: "SimulatorAgentLink", id: simulator.id });
}
});

/**
* @given A light to import into is set up in the "smartcity" realm, without a simulator agent
* @when Importing a template with simulator agent links
* @then Import is blocked, saying that an agent of the link type is needed
* @when A simulator agent is added elsewhere
* @then It can be picked and the template imported
*/
test("should block import until an agent of the link type exists", async ({
page,
manager,
assetsPage,
assetViewer,
}) => {
await manager.setup("smartcity", { assets: [targetLight] });
await assetsPage.gotoAssetId("smartcity", manager.assets[0].id!, true);
await selectTemplateFile(page, assetViewer, "linked.json", JSON.stringify(linkedLightTemplate));
await continueToParameters(page, assetViewer);

const parameters = [
assetViewer.getConfigurationTemplateParameter("Dimmer"),
assetViewer.getConfigurationTemplateParameter("Power"),
];
const importButton = page.getByRole("dialog").getByRole("button", { name: "Import", exact: true });
for (const parameter of parameters) {
await expect(parameter.getByText(/^There is no agent for a .+ yet/)).toBeVisible();
}
await expect(importButton).toBeDisabled();

await manager.setup("smartcity", { assets: [agent] });
const simulator = manager.assets.find(({ type }) => type === agent.type)!;
for (const parameter of parameters) {
await expect(parameter.getByText(/^There is no agent for a .+ yet/)).toHaveCount(0);
await pickAgent(parameter, simulator);
}
await expect(importButton).toBeEnabled();
});

/**
* @given An agent is set up in the "smartcity" realm
* @when Opening the agent in modify mode
Expand Down
23 changes: 23 additions & 0 deletions ui/app/manager/test/fixtures/data/configuration-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,26 @@ export const pumpTemplate = {
flowRate: { type: "number", meta: { label: "Flow rate" } },
},
};

/**
* A template whose agent links point at agents of another instance, as the links of the config template setup's pump
* do: Dimmer and White tone share an agent, Power uses another one.
*/
export const linkedLightTemplate = {
version: 1,
assetType: "LightAsset",
attributes: {
brightness: {
type: "positiveInteger",
meta: { label: "Dimmer", agentLink: { type: "SimulatorAgentLink", id: "templateAgentA00000001" } },
},
colourTemperature: {
type: "positiveInteger",
meta: { label: "White tone", agentLink: { type: "SimulatorAgentLink", id: "templateAgentA00000001" } },
},
onOff: {
type: "boolean",
meta: { label: "Power", agentLink: { type: "SimulatorAgentLink", id: "templateAgentB00000001" } },
},
},
};
10 changes: 10 additions & 0 deletions ui/app/shared/locales/ar/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@
},
"writeValueFilters": {
"description": ""
},
"id": {
"label": ""
}
}
}
Expand Down Expand Up @@ -577,6 +580,13 @@
}
}
}
},
"modbus": {
"ModbusAgentLink": {
"unitId": {
"label": ""
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions ui/app/shared/locales/cn/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@
},
"writeValueFilters": {
"description": ""
},
"id": {
"label": ""
}
}
}
Expand Down Expand Up @@ -626,6 +629,13 @@
}
}
}
},
"modbus": {
"ModbusAgentLink": {
"unitId": {
"label": ""
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions ui/app/shared/locales/cs/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,9 @@
},
"writeValueFilters": {
"description": "Podobné jako valueFilter, ale použije se na odchozí hodnoty, což umožňuje inverzní filtrování."
},
"id": {
"label": ""
}
}
}
Expand Down Expand Up @@ -1322,6 +1325,13 @@
}
}
}
},
"modbus": {
"ModbusAgentLink": {
"unitId": {
"label": ""
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions ui/app/shared/locales/de/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,9 @@
},
"writeValueFilters": {
"description": ""
},
"id": {
"label": ""
}
}
}
Expand Down Expand Up @@ -672,6 +675,13 @@
}
}
}
},
"modbus": {
"ModbusAgentLink": {
"unitId": {
"label": ""
}
}
}
}
}
Expand Down
Loading
Loading