]> git.basschouten.com Git - openhab-addons.git/blob
3d69a0b0b6144a5d54c6ba0369e09969ddb9ec3f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.draytonwiser.internal.model;
14
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Helper class to get specific data from the domain object.
23  *
24  * @author Andrew Schofield - Initial contribution
25  * @author Hilbrand Bouwkamp - Moved domain object helper code to it's own class
26  */
27 @NonNullByDefault
28 public class DraytonWiserDTO {
29
30     private final DomainDTO domain;
31
32     public DraytonWiserDTO(final DomainDTO domain) {
33         this.domain = domain;
34     }
35
36     public List<RoomStatDTO> getRoomStats() {
37         return domain.getRoomStat() == null ? Collections.emptyList() : domain.getRoomStat();
38     }
39
40     public List<SmartValveDTO> getSmartValves() {
41         return domain.getSmartValve() == null ? Collections.emptyList() : domain.getSmartValve();
42     }
43
44     public List<SmartPlugDTO> getSmartPlugs() {
45         return domain.getSmartPlug() == null ? Collections.emptyList() : domain.getSmartPlug();
46     }
47
48     public List<RoomDTO> getRooms() {
49         return domain.getRoom() == null ? Collections.emptyList() : domain.getRoom();
50     }
51
52     public @Nullable RoomDTO getRoomByName(final String name) {
53         for (final RoomDTO room : domain.getRoom()) {
54             if (room.getName().equalsIgnoreCase(name)) {
55                 return room;
56             }
57         }
58         return null;
59     }
60
61     public @Nullable RoomStatDTO getRoomStat(final String serialNumber) {
62         final Integer id = getIdFromSerialNumber(serialNumber);
63
64         return id == null ? null : getRoomStat(id);
65     }
66
67     public @Nullable RoomStatDTO getRoomStat(final int id) {
68         for (final RoomStatDTO roomStat : domain.getRoomStat()) {
69             if (roomStat.getId().equals(id)) {
70                 return roomStat;
71             }
72         }
73         return null;
74     }
75
76     public @Nullable SmartPlugDTO getSmartPlug(final String serialNumber) {
77         final Integer id = getIdFromSerialNumber(serialNumber);
78
79         if (id == null) {
80             return null;
81         }
82         for (final SmartPlugDTO smartPlug : domain.getSmartPlug()) {
83             if (smartPlug.getId().equals(id)) {
84                 return smartPlug;
85             }
86         }
87         return null;
88     }
89
90     public @Nullable DeviceDTO getExtendedDeviceProperties(final int id) {
91         for (final DeviceDTO device : domain.getDevice()) {
92             if (device.getId().equals(id)) {
93                 return device;
94             }
95         }
96
97         return null;
98     }
99
100     public @Nullable SystemDTO getSystem() {
101         return domain.getSystem();
102     }
103
104     public List<HeatingChannelDTO> getHeatingChannels() {
105         return domain.getHeatingChannel() == null ? Collections.emptyList() : domain.getHeatingChannel();
106     }
107
108     public List<HotWaterDTO> getHotWater() {
109         return domain.getHotWater() == null ? Collections.emptyList() : domain.getHotWater();
110     }
111
112     @Nullable
113     public RoomDTO getRoomForDeviceId(final Integer id) {
114         for (final RoomDTO room : domain.getRoom()) {
115             if (room != null) {
116                 if (room.getRoomStatId() != null && room.getRoomStatId().equals(id)) {
117                     return room;
118                 }
119
120                 final List<Integer> trvs = room.getSmartValveIds();
121                 if (trvs != null) {
122                     for (final Integer itrv : trvs) {
123                         if (itrv.equals(id)) {
124                             return room;
125                         }
126                     }
127                 }
128             }
129         }
130         return null;
131     }
132
133     public @Nullable SmartValveDTO getSmartValve(final String serialNumber) {
134         final Integer id = getIdFromSerialNumber(serialNumber);
135
136         if (id == null) {
137             return null;
138         }
139
140         for (final SmartValveDTO smartValve : domain.getSmartValve()) {
141             if (smartValve.getId().equals(id)) {
142                 return smartValve;
143             }
144         }
145         return null;
146     }
147
148     private @Nullable Integer getIdFromSerialNumber(final String serialNumber) {
149         for (final DeviceDTO device : domain.getDevice()) {
150             if (device.getSerialNumber() != null
151                     && device.getSerialNumber().toLowerCase().equals(serialNumber.toLowerCase())) {
152                 return device.getId();
153             }
154         }
155         return null;
156     }
157 }