]> git.basschouten.com Git - openhab-addons.git/blob
69f93cfd8f345cc947d89ec9530da0378c56ffc5
[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.caddx.internal.handler;
14
15 import java.util.Collection;
16 import java.util.Collections;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.caddx.internal.CaddxBindingConstants;
20 import org.openhab.binding.caddx.internal.CaddxEvent;
21 import org.openhab.binding.caddx.internal.CaddxMessage;
22 import org.openhab.binding.caddx.internal.CaddxMessageType;
23 import org.openhab.binding.caddx.internal.CaddxProperty;
24 import org.openhab.binding.caddx.internal.action.CaddxKeypadActions;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusInfo;
30 import org.openhab.core.thing.binding.ThingHandlerService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This is a class for handling a Keypad type Thing.
36  *
37  * @author Georgios Moutsos - Initial contribution
38  */
39 @NonNullByDefault
40 public class ThingHandlerKeypad extends CaddxBaseThingHandler {
41     private final Logger logger = LoggerFactory.getLogger(ThingHandlerKeypad.class);
42
43     public ThingHandlerKeypad(Thing thing) {
44         super(thing, CaddxThingType.KEYPAD);
45     }
46
47     @Override
48     public void updateChannel(ChannelUID channelUID, String data) {
49         if (channelUID.getId().equals(CaddxBindingConstants.KEYPAD_KEY_PRESSED)) {
50             StringType stringType = new StringType(data);
51             updateState(channelUID, stringType);
52         }
53     }
54
55     @Override
56     public void caddxEventReceived(CaddxEvent event, Thing thing) {
57         logger.trace("caddxEventReceived(): Event Received - {}.", event);
58
59         if (getThing().equals(thing)) {
60             CaddxMessage message = event.getCaddxMessage();
61             CaddxMessageType mt = message.getCaddxMessageType();
62
63             // Log event messages have special handling
64             if (CaddxMessageType.KEYPAD_MESSAGE_RECEIVED.equals(mt)) {
65                 for (CaddxProperty p : mt.properties) {
66                     if (!("".equals(p.getId()))) {
67                         String value = message.getPropertyById(p.getId());
68                         ChannelUID channelUID = new ChannelUID(getThing().getUID(), p.getId());
69                         updateChannel(channelUID, value);
70                     }
71                 }
72             }
73
74             updateStatus(ThingStatus.ONLINE);
75         }
76     }
77
78     @Override
79     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
80         // Keypad follows the status of the bridge
81         updateStatus(bridgeStatusInfo.getStatus());
82
83         super.bridgeStatusChanged(bridgeStatusInfo);
84     }
85
86     @Override
87     public Collection<Class<? extends ThingHandlerService>> getServices() {
88         return Collections.singleton(CaddxKeypadActions.class);
89     }
90
91     public void enterTerminalMode() {
92         String cmd = CaddxBindingConstants.KEYPAD_TERMINAL_MODE_REQUEST;
93         logger.debug("Address: {}, Seconds: {}", getKeypadAddress(), getTerminalModeSeconds());
94         String data = String.format("%d,15", getKeypadAddress(), getTerminalModeSeconds());
95
96         CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
97         if (bridgeHandler == null) {
98             return;
99         }
100         bridgeHandler.sendCommand(cmd, data);
101     }
102
103     public void sendKeypadTextMessage(String displayLocation, String text) {
104         if (text.length() != 8) {
105             logger.debug("Text to be displayed on the keypad has not the correct length");
106             return;
107         }
108         String cmd = CaddxBindingConstants.KEYPAD_SEND_KEYPAD_TEXT_MESSAGE;
109         String data = String.format("%d,0,%d,%d,%d,%d,%d,%d,%d,%d,%d", getKeypadAddress(), displayLocation,
110                 text.charAt(0), text.charAt(1), text.charAt(2), text.charAt(3), text.charAt(4), text.charAt(5),
111                 text.charAt(6), text.charAt(7));
112
113         CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
114         if (bridgeHandler == null) {
115             return;
116         }
117         bridgeHandler.sendCommand(cmd, data);
118     }
119 }