]> git.basschouten.com Git - openhab-addons.git/blob
3868df1237775becff97504170f418813e8f6d3a
[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 java.time.LocalTime;
18
19 import org.openhab.binding.lametrictime.api.local.ApplicationActionException;
20 import org.openhab.binding.lametrictime.api.model.CoreApps;
21 import org.openhab.binding.lametrictime.internal.config.LaMetricTimeAppConfiguration;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.StringType;
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 ClockAppHandler} represents an instance of the built-in clock app.
34  *
35  * @author Gregory Moyer - Initial contribution
36  */
37 public class ClockAppHandler extends AbstractLaMetricTimeAppHandler {
38     private static final String PACKAGE_NAME = "com.lametric.clock";
39
40     public static final String COMMAND_DISABLE_ALARM = "disableAlarm";
41
42     private final Logger logger = LoggerFactory.getLogger(ClockAppHandler.class);
43
44     public ClockAppHandler(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_SET_ALARM: {
53                     LocalTime time = ((DateTimeType) command).getZonedDateTime().toLocalTime();
54                     getDevice().doAction(getWidget(), CoreApps.clock().setAlarm(true, time, null));
55                     updateActiveAppOnDevice();
56                     break;
57                 }
58                 case CHANNEL_APP_COMMAND:
59                     handleCommandChannel(command);
60                     updateState(channelUID, new StringType()); // clear state
61                     updateActiveAppOnDevice();
62                     break;
63                 default:
64                     logger.debug("Channel '{}' not supported", channelUID);
65                     break;
66             }
67             updateStatus(ThingStatus.ONLINE);
68         } catch (Exception e) {
69             logger.debug("Failed to perform action - taking app offline", e);
70             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
71         }
72     }
73
74     @Override
75     protected String getPackageName(LaMetricTimeAppConfiguration config) {
76         return PACKAGE_NAME;
77     }
78
79     private void handleCommandChannel(Command command) throws ApplicationActionException {
80         String commandStr = command.toFullString();
81         switch (commandStr) {
82             case COMMAND_DISABLE_ALARM:
83                 getDevice().doAction(getWidget(), CoreApps.clock().stopAlarm());
84                 break;
85             default:
86                 logger.debug("Clock app command '{}' not supported", commandStr);
87                 break;
88         }
89     }
90 }