Skip to content

Commit

Permalink
Make refresh interval configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Feb 2, 2025
1 parent 22fe2a1 commit 92d250f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
15 changes: 8 additions & 7 deletions bundles/org.openhab.binding.bluetooth.grundfosalpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This binding adds support for reading out the data of Grundfos Alpha pumps with a [Grundfos Alpha Reader](https://product-selection.grundfos.com/products/alpha-reader) or [Alpha3 pump](https://product-selection.grundfos.com/products/alpha/alpha3) with built-in Bluetooth.

The reverse engineering of the protocol was taken from [https://github.com/JsBergbau/AlphaDecoder](https://github.com/JsBergbau/AlphaDecoder).
The reverse engineering of the Alpha Reader protocol was taken from [https://github.com/JsBergbau/AlphaDecoder](https://github.com/JsBergbau/AlphaDecoder).

## Supported Things

Expand All @@ -17,9 +17,10 @@ All pumps and readers are auto-detected as soon as Bluetooth is configured in op

### `alpha3` Thing Configuration

| Name | Type | Description | Default | Required | Advanced |
|---------|------|-----------------------------------------------|---------|----------|----------|
| address | text | Bluetooth address in XX:XX:XX:XX:XX:XX format | N/A | yes | no |
| Name | Type | Description | Default | Required | Advanced |
|-----------------|---------|---------------------------------------------------------|---------|----------|----------|
| address | text | Bluetooth address in XX:XX:XX:XX:XX:XX format | N/A | yes | no |
| refreshInterval | integer | Number of seconds between fetching values from the pump | 30 | no | yes |

### Pairing

Expand All @@ -30,7 +31,7 @@ When the LED stops blinking and stays lit, the connection has been established,
However, the pump may still not be bonded correctly, which could prevent the binding from reconnecting after a disconnection.
On Linux, you can take additional steps to fix this issue by manually pairing the pump:

```bash
```shell
bluetoothctl pair XX:XX:XX:XX:XX:XX
Attempting to pair with XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Bonded: yes
Expand All @@ -50,7 +51,7 @@ Pairing successful

| Channel | Type | Read/Write | Description |
|------------------|---------------------------|------------|------------------------------------|
| rssi | Number | R | Received Signal Strength Indicator |
| rssi | Number:Power | R | Received Signal Strength Indicator |
| flow-rate | Number:VolumetricFlowRate | R | The flow rate of the pump |
| pump-head | Number:Length | R | The water head above the pump |
| voltage-ac | Number:ElectricPotential | R | Current AC pump voltage |
Expand All @@ -61,7 +62,7 @@ Pairing successful

| Channel | Type | Read/Write | Description |
|------------------|---------------------------|------------|------------------------------------|
| rssi | Number | R | Received Signal Strength Indicator |
| rssi | Number:Power | R | Received Signal Strength Indicator |
| flow-rate | Number:VolumetricFlowRate | R | The flow rate of the pump |
| pump-head | Number:Length | R | The water head above the pump |
| pump-temperature | Number:Temperature | R | The temperature of the pump |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class GrundfosAlphaBindingConstants {

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ALPHA3, THING_TYPE_MI401);

// List of configuration parameters
public static final String CONFIGURATION_REFRESH_INTERVAL = "refreshInterval";

// List of all Channel ids
public static final String CHANNEL_FLOW_RATE = "flow-rate";
public static final String CHANNEL_PUMP_HEAD = "pump-head";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ public class GrundfosAlpha3Handler extends ConnectedBluetoothHandler {
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);

private static final int UPDATE_INTERVAL_SECONDS = 30;
private static final int DEFAULT_REFRESH_INTERVAL_SECONDS = 30;

private final Logger logger = LoggerFactory.getLogger(GrundfosAlpha3Handler.class);
private final Lock stateLock = new ReentrantLock();
private final BlockingQueue<CharacteristicRequest> sendQueue = new LinkedBlockingQueue<>();

private @Nullable ScheduledFuture<?> refreshFuture;
private @Nullable WriteCharacteristicThread senderThread;
private ResponseMessage decoder = new ResponseMessage();
private int sequence;
private ResponseMessage responseMessage = new ResponseMessage();

public GrundfosAlpha3Handler(Thing thing) {
super(thing);
Expand All @@ -105,6 +104,18 @@ public void dispose() {
super.dispose();
}

private void scheduleFuture() {
cancelFuture();

Object refreshIntervalRaw = getConfig().get(CONFIGURATION_REFRESH_INTERVAL);
int refreshInterval = DEFAULT_REFRESH_INTERVAL_SECONDS;
if (refreshIntervalRaw instanceof Number number) {
refreshInterval = number.intValue();
}

refreshFuture = scheduler.scheduleWithFixedDelay(this::refreshChannels, 0, refreshInterval, TimeUnit.SECONDS);
}

private void cancelFuture() {
ScheduledFuture<?> refreshFuture = this.refreshFuture;
if (refreshFuture != null) {
Expand Down Expand Up @@ -155,10 +166,8 @@ public void onServicesDiscovered() {
logger.debug("Characteristic {} found for service {}", UUID_CHARACTERISTIC_GENI,
UUID_SERVICE_GENI);
device.enableNotifications(characteristic);
cancelFuture();
refreshFuture = scheduler.scheduleWithFixedDelay(this::refreshChannels, 0,
UPDATE_INTERVAL_SECONDS, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
scheduleFuture();
}
}
}
Expand All @@ -180,19 +189,21 @@ public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
@Override
public void onCharacteristicUpdate(BluetoothCharacteristic characteristic, byte[] value) {
super.onCharacteristicUpdate(characteristic, value);
if (!UUID_CHARACTERISTIC_GENI.equals(characteristic.getUuid())) {
return;
}

stateLock.lock();
try {
logger.debug("onCharacteristicUpdate/{} Received update {} to characteristic {} of device {}", sequence++,
HexUtils.bytesToHex(value), characteristic.getUuid(), address);
if (!UUID_CHARACTERISTIC_GENI.equals(characteristic.getUuid())) {
if (logger.isDebugEnabled()) {
logger.debug("Received update {} to unknown characteristic {} of device {}",
HexUtils.bytesToHex(value), characteristic.getUuid(), address);
}
return;
}

if (decoder.addPacket(value)) {
Map<SensorDataType, BigDecimal> values = decoder.decode();
if (responseMessage.addPacket(value)) {
Map<SensorDataType, BigDecimal> values = responseMessage.decode();
updateChannels(values);
decoder = new ResponseMessage();
responseMessage = new ResponseMessage();
}
} finally {
stateLock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ thing-type.bluetooth.mi401.description = A Grundfos Alpha Reader MI401

thing-type.config.bluetooth.alpha3.address.label = Address
thing-type.config.bluetooth.alpha3.address.description = Bluetooth address in XX:XX:XX:XX:XX:XX format
thing-type.config.bluetooth.alpha3.refreshInterval.label = Refresh Interval
thing-type.config.bluetooth.alpha3.refreshInterval.description = Number of seconds between fetching values from the pump. Default is 30
thing-type.config.bluetooth.mi401.address.label = Address
thing-type.config.bluetooth.mi401.address.description = Bluetooth address in XX:XX:XX:XX:XX:XX format

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<label>Motor Speed</label>
<description>Current rotation of the pump motor</description>
<category>Fan</category>
<state readOnly="true" pattern="%.0f %unit%"/>
<state readOnly="true" pattern="%.0f rpm"/>
</channel-type>

<thing-type id="mi401">
Expand Down Expand Up @@ -101,6 +101,7 @@

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

<representation-property>address</representation-property>
Expand All @@ -110,6 +111,13 @@
<label>Address</label>
<description>Bluetooth address in XX:XX:XX:XX:XX:XX format</description>
</parameter>
<parameter name="refreshInterval" type="integer" min="5" required="false" unit="s">
<default>30</default>
<label>Refresh Interval</label>
<description>Number of seconds between fetching values from the pump. Default is 30</description>
<advanced>true</advanced>
<unitLabel>seconds</unitLabel>
</parameter>
</config-description>
</thing-type>

Expand Down

0 comments on commit 92d250f

Please sign in to comment.