]> git.basschouten.com Git - openhab-addons.git/blob
2483e00ad50ffb411b02065b4a13973c8c69e805
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.lametrictime.api.model;
17
18 import java.time.LocalTime;
19 import java.time.format.DateTimeFormatter;
20 import java.util.SortedMap;
21 import java.util.TreeMap;
22
23 import org.openhab.binding.lametrictime.api.local.model.BooleanParameter;
24 import org.openhab.binding.lametrictime.api.local.model.Parameter;
25 import org.openhab.binding.lametrictime.api.local.model.StringParameter;
26 import org.openhab.binding.lametrictime.api.local.model.UpdateAction;
27
28 public class ClockApp extends CoreApplication
29 {
30     private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
31
32     private static final String NAME = "com.lametric.clock";
33
34     private static final String ACTION_ALARM = "clock.alarm";
35
36     private static final String PARAMETER_ENABLED = "enabled";
37     private static final String PARAMETER_TIME = "time";
38     private static final String PARAMETER_WAKE_WITH_RADIO = "wake_with_radio";
39
40     public ClockApp()
41     {
42         super(NAME);
43     }
44
45     public CoreAction setAlarm(Boolean enabled, LocalTime time, Boolean wakeWithRadio)
46     {
47         SortedMap<String, Parameter> parameters = new TreeMap<>();
48
49         if (enabled != null)
50         {
51             parameters.put(PARAMETER_ENABLED, new BooleanParameter().withValue(enabled));
52         }
53
54         if (time != null)
55         {
56             parameters.put(PARAMETER_TIME,
57                            new StringParameter().withValue(time.format(TIME_FORMATTER)));
58         }
59
60         if (wakeWithRadio != null)
61         {
62             parameters.put(PARAMETER_WAKE_WITH_RADIO,
63                            new BooleanParameter().withValue(wakeWithRadio));
64         }
65
66         return new CoreAction(this,
67                               new UpdateAction().withId(ACTION_ALARM).withParameters(parameters));
68     }
69
70     public CoreAction stopAlarm()
71     {
72         SortedMap<String, Parameter> parameters = new TreeMap<>();
73         parameters.put(PARAMETER_ENABLED, new BooleanParameter().withValue(false));
74
75         return new CoreAction(this,
76                               new UpdateAction().withId(ACTION_ALARM).withParameters(parameters));
77     }
78 }