]> git.basschouten.com Git - openhab-addons.git/blob
b847d5eeda023bbaca3e5c402008d6de62016fb6
[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.openwebnet.handler;
14
15 import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_POWER;
16
17 import java.util.Set;
18
19 import javax.measure.quantity.Power;
20
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;
39
40 /**
41  * The {@link OpenWebNetEnergyHandler} is responsible for handling commands/messages for a Energy Management OpenWebNet
42  * device. It extends the abstract {@link OpenWebNetThingHandler}.
43  *
44  * @author Massimo Valla - Initial contribution
45  * @author Andrea Conte - Energy management
46  */
47 @NonNullByDefault
48 public class OpenWebNetEnergyHandler extends OpenWebNetThingHandler {
49
50     private final Logger logger = LoggerFactory.getLogger(OpenWebNetEnergyHandler.class);
51
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.ENERGY_MANAGEMENT_SUPPORTED_THING_TYPES;
53
54     public OpenWebNetEnergyHandler(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
60         return new WhereEnergyManagement(wStr);
61     }
62
63     @Override
64     protected void requestChannelState(ChannelUID channel) {
65         logger.debug("requestChannelState() thingUID={} channel={}", thing.getUID(), channel.getId());
66         Where w = deviceWhere;
67         if (w != null) {
68             try {
69                 send(EnergyManagement.requestActivePower(w.value()));
70             } catch (OWNException e) {
71                 logger.debug("Exception while requesting channel {} state: {}", channel, e.getMessage(), e);
72             }
73         } else {
74             logger.warn("Could not requestChannelState(): deviceWhere is null");
75         }
76     }
77
78     @Override
79     protected void refreshDevice(boolean refreshAll) {
80         requestChannelState(new ChannelUID("any:any:any:any"));
81     }
82
83     @Override
84     protected void handleChannelCommand(ChannelUID channel, Command command) {
85         logger.warn("handleChannelCommand() Read only channel, unsupported command {}", command);
86     }
87
88     @Override
89     protected String ownIdPrefix() {
90         return Who.ENERGY_MANAGEMENT.value().toString();
91     }
92
93     @Override
94     protected void handleMessage(BaseOpenMessage msg) {
95         super.handleMessage(msg);
96
97         if (msg.isCommand()) {
98             logger.warn("handleMessage() Ignoring unsupported command for thing {}. Frame={}", getThing().getUID(),
99                     msg);
100             return;
101         } else {
102             updateActivePower(msg);
103         }
104     }
105
106     /**
107      * Updates energy power state based on a EnergyManagement message received from the OWN network
108      *
109      * @param msg the EnergyManagement message received
110      * @throws FrameException
111      */
112     private void updateActivePower(BaseOpenMessage msg) {
113         Integer activePower;
114         try {
115             activePower = Integer.parseInt(msg.getDimValues()[0]);
116             updateState(CHANNEL_POWER, new QuantityType<Power>(activePower, Units.WATT));
117         } catch (FrameException e) {
118             logger.warn("FrameException on frame {}: {}", msg, e.getMessage());
119             updateState(CHANNEL_POWER, UnDefType.UNDEF);
120         }
121     }
122 }