]> git.basschouten.com Git - openhab-addons.git/blob
af7edb077cceb9d94c9754150e406793d575867a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.heliosventilation.internal;
14
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.Enumeration;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Properties;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link HeliosVentilationBindingConstants} class defines common constants, which are
29  * used across the whole binding.
30  *
31  * @author Raphael Mack - Initial contribution
32  */
33 @NonNullByDefault
34 public class HeliosVentilationBindingConstants {
35
36     public static final String BINDING_ID = "heliosventilation";
37
38     public static final String DATAPOINT_FILE = "datapoints.properties";
39
40     // List of all Thing Type UIDs
41     public static final ThingTypeUID THING_TYPE_HELIOS_VENTILATION = new ThingTypeUID(BINDING_ID, "ventilation");
42
43     public static final Map<Byte, HeliosVentilationDataPoint> DATAPOINTS;
44
45     private static final Logger LOGGER;
46     static {
47         /* logger is used by readChannelProperties() so we need to initialize logger first. */
48         LOGGER = LoggerFactory.getLogger(HeliosVentilationBindingConstants.class);
49         DATAPOINTS = readChannelProperties();
50     }
51     // List of all Channel ids
52     // Channel ids are only in datapoints.properties and thing-types.xml
53
54     /**
55      * parse datapoints from properties
56      *
57      */
58     private static Map<Byte, HeliosVentilationDataPoint> readChannelProperties() {
59         HashMap<Byte, HeliosVentilationDataPoint> result = new HashMap<Byte, HeliosVentilationDataPoint>();
60
61         URL resource = Thread.currentThread().getContextClassLoader().getResource(DATAPOINT_FILE);
62         Properties properties = new Properties();
63         try {
64             properties.load(resource.openStream());
65
66             Enumeration<Object> keys = properties.keys();
67             while (keys.hasMoreElements()) {
68                 String channel = (String) keys.nextElement();
69                 HeliosVentilationDataPoint dp;
70                 try {
71                     dp = new HeliosVentilationDataPoint(channel, properties.getProperty(channel));
72                     if (result.containsKey(dp.address())) {
73                         result.get(dp.address()).append(dp);
74                     } else {
75                         result.put(dp.address(), dp);
76                     }
77                 } catch (HeliosPropertiesFormatException e) {
78                     LOGGER.warn("could not read resource file {}, binding will probably fail: {}", DATAPOINT_FILE,
79                             e.getMessage());
80                 }
81             }
82         } catch (IOException e) {
83             LOGGER.warn("could not read resource file {}, binding will probably fail: {}", DATAPOINT_FILE,
84                     e.getMessage());
85         }
86
87         return result;
88     }
89 }