]> git.basschouten.com Git - openhab-addons.git/blob
8d30b45ea2989eaa197396acfee255ac3cb4de13
[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.openhab.binding.lametrictime.api.local.ApplicationActionException;
18 import org.openhab.binding.lametrictime.api.model.CoreApps;
19 import org.openhab.binding.lametrictime.internal.config.LaMetricTimeAppConfiguration;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.thing.ChannelUID;
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.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link CountdownAppHandler} represents an instance of the built-in countdown app.
31  *
32  * @author Gregory Moyer - Initial contribution
33  */
34 public class CountdownAppHandler extends AbstractLaMetricTimeAppHandler {
35     private static final String PACKAGE_NAME = "com.lametric.countdown";
36
37     public static final String COMMAND_PAUSE = "pause";
38     public static final String COMMAND_RESET = "reset";
39     public static final String COMMAND_START = "start";
40
41     private final Logger logger = LoggerFactory.getLogger(CountdownAppHandler.class);
42
43     public CountdownAppHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void handleAppCommand(ChannelUID channelUID, Command command) {
49         try {
50             switch (channelUID.getId()) {
51                 case CHANNEL_APP_DURATION: {
52                     getDevice().doAction(getWidget(),
53                             CoreApps.countdown().configure(((Number) command).intValue(), false));
54                     updateActiveAppOnDevice();
55                     break;
56                 }
57                 case CHANNEL_APP_COMMAND:
58                     handleCommandChannel(command);
59                     updateActiveAppOnDevice();
60                     updateState(channelUID, new StringType()); // clear state
61                     break;
62                 default:
63                     logger.debug("Channel '{}' not supported", channelUID);
64                     break;
65             }
66             updateStatus(ThingStatus.ONLINE);
67         } catch (Exception e) {
68             logger.debug("Failed to perform action - taking app offline", e);
69             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
70         }
71     }
72
73     @Override
74     protected String getPackageName(LaMetricTimeAppConfiguration config) {
75         return PACKAGE_NAME;
76     }
77
78     private void handleCommandChannel(Command command) throws ApplicationActionException {
79         String commandStr = command.toFullString();
80         switch (commandStr) {
81             case COMMAND_PAUSE:
82                 getDevice().doAction(getWidget(), CoreApps.countdown().pause());
83                 break;
84             case COMMAND_RESET:
85                 getDevice().doAction(getWidget(), CoreApps.countdown().reset());
86                 break;
87             case COMMAND_START:
88                 getDevice().doAction(getWidget(), CoreApps.countdown().start());
89                 break;
90             default:
91                 logger.debug("Countdown app command '{}' not supported", commandStr);
92                 break;
93         }
94     }
95 }