2 * Copyright (c) 2010-2022 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.mielecloud.internal.webservice;
15 import java.util.Optional;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.function.Supplier;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
21 import org.openhab.binding.mielecloud.internal.webservice.exception.AuthorizationFailedException;
22 import org.openhab.binding.mielecloud.internal.webservice.exception.MieleWebserviceException;
23 import org.openhab.binding.mielecloud.internal.webservice.exception.TooManyRequestsException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link ActionStateFetcher} fetches the updated actions state for a device from the {@link MieleWebservice} if
29 * the state of that device changed.
31 * Note that an instance of this class is required for each device.
33 * @author Roland Edelhoff - Initial contribution
34 * @author Björn Lange - Make calls to webservice asynchronous
37 public class ActionStateFetcher {
38 private Optional<DeviceState> lastDeviceState = Optional.empty();
39 private final Supplier<MieleWebservice> webserviceSupplier;
40 private final ScheduledExecutorService scheduler;
42 private final Logger logger = LoggerFactory.getLogger(ActionStateFetcher.class);
45 * Creates a new {@link ActionStateFetcher}.
47 * @param webserviceSupplier Getter function for access to the {@link MieleWebservice}.
48 * @param scheduler System-wide scheduler.
50 public ActionStateFetcher(Supplier<MieleWebservice> webserviceSupplier, ScheduledExecutorService scheduler) {
51 this.webserviceSupplier = webserviceSupplier;
52 this.scheduler = scheduler;
56 * Invoked when the state of a device was updated.
58 public void onDeviceStateUpdated(DeviceState deviceState) {
59 if (hasDeviceStatusChanged(deviceState)) {
60 scheduler.submit(() -> fetchActions(deviceState));
62 lastDeviceState = Optional.of(deviceState);
65 private boolean hasDeviceStatusChanged(DeviceState newDeviceState) {
66 return lastDeviceState.map(DeviceState::getStateType)
67 .map(rawStatus -> !newDeviceState.getStateType().equals(rawStatus)).orElse(true);
70 private void fetchActions(DeviceState deviceState) {
72 webserviceSupplier.get().fetchActions(deviceState.getDeviceIdentifier());
73 } catch (MieleWebserviceException e) {
74 logger.warn("Failed to fetch action state for device {}: {} - {}", deviceState.getDeviceIdentifier(),
75 e.getConnectionError(), e.getMessage());
76 } catch (AuthorizationFailedException | TooManyRequestsException e) {
77 logger.warn("Failed to fetch action state for device {}: {}", deviceState.getDeviceIdentifier(),