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.loxone.internal.types;
15 import java.lang.reflect.Type;
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
19 import com.google.gson.JsonDeserializationContext;
20 import com.google.gson.JsonDeserializer;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParseException;
25 * Unique identifier of an object on Loxone Miniserver.
27 * It is defined by the Miniserver. UUID can represent a control, room, category, etc. and provides a unique ID space
28 * across all objects residing on the Miniserver.
30 * @author Pawel Pieczul - initial contribution
34 private final String uuid;
35 private final String uuidOriginal;
37 public static final JsonDeserializer<LxUuid> DESERIALIZER = new JsonDeserializer<LxUuid>() {
39 public LxUuid deserialize(JsonElement json, Type type, JsonDeserializationContext context)
40 throws JsonParseException {
41 String uuid = json.getAsString();
43 return new LxUuid(uuid);
50 * Create a new {@link LxUuid} object from an UUID on a Miniserver.
52 * @param uuid identifier retrieved from Loxone Miniserver
54 public LxUuid(String uuid) {
56 this.uuid = init(uuid);
59 public LxUuid(byte[] data, int offset) {
60 String id = String.format("%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
61 ByteBuffer.wrap(data, offset, 4).order(ByteOrder.LITTLE_ENDIAN).getInt(),
62 ByteBuffer.wrap(data, offset + 4, 2).order(ByteOrder.LITTLE_ENDIAN).getShort(),
63 ByteBuffer.wrap(data, offset + 6, 2).order(ByteOrder.LITTLE_ENDIAN).getShort(), data[offset + 8],
64 data[offset + 9], data[offset + 10], data[offset + 11], data[offset + 12], data[offset + 13],
65 data[offset + 14], data[offset + 15]);
70 private String init(String uuid) {
71 return uuidOriginal.replaceAll("[^a-zA-Z0-9-]", "-").toUpperCase();
75 public boolean equals(Object o) {
82 if (o.getClass() != getClass()) {
85 LxUuid id = (LxUuid) o;
86 return uuid.equals(id.uuid);
90 public int hashCode() {
91 return uuid.hashCode();
95 public String toString() {
100 * Returns an original string that was used to create UUID.
102 * @return original string for the UUID
104 public String getOriginalString() {