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.millheat.internal.model;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
24 * The {@link MillheatModel} represents the home structure as designed by the user in the Millheat app.
26 * @author Arne Seime - Initial contribution
29 public class MillheatModel {
30 private final long lastUpdated;
31 private final List<Home> homes = new ArrayList<>();
33 public MillheatModel(final long lastUpdated) {
34 this.lastUpdated = lastUpdated;
37 public void addHome(final Home home) {
41 public List<Home> getHomes() {
45 public long getLastUpdated() {
49 public Optional<Heater> findHeaterById(final Long id) {
50 return findHeaters().filter(heater -> id.equals(heater.getId())).findFirst();
53 public Optional<Heater> findHeaterByMac(final String macAddress) {
54 return findHeaters().filter(heater -> macAddress.equals(heater.getMacAddress())).findFirst();
57 public Optional<Heater> findHeaterByMacOrId(@Nullable final String macAddress, @Nullable final Long id) {
58 Optional<Heater> heater = Optional.empty();
60 if (macAddress != null) {
61 heater = findHeaterByMac(macAddress);
63 if (!heater.isPresent() && id != null) {
64 heater = findHeaterById(id);
69 private Stream<Heater> findHeaters() {
71 homes.stream().flatMap(home -> home.getRooms().stream()).flatMap(room -> room.getHeaters().stream()),
72 homes.stream().flatMap(room -> room.getIndependentHeaters().stream()));
75 public Optional<Room> findRoomById(final Long id) {
76 return homes.stream().flatMap(home -> home.getRooms().stream()).filter(room -> id.equals(room.getId()))
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);
88 return Optional.empty();
91 public Optional<Home> findHomeById(Long homeId) {
92 return homes.stream().filter(e -> e.getId().equals(homeId)).findFirst();