]> git.basschouten.com Git - openhab-addons.git/blob
af2581fd244d5bf3f6b4e0ea2e7b6f5e114a82f9
[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.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 DeviceDTO}s inside of the {@link EnergyManagerDTO}
29  * 
30  * @author Sven Carstens - Initial contribution
31  */
32 @NonNullByDefault
33 public class EnergyManagerCollection {
34     private Logger logger = LoggerFactory.getLogger(EnergyManagerCollection.class);
35
36     private Map<String, Device> devices;
37
38     public EnergyManagerCollection(EnergyManagerDTO energyManagerDTO) {
39         this.devices = new HashMap<>();
40
41         energyManagerDTO.getItems().forEach(deviceDTO -> {
42             try {
43                 Device device = EnergyManagerDevicesFactory.getEnergyManagerDevice(deviceDTO);
44
45                 if (device != null) {
46                     this.devices.put(device.getGuid(), device);
47                 }
48             } catch (Exception ex) {
49                 this.logger.error("Error setting up initial device {}: {}", deviceDTO.getGuid(),
50                         deviceDTO.getDeviceModel(), ex);
51             }
52         });
53     }
54
55     public Map<String, Device> getDevices() {
56         return this.devices;
57     }
58 }