2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.fronius.internal.handler;
15 import java.math.BigDecimal;
17 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
18 import org.openhab.binding.fronius.internal.api.ValueUnit;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.Channel;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.openhab.core.types.State;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Basic Handler class for all Fronius services.
36 * @author Gerrit Beine - Initial contribution
37 * @author Thomas Rokohl - Refactoring to merge the concepts
39 public abstract class FroniusBaseThingHandler extends BaseThingHandler {
41 private final Logger logger = LoggerFactory.getLogger(FroniusBaseThingHandler.class);
42 private final String serviceDescription;
43 private FroniusBridgeHandler bridgeHandler;
45 public FroniusBaseThingHandler(Thing thing) {
47 serviceDescription = getDescription();
51 public void handleCommand(ChannelUID channelUID, Command command) {
52 if (command instanceof RefreshType) {
53 updateChannel(channelUID.getId());
58 public void initialize() {
59 if (getFroniusBridgeHandler() == null) {
60 logger.debug("Initializing {} Service is only supported within a bridge", serviceDescription);
61 updateStatus(ThingStatus.OFFLINE);
64 logger.debug("Initializing {} Service", serviceDescription);
65 getFroniusBridgeHandler().registerService(this);
68 private synchronized FroniusBridgeHandler getFroniusBridgeHandler() {
69 if (this.bridgeHandler == null) {
70 Bridge bridge = getBridge();
74 ThingHandler handler = bridge.getHandler();
75 if (handler instanceof FroniusBridgeHandler) {
76 this.bridgeHandler = (FroniusBridgeHandler) handler;
81 return this.bridgeHandler;
87 protected void updateChannels() {
88 for (Channel channel : getThing().getChannels()) {
89 updateChannel(channel.getUID().getId());
94 * Update the channel from the last data
96 * @param channelId the id identifying the channel to be updated
98 protected void updateChannel(String channelId) {
99 if (!isLinked(channelId)) {
103 Object value = getValue(channelId);
105 logger.debug("Value retrieved for channel '{}' was null. Can't update.", channelId);
110 if (value instanceof BigDecimal) {
111 state = new DecimalType((BigDecimal) value);
112 } else if (value instanceof Integer) {
113 state = new DecimalType(BigDecimal.valueOf(((Integer) value).longValue()));
114 } else if (value instanceof Double) {
115 state = new DecimalType((double) value);
116 } else if (value instanceof ValueUnit) {
117 state = new DecimalType(((ValueUnit) value).getValue());
119 logger.warn("Update channel {}: Unsupported value type {}", channelId, value.getClass().getSimpleName());
121 logger.debug("Update channel {} with state {} ({})", channelId, (state == null) ? "null" : state.toString(),
122 value.getClass().getSimpleName());
124 // Update the channel
126 updateState(channelId, state);
131 * return an internal description for logging
133 * @return the description of the thing
135 protected abstract String getDescription();
138 * get the "new" associated value for a channelId
140 * @param channelId the id identifying the channel
141 * @return the "new" associated value
143 protected abstract Object getValue(String channelId);
146 * do whatever a thing must do to update the values
147 * this function is called from the bridge in a given interval
149 * @param bridgeConfiguration the connected bridge configuration
151 public abstract void refresh(FroniusBridgeConfiguration bridgeConfiguration);