]> git.basschouten.com Git - openhab-addons.git/blob
1d9361ce9582a99e8022dae50f94833a05964dd0
[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.nobohub.internal.model;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nobohub.internal.NoboHubBindingConstants;
21
22 /**
23  * Nobø serial numbers are 12 digits where 3 and 3 digits form 2 bytes as decimal. In total 32 bits.
24  *
25  * @author Jørgen Austvik - Initial contribution
26  * @author Espen Fossen - Initial contribution
27  */
28 @NonNullByDefault
29 public final class SerialNumber {
30
31     private final String serialNumber;
32
33     public SerialNumber(String serialNumber) {
34         this.serialNumber = serialNumber.trim();
35     }
36
37     public boolean isWellFormed() {
38         if (serialNumber.length() != 12) {
39             return false;
40         }
41
42         List<String> parts = new ArrayList<>(4);
43         for (int i = 0; i < 4; i++) {
44             parts.add(serialNumber.substring((i * 3), (i * 3) + 3));
45         }
46
47         if (parts.size() != 4) {
48             return false;
49         }
50
51         for (String part : parts) {
52             try {
53                 int num = Integer.parseInt(part);
54                 if (num < 0 || num > 255) {
55                     return false;
56                 }
57             } catch (NumberFormatException nfe) {
58                 return false;
59             }
60         }
61
62         return true;
63     }
64
65     /**
66      * Returns the type string.
67      */
68     public String getTypeIdentifier() {
69         if (!isWellFormed()) {
70             return "Unknown";
71         }
72
73         return serialNumber.substring(0, 3);
74     }
75
76     /**
77      * Returns the type of this component.
78      */
79     public String getComponentType() {
80         String id = getTypeIdentifier();
81         String type = getTypeForSerialNumber(id);
82         if (null != type) {
83             return type;
84         }
85
86         return "Unknown, please contact maintainer to add a new type for " + serialNumber;
87     }
88
89     private @Nullable String getTypeForSerialNumber(String id) {
90         return NoboHubBindingConstants.SERIALNUMBERS_FOR_TYPES.get(id);
91     }
92
93     @Override
94     public String toString() {
95         return serialNumber;
96     }
97
98     @Override
99     public boolean equals(@Nullable Object obj) {
100         if (this == obj) {
101             return true;
102         }
103
104         if (obj == null || obj.getClass() != this.getClass()) {
105             return false;
106         }
107
108         SerialNumber other = (SerialNumber) obj;
109         return this.serialNumber.equals(other.serialNumber);
110     }
111
112     @Override
113     public int hashCode() {
114         return this.serialNumber.hashCode();
115     }
116 }