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] Refactor manufacturer map #15297

Merged
merged 2 commits into from
Jul 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
*/
package org.openhab.binding.knx.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;
Expand Down Expand Up @@ -106,4 +111,26 @@ public class KNXBindingConstants {
public static final String STOP_MOVE_GA = "stopMove";
public static final String SWITCH_GA = "switch";
public static final String UP_DOWN_GA = "upDown";

public static final Map<Integer, String> MANUFACTURER_MAP = readManufacturerMap();

private static Map<Integer, String> readManufacturerMap() {
ClassLoader classLoader = KNXBindingConstants.class.getClassLoader();
if (classLoader == null) {
return Map.of();
}

try (InputStream is = classLoader.getResourceAsStream("manufacturer.properties")) {
if (is == null) {
return Map.of();
}

Properties properties = new Properties();
properties.load(is);
return properties.entrySet().stream()
.collect(Collectors.toMap(e -> Integer.parseInt((String) e.getKey()), e -> (String) e.getValue()));
} catch (IOException e) {
return Map.of();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.knx.internal.handler.Manufacturer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -126,8 +125,8 @@ private Map<String, String> readDeviceProperties(IndividualAddress address) thro
OPERATION_TIMEOUT);
if ((elements == null ? 0 : toUnsigned(elements)) == 1) {
Thread.sleep(OPERATION_INTERVAL);
String manufacturerID = Manufacturer.getName(toUnsigned(getClient().readDeviceProperties(address,
DEVICE_OBJECT, PID.MANUFACTURER_ID, 1, 1, false, OPERATION_TIMEOUT)));
String manufacturerId = MANUFACTURER_MAP.getOrDefault(toUnsigned(getClient().readDeviceProperties(address,
DEVICE_OBJECT, PID.MANUFACTURER_ID, 1, 1, false, OPERATION_TIMEOUT)), "Unknown");

Thread.sleep(OPERATION_INTERVAL);
String serialNo = toHex(getClient().readDeviceProperties(address, DEVICE_OBJECT, PID.SERIAL_NUMBER, 1, 1,
Expand Down Expand Up @@ -260,7 +259,7 @@ private Map<String, String> readDeviceProperties(IndividualAddress address) thro
// allowed to fail, optional
}

ret.put(MANUFACTURER_NAME, manufacturerID);
ret.put(MANUFACTURER_NAME, manufacturerId);
if (serialNo != null) {
ret.put(MANUFACTURER_SERIAL_NO, serialNo);
}
Expand All @@ -272,7 +271,7 @@ private Map<String, String> readDeviceProperties(IndividualAddress address) thro
}
ret.put(MAX_APDU_LENGTH, maxApdu);
logger.debug("Identified device {} as {}, type {}, revision {}, serial number {}, max APDU {}", address,
manufacturerID, hardwareType, firmwareRevision, serialNo, maxApdu);
manufacturerId, hardwareType, firmwareRevision, serialNo, maxApdu);
} else {
logger.debug("The KNX device with address {} does not expose a Device Object", address);
}
Expand Down
Loading