Skip to content

Commit

Permalink
[freeboxos] Avoid useless API requests (server/player) (openhab#17237)
Browse files Browse the repository at this point in the history
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 6eb45a2 commit fb4a504
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ public class FreeboxOsBindingConstants {
// List of all Group Channel ids
public static final String GROUP_SENSORS = "sensors";
public static final String GROUP_FANS = "fans";
public static final String CONNECTION_STATUS = "connection-status";
public static final String SYS_INFO = "sysinfo";
public static final String ACTIONS = "actions";
public static final String FILE_SHARING = "file-sharing";
public static final String CONNECTIVITY = "connectivity";
public static final String DISPLAY = "display";
public static final String VM_STATUS = "vmstatus";
public static final String GROUP_CONNECTION_STATUS = "connection-status";
public static final String GROUP_SYS_INFO = "sysinfo";
public static final String GROUP_ACTIONS = "actions";
public static final String GROUP_PLAYER_STATUS = "player-status";
public static final String GROUP_FILE_SHARING = "file-sharing";
public static final String GROUP_CONNECTIVITY = "connectivity";
public static final String GROUP_DISPLAY = "display";
public static final String GROUP_VM_STATUS = "vmstatus";
public static final String GROUP_WIFI = "wifi";
public static final String REPEATER_MISC = "repeater-misc";
public static final String GROUP_REPEATER_MISC = "repeater-misc";

// List of all Channel ids
public static final String RSSI = "rssi";
Expand All @@ -122,6 +123,7 @@ public class FreeboxOsBindingConstants {
public static final String BYTES_DOWN = "bytes-down";
public static final String BW = "bandwidth";
public static final String PCT_BW = "bandwidth-usage";
public static final String CONNECTION_STATUS = "connection-status";
public static final String ONHOOK = "onhook";
public static final String RINGING = "ringing";
public static final String HARDWARE_STATUS = "hardware-status";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.freeboxos.internal.action.ActivePlayerActions;
Expand Down Expand Up @@ -52,7 +53,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);
eventChannelUID = new ChannelUID(getThing().getUID(), GROUP_SYS_INFO, BOX_EVENT);
}

@Override
Expand Down Expand Up @@ -90,12 +91,14 @@ private void poll() throws FreeboxException {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
}
if (player.reachable()) {
Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
if (status != null) {
updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
ForegroundApp foreground = status.foregroundApp();
if (foreground != null) {
updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package());
if (anyChannelLinked(GROUP_PLAYER_STATUS, Set.of(PLAYER_STATUS, PACKAGE))) {
Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
if (status != null) {
updateChannelString(GROUP_PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
ForegroundApp foreground = status.foregroundApp();
if (foreground != null) {
updateChannelString(GROUP_PLAYER_STATUS, PACKAGE, foreground._package());
}
}
}
Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
Expand All @@ -105,7 +108,7 @@ private void poll() throws FreeboxException {
uptime = 0;
}
}
updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
updateChannelQuantity(GROUP_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 @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -355,6 +356,12 @@ public void updateProperties(@Nullable Map<String, String> properties) {
super.updateProperties(properties);
}

@Override
public boolean anyChannelLinked(String groupId, Set<String> channelSet) {
return channelSet.stream().map(id -> new ChannelUID(getThing().getUID(), groupId, id))
.anyMatch(uid -> isLinked(uid));
}

@Override
public Configuration getConfig() {
return super.getConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -45,6 +46,8 @@ public interface ApiConsumerIntf extends ThingHandler {

void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description);

boolean anyChannelLinked(String groupId, Set<String> channelSet);

void stopJobs();

void addJob(String name, Runnable command, long initialDelay, long delay, TimeUnit unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ 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());
updateChannelOnOff(GROUP_CONNECTIVITY, REACHABLE, host.reachable());
updateChannelDateTimeState(GROUP_CONNECTIVITY, LAST_SEEN, host.getLastSeen());
updateChannelString(GROUP_CONNECTIVITY, IP_ADDRESS, host.getIpv4());
if (statusDrivenByLanConnectivity) {
if (host.reachable()) {
updateStatus(ThingStatus.ONLINE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class RepeaterHandler extends HostHandler implements FreeDeviceIntf {

public RepeaterHandler(Thing thing) {
super(thing);
eventChannelUID = new ChannelUID(getThing().getUID(), REPEATER_MISC, BOX_EVENT);
eventChannelUID = new ChannelUID(getThing().getUID(), GROUP_REPEATER_MISC, BOX_EVENT);
}

@Override
Expand Down Expand Up @@ -84,21 +84,21 @@ private void poll() throws FreeboxException {
RepeaterManager repeaterManager = getManager(RepeaterManager.class);

Repeater repeater = repeaterManager.getDevice(getClientId());
updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated());
updateChannelString(REPEATER_MISC, CONNECTION_STATUS, repeater.connection());
updateChannelOnOff(GROUP_REPEATER_MISC, LED, repeater.ledActivated());
updateChannelString(GROUP_REPEATER_MISC, CONNECTION_STATUS, repeater.connection());

List<LanHost> hosts = repeaterManager.getRepeaterHosts(getClientId());
updateChannelDecimal(REPEATER_MISC, HOST_COUNT, hosts.size());
updateChannelDecimal(GROUP_REPEATER_MISC, HOST_COUNT, hosts.size());

uptime = checkUptimeAndFirmware(repeater.getUptimeVal(), uptime, repeater.firmwareVersion());
updateChannelQuantity(REPEATER_MISC, UPTIME, uptime, Units.SECOND);
updateChannelQuantity(GROUP_REPEATER_MISC, UPTIME, uptime, Units.SECOND);
}

@Override
protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
if (ON_OFF_CLASSES.contains(command.getClass()) && LED.equals(channelId)) {
getManager(RepeaterManager.class).led(getClientId(), TRUE_COMMANDS.contains(command))
.ifPresent(repeater -> updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated()));
.ifPresent(repeater -> updateChannelOnOff(GROUP_REPEATER_MISC, LED, repeater.ledActivated()));
}
return super.internalHandleCommand(channelId, command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
import static org.openhab.core.library.unit.Units.PERCENT;

import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.freeboxos.internal.api.FreeboxException;
import org.openhab.binding.freeboxos.internal.api.rest.LcdManager;
Expand Down Expand Up @@ -66,10 +68,12 @@ protected boolean internalHandleCommand(String channelId, Command command) throw
@Override
protected void internalPoll() throws FreeboxException {
super.internalPoll();
Config config = getManager(LcdManager.class).getConfig();
updateChannelQuantity(DISPLAY, LCD_BRIGHTNESS, config.brightness(), PERCENT);
updateChannelDecimal(DISPLAY, LCD_ORIENTATION, config.orientation());
updateChannelOnOff(DISPLAY, LCD_FORCED, config.orientationForced());
if (anyChannelLinked(GROUP_DISPLAY, Set.of(LCD_BRIGHTNESS, LCD_ORIENTATION, LCD_FORCED))) {
Config config = getManager(LcdManager.class).getConfig();
updateChannelQuantity(GROUP_DISPLAY, LCD_BRIGHTNESS, config.brightness(), PERCENT);
updateChannelDecimal(GROUP_DISPLAY, LCD_ORIENTATION, config.orientation());
updateChannelOnOff(GROUP_DISPLAY, LCD_FORCED, config.orientationForced());
}
}

private void setOrientation(LcdManager manager, Config config, Command command) throws FreeboxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class ServerHandler extends ApiConsumerHandler implements FreeDeviceIntf

public ServerHandler(Thing thing) {
super(thing);
eventChannelUID = new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT);
eventChannelUID = new ChannelUID(getThing().getUID(), GROUP_SYS_INFO, BOX_EVENT);
}

@Override
Expand Down Expand Up @@ -115,15 +115,27 @@ protected void internalPoll() throws FreeboxException {
fetchConnectionStatus();
fetchSystemConfig();

updateChannelOnOff(ACTIONS, WIFI_STATUS, getManager(WifiManager.class).getStatus());
updateChannelOnOff(ACTIONS, AIRMEDIA_STATUS, getManager(AirMediaManager.class).getStatus());
updateChannelOnOff(ACTIONS, UPNPAV_STATUS, getManager(UPnPAVManager.class).getStatus());
if (anyChannelLinked(GROUP_ACTIONS, Set.of(WIFI_STATUS))) {
updateChannelOnOff(GROUP_ACTIONS, WIFI_STATUS, getManager(WifiManager.class).getStatus());
}
if (anyChannelLinked(GROUP_ACTIONS, Set.of(AIRMEDIA_STATUS))) {
updateChannelOnOff(GROUP_ACTIONS, AIRMEDIA_STATUS, getManager(AirMediaManager.class).getStatus());
}
if (anyChannelLinked(GROUP_ACTIONS, Set.of(UPNPAV_STATUS))) {
updateChannelOnOff(GROUP_ACTIONS, UPNPAV_STATUS, getManager(UPnPAVManager.class).getStatus());
}

Samba response = getManager(SambaManager.class).getConfig();
updateChannelOnOff(FILE_SHARING, SAMBA_FILE_STATUS, response.fileShareEnabled());
updateChannelOnOff(FILE_SHARING, SAMBA_PRINTER_STATUS, response.printShareEnabled());
updateChannelOnOff(FILE_SHARING, FTP_STATUS, getManager(FtpManager.class).getStatus());
updateChannelOnOff(FILE_SHARING, AFP_FILE_STATUS, getManager(AfpManager.class).getStatus());
if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(SAMBA_FILE_STATUS, SAMBA_PRINTER_STATUS))) {
Samba response = getManager(SambaManager.class).getConfig();
updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_FILE_STATUS, response.fileShareEnabled());
updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_PRINTER_STATUS, response.printShareEnabled());
}
if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(FTP_STATUS))) {
updateChannelOnOff(GROUP_FILE_SHARING, FTP_STATUS, getManager(FtpManager.class).getStatus());
}
if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(AFP_FILE_STATUS))) {
updateChannelOnOff(GROUP_FILE_SHARING, AFP_FILE_STATUS, getManager(AfpManager.class).getStatus());
}
}

