[freeboxos] Avoid useless API requests (server/player) (#17237)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
lolodomo 2024-08-13 16:38:26 +02:00 committed by Ciprian Pascu
parent 53828a12d9
commit 81261c6da8
10 changed files with 106 additions and 65 deletions

View File

@ -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";
@ -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";

View File

@ -17,6 +17,7 @@ import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*
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;
@ -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
@ -90,12 +91,14 @@ public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
}
if (player.reachable()) {
if (anyChannelLinked(GROUP_PLAYER_STATUS, Set.of(PLAYER_STATUS, PACKAGE))) {
Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
if (status != null) {
updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
updateChannelString(GROUP_PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
ForegroundApp foreground = status.foregroundApp();
if (foreground != null) {
updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package());
updateChannelString(GROUP_PLAYER_STATUS, PACKAGE, foreground._package());
}
}
}
Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
@ -105,7 +108,7 @@ public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf
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");

View File

@ -17,6 +17,7 @@ import java.time.ZonedDateTime;
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;
@ -355,6 +356,12 @@ public abstract class ApiConsumerHandler extends BaseThingHandler implements Api
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();

View File

@ -14,6 +14,7 @@ package org.openhab.binding.freeboxos.internal.handler;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -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);

View File

@ -119,9 +119,9 @@ public class HostHandler extends ApiConsumerHandler {
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);

View File

@ -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
@ -84,21 +84,21 @@ public class RepeaterHandler extends HostHandler implements FreeDeviceIntf {
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);
}

View File

@ -15,6 +15,8 @@ package org.openhab.binding.freeboxos.internal.handler;
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;
@ -66,10 +68,12 @@ public class RevolutionHandler extends ServerHandler {
@Override
protected void internalPoll() throws FreeboxException {
super.internalPoll();
if (anyChannelLinked(GROUP_DISPLAY, Set.of(LCD_BRIGHTNESS, LCD_ORIENTATION, LCD_FORCED))) {
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());
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 {

View File

@ -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
@ -115,15 +115,27 @@ public class ServerHandler extends ApiConsumerHandler implements FreeDeviceIntf
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());
}
if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(SAMBA_FILE_STATUS, SAMBA_PRINTER_STATUS))) {
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());
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 {
@ -133,33 +145,41 @@ public class ServerHandler extends ApiConsumerHandler implements FreeDeviceIntf
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);
if (anyChannelLinked(GROUP_SYS_INFO, Set.of(IP_ADDRESS))) {
LanConfig lanConfig = getManager(LanManager.class).getConfig();
updateChannelString(SYS_INFO, IP_ADDRESS, lanConfig.ip());
updateChannelString(GROUP_SYS_INFO, IP_ADDRESS, lanConfig.ip());
}
}
private void fetchConnectionStatus() throws FreeboxException {
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(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());
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(CONNECTION_STATUS, BYTES_UP, new QuantityType<>(status.bytesUp(), OCTET), GIBIOCTET);
updateChannelQuantity(CONNECTION_STATUS, BYTES_DOWN, new QuantityType<>(status.bytesDown(), OCTET), GIBIOCTET);
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);
@ -171,27 +191,29 @@ public class ServerHandler extends ApiConsumerHandler implements FreeDeviceIntf
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;

View File

@ -74,8 +74,8 @@ public class VmHandler extends HostHandler {
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 {

View File

@ -77,7 +77,7 @@ public class WifiStationHandler extends HostHandler {
Optional<Station> station = getManager(APManager.class).getStation(mac);
if (station.isPresent()) {
Station data = station.get();
updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, data.getLastSeen());
updateChannelDateTimeState(GROUP_CONNECTIVITY, LAST_SEEN, data.getLastSeen());
updateChannelString(GROUP_WIFI, WIFI_HOST, SERVER_HOST);
updateWifiStationChannels(data.signal(), data.getSsid(), data.rxRate(), data.txRate());
return;
@ -86,7 +86,7 @@ public class WifiStationHandler extends HostHandler {
// Search if it is hosted by a repeater
Optional<LanHost> wifiHost = getManager(RepeaterManager.class).getHost(mac);
if (wifiHost.isPresent()) {
updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, wifiHost.get().getLastSeen());
updateChannelDateTimeState(GROUP_CONNECTIVITY, LAST_SEEN, wifiHost.get().getLastSeen());
LanAccessPoint lanAp = wifiHost.get().accessPoint();
if (lanAp != null) {
updateChannelString(GROUP_WIFI, WIFI_HOST, "%s-%s".formatted(lanAp.type(), lanAp.uid()));