]> git.basschouten.com Git - openhab-addons.git/blob
0f2813e5549c2d817d81ef20b550edf4065b953d
[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.phc.internal.handler;
14
15 import static org.openhab.binding.phc.internal.PHCBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.StopMoveType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link PHCHandler} is responsible for handling commands, which are
41  * sent to one of the channels.
42  *
43  * @author Jonas Hohaus - Initial contribution
44  *
45  */
46 public class PHCHandler extends BaseThingHandler {
47
48     private final Logger logger = LoggerFactory.getLogger(PHCHandler.class);
49
50     private String moduleAddress; // like DIP switches
51     private byte module;
52     private final short[] times = new short[4];
53     private final Map<String, State> channelState = new HashMap<>();
54     private PHCBridgeHandler bridgeHandler;
55
56     public PHCHandler(Thing thing) {
57         super(thing);
58     }
59
60     @Override
61     public void initialize() {
62         moduleAddress = (String) getConfig().get(ADDRESS);
63
64         if (getPHCBridgeHandler() == null) {
65             return;
66         }
67
68         module = Byte.parseByte(new StringBuilder(moduleAddress).reverse().toString(), 2);
69
70         if (getThing().getThingTypeUID().equals(THING_TYPE_AM) || getThing().getThingTypeUID().equals(THING_TYPE_JRM)) {
71             module |= 0x40;
72         } else if (getThing().getThingTypeUID().equals(THING_TYPE_DIM)) {
73             module |= 0xA0;
74         }
75         getPHCBridgeHandler().addModule(module);
76
77         if (getThing().getThingTypeUID().equals(THING_TYPE_JRM)) {
78             times[0] = (short) (((BigDecimal) getConfig().get(UP_DOWN_TIME_1)).shortValue() * 10);
79             times[1] = (short) (((BigDecimal) getConfig().get(UP_DOWN_TIME_2)).shortValue() * 10);
80             times[2] = (short) (((BigDecimal) getConfig().get(UP_DOWN_TIME_3)).shortValue() * 10);
81             times[3] = (short) (((BigDecimal) getConfig().get(UP_DOWN_TIME_4)).shortValue() * 10);
82
83         } else if (getThing().getThingTypeUID().equals(THING_TYPE_DIM)) {
84             times[0] = (((BigDecimal) getConfig().get(DIM_TIME_1)).shortValue());
85             times[1] = (((BigDecimal) getConfig().get(DIM_TIME_2)).shortValue());
86         }
87
88         Bridge bridge = getBridge();
89         if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
90             updateStatus(ThingStatus.ONLINE);
91         } else {
92             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
93         }
94     }
95
96     public void handleIncoming(String channelId, OnOffType state) {
97         if (logger.isDebugEnabled()) {
98             logger.debug("EM command: {}, last: {}, in: {}", channelId, channelState.get(channelId), state);
99         }
100
101         if (!channelState.containsKey(channelId) || !channelState.get(channelId).equals(state)) {
102             postCommand(channelId, state);
103             channelState.put(channelId, state);
104         }
105     }
106
107     @Override
108     public void handleCommand(ChannelUID channelUID, Command command) {
109         final String groupId = channelUID.getGroupId();
110         if (getThing().getStatus().equals(ThingStatus.ONLINE)) {
111             if ((CHANNELS_JRM.equals(groupId) && (command instanceof UpDownType || command instanceof StopMoveType))
112                     || (CHANNELS_DIM.equals(groupId)
113                             && (command instanceof OnOffType || command instanceof PercentType))) {
114                 getPHCBridgeHandler().send(groupId, module & 0x1F, channelUID.getIdWithoutGroup(), command,
115                         times[Integer.parseInt(channelUID.getIdWithoutGroup())]);
116             } else if ((CHANNELS_AM.equals(groupId) || CHANNELS_EM_LED.equals(groupId))
117                     && command instanceof OnOffType) {
118                 getPHCBridgeHandler().send(groupId, module & 0x1F, channelUID.getIdWithoutGroup(), command, (short) 0);
119             }
120
121             logger.debug("send command: {}, {}", channelUID, command);
122         } else {
123             logger.info("The Thing {} is offline.", getThing().getUID());
124         }
125     }
126
127     @Override
128     public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
129         if (isInitialized()) { // prevents change of address
130             validateConfigurationParameters(configurationParameters);
131
132             Configuration configuration = editConfiguration();
133             for (Entry<String, Object> configurationParmeter : configurationParameters.entrySet()) {
134                 if (!configurationParmeter.getKey().equals(ADDRESS)) {
135                     configuration.put(configurationParmeter.getKey(), configurationParmeter.getValue());
136                 } else {
137                     configuration.put(configurationParmeter.getKey(), moduleAddress);
138                 }
139             }
140
141             // persist new configuration and reinitialize handler
142             dispose();
143             updateConfiguration(configuration);
144             initialize();
145         } else {
146             super.handleConfigurationUpdate(configurationParameters);
147         }
148     }
149
150     private PHCBridgeHandler getPHCBridgeHandler() {
151         if (bridgeHandler == null) {
152             Bridge bridge = getBridge();
153             if (bridge == null) {
154                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
155                         "The Thing requires to select a Bridge");
156                 return null;
157             }
158
159             ThingHandler handler = bridge.getHandler();
160             if (handler instanceof PHCBridgeHandler phcBridgeHandler) {
161                 bridgeHandler = phcBridgeHandler;
162             } else {
163                 logger.debug("No available bridge handler for {}.", bridge.getUID());
164
165                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR,
166                         "No available bridge handler.");
167
168                 return null;
169             }
170         }
171
172         return bridgeHandler;
173     }
174 }