]> git.basschouten.com Git - openhab-addons.git/blob
9e07d0f189479dbb8c658dd23abd8304c4b42393
[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 package org.openhab.binding.pentair.internal.handler;
14
15 import static org.openhab.binding.pentair.internal.PentairBindingConstants.*;
16
17 import org.openhab.binding.pentair.internal.PentairBindingConstants;
18 import org.openhab.binding.pentair.internal.PentairPacket;
19 import org.openhab.binding.pentair.internal.PentairPacketIntellichlor;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link PentairIntelliChlorHandler} is responsible for implementation of the Intellichlor Salt generator. It will
31  * process
32  * Intellichlor commands and set the appropriate channel states. There are currently no commands implemented for this
33  * Thing to receive from the framework.
34  *
35  * @author Jeff James - Initial contribution
36  */
37 public class PentairIntelliChlorHandler extends PentairBaseThingHandler {
38     private final Logger logger = LoggerFactory.getLogger(PentairIntelliChlorHandler.class);
39
40     protected PentairPacketIntellichlor pic3cur = new PentairPacketIntellichlor();
41     protected PentairPacketIntellichlor pic4cur = new PentairPacketIntellichlor();
42
43     public PentairIntelliChlorHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void initialize() {
49         logger.debug("Initializing IntelliChlor - Thing ID: {}.", this.getThing().getUID());
50
51         id = 0; // Intellichlor doesn't have ID
52
53         updateStatus(ThingStatus.ONLINE);
54     }
55
56     @Override
57     public void dispose() {
58         logger.debug("Thing {} disposed.", getThing().getUID());
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         if (command instanceof RefreshType) {
64             logger.debug("IntelliChlor received refresh command");
65             updateChannel(channelUID.getId(), null);
66         }
67     }
68
69     @Override
70     public void processPacketFrom(PentairPacket p) {
71         PentairPacketIntellichlor pic = (PentairPacketIntellichlor) p;
72
73         switch (pic.getLength()) {
74             case 3:
75                 if (pic.getCmd() != 0x11) { // only packets with 0x11 have valid saltoutput numbers.
76                     break;
77                 }
78
79                 PentairPacketIntellichlor pic3Old = pic3cur;
80                 pic3cur = pic;
81
82                 updateChannel(INTELLICHLOR_SALTOUTPUT, pic3Old);
83
84                 break;
85             case 4:
86                 if (pic.getCmd() != 0x12) {
87                     break;
88                 }
89
90                 PentairPacketIntellichlor pic4Old = pic4cur;
91                 pic4cur = pic;
92
93                 updateChannel(INTELLICHLOR_SALINITY, pic4Old);
94
95                 break;
96         }
97
98         logger.debug("Intellichlor command: {}", pic);
99     }
100
101     /**
102      * Helper function to compare and update channel if needed. The class variables p29_cur and phsp_cur are used to
103      * determine the appropriate state of the channel.
104      *
105      * @param channel name of channel to be updated, corresponds to channel name in {@link PentairBindingConstants}
106      * @param p Packet representing the former state. If null, no compare is done and state is updated.
107      */
108     public void updateChannel(String channel, PentairPacket p) {
109         PentairPacketIntellichlor pic = (PentairPacketIntellichlor) p;
110
111         switch (channel) {
112             case INTELLICHLOR_SALINITY:
113                 if (pic == null || (pic.salinity != pic4cur.salinity)) {
114                     updateState(channel, new DecimalType(pic4cur.salinity));
115                 }
116                 break;
117             case INTELLICHLOR_SALTOUTPUT:
118                 if (pic == null || (pic.saltoutput != pic3cur.saltoutput)) {
119                     updateState(channel, new DecimalType(pic3cur.saltoutput));
120                 }
121                 break;
122         }
123     }
124 }