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 static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.CHANNEL_APP_COMMAND;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lametrictime.internal.api.dto.CoreApps;
20 import org.openhab.binding.lametrictime.internal.api.local.ApplicationActionException;
21 import org.openhab.binding.lametrictime.internal.config.LaMetricTimeAppConfiguration;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.types.Command;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link StopwatchAppHandler} represents an instance of the built-in stopwatch app.
34 * @author Gregory Moyer - Initial contribution
37 public class StopwatchAppHandler extends AbstractLaMetricTimeAppHandler {
38 private static final String PACKAGE_NAME = "com.lametric.stopwatch";
40 public static final String COMMAND_PAUSE = "pause";
41 public static final String COMMAND_RESET = "reset";
42 public static final String COMMAND_START = "start";
44 private final Logger logger = LoggerFactory.getLogger(StopwatchAppHandler.class);
46 public StopwatchAppHandler(Thing thing) {
51 public void handleAppCommand(ChannelUID channelUID, Command command) {
53 switch (channelUID.getId()) {
54 case CHANNEL_APP_COMMAND:
55 handleCommandChannel(command);
56 updateState(channelUID, new StringType()); // clear state
57 updateActiveAppOnDevice();
60 logger.debug("Channel '{}' not supported", channelUID);
63 updateStatus(ThingStatus.ONLINE);
64 } catch (Exception e) {
65 logger.debug("Failed to perform action - taking app offline", e);
66 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
71 protected @Nullable String getPackageName(LaMetricTimeAppConfiguration config) {
75 private void handleCommandChannel(Command command) throws ApplicationActionException {
76 String commandStr = command.toFullString();
79 getDevice().doAction(getWidget(), CoreApps.stopwatch().pause());
82 getDevice().doAction(getWidget(), CoreApps.stopwatch().reset());
85 getDevice().doAction(getWidget(), CoreApps.stopwatch().start());
88 logger.debug("Stopwatch app command '{}' not supported", commandStr);