]> git.basschouten.com Git - openhab-addons.git/blob
83929106c77438c585b2ec1e6992dd7b1dea65f5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 fields representing a Niko Home Control energyMeters meter and has methods to receive energyMeters usage
24  * information. A specific implementation is {@link NhcEnergyMeter2}.
25  *
26  * @author Mark Herwege - Initial Contribution
27  */
28 @NonNullByDefault
29 public abstract class NhcEnergyMeter {
30
31     private final Logger logger = LoggerFactory.getLogger(NhcEnergyMeter.class);
32
33     protected NikoHomeControlCommunication nhcComm;
34
35     protected String id;
36     protected String name;
37     protected @Nullable String location;
38     // This can be null as long as we do not receive power readings
39     protected volatile @Nullable Integer power = null;
40
41     private @Nullable NhcEnergyMeterEvent eventHandler;
42
43     protected NhcEnergyMeter(String id, String name, @Nullable String location, NikoHomeControlCommunication nhcComm) {
44         this.id = id;
45         this.name = name;
46         this.location = location;
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             unsetEventHandler();
73         }
74     }
75
76     /**
77      * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is initialized.
78      * It keeps a record of the event handler in that object so it can be updated when the action receives an update
79      * from the Niko Home Control IP-interface.
80      *
81      * @param eventHandler
82      */
83     public void setEventHandler(NhcEnergyMeterEvent eventHandler) {
84         this.eventHandler = eventHandler;
85     }
86
87     /**
88      * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is disposed.
89      * It resets the reference, so no updates go to the handler anymore.
90      *
91      */
92     public void unsetEventHandler() {
93         this.eventHandler = null;
94     }
95
96     /**
97      * Get id of meter.
98      *
99      * @return id
100      */
101     public String getId() {
102         return id;
103     }
104
105     /**
106      * Get name of meter.
107      *
108      * @return energyMeter name
109      */
110     public String getName() {
111         return name;
112     }
113
114     /**
115      * Set name of meter.
116      *
117      * @param name meter name
118      */
119     public void setName(String name) {
120         this.name = name;
121     }
122
123     /**
124      * Get location name of meter.
125      *
126      * @return location energyMeter location
127      */
128     public @Nullable String getLocation() {
129         return location;
130     }
131
132     /**
133      * Set location name of meter.
134      *
135      * @param location meter location name
136      */
137     public void setLocation(@Nullable String location) {
138         this.location = location;
139     }
140
141     /**
142      * @return the power in W (positive for consumption, negative for production), return null if no reading received
143      *         yet
144      */
145     public @Nullable Integer getPower() {
146         return power;
147     }
148
149     /**
150      * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
151      *            was received
152      */
153     public void setPower(@Nullable Integer power) {
154         this.power = power;
155         NhcEnergyMeterEvent handler = eventHandler;
156         if (handler != null) {
157             logger.debug("update power channel for {} with {}", id, power);
158             handler.energyMeterEvent(power);
159         }
160     }
161 }