]> git.basschouten.com Git - openhab-addons.git/blob
10884ddaa804fe02439eb0716da4c16fe148eb6e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.bmwconnecteddrive.internal.action;
14
15 import static org.openhab.binding.bmwconnecteddrive.internal.utils.ChargeProfileWrapper.ProfileKey.*;
16
17 import java.time.DayOfWeek;
18 import java.time.LocalTime;
19 import java.util.Optional;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.bmwconnecteddrive.internal.handler.VehicleHandler;
25 import org.openhab.binding.bmwconnecteddrive.internal.utils.ChargeProfileWrapper;
26 import org.openhab.binding.bmwconnecteddrive.internal.utils.ChargeProfileWrapper.ProfileKey;
27 import org.openhab.core.automation.annotation.ActionInput;
28 import org.openhab.core.automation.annotation.ActionOutput;
29 import org.openhab.core.automation.annotation.RuleAction;
30 import org.openhab.core.thing.binding.ThingActions;
31 import org.openhab.core.thing.binding.ThingActionsScope;
32 import org.openhab.core.thing.binding.ThingHandler;
33
34 /**
35  * The {@link BMWConnectedDriveActions} provides actions for VehicleHandler
36  *
37  * @author Norbert Truchsess - Initial contribution
38  */
39 @ThingActionsScope(name = "bmwconnecteddrive")
40 @NonNullByDefault
41 public class BMWConnectedDriveActions implements ThingActions {
42
43     private Optional<VehicleHandler> handler = Optional.empty();
44
45     private Optional<ChargeProfileWrapper> profile = Optional.empty();
46
47     @RuleAction(label = "getTimer1Departure", description = "returns the departure time of timer1")
48     public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getTimer1Departure() {
49         return getTime(TIMER1);
50     }
51
52     @RuleAction(label = "setTimer1Departure", description = "sets the timer1 departure time")
53     public void setTimer1Departure(@ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
54         setTime(TIMER1, time);
55     }
56
57     @RuleAction(label = "getTimer1Enabled", description = "returns the enabled state of timer1")
58     public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getTimer1Enabled() {
59         return getEnabled(TIMER1);
60     }
61
62     @RuleAction(label = "setTimer1Enabled", description = "sets the enabled state of timer1")
63     public void setTimer1Enabled(@ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
64         setEnabled(TIMER1, enabled);
65     }
66
67     @RuleAction(label = "getTimer2Departure", description = "returns the departure time of timer2")
68     public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getTimer2Departure() {
69         return getTime(TIMER2);
70     }
71
72     @RuleAction(label = "setTimer2Departure", description = "sets the timer2 departure time")
73     public void setTimer2Departure(@ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
74         setTime(TIMER2, time);
75     }
76
77     @RuleAction(label = "getTimer2Enabled", description = "returns the enabled state of timer2")
78     public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getTimer2Enabled() {
79         return getEnabled(TIMER2);
80     }
81
82     @RuleAction(label = "setTimer2Enabled", description = "sets the enabled state of timer2")
83     public void setTimer2Enabled(@ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
84         setEnabled(TIMER2, enabled);
85     }
86
87     @RuleAction(label = "getTimer3Departure", description = "returns the departure time of timer3")
88     public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getTimer3Departure() {
89         return getTime(TIMER3);
90     }
91
92     @RuleAction(label = "setTimer3Departure", description = "sets the timer3 departure time")
93     public void setTimer3Departure(@ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
94         setTime(TIMER3, time);
95     }
96
97     @RuleAction(label = "getTimer3Enabled", description = "returns the enabled state of timer3")
98     public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getTimer3Enabled() {
99         return getEnabled(TIMER3);
100     }
101
102     @RuleAction(label = "setTimer3Enabled", description = "sets the enabled state of timer3")
103     public void setTimer3Enabled(@ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
104         setEnabled(TIMER3, enabled);
105     }
106
107     @RuleAction(label = "getOverrideTimerDeparture", description = "returns the departure time of overrideTimer")
108     public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getOverrideTimerDeparture() {
109         return getTime(OVERRIDE);
110     }
111
112     @RuleAction(label = "setOverrideTimerDeparture", description = "sets the overrideTimer departure time")
113     public void setOverrideTimerDeparture(
114             @ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
115         setTime(OVERRIDE, time);
116     }
117
118     @RuleAction(label = "getOverrideTimerEnabled", description = "returns the enabled state of overrideTimer")
119     public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getOverrideTimerEnabled() {
120         return getEnabled(OVERRIDE);
121     }
122
123     @RuleAction(label = "setOverrideTimerEnabled", description = "sets the enabled state of overrideTimer")
124     public void setOverrideTimerEnabled(
125             @ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
126         setEnabled(OVERRIDE, enabled);
127     }
128
129     @RuleAction(label = "getPreferredWindowStart", description = "returns the preferred charging-window start time")
130     public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getPreferredWindowStart() {
131         return getTime(WINDOWSTART);
132     }
133
134     @RuleAction(label = "setPreferredWindowStart", description = "sets the preferred charging-window start time")
135     public void setPreferredWindowStart(
136             @ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
137         setTime(WINDOWSTART, time);
138     }
139
140     @RuleAction(label = "getPreferredWindowEnd", description = "returns the preferred charging-window end time")
141     public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getPreferredWindowEnd() {
142         return getTime(WINDOWEND);
143     }
144
145     @RuleAction(label = "setPreferredWindowEnd", description = "sets the preferred charging-window end time")
146     public void setPreferredWindowEnd(
147             @ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
148         setTime(WINDOWEND, time);
149     }
150
151     @RuleAction(label = "getClimatizationEnabled", description = "returns the enabled state of climatization")
152     public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getClimatizationEnabled() {
153         return getEnabled(CLIMATE);
154     }
155
156     @RuleAction(label = "setClimatizationEnabled", description = "sets the enabled state of climatization")
157     public void setClimatizationEnabled(
158             @ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
159         setEnabled(CLIMATE, enabled);
160     }
161
162     @RuleAction(label = "getChargingMode", description = "gets the charging-mode")
163     public @ActionOutput(name = "mode", type = "java.util.Optional<java.lang.String>") Optional<String> getChargingMode() {
164         return getProfile().map(profile -> profile.getMode());
165     }
166
167     @RuleAction(label = "setChargingMode", description = "sets the charging-mode")
168     public void setChargingMode(@ActionInput(name = "mode", type = "java.lang.String") @Nullable String mode) {
169         getProfile().ifPresent(profile -> profile.setMode(mode));
170     }
171
172     @RuleAction(label = "getTimer1Days", description = "returns the days of week timer1 is enabled for")
173     public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getTimer1Days() {
174         return getDays(TIMER1);
175     }
176
177     @RuleAction(label = "setTimer1Days", description = "sets the days of week timer1 is enabled for")
178     public void setTimer1Days(
179             @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
180         setDays(TIMER1, days);
181     }
182
183     @RuleAction(label = "getTimer2Days", description = "returns the days of week timer2 is enabled for")
184     public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getTimer2Days() {
185         return getDays(TIMER2);
186     }
187
188     @RuleAction(label = "setTimer2Days", description = "sets the days of week timer2 is enabled for")
189     public void setTimer2Days(
190             @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
191         setDays(TIMER2, days);
192     }
193
194     @RuleAction(label = "getTimer3Days", description = "returns the days of week timer3 is enabled for")
195     public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getTimer3Days() {
196         return getDays(TIMER3);
197     }
198
199     @RuleAction(label = "setTimer3Days", description = "sets the days of week timer3 is enabled for")
200     public void setTimer3Days(
201             @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
202         setDays(TIMER3, days);
203     }
204
205     @RuleAction(label = "getOverrideTimerDays", description = "returns the days of week the overrideTimer is enabled for")
206     public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getOverrideTimerDays() {
207         return getDays(OVERRIDE);
208     }
209
210     @RuleAction(label = "setOverrideTimerDays", description = "sets the days of week the overrideTimer is enabled for")
211     public void setOverrideTimerDays(
212             @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
213         setDays(OVERRIDE, days);
214     }
215
216     @RuleAction(label = "sendChargeProfile", description = "sends the charging profile to the vehicle")
217     public void sendChargeProfile() {
218         handler.ifPresent(handle -> handle.sendChargeProfile(getProfile()));
219     }
220
221     @RuleAction(label = "cancel", description = "cancel current edit of charging profile")
222     public void cancelEditChargeProfile() {
223         profile = Optional.empty();
224     }
225
226     public static Optional<LocalTime> getTimer1Departure(ThingActions actions) {
227         return ((BMWConnectedDriveActions) actions).getTimer1Departure();
228     }
229
230     public static void setTimer1Departure(ThingActions actions, @Nullable LocalTime time) {
231         ((BMWConnectedDriveActions) actions).setTimer1Departure(time);
232     }
233
234     public static Optional<Boolean> getTimer1Enabled(ThingActions actions) {
235         return ((BMWConnectedDriveActions) actions).getTimer1Enabled();
236     }
237
238     public static void setTimer1Enabled(ThingActions actions, @Nullable Boolean enabled) {
239         ((BMWConnectedDriveActions) actions).setTimer1Enabled(enabled);
240     }
241
242     public static Optional<LocalTime> getTimer2Departure(ThingActions actions) {
243         return ((BMWConnectedDriveActions) actions).getTimer2Departure();
244     }
245
246     public static void setTimer2Departure(ThingActions actions, @Nullable LocalTime time) {
247         ((BMWConnectedDriveActions) actions).setTimer2Departure(time);
248     }
249
250     public static Optional<Boolean> getTimer2Enabled(ThingActions actions) {
251         return ((BMWConnectedDriveActions) actions).getTimer2Enabled();
252     }
253
254     public static void setTimer2Enabled(ThingActions actions, @Nullable Boolean enabled) {
255         ((BMWConnectedDriveActions) actions).setTimer2Enabled(enabled);
256     }
257
258     public static Optional<LocalTime> getTimer3Departure(ThingActions actions) {
259         return ((BMWConnectedDriveActions) actions).getTimer3Departure();
260     }
261
262     public static void setTimer3Departure(ThingActions actions, @Nullable LocalTime time) {
263         ((BMWConnectedDriveActions) actions).setTimer3Departure(time);
264     }
265
266     public static Optional<Boolean> getTimer3Enabled(ThingActions actions) {
267         return ((BMWConnectedDriveActions) actions).getTimer3Enabled();
268     }
269
270     public static void setTimer3Enabled(ThingActions actions, @Nullable Boolean enabled) {
271         ((BMWConnectedDriveActions) actions).setTimer3Enabled(enabled);
272     }
273
274     public static Optional<LocalTime> getOverrideTimerDeparture(ThingActions actions) {
275         return ((BMWConnectedDriveActions) actions).getOverrideTimerDeparture();
276     }
277
278     public static void setOverrideTimerDeparture(ThingActions actions, @Nullable LocalTime time) {
279         ((BMWConnectedDriveActions) actions).setOverrideTimerDeparture(time);
280     }
281
282     public static Optional<Boolean> getOverrideTimerEnabled(ThingActions actions) {
283         return ((BMWConnectedDriveActions) actions).getOverrideTimerEnabled();
284     }
285
286     public static void setOverrideTimerEnabled(ThingActions actions, @Nullable Boolean enabled) {
287         ((BMWConnectedDriveActions) actions).setOverrideTimerEnabled(enabled);
288     }
289
290     public static Optional<LocalTime> getPreferredWindowStart(ThingActions actions) {
291         return ((BMWConnectedDriveActions) actions).getPreferredWindowStart();
292     }
293
294     public static void setPreferredWindowStart(ThingActions actions, @Nullable LocalTime time) {
295         ((BMWConnectedDriveActions) actions).setPreferredWindowStart(time);
296     }
297
298     public static Optional<LocalTime> getPreferredWindowEnd(ThingActions actions) {
299         return ((BMWConnectedDriveActions) actions).getPreferredWindowEnd();
300     }
301
302     public static void setPreferredWindowEnd(ThingActions actions, @Nullable LocalTime time) {
303         ((BMWConnectedDriveActions) actions).setPreferredWindowEnd(time);
304     }
305
306     public static Optional<Boolean> getClimatizationEnabled(ThingActions actions) {
307         return ((BMWConnectedDriveActions) actions).getClimatizationEnabled();
308     }
309
310     public static void setClimatizationEnabled(ThingActions actions, @Nullable Boolean enabled) {
311         ((BMWConnectedDriveActions) actions).setClimatizationEnabled(enabled);
312     }
313
314     public static Optional<String> getChargingMode(ThingActions actions) {
315         return ((BMWConnectedDriveActions) actions).getChargingMode();
316     }
317
318     public static void setChargingMode(ThingActions actions, @Nullable String mode) {
319         ((BMWConnectedDriveActions) actions).setChargingMode(mode);
320     }
321
322     public static Optional<Set<DayOfWeek>> getTimer1Days(ThingActions actions) {
323         return ((BMWConnectedDriveActions) actions).getTimer1Days();
324     }
325
326     public static void setTimer1Days(ThingActions actions, @Nullable Set<DayOfWeek> days) {
327         ((BMWConnectedDriveActions) actions).setTimer1Days(days);
328     }
329
330     public static Optional<Set<DayOfWeek>> getTimer2Days(ThingActions actions) {
331         return ((BMWConnectedDriveActions) actions).getTimer2Days();
332     }
333
334     public static void setTimer2Days(ThingActions actions, @Nullable Set<DayOfWeek> days) {
335         ((BMWConnectedDriveActions) actions).setTimer2Days(days);
336     }
337
338     public static Optional<Set<DayOfWeek>> getTimer3Days(ThingActions actions) {
339         return ((BMWConnectedDriveActions) actions).getTimer3Days();
340     }
341
342     public static void setTimer3Days(ThingActions actions, @Nullable Set<DayOfWeek> days) {
343         ((BMWConnectedDriveActions) actions).setTimer3Days(days);
344     }
345
346     public static Optional<Set<DayOfWeek>> getOverrideTimerDays(ThingActions actions) {
347         return ((BMWConnectedDriveActions) actions).getOverrideTimerDays();
348     }
349
350     public static void setOverrideTimerDays(ThingActions actions, @Nullable Set<DayOfWeek> days) {
351         ((BMWConnectedDriveActions) actions).setOverrideTimerDays(days);
352     }
353
354     public static void sendChargeProfile(ThingActions actions) {
355         ((BMWConnectedDriveActions) actions).sendChargeProfile();
356     }
357
358     public static void cancelEditChargeProfile(ThingActions actions) {
359         ((BMWConnectedDriveActions) actions).cancelEditChargeProfile();
360     }
361
362     @Override
363     public void setThingHandler(@Nullable ThingHandler handler) {
364         if (handler instanceof VehicleHandler) {
365             this.handler = Optional.of((VehicleHandler) handler);
366         }
367     }
368
369     @Override
370     public @Nullable ThingHandler getThingHandler() {
371         return handler.get();
372     }
373
374     private Optional<ChargeProfileWrapper> getProfile() {
375         if (profile.isEmpty()) {
376             profile = handler.flatMap(handle -> handle.getChargeProfileWrapper());
377         }
378         return profile;
379     }
380
381     private Optional<LocalTime> getTime(ProfileKey key) {
382         return getProfile().map(profile -> profile.getTime(key));
383     }
384
385     private void setTime(ProfileKey key, @Nullable LocalTime time) {
386         getProfile().ifPresent(profile -> profile.setTime(key, time));
387     }
388
389     private Optional<Boolean> getEnabled(ProfileKey key) {
390         return getProfile().map(profile -> profile.isEnabled(key));
391     }
392
393     private void setEnabled(ProfileKey key, @Nullable Boolean enabled) {
394         getProfile().ifPresent(profile -> profile.setEnabled(key, enabled));
395     }
396
397     private Optional<Set<DayOfWeek>> getDays(ProfileKey key) {
398         return getProfile().map(profile -> profile.getDays(key));
399     }
400
401     private void setDays(ProfileKey key, @Nullable Set<DayOfWeek> days) {
402         getProfile().ifPresent(profile -> {
403             profile.setDays(key, days);
404         });
405     }
406 }