[shelly] Add BLU packet threshhold and fix typo (#21031)

If a BLE device is restarted, its PID counter starts anew.

Signed-off-by: Erik De Boeck <deboeck.erik@gmail.com>
This commit is contained in:
ErikDB87
2026-06-24 17:58:04 +02:00
committed by GitHub
parent b6cde8d55a
commit 1bec51d1ab
@@ -17,6 +17,7 @@ import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
import static org.openhab.binding.shelly.internal.api2.ShellyBluJsonDTO.*; import static org.openhab.binding.shelly.internal.api2.ShellyBluJsonDTO.*;
import static org.openhab.binding.shelly.internal.util.ShellyUtils.*; import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
import java.time.Instant;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -73,7 +74,9 @@ public class ShellyBluApi extends Shelly2ApiRpc {
private boolean connected; // true = BLU devices has connected private boolean connected; // true = BLU devices has connected
private ShellySettingsStatus deviceStatus = new ShellySettingsStatus(); private ShellySettingsStatus deviceStatus = new ShellySettingsStatus();
private int lastPid = -1; private int lastPid = -1;
private static final int PID_CYCLE_TRESHHOLD = 50; private static final int PID_CYCLE_TRESHOLD = 50;
private long lastTimeStampPacket = 0;
private static final int PACKET_TIMESTAMP_TRESHOLD = 10;
/** /**
* Regular constructor - called by Thing handler * Regular constructor - called by Thing handler
@@ -192,16 +195,22 @@ public class ShellyBluApi extends Shelly2ApiRpc {
logger.debug("{}: BLU event {} received from address {}, pid={} (JSON={})", thingName, event, logger.debug("{}: BLU event {} received from address {}, pid={} (JSON={})", thingName, event,
getString(e.blu.addr), getInteger(e.blu.pid), eventJSON); getString(e.blu.addr), getInteger(e.blu.pid), eventJSON);
if (e.blu.pid != null) { if (e.blu.pid != null) {
long epochNow = Instant.now().getEpochSecond();
int pid = e.blu.pid; int pid = e.blu.pid;
if (lastPid != -1 && pid < (lastPid - PID_CYCLE_TRESHHOLD)) { if (lastPid != -1 && pid < (lastPid - PID_CYCLE_TRESHOLD)) {
logger.debug( logger.debug(
"{}: Received pid {} is so low that a new cycle has probably begun since lastPID={}", "{}: Received pid {} is so low that a new cycle has probably begun since lastPID={}",
thingName, pid, lastPid); thingName, pid, lastPid);
} else if (pid <= lastPid && epochNow - lastTimeStampPacket > PACKET_TIMESTAMP_TRESHOLD) {
logger.debug(
"{}: Received pid {} is too low, but received more than {} sec. after lastPID={}. A new cycle has thus probably begun",
thingName, pid, PACKET_TIMESTAMP_TRESHOLD, lastPid);
} else if (pid <= lastPid) { } else if (pid <= lastPid) {
logger.debug("{}: Duplicate packet for pid {} received, ignore", thingName, pid); logger.debug("{}: Duplicate packet for pid {} received, ignore", thingName, pid);
break; break;
} }
lastPid = pid; lastPid = pid;
lastTimeStampPacket = epochNow;
} }
getThing().getProfile().gateway = message.src; getThing().getProfile().gateway = message.src;
} }