]> git.basschouten.com Git - openhab-addons.git/blob
129d95a47b6d9e80ccfab30a57b99bf7d5e7922e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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             case "system.power":
71                 return Integer.parseInt(value) > 0 ? OnOffType.ON : OnOffType.OFF;
72             case "Number:Dimensionless":
73                 switch (channelID) {
74                     case HaywardBindingConstants.CHANNEL_CHLORINATOR_AVGSALTLEVEL:
75                         return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
76                     case HaywardBindingConstants.CHANNEL_CHLORINATOR_INSTANTSALTLEVEL:
77                         return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
78                     case HaywardBindingConstants.CHANNEL_CHLORINATOR_TIMEDPERCENT:
79                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
80                     case HaywardBindingConstants.CHANNEL_FILTER_LASTSPEED:
81                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
82                     case HaywardBindingConstants.CHANNEL_FILTER_SPEEDPERCENT:
83                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
84                     case HaywardBindingConstants.CHANNEL_FILTER_SPEEDRPM:
85                     case HaywardBindingConstants.CHANNEL_PUMP_LASTSPEED:
86                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
87                     case HaywardBindingConstants.CHANNEL_PUMP_SPEEDPERCENT:
88                         return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
89                     case HaywardBindingConstants.CHANNEL_PUMP_SPEEDRPM:
90                 }
91                 return StringType.valueOf(value);
92             case "Number:Temperature":
93                 Bridge bridge = getBridge();
94                 if (bridge != null) {
95                     HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
96                     if (bridgehandler != null) {
97                         if (bridgehandler.account.units.equals("Standard")) {
98                             return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
99                         } else {
100                             return new QuantityType<>(Integer.parseInt(value), SIUnits.CELSIUS);
101                         }
102                     }
103                 }
104                 // default to imperial if no bridge
105                 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
106             default:
107                 return StringType.valueOf(value);
108         }
109     }
110
111     public String cmdToString(Command command) {
112         if (command == OnOffType.OFF) {
113             return "0";
114         } else if (command == OnOffType.ON) {
115             return "1";
116         } else if (command instanceof DecimalType) {
117             return ((DecimalType) command).toString();
118         } else if (command instanceof QuantityType) {
119             return ((QuantityType<?>) command).format("%1.0f");
120         } else {
121             return command.toString();
122         }
123     }
124
125     public Map<String, State> updateData(String channelID, String data) {
126         Map<String, State> channelStates = new HashMap<>();
127         Channel chan = getThing().getChannel(channelID);
128         if (chan != null) {
129             String acceptedItemType = chan.getAcceptedItemType();
130             if (acceptedItemType != null) {
131                 State state = toState(acceptedItemType, channelID, data);
132                 updateState(chan.getUID(), state);
133                 channelStates.put(channelID, state);
134             }
135         }
136         return channelStates;
137     }
138 }