]> git.basschouten.com Git - openhab-addons.git/blob
19351981809d2d36ff71d28909ac5b6830b3fdab
[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.teleinfo.internal.handler;
14
15 import static org.openhab.binding.teleinfo.internal.TeleinfoBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.teleinfo.internal.dto.common.FrameBaseOption;
20 import org.openhab.binding.teleinfo.internal.dto.common.FrameEjpOption;
21 import org.openhab.binding.teleinfo.internal.dto.common.FrameHcOption;
22 import org.openhab.binding.teleinfo.internal.dto.common.FrameTempoOption;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Channel;
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.ThingStatusInfo;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link TeleinfoAbstractElectricityMeterHandler} class defines a skeleton for Electricity Meters handlers.
41  *
42  * @author Nicolas SIBERIL - Initial contribution
43  */
44 @NonNullByDefault
45 public abstract class TeleinfoAbstractElectricityMeterHandler extends BaseThingHandler
46         implements TeleinfoControllerHandlerListener {
47     private final Logger logger = LoggerFactory.getLogger(TeleinfoAbstractElectricityMeterHandler.class);
48     protected TeleinfoElectricityMeterConfiguration configuration = new TeleinfoElectricityMeterConfiguration();
49
50     public TeleinfoAbstractElectricityMeterHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public void initialize() {
56         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
57
58         Bridge bridge = getBridge();
59         logger.debug("bridge = {}", bridge);
60         if (bridge != null) {
61             bridgeStatusChanged(bridge.getStatusInfo());
62         }
63         configuration = getConfigAs(TeleinfoElectricityMeterConfiguration.class);
64     }
65
66     @Override
67     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
68         if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
69             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
70             return;
71         }
72
73         Bridge bridge = getBridge();
74         if (bridge != null) {
75             TeleinfoAbstractControllerHandler controllerHandler = (TeleinfoAbstractControllerHandler) bridge
76                     .getHandler();
77             if (controllerHandler != null) {
78                 controllerHandler.addListener(this);
79                 updateStatus(ThingStatus.ONLINE);
80             }
81         }
82     }
83
84     @Override
85     public void handleCommand(ChannelUID channelUID, Command command) {
86         // no commands supported
87     }
88
89     protected void updateStatesForBaseFrameOption(FrameBaseOption frameBaseOption) {
90         updateState(CHANNEL_BASE_FRAME_BASE, QuantityType.valueOf(frameBaseOption.getBase(), Units.WATT_HOUR));
91     }
92
93     protected void updateStatesForHcFrameOption(FrameHcOption frameHcOption) {
94         updateState(CHANNEL_HC_FRAME_HCHC, QuantityType.valueOf(frameHcOption.getHchc(), Units.WATT_HOUR));
95         updateState(CHANNEL_HC_FRAME_HCHP, QuantityType.valueOf(frameHcOption.getHchp(), Units.WATT_HOUR));
96         updateState(CHANNEL_HC_FRAME_HHPHC, new StringType(frameHcOption.getHhphc().name()));
97     }
98
99     protected void updateStatesForTempoFrameOption(FrameTempoOption frameTempoOption) {
100         updateState(CHANNEL_TEMPO_FRAME_BBRHPJR, QuantityType.valueOf(frameTempoOption.getBbrhpjr(), Units.WATT_HOUR));
101         updateState(CHANNEL_TEMPO_FRAME_BBRHCJR, QuantityType.valueOf(frameTempoOption.getBbrhcjr(), Units.WATT_HOUR));
102         updateState(CHANNEL_TEMPO_FRAME_BBRHPJW, QuantityType.valueOf(frameTempoOption.getBbrhpjw(), Units.WATT_HOUR));
103         updateState(CHANNEL_TEMPO_FRAME_BBRHCJW, QuantityType.valueOf(frameTempoOption.getBbrhcjw(), Units.WATT_HOUR));
104         updateState(CHANNEL_TEMPO_FRAME_BBRHPJB, QuantityType.valueOf(frameTempoOption.getBbrhpjb(), Units.WATT_HOUR));
105         updateState(CHANNEL_TEMPO_FRAME_BBRHCJB, QuantityType.valueOf(frameTempoOption.getBbrhcjb(), Units.WATT_HOUR));
106         updateState(CHANNEL_TEMPO_FRAME_HHPHC, new StringType(frameTempoOption.getHhphc().name()));
107         updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_1,
108                 new StringType(frameTempoOption.getProgrammeCircuit1().name()));
109         updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_2,
110                 new StringType(frameTempoOption.getProgrammeCircuit2().name()));
111
112         if (frameTempoOption.getDemain() == null) {
113             updateState(CHANNEL_TEMPO_FRAME_DEMAIN, UnDefType.NULL);
114         } else {
115             updateState(CHANNEL_TEMPO_FRAME_DEMAIN, new StringType(frameTempoOption.getDemain().name()));
116         }
117     }
118
119     protected void updateStatesForEjpFrameOption(FrameEjpOption frameEjpOption) {
120         updateState(CHANNEL_EJP_FRAME_EJPHN, QuantityType.valueOf(frameEjpOption.getEjphn(), Units.WATT_HOUR));
121         updateState(CHANNEL_EJP_FRAME_EJPHPM, QuantityType.valueOf(frameEjpOption.getEjphpm(), Units.WATT_HOUR));
122
123         if (frameEjpOption.getPejp() == null) {
124             updateState(CHANNEL_EJP_FRAME_PEJP, UnDefType.NULL);
125         } else {
126             updateState(CHANNEL_EJP_FRAME_PEJP, QuantityType.valueOf(frameEjpOption.getPejp(), Units.MINUTE));
127         }
128     }
129
130     @Override
131     protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
132         super.updateStatus(status, statusDetail, description);
133
134         if (!(ThingStatus.ONLINE.equals(status))) {
135             for (Channel channel : getThing().getChannels()) {
136                 if (!CHANNEL_LAST_UPDATE.equals(channel.getUID().getId())) {
137                     updateState(channel.getUID(), UnDefType.UNDEF);
138                 }
139             }
140         }
141     }
142
143     @Override
144     protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
145         this.updateStatus(status, statusDetail, null);
146     }
147
148     @Override
149     protected void updateStatus(ThingStatus status) {
150         this.updateStatus(status, ThingStatusDetail.NONE, null);
151     }
152 }