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.autelis.internal.handler;
15 import java.io.StringReader;
16 import java.util.Collections;
17 import java.util.HashMap;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import javax.xml.xpath.XPath;
25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory;
28 import org.apache.commons.lang.StringUtils;
29 import org.eclipse.jetty.client.HttpClient;
30 import org.eclipse.jetty.client.api.ContentResponse;
31 import org.eclipse.jetty.client.api.Request;
32 import org.eclipse.jetty.http.HttpHeader;
33 import org.eclipse.jetty.http.HttpStatus;
34 import org.eclipse.jetty.util.B64Code;
35 import org.eclipse.jetty.util.StringUtil;
36 import org.openhab.binding.autelis.internal.AutelisBindingConstants;
37 import org.openhab.binding.autelis.internal.config.AutelisConfiguration;
38 import org.openhab.core.library.types.DecimalType;
39 import org.openhab.core.library.types.IncreaseDecreaseType;
40 import org.openhab.core.library.types.OnOffType;
41 import org.openhab.core.library.types.StringType;
42 import org.openhab.core.thing.Channel;
43 import org.openhab.core.thing.ChannelUID;
44 import org.openhab.core.thing.Thing;
45 import org.openhab.core.thing.ThingStatus;
46 import org.openhab.core.thing.ThingStatusDetail;
47 import org.openhab.core.thing.binding.BaseThingHandler;
48 import org.openhab.core.types.Command;
49 import org.openhab.core.types.State;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.xml.sax.InputSource;
56 * Autelis Pool Control Binding
58 * Autelis controllers allow remote access to many common pool systems. This
59 * binding allows openHAB to both monitor and control a pool system through
62 * @see <a href="http://Autelis.com">http://autelis.com</a>
63 * @see <a href="http://www.autelis.com/wiki/index.php?title=Pool_Control_HTTP_Command_Reference"</a> for Jandy API
64 * @see <a href="http://www.autelis.com/wiki/index.php?title=Pool_Control_(PI)_HTTP_Command_Reference"</a> for Pentair
67 * The {@link AutelisHandler} is responsible for handling commands, which
68 * are sent to one of the channels.
70 * @author Dan Cunningham - Initial contribution
71 * @author Svilen Valkanov - Replaced Apache HttpClient with Jetty
73 public class AutelisHandler extends BaseThingHandler {
75 private final Logger logger = LoggerFactory.getLogger(AutelisHandler.class);
78 * Default timeout for http connections to a Autelis controller
80 static final int TIMEOUT_SECONDS = 5;
83 * Autelis controllers will not update their XML immediately after we change
84 * a value. To compensate we cache previous values for a {@link Channel}
85 * using the item name as a key. After a polling run has been executed we
86 * only update an channel if the value is different then what's in the
87 * cache. This cache is cleared after a fixed time period when commands are
90 private Map<String, State> stateMap = Collections.synchronizedMap(new HashMap<>());
93 * Clear our state every hour
95 private static final int NORMAL_CLEARTIME_SECONDS = 60 * 60;
98 * Default poll rate rate, this is derived from the Autelis web UI
100 private static final int DEFAULT_REFRESH_SECONDS = 3;
103 * How long should we wait to poll after we send an update, derived from trial and error
105 private static final int COMMAND_UPDATE_TIME_SECONDS = 6;
108 * The autelis unit will 'loose' commands if sent to fast
110 private static final int THROTTLE_TIME_MILLISECONDS = 500;
115 private static final int WEB_PORT = 80;
118 * Pentair values for pump response
120 private static final String[] PUMP_TYPES = { "watts", "rpm", "gpm", "filer", "error" };
123 * Matcher for pump channel names for Pentair
125 private static final Pattern PUMPS_PATTERN = Pattern.compile("(pumps/pump\\d?)-(watts|rpm|gpm|filter|error)");
128 * Holds the next clear time in millis
130 private long clearTime;
133 * Constructed URL consisting of host and port
135 private String baseURL;
143 * The http client used for polling requests
145 private HttpClient client = new HttpClient();
148 * last time we finished a request
150 private long lastRequestTime = 0;
153 * Authentication for login
155 private String basicAuthentication;
158 * Regex expression to match XML responses from the Autelis, this is used to
159 * combine similar XML docs into a single document, {@link XPath} is still
160 * used for XML querying
162 private Pattern responsePattern = Pattern.compile("<response>(.+?)</response>", Pattern.DOTALL);
165 * Future to poll for updated
167 private ScheduledFuture<?> pollFuture;
169 public AutelisHandler(Thing thing) {
174 public void initialize() {
175 startHttpClient(client);
180 public void dispose() {
181 logger.debug("Handler disposed.");
183 stopHttpClient(client);
187 public void channelLinked(ChannelUID channelUID) {
188 // clear our cached values so the new channel gets updated
193 public void handleCommand(ChannelUID channelUID, Command command) {
194 logger.debug("handleCommand channel: {} command: {}", channelUID.getId(), command);
195 if (AutelisBindingConstants.CMD_LIGHTS.equals(channelUID.getId())) {
197 * lighting command possible values, but we will let anything
198 * through. alloff, allon, csync, cset, cswim, party, romance,
199 * caribbean, american, sunset, royalty, blue, green, red, white,
200 * magenta, hold, recall
202 getUrl(baseURL + "/lights.cgi?val=" + command.toString(), TIMEOUT_SECONDS);
203 } else if (AutelisBindingConstants.CMD_REBOOT.equals(channelUID.getId()) && command == OnOffType.ON) {
204 getUrl(baseURL + "/userreboot.cgi?do=true" + command.toString(), TIMEOUT_SECONDS);
205 updateState(channelUID, OnOffType.OFF);
207 String[] args = channelUID.getId().split("-");
208 if (args.length < 2) {
209 logger.warn("Unown channel {} for command {}", channelUID, command);
212 String type = args[0];
213 String name = args[1];
215 if (AutelisBindingConstants.CMD_EQUIPMENT.equals(type)) {
216 String cmd = "value";
218 if (command == OnOffType.OFF) {
220 } else if (command == OnOffType.ON) {
222 } else if (command instanceof DecimalType) {
223 value = ((DecimalType) command).intValue();
224 if (!isJandy() && value >= 3) {
225 // this is a autelis dim type. not sure what 2 does
229 logger.error("command type {} is not supported", command);
232 String response = getUrl(baseURL + "/set.cgi?name=" + name + "&" + cmd + "=" + value, TIMEOUT_SECONDS);
233 logger.debug("equipment set {} {} {} : result {}", name, cmd, value, response);
234 } else if (AutelisBindingConstants.CMD_TEMP.equals(type)) {
236 if (command == IncreaseDecreaseType.INCREASE) {
238 } else if (command == IncreaseDecreaseType.DECREASE) {
240 } else if (command == OnOffType.OFF) {
242 } else if (command == OnOffType.ON) {
245 value = command.toString();
249 // name ending in sp are setpoints, ht are heater?
250 if (name.endsWith("sp")) {
252 } else if (name.endsWith("ht")) {
255 logger.error("Unknown temp type {}", name);
258 String response = getUrl(baseURL + "/set.cgi?wait=1&name=" + name + "&" + cmd + "=" + value,
260 logger.debug("temp set name:{} cmd:{} value:{} : result {}", name, cmd, value, response);
261 } else if (AutelisBindingConstants.CMD_CHEM.equals(type)) {
262 String response = getUrl(baseURL + "/set.cgi?name=" + name + "&chem=" + command.toString(),
264 logger.debug("chlrp {} {}: result {}", name, command, response);
265 } else if (AutelisBindingConstants.CMD_PUMPS.equals(type)) {
266 String response = getUrl(baseURL + "/set.cgi?name=" + name + "&speed=" + command.toString(),
268 logger.debug("pumps {} {}: result {}", name, command, response);
270 logger.error("Unsupported type {}", type);
274 // reset the schedule for our next poll which at that time will reflect if our command was successful or not.
275 initPolling(COMMAND_UPDATE_TIME_SECONDS);
279 * Configures this thing
281 private void configure() {
284 AutelisConfiguration configuration = getConfig().as(AutelisConfiguration.class);
285 Integer refreshOrNull = configuration.refresh;
286 Integer portOrNull = configuration.port;
287 String host = configuration.host;
288 String username = configuration.user;
289 String password = configuration.password;
291 if (StringUtils.isBlank(username)) {
292 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "username must not be empty");
296 if (StringUtils.isBlank(password)) {
297 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "password must not be empty");
301 if (StringUtils.isBlank(host)) {
302 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "hostname must not be empty");
306 refresh = DEFAULT_REFRESH_SECONDS;
307 if (refreshOrNull != null) {
308 refresh = refreshOrNull.intValue();
312 if (portOrNull != null) {
313 port = portOrNull.intValue();
316 baseURL = "http://" + host + ":" + port;
317 basicAuthentication = "Basic " + B64Code.encode(username + ":" + password, StringUtil.__ISO_8859_1);
318 logger.debug("Autelius binding configured with base url {} and refresh period of {}", baseURL, refresh);
324 * Starts/Restarts polling with an initial delay. This allows changes in the poll cycle for when commands are sent
325 * and we need to poll sooner then the next refresh cycle.
327 private synchronized void initPolling(int initalDelay) {
329 pollFuture = scheduler.scheduleWithFixedDelay(() -> {
331 pollAutelisController();
332 } catch (Exception e) {
333 logger.debug("Exception during poll", e);
335 }, initalDelay, DEFAULT_REFRESH_SECONDS, TimeUnit.SECONDS);
339 * Stops/clears this thing's polling future
341 private void clearPolling() {
342 if (pollFuture != null && !pollFuture.isCancelled()) {
343 logger.trace("Canceling future");
344 pollFuture.cancel(false);
349 * Poll the Autelis controller for updates. This will retrieve various xml documents and update channel states from
352 private void pollAutelisController() {
353 logger.trace("Connecting to {}", baseURL);
355 // clear our cached stated IF it is time.
358 // we will reconstruct the document with all the responses combined for XPATH
359 StringBuilder sb = new StringBuilder("<response>");
361 // pull down the three xml documents
362 String[] statuses = { "status", "chem", "pumps" };
364 for (String status : statuses) {
365 String response = getUrl(baseURL + "/" + status + ".xml", TIMEOUT_SECONDS);
366 logger.trace("{}/{}.xml \n {}", baseURL, status, response);
367 if (response == null) {
368 // all models and versions have the status.xml endpoint
369 if (status.equals("status")) {
370 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR);
373 // not all models have the other endpoints, so we ignore errors
377 // get the xml data between the response tags and append to our main
379 Matcher m = responsePattern.matcher(response);
381 sb.append(m.group(1));
384 // finish our "new" XML Document
385 sb.append("</response>");
387 if (!ThingStatus.ONLINE.equals(getThing().getStatus())) {
388 updateStatus(ThingStatus.ONLINE);
392 * This xmlDoc will now contain the three XML documents we retrieved
393 * wrapped in response tags for easier querying in XPath.
395 HashMap<String, String> pumps = new HashMap<>();
396 String xmlDoc = sb.toString();
397 for (Channel channel : getThing().getChannels()) {
398 String key = channel.getUID().getId().replaceFirst("-", "/");
399 XPathFactory xpathFactory = XPathFactory.newInstance();
400 XPath xpath = xpathFactory.newXPath();
402 InputSource is = new InputSource(new StringReader(xmlDoc));
406 * Work around for Pentair pumps. Rather then have child XML elements, the response rather uses commas
407 * on the pump response to separate the different values like so:
409 * watts,rpm,gpm,filter,error
411 * Also, some pools will only report the first 3 out of the 5 values.
414 Matcher matcher = PUMPS_PATTERN.matcher(key);
415 if (matcher.matches()) {
416 if (!pumps.containsKey(key)) {
417 String pumpValue = xpath.evaluate("response/" + matcher.group(1), is);
418 String[] values = pumpValue.split(",");
419 for (int i = 0; i < PUMP_TYPES.length; i++) {
421 // this will be something like pump/pump1-rpm
422 String newKey = matcher.group(1) + '-' + PUMP_TYPES[i];
424 // some Pentair models only have the first 3 values
425 if (i < values.length) {
426 pumps.put(newKey, values[i]);
428 pumps.put(newKey, "");
432 value = pumps.get(key);
434 value = xpath.evaluate("response/" + key, is);
436 // Convert pentair salt levels to PPM.
437 if ("chlor/salt".equals(key)) {
439 value = String.valueOf(Integer.parseInt(value) * 50);
440 } catch (NumberFormatException ignored) {
441 logger.debug("Failed to parse pentair salt level as integer");
446 if (StringUtils.isEmpty((value))) {
450 State state = toState(channel.getAcceptedItemType(), value);
451 State oldState = stateMap.put(channel.getUID().getAsString(), state);
452 if (!state.equals(oldState)) {
453 logger.trace("updating channel {} with state {} (old state {})", channel.getUID(), state, oldState);
454 updateState(channel.getUID(), state);
456 } catch (XPathExpressionException e) {
457 logger.error("could not parse xml", e);
463 * Simple logic to perform a authenticated GET request
469 private synchronized String getUrl(String url, int timeout) {
470 // throttle commands for a very short time to avoid 'loosing' them
471 long now = System.currentTimeMillis();
472 long nextReq = lastRequestTime + THROTTLE_TIME_MILLISECONDS;
475 logger.trace("Throttling request for {} mills", nextReq - now);
476 Thread.sleep(nextReq - now);
477 } catch (InterruptedException ignored) {
480 String getURL = url + (url.contains("?") ? "&" : "?") + "timestamp=" + System.currentTimeMillis();
481 logger.trace("Getting URL {} ", getURL);
482 Request request = client.newRequest(getURL).timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS);
483 request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
485 ContentResponse response = request.send();
486 int statusCode = response.getStatus();
487 if (statusCode != HttpStatus.OK_200) {
488 logger.trace("Method failed: {}", response.getStatus() + " " + response.getReason());
491 lastRequestTime = System.currentTimeMillis();
492 return response.getContentAsString();
493 } catch (Exception e) {
494 logger.debug("Could not make http connection", e);
500 * Converts a {@link String} value to a {@link State} for a given
501 * {@link String} accepted type
505 * @return {@link State}
507 private State toState(String type, String value) throws NumberFormatException {
508 if ("Number".equals(type)) {
509 return new DecimalType(value);
510 } else if ("Switch".equals(type)) {
511 return Integer.parseInt(value) > 0 ? OnOffType.ON : OnOffType.OFF;
513 return StringType.valueOf(value);
518 * Clears our state if it is time
520 private void clearState(boolean force) {
521 if (force || System.currentTimeMillis() >= clearTime) {
523 clearTime = System.currentTimeMillis() + (NORMAL_CLEARTIME_SECONDS * 1000);
527 private void startHttpClient(HttpClient client) {
528 if (!client.isStarted()) {
531 } catch (Exception e) {
532 logger.error("Could not stop HttpClient", e);
537 private void stopHttpClient(HttpClient client) {
538 if (client != null) {
539 client.getAuthenticationStore().clearAuthentications();
540 client.getAuthenticationStore().clearAuthenticationResults();
541 if (client.isStarted()) {
544 } catch (Exception e) {
545 logger.error("Could not stop HttpClient", e);
551 private boolean isJandy() {
552 return getThing().getThingTypeUID() == AutelisBindingConstants.JANDY_THING_TYPE_UID;