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.haywardomnilogic.internal.handler;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.haywardomnilogic.internal.HaywardBindingConstants;
20 import org.openhab.binding.haywardomnilogic.internal.HaywardException;
21 import org.openhab.binding.haywardomnilogic.internal.HaywardThingHandler;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The Chlorinator Handler
35 * @author Matt Myers - Initial contribution
38 public class HaywardChlorinatorHandler extends HaywardThingHandler {
39 private final Logger logger = LoggerFactory.getLogger(HaywardChlorinatorHandler.class);
40 public String chlorTimedPercent = "";
41 public String chlorState = "";
43 public HaywardChlorinatorHandler(Thing thing) {
48 public void getTelemetry(String xmlResponse) throws HaywardException {
49 List<String> systemIDs = new ArrayList<>();
50 List<String> data = new ArrayList<>();
52 Bridge bridge = getBridge();
54 HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
55 if (bridgehandler != null) {
56 systemIDs = bridgehandler.evaluateXPath("//Chlorinator/@systemId", xmlResponse);
57 String thingSystemID = getThing().getUID().getId();
58 for (int i = 0; i < systemIDs.size(); i++) {
59 if (systemIDs.get(i).equals(thingSystemID)) {
61 data = bridgehandler.evaluateXPath("//Chlorinator/@operatingMode", xmlResponse);
62 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_OPERATINGMODE, data.get(i));
65 data = bridgehandler.evaluateXPath("//Chlorinator/@Timed-Percent", xmlResponse);
66 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_TIMEDPERCENT, data.get(i));
67 this.chlorTimedPercent = data.get(0);
70 data = bridgehandler.evaluateXPath("//Chlorinator/@scMode", xmlResponse);
71 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_SCMODE, data.get(i));
74 data = bridgehandler.evaluateXPath("//Chlorinator/@chlrError", xmlResponse);
75 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_ERROR, data.get(i));
78 data = bridgehandler.evaluateXPath("//Chlorinator/@chlrAlert", xmlResponse);
79 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_ALERT, data.get(i));
82 data = bridgehandler.evaluateXPath("//Chlorinator/@avgSaltLevel", xmlResponse);
83 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_AVGSALTLEVEL, data.get(i));
86 data = bridgehandler.evaluateXPath("//Chlorinator/@instantSaltLevel", xmlResponse);
87 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_INSTANTSALTLEVEL, data.get(i));
90 data = bridgehandler.evaluateXPath("//Chlorinator/@status", xmlResponse);
91 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_STATUS, data.get(i));
93 if ("0".equals(data.get(i))) {
94 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_ENABLE, "0");
95 // chlorState is used to set the chlorinator cfgState in the timedPercent command
96 this.chlorState = "2";
98 updateData(HaywardBindingConstants.CHANNEL_CHLORINATOR_ENABLE, "1");
99 // chlorState is used to set the chlorinator cfgState in the timedPercent command
100 this.chlorState = "3";
104 this.updateStatus(ThingStatus.ONLINE);
106 this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
112 public void handleCommand(ChannelUID channelUID, Command command) {
113 if ((command instanceof RefreshType)) {
117 String chlorCfgState = null;
118 String chlorTimedPercent = "0";
120 String systemID = getThing().getProperties().get(HaywardBindingConstants.PROPERTY_SYSTEM_ID);
121 String poolID = getThing().getProperties().get(HaywardBindingConstants.PROPERTY_BOWID);
123 Bridge bridge = getBridge();
124 if (bridge != null) {
125 HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
126 if (bridgehandler != null) {
127 String cmdString = this.cmdToString(command);
129 switch (channelUID.getId()) {
130 case HaywardBindingConstants.CHANNEL_CHLORINATOR_ENABLE:
131 if ("1".equals(cmdString)) {
133 chlorTimedPercent = this.chlorTimedPercent;
136 chlorTimedPercent = this.chlorTimedPercent;
139 case HaywardBindingConstants.CHANNEL_CHLORINATOR_TIMEDPERCENT:
140 chlorCfgState = this.chlorState;
141 chlorTimedPercent = cmdString;
144 logger.warn("haywardCommand Unsupported type {}", channelUID);
148 String cmdURL = HaywardBindingConstants.COMMAND_PARAMETERS
149 + "<Name>SetCHLORParams</Name><Parameters>"
150 + "<Parameter name=\"Token\" dataType=\"String\">" + bridgehandler.account.token
151 + "</Parameter>" + "<Parameter name=\"MspSystemID\" dataType=\"int\">"
152 + bridgehandler.account.mspSystemID + "</Parameter>"
153 + "<Parameter name=\"PoolID\" dataType=\"int\">" + poolID + "</Parameter>"
154 + "<Parameter name=\"ChlorID\" dataType=\"int\" alias=\"EquipmentID\">" + systemID
155 + "</Parameter>" + "<Parameter name=\"CfgState\" dataType=\"byte\" alias=\"Data1\">"
156 + chlorCfgState + "</Parameter>"
157 + "<Parameter name=\"OpMode\" dataType=\"byte\" alias=\"Data2\">1</Parameter>"
158 + "<Parameter name=\"BOWType\" dataType=\"byte\" alias=\"Data3\">1</Parameter>"
159 + "<Parameter name=\"CellType\" dataType=\"byte\" alias=\"Data4\">4</Parameter>"
160 + "<Parameter name=\"TimedPercent\" dataType=\"byte\" alias=\"Data5\">" + chlorTimedPercent
162 + "<Parameter name=\"SCTimeout\" dataType=\"byte\" unit=\"hour\" alias=\"Data6\">24</Parameter>"
163 + "<Parameter name=\"ORPTimout\" dataType=\"byte\" unit=\"hour\" alias=\"Data7\">24</Parameter>"
164 + "</Parameters></Request>";
166 // *****Send Command to Hayward server
167 String xmlResponse = bridgehandler.httpXmlResponse(cmdURL);
168 String status = bridgehandler.evaluateXPath("//Parameter[@name='Status']/text()", xmlResponse)
171 if (!("0".equals(status))) {
172 logger.debug("haywardCommand XML response: {}", xmlResponse);
175 } catch (HaywardException e) {
176 logger.debug("Unable to send command to Hayward's server {}:{}:{}",
177 bridgehandler.config.endpointUrl, bridgehandler.config.username, e.getMessage());
178 } catch (InterruptedException e) {
181 this.updateStatus(ThingStatus.ONLINE);
183 this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);