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