]> git.basschouten.com Git - openhab-addons.git/blob
46f12673e8eae2d6ed5725a74409638244300ab6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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     protected @Nullable String location;
40     // This can be null as long as we do not receive power readings
41     protected volatile @Nullable Integer power = null;
42
43     private @Nullable NhcEnergyMeterEvent eventHandler;
44
45     protected NhcEnergyMeter(String id, String name, @Nullable String location, NikoHomeControlCommunication nhcComm) {
46         this.id = id;
47         this.name = name;
48         this.location = location;
49         this.nhcComm = nhcComm;
50     }
51
52     /**
53      * Update all values of the energyMeters meter without touching the energyMeters meter definition (id, name) and
54      * without changing the ThingHandler callback.
55      *
56      * @param power current power consumption/production in W (positive for consumption)
57      */
58     public void updateState(int power) {
59         NhcEnergyMeterEvent handler = eventHandler;
60         if (handler != null) {
61             logger.debug("update channel for {}", id);
62             handler.energyMeterEvent(power);
63         }
64     }
65
66     /**
67      * Method called when energyMeters meter is removed from the Niko Home Control Controller.
68      */
69     public void energyMeterRemoved() {
70         logger.debug("action removed {}, {}", id, name);
71         NhcEnergyMeterEvent eventHandler = this.eventHandler;
72         if (eventHandler != null) {
73             eventHandler.energyMeterRemoved();
74             unsetEventHandler();
75         }
76     }
77
78     /**
79      * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is initialized.
80      * It keeps a record of the event handler in that object so it can be updated when the action receives an update
81      * from the Niko Home Control IP-interface.
82      *
83      * @param eventHandler
84      */
85     public void setEventHandler(NhcEnergyMeterEvent eventHandler) {
86         this.eventHandler = eventHandler;
87     }
88
89     /**
90      * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is disposed.
91      * It resets the reference, so no updates go to the handler anymore.
92      *
93      */
94     public void unsetEventHandler() {
95         this.eventHandler = null;
96     }
97
98     /**
99      * Get id of meter.
100      *
101      * @return id
102      */
103     public String getId() {
104         return id;
105     }
106
107     /**
108      * Get name of meter.
109      *
110      * @return energyMeter name
111      */
112     public String getName() {
113         return name;
114     }
115
116     /**
117      * Set name of meter.
118      *
119      * @param name meter name
120      */
121     public void setName(String name) {
122         this.name = name;
123     }
124
125     /**
126      * Get location name of meter.
127      *
128      * @return location energyMeter location
129      */
130     public @Nullable String getLocation() {
131         return location;
132     }
133
134     /**
135      * Set location name of meter.
136      *
137      * @param location meter location name
138      */
139     public void setLocation(@Nullable String location) {
140         this.location = location;
141     }
142
143     /**
144      * @return the power in W (positive for consumption, negative for production), return null if no reading received
145      *         yet
146      */
147     public @Nullable Integer getPower() {
148         return power;
149     }
150
151     /**
152      * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
153      *            was received
154      */
155     public void setPower(@Nullable Integer power) {
156         this.power = power;
157         NhcEnergyMeterEvent handler = eventHandler;
158         if (handler != null) {
159             logger.debug("update power channel for {} with {}", id, power);
160             handler.energyMeterEvent(power);
161         }
162     }
163 }