2 * Copyright (c) 2010-2021 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.openwebnet.handler;
15 import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_POWER;
19 import javax.measure.quantity.Power;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.openwebnet.OpenWebNetBindingConstants;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.UnDefType;
30 import org.openwebnet4j.communication.OWNException;
31 import org.openwebnet4j.message.BaseOpenMessage;
32 import org.openwebnet4j.message.EnergyManagement;
33 import org.openwebnet4j.message.FrameException;
34 import org.openwebnet4j.message.Where;
35 import org.openwebnet4j.message.WhereEnergyManagement;
36 import org.openwebnet4j.message.Who;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link OpenWebNetEnergyHandler} is responsible for handling commands/messages for a Energy Management OpenWebNet
42 * device. It extends the abstract {@link OpenWebNetThingHandler}.
44 * @author Massimo Valla - Initial contribution
45 * @author Andrea Conte - Energy management
48 public class OpenWebNetEnergyHandler extends OpenWebNetThingHandler {
50 private final Logger logger = LoggerFactory.getLogger(OpenWebNetEnergyHandler.class);
52 public final static Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.ENERGY_MANAGEMENT_SUPPORTED_THING_TYPES;
54 public OpenWebNetEnergyHandler(Thing thing) {
59 protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
60 return new WhereEnergyManagement(wStr);
64 protected void requestChannelState(ChannelUID channel) {
65 logger.debug("requestChannelState() thingUID={} channel={}", thing.getUID(), channel.getId());
67 bridgeHandler.gateway.send(EnergyManagement.requestActivePower(deviceWhere.value()));
68 } catch (OWNException e) {
69 logger.warn("requestChannelState() OWNException thingUID={} channel={}: {}", thing.getUID(),
70 channel.getId(), e.getMessage());
75 protected void handleChannelCommand(ChannelUID channel, Command command) {
76 logger.warn("handleChannelCommand() Read only channel, unsupported command {}", command);
80 protected String ownIdPrefix() {
81 return Who.ENERGY_MANAGEMENT.value().toString();
85 protected void handleMessage(BaseOpenMessage msg) {
86 super.handleMessage(msg);
88 if (msg.isCommand()) {
89 logger.warn("handleMessage() Ignoring unsupported command for thing {}. Frame={}", getThing().getUID(),
93 updateActivePower(msg);
98 * Updates energy power state based on a EnergyManagement message received from the OWN network
100 * @param msg the EnergyManagement message received
101 * @throws FrameException
103 private void updateActivePower(BaseOpenMessage msg) {
106 activePower = Integer.parseInt(msg.getDimValues()[0]);
107 updateState(CHANNEL_POWER, new QuantityType<Power>(activePower, Units.WATT));
108 } catch (FrameException e) {
109 logger.warn("FrameException on frame {}: {}", msg, e.getMessage());
110 updateState(CHANNEL_POWER, UnDefType.UNDEF);