Skip to content

Commit

Permalink
Deduplicate dpt name method
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinowsky committed Feb 24, 2024
1 parent 8b4f420 commit 0e0e796
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 102 deletions.
27 changes: 2 additions & 25 deletions src/io/calimero/tools/BaosClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import io.calimero.KNXFormatException;
import io.calimero.KNXIllegalArgumentException;
import io.calimero.KNXTimeoutException;
import io.calimero.KnxRuntimeException;
import io.calimero.baos.BaosLink;
import io.calimero.baos.BaosLinkAdapter;
import io.calimero.baos.BaosService;
Expand Down Expand Up @@ -451,7 +450,7 @@ private Item<DatapointCommand> parseDatapointValue(final int dpId, final String[
case SetValue, SetValueAndSendOnBus -> {
final DPTXlator xlator;
if (isDpt(args[1])) {
final var dptId = fromDptName(args[1]);
final var dptId = Main.fromDptName(args[1]);
xlator = TranslatorTypes.createTranslator(dptId);
dpIdToDpt.put(dpId, xlator.getType());
xlator.setValue(args[2]);
Expand Down Expand Up @@ -507,28 +506,6 @@ private static String datapointHistoryState(final int state) {
};
}

private static String fromDptName(final String id) {
return switch (id) {
case "switch" -> "1.001";
case "bool" -> "1.002";
case "dimmer" -> "3.007";
case "blinds" -> "3.008";
case "string" -> "16.001";
case "temp" -> "9.001";
case "float", "float2" -> "9.002";
case "float4" -> "14.005";
case "ucount" -> "5.010";
case "int" -> "13.001";
case "angle" -> "5.003";
case "percent", "%" -> "5.001";
default -> {
if (!"-".equals(id) && !Character.isDigit(id.charAt(0)))
throw new KnxRuntimeException("unrecognized DPT '" + id + "'");
yield id;
}
};
}

private void issueBaosService() throws KNXException, InterruptedException
{
if (!options.containsKey("cmd"))
Expand Down Expand Up @@ -654,7 +631,7 @@ static ZonedDateTime parseDateTime(final CharSequence dateTime) {
private static boolean isDpt(final String s) {
if (s.startsWith("-"))
return false;
final var id = fromDptName(s);
final var id = Main.fromDptName(s);
final var regex = "[0-9][0-9]*\\.[0-9][0-9][0-9]";
return Pattern.matches(regex, id);
}
Expand Down
26 changes: 25 additions & 1 deletion src/io/calimero/tools/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Calimero 2 - A library for KNX network access
Copyright (c) 2010, 2023 B. Malinowsky
Copyright (c) 2010, 2024 B. Malinowsky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -74,6 +74,7 @@
import io.calimero.KNXException;
import io.calimero.KNXFormatException;
import io.calimero.KNXIllegalArgumentException;
import io.calimero.KnxRuntimeException;
import io.calimero.Settings;
import io.calimero.knxnetip.KNXnetIPConnection;
import io.calimero.knxnetip.SecureConnection;
Expand Down Expand Up @@ -697,6 +698,29 @@ private static byte[] fromHex(final String hex) {
return HexFormat.of().parseHex(hex);
}

static String fromDptName(final String id) {
return switch (id) {
case "switch" -> "1.001";
case "bool" -> "1.002";
case "dimmer" -> "3.007";
case "blinds" -> "3.008";
case "string" -> "16.001";
case "temp" -> "9.001";
case "float", "float2" -> "9.002";
case "float4" -> "14.005";
case "ucount" -> "5.010";
case "int" -> "13.001";
case "angle" -> "5.003";
case "percent", "%" -> "5.001";
default -> {
if (!"-".equals(id) && !Character.isDigit(id.charAt(0)))
throw new KnxRuntimeException("unrecognized DPT '" + id + "'");
yield id;
}
};
}


static final class ShutdownHandler extends Thread
{
private final Thread t = Thread.currentThread();
Expand Down
42 changes: 3 additions & 39 deletions src/io/calimero/tools/ProcComm.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import io.calimero.KNXFormatException;
import io.calimero.KNXIllegalArgumentException;
import io.calimero.KNXTimeoutException;
import io.calimero.KnxRuntimeException;
import io.calimero.LteHeeTag;
import io.calimero.Priority;
import io.calimero.SerialNumber;
Expand Down Expand Up @@ -411,7 +410,7 @@ else if (read || write) {
StateDP dp;

final GroupAddress ga = new GroupAddress(addr);
dp = new StateDP(ga, "tmp", 0, withDpt ? fromDptName(s[2]) : null);
dp = new StateDP(ga, "tmp", 0, withDpt ? Main.fromDptName(s[2]) : null);
if (withDpt && !s[2].equals("-")) {
datapoints.remove(dp);
datapoints.add(dp);
Expand Down Expand Up @@ -530,49 +529,14 @@ private String getDPT(final GroupAddress group)
final var datapoint = datapoints.get(group);
if (dpt == null)
return datapoint != null ? datapoint.getDPT() : null;
final var id = fromDptName(dpt);
final var id = Main.fromDptName(dpt);
if (datapoint != null)
datapoint.setDPT(0, id);
else
datapoints.add(new StateDP(group, "", 0, id));
return id;
}

private static String fromDptName(final String id)
{
if ("switch".equals(id))
return "1.001";
if ("bool".equals(id))
return "1.002";
if ("dimmer".equals(id))
return "3.007";
if ("blinds".equals(id))
return "3.008";
if ("string".equals(id))
return "16.001";
if ("temp".equals(id))
return "9.001";
if ("float".equals(id))
return "9.002";
if ("float2".equals(id))
return "9.002";
if ("float4".equals(id))
return "14.005";
if ("ucount".equals(id))
return "5.010";
if ("int".equals(id))
return "13.001";
if ("angle".equals(id))
return "5.003";
if ("percent".equals(id))
return "5.001";
if ("%".equals(id))
return "5.001";
if (!"-".equals(id) && !Character.isDigit(id.charAt(0)))
throw new KnxRuntimeException("unrecognized DPT '" + id + "'");
return id;
}

private void readWrite() throws KNXException, InterruptedException
{
if (options.containsKey("lte")) {
Expand Down Expand Up @@ -1033,7 +997,7 @@ private void setRfDeviceSettings() {
private static boolean isDpt(final String s) {
if (s.startsWith("-"))
return false;
final var id = fromDptName(s);
final var id = Main.fromDptName(s);
final var regex = "[0-9][0-9]*\\.[0-9][0-9][0-9]";
return Pattern.matches(regex, id);
}
Expand Down
39 changes: 2 additions & 37 deletions src/io/calimero/tools/TrafficMonitor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Calimero 2 - A library for KNX network access
Copyright (c) 2019, 2023 B. Malinowsky
Copyright (c) 2019, 2024 B. Malinowsky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -65,7 +65,6 @@
import io.calimero.KNXException;
import io.calimero.KNXFormatException;
import io.calimero.KNXIllegalArgumentException;
import io.calimero.KnxRuntimeException;
import io.calimero.SerialNumber;
import io.calimero.cemi.CEMILData;
import io.calimero.cemi.CEMILDataEx;
Expand Down Expand Up @@ -267,40 +266,6 @@ private String asString(final byte[] asdu, final int dptMainNumber, final String
return t.getValue();
}

private static String fromDptName(final String id) {
if ("switch".equals(id))
return "1.001";
if ("bool".equals(id))
return "1.002";
if ("dimmer".equals(id))
return "3.007";
if ("blinds".equals(id))
return "3.008";
if ("string".equals(id))
return "16.001";
if ("temp".equals(id))
return "9.001";
if ("float".equals(id))
return "9.002";
if ("float2".equals(id))
return "9.002";
if ("float4".equals(id))
return "14.005";
if ("ucount".equals(id))
return "5.010";
if ("int".equals(id))
return "13.001";
if ("angle".equals(id))
return "5.003";
if ("percent".equals(id))
return "5.001";
if ("%".equals(id))
return "5.001";
if (!"-".equals(id) && !Character.isDigit(id.charAt(0)))
throw new KnxRuntimeException("unrecognized DPT '" + id + "'");
return id;
}

private void onFrameEvent(final FrameEvent e) {
final var joiner = new StringJoiner(" ");
final var frame = e.getFrame();
Expand Down Expand Up @@ -397,7 +362,7 @@ private void runMonitorLoop() throws IOException, InterruptedException {
try {
try {
final var ga = new GroupAddress(cmd);
final var dpt = fromDptName(s[1]);
final var dpt = Main.fromDptName(s[1]);
final var dp = new StateDP(ga, "tmp", 0, dpt);
datapoints.remove(dp);
datapoints.add(dp);
Expand Down

0 comments on commit 0e0e796

Please sign in to comment.