]> git.basschouten.com Git - openhab-addons.git/blob
89f311fccffeb4748523131f6d9fa2eeac78282a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
14 package org.openhab.binding.haywardomnilogic.internal;
15
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;
33
34 /**
35  * The {@link HaywarThingHandler} is a subclass of the BaseThingHandler and a Super
36  * Class to each Hayward Thing Handler
37  *
38  * @author Matt Myers - Initial contribution
39  */
40
41 @NonNullByDefault
42 public abstract class HaywardThingHandler extends BaseThingHandler {
43
44     public HaywardThingHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void initialize() {
50         updateStatus(ThingStatus.ONLINE);
51     }
52
53     @Override
54     public void handleCommand(ChannelUID channelUID, Command command) {
55     }
56
57     public abstract void getTelemetry(String xmlResponse) throws HaywardException;
58
59     public State toState(String type, String channelID, String value) throws NumberFormatException {
60         switch (type) {
61             case "Number":
62                 return new DecimalType(value);
63             case "Switch":
64             case "system.power":
65                 return Integer.parseInt(value) > 0 ? OnOffType.ON : OnOffType.OFF;
66             case "Number:Dimensionless":
67                 switch (channelID) {
68                     case "chlorTimedPercent":
69                     case "filterSpeed":
70                     case "pumpSpeed":
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);
76                 }
77                 return StringType.valueOf(value);
78             case "Number:Temperature":
79                 Bridge bridge = getBridge();
80                 if (bridge != null) {
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);
85                         } else {
86                             return new QuantityType<>(Integer.parseInt(value), SIUnits.CELSIUS);
87                         }
88                     }
89                 }
90                 // default to imperial if no bridge
91                 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
92             default:
93                 return StringType.valueOf(value);
94         }
95     }
96
97     public String cmdToString(Command command) {
98         if (command == OnOffType.OFF) {
99             return "0";
100         } else if (command == OnOffType.ON) {
101             return "1";
102         } else if (command instanceof DecimalType) {
103             return ((DecimalType) command).toString();
104         } else if (command instanceof QuantityType) {
105             return ((QuantityType<?>) command).format("%1.0f");
106         } else {
107             return command.toString();
108         }
109     }
110
111     public void updateData(String channelID, String data) {
112         Channel chan = getThing().getChannel(channelID);
113         if (chan != null) {
114             String acceptedItemType = chan.getAcceptedItemType();
115             if (acceptedItemType != null) {
116                 State state = toState(acceptedItemType, channelID, data);
117                 updateState(chan.getUID(), state);
118             }
119         }
120     }
121 }