]> git.basschouten.com Git - openhab-addons.git/blob
083cb0e35d51569e169a1dbb8eba451e104b7913
[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                   """
83                 <?xml version="1.0" encoding="UTF-8"?>
84                 <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/">
85                  <soap:Body>
86                   <ns1:getIHCProjectSegment1 xmlns:ns1="utcs" xsi:type="xsd:int">%s</ns1:getIHCProjectSegment1>
87                   <ns2:getIHCProjectSegment2 xmlns:ns2="utcs" xsi:type="xsd:int">%s</ns2:getIHCProjectSegment2>
88                   <ns3:getIHCProjectSegment3 xmlns:ns3="utcs" xsi:type="xsd:int">%s</ns3:getIHCProjectSegment3>
89                  </soap:Body>
90                 </soap:Envelope>\
91                 """;
92         // @formatter:on
93
94         String query = String.format(soapQuery, index, major, minor);
95         String response = sendSoapQuery("getIHCProjectSegment", query);
96         return new WSFile().parseXMLData(response);
97     }
98
99     /**
100      * Query controller current state.
101      *
102      * @return controller's current state.
103      * @throws IhcExecption
104      */
105     public synchronized WSControllerState getControllerState() throws IhcExecption {
106         String response = sendSoapQuery("getState", EMPTY_QUERY);
107         return new WSControllerState().parseXMLData(response);
108     }
109
110     /**
111      * Wait controller state change notification.
112      *
113      * @param previousState Previous controller state.
114      * @param timeoutInSeconds How many seconds to wait notifications.
115      * @return current controller state.
116      * @throws IhcExecption
117      */
118     public synchronized WSControllerState waitStateChangeNotifications(WSControllerState previousState,
119             int timeoutInSeconds) throws IhcExecption {
120         // @formatter:off
121         final String soapQuery =
122                   """
123                 <?xml version="1.0" encoding="UTF-8"?>
124                 <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/">
125                  <soapenv:Body>
126                   <ns1:waitForControllerStateChange1 xmlns:ns1="utcs" xsi:type="ns1:WSControllerState">
127                    <ns1:state xsi:type="xsd:string">%s</ns1:state>
128                   </ns1:waitForControllerStateChange1>
129                   <ns2:waitForControllerStateChange2 xmlns:ns2="utcs" xsi:type="xsd:int">%s</ns2:waitForControllerStateChange2>
130                  </soapenv:Body>
131                 </soapenv:Envelope>\
132                 """;
133         // @formatter:on
134
135         String query = String.format(soapQuery, previousState.getState(), timeoutInSeconds);
136         String response = sendSoapQuery("waitForControllerStateChange", query, getTimeout() + timeoutInSeconds * 1000);
137         return new WSControllerState().parseXMLData(response);
138     }
139 }