2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16 import static org.openhab.core.library.unit.Units.*;
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.freeboxos.internal.action.ServerActions;
27 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
28 import org.openhab.binding.freeboxos.internal.api.rest.AfpManager;
29 import org.openhab.binding.freeboxos.internal.api.rest.AirMediaManager;
30 import org.openhab.binding.freeboxos.internal.api.rest.ConnectionManager;
31 import org.openhab.binding.freeboxos.internal.api.rest.ConnectionManager.FtthStatus;
32 import org.openhab.binding.freeboxos.internal.api.rest.ConnectionManager.Media;
33 import org.openhab.binding.freeboxos.internal.api.rest.ConnectionManager.Status;
34 import org.openhab.binding.freeboxos.internal.api.rest.FtpManager;
35 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.Source;
36 import org.openhab.binding.freeboxos.internal.api.rest.LanManager;
37 import org.openhab.binding.freeboxos.internal.api.rest.LanManager.LanConfig;
38 import org.openhab.binding.freeboxos.internal.api.rest.SambaManager;
39 import org.openhab.binding.freeboxos.internal.api.rest.SambaManager.Samba;
40 import org.openhab.binding.freeboxos.internal.api.rest.SystemManager;
41 import org.openhab.binding.freeboxos.internal.api.rest.SystemManager.Config;
42 import org.openhab.binding.freeboxos.internal.api.rest.UPnPAVManager;
43 import org.openhab.binding.freeboxos.internal.api.rest.WifiManager;
44 import org.openhab.core.library.types.QuantityType;
45 import org.openhab.core.library.unit.SIUnits;
46 import org.openhab.core.library.unit.Units;
47 import org.openhab.core.thing.Channel;
48 import org.openhab.core.thing.ChannelUID;
49 import org.openhab.core.thing.Thing;
50 import org.openhab.core.thing.binding.ThingHandlerService;
51 import org.openhab.core.thing.binding.builder.ChannelBuilder;
52 import org.openhab.core.thing.type.ChannelTypeUID;
53 import org.openhab.core.types.Command;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * The {@link ServerHandler} handle common parts of Freebox bridges.
60 * @author Gaël L'hopital - Initial contribution
63 public class ServerHandler extends ApiConsumerHandler implements FreeDeviceIntf {
64 private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
66 private final Logger logger = LoggerFactory.getLogger(ServerHandler.class);
67 private final ChannelUID eventChannelUID;
69 private long uptime = -1;
71 public ServerHandler(Thing thing) {
73 eventChannelUID = new ChannelUID(getThing().getUID(), GROUP_SYS_INFO, BOX_EVENT);
77 void initializeProperties(Map<String, String> properties) throws FreeboxException {
78 LanConfig lanConfig = getManager(LanManager.class).getConfig();
79 Config config = getManager(SystemManager.class).getConfig();
81 properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
82 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
83 properties.put(Thing.PROPERTY_HARDWARE_VERSION, config.modelInfo().prettyName());
84 properties.put(Thing.PROPERTY_MAC_ADDRESS, config.mac().toColonDelimitedString());
85 properties.put(Source.UPNP.name(), lanConfig.name());
87 List<Channel> channels = new ArrayList<>(getThing().getChannels());
89 // Remove channels of the not active media type
90 Status connectionConfig = getManager(ConnectionManager.class).getConfig();
91 channels.removeIf(c -> (GROUP_FTTH.equals(c.getUID().getGroupId()) && connectionConfig.media() != Media.FTTH)
92 || (GROUP_XDSL.equals(c.getUID().getGroupId()) && connectionConfig.media() != Media.XDSL));
94 // Add temperature sensors
95 config.sensors().forEach(sensor -> {
96 ChannelUID sensorId = new ChannelUID(thing.getUID(), GROUP_SENSORS, sensor.id());
97 if (getThing().getChannel(sensorId) == null) {
98 String label = sensor.name();
99 // For revolution, API returns only "Disque dur" so we patch it to have naming consistency with other
100 // temperature sensors
101 if ("Disque dur".equals(label)) {
102 label = "Température " + label;
104 channels.add(ChannelBuilder.create(sensorId).withLabel(label).withAcceptedItemType("Number:Temperature")
105 .withType(new ChannelTypeUID(BINDING_ID, "temperature")).build());
110 config.fans().forEach(sensor -> {
111 ChannelUID sensorId = new ChannelUID(thing.getUID(), GROUP_FANS, sensor.id());
112 if (getThing().getChannel(sensorId) == null) {
113 channels.add(ChannelBuilder.create(sensorId).withLabel(sensor.name())
114 .withAcceptedItemType("Number:Frequency").withType(new ChannelTypeUID(BINDING_ID, "fanspeed"))
119 // And finally update the thing with appropriate channels
120 updateThing(editThing().withChannels(channels).build());
124 protected void internalPoll() throws FreeboxException {
125 logger.debug("Polling server state...");
126 fetchConnectionStatus();
129 if (anyChannelLinked(GROUP_ACTIONS, Set.of(WIFI_STATUS))) {
130 updateChannelOnOff(GROUP_ACTIONS, WIFI_STATUS, getManager(WifiManager.class).getStatus());
132 if (anyChannelLinked(GROUP_ACTIONS, Set.of(AIRMEDIA_STATUS))) {
133 updateChannelOnOff(GROUP_ACTIONS, AIRMEDIA_STATUS, getManager(AirMediaManager.class).getStatus());
135 if (anyChannelLinked(GROUP_ACTIONS, Set.of(UPNPAV_STATUS))) {
136 updateChannelOnOff(GROUP_ACTIONS, UPNPAV_STATUS, getManager(UPnPAVManager.class).getStatus());
139 if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(SAMBA_FILE_STATUS, SAMBA_PRINTER_STATUS))) {
140 Samba response = getManager(SambaManager.class).getConfig();
141 updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_FILE_STATUS, response.fileShareEnabled());
142 updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_PRINTER_STATUS, response.printShareEnabled());
144 if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(FTP_STATUS))) {
145 updateChannelOnOff(GROUP_FILE_SHARING, FTP_STATUS, getManager(FtpManager.class).getStatus());
147 if (anyChannelLinked(GROUP_FILE_SHARING, Set.of(AFP_FILE_STATUS))) {
148 updateChannelOnOff(GROUP_FILE_SHARING, AFP_FILE_STATUS, getManager(AfpManager.class).getStatus());
152 private void fetchSystemConfig() throws FreeboxException {
153 Config config = getManager(SystemManager.class).getConfig();
155 config.sensors().forEach(s -> updateChannelQuantity(GROUP_SENSORS, s.id(), s.value(), SIUnits.CELSIUS));
156 config.fans().forEach(f -> updateChannelQuantity(GROUP_FANS, f.id(), f.value(), Units.RPM));
158 uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
159 updateChannelQuantity(GROUP_SYS_INFO, UPTIME, uptime, Units.SECOND);
161 if (anyChannelLinked(GROUP_SYS_INFO, Set.of(IP_ADDRESS))) {
162 LanConfig lanConfig = getManager(LanManager.class).getConfig();
163 updateChannelString(GROUP_SYS_INFO, IP_ADDRESS, lanConfig.ip());
167 private void fetchConnectionStatus() throws FreeboxException {
168 if (anyChannelLinked(GROUP_CONNECTION_STATUS,
169 Set.of(LINE_STATUS, LINE_TYPE, LINE_MEDIA, IP_ADDRESS, IPV6_ADDRESS, BYTES_UP, BYTES_DOWN, RATE + "-up",
170 BW + "-up", PCT_BW + "-up", RATE + "-down", BW + "-down", PCT_BW + "-down"))) {
171 Status status = getManager(ConnectionManager.class).getConfig();
172 updateChannelString(GROUP_CONNECTION_STATUS, LINE_STATUS, status.state());
173 updateChannelString(GROUP_CONNECTION_STATUS, LINE_TYPE, status.type());
174 updateChannelString(GROUP_CONNECTION_STATUS, LINE_MEDIA, status.media());
175 updateChannelString(GROUP_CONNECTION_STATUS, IP_ADDRESS, status.ipv4());
176 updateChannelString(GROUP_CONNECTION_STATUS, IPV6_ADDRESS, status.ipv6());
177 updateRateBandwidth(status.rateUp(), status.bandwidthUp(), "up");
178 updateRateBandwidth(status.rateDown(), status.bandwidthDown(), "down");
180 updateChannelQuantity(GROUP_CONNECTION_STATUS, BYTES_UP, new QuantityType<>(status.bytesUp(), OCTET),
182 updateChannelQuantity(GROUP_CONNECTION_STATUS, BYTES_DOWN, new QuantityType<>(status.bytesDown(), OCTET),
185 if (anyChannelLinked(GROUP_FTTH,
186 Set.of(SFP_PRESENT, SFP_ALIM, SFP_POWER, SFP_SIGNAL, SFP_LINK, SFP_PWR_TX, SFP_PWR_RX))) {
187 FtthStatus ftthStatus = getManager(ConnectionManager.class).getFtthStatus();
188 updateChannelOnOff(GROUP_FTTH, SFP_PRESENT, ftthStatus.sfpPresent());
189 updateChannelOnOff(GROUP_FTTH, SFP_ALIM, ftthStatus.sfpAlimOk());
190 updateChannelOnOff(GROUP_FTTH, SFP_POWER, ftthStatus.sfpHasPowerReport());
191 updateChannelOnOff(GROUP_FTTH, SFP_SIGNAL, ftthStatus.sfpHasSignal());
192 updateChannelOnOff(GROUP_FTTH, SFP_LINK, ftthStatus.link());
193 updateChannelQuantity(GROUP_FTTH, SFP_PWR_TX, ftthStatus.getTransmitDBM(), Units.DECIBEL_MILLIWATTS);
194 updateChannelQuantity(GROUP_FTTH, SFP_PWR_RX, ftthStatus.getReceivedDBM(), Units.DECIBEL_MILLIWATTS);
198 private void updateRateBandwidth(long rate, long bandwidth, String orientation) {
199 QuantityType<?> rateUp = new QuantityType<>(rate * 8, Units.BIT_PER_SECOND);
200 QuantityType<?> bandwidthUp = new QuantityType<>(bandwidth, BIT_PER_SECOND);
201 updateChannelQuantity(GROUP_CONNECTION_STATUS, RATE + "-" + orientation, rateUp, KILOBIT_PER_SECOND);
202 updateChannelQuantity(GROUP_CONNECTION_STATUS, BW + "-" + orientation, bandwidthUp, KILOBIT_PER_SECOND);
203 updateChannelQuantity(GROUP_CONNECTION_STATUS, PCT_BW + "-" + orientation,
204 !bandwidthUp.equals(QuantityType.ZERO) ? rateUp.multiply(HUNDRED).divide(bandwidthUp)
210 protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
211 if (ON_OFF_CLASSES.contains(command.getClass())) {
212 boolean enable = TRUE_COMMANDS.contains(command);
215 updateChannelOnOff(GROUP_ACTIONS, WIFI_STATUS, getManager(WifiManager.class).setStatus(enable));
218 updateChannelOnOff(GROUP_FILE_SHARING, FTP_STATUS, getManager(FtpManager.class).setStatus(enable));
220 case SAMBA_FILE_STATUS:
221 updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_FILE_STATUS,
222 getManager(SambaManager.class).setFileShare(enable));
224 case SAMBA_PRINTER_STATUS:
225 updateChannelOnOff(GROUP_FILE_SHARING, SAMBA_PRINTER_STATUS,
226 getManager(SambaManager.class).setPrintShare(enable));
229 updateChannelOnOff(GROUP_ACTIONS, UPNPAV_STATUS, getManager(UPnPAVManager.class).setStatus(enable));
231 case AFP_FILE_STATUS:
232 updateChannelOnOff(GROUP_FILE_SHARING, AFP_FILE_STATUS,
233 getManager(AfpManager.class).setStatus(enable));
235 case AIRMEDIA_STATUS:
236 updateChannelOnOff(GROUP_ACTIONS, AIRMEDIA_STATUS,
237 getManager(AirMediaManager.class).setStatus(enable));
243 return super.internalHandleCommand(channelId, command);
246 public void reboot() {
247 processReboot(() -> {
249 getManager(SystemManager.class).reboot();
250 } catch (FreeboxException e) {
251 logger.warn("Error rebooting: {}", e.getMessage());
257 public Collection<Class<? extends ThingHandlerService>> getServices() {
258 return Set.of(ServerActions.class);
262 public ChannelUID getEventChannelUID() {
263 return eventChannelUID;
267 public void triggerChannel(ChannelUID channelUID, String event) {
268 super.triggerChannel(channelUID, event);