2 * Copyright (c) 2010-2023 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.network.internal.handler;
15 import static org.openhab.binding.network.internal.NetworkBindingConstants.*;
16 import static org.openhab.core.library.unit.Units.*;
18 import java.math.BigDecimal;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.network.internal.SpeedTestConfiguration;
25 import org.openhab.core.library.dimension.DataTransferRate;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.State;
37 import org.openhab.core.types.UnDefType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import fr.bmartel.speedtest.SpeedTestReport;
42 import fr.bmartel.speedtest.SpeedTestSocket;
43 import fr.bmartel.speedtest.inter.ISpeedTestListener;
44 import fr.bmartel.speedtest.model.SpeedTestError;
47 * The {@link SpeedTestHandler } is responsible for launching bandwidth
48 * measurements at a given interval and for given file / size
50 * @author Gaƫl L'hopital - Initial contribution
53 public class SpeedTestHandler extends BaseThingHandler implements ISpeedTestListener {
54 private final Logger logger = LoggerFactory.getLogger(SpeedTestHandler.class);
55 private @Nullable SpeedTestSocket speedTestSocket;
56 private @NonNullByDefault({}) ScheduledFuture<?> refreshTask;
57 private @NonNullByDefault({}) SpeedTestConfiguration configuration;
58 private State bufferedProgress = UnDefType.UNDEF;
61 public SpeedTestHandler(Thing thing) {
66 public void initialize() {
67 configuration = getConfigAs(SpeedTestConfiguration.class);
71 private synchronized void startSpeedTest() {
72 String url = configuration.getDownloadURL();
73 if (speedTestSocket == null && url != null) {
74 logger.debug("Network speedtest started");
75 final SpeedTestSocket socket = new SpeedTestSocket(1500);
76 speedTestSocket = socket;
77 socket.addSpeedTestListener(this);
78 updateState(CHANNEL_TEST_ISRUNNING, OnOffType.ON);
79 updateState(CHANNEL_TEST_START, new DateTimeType());
80 updateState(CHANNEL_TEST_END, UnDefType.NULL);
81 updateProgress(new QuantityType<>(0, Units.PERCENT));
82 socket.startDownload(url);
84 logger.info("A speedtest is already in progress, will retry on next refresh");
88 private synchronized void stopSpeedTest() {
89 updateState(CHANNEL_TEST_ISRUNNING, OnOffType.OFF);
90 updateProgress(UnDefType.NULL);
91 updateState(CHANNEL_TEST_END, new DateTimeType());
92 if (speedTestSocket != null) {
93 SpeedTestSocket socket = speedTestSocket;
95 socket.removeSpeedTestListener(this);
97 speedTestSocket = null;
98 logger.debug("Network speedtest finished");
103 public void onCompletion(final @Nullable SpeedTestReport testReport) {
104 timeouts = configuration.maxTimeout;
105 if (testReport != null) {
106 BigDecimal rate = testReport.getTransferRateBit();
107 QuantityType<DataTransferRate> quantity = new QuantityType<>(rate, BIT_PER_SECOND)
108 .toUnit(MEGABIT_PER_SECOND);
109 if (quantity != null) {
110 switch (testReport.getSpeedTestMode()) {
112 updateState(CHANNEL_RATE_DOWN, quantity);
113 String url = configuration.getUploadURL();
114 if (speedTestSocket != null && url != null) {
115 speedTestSocket.startUpload(configuration.getUploadURL(), configuration.uploadSize);
119 updateState(CHANNEL_RATE_UP, quantity);
130 public void onError(final @Nullable SpeedTestError testError, final @Nullable String errorMessage) {
131 if (SpeedTestError.UNSUPPORTED_PROTOCOL.equals(testError) || SpeedTestError.MALFORMED_URI.equals(testError)) {
132 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMessage);
135 } else if (SpeedTestError.SOCKET_TIMEOUT.equals(testError)) {
138 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Max timeout count reached");
141 logger.warn("Speedtest timed out, {} attempts left. Message '{}'", timeouts, errorMessage);
145 } else if (SpeedTestError.SOCKET_ERROR.equals(testError)
146 || SpeedTestError.INVALID_HTTP_RESPONSE.equals(testError)) {
147 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, errorMessage);
152 logger.warn("Speedtest failed: {}", errorMessage);
157 public void onProgress(float percent, @Nullable SpeedTestReport testReport) {
158 updateProgress(new QuantityType<>(Math.round(percent), Units.PERCENT));
161 private void updateProgress(State state) {
162 if (!state.toString().equals(bufferedProgress.toString())) {
163 bufferedProgress = state;
164 updateState(CHANNEL_TEST_PROGRESS, bufferedProgress);
169 public void handleCommand(ChannelUID channelUID, Command command) {
170 if (command == OnOffType.ON
171 && ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR == getThing().getStatusInfo().getStatusDetail()) {
172 logger.debug("Speedtest was offline, restarting it upon command to do so");
175 if (CHANNEL_TEST_ISRUNNING.equals(channelUID.getId())) {
176 if (command == OnOffType.ON) {
178 } else if (command == OnOffType.OFF) {
182 logger.debug("Command {} is not supported for channel: {}.", command, channelUID.getId());
188 public void dispose() {
192 private void freeRefreshTask() {
194 if (refreshTask != null) {
195 refreshTask.cancel(true);
200 private void startRefreshTask() {
201 logger.info("Speedtests starts in {} minutes, then refreshes every {} minutes", configuration.initialDelay,
202 configuration.refreshInterval);
203 refreshTask = scheduler.scheduleWithFixedDelay(this::startSpeedTest, configuration.initialDelay,
204 configuration.refreshInterval, TimeUnit.MINUTES);
205 timeouts = configuration.maxTimeout;
206 updateStatus(ThingStatus.ONLINE);