]> git.basschouten.com Git - openhab-addons.git/blob
e67aa19c029d89aab5d45a4cad8083edc1e91cef
[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.pentair.internal.handler;
14
15 import static org.openhab.binding.pentair.internal.PentairBindingConstants.*;
16
17 import java.math.BigDecimal;
18
19 import org.openhab.binding.pentair.internal.PentairBindingConstants;
20 import org.openhab.binding.pentair.internal.PentairPacket;
21 import org.openhab.binding.pentair.internal.PentairPacketPumpStatus;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link PentairIntelliFloHandler} is responsible for implementation of the Intelliflo Pump. This will
34  * parse/dispose of
35  * status packets to set the stat for various channels.
36  *
37  * @author Jeff James - Initial contribution
38  */
39 public class PentairIntelliFloHandler extends PentairBaseThingHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(PentairIntelliFloHandler.class);
42     protected PentairPacketPumpStatus ppscur = new PentairPacketPumpStatus();
43
44     public PentairIntelliFloHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void initialize() {
50         logger.debug("Initializing Intelliflo - Thing ID: {}.", this.getThing().getUID());
51
52         id = ((BigDecimal) getConfig().get("id")).intValue();
53
54         updateStatus(ThingStatus.ONLINE);
55     }
56
57     @Override
58     public void dispose() {
59         logger.debug("Thing {} disposed.", getThing().getUID());
60     }
61
62     @Override
63     public void handleCommand(ChannelUID channelUID, Command command) {
64         if (command instanceof RefreshType) {
65             logger.debug("IntelliFlo received refresh command");
66             updateChannel(channelUID.getId(), null);
67         }
68     }
69
70     @Override
71     public void processPacketFrom(PentairPacket p) {
72         switch (p.getAction()) {
73             case 1: // Pump command - A5 00 10 60 01 02 00 20
74                 logger.trace("Pump command (ack): {}: ", p);
75                 break;
76             case 4: // Pump control panel on/off
77                 logger.trace("Turn pump control panel (ack) {}: {} - {}", p.getSource(),
78                         p.getByte(PentairPacket.STARTOFDATA), p);
79                 break;
80             case 5: // Set pump mode
81                 logger.trace("Set pump mode (ack) {}: {} - {}", p.getSource(), p.getByte(PentairPacket.STARTOFDATA), p);
82                 break;
83             case 6: // Set run mode
84                 logger.trace("Set run mode (ack) {}: {} - {}", p.getSource(), p.getByte(PentairPacket.STARTOFDATA), p);
85                 break;
86             case 7: // Pump status (after a request)
87                 if (p.getLength() != 15) {
88                     logger.debug("Expected length of 15: {}", p);
89                     return;
90                 }
91
92                 /*
93                  * P: A500 d=10 s=60 c=07 l=0f 0A0602024A08AC120000000A000F22 <028A>
94                  * RUN 0a Started
95                  * MOD 06 Feature 1
96                  * PMP 02 ? drive state
97                  * PWR 024a 586 WATT
98                  * RPM 08ac 2220 RPM
99                  * GPM 12 18 GPM
100                  * PPC 00 0 %
101                  * b09 00 ?
102                  * ERR 00 ok
103                  * b11 0a ?
104                  * TMR 00 0 MIN
105                  * CLK 0f22 15:34
106                  */
107
108                 logger.debug("Pump status: {}", p);
109
110                 /*
111                  * Save the previous state of the packet (p29cur) into a temp variable (p29old)
112                  * Update the current state to the new packet we just received.
113                  * Then call updateChannel which will compare the previous state (now p29old) to the new state (p29cur)
114                  * to determine if updateState needs to be called
115                  */
116                 PentairPacketPumpStatus ppsOld = ppscur;
117                 ppscur = new PentairPacketPumpStatus(p);
118
119                 updateChannel(INTELLIFLO_RUN, ppsOld);
120                 updateChannel(INTELLIFLO_MODE, ppsOld);
121                 updateChannel(INTELLIFLO_DRIVESTATE, ppsOld);
122                 updateChannel(INTELLIFLO_POWER, ppsOld);
123                 updateChannel(INTELLIFLO_RPM, ppsOld);
124                 updateChannel(INTELLIFLO_PPC, ppsOld);
125                 updateChannel(INTELLIFLO_ERROR, ppsOld);
126                 updateChannel(INTELLIFLO_TIMER, ppsOld);
127
128                 break;
129             default:
130                 logger.debug("Unhandled Intelliflo command: {}", p.toString());
131                 break;
132         }
133     }
134
135     /**
136      * Helper function to compare and update channel if needed. The class variables p29_cur and phsp_cur are used to
137      * determine the appropriate state of the channel.
138      *
139      * @param channel name of channel to be updated, corresponds to channel name in {@link PentairBindingConstants}
140      * @param p Packet representing the former state. If null, no compare is done and state is updated.
141      */
142     public void updateChannel(String channel, PentairPacket p) {
143         // Only called from this class's processPacketFrom, so we are confident this will be a PentairPacketPumpStatus
144         PentairPacketPumpStatus pps = (PentairPacketPumpStatus) p;
145
146         switch (channel) {
147             case INTELLIFLO_RUN:
148                 if (pps == null || (pps.run != ppscur.run)) {
149                     updateState(channel, OnOffType.from((ppscur.run)));
150                 }
151                 break;
152             case INTELLIFLO_MODE:
153                 if (pps == null || (pps.mode != ppscur.mode)) {
154                     updateState(channel, new DecimalType(ppscur.mode));
155                 }
156                 break;
157             case INTELLIFLO_DRIVESTATE:
158                 if (pps == null || (pps.drivestate != ppscur.drivestate)) {
159                     updateState(channel, new DecimalType(ppscur.drivestate));
160                 }
161                 break;
162             case INTELLIFLO_POWER:
163                 if (pps == null || (pps.power != ppscur.power)) {
164                     updateState(channel, new DecimalType(ppscur.power));
165                 }
166                 break;
167             case INTELLIFLO_RPM:
168                 if (pps == null || (pps.rpm != ppscur.rpm)) {
169                     updateState(channel, new DecimalType(ppscur.rpm));
170                 }
171                 break;
172             case INTELLIFLO_PPC:
173                 if (pps == null || (pps.ppc != ppscur.ppc)) {
174                     updateState(channel, new DecimalType(ppscur.ppc));
175                 }
176                 break;
177             case INTELLIFLO_ERROR:
178                 if (pps == null || (pps.error != ppscur.error)) {
179                     updateState(channel, new DecimalType(ppscur.error));
180                 }
181                 break;
182             case INTELLIFLO_TIMER:
183                 if (pps == null || (pps.timer != ppscur.timer)) {
184                     updateState(channel, new DecimalType(ppscur.timer));
185                 }
186                 break;
187         }
188     }
189 }