]> git.basschouten.com Git - openhab-addons.git/blob
63afe7402c5e94d8c7875f46fc36913c1c72a1cc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.nhc2;
14
15 import java.util.concurrent.ScheduledExecutorService;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcEnergyMeter;
22 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
23
24 /**
25  * The {@link NhcEnergyMeter} class represents the energyMeters metering Niko Home Control communication object. It
26  * contains all fields representing a Niko Home Control energyMeters meter and has methods to receive energyMeters usage
27  * information.
28  *
29  * @author Mark Herwege - Initial Contribution
30  */
31 @NonNullByDefault
32 public class NhcEnergyMeter2 extends NhcEnergyMeter {
33
34     private ScheduledExecutorService scheduler;
35     private volatile @Nullable ScheduledFuture<?> restartTimer;
36
37     private String deviceType;
38     private String deviceTechnology;
39     private String deviceModel;
40
41     protected NhcEnergyMeter2(String id, String name, String deviceType, String deviceTechnology, String deviceModel,
42             @Nullable String location, NikoHomeControlCommunication nhcComm, ScheduledExecutorService scheduler) {
43         super(id, name, location, nhcComm);
44         this.deviceType = deviceType;
45         this.deviceTechnology = deviceTechnology;
46         this.deviceModel = deviceModel;
47
48         this.scheduler = scheduler;
49     }
50
51     /**
52      * Start the flow from energy information from the energy meter. The Niko Home Control energy meter will send power
53      * information every 2s for 30s. This method will retrigger every 25s to make sure the information continues
54      * flowing. If the information is no longer required, make sure to use the {@link stopEnergyMeter} method to stop
55      * the flow of information.
56      *
57      * @param topic topic the start event will have to be sent to every 25s
58      * @param gsonMessage content of message
59      */
60     public void startEnergyMeter(String topic, String gsonMessage) {
61         stopEnergyMeter();
62         restartTimer = scheduler.scheduleWithFixedDelay(() -> {
63             ((NikoHomeControlCommunication2) nhcComm).executeEnergyMeter(topic, gsonMessage);
64         }, 0, 25, TimeUnit.SECONDS);
65     }
66
67     /**
68      * Cancel receiving energy information from the controller. We therefore stop the automatic retriggering of the
69      * subscription, see {@link startEnergyMeter}.
70      */
71     public void stopEnergyMeter() {
72         ScheduledFuture<?> timer = restartTimer;
73         if (timer != null) {
74             timer.cancel(true);
75             restartTimer = null;
76         }
77     }
78
79     /**
80      * @return type as returned from Niko Home Control
81      */
82     public String getDeviceType() {
83         return deviceType;
84     }
85
86     /**
87      * @return technology as returned from Niko Home Control
88      */
89     public String getDeviceTechnology() {
90         return deviceTechnology;
91     }
92
93     /**
94      * @return model as returned from Niko Home Control
95      */
96     public String getDeviceModel() {
97         return deviceModel;
98     }
99 }