2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nikohomecontrol.internal.protocol.nhc2;
15 import java.util.concurrent.ScheduledExecutorService;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
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;
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
29 * @author Mark Herwege - Initial Contribution
32 public class NhcEnergyMeter2 extends NhcEnergyMeter {
34 private ScheduledExecutorService scheduler;
35 private volatile @Nullable ScheduledFuture<?> restartTimer;
37 private String deviceType;
38 private String deviceTechnology;
39 private String deviceModel;
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;
48 this.scheduler = scheduler;
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.
57 * @param topic topic the start event will have to be sent to every 25s
58 * @param gsonMessage content of message
60 public void startEnergyMeter(String topic, String gsonMessage) {
62 restartTimer = scheduler.scheduleWithFixedDelay(() -> {
63 ((NikoHomeControlCommunication2) nhcComm).executeEnergyMeter(topic, gsonMessage);
64 }, 0, 25, TimeUnit.SECONDS);
68 * Cancel receiving energy information from the controller. We therefore stop the automatic retriggering of the
69 * subscription, see {@link startEnergyMeter}.
71 public void stopEnergyMeter() {
72 ScheduledFuture<?> timer = restartTimer;
80 * @return type as returned from Niko Home Control
82 public String getDeviceType() {
87 * @return technology as returned from Niko Home Control
89 public String getDeviceTechnology() {
90 return deviceTechnology;
94 * @return model as returned from Niko Home Control
96 public String getDeviceModel() {