]> git.basschouten.com Git - openhab-addons.git/blob
47c851a8bc7e512bee504ff1bd7c983e530553df
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.webservice;
14
15 import java.util.Optional;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.function.Supplier;
18
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;
26
27 /**
28  * The {@link ActionStateFetcher} fetches the updated actions state for a device from the {@link MieleWebservice} if
29  * the state of that device changed.
30  *
31  * Note that an instance of this class is required for each device.
32  *
33  * @author Roland Edelhoff - Initial contribution
34  * @author Björn Lange - Make calls to webservice asynchronous
35  */
36 @NonNullByDefault
37 public class ActionStateFetcher {
38     private Optional<DeviceState> lastDeviceState = Optional.empty();
39     private final Supplier<MieleWebservice> webserviceSupplier;
40     private final ScheduledExecutorService scheduler;
41
42     private final Logger logger = LoggerFactory.getLogger(ActionStateFetcher.class);
43
44     /**
45      * Creates a new {@link ActionStateFetcher}.
46      *
47      * @param webserviceSupplier Getter function for access to the {@link MieleWebservice}.
48      * @param scheduler System-wide scheduler.
49      */
50     public ActionStateFetcher(Supplier<MieleWebservice> webserviceSupplier, ScheduledExecutorService scheduler) {
51         this.webserviceSupplier = webserviceSupplier;
52         this.scheduler = scheduler;
53     }
54
55     /**
56      * Invoked when the state of a device was updated.
57      */
58     public void onDeviceStateUpdated(DeviceState deviceState) {
59         if (hasDeviceStatusChanged(deviceState)) {
60             scheduler.submit(() -> fetchActions(deviceState));
61         }
62         lastDeviceState = Optional.of(deviceState);
63     }
64
65     private boolean hasDeviceStatusChanged(DeviceState newDeviceState) {
66         return lastDeviceState.map(DeviceState::getStateType)
67                 .map(rawStatus -> !newDeviceState.getStateType().equals(rawStatus)).orElse(true);
68     }
69
70     private void fetchActions(DeviceState deviceState) {
71         try {
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(),
78                     e.getMessage());
79         }
80     }
81 }