2 * Copyright (c) 2010-2020 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;
42 public CaddxProperty(String id, int byteFrom, int byteLength, int bitFrom, int bitLength, CaddxPropertyType type,
43 String name, boolean external) {
47 this.byteFrom = byteFrom;
48 this.byteLength = byteLength;
49 this.bitFrom = bitFrom;
50 this.bitLength = bitLength;
51 this.external = external;
54 public String getName() {
58 public CaddxPropertyType getType() {
62 public boolean getExternal() {
66 public String getId() {
70 public String getValue(byte[] message) {
76 if (bitFrom == 0 && bitLength == 0) {
78 val = message[byteFrom - 1] & mask;
80 mask = ((1 << ((bitLength - bitFrom))) - 1) << bitFrom;
81 val = (message[byteFrom - 1] & mask) >> bitFrom;
84 return Integer.toString(val);
86 byte[] str = Arrays.copyOfRange(message, byteFrom - 1, byteFrom + byteLength);
87 return mapCaddxString(new String(str, StandardCharsets.US_ASCII));
89 return (((message[byteFrom - 1] & (1 << bitFrom)) > 0) ? "true" : "false");
91 throw new IllegalArgumentException("type is unknown.");
95 public String toString(byte[] message) {
98 StringWriter sWriter = new StringWriter();
99 PrintWriter pWriter = new PrintWriter(sWriter);
103 if (bitFrom == 0 && bitLength == 0) {
105 val = message[byteFrom - 1];
107 mask = ((1 << ((bitLength - bitFrom) + 1)) - 1) << bitFrom;
108 val = (message[byteFrom - 1] & mask) >> bitFrom;
111 pWriter.printf("%s: %02x - %d - %c", name, val, val, Character.isValidCodePoint(val) ? val : 32);
114 return sWriter.toString();
119 byte[] a = Arrays.copyOfRange(message, byteFrom - 1, byteFrom + byteLength);
120 pWriter.println(mapCaddxString(new String(a, StandardCharsets.US_ASCII)));
122 for (int i = 0; i < byteLength; i++) {
123 pWriter.printf("%02x", message[byteFrom - 1 + i]);
124 pWriter.print(" - ");
125 pWriter.println((char) message[byteFrom - 1 + i]);
129 return sWriter.toString();
133 pWriter.print(((message[byteFrom - 1] & (1 << bitFrom)) > 0));
136 return sWriter.toString();
138 pWriter.print("Unknown type: ");
139 pWriter.print(type.toString());
142 return sWriter.toString();
146 private String mapCaddxString(String str) {
147 StringBuilder s = new StringBuilder(str.length());
149 CharacterIterator it = new StringCharacterIterator(str);
150 for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {