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.ojelectronics.internal.services;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.function.BiConsumer;
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;
30 * Refreshes values of {@link ThermostatHandler}
32 * @author Christian Kittel - Initial Contribution
35 public class RefreshThermostatsService {
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;
43 * Creates a new instance of {@link RefreshThermostatsService}
45 * @param thermostats {@link ThermostatModel}
46 * @param things Things
48 public RefreshThermostatsService(List<ThermostatModel> thermostats, List<Thing> things) {
49 this(thermostats, new ArrayList<>(), things);
53 * Creates a new instance of {@link RefreshThermostatsService}
55 * @param thermostats {@link ThermostatModel}
56 * @param realTimeValues {@link ThermostatRealTimeValuesModel}
57 * @param things Things
59 public RefreshThermostatsService(List<ThermostatModel> thermostats,
60 List<ThermostatRealTimeValuesModel> realTimeValues, List<Thing> things) {
61 this.thermostats = thermostats;
63 this.realTimeValues = realTimeValues;
64 if (this.things.isEmpty()) {
65 logger.warn("Bridge contains no thermostats.");
70 * Handles the changes to all things.
72 public synchronized void handle() {
73 thermostats.forEach(thermostat -> handleThermostat(thermostat, this::handleThermostatRefresh));
74 realTimeValues.forEach(thermostat -> handleThermostat(thermostat, this::handleThermostatRealTimeValueRefresh));
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 -> {
84 refreshHandler.accept(Objects.requireNonNull(thingHandler), thermostat);
85 } catch (Exception e) {
86 logger.info("Error Handling Refresh of thermostat {}", thermostat, e);
91 private void handleThermostatRefresh(ThermostatHandler thingHandler, ThermostatModel thermostat) {
92 thingHandler.handleThermostatRefresh(thermostat);
95 private void handleThermostatRealTimeValueRefresh(ThermostatHandler thingHandler,
96 ThermostatRealTimeValuesModel thermostat) {
97 thingHandler.handleThermostatRefresh(thermostat);