2 * Copyright (c) 2010-2023 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.luxom.internal.handler;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
20 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.ThingStatusInfo;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Base type for all Luxom thing handlers.
33 * @author Kris Jespers - Initial contribution
36 public abstract class LuxomThingHandler extends BaseThingHandler {
37 private final Logger logger = LoggerFactory.getLogger(LuxomThingHandler.class);
39 private String address = "";
42 public void initialize() {
43 String id = (String) getConfig().get("address");
45 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
46 "@text/status.thing-address-missing");
47 address = "noaddress";
53 public LuxomThingHandler(Thing thing) {
57 public abstract void handleCommandComingFromBridge(LuxomCommand command);
59 public final String getAddress() {
64 * Queries for any device state needed at initialization time or after losing connectivity to the bridge, and
65 * updates device status. Will be called when bridge status changes to ONLINE and thing has status
66 * OFFLINE:BRIDGE_OFFLINE.
68 protected abstract void initDeviceState();
71 * Called when changing thing status to offline. Subclasses may override to take any needed actions.
73 protected void thingOfflineNotify() {
76 protected @Nullable LuxomBridgeHandler getBridgeHandler() {
77 Bridge bridge = getBridge();
79 return bridge == null ? null : (LuxomBridgeHandler) bridge.getHandler();
83 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
84 logger.debug("Bridge status changed to {} for luxom device handler {}", bridgeStatusInfo.getStatus(),
87 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
88 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
91 } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
92 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
97 protected void sendCommands(List<CommandExecutionSpecification> commands) {
99 LuxomBridgeHandler bridgeHandler = getBridgeHandler();
101 if (bridgeHandler == null) {
102 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR,
103 "@text/status.bridge-handler-missing");
104 thingOfflineNotify();
106 bridgeHandler.sendCommands(commands);
111 * example : *P,0,1,21;
113 protected void ping() {
114 sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.PING.getCommand() + ",0," + getAddress())));
118 * example : *S,0,1,21;
120 protected void set() {
121 sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress())));
125 * example : *C,0,1,21;
127 protected void clear() {
128 sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress())));