]> git.basschouten.com Git - openhab-addons.git/blob
54539204c76a4dab433f0584e0236203d4d735ae
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.echonetlite.internal;
14
15 import java.nio.ByteBuffer;
16 import java.util.Collection;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * @author Michael Barker - Initial contribution
23  */
24 @NonNullByDefault
25 public class EchonetPropertyMap {
26     private static final int[][] PROPERTY_MAP = { { 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, },
27             { 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1, }, { 0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2, },
28             { 0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3, }, { 0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4, },
29             { 0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5, }, { 0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6, },
30             { 0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7, }, { 0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8, },
31             { 0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9, }, { 0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA, },
32             { 0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB, }, { 0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC, },
33             { 0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD, }, { 0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE, },
34             { 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF, }, };
35
36     private int[] propertyMap = {};
37     private final Epc epc;
38
39     public EchonetPropertyMap(final Epc epc) {
40         this.epc = epc;
41     }
42
43     public Epc epc() {
44         return epc;
45     }
46
47     public void update(final ByteBuffer edt) {
48         propertyMap = parsePropertyMap(edt);
49     }
50
51     public void getProperties(int groupCode, int classCode, final Set<Epc> existing, Collection<Epc> toFill) {
52         for (int epcCode : propertyMap) {
53             final Epc epc = Epc.lookup(groupCode, classCode, epcCode);
54             if (!existing.contains(epc)) {
55                 toFill.add(epc);
56             }
57         }
58     }
59
60     static int[] parsePropertyMap(final ByteBuffer buffer) {
61         final int numProperties = buffer.get() & 0xFF;
62         final int[] properties = new int[numProperties];
63         int propertyIndex = 0;
64         if (numProperties < 16) {
65             for (int i = 0; i < numProperties; i++) {
66                 properties[propertyIndex] = (buffer.get() & 0xFF);
67                 propertyIndex++;
68             }
69         } else {
70             assert 16 == buffer.remaining();
71
72             for (int i = 0; i < 16; i++) {
73                 int b = buffer.get() & 0xFF;
74                 for (int j = 0; j < 8; j++) {
75                     if (0 != (b & (1 << j))) {
76                         assert propertyIndex < properties.length;
77
78                         properties[propertyIndex] = PROPERTY_MAP[i][j];
79                         propertyIndex++;
80                     }
81                 }
82             }
83         }
84
85         assert propertyIndex == properties.length;
86         return properties;
87     }
88
89     public String toString() {
90         return "EnPropertyMap{" + "propertyMap=" + HexUtil.hex(propertyMap) + '}';
91     }
92 }