]> git.basschouten.com Git - openhab-addons.git/blob
89cacbf69504395495db313b4deb97d2654bc9db
[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         }
75     }
76
77     /**
78      * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is 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      * Get id of meter.
90      *
91      * @return id
92      */
93     public String getId() {
94         return id;
95     }
96
97     /**
98      * Get name of meter.
99      *
100      * @return energyMeter name
101      */
102     public String getName() {
103         return name;
104     }
105
106     /**
107      * Set name of meter.
108      *
109      * @param name meter name
110      */
111     public void setName(String name) {
112         this.name = name;
113     }
114
115     /**
116      * Get location name of meter.
117      *
118      * @return location energyMeter location
119      */
120     public @Nullable String getLocation() {
121         return location;
122     }
123
124     /**
125      * Set location name of meter.
126      *
127      * @param location meter location name
128      */
129     public void setLocation(@Nullable String location) {
130         this.location = location;
131     }
132
133     /**
134      * @return the power in W (positive for consumption, negative for production), return null if no reading received
135      *         yet
136      */
137     public @Nullable Integer getPower() {
138         return power;
139     }
140
141     /**
142      * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
143      *            was received
144      */
145     public void setPower(@Nullable Integer power) {
146         this.power = power;
147         NhcEnergyMeterEvent handler = eventHandler;
148         if (handler != null) {
149             logger.debug("update power channel for {} with {}", id, power);
150             handler.energyMeterEvent(power);
151         }
152     }
153 }