2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.caddx.internal;
15 import java.io.PrintWriter;
16 import java.io.StringWriter;
17 import java.nio.charset.StandardCharsets;
18 import java.text.CharacterIterator;
19 import java.text.StringCharacterIterator;
20 import java.util.Arrays;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
25 * Panel message property class
27 * @author Georgios Moutsos - Initial contribution
30 public class CaddxProperty {
32 private final String name;
33 private final CaddxPropertyType type; // 'Int', 'String', 'Bit'
34 private final int byteFrom;
35 private final int byteLength;
36 private final int bitFrom;
37 private final int bitLength;
38 private final boolean external;
39 private final String id;
41 public CaddxProperty(String id, int byteFrom, int byteLength, int bitFrom, int bitLength, CaddxPropertyType type,
42 String name, boolean external) {
46 this.byteFrom = byteFrom;
47 this.byteLength = byteLength;
48 this.bitFrom = bitFrom;
49 this.bitLength = bitLength;
50 this.external = external;
53 public String getName() {
57 public CaddxPropertyType getType() {
61 public boolean getExternal() {
65 public String getId() {
69 public String getValue(byte[] message) {
75 if (bitFrom == 0 && bitLength == 0) {
77 val = message[byteFrom - 1] & mask;
79 mask = ((1 << ((bitLength - bitFrom))) - 1) << bitFrom;
80 val = (message[byteFrom - 1] & mask) >> bitFrom;
83 return Integer.toString(val);
85 byte[] str = Arrays.copyOfRange(message, byteFrom - 1, byteFrom + byteLength - 1);
86 return mapCaddxString(new String(str, StandardCharsets.US_ASCII));
88 return (((message[byteFrom - 1] & (1 << bitFrom)) > 0) ? "true" : "false");
90 throw new IllegalArgumentException("type is unknown.");
94 public String toString(byte[] message) {
97 StringWriter sWriter = new StringWriter();
98 PrintWriter pWriter = new PrintWriter(sWriter);
102 if (bitFrom == 0 && bitLength == 0) {
104 val = message[byteFrom - 1];
106 mask = ((1 << ((bitLength - bitFrom) + 1)) - 1) << bitFrom;
107 val = (message[byteFrom - 1] & mask) >> bitFrom;
110 pWriter.printf("%s: %02x - %d - %c", name, val, val, Character.isValidCodePoint(val) ? val : 32);
113 return sWriter.toString();
118 byte[] a = Arrays.copyOfRange(message, byteFrom - 1, byteFrom + byteLength);
119 pWriter.println(mapCaddxString(new String(a, StandardCharsets.US_ASCII)));
121 for (int i = 0; i < byteLength; i++) {
122 pWriter.printf("%02x", message[byteFrom - 1 + i]);
123 pWriter.print(" - ");
124 pWriter.println((char) message[byteFrom - 1 + i]);
128 return sWriter.toString();
132 pWriter.print(((message[byteFrom - 1] & (1 << bitFrom)) > 0));
135 return sWriter.toString();
137 pWriter.print("Unknown type: ");
138 pWriter.print(type.toString());
141 return sWriter.toString();
145 private String mapCaddxString(String str) {
146 StringBuilder s = new StringBuilder(str.length());
148 CharacterIterator it = new StringCharacterIterator(str);
149 for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {