]> git.basschouten.com Git - openhab-addons.git/blob
e8564004ae856a0f6d2312d406410ea3a4bd0f84
[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_COMMAND;
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 WeatherAppHandler} represents an instance of the built-in weather app.
31  *
32  * @author Gregory Moyer - Initial contribution
33  */
34 public class WeatherAppHandler extends AbstractLaMetricTimeAppHandler {
35     private static final String PACKAGE_NAME = "com.lametric.weather";
36
37     public static final String COMMAND_FORECAST = "forecast";
38
39     private final Logger logger = LoggerFactory.getLogger(WeatherAppHandler.class);
40
41     public WeatherAppHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public void handleAppCommand(ChannelUID channelUID, Command command) {
47         try {
48             switch (channelUID.getId()) {
49                 case CHANNEL_APP_COMMAND:
50                     handleCommandChannel(command);
51                     updateActiveAppOnDevice();
52                     updateState(channelUID, new StringType()); // clear state
53                     break;
54                 default:
55                     logger.debug("Channel '{}' not supported", channelUID);
56                     break;
57             }
58             updateStatus(ThingStatus.ONLINE);
59         } catch (Exception e) {
60             logger.debug("Failed to perform action - taking app offline", e);
61             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
62         }
63     }
64
65     @Override
66     protected String getPackageName(LaMetricTimeAppConfiguration config) {
67         return PACKAGE_NAME;
68     }
69
70     private void handleCommandChannel(Command command) throws ApplicationActionException {
71         String commandStr = command.toFullString();
72         switch (commandStr) {
73             case COMMAND_FORECAST:
74                 getDevice().doAction(getWidget(), CoreApps.weather().forecast());
75                 break;
76             default:
77                 logger.debug("Weather app command '{}' not supported", commandStr);
78                 break;
79         }
80     }
81 }