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
14 package org.openhab.binding.haywardomnilogic.internal;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.openhab.binding.haywardomnilogic.internal.handler.HaywardBridgeHandler;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.library.unit.ImperialUnits;
23 import org.openhab.core.library.unit.SIUnits;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
35 * The {@link HaywarThingHandler} is a subclass of the BaseThingHandler and a Super
36 * Class to each Hayward Thing Handler
38 * @author Matt Myers - Initial contribution
42 public abstract class HaywardThingHandler extends BaseThingHandler {
44 public HaywardThingHandler(Thing thing) {
49 public void initialize() {
50 updateStatus(ThingStatus.ONLINE);
54 public void handleCommand(ChannelUID channelUID, Command command) {
57 public abstract void getTelemetry(String xmlResponse) throws HaywardException;
59 public State toState(String type, String channelID, String value) throws NumberFormatException {
62 return new DecimalType(value);
65 return Integer.parseInt(value) > 0 ? OnOffType.ON : OnOffType.OFF;
66 case "Number:Dimensionless":
68 case "chlorTimedPercent":
71 case "filterLastSpeed":
72 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
73 case "chlorAvgSaltLevel":
74 case "chlorInstantSaltLevel":
75 return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
77 return StringType.valueOf(value);
78 case "Number:Temperature":
79 Bridge bridge = getBridge();
81 HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
82 if (bridgehandler != null) {
83 if (bridgehandler.account.units.equals("Standard")) {
84 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
86 return new QuantityType<>(Integer.parseInt(value), SIUnits.CELSIUS);
90 // default to imperial if no bridge
91 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
93 return StringType.valueOf(value);
97 public String cmdToString(Command command) {
98 if (command == OnOffType.OFF) {
100 } else if (command == OnOffType.ON) {
102 } else if (command instanceof DecimalType) {
103 return ((DecimalType) command).toString();
104 } else if (command instanceof QuantityType) {
105 return ((QuantityType<?>) command).format("%1.0f");
107 return command.toString();
111 public void updateData(String channelID, String data) {
112 Channel chan = getThing().getChannel(channelID);
114 String acceptedItemType = chan.getAcceptedItemType();
115 if (acceptedItemType != null) {
116 State state = toState(acceptedItemType, channelID, data);
117 updateState(chan.getUID(), state);