]> git.basschouten.com Git - openhab-addons.git/blob
002ceb4c378f5db6df402219b4d6da27093fbdc2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.internal.handler;
14
15 import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants.CHANNEL_AUX;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.types.Command;
28 import org.openwebnet4j.communication.OWNException;
29 import org.openwebnet4j.message.Auxiliary;
30 import org.openwebnet4j.message.BaseOpenMessage;
31 import org.openwebnet4j.message.MalformedFrameException;
32 import org.openwebnet4j.message.OpenMessage;
33 import org.openwebnet4j.message.UnsupportedFrameException;
34 import org.openwebnet4j.message.Where;
35 import org.openwebnet4j.message.WhereAuxiliary;
36 import org.openwebnet4j.message.Who;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link OpenWebNetAuxiliaryHandler} is responsible for sending Auxiliary (AUX) commands/messages to the bus
42  * It extends the abstract {@link OpenWebNetThingHandler}.
43  *
44  * NOTICE: Support for handling messages from the bus regarding alarm control has to be implemented
45  *
46  * @author Giovanni Fabiani - Initial contribution
47  *
48  */
49 @NonNullByDefault
50 public class OpenWebNetAuxiliaryHandler extends OpenWebNetThingHandler {
51
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.AUX_SUPPORTED_THING_TYPES;
53     private final Logger logger = LoggerFactory.getLogger(OpenWebNetAuxiliaryHandler.class);
54
55     public OpenWebNetAuxiliaryHandler(Thing thing) {
56         super(thing);
57     }
58
59     /**
60      * Handles Auxiliary command for a channel
61      *
62      * @param channel the channel
63      * @param command the Command
64      */
65     @Override
66     protected void handleChannelCommand(ChannelUID channel, Command command) {
67         logger.debug("handleAuxiliaryCommand() (command={} - channel={})", command, channel);
68         Where w = deviceWhere;
69         if (w != null) {
70             if (channel.getId().equals(CHANNEL_AUX)) {
71                 if (command instanceof StringType) {
72                     try {
73                         if (command.toString().equals(Auxiliary.WhatAuxiliary.ON.name())) {
74                             send(Auxiliary.requestTurnOn(w.value()));
75                         } else if (command.toString().equals(Auxiliary.WhatAuxiliary.OFF.name())) {
76                             send(Auxiliary.requestTurnOff(w.value()));
77                         }
78                     } catch (OWNException e) {
79                         logger.debug("Exception while processing command {}: {}", command, e.getMessage());
80                     }
81                 } else {
82                     logger.debug("Unsupported command {} for channel {}", command, channel);
83                 }
84             } else {
85                 logger.debug("Unsupported ChannelUID {}", channel);
86             }
87         }
88     }
89
90     @Override
91     protected void requestChannelState(ChannelUID channel) {
92         /*
93          * NOTICE: It is not possible to get the state of a
94          * WHO=9 command. To get state of the Alarm system use WHO=5 instead
95          */
96
97         super.requestChannelState(channel);
98         Where w = deviceWhere;
99         if (w != null) {
100             try {
101                 OpenMessage msg = BaseOpenMessage.parse("*#9##");
102                 // initializing
103                 send(msg);
104             } catch (MalformedFrameException | UnsupportedFrameException | OWNException e) {
105                 logger.debug("Exception while processing command: {}", e.getMessage());
106                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
107             }
108         }
109     }
110
111     @Override
112     protected void refreshDevice(boolean refreshAll) {
113         /*
114          * NOTICE: It is not possible to refresh the state of a
115          * WHO=9 command. To refresh the state of the Alarm system use WHO=5 instead
116          */
117
118         logger.debug("--- refreshDevice() : refreshing SINGLE... ({})", thing.getUID());
119         requestChannelState(new ChannelUID(thing.getUID(), CHANNEL_AUX));
120     }
121
122     @Override
123     protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
124         return new WhereAuxiliary(wStr);
125     }
126
127     @Override
128     protected String ownIdPrefix() {
129         return Who.AUX.value().toString();
130     }
131 }