]> git.basschouten.com Git - openhab-addons.git/blob
777d32445bc9c929fa4479e6d2f2d7c3292de869
[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.ecobee.internal.dto.thermostat.summary;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.ecobee.internal.dto.AbstractResponseDTO;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.gson.annotations.SerializedName;
28
29 /**
30  * The {@link SummaryResponseDTO} contains the
31  *
32  * @author Mark Hilbush - Initial contribution
33  */
34 @NonNullByDefault
35 public class SummaryResponseDTO extends AbstractResponseDTO {
36
37     private final transient Logger logger = LoggerFactory.getLogger(SummaryResponseDTO.class);
38
39     /*
40      * Number of thermostats listed in the Revision List.
41      */
42     public @Nullable Integer thermostatCount;
43
44     /*
45      * The list of CSV revision values.
46      */
47     public @Nullable List<RevisionDTO> revisionList;
48
49     /*
50      * The list of CSV status values.
51      */
52     @SerializedName("statusList")
53     public @Nullable List<RunningDTO> runningList;
54
55     public boolean hasChanged(@Nullable SummaryResponseDTO previousSummary) {
56         if (previousSummary == null) {
57             logger.debug("SummaryResponse: No previous summary");
58             return true;
59         }
60
61         boolean changed = false;
62         if (revisionHasChanged(previousSummary.revisionList)) {
63             changed = true;
64         }
65         if (runningHasChanged(previousSummary.runningList)) {
66             changed = true;
67         }
68         return changed;
69     }
70
71     private boolean revisionHasChanged(@Nullable List<RevisionDTO> previousList) {
72         List<RevisionDTO> currentList = revisionList;
73         if (previousList == null || currentList == null) {
74             logger.debug("SummaryResponse: Previous and/or current revision list is null");
75             return true;
76         }
77         // Check to see if there are different thermostat Ids in current vs previous
78         Set<String> previousIds = previousList.stream().map(RevisionDTO::getId).collect(Collectors.toSet());
79         Set<String> currentIds = currentList.stream().map(RevisionDTO::getId).collect(Collectors.toSet());
80         if (!previousIds.equals(currentIds)) {
81             logger.debug("SummaryResponse: Thermostat id maps are different");
82             logger.trace("               : Curr: {}", Arrays.toString(currentIds.toArray()));
83             logger.trace("               : Prev: {}", Arrays.toString(previousIds.toArray()));
84             return true;
85         }
86         // Create a map of each thermostat id with its RevisionDTO object
87         Map<String, RevisionDTO> previousMap = previousList.stream()
88                 .collect(Collectors.toMap(RevisionDTO::getId, RevisionDTO::getThis));
89         // Go through list of current RevisionDTOs to see if something has changed
90         for (RevisionDTO current : currentList) {
91             RevisionDTO previous = previousMap.get(current.getId());
92             if (current.hasChanged(previous)) {
93                 logger.debug("SummaryResponse: Revisions has changed");
94                 logger.trace("               : Curr: {}", current.toString());
95                 logger.trace("               : Prev: {}", previous.toString());
96                 return true;
97             }
98         }
99         return false;
100     }
101
102     private boolean runningHasChanged(@Nullable List<RunningDTO> previousList) {
103         List<RunningDTO> currentList = runningList;
104         if (previousList == null || currentList == null) {
105             logger.debug("SummaryResponse: Previous and/or current running list is null");
106             return true;
107         }
108         // Check to see if there are different thermostat Ids in current vs previous
109         Set<String> previousIds = previousList.stream().map(RunningDTO::getId).collect(Collectors.toSet());
110         Set<String> currentIds = currentList.stream().map(RunningDTO::getId).collect(Collectors.toSet());
111         if (!previousIds.equals(currentIds)) {
112             logger.debug("SummaryResponse: Thermostat id maps are different");
113             logger.trace("               : Curr: {}", Arrays.toString(currentIds.toArray()));
114             logger.trace("               : Prev: {}", Arrays.toString(previousIds.toArray()));
115             return true;
116         }
117         // Create a map of each thermostat id with its RunningDTO object
118         Map<String, RunningDTO> previousMap = previousList.stream()
119                 .collect(Collectors.toMap(RunningDTO::getId, RunningDTO::getThis));
120         // Go through list of current RunningDTOs to see if something has changed
121         for (RunningDTO current : currentList) {
122             RunningDTO previous = previousMap.get(current.getId());
123             if (current.hasChanged(previous)) {
124                 logger.debug("SummaryResponse: Running Equipment has changed");
125                 logger.trace("               : Curr: {}", current.toString());
126                 logger.trace("               : Prev: {}", previous.toString());
127                 return true;
128             }
129         }
130         return false;
131     }
132 }