]> git.basschouten.com Git - openhab-addons.git/blob
c6dad086b0896149c4c1eb7282b5303582ab56d1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.nikohomecontrol.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NhcEnergyMeter2;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * The {@link NhcEnergyMeter} class represents the energyMeters metering Niko Home Control communication object. It
23  * contains all
24  * fields representing a Niko Home Control energyMeters meter and has methods to receive energyMeters usage information.
25  * A specific
26  * implementation is {@link NhcEnergyMeter2}.
27  *
28  * @author Mark Herwege - Initial Contribution
29  */
30 @NonNullByDefault
31 public abstract class NhcEnergyMeter {
32
33     private final Logger logger = LoggerFactory.getLogger(NhcEnergyMeter.class);
34
35     protected NikoHomeControlCommunication nhcComm;
36
37     protected String id;
38     protected String name;
39     // This can be null as long as we do not receive power readings
40     protected volatile @Nullable Integer power = null;
41
42     private @Nullable NhcEnergyMeterEvent eventHandler;
43
44     protected NhcEnergyMeter(String id, String name, NikoHomeControlCommunication nhcComm) {
45         this.id = id;
46         this.name = name;
47         this.nhcComm = nhcComm;
48     }
49
50     /**
51      * Update all values of the energyMeters meter without touching the energyMeters meter definition (id, name) and
52      * without changing the ThingHandler callback.
53      *
54      * @param power current power consumption/production in W (positive for consumption)
55      */
56     public void updateState(int power) {
57         NhcEnergyMeterEvent handler = eventHandler;
58         if (handler != null) {
59             logger.debug("update channel for {}", id);
60             handler.energyMeterEvent(power);
61         }
62     }
63
64     /**
65      * Method called when energyMeters meter is removed from the Niko Home Control Controller.
66      */
67     public void energyMeterRemoved() {
68         logger.debug("action removed {}, {}", id, name);
69         NhcEnergyMeterEvent eventHandler = this.eventHandler;
70         if (eventHandler != null) {
71             eventHandler.energyMeterRemoved();
72         }
73     }
74
75     /**
76      * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is initialized.
77      * It keeps a record of the event handler in that object so it can be updated when the action receives an update
78      * from the Niko Home Control IP-interface.
79      *
80      * @param eventHandler
81      */
82     public void setEventHandler(NhcEnergyMeterEvent eventHandler) {
83         this.eventHandler = eventHandler;
84     }
85
86     /**
87      * Get the id of the energyMeters meter.
88      *
89      * @return the id
90      */
91     public String getId() {
92         return id;
93     }
94
95     /**
96      * Get name of the energyMeters meter.
97      *
98      * @return energyMeters meter name
99      */
100     public String getName() {
101         return name;
102     }
103
104     /**
105      * @return the power in W (positive for consumption, negative for production), return null if no reading received
106      *         yet
107      */
108     public @Nullable Integer getPower() {
109         return power;
110     }
111
112     /**
113      * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
114      *            was received
115      */
116     public void setPower(@Nullable Integer power) {
117         this.power = power;
118         NhcEnergyMeterEvent handler = eventHandler;
119         if (handler != null) {
120             logger.debug("update power channel for {} with {}", id, power);
121             handler.energyMeterEvent(power);
122         }
123     }
124 }