]> git.basschouten.com Git - openhab-addons.git/blob
f96019e71d8f1a6eff12b2635c4db0c503b2f5fc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 model;
38     private String technology;
39
40     protected NhcEnergyMeter2(String id, String name, String model, String technology,
41             NikoHomeControlCommunication nhcComm, ScheduledExecutorService scheduler) {
42         super(id, name, nhcComm);
43         this.model = model;
44         this.technology = technology;
45
46         this.scheduler = scheduler;
47     }
48
49     /**
50      * Start the flow from energy information from the energy meter. The Niko Home Control energy meter will send power
51      * information every 2s for 30s. This method will retrigger every 25s to make sure the information continues
52      * flowing. If the information is no longer required, make sure to use the {@link stopEnergyMeter} method to stop
53      * the flow of information.
54      *
55      * @param topic topic the start event will have to be sent to every 25s
56      * @param gsonMessage content of message
57      */
58     public void startEnergyMeter(String topic, String gsonMessage) {
59         stopEnergyMeter();
60         restartTimer = scheduler.scheduleWithFixedDelay(() -> {
61             ((NikoHomeControlCommunication2) nhcComm).executeEnergyMeter(topic, gsonMessage);
62         }, 0, 25, TimeUnit.SECONDS);
63     }
64
65     /**
66      * Cancel receiving energy information from the controller. We therefore stop the automatic retriggering of the
67      * subscription, see {@link startEnergyMeter}.
68      */
69     public void stopEnergyMeter() {
70         ScheduledFuture<?> timer = restartTimer;
71         if (timer != null) {
72             timer.cancel(true);
73             restartTimer = null;
74         }
75     }
76
77     /**
78      * @return model as returned from Niko Home Control
79      */
80     public String getModel() {
81         return model;
82     }
83
84     /**
85      * @return technology as returned from Niko Home Control
86      */
87     public String getTechnology() {
88         return technology;
89     }
90 }