Skip to content

Commit

Permalink
[freeboxos] Fix thing status handling when rebooting the player (open…
Browse files Browse the repository at this point in the history
…hab#17238)

Fix openhab#17227

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Patrik Gfeller <patrik.gfeller@proton.me>
  • Loading branch information
lolodomo authored and pgfeller committed Sep 29, 2024
1 parent a0b4fea commit 6eb45a2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -50,6 +51,7 @@ public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf

public ActivePlayerHandler(Thing thing) {
super(thing);
statusDrivenByLanConnectivity = false;
eventChannelUID = new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT);
}

Expand All @@ -69,9 +71,24 @@ void initializeProperties(Map<String, String> properties) throws FreeboxExceptio
@Override
protected void internalPoll() throws FreeboxException {
super.internalPoll();
if (thing.getStatus().equals(ThingStatus.ONLINE)) {
poll();
}

@Override
protected void internalForcePoll() throws FreeboxException {
super.internalForcePoll();
poll();
}

private void poll() throws FreeboxException {
if (reachable) {
Player player = getManager(PlayerManager.class).getDevice(getClientId());
updateStatus(player.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
logger.debug("{}: poll with player.reachable() = {}", thing.getUID(), player.reachable());
if (player.reachable()) {
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
}
if (player.reachable()) {
Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
if (status != null) {
Expand All @@ -89,6 +106,9 @@ protected void internalPoll() throws FreeboxException {
}
}
updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
} else {
logger.debug("{}: poll with reachable={}", thing.getUID(), reachable);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private void startRefreshJob() {
ThingStatusDetail detail = thing.getStatusInfo().getStatusDetail();
if (ThingStatusDetail.DUTY_CYCLE.equals(detail)) {
try {
internalPoll();
internalForcePoll();
} catch (FreeboxException ignore) {
// An exception is normal if the box is rebooting then let's try again later...
addJob("Initialize", this::initialize, 10, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openhab.binding.freeboxos.internal.config.ApiConsumerConfiguration;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -46,6 +47,8 @@ public class HostHandler extends ApiConsumerHandler {
// We start in pull mode and switch to push after a first update...
protected boolean pushSubscribed = false;

protected boolean statusDrivenByLanConnectivity = true;

protected boolean reachable;

private int tryConfigureMediaSink = 1;
Expand Down Expand Up @@ -93,7 +96,8 @@ protected void internalPoll() throws FreeboxException {

LanHost host = getLanHost();
updateConnectivityChannels(host);
logger.debug("Switching to push mode - refreshInterval will now be ignored for Connectivity data");
logger.debug("{}: switching to push mode - refreshInterval will now be ignored for Connectivity data",
thing.getUID());
pushSubscribed = getManager(WebSocketManager.class).registerListener(host.getMac(), this);
}

Expand All @@ -114,10 +118,17 @@ protected LanHost getLanHost() throws FreeboxException {
}

public void updateConnectivityChannels(LanHost host) {
logger.debug("{}: updateConnectivityChannels with host.reachable() = {}", thing.getUID(), host.reachable());
updateChannelOnOff(CONNECTIVITY, REACHABLE, host.reachable());
updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, host.getLastSeen());
updateChannelString(CONNECTIVITY, IP_ADDRESS, host.getIpv4());
updateStatus(host.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
if (statusDrivenByLanConnectivity) {
if (host.reachable()) {
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-host-not-reachable");
}
}
// We will check and configure audio sink only when the host reachability changed
if (reachable != host.reachable()) {
reachable = host.reachable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ void initializeProperties(Map<String, String> properties) throws FreeboxExceptio
@Override
protected void internalPoll() throws FreeboxException {
super.internalPoll();
poll();
}

@Override
protected void internalForcePoll() throws FreeboxException {
super.internalForcePoll();
poll();
}

private void poll() throws FreeboxException {
if (!thing.getStatus().equals(ThingStatus.ONLINE)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -75,7 +76,11 @@ public void updateVmChannels(VirtualMachine vm) {
boolean running = Status.RUNNING.equals(vm.status());
updateChannelOnOff(VM_STATUS, STATUS, running);
updateChannelOnOff(CONNECTIVITY, REACHABLE, running);
updateStatus(running ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
if (running) {
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-vm-not-running");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ public WifiStationHandler(Thing thing) {
@Override
protected void internalPoll() throws FreeboxException {
super.internalPoll();
poll();
}

@Override
protected void internalForcePoll() throws FreeboxException {
super.internalForcePoll();
poll();
}

private void poll() throws FreeboxException {
MACAddress mac = getMac();
if (mac == null) {
throw new FreeboxException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ channel-type.freeboxos.wifi-status.description = Indicates whether the wifi netw
# messages

info-conf-pending = Please accept pairing request directly on your freebox
info-host-not-reachable = Host is not reachable
info-player-not-reachable = Player is not reachable
info-vm-not-running = VM is not running

# iconprovider

Expand Down

0 comments on commit 6eb45a2

Please sign in to comment.