private void fetchSystemConfig() throws FreeboxException {
Expand All @@ -133,33 +145,41 @@ private void fetchSystemConfig() throws FreeboxException {
config.fans().forEach(f -> updateChannelQuantity(GROUP_FANS, f.id(), f.value(), Units.RPM));

uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
updateChannelQuantity(GROUP_SYS_INFO, UPTIME, uptime, Units.SECOND);

LanConfig lanConfig = getManager(LanManager.class).getConfig();
updateChannelString(SYS_INFO, IP_ADDRESS, lanConfig.ip());
if (anyChannelLinked(GROUP_SYS_INFO, Set.of(IP_ADDRESS))) {
LanConfig lanConfig = getManager(LanManager.class).getConfig();
updateChannelString(GROUP_SYS_INFO, IP_ADDRESS, lanConfig.ip());
}
}

private void fetchConnectionStatus() throws FreeboxException {
Status status = getManager(ConnectionManager.class).getConfig();
updateChannelString(CONNECTION_STATUS, LINE_STATUS, status.state());
updateChannelString(CONNECTION_STATUS, LINE_TYPE, status.type());
updateChannelString(CONNECTION_STATUS, LINE_MEDIA, status.media());
updateChannelString(CONNECTION_STATUS, IP_ADDRESS, status.ipv4());
updateChannelString(CONNECTION_STATUS, IPV6_ADDRESS, status.ipv6());

updateRateBandwidth(status.rateUp(), status.bandwidthUp(), "up");
updateRateBandwidth(status.rateDown(), status.bandwidthDown(), "down");

updateChannelQuantity(CONNECTION_STATUS, BYTES_UP, new QuantityType<>(status.bytesUp(), OCTET), GIBIOCTET);
updateChannelQuantity(CONNECTION_STATUS, BYTES_DOWN, new QuantityType<>(status.bytesDown(), OCTET), GIBIOCTET);
if (anyChannelLinked(GROUP_CONNECTION_STATUS,
Set.of(LINE_STATUS, LINE_TYPE, LINE_MEDIA, IP_ADDRESS, IPV6_ADDRESS, BYTES_UP, BYTES_DOWN, RATE + "-up",
BW + "-up", PCT_BW + "-up", RATE + "-down", BW + "-down", PCT_BW + "-down"))) {
Status status = getManager(ConnectionManager.class).getConfig();
updateChannelString(GROUP_CONNECTION_STATUS, LINE_STATUS, status.state());
updateChannelString(GROUP_CONNECTION_STATUS, LINE_TYPE, status.type());
updateChannelString(GROUP_CONNECTION_STATUS, LINE_MEDIA, status.media());
updateChannelString(GROUP_CONNECTION_STATUS, IP_ADDRESS, status.ipv4());
updateChannelString(GROUP_CONNECTION_STATUS, IPV6_ADDRESS, status.ipv6());

updateRateBandwidth(status.rateUp(), status.bandwidthUp(), "up");
updateRateBandwidth(status.rateDown(), status.bandwidthDown(), "down");

updateChannelQuantity(GROUP_CONNECTION_STATUS, BYTES_UP, new QuantityType<>(status.bytesUp(), OCTET),
GIBIOCTET);
updateChannelQuantity(GROUP_CONNECTION_STATUS, BYTES_DOWN, new QuantityType<>(status.bytesDown(), OCTET),
GIBIOCTET);
}
}

private void updateRateBandwidth(long rate, long bandwidth, String orientation) {
QuantityType<?> rateUp = new QuantityType<>(rate * 8, Units.BIT_PER_SECOND);
QuantityType<?> bandwidthUp = new QuantityType<>(bandwidth, BIT_PER_SECOND);
updateChannelQuantity(CONNECTION_STATUS, RATE + "-" + orientation, rateUp, KILOBIT_PER_SECOND);
updateChannelQuantity(CONNECTION_STATUS, BW + "-" + orientation, bandwidthUp, KILOBIT_PER_SECOND);
updateChannelQuantity(CONNECTION_STATUS, PCT_BW + "-" + orientation,
updateChannelQuantity(GROUP_CONNECTION_STATUS, RATE + "-" + orientation, rateUp, KILOBIT_PER_SECOND);
updateChannelQuantity(GROUP_CONNECTION_STATUS, BW + "-" + orientation, bandwidthUp, KILOBIT_PER_SECOND);
updateChannelQuantity(GROUP_CONNECTION_STATUS, PCT_BW + "-" + orientation,
!bandwidthUp.equals(QuantityType.ZERO) ? rateUp.multiply(HUNDRED).divide(bandwidthUp)
: QuantityType.ZERO,
Units.PERCENT);
Expand All @@ -171,27 +191,29 @@ protected boolean internalHandleCommand(String channelId, Command command) throw
boolean enable = TRUE_COMMANDS.contains(command);
switch (channelId) {
case WIFI_STATUS:
updateChannelOnOff(ACTIONS, WIFI_STATUS, getManager(WifiManager.class).setStatus(enable));
updateChannelOnOff(GROUP_ACTIONS, WIFI_STATUS, getManager(WifiManager.class).setStatus(enable));
return true;
case FTP_STATUS:
updateChannelOnOff(FILE_SHARING, FTP_STATUS, getManager(FtpManager.class).setStatus(enable));
updateChannelOnOff(GROUP_FILE_SHARING, FTP_STATUS, getManager(FtpManager.class).setStatus(enable));
return true;
case SAMBA_FILE_STATUS:
updateChannelOnOff(FILE_SHARING, SAMBA_FILE_STATUS,
updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_FILE_STATUS,
getManager(SambaManager.class).setFileShare(enable));
return true;
case SAMBA_PRINTER_STATUS:
updateChannelOnOff(FILE_SHARING, SAMBA_PRINTER_STATUS,
updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_PRINTER_STATUS,
getManager(SambaManager.class).setPrintShare(enable));
return true;
case UPNPAV_STATUS:
updateChannelOnOff(ACTIONS, UPNPAV_STATUS, getManager(UPnPAVManager.class).setStatus(enable));
updateChannelOnOff(GROUP_ACTIONS, UPNPAV_STATUS, getManager(UPnPAVManager.class).setStatus(enable));
return true;
case AFP_FILE_STATUS:
updateChannelOnOff(FILE_SHARING, AFP_FILE_STATUS, getManager(AfpManager.class).setStatus(enable));
updateChannelOnOff(GROUP_FILE_SHARING, AFP_FILE_STATUS,
getManager(AfpManager.class).setStatus(enable));
return true;
case AIRMEDIA_STATUS:
updateChannelOnOff(ACTIONS, AIRMEDIA_STATUS, getManager(AirMediaManager.class).setStatus(enable));
updateChannelOnOff(GROUP_ACTIONS, AIRMEDIA_STATUS,
getManager(AirMediaManager.class).setStatus(enable));
return true;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ protected void internalForcePoll() throws FreeboxException {

public void updateVmChannels(VirtualMachine vm) {
boolean running = Status.RUNNING.equals(vm.status());
updateChannelOnOff(VM_STATUS, STATUS, running);
updateChannelOnOff(CONNECTIVITY, REACHABLE, running);
updateChannelOnOff(GROUP_VM_STATUS, STATUS, running);
updateChannelOnOff(GROUP_CONNECTIVITY, REACHABLE, running);
if (running) {
updateStatus(ThingStatus.ONLINE);
} else {
Expand Down
Loading

0 comments on commit fb4a504

Please sign in to comment.