]> git.basschouten.com Git - openhab-addons.git/blob
9a0a620a8777f245546e87eda42fe2456f91c47d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.fronius.internal.handler;
14
15 import java.math.BigDecimal;
16
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;
32
33 /**
34  * Basic Handler class for all Fronius services.
35  *
36  * @author Gerrit Beine - Initial contribution
37  * @author Thomas Rokohl - Refactoring to merge the concepts
38  */
39 public abstract class FroniusBaseThingHandler extends BaseThingHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(FroniusBaseThingHandler.class);
42     private final String serviceDescription;
43     private FroniusBridgeHandler bridgeHandler;
44
45     public FroniusBaseThingHandler(Thing thing) {
46         super(thing);
47         serviceDescription = getDescription();
48     }
49
50     @Override
51     public void handleCommand(ChannelUID channelUID, Command command) {
52         if (command instanceof RefreshType) {
53             updateChannel(channelUID.getId());
54         }
55     }
56
57     @Override
58     public void initialize() {
59         if (getFroniusBridgeHandler() == null) {
60             logger.debug("Initializing {} Service is only supported within a bridge", serviceDescription);
61             updateStatus(ThingStatus.OFFLINE);
62             return;
63         }
64         logger.debug("Initializing {} Service", serviceDescription);
65         getFroniusBridgeHandler().registerService(this);
66     }
67
68     private synchronized FroniusBridgeHandler getFroniusBridgeHandler() {
69         if (this.bridgeHandler == null) {
70             Bridge bridge = getBridge();
71             if (bridge == null) {
72                 return null;
73             }
74             ThingHandler handler = bridge.getHandler();
75             if (handler instanceof FroniusBridgeHandler) {
76                 this.bridgeHandler = (FroniusBridgeHandler) handler;
77             } else {
78                 return null;
79             }
80         }
81         return this.bridgeHandler;
82     }
83
84     /**
85      * Update all Channels
86      */
87     protected void updateChannels() {
88         for (Channel channel : getThing().getChannels()) {
89             updateChannel(channel.getUID().getId());
90         }
91     }
92
93     /**
94      * Update the channel from the last data
95      *
96      * @param channelId the id identifying the channel to be updated
97      */
98     protected void updateChannel(String channelId) {
99         if (!isLinked(channelId)) {
100             return;
101         }
102
103         Object value = getValue(channelId);
104         if (value == null) {
105             logger.debug("Value retrieved for channel '{}' was null. Can't update.", channelId);
106             return;
107         }
108
109         State state = null;
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());
118         } else {
119             logger.warn("Update channel {}: Unsupported value type {}", channelId, value.getClass().getSimpleName());
120         }
121         logger.debug("Update channel {} with state {} ({})", channelId, (state == null) ? "null" : state.toString(),
122                 value.getClass().getSimpleName());
123
124         // Update the channel
125         if (state != null) {
126             updateState(channelId, state);
127         }
128     }
129
130     /**
131      * return an internal description for logging
132      *
133      * @return the description of the thing
134      */
135     protected abstract String getDescription();
136
137     /**
138      * get the "new" associated value for a channelId
139      *
140      * @param channelId the id identifying the channel
141      * @return the "new" associated value
142      */
143     protected abstract Object getValue(String channelId);
144
145     /**
146      * do whatever a thing must do to update the values
147      * this function is called from the bridge in a given interval
148      *
149      * @param bridgeConfiguration the connected bridge configuration
150      */
151     public abstract void refresh(FroniusBridgeConfiguration bridgeConfiguration);
152 }