]> git.basschouten.com Git - openhab-addons.git/blob
041b2157f1d2b5aadeca1eca361e837a7b9539c9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.gce.internal.model;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.Comparator;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import java.util.stream.IntStream;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.gce.internal.handler.Ipx800EventListener;
30 import org.openhab.core.io.net.http.HttpUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
37 import org.xml.sax.SAXException;
38
39 /**
40  * This class takes care of interpreting the status.xml file
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 public class StatusFileInterpreter {
46     private static final String URL_TEMPLATE = "http://%s/globalstatus.xml";
47     private final Logger logger = LoggerFactory.getLogger(StatusFileInterpreter.class);
48     private final String hostname;
49     private @Nullable Document doc;
50     private final Ipx800EventListener listener;
51
52     public static enum StatusEntry {
53         VERSION,
54         CONFIG_MAC;
55     }
56
57     public StatusFileInterpreter(String hostname, Ipx800EventListener listener) {
58         this.hostname = hostname;
59         this.listener = listener;
60     }
61
62     public void read() {
63         try {
64             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
65             String statusPage = HttpUtil.executeUrl("GET", String.format(URL_TEMPLATE, hostname), 5000);
66             InputStream inputStream = new ByteArrayInputStream(statusPage.getBytes());
67             Document document = builder.parse(inputStream);
68             document.getDocumentElement().normalize();
69             doc = document;
70             pushDatas();
71             inputStream.close();
72         } catch (IOException | SAXException | ParserConfigurationException e) {
73             logger.warn("Unable to read IPX800 status page : {}", e.getMessage());
74             doc = null;
75         }
76     }
77
78     private void pushDatas() {
79         Element root = getRoot();
80         if (root != null) {
81             PortDefinition.asStream().forEach(portDefinition -> {
82                 List<Node> xmlNodes = getMatchingNodes(root.getChildNodes(), portDefinition.getNodeName());
83                 xmlNodes.forEach(xmlNode -> {
84                     String sPortNum = xmlNode.getNodeName().replace(portDefinition.getNodeName(), "");
85                     int portNum = Integer.parseInt(sPortNum) + 1;
86                     double value = Double.parseDouble(xmlNode.getTextContent().replace("dn", "1").replace("up", "0"));
87                     listener.dataReceived(String.format("%s%d", portDefinition.getPortName(), portNum), value);
88                 });
89             });
90         }
91     }
92
93     public String getElement(StatusEntry entry) {
94         Element root = getRoot();
95         if (root != null) {
96             return root.getElementsByTagName(entry.name().toLowerCase()).item(0).getTextContent();
97         } else {
98             return "";
99         }
100     }
101
102     private List<Node> getMatchingNodes(NodeList nodeList, String criteria) {
103         return IntStream.range(0, nodeList.getLength()).boxed().map(nodeList::item)
104                 .filter(node -> node.getNodeName().startsWith(criteria))
105                 .sorted(Comparator.comparing(o -> o.getNodeName())).collect(Collectors.toList());
106     }
107
108     public int getMaxNumberofNodeType(PortDefinition portDefinition) {
109         Element root = getRoot();
110         if (root != null) {
111             List<Node> filteredNodes = getMatchingNodes(root.getChildNodes(), portDefinition.getNodeName());
112             return filteredNodes.size();
113         }
114         return 0;
115     }
116
117     private @Nullable Element getRoot() {
118         if (doc == null) {
119             read();
120         }
121         if (doc != null) {
122             return doc.getDocumentElement();
123         }
124         return null;
125     }
126 }