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.upb.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.upb.internal.Constants;
18 import org.openhab.binding.upb.internal.UPBController;
19 import org.openhab.binding.upb.internal.UPBDevice;
20 import org.openhab.binding.upb.internal.message.UPBMessage;
21 import org.openhab.core.thing.Bridge;
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.binding.BaseBridgeHandler;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Base class for Powerline Interface Module handlers.
35 * @author Marcus Better - Initial contribution
39 public abstract class PIMHandler extends BaseBridgeHandler implements MessageListener, UPBIoHandler {
41 private final Logger logger = LoggerFactory.getLogger(PIMHandler.class);
43 // volatile to ensure visibility for callbacks from the serial I/O thread
44 private volatile UPBController controller = new UPBController();
46 public PIMHandler(final Bridge bridge) {
51 public void initialize() {
52 logger.debug("Initializing UPB PIM {}.", getThing().getUID());
53 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, Constants.OFFLINE_CTLR_OFFLINE);
54 controller = new UPBController();
58 public void dispose() {
59 logger.debug("UPB binding shutting down...");
64 public void handleCommand(final ChannelUID channelUID, final Command command) {
68 public void childHandlerInitialized(final ThingHandler childHandler, final Thing childThing) {
69 logger.debug("child handler initialized: {}", childThing.getUID());
70 controller.deviceAdded(childHandler, childThing);
71 super.childHandlerInitialized(childHandler, childThing);
75 public void childHandlerDisposed(final ThingHandler childHandler, final Thing childThing) {
76 logger.debug("child handler disposed: {}", childThing.getUID());
77 controller.deviceRemoved(childHandler, childThing);
78 super.childHandlerDisposed(childHandler, childThing);
82 public void incomingMessage(final UPBMessage msg) {
83 updateStatus(ThingStatus.ONLINE);
84 controller.incomingMessage(msg);
88 public void onError(final Throwable t) {
89 // Currently all PIM errors are unrecoverable, either a bug or
90 // the serial thread had an I/O error.
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, Constants.OFFLINE_COMM_ERROR);
94 public @Nullable UPBDevice getDevice(byte networkId, byte unitId) {
95 return controller.getDevice(networkId, unitId);