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.bluetooth.discovery.internal;
15 import java.util.Objects;
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;
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.
27 * @author Connor Petty - Initial Contribution
30 public class BluetoothDeviceSnapshot extends BluetoothDiscoveryDevice {
32 private @Nullable String name;
33 private @Nullable Integer manufacturer;
34 private @Nullable Integer txPower;
36 public BluetoothDeviceSnapshot(BluetoothDevice device) {
38 this.txPower = device.getTxPower();
39 this.manufacturer = device.getManufacturerId();
40 this.name = device.getName();
44 public @Nullable String getName() {
49 * Set the name of the device
51 * @param name a {@link String} defining the device name
53 public void setName(String name) {
58 * Sets the manufacturer id for the device
60 * @param manufacturer the manufacturer id
62 public void setManufacturerId(int manufacturer) {
63 this.manufacturer = manufacturer;
67 * Returns the manufacturer ID of the device
69 * @return an integer with manufacturer ID of the device, or null if not known
72 public @Nullable Integer getManufacturerId() {
77 * Sets the device transmit power
79 * @param power the current transmitter power in dBm
81 public void setTxPower(int txPower) {
82 this.txPower = txPower;
86 * Returns the last Transmit Power value or null if no transmit power has been received
88 * @return the last reported transmitter power value in dBm
91 public @Nullable Integer getTxPower() {
96 * Set the model of the device
98 * @param model a {@link String} defining the device model
100 public void setModel(String model) {
105 * Set the serial number of the device
107 * @param model a {@link String} defining the serial number
109 public void setSerialNumberl(String serialNumber) {
110 this.serialNumber = serialNumber;
114 * Set the hardware revision of the device
116 * @param model a {@link String} defining the hardware revision
118 public void setHardwareRevision(String hardwareRevision) {
119 this.hardwareRevision = hardwareRevision;
123 * Set the firmware revision of the device
125 * @param model a {@link String} defining the firmware revision
127 public void setFirmwareRevision(String firmwareRevision) {
128 this.firmwareRevision = firmwareRevision;
132 * Set the software revision of the device
134 * @param model a {@link String} defining the software revision
136 public void setSoftwareRevision(String softwareRevision) {
137 this.softwareRevision = softwareRevision;
141 public int hashCode() {
142 final int prime = 31;
145 BluetoothAddress address = this.address;
146 Integer manufacturer = this.manufacturer;
147 Integer txPower = this.txPower;
148 String name = this.name;
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;
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());
168 @SuppressWarnings("PMD.SimplifyBooleanReturns")
170 public boolean equals(@Nullable Object obj) {
177 if (getClass() != obj.getClass()) {
180 BluetoothDeviceSnapshot other = (BluetoothDeviceSnapshot) obj;
181 if (!Objects.equals(address, other.address)) {
184 if (!Objects.equals(manufacturer, other.manufacturer)) {
187 if (!Objects.equals(txPower, other.txPower)) {
190 if (!Objects.equals(name, other.name)) {
193 if (!Objects.equals(model, other.model)) {
196 if (!Objects.equals(serialNumber, other.serialNumber)) {
199 if (!Objects.equals(hardwareRevision, other.hardwareRevision)) {
202 if (!Objects.equals(firmwareRevision, other.firmwareRevision)) {
205 if (!Objects.equals(softwareRevision, other.softwareRevision)) {
212 * This merges non-null identity fields from the given device into this snapshot.
214 * @return true if this snapshot changed as a result of this operation
216 public void merge(BluetoothDeviceSnapshot device) {
217 Integer txPower = device.getTxPower();
218 Integer manufacturer = device.getManufacturerId();
219 String name = device.getName();
221 String model = device.getModel();
222 String serialNumber = device.getSerialNumber();
223 String hardwareRevision = device.getHardwareRevision();
224 String firmwareRevision = device.getFirmwareRevision();
225 String softwareRevision = device.getSoftwareRevision();
227 if (this.txPower == null && txPower != null) {
228 this.txPower = txPower;
230 if (this.manufacturer == null && manufacturer != null) {
231 this.manufacturer = manufacturer;
233 if (this.name == null && name != null) {
236 if (this.model == null && model != null) {
239 if (this.serialNumber == null && serialNumber != null) {
240 this.serialNumber = serialNumber;
242 if (this.hardwareRevision == null && hardwareRevision != null) {
243 this.hardwareRevision = hardwareRevision;
245 if (this.firmwareRevision == null && firmwareRevision != null) {
246 this.firmwareRevision = firmwareRevision;
248 if (this.softwareRevision == null && softwareRevision != null) {
249 this.softwareRevision = softwareRevision;