]> git.basschouten.com Git - openhab-addons.git/blob
38e58c470b4ca4d103f9a374c86a45105d6d8d82
[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.ihc.internal.ws.services;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import javax.xml.xpath.XPathExpressionException;
20
21 import org.openhab.binding.ihc.internal.ws.datatypes.WSRFDevice;
22 import org.openhab.binding.ihc.internal.ws.datatypes.XPathUtils;
23 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
24 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27
28 /**
29  * Class to handle IHC / ELKO LS Controller's airlink management service.
30  *
31  *
32  * @author Pauli Anttila - Initial contribution
33  */
34 public class IhcAirlinkManagementService extends IhcBaseService {
35
36     public IhcAirlinkManagementService(String host, int timeout, IhcConnectionPool ihcConnectionPool) {
37         super(ihcConnectionPool, timeout, host, "AirlinkManagementService");
38     }
39
40     /**
41      * Query system information from the controller.
42      *
43      * @return system information.
44      * @throws IhcExecption
45      */
46     public synchronized List<WSRFDevice> getDetectedDeviceList() throws IhcExecption {
47         String response = sendSoapQuery("getDetectedDeviceList", EMPTY_QUERY);
48
49         List<WSRFDevice> resourceValueList = new ArrayList<>();
50
51         try {
52             NodeList nodeList = XPathUtils.parseList(response,
53                     "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getDetectedDeviceList1/ns1:arrayItem");
54
55             if (nodeList != null) {
56                 for (int i = 0; i < nodeList.getLength(); i++) {
57                     Node node = nodeList.item(i);
58                     if (node != null) {
59                         WSRFDevice dev = parseResourceValue(node);
60                         if (dev != null) {
61                             resourceValueList.add(dev);
62                         }
63                     }
64                 }
65             } else {
66                 throw new IhcExecption("Illegal resource value notification response received");
67             }
68             return resourceValueList;
69         } catch (IOException | XPathExpressionException | NumberFormatException e) {
70             throw new IhcExecption("Error occured during XML data parsing", e);
71         }
72     }
73
74     private WSRFDevice parseResourceValue(Node n) throws XPathExpressionException, NumberFormatException {
75         try {
76             int batteryLevel = Integer.parseInt(XPathUtils.getValueFromNode(n, "batteryLevel"));
77             int deviceType = Integer.parseInt(XPathUtils.getValueFromNode(n, "deviceType"));
78             long serialNumber = Long.parseLong(XPathUtils.getValueFromNode(n, "serialNumber"));
79             int signalStrength = Integer.parseInt(XPathUtils.getValueFromNode(n, "signalStrength"));
80             int version = Integer.parseInt(XPathUtils.getValueFromNode(n, "version"));
81             boolean detected = Boolean.valueOf(XPathUtils.getValueFromNode(n, "detected"));
82             return new WSRFDevice(batteryLevel, deviceType, serialNumber, signalStrength, version, detected);
83         } catch (NumberFormatException e) {
84             return null;
85         }
86     }
87 }