2 * Copyright (c) 2010-2020 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.caddx.internal.handler;
15 import java.util.Collection;
16 import java.util.Collections;
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;
35 * This is a class for handling a Keypad type Thing.
37 * @author Georgios Moutsos - Initial contribution
40 public class ThingHandlerKeypad extends CaddxBaseThingHandler {
41 private final Logger logger = LoggerFactory.getLogger(ThingHandlerKeypad.class);
43 public ThingHandlerKeypad(Thing thing) {
44 super(thing, CaddxThingType.KEYPAD);
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);
56 public void caddxEventReceived(CaddxEvent event, Thing thing) {
57 logger.trace("caddxEventReceived(): Event Received - {}.", event);
59 if (getThing().equals(thing)) {
60 CaddxMessage message = event.getCaddxMessage();
61 CaddxMessageType mt = message.getCaddxMessageType();
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);
74 updateStatus(ThingStatus.ONLINE);
79 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
80 // Keypad follows the status of the bridge
81 updateStatus(bridgeStatusInfo.getStatus());
83 super.bridgeStatusChanged(bridgeStatusInfo);
87 public Collection<Class<? extends ThingHandlerService>> getServices() {
88 return Collections.singleton(CaddxKeypadActions.class);
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());
96 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
97 if (bridgeHandler == null) {
100 bridgeHandler.sendCommand(cmd, data);
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");
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));
113 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
114 if (bridgeHandler == null) {
117 bridgeHandler.sendCommand(cmd, data);