]> git.basschouten.com Git - openhab-addons.git/blob
6146b2c229f7d2b87ae017cac05c61f9f037a677
[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 org.openhab.binding.ihc.internal.ws.datatypes.WSControllerState;
16 import org.openhab.binding.ihc.internal.ws.datatypes.WSFile;
17 import org.openhab.binding.ihc.internal.ws.datatypes.WSNumberOfSegments;
18 import org.openhab.binding.ihc.internal.ws.datatypes.WSProjectInfo;
19 import org.openhab.binding.ihc.internal.ws.datatypes.WSSegmentationSize;
20 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
21 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
22
23 /**
24  * Class to handle IHC / ELKO LS Controller's controller service.
25  *
26  * Controller service is used to fetch information from the controller.
27  * E.g. Project file or controller status.
28  *
29  * @author Pauli Anttila - Initial contribution
30  */
31 public class IhcControllerService extends IhcBaseService {
32
33     public IhcControllerService(String host, int timeout, IhcConnectionPool ihcConnectionPool) {
34         super(ihcConnectionPool, timeout, host, "ControllerService");
35     }
36
37     /**
38      * Query project information from the controller.
39      *
40      * @return project information.
41      * @throws IhcExecption
42      */
43     public synchronized WSProjectInfo getProjectInfo() throws IhcExecption {
44         String response = sendSoapQuery("getProjectInfo", EMPTY_QUERY);
45         return new WSProjectInfo().parseXMLData(response);
46     }
47
48     /**
49      * Query number of segments project contains.
50      *
51      * @return number of segments.
52      * @throws IhcExecption
53      */
54     public synchronized int getProjectNumberOfSegments() throws IhcExecption {
55         String response = sendSoapQuery("getIHCProjectNumberOfSegments", EMPTY_QUERY);
56         return new WSNumberOfSegments().parseXMLData(response).getNumberOfSegments();
57     }
58
59     /**
60      * Query segmentation size.
61      *
62      * @return segmentation size in bytes.
63      * @throws IhcExecption
64      */
65     public synchronized int getProjectSegmentationSize() throws IhcExecption {
66         String response = sendSoapQuery("getIHCProjectSegmentationSize", EMPTY_QUERY);
67         return new WSSegmentationSize().parseXMLData(response).getSegmentationSize();
68     }
69
70     /**
71      * Query project segment data.
72      *
73      * @param index segments index.
74      * @param major project major revision number.
75      * @param minor project minor revision number.
76      * @return segments data.
77      * @throws IhcExecption
78      */
79     public synchronized WSFile getProjectSegment(int index, int major, int minor) throws IhcExecption {
80         // @formatter:off
81         final String soapQuery =
82                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
83                 + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
84                 + " <soap:Body>\n"
85                 + "  <ns1:getIHCProjectSegment1 xmlns:ns1=\"utcs\" xsi:type=\"xsd:int\">%s</ns1:getIHCProjectSegment1>\n"
86                 + "  <ns2:getIHCProjectSegment2 xmlns:ns2=\"utcs\" xsi:type=\"xsd:int\">%s</ns2:getIHCProjectSegment2>\n"
87                 + "  <ns3:getIHCProjectSegment3 xmlns:ns3=\"utcs\" xsi:type=\"xsd:int\">%s</ns3:getIHCProjectSegment3>\n"
88                 + " </soap:Body>\n"
89                 + "</soap:Envelope>";
90         // @formatter:on
91
92         String query = String.format(soapQuery, index, major, minor);
93         String response = sendSoapQuery("getIHCProjectSegment", query);
94         return new WSFile().parseXMLData(response);
95     }
96
97     /**
98      * Query controller current state.
99      *
100      * @return controller's current state.
101      * @throws IhcExecption
102      */
103     public synchronized WSControllerState getControllerState() throws IhcExecption {
104         String response = sendSoapQuery("getState", EMPTY_QUERY);
105         return new WSControllerState().parseXMLData(response);
106     }
107
108     /**
109      * Wait controller state change notification.
110      *
111      * @param previousState Previous controller state.
112      * @param timeoutInSeconds How many seconds to wait notifications.
113      * @return current controller state.
114      * @throws IhcExecption
115      */
116     public synchronized WSControllerState waitStateChangeNotifications(WSControllerState previousState,
117             int timeoutInSeconds) throws IhcExecption {
118         // @formatter:off
119         final String soapQuery =
120                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
121                 + "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
122                 + " <soapenv:Body>\n"
123                 + "  <ns1:waitForControllerStateChange1 xmlns:ns1=\"utcs\" xsi:type=\"ns1:WSControllerState\">\n"
124                 + "   <ns1:state xsi:type=\"xsd:string\">%s</ns1:state>\n"
125                 + "  </ns1:waitForControllerStateChange1>\n"
126                 + "  <ns2:waitForControllerStateChange2 xmlns:ns2=\"utcs\" xsi:type=\"xsd:int\">%s</ns2:waitForControllerStateChange2>\n"
127                 + " </soapenv:Body>\n"
128                 + "</soapenv:Envelope>";
129         // @formatter:on
130
131         String query = String.format(soapQuery, previousState.getState(), timeoutInSeconds);
132         String response = sendSoapQuery("waitForControllerStateChange", query, getTimeout() + timeoutInSeconds * 1000);
133         return new WSControllerState().parseXMLData(response);
134     }
135 }