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.lametrictime.internal.handler;
15 import java.util.SortedMap;
17 import org.openhab.binding.lametrictime.api.LaMetricTime;
18 import org.openhab.binding.lametrictime.api.local.ApplicationNotFoundException;
19 import org.openhab.binding.lametrictime.api.local.model.Application;
20 import org.openhab.binding.lametrictime.api.local.model.Widget;
21 import org.openhab.binding.lametrictime.internal.WidgetRef;
22 import org.openhab.binding.lametrictime.internal.config.LaMetricTimeAppConfiguration;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.ThingStatusInfo;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link AbstractLaMetricTimeAppHandler} is the parent of all app handlers for
38 * the LaMetric Time device.
40 * @author Gregory Moyer - Initial contribution
42 public abstract class AbstractLaMetricTimeAppHandler extends BaseThingHandler implements LaMetricTimeAppHandler {
44 private final Logger logger = LoggerFactory.getLogger(AbstractLaMetricTimeAppHandler.class);
46 private Widget widget;
48 public AbstractLaMetricTimeAppHandler(Thing thing) {
53 public void initialize() {
54 logger.debug("Verifying LaMetric Time device configuration");
56 Bridge bridge = getBridge();
58 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
59 "No device bridge has been configured");
63 if (ThingStatus.ONLINE != bridge.getStatus()) {
64 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
68 updateWidget(bridge.getHandler());
69 updateStatus(ThingStatus.ONLINE);
73 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
74 super.bridgeStatusChanged(bridgeStatusInfo);
76 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
77 updateWidget(getBridge().getHandler());
81 private void updateWidget(ThingHandler handler) {
82 if (!(handler instanceof LaMetricTimeHandler)) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Incorrect bridge thing found");
86 LaMetricTimeHandler deviceHandler = (LaMetricTimeHandler) handler;
88 logger.debug("Reading LaMetric Time app thing configuration");
89 LaMetricTimeAppConfiguration config = getConfigAs(LaMetricTimeAppConfiguration.class);
90 String packageName = getPackageName(config);
92 Application app = deviceHandler.getClock().getApplication(packageName);
94 SortedMap<String, Widget> widgets = app.getWidgets();
95 if (config.widgetId != null) {
96 widget = widgets.get(config.widgetId);
99 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
100 "No widget found with package name " + packageName + " and widget ID " + config.widgetId);
104 widget = widgets.get(widgets.firstKey());
106 } catch (ApplicationNotFoundException e) {
107 logger.debug("LaMetric Time application not found", e);
108 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
109 "No application found with package name " + packageName);
114 protected void updateActiveAppOnDevice() {
115 String widgetId = new WidgetRef(widget.getPackageName(), widget.getId()).toString();
116 ((LaMetricTimeHandler) getBridge().getHandler()).updateActiveApp(widgetId);
119 protected String getPackageName(LaMetricTimeAppConfiguration config) {
120 return config.packageName;
124 public void dispose() {
129 public Widget getWidget() {
130 if (widget == null) {
131 getBridge().getHandler().initialize();
136 protected LaMetricTime getDevice() {
137 return ((LaMetricTimeHandler) getBridge().getHandler()).getClock();
141 public void handleCommand(ChannelUID channelUID, Command command) {
142 logger.debug("Received channel: {}, command: {}", channelUID, command);
145 if (command instanceof RefreshType) {
146 // verify communication
147 getDevice().getApplication(getWidget().getPackageName());
151 handleAppCommand(channelUID, command);
152 } catch (Exception e) {
153 logger.debug("Failed to communicate - taking app offline", e);
154 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
158 protected abstract void handleAppCommand(ChannelUID channelUID, Command command);