]> git.basschouten.com Git - openhab-addons.git/blob
8cdaf1ea4d4d6123f7c8d7472d5e132be8ef6e5d
[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.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.ojelectronics.internal.ThermostatHandler;
19 import org.openhab.binding.ojelectronics.internal.models.Thermostat;
20 import org.openhab.binding.ojelectronics.internal.models.groups.GroupContent;
21 import org.openhab.core.thing.Thing;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Refreshes values of {@link ThermostatHandler}
27  *
28  * @author Christian Kittel - Initial Contribution
29  */
30 @NonNullByDefault
31 public class RefreshGroupContentService {
32
33     private final List<GroupContent> groupContentList;
34     private final Logger logger = LoggerFactory.getLogger(RefreshGroupContentService.class);
35     private List<Thing> things;
36
37     /**
38      * Creates a new instance of {@link RefreshGroupContentService}
39      *
40      * @param groupContents {@link GroupContent}
41      * @param things Things
42      */
43     public RefreshGroupContentService(List<GroupContent> groupContents, List<Thing> things) {
44         this.groupContentList = groupContents;
45         this.things = things;
46         if (this.things.isEmpty()) {
47             logger.warn("Bridge contains no thermostats.");
48         }
49     }
50
51     /**
52      * Handles the changes to all things.
53      */
54     public void handle() {
55         groupContentList.stream().flatMap(entry -> entry.thermostats.stream()).forEach(this::handleThermostat);
56     }
57
58     private void handleThermostat(Thermostat thermostat) {
59         things.stream().filter(thing -> thing.getHandler() instanceof ThermostatHandler)
60                 .map(thing -> (ThermostatHandler) thing.getHandler())
61                 .filter(thingHandler -> thingHandler.getSerialNumber().equals(thermostat.serialNumber))
62                 .forEach(thingHandler -> thingHandler.handleThermostatRefresh(thermostat));
63     }
64 }