mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[openwebnet] Energy meter returns always zero power if no other device has subscribed to power measures (#10565)
* add scheduler to start/stop sending instantaneous consumption Signed-off-by: Conte Andrea <andrea@conte.com> * added scheduler also in Initialize() in order to start reading data of a newly discovered thing Signed-off-by: Conte Andrea <andrea@conte.com> * removed setActivePowerNotificationsTime() from dispose() Signed-off-by: Conte Andrea <andrea@conte.com> * add log in dispose() method Signed-off-by: Conte Andrea <andrea@conte.com>
This commit is contained in:
parent
a1a990989e
commit
18497a9436
@ -15,21 +15,28 @@ package org.openhab.binding.openwebnet.handler;
|
|||||||
import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_POWER;
|
import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_POWER;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.measure.quantity.Power;
|
import javax.measure.quantity.Power;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.binding.openwebnet.OpenWebNetBindingConstants;
|
import org.openhab.binding.openwebnet.OpenWebNetBindingConstants;
|
||||||
import org.openhab.core.library.types.QuantityType;
|
import org.openhab.core.library.types.QuantityType;
|
||||||
import org.openhab.core.library.unit.Units;
|
import org.openhab.core.library.unit.Units;
|
||||||
import org.openhab.core.thing.ChannelUID;
|
import org.openhab.core.thing.ChannelUID;
|
||||||
import org.openhab.core.thing.Thing;
|
import org.openhab.core.thing.Thing;
|
||||||
|
import org.openhab.core.thing.ThingStatus;
|
||||||
|
import org.openhab.core.thing.ThingStatusInfo;
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
import org.openhab.core.thing.ThingTypeUID;
|
||||||
import org.openhab.core.types.Command;
|
import org.openhab.core.types.Command;
|
||||||
import org.openhab.core.types.UnDefType;
|
import org.openhab.core.types.UnDefType;
|
||||||
|
import org.openwebnet4j.OpenGateway;
|
||||||
import org.openwebnet4j.communication.OWNException;
|
import org.openwebnet4j.communication.OWNException;
|
||||||
import org.openwebnet4j.message.BaseOpenMessage;
|
import org.openwebnet4j.message.BaseOpenMessage;
|
||||||
import org.openwebnet4j.message.EnergyManagement;
|
import org.openwebnet4j.message.EnergyManagement;
|
||||||
|
import org.openwebnet4j.message.EnergyManagement.DIM;
|
||||||
import org.openwebnet4j.message.FrameException;
|
import org.openwebnet4j.message.FrameException;
|
||||||
import org.openwebnet4j.message.Where;
|
import org.openwebnet4j.message.Where;
|
||||||
import org.openwebnet4j.message.WhereEnergyManagement;
|
import org.openwebnet4j.message.WhereEnergyManagement;
|
||||||
@ -49,12 +56,87 @@ public class OpenWebNetEnergyHandler extends OpenWebNetThingHandler {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(OpenWebNetEnergyHandler.class);
|
private final Logger logger = LoggerFactory.getLogger(OpenWebNetEnergyHandler.class);
|
||||||
|
|
||||||
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.ENERGY_MANAGEMENT_SUPPORTED_THING_TYPES;
|
public final static Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.ENERGY_MANAGEMENT_SUPPORTED_THING_TYPES;
|
||||||
|
public final int ENERGY_SUBSCRIPTION_PERIOD = 10; // minutes
|
||||||
|
private @Nullable ScheduledFuture<?> notificationSchedule;
|
||||||
|
|
||||||
public OpenWebNetEnergyHandler(Thing thing) {
|
public OpenWebNetEnergyHandler(Thing thing) {
|
||||||
super(thing);
|
super(thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean isFirstSchedulerLaunch = true;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize() {
|
||||||
|
super.initialize();
|
||||||
|
|
||||||
|
// In order to get data from the probe we must send a command over the bus, this could be done only when the
|
||||||
|
// bridge is online.
|
||||||
|
// a) usual flow: binding is starting, the bridge isn't online (startup flow not yet completed) --> subscriber
|
||||||
|
// can't be started here, it will be started inside the bridgeStatusChanged event.
|
||||||
|
// b) thing's discovery: binding is up and running, the bridge is online --> subscriber must be started here
|
||||||
|
// otherwise data will be read only after a reboot.
|
||||||
|
|
||||||
|
OpenWebNetBridgeHandler h = bridgeHandler;
|
||||||
|
if (h != null && h.isBusGateway()) {
|
||||||
|
OpenGateway gw = h.gateway;
|
||||||
|
if (gw != null && gw.isConnected()) {
|
||||||
|
// bridge is online
|
||||||
|
subscribeToActivePowerChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
|
||||||
|
super.bridgeStatusChanged(bridgeStatusInfo);
|
||||||
|
|
||||||
|
// subscribe the scheduler only after the bridge is online
|
||||||
|
if (bridgeStatusInfo.getStatus().equals(ThingStatus.ONLINE)) {
|
||||||
|
subscribeToActivePowerChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribeToActivePowerChanges() {
|
||||||
|
notificationSchedule = scheduler.scheduleAtFixedRate(() -> {
|
||||||
|
if (isFirstSchedulerLaunch) {
|
||||||
|
logger.debug(
|
||||||
|
"subscribeToActivePowerChanges() For WHERE={} subscribing to active power changes notification for the next {}min",
|
||||||
|
deviceWhere, ENERGY_SUBSCRIPTION_PERIOD);
|
||||||
|
} else {
|
||||||
|
logger.debug(
|
||||||
|
"subscribeToActivePowerChanges() Refreshing subscription for the next {}min for WHERE={} to active power changes notification",
|
||||||
|
ENERGY_SUBSCRIPTION_PERIOD, deviceWhere);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
bridgeHandler.gateway.send(EnergyManagement.setActivePowerNotificationsTime(deviceWhere.value(),
|
||||||
|
ENERGY_SUBSCRIPTION_PERIOD));
|
||||||
|
isFirstSchedulerLaunch = false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (isFirstSchedulerLaunch) {
|
||||||
|
logger.warn(
|
||||||
|
"subscribeToActivePowerChanges() For WHERE={} could not subscribe to active power changes notifications. Exception={}",
|
||||||
|
deviceWhere, e.getMessage());
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
"subscribeToActivePowerChanges() Unable to refresh subscription to active power changes notifications for WHERE={}. Exception={}",
|
||||||
|
deviceWhere, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 0, ENERGY_SUBSCRIPTION_PERIOD - 1, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
if (notificationSchedule != null) {
|
||||||
|
logger.debug("dispose() scheduler stopped.");
|
||||||
|
|
||||||
|
notificationSchedule.cancel(false);
|
||||||
|
}
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
|
protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
|
||||||
return new WhereEnergyManagement(wStr);
|
return new WhereEnergyManagement(wStr);
|
||||||
@ -99,7 +181,12 @@ public class OpenWebNetEnergyHandler extends OpenWebNetThingHandler {
|
|||||||
msg);
|
msg);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
updateActivePower(msg);
|
// fix: check for correct DIM (ActivePower / 113)
|
||||||
|
if (msg.getDim().equals(DIM.ACTIVE_POWER))
|
||||||
|
updateActivePower(msg);
|
||||||
|
else
|
||||||
|
logger.debug("handleMessage() Ignoring message {} because it's not related to active power value.",
|
||||||
|
msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user