]> git.basschouten.com Git - openhab-addons.git/blob
b075fac55212908b3b007eeb4a77aab3fdb51613
[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.api.dto;
14
15 import java.time.LocalTime;
16 import java.time.format.DateTimeFormatter;
17 import java.util.SortedMap;
18 import java.util.TreeMap;
19
20 import org.openhab.binding.lametrictime.internal.api.local.dto.BooleanParameter;
21 import org.openhab.binding.lametrictime.internal.api.local.dto.Parameter;
22 import org.openhab.binding.lametrictime.internal.api.local.dto.StringParameter;
23 import org.openhab.binding.lametrictime.internal.api.local.dto.UpdateAction;
24
25 /**
26  * Implementation class for the ClockApp.
27  *
28  * @author Gregory Moyer - Initial contribution
29  */
30 public class ClockApp extends CoreApplication {
31     private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
32
33     private static final String NAME = "com.lametric.clock";
34
35     private static final String ACTION_ALARM = "clock.alarm";
36
37     private static final String PARAMETER_ENABLED = "enabled";
38     private static final String PARAMETER_TIME = "time";
39     private static final String PARAMETER_WAKE_WITH_RADIO = "wake_with_radio";
40
41     public ClockApp() {
42         super(NAME);
43     }
44
45     public CoreAction setAlarm(Boolean enabled, LocalTime time, Boolean wakeWithRadio) {
46         SortedMap<String, Parameter> parameters = new TreeMap<>();
47
48         if (enabled != null) {
49             parameters.put(PARAMETER_ENABLED, new BooleanParameter().withValue(enabled));
50         }
51
52         if (time != null) {
53             parameters.put(PARAMETER_TIME, new StringParameter().withValue(time.format(TIME_FORMATTER)));
54         }
55
56         if (wakeWithRadio != null) {
57             parameters.put(PARAMETER_WAKE_WITH_RADIO, new BooleanParameter().withValue(wakeWithRadio));
58         }
59
60         return new CoreAction(this, new UpdateAction().withId(ACTION_ALARM).withParameters(parameters));
61     }
62
63     public CoreAction stopAlarm() {
64         SortedMap<String, Parameter> parameters = new TreeMap<>();
65         parameters.put(PARAMETER_ENABLED, new BooleanParameter().withValue(false));
66
67         return new CoreAction(this, new UpdateAction().withId(ACTION_ALARM).withParameters(parameters));
68     }
69 }