]> git.basschouten.com Git - openhab-addons.git/blob
4c53b9341bfff048e2f4a04ec210040922130541
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lametrictime.internal.handler;
14
15 import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.*;
16
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;
30
31 /**
32  * The {@link CountdownAppHandler} represents an instance of the built-in countdown app.
33  *
34  * @author Gregory Moyer - Initial contribution
35  */
36 @NonNullByDefault
37 public class CountdownAppHandler extends AbstractLaMetricTimeAppHandler {
38     private static final String PACKAGE_NAME = "com.lametric.countdown";
39
40     public static final String COMMAND_PAUSE = "pause";
41     public static final String COMMAND_RESET = "reset";
42     public static final String COMMAND_START = "start";
43
44     private final Logger logger = LoggerFactory.getLogger(CountdownAppHandler.class);
45
46     public CountdownAppHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void handleAppCommand(ChannelUID channelUID, Command command) {
52         try {
53             switch (channelUID.getId()) {
54                 case CHANNEL_APP_DURATION: {
55                     getDevice().doAction(getWidget(),
56                             CoreApps.countdown().configure(((Number) command).intValue(), false));
57                     updateActiveAppOnDevice();
58                     break;
59                 }
60                 case CHANNEL_APP_COMMAND:
61                     handleCommandChannel(command);
62                     updateActiveAppOnDevice();
63                     updateState(channelUID, new StringType()); // clear state
64                     break;
65                 default:
66                     logger.debug("Channel '{}' not supported", channelUID);
67                     break;
68             }
69             updateStatus(ThingStatus.ONLINE);
70         } catch (Exception e) {
71             logger.debug("Failed to perform action - taking app offline", e);
72             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
73         }
74     }
75
76     @Override
77     protected @Nullable String getPackageName(LaMetricTimeAppConfiguration config) {
78         return PACKAGE_NAME;
79     }
80
81     private void handleCommandChannel(Command command) throws ApplicationActionException {
82         String commandStr = command.toFullString();
83         switch (commandStr) {
84             case COMMAND_PAUSE:
85                 getDevice().doAction(getWidget(), CoreApps.countdown().pause());
86                 break;
87             case COMMAND_RESET:
88                 getDevice().doAction(getWidget(), CoreApps.countdown().reset());
89                 break;
90             case COMMAND_START:
91                 getDevice().doAction(getWidget(), CoreApps.countdown().start());
92                 break;
93             default:
94                 logger.debug("Countdown app command '{}' not supported", commandStr);
95                 break;
96         }
97     }
98 }