]> git.basschouten.com Git - openhab-addons.git/blob
8b3dfa1f0bb6ea7dc703ddb1dd5b904788286d7e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.ojelectronics.internal.services;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.function.BiConsumer;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.ojelectronics.internal.ThermostatHandler;
22 import org.openhab.binding.ojelectronics.internal.models.thermostat.ThermostatModel;
23 import org.openhab.binding.ojelectronics.internal.models.thermostat.ThermostatModelBase;
24 import org.openhab.binding.ojelectronics.internal.models.thermostat.ThermostatRealTimeValuesModel;
25 import org.openhab.core.thing.Thing;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Refreshes values of {@link ThermostatHandler}
31  *
32  * @author Christian Kittel - Initial Contribution
33  */
34 @NonNullByDefault
35 public class RefreshThermostatsService {
36
37     private final List<ThermostatModel> thermostats;
38     private final Logger logger = LoggerFactory.getLogger(RefreshThermostatsService.class);
39     private final List<Thing> things;
40     private final List<ThermostatRealTimeValuesModel> realTimeValues;
41
42     /**
43      * Creates a new instance of {@link RefreshThermostatsService}
44      *
45      * @param thermostats {@link ThermostatModel}
46      * @param things Things
47      */
48     public RefreshThermostatsService(List<ThermostatModel> thermostats, List<Thing> things) {
49         this(thermostats, new ArrayList<>(), things);
50     }
51
52     /**
53      * Creates a new instance of {@link RefreshThermostatsService}
54      *
55      * @param thermostats {@link ThermostatModel}
56      * @param realTimeValues {@link ThermostatRealTimeValuesModel}
57      * @param things Things
58      */
59     public RefreshThermostatsService(List<ThermostatModel> thermostats,
60             List<ThermostatRealTimeValuesModel> realTimeValues, List<Thing> things) {
61         this.thermostats = thermostats;
62         this.things = things;
63         this.realTimeValues = realTimeValues;
64         if (this.things.isEmpty()) {
65             logger.warn("Bridge contains no thermostats.");
66         }
67     }
68
69     /**
70      * Handles the changes to all things.
71      */
72     public synchronized void handle() {
73         thermostats.forEach(thermostat -> handleThermostat(thermostat, this::handleThermostatRefresh));
74         realTimeValues.forEach(thermostat -> handleThermostat(thermostat, this::handleThermostatRealTimeValueRefresh));
75     }
76
77     private <T extends ThermostatModelBase> void handleThermostat(T thermostat,
78             BiConsumer<ThermostatHandler, T> refreshHandler) {
79         things.stream().filter(thing -> thing.getHandler() instanceof ThermostatHandler)
80                 .map(thing -> (ThermostatHandler) thing.getHandler())
81                 .filter(thingHandler -> thingHandler.getSerialNumber().equals(thermostat.serialNumber))
82                 .forEach(thingHandler -> {
83                     try {
84                         refreshHandler.accept(Objects.requireNonNull(thingHandler), thermostat);
85                     } catch (Exception e) {
86                         logger.info("Error Handling Refresh of thermostat {}", thermostat, e);
87                     }
88                 });
89     }
90
91     private void handleThermostatRefresh(ThermostatHandler thingHandler, ThermostatModel thermostat) {
92         thingHandler.handleThermostatRefresh(thermostat);
93     }
94
95     private void handleThermostatRealTimeValueRefresh(ThermostatHandler thingHandler,
96             ThermostatRealTimeValuesModel thermostat) {
97         thingHandler.handleThermostatRefresh(thermostat);
98     }
99 }