]> git.basschouten.com Git - openhab-addons.git/blob
9447ead183dbfdd7598bd3f3c63e07394cfd7e37
[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.loxone.internal.types;
14
15 import java.lang.reflect.Type;
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
18
19 import com.google.gson.JsonDeserializationContext;
20 import com.google.gson.JsonDeserializer;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParseException;
23
24 /**
25  * Unique identifier of an object on Loxone Miniserver.
26  * <p>
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.
29  *
30  * @author Pawel Pieczul - initial contribution
31  *
32  */
33 public class LxUuid {
34     private final String uuid;
35     private final String uuidOriginal;
36
37     public static final JsonDeserializer<LxUuid> DESERIALIZER = new JsonDeserializer<LxUuid>() {
38         @Override
39         public LxUuid deserialize(JsonElement json, Type type, JsonDeserializationContext context)
40                 throws JsonParseException {
41             String uuid = json.getAsString();
42             if (uuid != null) {
43                 return new LxUuid(uuid);
44             }
45             return null;
46         }
47     };
48
49     /**
50      * Create a new {@link LxUuid} object from an UUID on a Miniserver.
51      *
52      * @param uuid identifier retrieved from Loxone Miniserver
53      */
54     public LxUuid(String uuid) {
55         uuidOriginal = uuid;
56         this.uuid = init(uuid);
57     }
58
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]);
66         uuidOriginal = id;
67         this.uuid = init(id);
68     }
69
70     private String init(String uuid) {
71         return uuidOriginal.replaceAll("[^a-zA-Z0-9-]", "-").toUpperCase();
72     }
73
74     @Override
75     public boolean equals(Object o) {
76         if (this == o) {
77             return true;
78         }
79         if (o == null) {
80             return false;
81         }
82         if (o.getClass() != getClass()) {
83             return false;
84         }
85         LxUuid id = (LxUuid) o;
86         return uuid.equals(id.uuid);
87     }
88
89     @Override
90     public int hashCode() {
91         return uuid.hashCode();
92     }
93
94     @Override
95     public String toString() {
96         return uuid;
97     }
98
99     /**
100      * Returns an original string that was used to create UUID.
101      *
102      * @return original string for the UUID
103      */
104     public String getOriginalString() {
105         return uuidOriginal;
106     }
107 }