Skip to content

Commit

Permalink
[knx] Refactor manufacturer map (openhab#15297)
Browse files Browse the repository at this point in the history
* [knx] Refactor manufacturer map

This makes adding new manufacturers easy.

Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
  • Loading branch information
J-N-K authored and austvik committed Mar 27, 2024
1 parent 5e64d03 commit 2373357
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 354 deletions.
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

0 comments on commit 2373357

Please sign in to comment.