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