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