]> git.basschouten.com Git - openhab-addons.git/blob
1124d98a66f8568c3805a85acbb9287857ece328
[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.solarwatt.internal.domain;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.solarwatt.internal.domain.dto.EnergyManagerDTO;
20 import org.openhab.binding.solarwatt.internal.domain.model.Device;
21 import org.openhab.binding.solarwatt.internal.factory.EnergyManagerDevicesFactory;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Collection of all devices known to the energy manager including the energy manager itself.
27  *
28  * The {@link Device}s are generated from the {@link org.openhab.binding.solarwatt.internal.domain.dto.DeviceDTO}s
29  * inside of the {@link org.openhab.binding.solarwatt.internal.domain.dto.EnergyManagerDTO}
30  * 
31  * @author Sven Carstens - Initial contribution
32  */
33 @NonNullByDefault
34 public class EnergyManagerCollection {
35     private Logger logger = LoggerFactory.getLogger(EnergyManagerCollection.class);
36
37     private Map<String, Device> devices;
38
39     public EnergyManagerCollection(EnergyManagerDTO energyManagerDTO) {
40         this.devices = new HashMap<>();
41
42         energyManagerDTO.getItems().forEach(deviceDTO -> {
43             try {
44                 Device device = EnergyManagerDevicesFactory.getEnergyManagerDevice(deviceDTO);
45
46                 if (device != null) {
47                     this.devices.put(device.getGuid(), device);
48                 }
49             } catch (Exception ex) {
50                 this.logger.error("Error setting up initial device {}: {}", deviceDTO.getGuid(),
51                         deviceDTO.getDeviceModel(), ex);
52             }
53         });
54     }
55
56     public Map<String, Device> getDevices() {
57         return this.devices;
58     }
59 }