]> git.basschouten.com Git - openhab-addons.git/blob
735e338e162f6651a5d42fce16741c38a4602dde
[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 {@link NhcEnergyMeterEvent} interface is
78      * initialized.
79      * It keeps a record of the event handler in that object so it can be updated when the action receives an update
80      * from the Niko Home Control IP-interface.
81      *
82      * @param eventHandler
83      */
84     public void setEventHandler(NhcEnergyMeterEvent eventHandler) {
85         this.eventHandler = eventHandler;
86     }
87
88     /**
89      * This method should be called when an object implementing the {@link NhcEnergyMeterEvent} interface is disposed.
90      * It resets the reference, so no updates go to the handler anymore.
91      *
92      */
93     public void unsetEventHandler() {
94         this.eventHandler = null;
95     }
96
97     /**
98      * Get id of meter.
99      *
100      * @return id
101      */
102     public String getId() {
103         return id;
104     }
105
106     /**
107      * Get name of meter.
108      *
109      * @return energyMeter name
110      */
111     public String getName() {
112         return name;
113     }
114
115     /**
116      * Set name of meter.
117      *
118      * @param name meter name
119      */
120     public void setName(String name) {
121         this.name = name;
122     }
123
124     /**
125      * Get location name of meter.
126      *
127      * @return location energyMeter location
128      */
129     public @Nullable String getLocation() {
130         return location;
131     }
132
133     /**
134      * Set location name of meter.
135      *
136      * @param location meter location name
137      */
138     public void setLocation(@Nullable String location) {
139         this.location = location;
140     }
141
142     /**
143      * @return the power in W (positive for consumption, negative for production), return null if no reading received
144      *         yet
145      */
146     public @Nullable Integer getPower() {
147         return power;
148     }
149
150     /**
151      * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
152      *            was received
153      */
154     public void setPower(@Nullable Integer power) {
155         this.power = power;
156         NhcEnergyMeterEvent handler = eventHandler;
157         if (handler != null) {
158             logger.debug("update power channel for {} with {}", id, power);
159             handler.energyMeterEvent(power);
160         }
161     }
162 }