]> git.basschouten.com Git - openhab-addons.git/blob
a34b5980e33012c40a68a17e1149624c8eeff6e4
[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.CHANNEL_APP_CONTROL;
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.NextPreviousType;
23 import org.openhab.core.library.types.PlayPauseType;
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.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link RadioAppHandler} represents an instance of the built-in radio app.
34  *
35  * @author Gregory Moyer - Initial contribution
36  */
37 @NonNullByDefault
38 public class RadioAppHandler extends AbstractLaMetricTimeAppHandler {
39     private static final String PACKAGE_NAME = "com.lametric.radio";
40
41     private final Logger logger = LoggerFactory.getLogger(RadioAppHandler.class);
42
43     public RadioAppHandler(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_CONTROL:
52                     handleControl(command);
53                     updateActiveAppOnDevice();
54                     break;
55                 default:
56                     logger.debug("Channel '{}' not supported", channelUID);
57                     break;
58             }
59             updateStatus(ThingStatus.ONLINE);
60         } catch (Exception e) {
61             logger.debug("Failed to perform action - taking app offline", e);
62             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
63         }
64     }
65
66     private void handleControl(final Command command) throws ApplicationActionException {
67         if (command instanceof PlayPauseType) {
68             switch ((PlayPauseType) command) {
69                 case PLAY:
70                     play();
71                     return;
72                 case PAUSE:
73                     stop();
74                     return;
75                 default:
76                     logger.debug("{} command not supported by LaMetric Time Radio App", command);
77                     return;
78             }
79         }
80
81         if (command instanceof NextPreviousType) {
82             switch ((NextPreviousType) command) {
83                 case NEXT:
84                     next();
85                     return;
86                 case PREVIOUS:
87                     previous();
88                     return;
89                 default:
90                     logger.debug("{} command not supported by LaMetric Time Radio App", command);
91                     return;
92             }
93         }
94     }
95
96     private void next() throws ApplicationActionException {
97         getDevice().doAction(getWidget(), CoreApps.radio().next());
98     }
99
100     private void play() throws ApplicationActionException {
101         getDevice().doAction(getWidget(), CoreApps.radio().play());
102     }
103
104     private void previous() throws ApplicationActionException {
105         getDevice().doAction(getWidget(), CoreApps.radio().previous());
106     }
107
108     private void stop() throws ApplicationActionException {
109         getDevice().doAction(getWidget(), CoreApps.radio().stop());
110     }
111
112     @Override
113     protected @Nullable String getPackageName(LaMetricTimeAppConfiguration config) {
114         return PACKAGE_NAME;
115     }
116 }