Skip to content

Commit

Permalink
Add DPTs 21.1200 and 21.1201
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinowsky committed Mar 30, 2024
1 parent e9542a0 commit 5701cf9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
50 changes: 49 additions & 1 deletion src/io/calimero/dptxlator/DptXlator8BitSet.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) 2017, 2023 B. Malinowsky
Copyright (c) 2017, 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 @@ -50,6 +50,7 @@
* <li>HVAC</li>
* <li>Lighting</li>
* <li>System</li>
* <li>Metering</li>
* </ul>
* The KNX data type width is 1 byte. The default return value after creation is 0 for the defined bit array.
* <p>
Expand Down Expand Up @@ -162,6 +163,37 @@ public enum ChannelActivationState implements EnumBase<ChannelActivationState> {
public static final EnumDpt<ChannelActivationState> DptChannelActivation = new EnumDpt<>("21.1010",
ChannelActivationState.class);

// Metering

public enum VirtualDryContact implements EnumBase<VirtualDryContact> {
Status1, Status2, Status3, Status4, Status5, Status6, Status7, Status8
}
/**
* DPT 21.1200 Virtual Dry Contact.<br>
* Status bits are encoded as follows:
* <ul>
* <li>0: virtual contact open</li>
* <li>1: virtual contact closed</li>
* </ul>
*/
public static final EnumDpt<VirtualDryContact> DptVirtualDryContact = new EnumDpt<>("21.1200",
VirtualDryContact.class);

public enum PhasesStatus implements EnumBase<PhasesStatus> {
Phase1, Phase2, Phase3
}
/**
* DPT 21.1201 Phases Status.<br>
* Phase status is encoded as follows:
* <ul>
* <li>Cleared Bit (0): phase absent</li>
* <li>Set Bit (1): phase present</li>
* </ul>
*/
public static final EnumDpt<PhasesStatus> DptPhasesStatus = new EnumDpt<>("21.1201", PhasesStatus.class);



private static final Map<String, DPT> types = loadDatapointTypes(DptXlator8BitSet.class);

/**
Expand Down Expand Up @@ -222,6 +254,22 @@ public final void setValue(final EnumSet<?> elements)
data = new short[] { toDpt(elements) };
}

/**
* {@return the set bits of the first translation item}
*
* @throws KNXFormatException if the item has set an invalid bit value for the specified DPT
*/
public final EnumSet<?> value() throws KNXFormatException {
final short v = fromDpt(0);
final EnumSet<?> set = EnumSet.allOf(((EnumDpt<?>) dpt).elements);
for (final var i = set.iterator(); i.hasNext();) {
final var e = i.next();
if ((v & (1 << e.ordinal())) == 0)
i.remove();
}
return set;
}

@Override
public final double getNumericValue() throws KNXFormatException
{
Expand Down
67 changes: 57 additions & 10 deletions test/io/calimero/dptxlator/DptXlator8BitSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,30 @@

package io.calimero.dptxlator;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static io.calimero.dptxlator.DptXlator8BitSet.GeneralStatus.AlarmUnAck;
import static io.calimero.dptxlator.DptXlator8BitSet.GeneralStatus.Fault;
import static io.calimero.dptxlator.DptXlator8BitSet.GeneralStatus.InAlarm;
import static io.calimero.dptxlator.DptXlator8BitSet.GeneralStatus.OutOfService;
import static io.calimero.dptxlator.DptXlator8BitSet.GeneralStatus.Overridden;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import io.calimero.KNXFormatException;
import io.calimero.dptxlator.DptXlator8BitSet.EnumDpt;
import io.calimero.dptxlator.DptXlator8BitSet.VirtualDryContact;

class DptXlator8BitSetTest
{
Expand Down Expand Up @@ -111,19 +118,59 @@ void getValue() throws KNXFormatException
t.setValue(1);
assertEquals(e3, t.getValue());

final DptXlator8BitSet x = new DptXlator8BitSet(DptXlator8BitSet.DptDeviceControl);
final int v = (int) x.getNumericValue();
assertEquals(0, v);
final int element = DptXlator8BitSet.DeviceControl.VerifyMode.value();
x.setValue(element);
assertEquals(element, x.getNumericValue());
{
final DptXlator8BitSet x = new DptXlator8BitSet(DptXlator8BitSet.DptDeviceControl);
final var v = x.getValue();
assertEquals("", v);
final var element = DptXlator8BitSet.DeviceControl.VerifyMode;
x.setValue(element.value());
assertEquals(element.description(), x.getValue());
}
{
final DptXlator8BitSet x = new DptXlator8BitSet(DptXlator8BitSet.DptVirtualDryContact);
final var v = x.getValue();
assertEquals("", v);
final var set = EnumSet.of(VirtualDryContact.Status2, VirtualDryContact.Status5);
x.setValue(set);
assertTrue(x.getValue().contains(VirtualDryContact.Status2.description()));
assertTrue(x.getValue().contains(VirtualDryContact.Status5.description()));
}
{
final DptXlator8BitSet x = new DptXlator8BitSet(DptXlator8BitSet.DptPhasesStatus);
final var v = x.getValue();
assertEquals("", v);
final var element = DptXlator8BitSet.PhasesStatus.Phase2;
x.setValue(EnumSet.of(element));
assertEquals(element.description(), x.getValue());
}
}

@ParameterizedTest
@MethodSource("allXlators")
void value(final DptXlator8BitSet t) throws KNXFormatException {
assertTrue(t.value().isEmpty());
final EnumDpt<?> dpt = (EnumDpt<?>) t.getType();
t.setValue(EnumSet.allOf(dpt.elements));
assertEquals(EnumSet.allOf(dpt.elements), t.value());
}

private static Stream<DptXlator8BitSet> allXlators() {
return DptXlator8BitSet.getSubTypesStatic().values().stream().map(DptXlator8BitSetTest::create);
}

private static DptXlator8BitSet create(final DPT dpt) {
try {
return new DptXlator8BitSet(dpt.getID());
} catch (final KNXFormatException e) {
throw new IllegalStateException();
}
}

@Test
void getSubTypes()
{
final Map<String, DPT> subTypes = t.getSubTypes();
assertEquals(14, subTypes.size());
assertEquals(16, subTypes.size());
}

@Test
Expand Down

0 comments on commit 5701cf9

Please sign in to comment.