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