Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Feb 4, 2025
1 parent 9e718c6 commit 08ec080
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bluetooth.BluetoothCharacteristic;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.grundfosalpha.internal.protocol.MessageType;
import org.openhab.core.util.HexUtils;

/**
Expand All @@ -38,11 +39,11 @@ public class CharacteristicRequest {
* Creates a new request object.
*
* @param uuid The UUID of the characteristic
* @param value The data to write
* @param messageType The {@link MessageType} containing the data to write
*/
public CharacteristicRequest(UUID uuid, byte[] value) {
public CharacteristicRequest(UUID uuid, MessageType messageType) {
this.uuid = uuid;
this.value = value;
this.value = messageType.request();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ public class GrundfosAlpha3Handler extends ConnectedBluetoothHandler {
private static final UUID UUID_SERVICE_GENI = UUID.fromString("0000fe5d-0000-1000-8000-00805f9b34fb");
private static final UUID UUID_CHARACTERISTIC_GENI = UUID.fromString("859cffd1-036e-432a-aa28-1a0085b87ba9");

private static final byte[] TRIGGER_DISCONNECT = new byte[] { (byte) 0x2a, (byte) 0x05, (byte) 0xff, (byte) 0x01,
(byte) 0x03, (byte) 0x81, (byte) 0x81 };

private static final Set<String> FLOW_HEAD_CHANNELS = Set.of(CHANNEL_FLOW_RATE, CHANNEL_PUMP_HEAD);
private static final Set<String> POWER_CHANNELS = Set.of(CHANNEL_VOLTAGE_AC, CHANNEL_POWER, CHANNEL_MOTOR_SPEED);

Expand Down Expand Up @@ -100,7 +97,6 @@ public void dispose() {
this.senderThread = null;
}
cancelFuture();
sendDisconnect();
super.dispose();
}

Expand Down Expand Up @@ -139,16 +135,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}

if (FLOW_HEAD_CHANNELS.contains(channelUID.getId())) {
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.FlowHead.request()));
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.FlowHead));
} else if (POWER_CHANNELS.contains(channelUID.getId())) {
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.Power.request()));
}
}

private void sendDisconnect() {
BluetoothCharacteristic characteristic = device.getCharacteristic(UUID_CHARACTERISTIC_GENI);
if (characteristic != null) {
device.writeCharacteristic(characteristic, TRIGGER_DISCONNECT);
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.Power));
}
}

Expand All @@ -166,6 +155,10 @@ public void onServicesDiscovered() {
logger.debug("Characteristic {} found for service {}", UUID_CHARACTERISTIC_GENI,
UUID_SERVICE_GENI);
device.enableNotifications(characteristic);
String deviceName = device.getName();
if (deviceName != null) {
updateProperty(Thing.PROPERTY_MODEL_ID, deviceName);
}
updateStatus(ThingStatus.ONLINE);
scheduleFuture();
}
Expand Down Expand Up @@ -216,11 +209,11 @@ private void refreshChannels() {
}

if (FLOW_HEAD_CHANNELS.stream().anyMatch(this::isLinked)) {
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.FlowHead.request()));
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.FlowHead));
}

if (POWER_CHANNELS.stream().anyMatch(this::isLinked)) {
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.Power.request()));
sendQueue.add(new CharacteristicRequest(UUID_CHARACTERISTIC_GENI, MessageType.Power));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This defines protocol header related constants.
* This defines the protocol header.
*
* @author Jacob Laursen - Initial contribution
*/
Expand Down Expand Up @@ -60,7 +60,7 @@ public static void setRequestHeader(byte[] request, int messageLength) {
throw new IllegalArgumentException("Buffer is too small for header");
}

request[OFFSET_START_DELIMITER] = MessageStartDelimiter.Request.getValue();
request[OFFSET_START_DELIMITER] = MessageStartDelimiter.Request.value();
request[OFFSET_LENGTH] = (byte) messageLength;
request[OFFSET_SOURCE_ADDRESS] = CONTROLLER_ADDRESS;
request[OFFSET_DESTINATION_ADDRESS] = PERIPHERAL_ADDRESS;
Expand All @@ -74,7 +74,7 @@ public static void setRequestHeader(byte[] request, int messageLength) {
* @return true if determined to be first packet, otherwise false
*/
public static boolean isInitialResponsePacket(byte[] packet) {
return packet.length >= LENGTH && packet[OFFSET_START_DELIMITER] == MessageStartDelimiter.Reply.getValue()
return packet.length >= LENGTH && packet[OFFSET_START_DELIMITER] == MessageStartDelimiter.Reply.value()
&& packet[OFFSET_SOURCE_ADDRESS] == PERIPHERAL_ADDRESS
&& packet[OFFSET_DESTINATION_ADDRESS] == CONTROLLER_ADDRESS && packet[OFFSET_HEADER4] == HEADER4_VALUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
public enum MessageStartDelimiter {
Reply((byte) 0x24),
Message((byte) 0x26),
Request((byte) 0x27);
Request((byte) 0x27),
Echo((byte) 0x30);

private final byte value;

MessageStartDelimiter(byte value) {
this.value = value;
}

public byte getValue() {
public byte value() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public enum MessageType {
Power(new byte[] { 0x2c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x25 },
new byte[] { (byte) 0x03, (byte) 0x57, (byte) 0x00, (byte) 0x45 });

private final byte[] identifier;
private final byte[] responseType;
private final byte[] request;

MessageType(byte[] identifier, byte[] requestMessage) {
this.identifier = identifier;
MessageType(byte[] responseType, byte[] requestMessage) {
this.responseType = responseType;

int messageLength = requestMessage.length + 3;
request = new byte[messageLength + 4];
Expand All @@ -44,8 +44,8 @@ public enum MessageType {
CRC16Calculator.put(request, messageLength);
}

public byte[] identifier() {
return identifier;
public byte[] responseType() {
return responseType;
}

public byte[] request() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public Map<SensorDataType, BigDecimal> decode() {
}

private Optional<ByteBuffer> decode(SensorDataType dataType) {
byte[] expectedResponseType = dataType.messageType().identifier();
byte[] expectedResponseType = dataType.messageType().responseType();
byte[] responseType = new byte[GENI_RESPONSE_TYPE_LENGTH];
System.arraycopy(response, MessageHeader.LENGTH, responseType, 0, GENI_RESPONSE_TYPE_LENGTH);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@

<properties>
<property name="vendor">Grundfos</property>
<property name="modelId">Alpha3</property>
</properties>

<representation-property>address</representation-property>
Expand Down

0 comments on commit 08ec080

Please sign in to comment.