Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[knx] performance and resource improvements #114

Merged
merged 2 commits into from
Jun 22, 2021
Merged
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,107 @@
*/
package org.smarthomej.binding.knx.internal.channel;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tuwien.auto.calimero.GroupAddress;
import tuwien.auto.calimero.KNXFormatException;

/**
* Data structure representing a single group address configuration within a channel configuration parameter.
* Data structure representing the content of a channel's group address configuration.
*
* @author Simon Kaufmann - initial contribution and API.
*
*/
@NonNullByDefault
public class GroupAddressConfiguration {
public static final Logger LOGGER = LoggerFactory.getLogger(GroupAddressConfiguration.class);

private static final Pattern PATTERN_GA_CONFIGURATION = Pattern.compile(
"^((?<dpt>[1-9][0-9]{0,2}\\.[0-9]{3,4}):)?(?<read>\\<)?(?<mainGA>[0-9]{1,5}(/[0-9]{1,4}){0,2})(?<listenGAs>(\\+(\\<?[0-9]{1,5}(/[0-9]{1,4}){0,2}))*)$");
private static final Pattern PATTERN_LISTEN_GA = Pattern
.compile("\\+((?<read>\\<)?(?<GA>[0-9]{1,5}(/[0-9]{1,4}){0,2}))");

private final @Nullable String dpt;
private final GroupAddress mainGA;
private final Set<GroupAddress> listenGAs;
private final Set<GroupAddress> readGAs;

private final String ga;
private final boolean read;
private GroupAddressConfiguration(@Nullable String dpt, GroupAddress mainGA, Set<GroupAddress> listenGAs,
Set<GroupAddress> readGAs) {
this.dpt = dpt;
this.mainGA = mainGA;
this.listenGAs = listenGAs;
this.readGAs = readGAs;
}

public @Nullable String getDPT() {
return dpt;
}

public GroupAddressConfiguration(String ga, boolean read) {
super();
this.ga = ga;
this.read = read;
public GroupAddress getMainGA() {
return mainGA;
}

/**
* The group address.
*
* @return the group address.
*/
public String getGA() {
return ga;
public Set<GroupAddress> getListenGAs() {
return listenGAs;
}

/**
* Denotes whether the group address is marked to be actively read from.
*
* @return {@code true} if read requests should be issued to this address
*/
public boolean isRead() {
return read;
public Set<GroupAddress> getReadGAs() {
return readGAs;
}

public static @Nullable GroupAddressConfiguration parse(@Nullable Object configuration) {
if (!(configuration instanceof String)) {
return null;
}

Matcher matcher = PATTERN_GA_CONFIGURATION.matcher(((String) configuration).replace(" ", ""));
if (matcher.matches()) {
// Listen GAs
String input = matcher.group("listenGAs");
Matcher m2 = PATTERN_LISTEN_GA.matcher(input);
Set<GroupAddress> listenGAs = new HashSet<>();
Set<GroupAddress> readGAs = new HashSet<>();
while (m2.find()) {
String ga = m2.group("GA");
try {
GroupAddress groupAddress = new GroupAddress(ga);
listenGAs.add(groupAddress);
if (m2.group("read") != null) {
readGAs.add(groupAddress);
}
} catch (KNXFormatException e) {
LOGGER.warn("Failed to create GroupAddress from {}", ga);
return null;
}
}

// Main GA
String mainGA = matcher.group("mainGA");
try {
GroupAddress groupAddress = new GroupAddress(mainGA);
listenGAs.add(groupAddress); // also listening to main GA
if (matcher.group("read") != null) {
readGAs.add(groupAddress); // also reading main GA
}
return new GroupAddressConfiguration(matcher.group("dpt"), groupAddress, listenGAs, readGAs);
} catch (KNXFormatException e) {
LOGGER.warn("Failed to create GroupAddress from {}", mainGA);
return null;
}

} else {
LOGGER.warn("Failed parsing channel configuration '{}'.", configuration);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
* Copyright (c) 2021 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.knx.internal.channel;

import static java.util.stream.Collectors.*;
import static org.smarthomej.binding.knx.internal.KNXBindingConstants.CONTROL_CHANNEL_TYPES;
import static org.smarthomej.binding.knx.internal.KNXBindingConstants.GA;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.knx.internal.client.InboundSpec;
import org.smarthomej.binding.knx.internal.client.OutboundSpec;
import org.smarthomej.binding.knx.internal.dpt.KNXCoreTypeMapper;

import tuwien.auto.calimero.GroupAddress;

/**
* Meta-data abstraction for the KNX channel configurations.
*
* @author Simon Kaufmann - initial contribution and API
* @author Jan N. Klug - refactored from type definition to channel instance
*
*/
@NonNullByDefault
public abstract class KNXChannel {
private final Logger logger = LoggerFactory.getLogger(KNXChannel.class);
private final Set<String> gaKeys;

private final Map<String, GroupAddressConfiguration> groupAddressConfigurations = new HashMap<>();
private final Set<GroupAddress> listenAddresses = new HashSet<>();
private final Set<GroupAddress> writeAddresses = new HashSet<>();
private final String channelType;
private final ChannelUID channelUID;
private final boolean isControl;

KNXChannel(Channel channel) {
this(Set.of(GA), channel);
}

KNXChannel(Set<String> gaKeys, Channel channel) {
this.gaKeys = gaKeys;

// this is safe because we already checked the presence of the ChannelTypeUID before
this.channelType = Objects.requireNonNull(channel.getChannelTypeUID()).getId();
this.channelUID = channel.getUID();
this.isControl = CONTROL_CHANNEL_TYPES.contains(channelType);

// build map of ChannelConfigurations and GA lists
Configuration configuration = channel.getConfiguration();
gaKeys.forEach(key -> {
GroupAddressConfiguration groupAddressConfiguration = GroupAddressConfiguration
.parse(configuration.get(key));
if (groupAddressConfiguration != null) {
groupAddressConfigurations.put(key, groupAddressConfiguration);
// store address configuration for re-use
listenAddresses.addAll(groupAddressConfiguration.getListenGAs());
writeAddresses.add(groupAddressConfiguration.getMainGA());
}
});
}

public String getChannelType() {
return channelType;
}

public ChannelUID getChannelUID() {
return channelUID;
}

public boolean isControl() {
return isControl;
}

public final Set<GroupAddress> getAllGroupAddresses() {
return listenAddresses;
}

public final Set<GroupAddress> getWriteAddresses() {
return writeAddresses;
}

public final @Nullable OutboundSpec getCommandSpec(Type command) {
logger.trace("getCommandSpec checking keys '{}' for command '{}' ({})", gaKeys, command, command.getClass());
for (Map.Entry<String, GroupAddressConfiguration> entry : groupAddressConfigurations.entrySet()) {
String dpt = Objects.requireNonNullElse(entry.getValue().getDPT(), getDefaultDPT(entry.getKey()));
Set<Class<? extends Type>> expectedTypeClass = KNXCoreTypeMapper.getAllowedTypes(dpt);
if (expectedTypeClass.contains(command.getClass())) {
logger.trace("getCommandSpec key '{}' has expectedTypeClass '{}', matching command '{}' and dpt '{}'",
entry.getKey(), expectedTypeClass, command, dpt);
return new WriteSpecImpl(entry.getValue(), dpt, command);
}
}
logger.trace("getCommandSpec no Spec found!");
return null;
}

public final List<InboundSpec> getReadSpec() {
return groupAddressConfigurations.entrySet().stream()
.map(entry -> new ReadRequestSpecImpl(entry.getValue(), getDefaultDPT(entry.getKey())))
.filter(spec -> !spec.getGroupAddresses().isEmpty()).collect(toList());
}

public final @Nullable InboundSpec getListenSpec(GroupAddress groupAddress) {
return groupAddressConfigurations.entrySet().stream()
.map(entry -> new ListenSpecImpl(entry.getValue(), getDefaultDPT(entry.getKey())))
.filter(spec -> spec.getGroupAddresses().contains(groupAddress)).findFirst().orElse(null);
}

public final @Nullable OutboundSpec getResponseSpec(GroupAddress groupAddress, Type value) {
return groupAddressConfigurations.entrySet().stream()
.map(entry -> new ReadResponseSpecImpl(entry.getValue(), getDefaultDPT(entry.getKey()), value))
.filter(spec -> spec.matchesDestination(groupAddress)).findFirst().orElse(null);
}

protected abstract String getDefaultDPT(String gaConfigKey);
}
Loading