]> git.basschouten.com Git - openhab-addons.git/blob
e813a306d73b8fba94f75f836c0a42cb6dba935b
[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.bluetooth.discovery.internal;
14
15 import java.util.Objects;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.bluetooth.BluetoothAddress;
20 import org.openhab.binding.bluetooth.BluetoothDevice;
21 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
22
23 /**
24  * The {@link BluetoothDeviceSnapshot} acts as a dummy {@link BluetoothDevice} implementation that simply acts as a
25  * snapshot for device data at the time of creation.
26  *
27  * @author Connor Petty - Initial Contribution
28  */
29 @NonNullByDefault
30 public class BluetoothDeviceSnapshot extends BluetoothDiscoveryDevice {
31
32     private @Nullable String name;
33     private @Nullable Integer manufacturer;
34     private @Nullable Integer txPower;
35
36     public BluetoothDeviceSnapshot(BluetoothDevice device) {
37         super(device);
38         this.txPower = device.getTxPower();
39         this.manufacturer = device.getManufacturerId();
40         this.name = device.getName();
41     }
42
43     @Override
44     public @Nullable String getName() {
45         return name;
46     }
47
48     /**
49      * Set the name of the device
50      *
51      * @param name a {@link String} defining the device name
52      */
53     public void setName(String name) {
54         this.name = name;
55     }
56
57     /**
58      * Sets the manufacturer id for the device
59      *
60      * @param manufacturer the manufacturer id
61      */
62     public void setManufacturerId(int manufacturer) {
63         this.manufacturer = manufacturer;
64     }
65
66     /**
67      * Returns the manufacturer ID of the device
68      *
69      * @return an integer with manufacturer ID of the device, or null if not known
70      */
71     @Override
72     public @Nullable Integer getManufacturerId() {
73         return manufacturer;
74     }
75
76     /**
77      * Sets the device transmit power
78      *
79      * @param power the current transmitter power in dBm
80      */
81     public void setTxPower(int txPower) {
82         this.txPower = txPower;
83     }
84
85     /**
86      * Returns the last Transmit Power value or null if no transmit power has been received
87      *
88      * @return the last reported transmitter power value in dBm
89      */
90     @Override
91     public @Nullable Integer getTxPower() {
92         return txPower;
93     }
94
95     /**
96      * Set the model of the device
97      *
98      * @param model a {@link String} defining the device model
99      */
100     public void setModel(String model) {
101         this.model = model;
102     }
103
104     /**
105      * Set the serial number of the device
106      *
107      * @param model a {@link String} defining the serial number
108      */
109     public void setSerialNumberl(String serialNumber) {
110         this.serialNumber = serialNumber;
111     }
112
113     /**
114      * Set the hardware revision of the device
115      *
116      * @param model a {@link String} defining the hardware revision
117      */
118     public void setHardwareRevision(String hardwareRevision) {
119         this.hardwareRevision = hardwareRevision;
120     }
121
122     /**
123      * Set the firmware revision of the device
124      *
125      * @param model a {@link String} defining the firmware revision
126      */
127     public void setFirmwareRevision(String firmwareRevision) {
128         this.firmwareRevision = firmwareRevision;
129     }
130
131     /**
132      * Set the software revision of the device
133      *
134      * @param model a {@link String} defining the software revision
135      */
136     public void setSoftwareRevision(String softwareRevision) {
137         this.softwareRevision = softwareRevision;
138     }
139
140     @Override
141     public int hashCode() {
142         final int prime = 31;
143         int result = 1;
144
145         BluetoothAddress address = this.address;
146         Integer manufacturer = this.manufacturer;
147         Integer txPower = this.txPower;
148         String name = this.name;
149
150         String model = this.model;
151         String serialNumber = this.serialNumber;
152         String hardwareRevision = this.hardwareRevision;
153         String firmwareRevision = this.firmwareRevision;
154         String softwareRevision = this.softwareRevision;
155
156         result = prime * result + address.hashCode();
157         result = prime * result + ((firmwareRevision == null) ? 0 : firmwareRevision.hashCode());
158         result = prime * result + ((hardwareRevision == null) ? 0 : hardwareRevision.hashCode());
159         result = prime * result + ((manufacturer == null) ? 0 : manufacturer.hashCode());
160         result = prime * result + ((model == null) ? 0 : model.hashCode());
161         result = prime * result + ((name == null) ? 0 : name.hashCode());
162         result = prime * result + ((serialNumber == null) ? 0 : serialNumber.hashCode());
163         result = prime * result + ((softwareRevision == null) ? 0 : softwareRevision.hashCode());
164         result = prime * result + ((txPower == null) ? 0 : txPower.hashCode());
165         return result;
166     }
167
168     @SuppressWarnings("PMD.SimplifyBooleanReturns")
169     @Override
170     public boolean equals(@Nullable Object obj) {
171         if (this == obj) {
172             return true;
173         }
174         if (obj == null) {
175             return false;
176         }
177         if (getClass() != obj.getClass()) {
178             return false;
179         }
180         BluetoothDeviceSnapshot other = (BluetoothDeviceSnapshot) obj;
181         if (!Objects.equals(address, other.address)) {
182             return false;
183         }
184         if (!Objects.equals(manufacturer, other.manufacturer)) {
185             return false;
186         }
187         if (!Objects.equals(txPower, other.txPower)) {
188             return false;
189         }
190         if (!Objects.equals(name, other.name)) {
191             return false;
192         }
193         if (!Objects.equals(model, other.model)) {
194             return false;
195         }
196         if (!Objects.equals(serialNumber, other.serialNumber)) {
197             return false;
198         }
199         if (!Objects.equals(hardwareRevision, other.hardwareRevision)) {
200             return false;
201         }
202         if (!Objects.equals(firmwareRevision, other.firmwareRevision)) {
203             return false;
204         }
205         return Objects.equals(softwareRevision, other.softwareRevision);
206     }
207
208     /**
209      * This merges non-null identity fields from the given device into this snapshot.
210      *
211      * @return true if this snapshot changed as a result of this operation
212      */
213     public void merge(BluetoothDeviceSnapshot device) {
214         Integer txPower = device.getTxPower();
215         Integer manufacturer = device.getManufacturerId();
216         String name = device.getName();
217
218         String model = device.getModel();
219         String serialNumber = device.getSerialNumber();
220         String hardwareRevision = device.getHardwareRevision();
221         String firmwareRevision = device.getFirmwareRevision();
222         String softwareRevision = device.getSoftwareRevision();
223
224         if (this.txPower == null && txPower != null) {
225             this.txPower = txPower;
226         }
227         if (this.manufacturer == null && manufacturer != null) {
228             this.manufacturer = manufacturer;
229         }
230         if (this.name == null && name != null) {
231             this.name = name;
232         }
233         if (this.model == null && model != null) {
234             this.model = model;
235         }
236         if (this.serialNumber == null && serialNumber != null) {
237             this.serialNumber = serialNumber;
238         }
239         if (this.hardwareRevision == null && hardwareRevision != null) {
240             this.hardwareRevision = hardwareRevision;
241         }
242         if (this.firmwareRevision == null && firmwareRevision != null) {
243             this.firmwareRevision = firmwareRevision;
244         }
245         if (this.softwareRevision == null && softwareRevision != null) {
246             this.softwareRevision = softwareRevision;
247         }
248     }
249 }