]> git.basschouten.com Git - openhab-addons.git/blob
48db232cad3713fc52de45e793e932453fdf579d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.haywardomnilogic.internal.handler.HaywardBridgeHandler;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.ImperialUnits;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
36
37 /**
38  * The {@link HaywarThingHandler} is a subclass of the BaseThingHandler and a Super
39  * Class to each Hayward Thing Handler
40  *
41  * @author Matt Myers - Initial contribution
42  */
43
44 @NonNullByDefault
45 public abstract class HaywardThingHandler extends BaseThingHandler {
46
47     public HaywardThingHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     public void initialize() {
53         updateStatus(ThingStatus.ONLINE);
54     }
55
56     @Override
57     public void handleCommand(ChannelUID channelUID, Command command) {
58     }
59
60     public abstract void getTelemetry(String xmlResponse) throws HaywardException;
61
62     public void setStateDescriptions() throws HaywardException {
63     }
64
65     public State toState(String type, String channelID, String value) throws NumberFormatException {
66         switch (type) {
67             case "Number":
68                 return new DecimalType(value);
69             case "Switch":
70                 return Integer.parseInt(value) > 0 ? OnOffType.ON : OnOffType.OFF;
71             case "Number:Dimensionless":
72                 switch (channelID) {
73                     case HaywardBindingConstants.CHANNEL_CHLORINATOR_AVGSALTLEVEL:
74                         return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
75                     case HaywardBindingConstants.CHANNEL_CHLORINATOR_INSTANTSALTLEVEL:
76                         return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
77                     case HaywardBindingConstants.CHANNEL_CHLORINATOR_TIMEDPERCENT:
78                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
79                     case HaywardBindingConstants.CHANNEL_FILTER_LASTSPEED:
80                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
81                     case HaywardBindingConstants.CHANNEL_FILTER_SPEEDPERCENT:
82                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
83                     case HaywardBindingConstants.CHANNEL_FILTER_SPEEDRPM:
84                     case HaywardBindingConstants.CHANNEL_PUMP_LASTSPEED:
85                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
86                     case HaywardBindingConstants.CHANNEL_PUMP_SPEEDPERCENT:
87                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
88                     case HaywardBindingConstants.CHANNEL_PUMP_SPEEDRPM:
89                 }
90                 return StringType.valueOf(value);
91             case "Number:Temperature":
92                 Bridge bridge = getBridge();
93                 if (bridge != null) {
94                     HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
95                     if (bridgehandler != null) {
96                         if ("Standard".equals(bridgehandler.account.units)) {
97                             return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
98                         } else {
99                             return new QuantityType<>(Integer.parseInt(value), SIUnits.CELSIUS);
100                         }
101                     }
102                 }
103                 // default to imperial if no bridge
104                 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
105             default:
106                 return StringType.valueOf(value);
107         }
108     }
109
110     public String cmdToString(Command command) {
111         if (command == OnOffType.OFF) {
112             return "0";
113         } else if (command == OnOffType.ON) {
114             return "1";
115         } else if (command instanceof DecimalType decimalCommand) {
116             return decimalCommand.toString();
117         } else if (command instanceof QuantityType quantityCommand) {
118             return quantityCommand.format("%1.0f");
119         } else {
120             return command.toString();
121         }
122     }
123
124     public Map<String, State> updateData(String channelID, String data) {
125         Map<String, State> channelStates = new HashMap<>();
126         Channel chan = getThing().getChannel(channelID);
127         if (chan != null) {
128             String acceptedItemType = chan.getAcceptedItemType();
129             if (acceptedItemType != null) {
130                 State state = toState(acceptedItemType, channelID, data);
131                 updateState(chan.getUID(), state);
132                 channelStates.put(channelID, state);
133             }
134         }
135         return channelStates;
136     }
137 }