]> git.basschouten.com Git - openhab-addons.git/blob
a2766be76f294ab26fcc33bb724950e4dad92e2c
[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.millheat.internal.model;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * The {@link MillheatModel} represents the home structure as designed by the user in the Millheat app.
25  *
26  * @author Arne Seime - Initial contribution
27  */
28 @NonNullByDefault
29 public class MillheatModel {
30     private final long lastUpdated;
31     private final List<Home> homes = new ArrayList<>();
32
33     public MillheatModel(final long lastUpdated) {
34         this.lastUpdated = lastUpdated;
35     }
36
37     public void addHome(final Home home) {
38         homes.add(home);
39     }
40
41     public List<Home> getHomes() {
42         return homes;
43     }
44
45     public long getLastUpdated() {
46         return lastUpdated;
47     }
48
49     public Optional<Heater> findHeaterById(final Long id) {
50         return findHeaters().filter(heater -> id.equals(heater.getId())).findFirst();
51     }
52
53     public Optional<Heater> findHeaterByMac(final String macAddress) {
54         return findHeaters().filter(heater -> macAddress.equals(heater.getMacAddress())).findFirst();
55     }
56
57     public Optional<Heater> findHeaterByMacOrId(@Nullable final String macAddress, @Nullable final Long id) {
58         Optional<Heater> heater = Optional.empty();
59
60         if (macAddress != null) {
61             heater = findHeaterByMac(macAddress);
62         }
63         if (!heater.isPresent() && id != null) {
64             heater = findHeaterById(id);
65         }
66         return heater;
67     }
68
69     private Stream<Heater> findHeaters() {
70         return Stream.concat(
71                 homes.stream().flatMap(home -> home.getRooms().stream()).flatMap(room -> room.getHeaters().stream()),
72                 homes.stream().flatMap(room -> room.getIndependentHeaters().stream()));
73     }
74
75     public Optional<Room> findRoomById(final Long id) {
76         return homes.stream().flatMap(home -> home.getRooms().stream()).filter(room -> id.equals(room.getId()))
77                 .findFirst();
78     }
79
80     public Optional<Home> findHomeByRoomId(final Long id) {
81         for (final Home home : homes) {
82             for (final Room room : home.getRooms()) {
83                 if (id.equals(room.getId())) {
84                     return Optional.of(home);
85                 }
86             }
87         }
88         return Optional.empty();
89     }
90
91     public Optional<Home> findHomeById(Long homeId) {
92         return homes.stream().filter(e -> e.getId().equals(homeId)).findFirst();
93     }
94 }