2 * Copyright (c) 2010-2021 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.gce.internal.model;
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;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
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;
40 * This class takes care of interpreting the status.xml file
42 * @author Gaƫl L'hopital - Initial contribution
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;
52 public static enum StatusEntry {
57 public StatusFileInterpreter(String hostname, Ipx800EventListener listener) {
58 this.hostname = hostname;
59 this.listener = listener;
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();
72 } catch (IOException | SAXException | ParserConfigurationException e) {
73 logger.warn("Unable to read IPX800 status page : {}", e.getMessage());
78 private void pushDatas() {
79 Element root = getRoot();
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);
93 public String getElement(StatusEntry entry) {
94 Element root = getRoot();
96 return root.getElementsByTagName(entry.name().toLowerCase()).item(0).getTextContent();
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());
108 public int getMaxNumberofNodeType(PortDefinition portDefinition) {
109 Element root = getRoot();
111 List<Node> filteredNodes = getMatchingNodes(root.getChildNodes(), portDefinition.getNodeName());
112 return filteredNodes.size();
117 private @Nullable Element getRoot() {
122 return doc.getDocumentElement();