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
Add ChirpStack and TheThingsStack protocol implementations (#1880)
Wouter Born5b1da8ddfc52
diff --git a/agent/build.gradle b/agent/build.gradleindex 1ccfe8183..f83e252be 100644--- a/agent/build.gradle+++ b/agent/build.gradle@@ -45,6 +45,9 @@ dependencies {implementation "org.jdom:jdom2:$jdomVersion"+ implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-csv:$jacksonVersion"+ implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"+testImplementation 'org.junit.jupiter:junit-jupiter-api:5.13.4'testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.13.4'testRuntimeOnly "org.junit.platform:junit-platform-launcher"diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/AbstractLoRaWANProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/AbstractLoRaWANProtocol.javanew file mode 100644index 000000000..feb6380d9--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/AbstractLoRaWANProtocol.java@@ -0,0 +1,443 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan;++import com.fasterxml.jackson.dataformat.csv.CsvMapper;+import com.fasterxml.jackson.dataformat.csv.CsvSchema;+import org.openremote.agent.protocol.mqtt.MQTTAgent;+import org.openremote.agent.protocol.mqtt.MQTTAgentLink;+import org.openremote.agent.protocol.mqtt.MQTTProtocol;+import org.openremote.model.Container;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetTreeNode;+import org.openremote.model.asset.agent.Protocol;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.attribute.MetaItem;+import org.openremote.model.attribute.MetaMap;+import org.openremote.model.protocol.ProtocolAssetImport;+import org.openremote.model.protocol.ProtocolAssetService;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.ValueUtil;+import org.openremote.model.value.JsonPathFilter;+import org.openremote.model.value.RegexValueFilter;+import org.openremote.model.value.ValueFilter;+import org.openremote.model.value.ValueType;++import java.io.ByteArrayInputStream;+import java.io.IOException;+import java.lang.reflect.Constructor;+import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.Set;+import java.util.concurrent.CopyOnWriteArraySet;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Future;+import java.util.function.Consumer;+import java.util.logging.Level;+import java.util.logging.Logger;++import static org.openremote.agent.protocol.lorawan.LoRaWANAgent.APPLICATION_ID;+import static org.openremote.agent.protocol.lorawan.LoRaWANAgent.MQTT_HOST;+import static org.openremote.agent.protocol.lorawan.LoRaWANAgent.MQTT_PORT;+import static org.openremote.agent.protocol.lorawan.LoRaWANConstants.*;+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;+import static org.openremote.model.value.MetaItemType.AGENT_LINK;+import static org.openremote.model.value.MetaItemType.AGENT_LINK_CONFIG;+++public abstract class AbstractLoRaWANProtocol<S extends AbstractLoRaWANProtocol<S,T>, T extends LoRaWANAgent<T, S>> implements Protocol<T>, ProtocolAssetImport {++ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractLoRaWANProtocol.class);++ private ExecutorService executorService;+ private ProtocolAssetService assetService;+ private MQTTProtocol mqttProtocol;+ private MQTTAgent mqttAgent;+ private T agent;+ private Set<String> devEuiSet = new CopyOnWriteArraySet<>();+ protected Container container;++ public AbstractLoRaWANProtocol(T agent) {+ this.agent = agent;+ this.mqttAgent = new MQTTAgent(agent.getName());++ mqttAgent.setId(agent.getId());+ agent.getMqttHost().ifPresent(host -> mqttAgent.setHost(host));+ agent.getMqttPort().ifPresent(port -> mqttAgent.setPort(port));+ agent.getClientId().ifPresent(clientId -> mqttAgent.setClientId(clientId));+ agent.isSecureMode().ifPresent(secureMode -> mqttAgent.setSecureMode(secureMode));+ agent.getPublishQoS().ifPresent(publishQos -> mqttAgent.setPublishQos(publishQos));+ agent.getSubscribeQoS().ifPresent(subscribeQos -> mqttAgent.setSubscribeQos(subscribeQos));+ agent.getCertificateAlias().ifPresent(certificateAlias -> mqttAgent.setCertificateAlias(certificateAlias));+ agent.isResumeSession().ifPresent(resumeSession -> mqttAgent.setResumeSession(resumeSession));+ agent.isWebsocketMode().ifPresent(websocketMode -> mqttAgent.setWebsocketMode(websocketMode));+ agent.getWebsocketPath().ifPresent(websocketPath -> mqttAgent.setWebsocketPath(websocketPath));+ agent.getWebsocketQuery().ifPresent(websocketQuery -> mqttAgent.setWebsocketQuery(websocketQuery));+ agent.getLastWillTopic().ifPresent(lastWillTopic -> mqttAgent.setLastWillTopic(lastWillTopic));+ agent.getLastWillPayload().ifPresent(lastWillPayload -> mqttAgent.setLastWillPayload(lastWillPayload));+ agent.isLastWillRetain().ifPresent(lastWillRetain -> mqttAgent.setLastWillRetain(lastWillRetain));+ agent.getUsernamePassword().ifPresent(usernamePassword -> mqttAgent.setUsernamePassword(usernamePassword));+ mqttAgent.setWildcardSubscriptionTopics(createWildcardSubscriptionTopicList().toArray(new String[0]));++ this.mqttProtocol = new LoRaWANMQTTProtocol(mqttAgent);+ }++ @Override+ public Map<AttributeRef, Attribute<?>> getLinkedAttributes() {+ return mqttProtocol.getLinkedAttributes();+ }++ @Override+ public void linkAttribute(String assetId, Attribute<?> attribute) throws Exception {+ mqttProtocol.linkAttribute(assetId, attribute);++ if (ATTRIBUTE_NAME_DEV_EUI.equals(attribute.getName())) {+ attribute.getValue(String.class)+ .map(String::trim)+ .map(String::toUpperCase)+ .filter(s -> !s.isEmpty())+ .ifPresent(devEUI -> devEuiSet.add(devEUI));+ }+ }++ @Override+ public void unlinkAttribute(String assetId, Attribute<?> attribute) throws Exception {+ mqttProtocol.unlinkAttribute(assetId, attribute);++ if (ATTRIBUTE_NAME_DEV_EUI.equals(attribute.getName())) {+ attribute.getValue(String.class)+ .map(String::trim)+ .map(String::toUpperCase)+ .filter(s -> !s.isEmpty())+ .ifPresent(devEUI -> devEuiSet.remove(devEUI));+ }+ }++ @Override+ public void start(Container container) throws Exception {+ this.container = container;+ executorService = container.getExecutor();+ mqttProtocol.start(container);+ }++ @Override+ public void stop(Container container) throws Exception {+ mqttProtocol.stop(container);+ }++ @Override+ public T getAgent() {+ return agent;+ }++ @Override+ public void updateLinkedAttribute(AttributeRef attributeRef, Object value, long timestamp) {+ mqttProtocol.updateLinkedAttribute(attributeRef, value, timestamp);+ }++ @Override+ public void updateLinkedAttribute(AttributeRef attributeRef, Object value) {+ mqttProtocol.updateLinkedAttribute(attributeRef, value);+ }++ @Override+ public void setAssetService(ProtocolAssetService assetService) {+ this.assetService = assetService;+ mqttProtocol.setAssetService(assetService);+ }++ @Override+ public void processLinkedAttributeWrite(AttributeEvent event) {+ mqttProtocol.processLinkedAttributeWrite(event);+ }++ @Override+ public boolean onAgentAttributeChanged(AttributeEvent event) {+ if (MQTT_HOST.getName().equals(event.getName()) ||+ MQTT_PORT.getName().equals(event.getName()) ||+ APPLICATION_ID.getName().equals(event.getName()))+ {+ return true;+ }++ return mqttProtocol.onAgentAttributeChanged(event);+ }++ @Override+ public Future<Void> startAssetImport(byte[] fileData, Consumer<AssetTreeNode[]> assetConsumer) {+ return executorService.submit(() -> {+ if (!checkCSVImportPrerequisites()) {+ assetConsumer.accept(new AssetTreeNode[0]);+ return;+ }++ CsvMapper csvMapper = new CsvMapper();+ CsvSchema schema = CsvSchema.builder()+ .addColumn("devEUI")+ .addColumn("name")+ .addColumn("assetTypeName")+ .addColumn("vendor_id")+ .addColumn("model_id")+ .addColumn("firmwareVersion")+ .setUseHeader(false)+ .build();++ try (ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData)) {+ List<CsvRecord> csvEntries = csvMapper.readerFor(CsvRecord.class).with(schema).<CsvRecord>readValues(inputStream).readAll();++ AssetTreeNode[] assetTreeNodes = csvEntries.stream()+ .filter(record -> validateCsvRecord(record))+ .filter(record -> duplicateAssetCheck(record))+ .map(this::createAsset)+ .filter(Optional::isPresent)+ .map(Optional::get)+ .map(AssetTreeNode::new)+ .toArray(AssetTreeNode[]::new);++ assetConsumer.accept(assetTreeNodes);+ } catch (IOException e) {+ LOG.log(Level.WARNING, "CSV import failed", e);+ }+ }, null);+ }++ protected boolean checkCSVImportPrerequisites() {+ Optional<String> applicationId = getAgent().getApplicationId();+ boolean isOk = applicationId.map(id -> !id.trim().isEmpty()).orElse(false);+ if (!isOk) {+ LOG.log(Level.WARNING, "CSV import failed because agent attribute '" + APPLICATION_ID.getName() + "' is missing");+ }+ return isOk;+ }++ protected abstract List<String> createWildcardSubscriptionTopicList();+ protected abstract boolean configureMQTTSubscriptionTopic(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord);+ protected abstract boolean configureMQTTPublishTopic(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord);+ protected abstract boolean configureMQTTMessageMatchFilterAndPredicate(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord);+ protected abstract boolean configureMQTTWriteValueTemplate(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord);++ protected boolean configureMQTTValueFilter(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }+ getAgentConfigJsonPath(attribute).ifPresent(+ jsonPath -> agentLink.setValueFilters(new ValueFilter[] {new JsonPathFilter(jsonPath, true, false)})+ );+ getAgentConfigRegex(attribute).ifPresent(+ pattern -> agentLink.setValueFilters(new ValueFilter[] {new RegexValueFilter(pattern, false, false, 1, 0)})+ );+ return true;+ }++ protected boolean configureMQTTValueConverter(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }+ getAgentConfigValueConverter(attribute).ifPresent(converter -> agentLink.setValueConverter(converter));+ return true;+ }++ protected boolean configureMQTTWriteValueConverter(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }+ getAgentConfigWriteValueConverter(attribute).ifPresent(converter -> agentLink.setWriteValueConverter(converter));+ return true;+ }++ protected Optional<ValueType.ObjectMap> getAgentConfig(Attribute<?> attribute) {+ return Optional.ofNullable(attribute)+ .map(attr -> attr.getMeta())+ .flatMap(metaMap -> metaMap.get(AGENT_LINK_CONFIG))+ .flatMap(metItem -> metItem.getValue());+ }++ protected Optional<Integer> getAgentConfigUplinkPort(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_UPLINK_PORT))+ .filter(port -> port instanceof Integer)+ .map(port -> (Integer)port);+ }++ protected Optional<Integer> getAgentConfigDownlinkPort(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_DOWNLINK_PORT))+ .filter(port -> port instanceof Integer)+ .map(port -> (Integer)port);+ }++ protected Optional<String> getAgentConfigJsonPath(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_VALUE_FILTER_JSON_PATH))+ .filter(jsonPath -> jsonPath instanceof String)+ .map(jsonPath -> ((String) jsonPath).trim())+ .filter(jsonPath -> !jsonPath.isEmpty());+ }++ protected Optional<String> getAgentConfigRegex(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_VALUE_FILTER_REGEX))+ .filter(regex -> regex instanceof String)+ .map(regex -> ((String) regex).trim())+ .filter(regex -> !regex.isEmpty());+ }++ protected Optional<ValueType.ObjectMap> getAgentConfigValueConverter(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_VALUE_CONVERTER))+ .filter(valueConverter -> valueConverter instanceof ValueType.ObjectMap)+ .map(valueConverter -> (ValueType.ObjectMap) valueConverter);+ }++ protected Optional<ValueType.ObjectMap> getAgentConfigWriteValueConverter(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_WRITE_VALUE_CONVERTER))+ .filter(valueConverter -> valueConverter instanceof ValueType.ObjectMap)+ .map(valueConverter -> (ValueType.ObjectMap) valueConverter);+ }++ protected Optional<String> getAgentConfigWriteObjectValueTemplate(Attribute<?> attribute) {+ return getAgentConfig(attribute)+ .map(map -> map.get(AGENT_LINK_CONFIG_WRITE_OBJECT_VALUE_TEMPLATE))+ .filter(valueConverter -> valueConverter instanceof String)+ .map(valueConverter -> ((String) valueConverter).trim())+ .filter(valueConverter -> !valueConverter.isEmpty());+ }++ private boolean validateCsvRecord(CsvRecord record) {+ boolean isValid = false;+ if (record != null) {+ isValid = record.isValid();+ if (!isValid) {+ LOG.log(Level.WARNING, "CSV import skipped an invalid CSV record: " + record);+ }+ }+ return isValid;+ }++ private boolean duplicateAssetCheck(CsvRecord record) {+ if (record == null) {+ return false;+ }++ if (!duplicateAssetCheck(record.getDevEUI())) {+ LOG.log(Level.INFO, "CSV import skipped a CSV record because an asset already existed: " + record);+ return false;+ }++ return true;+ }++ private boolean duplicateAssetCheck(String devEUI) {+ return Optional.ofNullable(devEUI)+ .map(String::trim)+ .filter(s -> !s.isEmpty())+ .map(String::toUpperCase)+ .map(s -> !devEuiSet.contains(s))+ .orElse(false);+ }++ private Optional<Asset<?>> createAsset(CsvRecord csvRecord) {+ if (csvRecord == null) {+ return Optional.empty();+ }++ return Optional.ofNullable(csvRecord.getAssetTypeName())+ .flatMap(name -> resolveAssetClass(name, csvRecord))+ .map(clazz -> instantiateAsset(clazz, csvRecord))+ .flatMap(asset -> configureAsset(asset, csvRecord));+ }++ private Optional<Class<? extends Asset<?>>> resolveAssetClass(String simpleClassName, CsvRecord csvRecord) {+ if (simpleClassName == null || csvRecord == null) {+ return Optional.empty();+ }++ return ValueUtil.getAssetClass(simpleClassName)+ .or(() -> {+ LOG.log(Level.WARNING, "CSV import skipped a CSV record because of an invalid asset type name: " + csvRecord);+ return Optional.empty();+ });+ }++ private Asset<?> instantiateAsset(Class<?> clazz, CsvRecord csvRecord) {+ if (clazz == null || csvRecord == null) {+ return null;+ }++ Asset<?> asset = null;+ try {+ Constructor<?> constructor = clazz.getConstructor(String.class);+ asset = (Asset<?>) constructor.newInstance(csvRecord.getName());+ } catch (ReflectiveOperationException e) {+ LOG.log(Level.INFO, "CSV import failed to create asset for CSV record: " + csvRecord, e);+ }+ return asset;+ }++ private Optional<Asset<?>> configureAsset(Asset<?> asset, CsvRecord csvRecord) {+ if (asset == null || csvRecord == null) {+ return Optional.empty();+ }++ Optional.ofNullable(csvRecord.getDevEUI()).ifPresent(devEUI -> asset.getAttribute(ATTRIBUTE_NAME_DEV_EUI).ifPresent(attribute -> attribute.setValue(devEUI.toUpperCase())));+ Optional.ofNullable(csvRecord.getVendorId()).ifPresent(vendorId -> asset.getAttribute(ATTRIBUTE_NAME_VENDOR_ID).ifPresent(attribute -> attribute.setValue(vendorId)));+ Optional.ofNullable(csvRecord.getModelId()).ifPresent(modelId -> asset.getAttribute(ATTRIBUTE_NAME_MODEL_ID).ifPresent(attribute -> attribute.setValue(modelId)));+ Optional.ofNullable(csvRecord.getFirmwareVersion()).ifPresent(version -> asset.getAttribute(ATTRIBUTE_NAME_FIRMWARE_VERSION).ifPresent(attribute -> attribute.setValue(version)));++ boolean isOk = true;++ for (Map.Entry<String, Attribute<?>> entry : asset.getAttributes().entrySet()) {+ String name = entry.getKey();+ Attribute<?> attribute = entry.getValue();+ MQTTAgentLink agentLink = new MQTTAgentLink(agent.getId());++ Optional<String> jsonPath = getAgentConfigJsonPath(attribute);+ Optional<String> regex = getAgentConfigRegex(attribute);+ if (jsonPath.isPresent() || regex.isPresent()) {+ isOk = configureMQTTSubscriptionTopic(attribute, agentLink, csvRecord);+ }+ isOk = isOk && configureMQTTValueFilter(attribute, agentLink, csvRecord);+ isOk = isOk && configureMQTTMessageMatchFilterAndPredicate(attribute, agentLink, csvRecord);+ isOk = isOk && configureMQTTValueConverter(attribute, agentLink, csvRecord);+ Optional<Integer> downlinkPort = getAgentConfigDownlinkPort(attribute);+ if (downlinkPort.isPresent()) {+ isOk = isOk && configureMQTTPublishTopic(attribute, agentLink, csvRecord);+ isOk = isOk && configureMQTTWriteValueConverter(attribute, agentLink, csvRecord);+ isOk = isOk && configureMQTTWriteValueTemplate(attribute, agentLink, csvRecord);+ }++ if (getAgentConfig(attribute).isPresent() || ATTRIBUTE_NAME_DEV_EUI.equals(attribute.getName())) {+ attribute.addOrReplaceMeta(+ new MetaItem<>(AGENT_LINK, agentLink)+ );+ }++ MetaMap metaMap = attribute.getMeta();+ metaMap.remove(AGENT_LINK_CONFIG);+ attribute.setMeta(metaMap);+ }++ return isOk ? Optional.of(asset) : Optional.empty();+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/CsvRecord.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/CsvRecord.javanew file mode 100644index 000000000..fee682c61--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/CsvRecord.java@@ -0,0 +1,85 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan;++import com.fasterxml.jackson.annotation.JsonProperty;++import java.util.Optional;++public class CsvRecord {++ @JsonProperty("devEUI")+ private String devEUI;++ @JsonProperty("name")+ private String name;++ @JsonProperty("vendor_id")+ private String vendorId;++ @JsonProperty("model_id")+ private String modelId;++ @JsonProperty("firmwareVersion")+ private String firmwareVersion;++ @JsonProperty("assetTypeName")+ private String assetTypeName;++ public String getDevEUI() { return devEUI; }+ public void setDevEUI(String devEUI) { this.devEUI = devEUI; }++ public String getName() {+ if (name == null || name.trim().isEmpty()) {+ return Optional.ofNullable(devEUI).orElse("");+ }+ return name;+ }+ public void setName(String name) { this.name = name; }++ public String getVendorId() { return vendorId; }+ public void setVendorId(String vendorId) { this.vendorId = vendorId; }++ public String getModelId() { return modelId; }+ public void setModelId(String modelId) { this.modelId = modelId; }++ public String getFirmwareVersion() { return firmwareVersion; }+ public void setFirmwareVersion(String firmwareVersion) { this.firmwareVersion = firmwareVersion; }++ public String getAssetTypeName() { return assetTypeName; }+ public void setAssetTypeName(String assetTypeName) { this.assetTypeName = assetTypeName; }++ @Override+ public String toString() {+ return "CsvRecord{" ++ "devEUI='" + (devEUI != null ? devEUI : "") + '\'' ++ ", name='" + (name != null ? name : "") + '\'' ++ ", vendorId='" + (vendorId != null ? vendorId : "") + '\'' ++ ", modelId='" + (modelId != null ? modelId : "") + '\'' ++ ", firmwareVersion='" + (firmwareVersion != null ? firmwareVersion : "") + '\'' ++ ", assetTypeName='" + (assetTypeName != null ? assetTypeName : "") + '\'' ++ '}';+ }++ public boolean isValid() {+ return devEUI != null && !devEUI.trim().isEmpty() &&+ assetTypeName != null && !assetTypeName.trim().isEmpty();+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANAgent.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANAgent.javanew file mode 100644index 000000000..bef9bf341--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANAgent.java@@ -0,0 +1,193 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan;++import jakarta.persistence.Entity;+import org.openremote.agent.protocol.mqtt.MQTTAgent;+import org.openremote.agent.protocol.mqtt.MQTTAgentLink;+import org.openremote.model.asset.agent.Agent;+import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.ValueType;++import java.util.Optional;++@Entity+public abstract class LoRaWANAgent<T extends LoRaWANAgent<T, U>, U extends AbstractLoRaWANProtocol<U, T>> extends Agent<T, U, MQTTAgentLink>{++ public static final AttributeDescriptor<String> MQTT_HOST = new AttributeDescriptor<>("MQTTHost", ValueType.HOSTNAME_OR_IP_ADDRESS).withOptional(false);+ public static final AttributeDescriptor<Integer> MQTT_PORT = new AttributeDescriptor<>("MQTTPort", ValueType.PORT).withOptional(false);+ public static final AttributeDescriptor<String> CLIENT_ID = MQTTAgent.CLIENT_ID;+ public static final AttributeDescriptor<Boolean> SECURE_MODE = MQTTAgent.SECURE_MODE;+ public static final AttributeDescriptor<Integer> PUBLISH_QOS = MQTTAgent.PUBLISH_QOS;+ public static final AttributeDescriptor<Integer> SUBSCRIBE_QOS = MQTTAgent.SUBSCRIBE_QOS;+ public static final AttributeDescriptor<String> CLIENT_CERTIFICATE_ALIAS = MQTTAgent.CLIENT_CERTIFICATE_ALIAS;+ public static final AttributeDescriptor<Boolean> RESUME_SESSION = MQTTAgent.RESUME_SESSION;+ public static final AttributeDescriptor<Boolean> WEBSOCKET_MODE = MQTTAgent.WEBSOCKET_MODE;+ public static final AttributeDescriptor<String> WEBSOCKET_PATH = MQTTAgent.WEBSOCKET_PATH;+ public static final AttributeDescriptor<String> WEBSOCKET_QUERY = MQTTAgent.WEBSOCKET_QUERY;+ public static final AttributeDescriptor<String> LAST_WILL_TOPIC = MQTTAgent.LAST_WILL_TOPIC;+ public static final AttributeDescriptor<String> LAST_WILL_PAYLOAD = MQTTAgent.LAST_WILL_PAYLOAD;+ public static final AttributeDescriptor<Boolean> LAST_WILL_RETAIN = MQTTAgent.LAST_WILL_RETAIN;++ public static final AttributeDescriptor<String> APPLICATION_ID = new AttributeDescriptor<>("applicationId", ValueType.TEXT);+++ // For Hydrators+ protected LoRaWANAgent() {}++ protected LoRaWANAgent(String name) {+ super(name);+ }++ public Optional<String> getMqttHost() {+ return getAttributes().getValue(MQTT_HOST);+ }++ public LoRaWANAgent<T, U> setMqttHost(String host) {+ getAttributes().getOrCreate(MQTT_HOST).setValue(host);+ return this;+ }++ public Optional<Integer> getMqttPort() {+ return getAttributes().getValue(MQTT_PORT);+ }++ public LoRaWANAgent<T, U> setMqttPort(Integer port) {+ getAttributes().getOrCreate(MQTT_PORT).setValue(port);+ return this;+ }++ public Optional<String> getClientId() {+ return getAttributes().getValue(CLIENT_ID);+ }++ public LoRaWANAgent<T, U> setClientId(String clientId) {+ getAttributes().getOrCreate(CLIENT_ID).setValue(clientId);+ return this;+ }++ public Optional<Boolean> isSecureMode() {+ return getAttributes().getValue(SECURE_MODE);+ }++ public LoRaWANAgent<T, U> setSecureMode(boolean secureMode) {+ getAttributes().getOrCreate(SECURE_MODE).setValue(secureMode);+ return this;+ }++ public Optional<String> getCertificateAlias() {+ return getAttributes().getValue(CLIENT_CERTIFICATE_ALIAS);+ }++ public LoRaWANAgent<T, U> setCertificateAlias(String certificateAlias) {+ getAttributes().getOrCreate(CLIENT_CERTIFICATE_ALIAS).setValue(certificateAlias);+ return this;+ }++ public Optional<Boolean> isWebsocketMode() {+ return getAttributes().getValue(WEBSOCKET_MODE);+ }++ public LoRaWANAgent<T, U> setWebsocketMode(boolean websocketMode) {+ getAttributes().getOrCreate(WEBSOCKET_MODE).setValue(websocketMode);+ return this;+ }++ public Optional<Boolean> isResumeSession() {+ return getAttributes().getValue(RESUME_SESSION);+ }++ public LoRaWANAgent<T, U> setResumeSession(boolean resumeSession) {+ getAttributes().getOrCreate(RESUME_SESSION).setValue(resumeSession);+ return this;+ }++ public Optional<String> getWebsocketPath() {+ return getAttributes().getValue(WEBSOCKET_PATH);+ }++ public LoRaWANAgent<T, U> setWebsocketPath(String websocketPath) {+ getAttributes().getOrCreate(WEBSOCKET_PATH).setValue(websocketPath);+ return this;+ }++ public Optional<String> getWebsocketQuery() {+ return getAttributes().getValue(WEBSOCKET_QUERY);+ }++ public LoRaWANAgent<T, U> setWebsocketQuery(String websocketQuery) {+ getAttributes().getOrCreate(WEBSOCKET_QUERY).setValue(websocketQuery);+ return this;+ }++ public Optional<String> getLastWillTopic() {+ return getAttributes().getValue(LAST_WILL_TOPIC);+ }++ public LoRaWANAgent<T, U> setLastWillTopic(String lastWillTopic) {+ getAttributes().getOrCreate(LAST_WILL_TOPIC).setValue(lastWillTopic);+ return this;+ }++ public Optional<String> getLastWillPayload() {+ return getAttributes().getValue(LAST_WILL_PAYLOAD);+ }++ public LoRaWANAgent<T, U> setLastWillPayload(String lastWillPayload) {+ getAttributes().getOrCreate(LAST_WILL_PAYLOAD).setValue(lastWillPayload);+ return this;+ }++ public Optional<Boolean> isLastWillRetain() {+ return getAttributes().getValue(LAST_WILL_RETAIN);+ }++ public LoRaWANAgent<T, U> setLastWillRetain(boolean lastWillRetain) {+ getAttributes().getOrCreate(LAST_WILL_RETAIN).setValue(lastWillRetain);+ return this;+ }++ public Optional<Integer> getPublishQoS() {+ return getAttributes().getValue(PUBLISH_QOS);+ }++ public LoRaWANAgent<T, U> setPublishQos(int publishQos) {+ getAttributes().getOrCreate(PUBLISH_QOS).setValue(publishQos);+ return this;+ }++ public Optional<Integer> getSubscribeQoS() {+ return getAttributes().getValue(SUBSCRIBE_QOS);+ }++ public LoRaWANAgent<T, U> setSubscribeQos(int subscribeQos) {+ getAttributes().getOrCreate(SUBSCRIBE_QOS).setValue(subscribeQos);+ return this;+ }++ public Optional<String> getApplicationId() {+ return getAttributes().getValue(APPLICATION_ID);+ }++ public LoRaWANAgent<T, U> setApplicationId(String applicationId) {+ getAttributes().getOrCreate(APPLICATION_ID).setValue(applicationId);+ return this;+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANConstants.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANConstants.javanew file mode 100644index 000000000..b4bdc12e1--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANConstants.java@@ -0,0 +1,35 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan;++public final class LoRaWANConstants {+ public static final String AGENT_LINK_CONFIG_UPLINK_PORT = "uplinkPort";+ public static final String AGENT_LINK_CONFIG_DOWNLINK_PORT = "downlinkPort";+ public static final String AGENT_LINK_CONFIG_VALUE_FILTER_JSON_PATH = "valueFilterJsonPath";+ public static final String AGENT_LINK_CONFIG_VALUE_FILTER_REGEX = "valueFilterRegex";+ public static final String AGENT_LINK_CONFIG_VALUE_CONVERTER = "valueConverter";+ public static final String AGENT_LINK_CONFIG_WRITE_VALUE_CONVERTER = "writeValueConverter";+ public static final String AGENT_LINK_CONFIG_WRITE_OBJECT_VALUE_TEMPLATE = "writeObjectValueTemplate";++ public static final String ATTRIBUTE_NAME_DEV_EUI = "devEUI";+ public static final String ATTRIBUTE_NAME_VENDOR_ID = "vendorId";+ public static final String ATTRIBUTE_NAME_MODEL_ID = "modelId";+ public static final String ATTRIBUTE_NAME_FIRMWARE_VERSION= "firmwareVersion";+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANMQTTProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANMQTTProtocol.javanew file mode 100644index 000000000..6582a19ae--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/LoRaWANMQTTProtocol.java@@ -0,0 +1,29 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan;++import org.openremote.agent.protocol.mqtt.MQTTAgent;+import org.openremote.agent.protocol.mqtt.MQTTProtocol;++public class LoRaWANMQTTProtocol extends MQTTProtocol {+ protected LoRaWANMQTTProtocol(MQTTAgent agent) {+ super(agent);+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/chirpstack/ChirpStackAgent.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/chirpstack/ChirpStackAgent.javanew file mode 100644index 000000000..2c7175030--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/chirpstack/ChirpStackAgent.java@@ -0,0 +1,48 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan.chirpstack;++import jakarta.persistence.Entity;+import org.openremote.agent.protocol.lorawan.LoRaWANAgent;+import org.openremote.agent.protocol.mqtt.MQTTAgentLink;+import org.openremote.model.asset.agent.AgentDescriptor;++@Entity+public class ChirpStackAgent extends LoRaWANAgent<ChirpStackAgent, ChirpStackProtocol> {++ public static final AgentDescriptor<ChirpStackAgent, ChirpStackProtocol, MQTTAgentLink> DESCRIPTOR = new AgentDescriptor<>(+ ChirpStackAgent.class, ChirpStackProtocol.class, MQTTAgentLink.class+ );++ /**+ * For use by hydrators (i.e. JPA/Jackson)+ */+ protected ChirpStackAgent() {+ }++ public ChirpStackAgent(String name) {+ super(name);+ }++ @Override+ public ChirpStackProtocol getProtocolInstance() {+ return new ChirpStackProtocol(this);+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/chirpstack/ChirpStackProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/chirpstack/ChirpStackProtocol.javanew file mode 100644index 000000000..c727420ab--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/chirpstack/ChirpStackProtocol.java@@ -0,0 +1,128 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan.chirpstack;++import org.openremote.agent.protocol.lorawan.AbstractLoRaWANProtocol;+import org.openremote.agent.protocol.lorawan.CsvRecord;+import org.openremote.agent.protocol.mqtt.MQTTAgentLink;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.query.filter.NumberPredicate;+import org.openremote.model.value.JsonPathFilter;+import org.openremote.model.value.ValueFilter;++import java.util.ArrayList;+import java.util.Collections;+import java.util.List;+import java.util.Optional;++public class ChirpStackProtocol extends AbstractLoRaWANProtocol<ChirpStackProtocol, ChirpStackAgent> {++ public static final String PROTOCOL_DISPLAY_NAME = "ChirpStack";++ public ChirpStackProtocol(ChirpStackAgent agent) {+ super(agent);+ }++ @Override+ public String getProtocolName() {+ return PROTOCOL_DISPLAY_NAME;+ }++ @Override+ public String getProtocolInstanceUri() {+ return "chirpstack://" + getAgent().getMqttHost().orElse("-") + ":"+ + getAgent().getMqttPort().map(p -> p.toString()).orElse("-");+ }++ @Override+ protected List<String> createWildcardSubscriptionTopicList() {+ return getAgent().getApplicationId()+ .map(applicationId -> Collections.singletonList("application/" + applicationId + "/device/+/event/up"))+ .orElse(new ArrayList<>());+ }++ @Override+ protected boolean configureMQTTSubscriptionTopic(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }++ Optional<String> applicationId = getAgent().getApplicationId().map(id -> id.trim());+ Optional<String> devEUI = Optional.ofNullable(csvRecord.getDevEUI()).map(eui -> eui.trim());++ if (applicationId.isPresent() && devEUI.isPresent()) {+ agentLink.setSubscriptionTopic("application/" + applicationId.get() + "/device/" + devEUI.get().toLowerCase() + "/event/up");+ }+ return true;+ }++ @Override+ protected boolean configureMQTTPublishTopic(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }++ Optional<String> applicationId = getAgent().getApplicationId().map(id -> id.trim());+ Optional<String> devEUI = Optional.ofNullable(csvRecord.getDevEUI()).map(eui -> eui.trim());++ if (applicationId.isPresent() && devEUI.isPresent()) {+ agentLink.setPublishTopic("application/" + applicationId.get() + "/device/" + devEUI.get().toLowerCase() + "/command/down");+ }+ return true;+ }++ @Override+ protected boolean configureMQTTMessageMatchFilterAndPredicate(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }+ getAgentConfigUplinkPort(attribute).ifPresent(port ->+ agentLink.setMessageMatchFilters(new ValueFilter[] {new JsonPathFilter("$.fPort", true, false)})+ .setMessageMatchPredicate(new NumberPredicate(port))+ );+ return true;+ }++ @Override+ protected boolean configureMQTTWriteValueTemplate(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }++ Optional<Integer> downlinkPort = getAgentConfigDownlinkPort(attribute);+ Optional<String> devEUI = Optional.ofNullable(csvRecord.getDevEUI()).map(eui -> eui.trim());+ Optional<String> objectTemplate = getAgentConfigWriteObjectValueTemplate(attribute);++ if (downlinkPort.isPresent() && devEUI.isPresent()) {+ String writeValue = "{" ++ "\n \"devEui\": \"" + devEUI.get() + "\"," ++ "\n \"confirmed\": true,";+ if (objectTemplate.isPresent()) {+ writeValue += "\n \"object\": {\n " + objectTemplate.get() + "\n },";+ } else {+ writeValue += "\n \"data\": \"%VALUE%\",";+ }+ writeValue += "\n \"fPort\": " + downlinkPort.get() + "\n}";++ agentLink.setWriteValue(writeValue);+ }+ return true;+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/tts/TheThingsStackAgent.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/tts/TheThingsStackAgent.javanew file mode 100644index 000000000..7dab287d2--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/tts/TheThingsStackAgent.java@@ -0,0 +1,76 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan.tts;++import jakarta.persistence.Entity;+import org.openremote.agent.protocol.lorawan.LoRaWANAgent;+import org.openremote.agent.protocol.mqtt.MQTTAgentLink;+import org.openremote.model.asset.agent.Agent;+import org.openremote.model.asset.agent.AgentDescriptor;+import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.ValueType;++import java.util.Optional;++@Entity+public class TheThingsStackAgent extends LoRaWANAgent<TheThingsStackAgent, TheThingsStackProtocol> {++ public static final AttributeDescriptor<String> HOST = Agent.HOST.withOptional(false);+ public static final AttributeDescriptor<Integer> PORT = Agent.PORT.withOptional(false);+ public static final AttributeDescriptor<String> TENANT_ID = new AttributeDescriptor<>("tenantId", ValueType.TEXT);+ public static final AttributeDescriptor<String> API_KEY = new AttributeDescriptor<>("apiKey", ValueType.TEXT);++ public static final AgentDescriptor<TheThingsStackAgent, TheThingsStackProtocol, MQTTAgentLink> DESCRIPTOR = new AgentDescriptor<>(+ TheThingsStackAgent.class, TheThingsStackProtocol.class, MQTTAgentLink.class+ );++ /**+ * For use by hydrators (i.e. JPA/Jackson)+ */+ protected TheThingsStackAgent() {+ }++ public TheThingsStackAgent(String name) {+ super(name);+ }++ @Override+ public TheThingsStackProtocol getProtocolInstance() {+ return new TheThingsStackProtocol(this);+ }++ public Optional<String> getTenantId() {+ return getAttributes().getValue(TENANT_ID);+ }++ public TheThingsStackAgent setTenantId(String tenantId) {+ getAttributes().getOrCreate(TENANT_ID).setValue(tenantId);+ return this;+ }++ public Optional<String> getApiKey() {+ return getAttributes().getValue(API_KEY);+ }++ public TheThingsStackAgent setApiKey(String apiKey) {+ getAttributes().getOrCreate(API_KEY).setValue(apiKey);+ return this;+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/lorawan/tts/TheThingsStackProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/lorawan/tts/TheThingsStackProtocol.javanew file mode 100644index 000000000..694a37d7d--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/lorawan/tts/TheThingsStackProtocol.java@@ -0,0 +1,313 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.lorawan.tts;++import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import org.openremote.agent.protocol.lorawan.AbstractLoRaWANProtocol;+import org.openremote.agent.protocol.lorawan.CsvRecord;+import org.openremote.agent.protocol.mqtt.MQTTAgentLink;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.query.filter.NumberPredicate;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.JsonPathFilter;+import org.openremote.model.value.ValueFilter;++import java.net.URI;+import java.net.http.HttpClient;+import java.net.http.HttpRequest;+import java.net.http.HttpResponse;+import java.util.AbstractMap;+import java.util.ArrayList;+import java.util.Collections;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.logging.Level;+import java.util.logging.Logger;++import static org.openremote.agent.protocol.lorawan.tts.TheThingsStackAgent.API_KEY;+import static org.openremote.agent.protocol.lorawan.tts.TheThingsStackAgent.HOST;+import static org.openremote.agent.protocol.lorawan.tts.TheThingsStackAgent.TENANT_ID;+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;+import static org.openremote.container.util.MapAccess.getBoolean;++public class TheThingsStackProtocol extends AbstractLoRaWANProtocol<TheThingsStackProtocol, TheThingsStackAgent> {++ public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, TheThingsStackProtocol.class);++ public static final String PROTOCOL_DISPLAY_NAME = "The Things Stack";+ public static final int GET_DEVICE_LIMIT = 1000;++ private Optional<Map<String, String>> devEuiToDeviceIdMap = Optional.empty();++ public TheThingsStackProtocol(TheThingsStackAgent agent) {+ super(agent);+ }++ public static final String THE_THINGS_STACK_TEST = "THE_THINGS_STACK_TEST";++ @Override+ public String getProtocolName() {+ return PROTOCOL_DISPLAY_NAME;+ }++ @Override+ public String getProtocolInstanceUri() {+ return "tts://" + getAgent().getHost().orElse("-") + ":"+ + getAgent().getPort().map(p -> p.toString()).orElse("-");+ }++ @Override+ public boolean onAgentAttributeChanged(AttributeEvent event) {+ if (TENANT_ID.getName().equals(event.getName())) {+ return true;+ }+ return super.onAgentAttributeChanged(event);+ }++ @Override+ protected boolean checkCSVImportPrerequisites() {+ boolean isOk = super.checkCSVImportPrerequisites();++ List<AbstractMap.SimpleEntry<AttributeDescriptor<String>, Optional<String>>> list = new ArrayList<>(3);+ list.add(new AbstractMap.SimpleEntry<>(HOST, getAgent().getHost()));+ list.add(new AbstractMap.SimpleEntry<>(TENANT_ID, getAgent().getTenantId()));+ list.add(new AbstractMap.SimpleEntry<>(API_KEY, getAgent().getApiKey()));++ for (AbstractMap.SimpleEntry<AttributeDescriptor<String>, Optional<String>> item : list) {+ if (!item.getValue().map(attrValue -> !attrValue.trim().isEmpty()).orElse(false)) {+ isOk = false;+ LOG.log(Level.WARNING, "CSV import failed because agent attribute '" + item.getKey().getName() + "' is missing");+ }+ }++ initializeDevEuiToDeviceIdMap();+ if(devEuiToDeviceIdMap.isEmpty()) {+ isOk = false;+ }++ return isOk;+ }++ @Override+ protected List<String> createWildcardSubscriptionTopicList() {+ Optional<String> tenantId = getAgent().getTenantId().map(id -> id.trim());+ Optional<String> applicationId = getAgent().getApplicationId().map(id -> id.trim());+ return applicationId+ .map(id -> Collections.singletonList(+ "v3/" + (tenantId.isPresent() ? (applicationId.get() + "@" + tenantId.get()) : applicationId.get()) + "/devices/+/up"))+ .orElse(new ArrayList<>());+ }++ @Override+ protected boolean configureMQTTSubscriptionTopic(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }++ boolean isOk = true;+ Optional<String> applicationId = getAgent().getApplicationId().map(id -> id.trim());+ Optional<String> apiKey = getAgent().getApiKey().map(key -> key.trim());+ Optional<String> tenantId = getAgent().getTenantId().map(id -> id.trim());+ Optional<String> devEUI = Optional.ofNullable(csvRecord.getDevEUI()).map(eui -> eui.trim());++ if (applicationId.isPresent() && devEUI.isPresent() && apiKey.isPresent()) {+ if (devEuiToDeviceIdMap.isEmpty()) {+ return false;+ }+ Optional<String> deviceId = devEuiToDeviceIdMap.flatMap(map -> Optional.ofNullable(map.get(devEUI.get())));+ if (deviceId.isPresent()) {+ agentLink.setSubscriptionTopic("v3/" + (tenantId.isPresent() ? (applicationId.get() + "@" + tenantId.get()) : applicationId.get()) + "/devices/" + deviceId.get() + "/up");+ } else {+ LOG.log(+ Level.WARNING,+ "CSV import failure because couldn't find device on LoRaWAN network server: " + csvRecord+ );+ isOk = false;+ }+ }+ return isOk;+ }++ @Override+ protected boolean configureMQTTPublishTopic(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }++ boolean isOk = true;+ Optional<String> applicationId = getAgent().getApplicationId().map(id -> id.trim());+ Optional<String> apiKey = getAgent().getApiKey().map(key -> key.trim());+ Optional<String> tenantId = getAgent().getTenantId().map(id -> id.trim());+ Optional<String> devEUI = Optional.ofNullable(csvRecord.getDevEUI()).map(eui -> eui.trim());++ if (applicationId.isPresent() && devEUI.isPresent() && apiKey.isPresent()) {+ if (devEuiToDeviceIdMap.isEmpty()) {+ return false;+ }+ Optional<String> deviceId = devEuiToDeviceIdMap.flatMap(map -> Optional.ofNullable(map.get(devEUI.get())));+ if (deviceId.isPresent()) {+ agentLink.setPublishTopic("v3/" + (tenantId.isPresent() ? (applicationId.get() + "@" + tenantId.get()) : applicationId.get()) + "/devices/" + deviceId.get() + "/down/push");+ } else {+ LOG.log(+ Level.WARNING,+ "CSV import failure because couldn't find device on the LoRaWAN network server: " + csvRecord+ );+ isOk = false;+ }+ }++ return isOk;+ }++ @Override+ protected boolean configureMQTTMessageMatchFilterAndPredicate(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }+ getAgentConfigUplinkPort(attribute).ifPresent(port ->+ agentLink.setMessageMatchFilters(new ValueFilter[] {new JsonPathFilter("$.uplink_message.f_port", true, false)})+ .setMessageMatchPredicate(new NumberPredicate(port))+ );+ return true;+ }++ @Override+ protected boolean configureMQTTWriteValueTemplate(Attribute<?> attribute, MQTTAgentLink agentLink, CsvRecord csvRecord) {+ if (attribute == null || agentLink == null || csvRecord == null) {+ return false;+ }++ boolean isOk = true;+ Optional<Integer> downlinkPort = getAgentConfigDownlinkPort(attribute);++ if (downlinkPort.isPresent()) {+ ObjectMapper mapper = new ObjectMapper();++ Map<String, Object> downlink = new HashMap<>();+ downlink.put("f_port", downlinkPort.get());+ downlink.put("priority", "NORMAL");+ downlink.put("frm_payload", "%VALUE%");++ Map<String, Object> root = new HashMap<>();+ root.put("downlinks", Collections.singletonList(downlink));++ try {+ String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);+ agentLink.setWriteValue(json);+ } catch (JsonProcessingException e) {+ isOk = false;+ LOG.log(Level.SEVERE, "CSV import failure for CSV record: " + csvRecord, e);+ }+ }+ return isOk;+ }++ private void initializeDevEuiToDeviceIdMap() {+ Optional<String> host = getAgent().getHost().map(String::trim);+ Optional<Integer> port = getAgent().getPort();+ Optional<String> apiKey = getAgent().getApiKey().map(String::trim);+ Optional<String> applicationId = getAgent().getApplicationId().map(String::trim);++ if (host.isEmpty() || apiKey.isEmpty() || applicationId.isEmpty()) {+ return;+ }++ Map<String, String> map = new HashMap<>();++ try {+ String hostWithPort = port+ .map(p -> String.format("%s:%d", host.get(), p))+ .orElse(host.get());++ HttpClient client = HttpClient.newHttpClient();+ ObjectMapper mapper = new ObjectMapper();++ int page = 1;++ while (true) {+ String url = String.format(+ "%s://%s/api/v3/applications/%s/devices?field_mask=name,ids&limit=%d&page=%d",+ getTTSApiScheme(), hostWithPort, applicationId.get(), GET_DEVICE_LIMIT, page+ );++ HttpRequest request = HttpRequest.newBuilder()+ .uri(URI.create(url))+ .header("Authorization", "Bearer " + apiKey.get())+ .header("Accept", "application/json")+ .GET()+ .build();++ HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());++ if (response.statusCode() != 200) {+ LOG.log(+ Level.WARNING,+ "CSV import failure because couldn't retrieve device list from LoRaWAN server [HTTP " ++ response.statusCode() + "]: " + response.body()+ );+ break;+ }++ JsonNode root = mapper.readTree(response.body());+ JsonNode devices = root.path("end_devices");++ if (!devices.isArray() || devices.isEmpty()) {+ break;+ }++ for (JsonNode device : devices) {+ JsonNode ids = device.path("ids");+ String devEui = ids.path("dev_eui").asText();+ String deviceId = ids.path("device_id").asText();++ if (!devEui.isEmpty()) {+ map.put(devEui.toLowerCase(), deviceId);+ }+ }++ if (devices.size() < GET_DEVICE_LIMIT) {+ break;+ }++ page++;+ }++ this.devEuiToDeviceIdMap = Optional.of(map);+ } catch (Exception e) {+ LOG.log(+ Level.WARNING,+ "CSV import failure because couldn't retrieve device list from the LoRaWAN server", e+ );+ }+ }++ private String getTTSApiScheme() {+ return Optional.ofNullable(container)+ .map(c -> getBoolean(c.getConfig(), THE_THINGS_STACK_TEST, false))+ .map(isTest -> isTest ? "http" : "https")+ .orElse("https");+ }+}diff --git a/gradle.properties b/gradle.propertiesindex c9b159403..a5fb4adee 100644--- a/gradle.properties+++ b/gradle.properties@@ -70,3 +70,4 @@ jdomVersion = 2.0.6.1jSchVersion = 2.27.3bouncyCastleVersion = 1.81victoolsJsonSchemaGeneratorVersion = 4.38.0+moquetteVersion = 0.18.0diff --git a/model/src/main/java/org/openremote/model/value/MetaItemType.java b/model/src/main/java/org/openremote/model/value/MetaItemType.javaindex 7ecae9918..6bbfbf607 100644--- a/model/src/main/java/org/openremote/model/value/MetaItemType.java+++ b/model/src/main/java/org/openremote/model/value/MetaItemType.java@@ -24,6 +24,7 @@ import org.openremote.model.asset.agent.AgentLink;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeLink;import org.openremote.model.util.TsIgnore;+import org.openremote.model.value.ValueType.ObjectMap;import java.util.Date;@@ -39,6 +40,12 @@ public final class MetaItemType {*/public static final MetaItemDescriptor<AgentLink> AGENT_LINK = new MetaItemDescriptor<>("agentLink", ValueType.VALUE_AGENT_LINK);+ /**+ * Stores {@link #AGENT_LINK} configuration data. This meta item is useful for protocol implementations that require+ * additional information at the time of asset instantiation for configuring agent links.+ */+ public static final MetaItemDescriptor<ObjectMap> AGENT_LINK_CONFIG = new MetaItemDescriptor<>("agentLinkConfig", ValueType.JSON_OBJECT);+/*** Links the attribute to another attribute, so an attribute event on the attribute triggers the same attribute* event on the linked attribute.diff --git a/setup/src/integration/java/org/openremote/setup/integration/model/asset/ChirpStackTestAsset.java b/setup/src/integration/java/org/openremote/setup/integration/model/asset/ChirpStackTestAsset.javanew file mode 100644index 000000000..2833e8487--- /dev/null+++ b/setup/src/integration/java/org/openremote/setup/integration/model/asset/ChirpStackTestAsset.java@@ -0,0 +1,96 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.setup.integration.model.asset;++import jakarta.persistence.Entity;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetDescriptor;+import org.openremote.model.attribute.MetaItem;+import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.MetaItemType;+import org.openremote.model.value.ValueType;++import java.util.Map;+import java.util.Optional;++import static org.openremote.model.Constants.UNITS_CELSIUS;++@Entity+public class ChirpStackTestAsset extends Asset<ChirpStackTestAsset> {++ public static final AttributeDescriptor<Double> TEMPERATURE = new AttributeDescriptor<>("temperature", ValueType.NUMBER,+ new MetaItem<>(MetaItemType.READ_ONLY),+ new MetaItem<>(MetaItemType.AGENT_LINK_CONFIG, new ValueType.ObjectMap() {{+ putAll(Map.of(+ "uplinkPort", 2,+ "valueFilterJsonPath", "$.object.Temperature"+ ));+ }})+ ).withUnits(UNITS_CELSIUS);+ public static final AttributeDescriptor<Double> RELATIVE_HUMIDITY = new AttributeDescriptor<>("relativeHumidity", ValueType.POSITIVE_NUMBER,+ new MetaItem<>(MetaItemType.READ_ONLY),+ new MetaItem<>(MetaItemType.AGENT_LINK_CONFIG, new ValueType.ObjectMap() {{+ putAll(Map.of(+ "uplinkPort", 2,+ "valueFilterJsonPath", "$.object.Humidity"+ ));+ }})+ );+ public static final AttributeDescriptor<Boolean> SWITCH = new AttributeDescriptor<>("switch", ValueType.BOOLEAN,+ new MetaItem<>(MetaItemType.AGENT_LINK_CONFIG, new ValueType.ObjectMap() {{+ putAll(Map.of(+ "downlinkPort", 4,+ "writeValueConverter", new ValueType.ObjectMap() {{+ putAll(Map.of(+ "TRUE", "DAE=",+ "FALSE", "DAA="+ ));+ }}+ ));+ }})+ );++ public static final AttributeDescriptor<String> DEV_EUI = new AttributeDescriptor<>("devEUI", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<String> VENDOR_ID = new AttributeDescriptor<>("vendorId", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<String> MODEL_ID = new AttributeDescriptor<>("modelId", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<String> FIRMWARE_VERSION = new AttributeDescriptor<>("firmwareVersion", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<Boolean> SUPPORTS_CLASS_C = new AttributeDescriptor<>("supportsClassC", ValueType.BOOLEAN, new MetaItem<>(MetaItemType.READ_ONLY));++ public static final AssetDescriptor<ChirpStackTestAsset> DESCRIPTOR = new AssetDescriptor<>("molecule-co2", "f18546", ChirpStackTestAsset.class);++ /**+ * For use by hydrators (i.e. JPA/Jackson)+ */+ protected ChirpStackTestAsset() {+ }++ public ChirpStackTestAsset(String name) {+ super(name);+ }++ public Optional<Double> getTemperature() {+ return getAttributes().getValue(TEMPERATURE);+ }++ public Optional<Double> getRelativeHumidity() {+ return getAttributes().getValue(RELATIVE_HUMIDITY);+ }++}diff --git a/setup/src/integration/java/org/openremote/setup/integration/model/asset/TheThingsStackTestAsset.java b/setup/src/integration/java/org/openremote/setup/integration/model/asset/TheThingsStackTestAsset.javanew file mode 100644index 000000000..14144af9a--- /dev/null+++ b/setup/src/integration/java/org/openremote/setup/integration/model/asset/TheThingsStackTestAsset.java@@ -0,0 +1,96 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.setup.integration.model.asset;++import jakarta.persistence.Entity;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetDescriptor;+import org.openremote.model.attribute.MetaItem;+import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.MetaItemType;+import org.openremote.model.value.ValueType;++import java.util.Map;+import java.util.Optional;++import static org.openremote.model.Constants.UNITS_CELSIUS;++@Entity+public class TheThingsStackTestAsset extends Asset<TheThingsStackTestAsset> {++ public static final AttributeDescriptor<Double> TEMPERATURE = new AttributeDescriptor<>("temperature", ValueType.NUMBER,+ new MetaItem<>(MetaItemType.READ_ONLY),+ new MetaItem<>(MetaItemType.AGENT_LINK_CONFIG, new ValueType.ObjectMap() {{+ putAll(Map.of(+ "uplinkPort", 2,+ "valueFilterJsonPath", "$.uplink_message.decoded_payload.Temperature"+ ));+ }})+ ).withUnits(UNITS_CELSIUS);+ public static final AttributeDescriptor<Double> RELATIVE_HUMIDITY = new AttributeDescriptor<>("relativeHumidity", ValueType.POSITIVE_NUMBER,+ new MetaItem<>(MetaItemType.READ_ONLY),+ new MetaItem<>(MetaItemType.AGENT_LINK_CONFIG, new ValueType.ObjectMap() {{+ putAll(Map.of(+ "uplinkPort", 2,+ "valueFilterJsonPath", "$.uplink_message.decoded_payload.Humidity"+ ));+ }})+ );+ public static final AttributeDescriptor<Boolean> SWITCH = new AttributeDescriptor<>("switch", ValueType.BOOLEAN,+ new MetaItem<>(MetaItemType.AGENT_LINK_CONFIG, new ValueType.ObjectMap() {{+ putAll(Map.of(+ "downlinkPort", 4,+ "writeValueConverter", new ValueType.ObjectMap() {{+ putAll(Map.of(+ "TRUE", "DAE=",+ "FALSE", "DAA="+ ));+ }}+ ));+ }})+ );++ public static final AttributeDescriptor<String> DEV_EUI = new AttributeDescriptor<>("devEUI", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<String> VENDOR_ID = new AttributeDescriptor<>("vendorId", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<String> MODEL_ID = new AttributeDescriptor<>("modelId", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<String> FIRMWARE_VERSION = new AttributeDescriptor<>("firmwareVersion", ValueType.TEXT, new MetaItem<>(MetaItemType.READ_ONLY));+ public static final AttributeDescriptor<Boolean> SUPPORTS_CLASS_C = new AttributeDescriptor<>("supportsClassC", ValueType.BOOLEAN, new MetaItem<>(MetaItemType.READ_ONLY));++ public static final AssetDescriptor<TheThingsStackTestAsset> DESCRIPTOR = new AssetDescriptor<>("molecule-co2", "f18546", TheThingsStackTestAsset.class);++ /**+ * For use by hydrators (i.e. JPA/Jackson)+ */+ protected TheThingsStackTestAsset() {+ }++ public TheThingsStackTestAsset(String name) {+ super(name);+ }++ public Optional<Double> getTemperature() {+ return getAttributes().getValue(TEMPERATURE);+ }++ public Optional<Double> getRelativeHumidity() {+ return getAttributes().getValue(RELATIVE_HUMIDITY);+ }++}diff --git a/test/build.gradle b/test/build.gradleindex caa5479d5..654c4b1f3 100644--- a/test/build.gradle+++ b/test/build.gradle@@ -35,7 +35,8 @@ dependencies {testImplementation "net.solarnetwork.common:nifty-modbus-api:$niftyModbusVersion"testImplementation "net.solarnetwork.common:nifty-modbus-core:$niftyModbusVersion"-+ testImplementation "com.github.moquette-io:moquette:$moquetteVersion"+ testImplementation "com.hivemq:hivemq-mqtt-client:$hiveMQClientVersion"}tasks.withType(Test) {diff --git a/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovynew file mode 100644index 000000000..8aeb9cea2--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovy@@ -0,0 +1,208 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.test.protocol.lorawan.chirpstack++import com.hivemq.client.mqtt.MqttClient+import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient+import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish+import groovy.json.JsonOutput+import groovy.json.JsonSlurper+import io.moquette.broker.Server+import io.moquette.broker.config.MemoryConfig+import org.openremote.agent.protocol.lorawan.chirpstack.ChirpStackAgent+import org.openremote.agent.protocol.lorawan.chirpstack.ChirpStackProtocol+import org.openremote.manager.agent.AgentService+import org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.asset.AssetStorageService+import org.openremote.model.asset.AssetTreeNode+import org.openremote.model.asset.agent.Agent+import org.openremote.model.asset.agent.AgentResource+import org.openremote.model.asset.agent.ConnectionStatus+import org.openremote.model.attribute.AttributeEvent+import org.openremote.model.file.FileInfo+import org.openremote.test.ManagerContainerTrait+import spock.lang.Shared+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++import java.nio.file.Path+import java.util.concurrent.CopyOnWriteArrayList+import java.util.concurrent.TimeUnit++import static org.openremote.container.security.IdentityProvider.OR_ADMIN_PASSWORD+import static org.openremote.container.security.IdentityProvider.OR_ADMIN_PASSWORD_DEFAULT+import static org.openremote.container.util.MapAccess.getString+import static org.openremote.model.Constants.*+import static org.openremote.setup.integration.model.asset.ChirpStackTestAsset.*++class ChirpStackTest extends Specification implements ManagerContainerTrait {++ static final String APPLICATION_ID = "807036d9-9d96-4305-82c9-92f681a11908"+ static final String CLIENT_ID = "chirpstackAgentClientId"+ static final String DEV_EUI_1 = "1111111111111111"+ static final String DEV_EUI_2 = "2222222222222222"+ static final String ASSET_NAME_1 = "Test Asset 1"+ static final String ASSET_NAME_2 = "Test Asset 2"+ static final String VENDOR_ID = "dragino"+ static final String MODEL_ID = "lht65"+ static final String FIRMWARE_VERSION = "1.8"+ static final String HOST = "localhost"+ static final Integer UPLINK_PORT = 2+ static final Integer DOWNLINK_PORT = 4+ static final Double TEMPERATURE_VALUE = 20.4+ static final Double HUMIDITY_VALUE = 56.4++ @Shared+ int mqttBrokerPort++ @Shared+ Server mqttBroker++ @Shared+ Path tempDataDir++ @Shared+ Mqtt3AsyncClient mqttClient++ def setupSpec() {+ tempDataDir = File.createTempDir('moquette_data_', '').toPath()+ mqttBrokerPort = findEphemeralPort()+ def props = new Properties()+ props.setProperty('port', mqttBrokerPort.toString())+ props.setProperty('persistent_store', tempDataDir.resolve('moquette_store.mapdb').toString())+ def config = new MemoryConfig(props)+ mqttBroker = new Server()+ mqttBroker.startServer(config)++ mqttClient = MqttClient.builder()+ .useMqttVersion3()+ .identifier("testClientId")+ .serverHost(HOST)+ .serverPort(mqttBrokerPort)+ .buildAsync()+ mqttClient.connect().get(2, TimeUnit.SECONDS)+ }++ def cleanupSpec() {+ mqttClient?.disconnect()+ mqttBroker?.stopServer()+ tempDataDir?.deleteDir()+ }++ def "ChirpStack Integration Test"() {+ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)++ when: "the container starts"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def agentService = container.getService(AgentService.class)++ and: "a ChirpStack agent is created"+ def agent = new ChirpStackAgent("ChirpStackAgent")+ agent.setRealm(MASTER_REALM)+ agent.setMqttHost(HOST)+ agent.setMqttPort(mqttBrokerPort)+ agent.setClientId(CLIENT_ID)+ agent.setApplicationId(APPLICATION_ID)+ agent = assetStorageService.merge(agent)++ then: "the protocol instance for the agent should be created"+ conditions.eventually {+ assert agentService.getProtocolInstance(agent.id) != null+ assert ((ChirpStackProtocol)agentService.getProtocolInstance(agent.id)) != null+ }++ and: "the connection status should be CONNECTED"+ conditions.eventually {+ agent = assetStorageService.find(agent.id)+ agent.getAttribute(Agent.STATUS).get().getValue().get() == ConnectionStatus.CONNECTED+ }++ when: "an authenticated admin user"+ def accessToken = authenticate(+ container,+ MASTER_REALM,+ KEYCLOAK_CLIENT_ID,+ MASTER_REALM_ADMIN_USER,+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)+ ).token++ and: "the agent resource"+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AgentResource.class)++ and: "CSV import is executed"+ def csvContent = """\+ ${DEV_EUI_1},${ASSET_NAME_1},ChirpStackTestAsset,${VENDOR_ID},${MODEL_ID},${FIRMWARE_VERSION}+ ${DEV_EUI_2},${ASSET_NAME_2},ChirpStackTestAsset,${VENDOR_ID},${MODEL_ID},${FIRMWARE_VERSION}+ """.stripIndent()+ def fileInfo = new FileInfo("devices.csv", csvContent, false)+ AssetTreeNode[] assets = agentResource.doProtocolAssetImport(null, agent.getId(), null, fileInfo)++ then: "new assets should have been imported"+ assert assets != null+ assert assets.length == 2+ def node1 = assets.find {it.asset.getAttribute(DEV_EUI).map {DEV_EUI_1.equalsIgnoreCase(it.value.get())}.orElse(false)}+ def node2 = assets.find {it.asset.getAttribute(DEV_EUI).map {DEV_EUI_2.equalsIgnoreCase(it.value.get())}.orElse(false)}+ assert node1 != null+ assert node2 != null++ when: "the device sends a LoRaWAN uplink message"+ def asset1 = node1.asset+ def json = [+ fPort: UPLINK_PORT,+ object: [+ Temperature: TEMPERATURE_VALUE,+ Humidity: HUMIDITY_VALUE+ ]+ ]+ mqttClient.publishWith()+ .topic("application/${APPLICATION_ID}/device/${DEV_EUI_1}/event/up")+ .payload(JsonOutput.toJson(json).bytes)+ .send()++ then: "asset attribute values should be updated"+ conditions.eventually {+ def asset = assetStorageService.find(asset1.id)+ assert asset.getAttribute(TEMPERATURE).flatMap {it.value}.map {it == TEMPERATURE_VALUE}.orElse(false)+ assert asset.getAttribute(RELATIVE_HUMIDITY).flatMap {it.value}.map {it == HUMIDITY_VALUE}.orElse(false)+ }++ when: "an asset attribute value is written"+ def downlinkMessages = new CopyOnWriteArrayList<String>()+ mqttClient.subscribeWith()+ .topicFilter("application/${APPLICATION_ID}/device/${DEV_EUI_1}/command/down")+ .callback { Mqtt3Publish publish ->+ downlinkMessages.add(new String(publish.payloadAsBytes))+ }.send()+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, SWITCH, true))++ then: "a LoRaWAN downlink message should have been published"+ conditions.eventually {+ assert downlinkMessages.size() == 1+ def downlinkMessage = new JsonSlurper().parseText(downlinkMessages.get(0))+ assert downlinkMessage != null+ assert downlinkMessage.devEui == DEV_EUI_1+ assert downlinkMessage.fPort == DOWNLINK_PORT+ assert downlinkMessage.data == "DAE="+ }+ }+}diff --git a/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovynew file mode 100644index 000000000..0975add77--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovy@@ -0,0 +1,264 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.test.protocol.lorawan.tts++import com.hivemq.client.mqtt.MqttClient+import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient+import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish+import com.sun.net.httpserver.HttpExchange+import com.sun.net.httpserver.HttpHandler+import com.sun.net.httpserver.HttpServer+import groovy.json.JsonOutput+import groovy.json.JsonSlurper+import io.moquette.broker.Server+import io.moquette.broker.config.MemoryConfig+import org.openremote.agent.protocol.lorawan.tts.TheThingsStackAgent+import org.openremote.agent.protocol.lorawan.tts.TheThingsStackProtocol+import org.openremote.manager.agent.AgentService+import org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.asset.AssetStorageService+import org.openremote.model.asset.AssetTreeNode+import org.openremote.model.asset.agent.Agent+import org.openremote.model.asset.agent.AgentResource+import org.openremote.model.asset.agent.ConnectionStatus+import org.openremote.model.attribute.AttributeEvent+import org.openremote.model.file.FileInfo+import org.openremote.test.ManagerContainerTrait+import spock.lang.Shared+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++import java.nio.file.Path+import java.util.concurrent.CopyOnWriteArrayList+import java.util.concurrent.Executors+import java.util.concurrent.TimeUnit++import static org.openremote.agent.protocol.lorawan.tts.TheThingsStackProtocol.THE_THINGS_STACK_TEST+import static org.openremote.container.security.IdentityProvider.OR_ADMIN_PASSWORD+import static org.openremote.container.security.IdentityProvider.OR_ADMIN_PASSWORD_DEFAULT+import static org.openremote.container.util.MapAccess.getString+import static org.openremote.model.Constants.*+import static org.openremote.setup.integration.model.asset.TheThingsStackTestAsset.*++class TheThingsStackTest extends Specification implements ManagerContainerTrait {++ static final String TENANT_ID = "ttn"+ static final String API_KEY = "NNSXS.1234"+ static final String APPLICATION_ID = "test-app-id"+ static final String CLIENT_ID = "ttsAgentClientId"+ static final String DEV_EUI_1 = "1111111111111111"+ static final String DEV_EUI_2 = "2222222222222222"+ static final String DEVICE_ID_1 = "device_id_1"+ static final String DEVICE_ID_2 = "device_id_2"+ static final String ASSET_NAME_1 = "Test Asset 1"+ static final String ASSET_NAME_2 = "Test Asset 2"+ static final String VENDOR_ID = "dragino"+ static final String MODEL_ID = "lht65"+ static final String FIRMWARE_VERSION = "1.8"+ static final String HOST = "localhost"+ static final Integer UPLINK_PORT = 2+ static final Integer DOWNLINK_PORT = 4+ static final Double TEMPERATURE_VALUE = 20.4+ static final Double HUMIDITY_VALUE = 56.4++ @Shared+ int ttsServerPort++ @Shared+ HttpServer ttsServer++ @Shared+ int mqttBrokerPort++ @Shared+ Server mqttBroker++ @Shared+ Path tempDataDir++ @Shared+ Mqtt3AsyncClient mqttClient++ def setupSpec() {+ tempDataDir = File.createTempDir('moquette_data_', '').toPath()+ mqttBrokerPort = findEphemeralPort()+ def props = new Properties()+ props.setProperty('port', mqttBrokerPort.toString())+ props.setProperty('persistent_store', tempDataDir.resolve('moquette_store.mapdb').toString())+ def config = new MemoryConfig(props)+ mqttBroker = new Server()+ mqttBroker.startServer(config)++ mqttClient = MqttClient.builder()+ .useMqttVersion3()+ .identifier("testClientId")+ .serverHost(HOST)+ .serverPort(mqttBrokerPort)+ .buildAsync()+ mqttClient.connect().get(2, TimeUnit.SECONDS)++ ttsServerPort = findEphemeralPort()+ ttsServer = HttpServer.create(new InetSocketAddress(ttsServerPort), 0)+ ttsServer.createContext("/api/v3/applications/${APPLICATION_ID}/devices", new HttpHandler() {+ @Override+ void handle(HttpExchange exchange) throws IOException {+ String responseJson = """+ {+ "end_devices": [+ {+ "ids": {+ "dev_eui": "${DEV_EUI_1}",+ "device_id": "${DEVICE_ID_1}"+ }+ },+ {+ "ids": {+ "dev_eui": "${DEV_EUI_2}",+ "device_id": "${DEVICE_ID_2}"+ }+ }+ ]+ }+ """+ exchange.responseHeaders.add("Content-Type", "application/json")+ exchange.sendResponseHeaders(200, responseJson.getBytes().length)+ exchange.responseBody.withCloseable {+ it.write(responseJson.getBytes())+ }+ }+ })++ ttsServer.setExecutor(Executors.newSingleThreadExecutor())+ ttsServer.start()+ }++ def cleanupSpec() {+ ttsServer?.stop(0)+ mqttClient?.disconnect()+ mqttBroker?.stopServer()+ tempDataDir?.deleteDir()+ }++ def "TheThingsStack Integration Test"() {+ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)++ when: "the container starts"+ def container = startContainer(defaultConfig(), defaultServices())+ container.getConfig().put(THE_THINGS_STACK_TEST, true.toString())+ def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def agentService = container.getService(AgentService.class)++ and: "a TheThingsStack agent is created"+ def agent = new TheThingsStackAgent("TheThingsStackAgent")+ agent.setRealm(MASTER_REALM)+ agent.setHost(HOST)+ agent.setPort(ttsServerPort)+ agent.setMqttHost(HOST)+ agent.setMqttPort(mqttBrokerPort)+ agent.setClientId(CLIENT_ID)+ agent.setApplicationId(APPLICATION_ID)+ agent.setTenantId(TENANT_ID)+ agent.setApiKey(API_KEY)+ agent = assetStorageService.merge(agent)++ then: "the protocol instance for the agent should be created"+ conditions.eventually {+ assert agentService.getProtocolInstance(agent.id) != null+ assert ((TheThingsStackProtocol)agentService.getProtocolInstance(agent.id)) != null+ }++ and: "the connection status should be CONNECTED"+ conditions.eventually {+ agent = assetStorageService.find(agent.id)+ agent.getAttribute(Agent.STATUS).get().getValue().get() == ConnectionStatus.CONNECTED+ }++ when: "an authenticated admin user"+ def accessToken = authenticate(+ container,+ MASTER_REALM,+ KEYCLOAK_CLIENT_ID,+ MASTER_REALM_ADMIN_USER,+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)+ ).token++ and: "the agent resource"+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AgentResource.class)++ and: "CSV import is executed"+ def csvContent = """\+ ${DEV_EUI_1},${ASSET_NAME_1},TheThingsStackTestAsset,${VENDOR_ID},${MODEL_ID},${FIRMWARE_VERSION}+ ${DEV_EUI_2},${ASSET_NAME_2},TheThingsStackTestAsset,${VENDOR_ID},${MODEL_ID},${FIRMWARE_VERSION}+ """.stripIndent()+ def fileInfo = new FileInfo("devices.csv", csvContent, false)+ AssetTreeNode[] assets = agentResource.doProtocolAssetImport(null, agent.getId(), null, fileInfo)++ then: "new assets should have been imported"+ assert assets != null+ assert assets.length == 2+ def node1 = assets.find {it.asset.getAttribute(DEV_EUI).map {DEV_EUI_1.equalsIgnoreCase(it.value.get())}.orElse(false)}+ def node2 = assets.find {it.asset.getAttribute(DEV_EUI).map {DEV_EUI_2.equalsIgnoreCase(it.value.get())}.orElse(false)}+ assert node1 != null+ assert node2 != null++ when: "the device sends a LoRaWAN uplink message"+ def asset1 = node1.asset+ def json = [+ uplink_message: [+ f_port: UPLINK_PORT,+ decoded_payload: [+ Temperature: TEMPERATURE_VALUE,+ Humidity: HUMIDITY_VALUE+ ]+ ]+ ]+ mqttClient.publishWith()+ .topic("v3/${APPLICATION_ID}@${TENANT_ID}/devices/${DEVICE_ID_1}/up")+ .payload(JsonOutput.toJson(json).bytes)+ .send()++ then: "asset attribute values should be updated"+ conditions.eventually {+ def asset = assetStorageService.find(asset1.id)+ assert asset.getAttribute(TEMPERATURE).flatMap {it.value}.map {it == TEMPERATURE_VALUE}.orElse(false)+ assert asset.getAttribute(RELATIVE_HUMIDITY).flatMap {it.value}.map {it == HUMIDITY_VALUE}.orElse(false)+ }++ when: "an asset attribute value is written"+ def downlinkMessages = new CopyOnWriteArrayList<String>()+ mqttClient.subscribeWith()+ .topicFilter("v3/${APPLICATION_ID}@${TENANT_ID}/devices/${DEVICE_ID_1}/down/push")+ .callback { Mqtt3Publish publish ->+ downlinkMessages.add(new String(publish.payloadAsBytes))+ }.send()+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, SWITCH, true))++ then: "a LoRaWAN downlink message should have been published"+ conditions.eventually {+ assert downlinkMessages.size() == 1+ def downlinkMessage = new JsonSlurper().parseText(downlinkMessages.get(0))+ assert downlinkMessage != null+ assert downlinkMessage.downlinks[0].f_port == DOWNLINK_PORT+ assert downlinkMessage.downlinks[0].frm_payload == "DAE="+ }+ }+}