Skip to content

Commit b926e4c

Browse files
authored
[freeboxos] Mark unknown freeplug mac as OFFLINE (#20611)
* Check if the freeplug mac is known by REST api. Signed-off-by: gael@lhopital.org <gael@lhopital.org>
1 parent d21e43c commit b926e4c

3 files changed

Lines changed: 33 additions & 25 deletions

File tree

bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeplugManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.THING_FREEPLUG;
1616

1717
import java.util.List;
18-
import java.util.Optional;
1918

2019
import org.eclipse.jdt.annotation.NonNullByDefault;
20+
import org.eclipse.jdt.annotation.Nullable;
2121
import org.openhab.binding.freeboxos.internal.api.FreeboxException;
2222
import org.openhab.binding.freeboxos.internal.api.Response;
2323

@@ -73,8 +73,8 @@ public List<Freeplug> getPlugs() throws FreeboxException {
7373
return get(Networks.class).stream().map(Network::members).flatMap(List::stream).toList();
7474
}
7575

76-
public Optional<Freeplug> getPlug(MACAddress mac) throws FreeboxException {
77-
return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst();
76+
public @Nullable Freeplug getPlug(MACAddress mac) throws FreeboxException {
77+
return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst().orElse(null);
7878
}
7979

8080
public void reboot(MACAddress mac) throws FreeboxException {

bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
import org.openhab.binding.freeboxos.internal.action.FreeplugActions;
2626
import org.openhab.binding.freeboxos.internal.api.FreeboxException;
2727
import org.openhab.binding.freeboxos.internal.api.rest.FreeplugManager;
28+
import org.openhab.binding.freeboxos.internal.api.rest.FreeplugManager.Freeplug;
2829
import org.openhab.core.library.types.QuantityType;
2930
import org.openhab.core.library.unit.Units;
3031
import org.openhab.core.thing.Channel;
3132
import org.openhab.core.thing.Thing;
33+
import org.openhab.core.thing.ThingStatus;
34+
import org.openhab.core.thing.ThingStatusDetail;
3235
import org.openhab.core.thing.binding.ThingHandlerService;
3336
import org.slf4j.Logger;
3437
import org.slf4j.LoggerFactory;
@@ -47,17 +50,21 @@ public class FreeplugHandler extends ApiConsumerHandler {
4750

4851
public FreeplugHandler(Thing thing) {
4952
super(thing);
53+
statusDrivenByBridge = false;
5054
}
5155

52-
@Override
53-
void initializeProperties(Map<String, String> properties) throws FreeboxException {
56+
private MACAddress getCheckMac() throws FreeboxException {
5457
MACAddress mac = getMac();
5558
if (mac == null) {
56-
throw new FreeboxException(
57-
"initializeProperties is not possible because MAC address is undefined for the thing "
58-
+ thing.getUID());
59+
throw new FreeboxException("MAC address is undefined for the thing " + thing.getUID());
5960
}
60-
getManager(FreeplugManager.class).getPlug(mac).ifPresent(plug -> {
61+
return mac;
62+
}
63+
64+
@Override
65+
void initializeProperties(Map<String, String> properties) throws FreeboxException {
66+
MACAddress mac = getCheckMac();
67+
if (getManager(FreeplugManager.class).getPlug(mac) instanceof Freeplug plug) {
6168
properties.put(Thing.PROPERTY_MODEL_ID, plug.model());
6269
properties.put(ROLE, plug.netRole().name());
6370
properties.put(NET_ID, plug.netId());
@@ -73,25 +80,30 @@ void initializeProperties(Map<String, String> properties) throws FreeboxExceptio
7380
updateThing(editThing().withChannels(channels).build());
7481
}
7582
}
76-
});
83+
} else {
84+
throw new FreeboxException("Freeplug is absent");
85+
}
7786
}
7887

7988
@Override
8089
protected void internalPoll() throws FreeboxException {
81-
MACAddress mac = getMac();
82-
if (mac == null) {
83-
throw new FreeboxException(
84-
"internalPoll is not possible because MAC address is undefined for the thing " + thing.getUID());
85-
}
86-
getManager(FreeplugManager.class).getPlug(mac).ifPresent(plug -> {
90+
MACAddress mac = getCheckMac();
91+
if (getManager(FreeplugManager.class).getPlug(mac) instanceof Freeplug plug) {
8792
updateChannelDateTimeState(LAST_SEEN, Instant.now().minusSeconds(plug.inactive()));
8893

8994
updateChannelString(LINE_STATUS, plug.ethPortStatus());
9095
updateChannelOnOff(REACHABLE, plug.hasNetwork());
9196

9297
updateRateChannel(RATE + "-down", plug.rxRate());
9398
updateRateChannel(RATE + "-up", plug.txRate());
94-
});
99+
if (plug.hasNetwork()) {
100+
updateStatus(ThingStatus.ONLINE);
101+
} else {
102+
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-plug-not-reachable");
103+
}
104+
} else {
105+
throw new FreeboxException("Freeplug is absent");
106+
}
95107
}
96108

97109
private void updateRateChannel(String channel, int rate) {
@@ -100,17 +112,12 @@ private void updateRateChannel(String channel, int rate) {
100112
}
101113

102114
public void reset() {
103-
MACAddress mac = getMac();
104-
if (mac == null) {
105-
logger.warn("Freeplug restart is not possible because MAC address is undefined for the thing {}",
106-
thing.getUID());
107-
return;
108-
}
109115
try {
116+
MACAddress mac = getCheckMac();
110117
getManager(FreeplugManager.class).reboot(mac);
111-
logger.debug("Freeplug {} succesfully restarted", mac);
118+
logger.debug("Freeplug {} successfully restarted", mac);
112119
} catch (FreeboxException e) {
113-
logger.warn("Error restarting freeplug {}: {}", mac, e.getMessage());
120+
logger.warn("Error restarting freeplug {}: {}", thing.getUID(), e.getMessage());
114121
}
115122
}
116123

bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ channel-type.freeboxos.xdsl-status.state.option.DISABLED = Disabled
424424

425425
info-conf-pending = Please accept pairing request directly on your freebox
426426
info-host-not-reachable = Host is not reachable
427+
info-plug-not-reachable = Freeplug is not reachable
427428
info-player-not-reachable = Player is not reachable
428429
info-vm-not-running = VM is not running
429430

0 commit comments

Comments
 (0)