2 * Copyright (c) 2010-2023 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.ihc.internal.ws.services;
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;
24 * Class to handle IHC / ELKO LS Controller's controller service.
26 * Controller service is used to fetch information from the controller.
27 * E.g. Project file or controller status.
29 * @author Pauli Anttila - Initial contribution
31 public class IhcControllerService extends IhcBaseService {
33 public IhcControllerService(String host, int timeout, IhcConnectionPool ihcConnectionPool) {
34 super(ihcConnectionPool, timeout, host, "ControllerService");
38 * Query project information from the controller.
40 * @return project information.
41 * @throws IhcExecption
43 public synchronized WSProjectInfo getProjectInfo() throws IhcExecption {
44 String response = sendSoapQuery("getProjectInfo", EMPTY_QUERY);
45 return new WSProjectInfo().parseXMLData(response);
49 * Query number of segments project contains.
51 * @return number of segments.
52 * @throws IhcExecption
54 public synchronized int getProjectNumberOfSegments() throws IhcExecption {
55 String response = sendSoapQuery("getIHCProjectNumberOfSegments", EMPTY_QUERY);
56 return new WSNumberOfSegments().parseXMLData(response).getNumberOfSegments();
60 * Query segmentation size.
62 * @return segmentation size in bytes.
63 * @throws IhcExecption
65 public synchronized int getProjectSegmentationSize() throws IhcExecption {
66 String response = sendSoapQuery("getIHCProjectSegmentationSize", EMPTY_QUERY);
67 return new WSSegmentationSize().parseXMLData(response).getSegmentationSize();
71 * Query project segment data.
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
79 public synchronized WSFile getProjectSegment(int index, int major, int minor) throws IhcExecption {
81 final String soapQuery =
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/">
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>
94 String query = String.format(soapQuery, index, major, minor);
95 String response = sendSoapQuery("getIHCProjectSegment", query);
96 return new WSFile().parseXMLData(response);
100 * Query controller current state.
102 * @return controller's current state.
103 * @throws IhcExecption
105 public synchronized WSControllerState getControllerState() throws IhcExecption {
106 String response = sendSoapQuery("getState", EMPTY_QUERY);
107 return new WSControllerState().parseXMLData(response);
111 * Wait controller state change notification.
113 * @param previousState Previous controller state.
114 * @param timeoutInSeconds How many seconds to wait notifications.
115 * @return current controller state.
116 * @throws IhcExecption
118 public synchronized WSControllerState waitStateChangeNotifications(WSControllerState previousState,
119 int timeoutInSeconds) throws IhcExecption {
121 final String soapQuery =
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/">
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>
135 String query = String.format(soapQuery, previousState.getState(), timeoutInSeconds);
136 String response = sendSoapQuery("waitForControllerStateChange", query, getTimeout() + timeoutInSeconds * 1000);
137 return new WSControllerState().parseXMLData(response);